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