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