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