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