Merge branch 'packaging' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek...
[pandora-kernel.git] / drivers / usb / gadget / dummy_hcd.c
1 /*
2  * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
3  *
4  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5  *
6  * Copyright (C) 2003 David Brownell
7  * Copyright (C) 2003-2005 Alan Stern
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24
25 /*
26  * This exposes a device side "USB gadget" API, driven by requests to a
27  * Linux-USB host controller driver.  USB traffic is simulated; there's
28  * no need for USB hardware.  Use this with two other drivers:
29  *
30  *  - Gadget driver, responding to requests (slave);
31  *  - Host-side device driver, as already familiar in Linux.
32  *
33  * Having this all in one kernel can help some stages of development,
34  * bypassing some hardware (and driver) issues.  UML could help too.
35  */
36
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/delay.h>
40 #include <linux/ioport.h>
41 #include <linux/slab.h>
42 #include <linux/errno.h>
43 #include <linux/init.h>
44 #include <linux/timer.h>
45 #include <linux/list.h>
46 #include <linux/interrupt.h>
47 #include <linux/platform_device.h>
48 #include <linux/usb.h>
49 #include <linux/usb/gadget.h>
50 #include <linux/usb/hcd.h>
51
52 #include <asm/byteorder.h>
53 #include <asm/io.h>
54 #include <asm/irq.h>
55 #include <asm/system.h>
56 #include <asm/unaligned.h>
57
58
59 #define DRIVER_DESC     "USB Host+Gadget Emulator"
60 #define DRIVER_VERSION  "02 May 2005"
61
62 #define POWER_BUDGET    500     /* in mA; use 8 for low-power port testing */
63
64 static const char       driver_name [] = "dummy_hcd";
65 static const char       driver_desc [] = "USB Host+Gadget Emulator";
66
67 static const char       gadget_name [] = "dummy_udc";
68
69 MODULE_DESCRIPTION (DRIVER_DESC);
70 MODULE_AUTHOR ("David Brownell");
71 MODULE_LICENSE ("GPL");
72
73 /*-------------------------------------------------------------------------*/
74
75 /* gadget side driver data structres */
76 struct dummy_ep {
77         struct list_head                queue;
78         unsigned long                   last_io;        /* jiffies timestamp */
79         struct usb_gadget               *gadget;
80         const struct usb_endpoint_descriptor *desc;
81         struct usb_ep                   ep;
82         unsigned                        halted : 1;
83         unsigned                        wedged : 1;
84         unsigned                        already_seen : 1;
85         unsigned                        setup_stage : 1;
86 };
87
88 struct dummy_request {
89         struct list_head                queue;          /* ep's requests */
90         struct usb_request              req;
91 };
92
93 static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
94 {
95         return container_of (_ep, struct dummy_ep, ep);
96 }
97
98 static inline struct dummy_request *usb_request_to_dummy_request
99                 (struct usb_request *_req)
100 {
101         return container_of (_req, struct dummy_request, req);
102 }
103
104 /*-------------------------------------------------------------------------*/
105
106 /*
107  * Every device has ep0 for control requests, plus up to 30 more endpoints,
108  * in one of two types:
109  *
110  *   - Configurable:  direction (in/out), type (bulk, iso, etc), and endpoint
111  *     number can be changed.  Names like "ep-a" are used for this type.
112  *
113  *   - Fixed Function:  in other cases.  some characteristics may be mutable;
114  *     that'd be hardware-specific.  Names like "ep12out-bulk" are used.
115  *
116  * Gadget drivers are responsible for not setting up conflicting endpoint
117  * configurations, illegal or unsupported packet lengths, and so on.
118  */
119
120 static const char ep0name [] = "ep0";
121
122 static const char *const ep_name [] = {
123         ep0name,                                /* everyone has ep0 */
124
125         /* act like a net2280: high speed, six configurable endpoints */
126         "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
127
128         /* or like pxa250: fifteen fixed function endpoints */
129         "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
130         "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
131         "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
132                 "ep15in-int",
133
134         /* or like sa1100: two fixed function endpoints */
135         "ep1out-bulk", "ep2in-bulk",
136 };
137 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
138
139 /*-------------------------------------------------------------------------*/
140
141 #define FIFO_SIZE               64
142
143 struct urbp {
144         struct urb              *urb;
145         struct list_head        urbp_list;
146 };
147
148
149 enum dummy_rh_state {
150         DUMMY_RH_RESET,
151         DUMMY_RH_SUSPENDED,
152         DUMMY_RH_RUNNING
153 };
154
155 struct dummy {
156         spinlock_t                      lock;
157
158         /*
159          * SLAVE/GADGET side support
160          */
161         struct dummy_ep                 ep [DUMMY_ENDPOINTS];
162         int                             address;
163         struct usb_gadget               gadget;
164         struct usb_gadget_driver        *driver;
165         struct dummy_request            fifo_req;
166         u8                              fifo_buf [FIFO_SIZE];
167         u16                             devstatus;
168         unsigned                        udc_suspended:1;
169         unsigned                        pullup:1;
170         unsigned                        active:1;
171         unsigned                        old_active:1;
172
173         /*
174          * MASTER/HOST side support
175          */
176         enum dummy_rh_state             rh_state;
177         struct timer_list               timer;
178         u32                             port_status;
179         u32                             old_status;
180         unsigned                        resuming:1;
181         unsigned long                   re_timeout;
182
183         struct usb_device               *udev;
184         struct list_head                urbp_list;
185 };
186
187 static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
188 {
189         return (struct dummy *) (hcd->hcd_priv);
190 }
191
192 static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
193 {
194         return container_of((void *) dum, struct usb_hcd, hcd_priv);
195 }
196
197 static inline struct device *dummy_dev (struct dummy *dum)
198 {
199         return dummy_to_hcd(dum)->self.controller;
200 }
201
202 static inline struct device *udc_dev (struct dummy *dum)
203 {
204         return dum->gadget.dev.parent;
205 }
206
207 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
208 {
209         return container_of (ep->gadget, struct dummy, gadget);
210 }
211
212 static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
213 {
214         return container_of (gadget, struct dummy, gadget);
215 }
216
217 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
218 {
219         return container_of (dev, struct dummy, gadget.dev);
220 }
221
222 static struct dummy                     *the_controller;
223
224 /*-------------------------------------------------------------------------*/
225
226 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
227
228 /* called with spinlock held */
229 static void nuke (struct dummy *dum, struct dummy_ep *ep)
230 {
231         while (!list_empty (&ep->queue)) {
232                 struct dummy_request    *req;
233
234                 req = list_entry (ep->queue.next, struct dummy_request, queue);
235                 list_del_init (&req->queue);
236                 req->req.status = -ESHUTDOWN;
237
238                 spin_unlock (&dum->lock);
239                 req->req.complete (&ep->ep, &req->req);
240                 spin_lock (&dum->lock);
241         }
242 }
243
244 /* caller must hold lock */
245 static void
246 stop_activity (struct dummy *dum)
247 {
248         struct dummy_ep *ep;
249
250         /* prevent any more requests */
251         dum->address = 0;
252
253         /* The timer is left running so that outstanding URBs can fail */
254
255         /* nuke any pending requests first, so driver i/o is quiesced */
256         list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
257                 nuke (dum, ep);
258
259         /* driver now does any non-usb quiescing necessary */
260 }
261
262 /* caller must hold lock */
263 static void
264 set_link_state (struct dummy *dum)
265 {
266         dum->active = 0;
267         if ((dum->port_status & USB_PORT_STAT_POWER) == 0)
268                 dum->port_status = 0;
269
270         /* UDC suspend must cause a disconnect */
271         else if (!dum->pullup || dum->udc_suspended) {
272                 dum->port_status &= ~(USB_PORT_STAT_CONNECTION |
273                                         USB_PORT_STAT_ENABLE |
274                                         USB_PORT_STAT_LOW_SPEED |
275                                         USB_PORT_STAT_HIGH_SPEED |
276                                         USB_PORT_STAT_SUSPEND);
277                 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0)
278                         dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
279         } else {
280                 dum->port_status |= USB_PORT_STAT_CONNECTION;
281                 if ((dum->old_status & USB_PORT_STAT_CONNECTION) == 0)
282                         dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
283                 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0)
284                         dum->port_status &= ~USB_PORT_STAT_SUSPEND;
285                 else if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
286                                 dum->rh_state != DUMMY_RH_SUSPENDED)
287                         dum->active = 1;
288         }
289
290         if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0 || dum->active)
291                 dum->resuming = 0;
292
293         if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
294                         (dum->port_status & USB_PORT_STAT_RESET) != 0) {
295                 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
296                                 (dum->old_status & USB_PORT_STAT_RESET) == 0 &&
297                                 dum->driver) {
298                         stop_activity (dum);
299                         spin_unlock (&dum->lock);
300                         dum->driver->disconnect (&dum->gadget);
301                         spin_lock (&dum->lock);
302                 }
303         } else if (dum->active != dum->old_active) {
304                 if (dum->old_active && dum->driver->suspend) {
305                         spin_unlock (&dum->lock);
306                         dum->driver->suspend (&dum->gadget);
307                         spin_lock (&dum->lock);
308                 } else if (!dum->old_active && dum->driver->resume) {
309                         spin_unlock (&dum->lock);
310                         dum->driver->resume (&dum->gadget);
311                         spin_lock (&dum->lock);
312                 }
313         }
314
315         dum->old_status = dum->port_status;
316         dum->old_active = dum->active;
317 }
318
319 /*-------------------------------------------------------------------------*/
320
321 /* SLAVE/GADGET SIDE DRIVER
322  *
323  * This only tracks gadget state.  All the work is done when the host
324  * side tries some (emulated) i/o operation.  Real device controller
325  * drivers would do real i/o using dma, fifos, irqs, timers, etc.
326  */
327
328 #define is_enabled(dum) \
329         (dum->port_status & USB_PORT_STAT_ENABLE)
330
331 static int
332 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
333 {
334         struct dummy            *dum;
335         struct dummy_ep         *ep;
336         unsigned                max;
337         int                     retval;
338
339         ep = usb_ep_to_dummy_ep (_ep);
340         if (!_ep || !desc || ep->desc || _ep->name == ep0name
341                         || desc->bDescriptorType != USB_DT_ENDPOINT)
342                 return -EINVAL;
343         dum = ep_to_dummy (ep);
344         if (!dum->driver || !is_enabled (dum))
345                 return -ESHUTDOWN;
346         max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
347
348         /* drivers must not request bad settings, since lower levels
349          * (hardware or its drivers) may not check.  some endpoints
350          * can't do iso, many have maxpacket limitations, etc.
351          *
352          * since this "hardware" driver is here to help debugging, we
353          * have some extra sanity checks.  (there could be more though,
354          * especially for "ep9out" style fixed function ones.)
355          */
356         retval = -EINVAL;
357         switch (desc->bmAttributes & 0x03) {
358         case USB_ENDPOINT_XFER_BULK:
359                 if (strstr (ep->ep.name, "-iso")
360                                 || strstr (ep->ep.name, "-int")) {
361                         goto done;
362                 }
363                 switch (dum->gadget.speed) {
364                 case USB_SPEED_HIGH:
365                         if (max == 512)
366                                 break;
367                         goto done;
368                 case USB_SPEED_FULL:
369                         if (max == 8 || max == 16 || max == 32 || max == 64)
370                                 /* we'll fake any legal size */
371                                 break;
372                         /* save a return statement */
373                 default:
374                         goto done;
375                 }
376                 break;
377         case USB_ENDPOINT_XFER_INT:
378                 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
379                         goto done;
380                 /* real hardware might not handle all packet sizes */
381                 switch (dum->gadget.speed) {
382                 case USB_SPEED_HIGH:
383                         if (max <= 1024)
384                                 break;
385                         /* save a return statement */
386                 case USB_SPEED_FULL:
387                         if (max <= 64)
388                                 break;
389                         /* save a return statement */
390                 default:
391                         if (max <= 8)
392                                 break;
393                         goto done;
394                 }
395                 break;
396         case USB_ENDPOINT_XFER_ISOC:
397                 if (strstr (ep->ep.name, "-bulk")
398                                 || strstr (ep->ep.name, "-int"))
399                         goto done;
400                 /* real hardware might not handle all packet sizes */
401                 switch (dum->gadget.speed) {
402                 case USB_SPEED_HIGH:
403                         if (max <= 1024)
404                                 break;
405                         /* save a return statement */
406                 case USB_SPEED_FULL:
407                         if (max <= 1023)
408                                 break;
409                         /* save a return statement */
410                 default:
411                         goto done;
412                 }
413                 break;
414         default:
415                 /* few chips support control except on ep0 */
416                 goto done;
417         }
418
419         _ep->maxpacket = max;
420         ep->desc = desc;
421
422         dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
423                 _ep->name,
424                 desc->bEndpointAddress & 0x0f,
425                 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
426                 ({ char *val;
427                  switch (desc->bmAttributes & 0x03) {
428                  case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
429                  case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
430                  case USB_ENDPOINT_XFER_INT: val = "intr"; break;
431                  default: val = "ctrl"; break;
432                  }; val; }),
433                 max);
434
435         /* at this point real hardware should be NAKing transfers
436          * to that endpoint, until a buffer is queued to it.
437          */
438         ep->halted = ep->wedged = 0;
439         retval = 0;
440 done:
441         return retval;
442 }
443
444 static int dummy_disable (struct usb_ep *_ep)
445 {
446         struct dummy_ep         *ep;
447         struct dummy            *dum;
448         unsigned long           flags;
449         int                     retval;
450
451         ep = usb_ep_to_dummy_ep (_ep);
452         if (!_ep || !ep->desc || _ep->name == ep0name)
453                 return -EINVAL;
454         dum = ep_to_dummy (ep);
455
456         spin_lock_irqsave (&dum->lock, flags);
457         ep->desc = NULL;
458         retval = 0;
459         nuke (dum, ep);
460         spin_unlock_irqrestore (&dum->lock, flags);
461
462         dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
463         return retval;
464 }
465
466 static struct usb_request *
467 dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
468 {
469         struct dummy_ep         *ep;
470         struct dummy_request    *req;
471
472         if (!_ep)
473                 return NULL;
474         ep = usb_ep_to_dummy_ep (_ep);
475
476         req = kzalloc(sizeof(*req), mem_flags);
477         if (!req)
478                 return NULL;
479         INIT_LIST_HEAD (&req->queue);
480         return &req->req;
481 }
482
483 static void
484 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
485 {
486         struct dummy_ep         *ep;
487         struct dummy_request    *req;
488
489         ep = usb_ep_to_dummy_ep (_ep);
490         if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
491                 return;
492
493         req = usb_request_to_dummy_request (_req);
494         WARN_ON (!list_empty (&req->queue));
495         kfree (req);
496 }
497
498 static void
499 fifo_complete (struct usb_ep *ep, struct usb_request *req)
500 {
501 }
502
503 static int
504 dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
505                 gfp_t mem_flags)
506 {
507         struct dummy_ep         *ep;
508         struct dummy_request    *req;
509         struct dummy            *dum;
510         unsigned long           flags;
511
512         req = usb_request_to_dummy_request (_req);
513         if (!_req || !list_empty (&req->queue) || !_req->complete)
514                 return -EINVAL;
515
516         ep = usb_ep_to_dummy_ep (_ep);
517         if (!_ep || (!ep->desc && _ep->name != ep0name))
518                 return -EINVAL;
519
520         dum = ep_to_dummy (ep);
521         if (!dum->driver || !is_enabled (dum))
522                 return -ESHUTDOWN;
523
524 #if 0
525         dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
526                         ep, _req, _ep->name, _req->length, _req->buf);
527 #endif
528
529         _req->status = -EINPROGRESS;
530         _req->actual = 0;
531         spin_lock_irqsave (&dum->lock, flags);
532
533         /* implement an emulated single-request FIFO */
534         if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
535                         list_empty (&dum->fifo_req.queue) &&
536                         list_empty (&ep->queue) &&
537                         _req->length <= FIFO_SIZE) {
538                 req = &dum->fifo_req;
539                 req->req = *_req;
540                 req->req.buf = dum->fifo_buf;
541                 memcpy (dum->fifo_buf, _req->buf, _req->length);
542                 req->req.context = dum;
543                 req->req.complete = fifo_complete;
544
545                 list_add_tail(&req->queue, &ep->queue);
546                 spin_unlock (&dum->lock);
547                 _req->actual = _req->length;
548                 _req->status = 0;
549                 _req->complete (_ep, _req);
550                 spin_lock (&dum->lock);
551         }  else
552                 list_add_tail(&req->queue, &ep->queue);
553         spin_unlock_irqrestore (&dum->lock, flags);
554
555         /* real hardware would likely enable transfers here, in case
556          * it'd been left NAKing.
557          */
558         return 0;
559 }
560
561 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
562 {
563         struct dummy_ep         *ep;
564         struct dummy            *dum;
565         int                     retval = -EINVAL;
566         unsigned long           flags;
567         struct dummy_request    *req = NULL;
568
569         if (!_ep || !_req)
570                 return retval;
571         ep = usb_ep_to_dummy_ep (_ep);
572         dum = ep_to_dummy (ep);
573
574         if (!dum->driver)
575                 return -ESHUTDOWN;
576
577         local_irq_save (flags);
578         spin_lock (&dum->lock);
579         list_for_each_entry (req, &ep->queue, queue) {
580                 if (&req->req == _req) {
581                         list_del_init (&req->queue);
582                         _req->status = -ECONNRESET;
583                         retval = 0;
584                         break;
585                 }
586         }
587         spin_unlock (&dum->lock);
588
589         if (retval == 0) {
590                 dev_dbg (udc_dev(dum),
591                                 "dequeued req %p from %s, len %d buf %p\n",
592                                 req, _ep->name, _req->length, _req->buf);
593                 _req->complete (_ep, _req);
594         }
595         local_irq_restore (flags);
596         return retval;
597 }
598
599 static int
600 dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
601 {
602         struct dummy_ep         *ep;
603         struct dummy            *dum;
604
605         if (!_ep)
606                 return -EINVAL;
607         ep = usb_ep_to_dummy_ep (_ep);
608         dum = ep_to_dummy (ep);
609         if (!dum->driver)
610                 return -ESHUTDOWN;
611         if (!value)
612                 ep->halted = ep->wedged = 0;
613         else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
614                         !list_empty (&ep->queue))
615                 return -EAGAIN;
616         else {
617                 ep->halted = 1;
618                 if (wedged)
619                         ep->wedged = 1;
620         }
621         /* FIXME clear emulated data toggle too */
622         return 0;
623 }
624
625 static int
626 dummy_set_halt(struct usb_ep *_ep, int value)
627 {
628         return dummy_set_halt_and_wedge(_ep, value, 0);
629 }
630
631 static int dummy_set_wedge(struct usb_ep *_ep)
632 {
633         if (!_ep || _ep->name == ep0name)
634                 return -EINVAL;
635         return dummy_set_halt_and_wedge(_ep, 1, 1);
636 }
637
638 static const struct usb_ep_ops dummy_ep_ops = {
639         .enable         = dummy_enable,
640         .disable        = dummy_disable,
641
642         .alloc_request  = dummy_alloc_request,
643         .free_request   = dummy_free_request,
644
645         .queue          = dummy_queue,
646         .dequeue        = dummy_dequeue,
647
648         .set_halt       = dummy_set_halt,
649         .set_wedge      = dummy_set_wedge,
650 };
651
652 /*-------------------------------------------------------------------------*/
653
654 /* there are both host and device side versions of this call ... */
655 static int dummy_g_get_frame (struct usb_gadget *_gadget)
656 {
657         struct timeval  tv;
658
659         do_gettimeofday (&tv);
660         return tv.tv_usec / 1000;
661 }
662
663 static int dummy_wakeup (struct usb_gadget *_gadget)
664 {
665         struct dummy    *dum;
666
667         dum = gadget_to_dummy (_gadget);
668         if (!(dum->devstatus &  ( (1 << USB_DEVICE_B_HNP_ENABLE)
669                                 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
670                 return -EINVAL;
671         if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0)
672                 return -ENOLINK;
673         if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
674                          dum->rh_state != DUMMY_RH_SUSPENDED)
675                 return -EIO;
676
677         /* FIXME: What if the root hub is suspended but the port isn't? */
678
679         /* hub notices our request, issues downstream resume, etc */
680         dum->resuming = 1;
681         dum->re_timeout = jiffies + msecs_to_jiffies(20);
682         mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout);
683         return 0;
684 }
685
686 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
687 {
688         struct dummy    *dum;
689
690         dum = gadget_to_dummy (_gadget);
691         if (value)
692                 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
693         else
694                 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
695         return 0;
696 }
697
698 static int dummy_pullup (struct usb_gadget *_gadget, int value)
699 {
700         struct dummy    *dum;
701         unsigned long   flags;
702
703         dum = gadget_to_dummy (_gadget);
704         spin_lock_irqsave (&dum->lock, flags);
705         dum->pullup = (value != 0);
706         set_link_state (dum);
707         spin_unlock_irqrestore (&dum->lock, flags);
708
709         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
710         return 0;
711 }
712
713 static const struct usb_gadget_ops dummy_ops = {
714         .get_frame      = dummy_g_get_frame,
715         .wakeup         = dummy_wakeup,
716         .set_selfpowered = dummy_set_selfpowered,
717         .pullup         = dummy_pullup,
718 };
719
720 /*-------------------------------------------------------------------------*/
721
722 /* "function" sysfs attribute */
723 static ssize_t
724 show_function (struct device *dev, struct device_attribute *attr, char *buf)
725 {
726         struct dummy    *dum = gadget_dev_to_dummy (dev);
727
728         if (!dum->driver || !dum->driver->function)
729                 return 0;
730         return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
731 }
732 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
733
734 /*-------------------------------------------------------------------------*/
735
736 /*
737  * Driver registration/unregistration.
738  *
739  * This is basically hardware-specific; there's usually only one real USB
740  * device (not host) controller since that's how USB devices are intended
741  * to work.  So most implementations of these api calls will rely on the
742  * fact that only one driver will ever bind to the hardware.  But curious
743  * hardware can be built with discrete components, so the gadget API doesn't
744  * require that assumption.
745  *
746  * For this emulator, it might be convenient to create a usb slave device
747  * for each driver that registers:  just add to a big root hub.
748  */
749
750 int
751 usb_gadget_probe_driver(struct usb_gadget_driver *driver,
752                 int (*bind)(struct usb_gadget *))
753 {
754         struct dummy    *dum = the_controller;
755         int             retval, i;
756
757         if (!dum)
758                 return -EINVAL;
759         if (dum->driver)
760                 return -EBUSY;
761         if (!bind || !driver->setup || driver->speed == USB_SPEED_UNKNOWN)
762                 return -EINVAL;
763
764         /*
765          * SLAVE side init ... the layer above hardware, which
766          * can't enumerate without help from the driver we're binding.
767          */
768
769         dum->devstatus = 0;
770
771         INIT_LIST_HEAD (&dum->gadget.ep_list);
772         for (i = 0; i < DUMMY_ENDPOINTS; i++) {
773                 struct dummy_ep *ep = &dum->ep [i];
774
775                 if (!ep_name [i])
776                         break;
777                 ep->ep.name = ep_name [i];
778                 ep->ep.ops = &dummy_ep_ops;
779                 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
780                 ep->halted = ep->wedged = ep->already_seen =
781                                 ep->setup_stage = 0;
782                 ep->ep.maxpacket = ~0;
783                 ep->last_io = jiffies;
784                 ep->gadget = &dum->gadget;
785                 ep->desc = NULL;
786                 INIT_LIST_HEAD (&ep->queue);
787         }
788
789         dum->gadget.ep0 = &dum->ep [0].ep;
790         dum->ep [0].ep.maxpacket = 64;
791         list_del_init (&dum->ep [0].ep.ep_list);
792         INIT_LIST_HEAD(&dum->fifo_req.queue);
793
794         driver->driver.bus = NULL;
795         dum->driver = driver;
796         dum->gadget.dev.driver = &driver->driver;
797         dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
798                         driver->driver.name);
799         retval = bind(&dum->gadget);
800         if (retval) {
801                 dum->driver = NULL;
802                 dum->gadget.dev.driver = NULL;
803                 return retval;
804         }
805
806         /* khubd will enumerate this in a while */
807         spin_lock_irq (&dum->lock);
808         dum->pullup = 1;
809         set_link_state (dum);
810         spin_unlock_irq (&dum->lock);
811
812         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
813         return 0;
814 }
815 EXPORT_SYMBOL(usb_gadget_probe_driver);
816
817 int
818 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
819 {
820         struct dummy    *dum = the_controller;
821         unsigned long   flags;
822
823         if (!dum)
824                 return -ENODEV;
825         if (!driver || driver != dum->driver || !driver->unbind)
826                 return -EINVAL;
827
828         dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
829                         driver->driver.name);
830
831         spin_lock_irqsave (&dum->lock, flags);
832         dum->pullup = 0;
833         set_link_state (dum);
834         spin_unlock_irqrestore (&dum->lock, flags);
835
836         driver->unbind (&dum->gadget);
837         dum->gadget.dev.driver = NULL;
838         dum->driver = NULL;
839
840         spin_lock_irqsave (&dum->lock, flags);
841         dum->pullup = 0;
842         set_link_state (dum);
843         spin_unlock_irqrestore (&dum->lock, flags);
844
845         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
846         return 0;
847 }
848 EXPORT_SYMBOL (usb_gadget_unregister_driver);
849
850 #undef is_enabled
851
852 /* just declare this in any driver that really need it */
853 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
854
855 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
856 {
857         return -ENOSYS;
858 }
859 EXPORT_SYMBOL (net2280_set_fifo_mode);
860
861
862 /* The gadget structure is stored inside the hcd structure and will be
863  * released along with it. */
864 static void
865 dummy_gadget_release (struct device *dev)
866 {
867         struct dummy    *dum = gadget_dev_to_dummy (dev);
868
869         usb_put_hcd (dummy_to_hcd (dum));
870 }
871
872 static int dummy_udc_probe (struct platform_device *pdev)
873 {
874         struct dummy    *dum = the_controller;
875         int             rc;
876
877         usb_get_hcd(dummy_to_hcd(dum));
878
879         dum->gadget.name = gadget_name;
880         dum->gadget.ops = &dummy_ops;
881         dum->gadget.is_dualspeed = 1;
882
883         /* maybe claim OTG support, though we won't complete HNP */
884         dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
885
886         dev_set_name(&dum->gadget.dev, "gadget");
887         dum->gadget.dev.parent = &pdev->dev;
888         dum->gadget.dev.release = dummy_gadget_release;
889         rc = device_register (&dum->gadget.dev);
890         if (rc < 0) {
891                 put_device(&dum->gadget.dev);
892                 return rc;
893         }
894
895         platform_set_drvdata (pdev, dum);
896         rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
897         if (rc < 0)
898                 device_unregister (&dum->gadget.dev);
899         return rc;
900 }
901
902 static int dummy_udc_remove (struct platform_device *pdev)
903 {
904         struct dummy    *dum = platform_get_drvdata (pdev);
905
906         platform_set_drvdata (pdev, NULL);
907         device_remove_file (&dum->gadget.dev, &dev_attr_function);
908         device_unregister (&dum->gadget.dev);
909         return 0;
910 }
911
912 static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
913 {
914         struct dummy    *dum = platform_get_drvdata(pdev);
915
916         dev_dbg (&pdev->dev, "%s\n", __func__);
917         spin_lock_irq (&dum->lock);
918         dum->udc_suspended = 1;
919         set_link_state (dum);
920         spin_unlock_irq (&dum->lock);
921
922         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
923         return 0;
924 }
925
926 static int dummy_udc_resume (struct platform_device *pdev)
927 {
928         struct dummy    *dum = platform_get_drvdata(pdev);
929
930         dev_dbg (&pdev->dev, "%s\n", __func__);
931         spin_lock_irq (&dum->lock);
932         dum->udc_suspended = 0;
933         set_link_state (dum);
934         spin_unlock_irq (&dum->lock);
935
936         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
937         return 0;
938 }
939
940 static struct platform_driver dummy_udc_driver = {
941         .probe          = dummy_udc_probe,
942         .remove         = dummy_udc_remove,
943         .suspend        = dummy_udc_suspend,
944         .resume         = dummy_udc_resume,
945         .driver         = {
946                 .name   = (char *) gadget_name,
947                 .owner  = THIS_MODULE,
948         },
949 };
950
951 /*-------------------------------------------------------------------------*/
952
953 /* MASTER/HOST SIDE DRIVER
954  *
955  * this uses the hcd framework to hook up to host side drivers.
956  * its root hub will only have one device, otherwise it acts like
957  * a normal host controller.
958  *
959  * when urbs are queued, they're just stuck on a list that we
960  * scan in a timer callback.  that callback connects writes from
961  * the host with reads from the device, and so on, based on the
962  * usb 2.0 rules.
963  */
964
965 static int dummy_urb_enqueue (
966         struct usb_hcd                  *hcd,
967         struct urb                      *urb,
968         gfp_t                           mem_flags
969 ) {
970         struct dummy    *dum;
971         struct urbp     *urbp;
972         unsigned long   flags;
973         int             rc;
974
975         if (!urb->transfer_buffer && urb->transfer_buffer_length)
976                 return -EINVAL;
977
978         urbp = kmalloc (sizeof *urbp, mem_flags);
979         if (!urbp)
980                 return -ENOMEM;
981         urbp->urb = urb;
982
983         dum = hcd_to_dummy (hcd);
984         spin_lock_irqsave (&dum->lock, flags);
985         rc = usb_hcd_link_urb_to_ep(hcd, urb);
986         if (rc) {
987                 kfree(urbp);
988                 goto done;
989         }
990
991         if (!dum->udev) {
992                 dum->udev = urb->dev;
993                 usb_get_dev (dum->udev);
994         } else if (unlikely (dum->udev != urb->dev))
995                 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
996
997         list_add_tail (&urbp->urbp_list, &dum->urbp_list);
998         urb->hcpriv = urbp;
999         if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1000                 urb->error_count = 1;           /* mark as a new urb */
1001
1002         /* kick the scheduler, it'll do the rest */
1003         if (!timer_pending (&dum->timer))
1004                 mod_timer (&dum->timer, jiffies + 1);
1005
1006  done:
1007         spin_unlock_irqrestore(&dum->lock, flags);
1008         return rc;
1009 }
1010
1011 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1012 {
1013         struct dummy    *dum;
1014         unsigned long   flags;
1015         int             rc;
1016
1017         /* giveback happens automatically in timer callback,
1018          * so make sure the callback happens */
1019         dum = hcd_to_dummy (hcd);
1020         spin_lock_irqsave (&dum->lock, flags);
1021
1022         rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1023         if (!rc && dum->rh_state != DUMMY_RH_RUNNING &&
1024                         !list_empty(&dum->urbp_list))
1025                 mod_timer (&dum->timer, jiffies);
1026
1027         spin_unlock_irqrestore (&dum->lock, flags);
1028         return rc;
1029 }
1030
1031 /* transfer up to a frame's worth; caller must own lock */
1032 static int
1033 transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1034                 int *status)
1035 {
1036         struct dummy_request    *req;
1037
1038 top:
1039         /* if there's no request queued, the device is NAKing; return */
1040         list_for_each_entry (req, &ep->queue, queue) {
1041                 unsigned        host_len, dev_len, len;
1042                 int             is_short, to_host;
1043                 int             rescan = 0;
1044
1045                 /* 1..N packets of ep->ep.maxpacket each ... the last one
1046                  * may be short (including zero length).
1047                  *
1048                  * writer can send a zlp explicitly (length 0) or implicitly
1049                  * (length mod maxpacket zero, and 'zero' flag); they always
1050                  * terminate reads.
1051                  */
1052                 host_len = urb->transfer_buffer_length - urb->actual_length;
1053                 dev_len = req->req.length - req->req.actual;
1054                 len = min (host_len, dev_len);
1055
1056                 /* FIXME update emulated data toggle too */
1057
1058                 to_host = usb_pipein (urb->pipe);
1059                 if (unlikely (len == 0))
1060                         is_short = 1;
1061                 else {
1062                         char            *ubuf, *rbuf;
1063
1064                         /* not enough bandwidth left? */
1065                         if (limit < ep->ep.maxpacket && limit < len)
1066                                 break;
1067                         len = min (len, (unsigned) limit);
1068                         if (len == 0)
1069                                 break;
1070
1071                         /* use an extra pass for the final short packet */
1072                         if (len > ep->ep.maxpacket) {
1073                                 rescan = 1;
1074                                 len -= (len % ep->ep.maxpacket);
1075                         }
1076                         is_short = (len % ep->ep.maxpacket) != 0;
1077
1078                         /* else transfer packet(s) */
1079                         ubuf = urb->transfer_buffer + urb->actual_length;
1080                         rbuf = req->req.buf + req->req.actual;
1081                         if (to_host)
1082                                 memcpy (ubuf, rbuf, len);
1083                         else
1084                                 memcpy (rbuf, ubuf, len);
1085                         ep->last_io = jiffies;
1086
1087                         limit -= len;
1088                         urb->actual_length += len;
1089                         req->req.actual += len;
1090                 }
1091
1092                 /* short packets terminate, maybe with overflow/underflow.
1093                  * it's only really an error to write too much.
1094                  *
1095                  * partially filling a buffer optionally blocks queue advances
1096                  * (so completion handlers can clean up the queue) but we don't
1097                  * need to emulate such data-in-flight.
1098                  */
1099                 if (is_short) {
1100                         if (host_len == dev_len) {
1101                                 req->req.status = 0;
1102                                 *status = 0;
1103                         } else if (to_host) {
1104                                 req->req.status = 0;
1105                                 if (dev_len > host_len)
1106                                         *status = -EOVERFLOW;
1107                                 else
1108                                         *status = 0;
1109                         } else if (!to_host) {
1110                                 *status = 0;
1111                                 if (host_len > dev_len)
1112                                         req->req.status = -EOVERFLOW;
1113                                 else
1114                                         req->req.status = 0;
1115                         }
1116
1117                 /* many requests terminate without a short packet */
1118                 } else {
1119                         if (req->req.length == req->req.actual
1120                                         && !req->req.zero)
1121                                 req->req.status = 0;
1122                         if (urb->transfer_buffer_length == urb->actual_length
1123                                         && !(urb->transfer_flags
1124                                                 & URB_ZERO_PACKET))
1125                                 *status = 0;
1126                 }
1127
1128                 /* device side completion --> continuable */
1129                 if (req->req.status != -EINPROGRESS) {
1130                         list_del_init (&req->queue);
1131
1132                         spin_unlock (&dum->lock);
1133                         req->req.complete (&ep->ep, &req->req);
1134                         spin_lock (&dum->lock);
1135
1136                         /* requests might have been unlinked... */
1137                         rescan = 1;
1138                 }
1139
1140                 /* host side completion --> terminate */
1141                 if (*status != -EINPROGRESS)
1142                         break;
1143
1144                 /* rescan to continue with any other queued i/o */
1145                 if (rescan)
1146                         goto top;
1147         }
1148         return limit;
1149 }
1150
1151 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1152 {
1153         int     limit = ep->ep.maxpacket;
1154
1155         if (dum->gadget.speed == USB_SPEED_HIGH) {
1156                 int     tmp;
1157
1158                 /* high bandwidth mode */
1159                 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1160                 tmp = (tmp >> 11) & 0x03;
1161                 tmp *= 8 /* applies to entire frame */;
1162                 limit += limit * tmp;
1163         }
1164         return limit;
1165 }
1166
1167 #define is_active(dum)  ((dum->port_status & \
1168                 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1169                         USB_PORT_STAT_SUSPEND)) \
1170                 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1171
1172 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1173 {
1174         int             i;
1175
1176         if (!is_active (dum))
1177                 return NULL;
1178         if ((address & ~USB_DIR_IN) == 0)
1179                 return &dum->ep [0];
1180         for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1181                 struct dummy_ep *ep = &dum->ep [i];
1182
1183                 if (!ep->desc)
1184                         continue;
1185                 if (ep->desc->bEndpointAddress == address)
1186                         return ep;
1187         }
1188         return NULL;
1189 }
1190
1191 #undef is_active
1192
1193 #define Dev_Request     (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1194 #define Dev_InRequest   (Dev_Request | USB_DIR_IN)
1195 #define Intf_Request    (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1196 #define Intf_InRequest  (Intf_Request | USB_DIR_IN)
1197 #define Ep_Request      (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1198 #define Ep_InRequest    (Ep_Request | USB_DIR_IN)
1199
1200 /* drive both sides of the transfers; looks like irq handlers to
1201  * both drivers except the callbacks aren't in_irq().
1202  */
1203 static void dummy_timer (unsigned long _dum)
1204 {
1205         struct dummy            *dum = (struct dummy *) _dum;
1206         struct urbp             *urbp, *tmp;
1207         unsigned long           flags;
1208         int                     limit, total;
1209         int                     i;
1210
1211         /* simplistic model for one frame's bandwidth */
1212         switch (dum->gadget.speed) {
1213         case USB_SPEED_LOW:
1214                 total = 8/*bytes*/ * 12/*packets*/;
1215                 break;
1216         case USB_SPEED_FULL:
1217                 total = 64/*bytes*/ * 19/*packets*/;
1218                 break;
1219         case USB_SPEED_HIGH:
1220                 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1221                 break;
1222         default:
1223                 dev_err (dummy_dev(dum), "bogus device speed\n");
1224                 return;
1225         }
1226
1227         /* FIXME if HZ != 1000 this will probably misbehave ... */
1228
1229         /* look at each urb queued by the host side driver */
1230         spin_lock_irqsave (&dum->lock, flags);
1231
1232         if (!dum->udev) {
1233                 dev_err (dummy_dev(dum),
1234                                 "timer fired with no URBs pending?\n");
1235                 spin_unlock_irqrestore (&dum->lock, flags);
1236                 return;
1237         }
1238
1239         for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1240                 if (!ep_name [i])
1241                         break;
1242                 dum->ep [i].already_seen = 0;
1243         }
1244
1245 restart:
1246         list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1247                 struct urb              *urb;
1248                 struct dummy_request    *req;
1249                 u8                      address;
1250                 struct dummy_ep         *ep = NULL;
1251                 int                     type;
1252                 int                     status = -EINPROGRESS;
1253
1254                 urb = urbp->urb;
1255                 if (urb->unlinked)
1256                         goto return_urb;
1257                 else if (dum->rh_state != DUMMY_RH_RUNNING)
1258                         continue;
1259                 type = usb_pipetype (urb->pipe);
1260
1261                 /* used up this frame's non-periodic bandwidth?
1262                  * FIXME there's infinite bandwidth for control and
1263                  * periodic transfers ... unrealistic.
1264                  */
1265                 if (total <= 0 && type == PIPE_BULK)
1266                         continue;
1267
1268                 /* find the gadget's ep for this request (if configured) */
1269                 address = usb_pipeendpoint (urb->pipe);
1270                 if (usb_pipein (urb->pipe))
1271                         address |= USB_DIR_IN;
1272                 ep = find_endpoint(dum, address);
1273                 if (!ep) {
1274                         /* set_configuration() disagreement */
1275                         dev_dbg (dummy_dev(dum),
1276                                 "no ep configured for urb %p\n",
1277                                 urb);
1278                         status = -EPROTO;
1279                         goto return_urb;
1280                 }
1281
1282                 if (ep->already_seen)
1283                         continue;
1284                 ep->already_seen = 1;
1285                 if (ep == &dum->ep [0] && urb->error_count) {
1286                         ep->setup_stage = 1;    /* a new urb */
1287                         urb->error_count = 0;
1288                 }
1289                 if (ep->halted && !ep->setup_stage) {
1290                         /* NOTE: must not be iso! */
1291                         dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1292                                         ep->ep.name, urb);
1293                         status = -EPIPE;
1294                         goto return_urb;
1295                 }
1296                 /* FIXME make sure both ends agree on maxpacket */
1297
1298                 /* handle control requests */
1299                 if (ep == &dum->ep [0] && ep->setup_stage) {
1300                         struct usb_ctrlrequest          setup;
1301                         int                             value = 1;
1302                         struct dummy_ep                 *ep2;
1303                         unsigned                        w_index;
1304                         unsigned                        w_value;
1305
1306                         setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1307                         w_index = le16_to_cpu(setup.wIndex);
1308                         w_value = le16_to_cpu(setup.wValue);
1309
1310                         /* paranoia, in case of stale queued data */
1311                         list_for_each_entry (req, &ep->queue, queue) {
1312                                 list_del_init (&req->queue);
1313                                 req->req.status = -EOVERFLOW;
1314                                 dev_dbg (udc_dev(dum), "stale req = %p\n",
1315                                                 req);
1316
1317                                 spin_unlock (&dum->lock);
1318                                 req->req.complete (&ep->ep, &req->req);
1319                                 spin_lock (&dum->lock);
1320                                 ep->already_seen = 0;
1321                                 goto restart;
1322                         }
1323
1324                         /* gadget driver never sees set_address or operations
1325                          * on standard feature flags.  some hardware doesn't
1326                          * even expose them.
1327                          */
1328                         ep->last_io = jiffies;
1329                         ep->setup_stage = 0;
1330                         ep->halted = 0;
1331                         switch (setup.bRequest) {
1332                         case USB_REQ_SET_ADDRESS:
1333                                 if (setup.bRequestType != Dev_Request)
1334                                         break;
1335                                 dum->address = w_value;
1336                                 status = 0;
1337                                 dev_dbg (udc_dev(dum), "set_address = %d\n",
1338                                                 w_value);
1339                                 value = 0;
1340                                 break;
1341                         case USB_REQ_SET_FEATURE:
1342                                 if (setup.bRequestType == Dev_Request) {
1343                                         value = 0;
1344                                         switch (w_value) {
1345                                         case USB_DEVICE_REMOTE_WAKEUP:
1346                                                 break;
1347                                         case USB_DEVICE_B_HNP_ENABLE:
1348                                                 dum->gadget.b_hnp_enable = 1;
1349                                                 break;
1350                                         case USB_DEVICE_A_HNP_SUPPORT:
1351                                                 dum->gadget.a_hnp_support = 1;
1352                                                 break;
1353                                         case USB_DEVICE_A_ALT_HNP_SUPPORT:
1354                                                 dum->gadget.a_alt_hnp_support
1355                                                         = 1;
1356                                                 break;
1357                                         default:
1358                                                 value = -EOPNOTSUPP;
1359                                         }
1360                                         if (value == 0) {
1361                                                 dum->devstatus |=
1362                                                         (1 << w_value);
1363                                                 status = 0;
1364                                         }
1365
1366                                 } else if (setup.bRequestType == Ep_Request) {
1367                                         // endpoint halt
1368                                         ep2 = find_endpoint (dum, w_index);
1369                                         if (!ep2 || ep2->ep.name == ep0name) {
1370                                                 value = -EOPNOTSUPP;
1371                                                 break;
1372                                         }
1373                                         ep2->halted = 1;
1374                                         value = 0;
1375                                         status = 0;
1376                                 }
1377                                 break;
1378                         case USB_REQ_CLEAR_FEATURE:
1379                                 if (setup.bRequestType == Dev_Request) {
1380                                         switch (w_value) {
1381                                         case USB_DEVICE_REMOTE_WAKEUP:
1382                                                 dum->devstatus &= ~(1 <<
1383                                                         USB_DEVICE_REMOTE_WAKEUP);
1384                                                 value = 0;
1385                                                 status = 0;
1386                                                 break;
1387                                         default:
1388                                                 value = -EOPNOTSUPP;
1389                                                 break;
1390                                         }
1391                                 } else if (setup.bRequestType == Ep_Request) {
1392                                         // endpoint halt
1393                                         ep2 = find_endpoint (dum, w_index);
1394                                         if (!ep2) {
1395                                                 value = -EOPNOTSUPP;
1396                                                 break;
1397                                         }
1398                                         if (!ep2->wedged)
1399                                                 ep2->halted = 0;
1400                                         value = 0;
1401                                         status = 0;
1402                                 }
1403                                 break;
1404                         case USB_REQ_GET_STATUS:
1405                                 if (setup.bRequestType == Dev_InRequest
1406                                                 || setup.bRequestType
1407                                                         == Intf_InRequest
1408                                                 || setup.bRequestType
1409                                                         == Ep_InRequest
1410                                                 ) {
1411                                         char *buf;
1412
1413                                         // device: remote wakeup, selfpowered
1414                                         // interface: nothing
1415                                         // endpoint: halt
1416                                         buf = (char *)urb->transfer_buffer;
1417                                         if (urb->transfer_buffer_length > 0) {
1418                                                 if (setup.bRequestType ==
1419                                                                 Ep_InRequest) {
1420         ep2 = find_endpoint (dum, w_index);
1421         if (!ep2) {
1422                 value = -EOPNOTSUPP;
1423                 break;
1424         }
1425         buf [0] = ep2->halted;
1426                                                 } else if (setup.bRequestType ==
1427                                                                 Dev_InRequest) {
1428                                                         buf [0] = (u8)
1429                                                                 dum->devstatus;
1430                                                 } else
1431                                                         buf [0] = 0;
1432                                         }
1433                                         if (urb->transfer_buffer_length > 1)
1434                                                 buf [1] = 0;
1435                                         urb->actual_length = min_t(u32, 2,
1436                                                 urb->transfer_buffer_length);
1437                                         value = 0;
1438                                         status = 0;
1439                                 }
1440                                 break;
1441                         }
1442
1443                         /* gadget driver handles all other requests.  block
1444                          * until setup() returns; no reentrancy issues etc.
1445                          */
1446                         if (value > 0) {
1447                                 spin_unlock (&dum->lock);
1448                                 value = dum->driver->setup (&dum->gadget,
1449                                                 &setup);
1450                                 spin_lock (&dum->lock);
1451
1452                                 if (value >= 0) {
1453                                         /* no delays (max 64KB data stage) */
1454                                         limit = 64*1024;
1455                                         goto treat_control_like_bulk;
1456                                 }
1457                                 /* error, see below */
1458                         }
1459
1460                         if (value < 0) {
1461                                 if (value != -EOPNOTSUPP)
1462                                         dev_dbg (udc_dev(dum),
1463                                                 "setup --> %d\n",
1464                                                 value);
1465                                 status = -EPIPE;
1466                                 urb->actual_length = 0;
1467                         }
1468
1469                         goto return_urb;
1470                 }
1471
1472                 /* non-control requests */
1473                 limit = total;
1474                 switch (usb_pipetype (urb->pipe)) {
1475                 case PIPE_ISOCHRONOUS:
1476                         /* FIXME is it urb->interval since the last xfer?
1477                          * use urb->iso_frame_desc[i].
1478                          * complete whether or not ep has requests queued.
1479                          * report random errors, to debug drivers.
1480                          */
1481                         limit = max (limit, periodic_bytes (dum, ep));
1482                         status = -ENOSYS;
1483                         break;
1484
1485                 case PIPE_INTERRUPT:
1486                         /* FIXME is it urb->interval since the last xfer?
1487                          * this almost certainly polls too fast.
1488                          */
1489                         limit = max (limit, periodic_bytes (dum, ep));
1490                         /* FALLTHROUGH */
1491
1492                 // case PIPE_BULK:  case PIPE_CONTROL:
1493                 default:
1494                 treat_control_like_bulk:
1495                         ep->last_io = jiffies;
1496                         total = transfer(dum, urb, ep, limit, &status);
1497                         break;
1498                 }
1499
1500                 /* incomplete transfer? */
1501                 if (status == -EINPROGRESS)
1502                         continue;
1503
1504 return_urb:
1505                 list_del (&urbp->urbp_list);
1506                 kfree (urbp);
1507                 if (ep)
1508                         ep->already_seen = ep->setup_stage = 0;
1509
1510                 usb_hcd_unlink_urb_from_ep(dummy_to_hcd(dum), urb);
1511                 spin_unlock (&dum->lock);
1512                 usb_hcd_giveback_urb(dummy_to_hcd(dum), urb, status);
1513                 spin_lock (&dum->lock);
1514
1515                 goto restart;
1516         }
1517
1518         if (list_empty (&dum->urbp_list)) {
1519                 usb_put_dev (dum->udev);
1520                 dum->udev = NULL;
1521         } else if (dum->rh_state == DUMMY_RH_RUNNING) {
1522                 /* want a 1 msec delay here */
1523                 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1524         }
1525
1526         spin_unlock_irqrestore (&dum->lock, flags);
1527 }
1528
1529 /*-------------------------------------------------------------------------*/
1530
1531 #define PORT_C_MASK \
1532         ((USB_PORT_STAT_C_CONNECTION \
1533         | USB_PORT_STAT_C_ENABLE \
1534         | USB_PORT_STAT_C_SUSPEND \
1535         | USB_PORT_STAT_C_OVERCURRENT \
1536         | USB_PORT_STAT_C_RESET) << 16)
1537
1538 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1539 {
1540         struct dummy            *dum;
1541         unsigned long           flags;
1542         int                     retval = 0;
1543
1544         dum = hcd_to_dummy (hcd);
1545
1546         spin_lock_irqsave (&dum->lock, flags);
1547         if (!HCD_HW_ACCESSIBLE(hcd))
1548                 goto done;
1549
1550         if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
1551                 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1552                 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1553                 set_link_state (dum);
1554         }
1555
1556         if ((dum->port_status & PORT_C_MASK) != 0) {
1557                 *buf = (1 << 1);
1558                 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1559                                 dum->port_status);
1560                 retval = 1;
1561                 if (dum->rh_state == DUMMY_RH_SUSPENDED)
1562                         usb_hcd_resume_root_hub (hcd);
1563         }
1564 done:
1565         spin_unlock_irqrestore (&dum->lock, flags);
1566         return retval;
1567 }
1568
1569 static inline void
1570 hub_descriptor (struct usb_hub_descriptor *desc)
1571 {
1572         memset (desc, 0, sizeof *desc);
1573         desc->bDescriptorType = 0x29;
1574         desc->bDescLength = 9;
1575         desc->wHubCharacteristics = cpu_to_le16(0x0001);
1576         desc->bNbrPorts = 1;
1577         desc->bitmap [0] = 0xff;
1578         desc->bitmap [1] = 0xff;
1579 }
1580
1581 static int dummy_hub_control (
1582         struct usb_hcd  *hcd,
1583         u16             typeReq,
1584         u16             wValue,
1585         u16             wIndex,
1586         char            *buf,
1587         u16             wLength
1588 ) {
1589         struct dummy    *dum;
1590         int             retval = 0;
1591         unsigned long   flags;
1592
1593         if (!HCD_HW_ACCESSIBLE(hcd))
1594                 return -ETIMEDOUT;
1595
1596         dum = hcd_to_dummy (hcd);
1597         spin_lock_irqsave (&dum->lock, flags);
1598         switch (typeReq) {
1599         case ClearHubFeature:
1600                 break;
1601         case ClearPortFeature:
1602                 switch (wValue) {
1603                 case USB_PORT_FEAT_SUSPEND:
1604                         if (dum->port_status & USB_PORT_STAT_SUSPEND) {
1605                                 /* 20msec resume signaling */
1606                                 dum->resuming = 1;
1607                                 dum->re_timeout = jiffies +
1608                                                 msecs_to_jiffies(20);
1609                         }
1610                         break;
1611                 case USB_PORT_FEAT_POWER:
1612                         if (dum->port_status & USB_PORT_STAT_POWER)
1613                                 dev_dbg (dummy_dev(dum), "power-off\n");
1614                         /* FALLS THROUGH */
1615                 default:
1616                         dum->port_status &= ~(1 << wValue);
1617                         set_link_state (dum);
1618                 }
1619                 break;
1620         case GetHubDescriptor:
1621                 hub_descriptor ((struct usb_hub_descriptor *) buf);
1622                 break;
1623         case GetHubStatus:
1624                 *(__le32 *) buf = cpu_to_le32 (0);
1625                 break;
1626         case GetPortStatus:
1627                 if (wIndex != 1)
1628                         retval = -EPIPE;
1629
1630                 /* whoever resets or resumes must GetPortStatus to
1631                  * complete it!!
1632                  */
1633                 if (dum->resuming &&
1634                                 time_after_eq (jiffies, dum->re_timeout)) {
1635                         dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1636                         dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1637                 }
1638                 if ((dum->port_status & USB_PORT_STAT_RESET) != 0 &&
1639                                 time_after_eq (jiffies, dum->re_timeout)) {
1640                         dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
1641                         dum->port_status &= ~USB_PORT_STAT_RESET;
1642                         if (dum->pullup) {
1643                                 dum->port_status |= USB_PORT_STAT_ENABLE;
1644                                 /* give it the best speed we agree on */
1645                                 dum->gadget.speed = dum->driver->speed;
1646                                 dum->gadget.ep0->maxpacket = 64;
1647                                 switch (dum->gadget.speed) {
1648                                 case USB_SPEED_HIGH:
1649                                         dum->port_status |=
1650                                                 USB_PORT_STAT_HIGH_SPEED;
1651                                         break;
1652                                 case USB_SPEED_LOW:
1653                                         dum->gadget.ep0->maxpacket = 8;
1654                                         dum->port_status |=
1655                                                 USB_PORT_STAT_LOW_SPEED;
1656                                         break;
1657                                 default:
1658                                         dum->gadget.speed = USB_SPEED_FULL;
1659                                         break;
1660                                 }
1661                         }
1662                 }
1663                 set_link_state (dum);
1664                 ((__le16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1665                 ((__le16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1666                 break;
1667         case SetHubFeature:
1668                 retval = -EPIPE;
1669                 break;
1670         case SetPortFeature:
1671                 switch (wValue) {
1672                 case USB_PORT_FEAT_SUSPEND:
1673                         if (dum->active) {
1674                                 dum->port_status |= USB_PORT_STAT_SUSPEND;
1675
1676                                 /* HNP would happen here; for now we
1677                                  * assume b_bus_req is always true.
1678                                  */
1679                                 set_link_state (dum);
1680                                 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1681                                                 & dum->devstatus) != 0)
1682                                         dev_dbg (dummy_dev(dum),
1683                                                         "no HNP yet!\n");
1684                         }
1685                         break;
1686                 case USB_PORT_FEAT_POWER:
1687                         dum->port_status |= USB_PORT_STAT_POWER;
1688                         set_link_state (dum);
1689                         break;
1690                 case USB_PORT_FEAT_RESET:
1691                         /* if it's already enabled, disable */
1692                         dum->port_status &= ~(USB_PORT_STAT_ENABLE
1693                                         | USB_PORT_STAT_LOW_SPEED
1694                                         | USB_PORT_STAT_HIGH_SPEED);
1695                         dum->devstatus = 0;
1696                         /* 50msec reset signaling */
1697                         dum->re_timeout = jiffies + msecs_to_jiffies(50);
1698                         /* FALLS THROUGH */
1699                 default:
1700                         if ((dum->port_status & USB_PORT_STAT_POWER) != 0) {
1701                                 dum->port_status |= (1 << wValue);
1702                                 set_link_state (dum);
1703                         }
1704                 }
1705                 break;
1706
1707         default:
1708                 dev_dbg (dummy_dev(dum),
1709                         "hub control req%04x v%04x i%04x l%d\n",
1710                         typeReq, wValue, wIndex, wLength);
1711
1712                 /* "protocol stall" on error */
1713                 retval = -EPIPE;
1714         }
1715         spin_unlock_irqrestore (&dum->lock, flags);
1716
1717         if ((dum->port_status & PORT_C_MASK) != 0)
1718                 usb_hcd_poll_rh_status (hcd);
1719         return retval;
1720 }
1721
1722 static int dummy_bus_suspend (struct usb_hcd *hcd)
1723 {
1724         struct dummy *dum = hcd_to_dummy (hcd);
1725
1726         dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
1727
1728         spin_lock_irq (&dum->lock);
1729         dum->rh_state = DUMMY_RH_SUSPENDED;
1730         set_link_state (dum);
1731         hcd->state = HC_STATE_SUSPENDED;
1732         spin_unlock_irq (&dum->lock);
1733         return 0;
1734 }
1735
1736 static int dummy_bus_resume (struct usb_hcd *hcd)
1737 {
1738         struct dummy *dum = hcd_to_dummy (hcd);
1739         int rc = 0;
1740
1741         dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
1742
1743         spin_lock_irq (&dum->lock);
1744         if (!HCD_HW_ACCESSIBLE(hcd)) {
1745                 rc = -ESHUTDOWN;
1746         } else {
1747                 dum->rh_state = DUMMY_RH_RUNNING;
1748                 set_link_state (dum);
1749                 if (!list_empty(&dum->urbp_list))
1750                         mod_timer (&dum->timer, jiffies);
1751                 hcd->state = HC_STATE_RUNNING;
1752         }
1753         spin_unlock_irq (&dum->lock);
1754         return rc;
1755 }
1756
1757 /*-------------------------------------------------------------------------*/
1758
1759 static inline ssize_t
1760 show_urb (char *buf, size_t size, struct urb *urb)
1761 {
1762         int ep = usb_pipeendpoint (urb->pipe);
1763
1764         return snprintf (buf, size,
1765                 "urb/%p %s ep%d%s%s len %d/%d\n",
1766                 urb,
1767                 ({ char *s;
1768                  switch (urb->dev->speed) {
1769                  case USB_SPEED_LOW:    s = "ls"; break;
1770                  case USB_SPEED_FULL:   s = "fs"; break;
1771                  case USB_SPEED_HIGH:   s = "hs"; break;
1772                  default:               s = "?"; break;
1773                  }; s; }),
1774                 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1775                 ({ char *s; \
1776                  switch (usb_pipetype (urb->pipe)) { \
1777                  case PIPE_CONTROL:     s = ""; break; \
1778                  case PIPE_BULK:        s = "-bulk"; break; \
1779                  case PIPE_INTERRUPT:   s = "-int"; break; \
1780                  default:               s = "-iso"; break; \
1781                 }; s;}),
1782                 urb->actual_length, urb->transfer_buffer_length);
1783 }
1784
1785 static ssize_t
1786 show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
1787 {
1788         struct usb_hcd          *hcd = dev_get_drvdata (dev);
1789         struct dummy            *dum = hcd_to_dummy (hcd);
1790         struct urbp             *urbp;
1791         size_t                  size = 0;
1792         unsigned long           flags;
1793
1794         spin_lock_irqsave (&dum->lock, flags);
1795         list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1796                 size_t          temp;
1797
1798                 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1799                 buf += temp;
1800                 size += temp;
1801         }
1802         spin_unlock_irqrestore (&dum->lock, flags);
1803
1804         return size;
1805 }
1806 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1807
1808 static int dummy_start (struct usb_hcd *hcd)
1809 {
1810         struct dummy            *dum;
1811
1812         dum = hcd_to_dummy (hcd);
1813
1814         /*
1815          * MASTER side init ... we emulate a root hub that'll only ever
1816          * talk to one device (the slave side).  Also appears in sysfs,
1817          * just like more familiar pci-based HCDs.
1818          */
1819         spin_lock_init (&dum->lock);
1820         init_timer (&dum->timer);
1821         dum->timer.function = dummy_timer;
1822         dum->timer.data = (unsigned long) dum;
1823         dum->rh_state = DUMMY_RH_RUNNING;
1824
1825         INIT_LIST_HEAD (&dum->urbp_list);
1826
1827         hcd->power_budget = POWER_BUDGET;
1828         hcd->state = HC_STATE_RUNNING;
1829         hcd->uses_new_polling = 1;
1830
1831 #ifdef CONFIG_USB_OTG
1832         hcd->self.otg_port = 1;
1833 #endif
1834
1835         /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1836         return device_create_file (dummy_dev(dum), &dev_attr_urbs);
1837 }
1838
1839 static void dummy_stop (struct usb_hcd *hcd)
1840 {
1841         struct dummy            *dum;
1842
1843         dum = hcd_to_dummy (hcd);
1844
1845         device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1846         usb_gadget_unregister_driver (dum->driver);
1847         dev_info (dummy_dev(dum), "stopped\n");
1848 }
1849
1850 /*-------------------------------------------------------------------------*/
1851
1852 static int dummy_h_get_frame (struct usb_hcd *hcd)
1853 {
1854         return dummy_g_get_frame (NULL);
1855 }
1856
1857 static const struct hc_driver dummy_hcd = {
1858         .description =          (char *) driver_name,
1859         .product_desc =         "Dummy host controller",
1860         .hcd_priv_size =        sizeof(struct dummy),
1861
1862         .flags =                HCD_USB2,
1863
1864         .start =                dummy_start,
1865         .stop =                 dummy_stop,
1866
1867         .urb_enqueue =          dummy_urb_enqueue,
1868         .urb_dequeue =          dummy_urb_dequeue,
1869
1870         .get_frame_number =     dummy_h_get_frame,
1871
1872         .hub_status_data =      dummy_hub_status,
1873         .hub_control =          dummy_hub_control,
1874         .bus_suspend =          dummy_bus_suspend,
1875         .bus_resume =           dummy_bus_resume,
1876 };
1877
1878 static int dummy_hcd_probe(struct platform_device *pdev)
1879 {
1880         struct usb_hcd          *hcd;
1881         int                     retval;
1882
1883         dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1884
1885         hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
1886         if (!hcd)
1887                 return -ENOMEM;
1888         the_controller = hcd_to_dummy (hcd);
1889
1890         retval = usb_add_hcd(hcd, 0, 0);
1891         if (retval != 0) {
1892                 usb_put_hcd (hcd);
1893                 the_controller = NULL;
1894         }
1895         return retval;
1896 }
1897
1898 static int dummy_hcd_remove (struct platform_device *pdev)
1899 {
1900         struct usb_hcd          *hcd;
1901
1902         hcd = platform_get_drvdata (pdev);
1903         usb_remove_hcd (hcd);
1904         usb_put_hcd (hcd);
1905         the_controller = NULL;
1906         return 0;
1907 }
1908
1909 static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
1910 {
1911         struct usb_hcd          *hcd;
1912         struct dummy            *dum;
1913         int                     rc = 0;
1914
1915         dev_dbg (&pdev->dev, "%s\n", __func__);
1916
1917         hcd = platform_get_drvdata (pdev);
1918         dum = hcd_to_dummy (hcd);
1919         if (dum->rh_state == DUMMY_RH_RUNNING) {
1920                 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
1921                 rc = -EBUSY;
1922         } else
1923                 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1924         return rc;
1925 }
1926
1927 static int dummy_hcd_resume (struct platform_device *pdev)
1928 {
1929         struct usb_hcd          *hcd;
1930
1931         dev_dbg (&pdev->dev, "%s\n", __func__);
1932
1933         hcd = platform_get_drvdata (pdev);
1934         set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1935         usb_hcd_poll_rh_status (hcd);
1936         return 0;
1937 }
1938
1939 static struct platform_driver dummy_hcd_driver = {
1940         .probe          = dummy_hcd_probe,
1941         .remove         = dummy_hcd_remove,
1942         .suspend        = dummy_hcd_suspend,
1943         .resume         = dummy_hcd_resume,
1944         .driver         = {
1945                 .name   = (char *) driver_name,
1946                 .owner  = THIS_MODULE,
1947         },
1948 };
1949
1950 /*-------------------------------------------------------------------------*/
1951
1952 static struct platform_device *the_udc_pdev;
1953 static struct platform_device *the_hcd_pdev;
1954
1955 static int __init init (void)
1956 {
1957         int     retval = -ENOMEM;
1958
1959         if (usb_disabled ())
1960                 return -ENODEV;
1961
1962         the_hcd_pdev = platform_device_alloc(driver_name, -1);
1963         if (!the_hcd_pdev)
1964                 return retval;
1965         the_udc_pdev = platform_device_alloc(gadget_name, -1);
1966         if (!the_udc_pdev)
1967                 goto err_alloc_udc;
1968
1969         retval = platform_driver_register(&dummy_hcd_driver);
1970         if (retval < 0)
1971                 goto err_register_hcd_driver;
1972         retval = platform_driver_register(&dummy_udc_driver);
1973         if (retval < 0)
1974                 goto err_register_udc_driver;
1975
1976         retval = platform_device_add(the_hcd_pdev);
1977         if (retval < 0)
1978                 goto err_add_hcd;
1979         retval = platform_device_add(the_udc_pdev);
1980         if (retval < 0)
1981                 goto err_add_udc;
1982         return retval;
1983
1984 err_add_udc:
1985         platform_device_del(the_hcd_pdev);
1986 err_add_hcd:
1987         platform_driver_unregister(&dummy_udc_driver);
1988 err_register_udc_driver:
1989         platform_driver_unregister(&dummy_hcd_driver);
1990 err_register_hcd_driver:
1991         platform_device_put(the_udc_pdev);
1992 err_alloc_udc:
1993         platform_device_put(the_hcd_pdev);
1994         return retval;
1995 }
1996 module_init (init);
1997
1998 static void __exit cleanup (void)
1999 {
2000         platform_device_unregister(the_udc_pdev);
2001         platform_device_unregister(the_hcd_pdev);
2002         platform_driver_unregister(&dummy_udc_driver);
2003         platform_driver_unregister(&dummy_hcd_driver);
2004 }
2005 module_exit (cleanup);