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