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