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