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