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