2 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
6 * Copyright (C) 2003 David Brownell
7 * Copyright (C) 2003-2005 Alan Stern
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.
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:
21 * - Gadget driver, responding to requests (slave);
22 * - Host-side device driver, as already familiar in Linux.
24 * Having this all in one kernel can help some stages of development,
25 * bypassing some hardware (and driver) issues. UML could help too.
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>
44 #include <asm/byteorder.h>
47 #include <asm/unaligned.h>
49 #define DRIVER_DESC "USB Host+Gadget Emulator"
50 #define DRIVER_VERSION "02 May 2005"
52 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
54 static const char driver_name[] = "dummy_hcd";
55 static const char driver_desc[] = "USB Host+Gadget Emulator";
57 static const char gadget_name[] = "dummy_udc";
59 MODULE_DESCRIPTION(DRIVER_DESC);
60 MODULE_AUTHOR("David Brownell");
61 MODULE_LICENSE("GPL");
63 struct dummy_hcd_module_parameters {
69 static struct dummy_hcd_module_parameters mod_data = {
70 .is_super_speed = false,
71 .is_high_speed = true,
74 module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
75 MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
76 module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
77 MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
78 module_param_named(num, mod_data.num, uint, S_IRUGO);
79 MODULE_PARM_DESC(num, "number of emulated controllers");
80 /*-------------------------------------------------------------------------*/
82 /* gadget side driver data structres */
84 struct list_head queue;
85 unsigned long last_io; /* jiffies timestamp */
86 struct usb_gadget *gadget;
87 const struct usb_endpoint_descriptor *desc;
91 unsigned already_seen:1;
92 unsigned setup_stage:1;
96 struct dummy_request {
97 struct list_head queue; /* ep's requests */
98 struct usb_request req;
101 static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
103 return container_of(_ep, struct dummy_ep, ep);
106 static inline struct dummy_request *usb_request_to_dummy_request
107 (struct usb_request *_req)
109 return container_of(_req, struct dummy_request, req);
112 /*-------------------------------------------------------------------------*/
115 * Every device has ep0 for control requests, plus up to 30 more endpoints,
116 * in one of two types:
118 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
119 * number can be changed. Names like "ep-a" are used for this type.
121 * - Fixed Function: in other cases. some characteristics may be mutable;
122 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
124 * Gadget drivers are responsible for not setting up conflicting endpoint
125 * configurations, illegal or unsupported packet lengths, and so on.
128 static const char ep0name[] = "ep0";
130 static const char *const ep_name[] = {
131 ep0name, /* everyone has ep0 */
133 /* act like a net2280: high speed, six configurable endpoints */
134 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
136 /* or like pxa250: fifteen fixed function endpoints */
137 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
138 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
139 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
142 /* or like sa1100: two fixed function endpoints */
143 "ep1out-bulk", "ep2in-bulk",
145 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
147 /*-------------------------------------------------------------------------*/
153 struct list_head urbp_list;
154 struct sg_mapping_iter miter;
159 enum dummy_rh_state {
167 enum dummy_rh_state rh_state;
168 struct timer_list timer;
171 unsigned long re_timeout;
173 struct usb_device *udev;
174 struct list_head urbp_list;
176 u8 num_stream[30 / 2];
179 unsigned old_active:1;
187 * SLAVE/GADGET side support
189 struct dummy_ep ep[DUMMY_ENDPOINTS];
191 struct usb_gadget gadget;
192 struct usb_gadget_driver *driver;
193 struct dummy_request fifo_req;
194 u8 fifo_buf[FIFO_SIZE];
196 unsigned udc_suspended:1;
200 * MASTER/HOST side support
202 struct dummy_hcd *hs_hcd;
203 struct dummy_hcd *ss_hcd;
206 static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
208 return (struct dummy_hcd *) (hcd->hcd_priv);
211 static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
213 return container_of((void *) dum, struct usb_hcd, hcd_priv);
216 static inline struct device *dummy_dev(struct dummy_hcd *dum)
218 return dummy_hcd_to_hcd(dum)->self.controller;
221 static inline struct device *udc_dev(struct dummy *dum)
223 return dum->gadget.dev.parent;
226 static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
228 return container_of(ep->gadget, struct dummy, gadget);
231 static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
233 struct dummy *dum = container_of(gadget, struct dummy, gadget);
234 if (dum->gadget.speed == USB_SPEED_SUPER)
240 static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
242 return container_of(dev, struct dummy, gadget.dev);
245 /*-------------------------------------------------------------------------*/
247 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
249 /* called with spinlock held */
250 static void nuke(struct dummy *dum, struct dummy_ep *ep)
252 while (!list_empty(&ep->queue)) {
253 struct dummy_request *req;
255 req = list_entry(ep->queue.next, struct dummy_request, queue);
256 list_del_init(&req->queue);
257 req->req.status = -ESHUTDOWN;
259 spin_unlock(&dum->lock);
260 req->req.complete(&ep->ep, &req->req);
261 spin_lock(&dum->lock);
265 /* caller must hold lock */
266 static void stop_activity(struct dummy *dum)
270 /* prevent any more requests */
273 /* The timer is left running so that outstanding URBs can fail */
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)
279 /* driver now does any non-usb quiescing necessary */
283 * set_link_state_by_speed() - Sets the current state of the link according to
285 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
287 * This function updates the port_status according to the link state and the
290 static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
292 struct dummy *dum = dum_hcd->dum;
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);
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)
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);
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)
350 /* caller must hold lock */
351 static void set_link_state(struct dummy_hcd *dum_hcd)
353 struct dummy *dum = dum_hcd->dum;
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))
363 set_link_state_by_speed(dum_hcd);
365 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
367 dum_hcd->resuming = 0;
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) {
373 * We're connected and not reset (reset occurred now),
374 * and driver attached - disconnect!
376 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
377 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
380 spin_unlock(&dum->lock);
381 dum->driver->disconnect(&dum->gadget);
382 spin_lock(&dum->lock);
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);
396 dum_hcd->old_status = dum_hcd->port_status;
397 dum_hcd->old_active = dum_hcd->active;
400 /*-------------------------------------------------------------------------*/
402 /* SLAVE/GADGET SIDE DRIVER
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.
409 #define is_enabled(dum) \
410 (dum->port_status & USB_PORT_STAT_ENABLE)
412 static int dummy_enable(struct usb_ep *_ep,
413 const struct usb_endpoint_descriptor *desc)
416 struct dummy_hcd *dum_hcd;
421 ep = usb_ep_to_dummy_ep(_ep);
422 if (!_ep || !desc || ep->desc || _ep->name == ep0name
423 || desc->bDescriptorType != USB_DT_ENDPOINT)
425 dum = ep_to_dummy(ep);
429 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
430 if (!is_enabled(dum_hcd))
434 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
435 * maximum packet size.
436 * For SS devices the wMaxPacketSize is limited by 1024.
438 max = usb_endpoint_maxp(desc) & 0x7ff;
440 /* drivers must not request bad settings, since lower levels
441 * (hardware or its drivers) may not check. some endpoints
442 * can't do iso, many have maxpacket limitations, etc.
444 * since this "hardware" driver is here to help debugging, we
445 * have some extra sanity checks. (there could be more though,
446 * especially for "ep9out" style fixed function ones.)
449 switch (usb_endpoint_type(desc)) {
450 case USB_ENDPOINT_XFER_BULK:
451 if (strstr(ep->ep.name, "-iso")
452 || strstr(ep->ep.name, "-int")) {
455 switch (dum->gadget.speed) {
456 case USB_SPEED_SUPER:
465 if (max == 8 || max == 16 || max == 32 || max == 64)
466 /* we'll fake any legal size */
468 /* save a return statement */
473 case USB_ENDPOINT_XFER_INT:
474 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
476 /* real hardware might not handle all packet sizes */
477 switch (dum->gadget.speed) {
478 case USB_SPEED_SUPER:
482 /* save a return statement */
486 /* save a return statement */
493 case USB_ENDPOINT_XFER_ISOC:
494 if (strstr(ep->ep.name, "-bulk")
495 || strstr(ep->ep.name, "-int"))
497 /* real hardware might not handle all packet sizes */
498 switch (dum->gadget.speed) {
499 case USB_SPEED_SUPER:
503 /* save a return statement */
507 /* save a return statement */
513 /* few chips support control except on ep0 */
517 _ep->maxpacket = max;
518 if (usb_ss_max_streams(_ep->comp_desc)) {
519 if (!usb_endpoint_xfer_bulk(desc)) {
520 dev_err(udc_dev(dum), "Can't enable stream support on "
521 "non-bulk ep %s\n", _ep->name);
528 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
530 desc->bEndpointAddress & 0x0f,
531 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
533 switch (usb_endpoint_type(desc)) {
534 case USB_ENDPOINT_XFER_BULK:
537 case USB_ENDPOINT_XFER_ISOC:
540 case USB_ENDPOINT_XFER_INT:
547 max, ep->stream_en ? "enabled" : "disabled");
549 /* at this point real hardware should be NAKing transfers
550 * to that endpoint, until a buffer is queued to it.
552 ep->halted = ep->wedged = 0;
558 static int dummy_disable(struct usb_ep *_ep)
565 ep = usb_ep_to_dummy_ep(_ep);
566 if (!_ep || !ep->desc || _ep->name == ep0name)
568 dum = ep_to_dummy(ep);
570 spin_lock_irqsave(&dum->lock, flags);
575 spin_unlock_irqrestore(&dum->lock, flags);
577 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
581 static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
585 struct dummy_request *req;
589 ep = usb_ep_to_dummy_ep(_ep);
591 req = kzalloc(sizeof(*req), mem_flags);
594 INIT_LIST_HEAD(&req->queue);
598 static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
600 struct dummy_request *req;
607 req = usb_request_to_dummy_request(_req);
608 WARN_ON(!list_empty(&req->queue));
612 static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
616 static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
620 struct dummy_request *req;
622 struct dummy_hcd *dum_hcd;
625 req = usb_request_to_dummy_request(_req);
626 if (!_req || !list_empty(&req->queue) || !_req->complete)
629 ep = usb_ep_to_dummy_ep(_ep);
630 if (!_ep || (!ep->desc && _ep->name != ep0name))
633 dum = ep_to_dummy(ep);
634 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
635 if (!dum->driver || !is_enabled(dum_hcd))
639 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
640 ep, _req, _ep->name, _req->length, _req->buf);
642 _req->status = -EINPROGRESS;
644 spin_lock_irqsave(&dum->lock, flags);
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;
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;
658 list_add_tail(&req->queue, &ep->queue);
659 spin_unlock(&dum->lock);
660 _req->actual = _req->length;
662 _req->complete(_ep, _req);
663 spin_lock(&dum->lock);
665 list_add_tail(&req->queue, &ep->queue);
666 spin_unlock_irqrestore(&dum->lock, flags);
668 /* real hardware would likely enable transfers here, in case
669 * it'd been left NAKing.
674 static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
678 int retval = -EINVAL;
680 struct dummy_request *req = NULL;
684 ep = usb_ep_to_dummy_ep(_ep);
685 dum = ep_to_dummy(ep);
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;
700 spin_unlock(&dum->lock);
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);
708 local_irq_restore(flags);
713 dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
720 ep = usb_ep_to_dummy_ep(_ep);
721 dum = ep_to_dummy(ep);
725 ep->halted = ep->wedged = 0;
726 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
727 !list_empty(&ep->queue))
734 /* FIXME clear emulated data toggle too */
739 dummy_set_halt(struct usb_ep *_ep, int value)
741 return dummy_set_halt_and_wedge(_ep, value, 0);
744 static int dummy_set_wedge(struct usb_ep *_ep)
746 if (!_ep || _ep->name == ep0name)
748 return dummy_set_halt_and_wedge(_ep, 1, 1);
751 static const struct usb_ep_ops dummy_ep_ops = {
752 .enable = dummy_enable,
753 .disable = dummy_disable,
755 .alloc_request = dummy_alloc_request,
756 .free_request = dummy_free_request,
758 .queue = dummy_queue,
759 .dequeue = dummy_dequeue,
761 .set_halt = dummy_set_halt,
762 .set_wedge = dummy_set_wedge,
765 /*-------------------------------------------------------------------------*/
767 /* there are both host and device side versions of this call ... */
768 static int dummy_g_get_frame(struct usb_gadget *_gadget)
772 do_gettimeofday(&tv);
773 return tv.tv_usec / 1000;
776 static int dummy_wakeup(struct usb_gadget *_gadget)
778 struct dummy_hcd *dum_hcd;
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))))
784 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
786 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
787 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
790 /* FIXME: What if the root hub is suspended but the port isn't? */
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);
799 static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
803 dum = gadget_to_dummy_hcd(_gadget)->dum;
805 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
807 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
811 static void dummy_udc_update_ep0(struct dummy *dum)
813 if (dum->gadget.speed == USB_SPEED_SUPER)
814 dum->ep[0].ep.maxpacket = 9;
816 dum->ep[0].ep.maxpacket = 64;
819 static int dummy_pullup(struct usb_gadget *_gadget, int value)
821 struct dummy_hcd *dum_hcd;
825 dum = gadget_dev_to_dummy(&_gadget->dev);
827 if (value && dum->driver) {
828 if (mod_data.is_super_speed)
829 dum->gadget.speed = dum->driver->max_speed;
830 else if (mod_data.is_high_speed)
831 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
832 dum->driver->max_speed);
834 dum->gadget.speed = USB_SPEED_FULL;
835 dummy_udc_update_ep0(dum);
837 if (dum->gadget.speed < dum->driver->max_speed)
838 dev_dbg(udc_dev(dum), "This device can perform faster"
839 " if you connect it to a %s port...\n",
840 usb_speed_string(dum->driver->max_speed));
842 dum_hcd = gadget_to_dummy_hcd(_gadget);
844 spin_lock_irqsave(&dum->lock, flags);
845 dum->pullup = (value != 0);
846 set_link_state(dum_hcd);
847 spin_unlock_irqrestore(&dum->lock, flags);
849 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
853 static int dummy_udc_start(struct usb_gadget *g,
854 struct usb_gadget_driver *driver);
855 static int dummy_udc_stop(struct usb_gadget *g,
856 struct usb_gadget_driver *driver);
858 static const struct usb_gadget_ops dummy_ops = {
859 .get_frame = dummy_g_get_frame,
860 .wakeup = dummy_wakeup,
861 .set_selfpowered = dummy_set_selfpowered,
862 .pullup = dummy_pullup,
863 .udc_start = dummy_udc_start,
864 .udc_stop = dummy_udc_stop,
867 /*-------------------------------------------------------------------------*/
869 /* "function" sysfs attribute */
870 static ssize_t show_function(struct device *dev, struct device_attribute *attr,
873 struct dummy *dum = gadget_dev_to_dummy(dev);
875 if (!dum->driver || !dum->driver->function)
877 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
879 static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
881 /*-------------------------------------------------------------------------*/
884 * Driver registration/unregistration.
886 * This is basically hardware-specific; there's usually only one real USB
887 * device (not host) controller since that's how USB devices are intended
888 * to work. So most implementations of these api calls will rely on the
889 * fact that only one driver will ever bind to the hardware. But curious
890 * hardware can be built with discrete components, so the gadget API doesn't
891 * require that assumption.
893 * For this emulator, it might be convenient to create a usb slave device
894 * for each driver that registers: just add to a big root hub.
897 static int dummy_udc_start(struct usb_gadget *g,
898 struct usb_gadget_driver *driver)
900 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
901 struct dummy *dum = dum_hcd->dum;
903 if (driver->max_speed == USB_SPEED_UNKNOWN)
907 * SLAVE side init ... the layer above hardware, which
908 * can't enumerate without help from the driver we're binding.
913 dum->driver = driver;
914 dum->gadget.dev.driver = &driver->driver;
915 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
916 driver->driver.name);
920 static int dummy_udc_stop(struct usb_gadget *g,
921 struct usb_gadget_driver *driver)
923 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
924 struct dummy *dum = dum_hcd->dum;
926 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
927 driver->driver.name);
929 dum->gadget.dev.driver = NULL;
937 /* The gadget structure is stored inside the hcd structure and will be
938 * released along with it. */
939 static void dummy_gadget_release(struct device *dev)
944 static void init_dummy_udc_hw(struct dummy *dum)
948 INIT_LIST_HEAD(&dum->gadget.ep_list);
949 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
950 struct dummy_ep *ep = &dum->ep[i];
954 ep->ep.name = ep_name[i];
955 ep->ep.ops = &dummy_ep_ops;
956 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
957 ep->halted = ep->wedged = ep->already_seen =
959 ep->ep.maxpacket = ~0;
960 ep->ep.max_streams = 16;
961 ep->last_io = jiffies;
962 ep->gadget = &dum->gadget;
964 INIT_LIST_HEAD(&ep->queue);
967 dum->gadget.ep0 = &dum->ep[0].ep;
968 list_del_init(&dum->ep[0].ep.ep_list);
969 INIT_LIST_HEAD(&dum->fifo_req.queue);
971 #ifdef CONFIG_USB_OTG
972 dum->gadget.is_otg = 1;
976 static int dummy_udc_probe(struct platform_device *pdev)
981 dum = *((void **)dev_get_platdata(&pdev->dev));
982 dum->gadget.name = gadget_name;
983 dum->gadget.ops = &dummy_ops;
984 dum->gadget.max_speed = USB_SPEED_SUPER;
986 dev_set_name(&dum->gadget.dev, "gadget");
987 dum->gadget.dev.parent = &pdev->dev;
988 dum->gadget.dev.release = dummy_gadget_release;
989 rc = device_register(&dum->gadget.dev);
991 put_device(&dum->gadget.dev);
995 init_dummy_udc_hw(dum);
997 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1001 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
1004 platform_set_drvdata(pdev, dum);
1008 usb_del_gadget_udc(&dum->gadget);
1010 device_unregister(&dum->gadget.dev);
1014 static int dummy_udc_remove(struct platform_device *pdev)
1016 struct dummy *dum = platform_get_drvdata(pdev);
1018 usb_del_gadget_udc(&dum->gadget);
1019 platform_set_drvdata(pdev, NULL);
1020 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1021 device_unregister(&dum->gadget.dev);
1025 static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1028 spin_lock_irq(&dum->lock);
1029 dum->udc_suspended = suspend;
1030 set_link_state(dum_hcd);
1031 spin_unlock_irq(&dum->lock);
1034 static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1036 struct dummy *dum = platform_get_drvdata(pdev);
1037 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1039 dev_dbg(&pdev->dev, "%s\n", __func__);
1040 dummy_udc_pm(dum, dum_hcd, 1);
1041 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1045 static int dummy_udc_resume(struct platform_device *pdev)
1047 struct dummy *dum = platform_get_drvdata(pdev);
1048 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1050 dev_dbg(&pdev->dev, "%s\n", __func__);
1051 dummy_udc_pm(dum, dum_hcd, 0);
1052 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1056 static struct platform_driver dummy_udc_driver = {
1057 .probe = dummy_udc_probe,
1058 .remove = dummy_udc_remove,
1059 .suspend = dummy_udc_suspend,
1060 .resume = dummy_udc_resume,
1062 .name = (char *) gadget_name,
1063 .owner = THIS_MODULE,
1067 /*-------------------------------------------------------------------------*/
1069 static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1073 index = usb_endpoint_num(desc) << 1;
1074 if (usb_endpoint_dir_in(desc))
1079 /* MASTER/HOST SIDE DRIVER
1081 * this uses the hcd framework to hook up to host side drivers.
1082 * its root hub will only have one device, otherwise it acts like
1083 * a normal host controller.
1085 * when urbs are queued, they're just stuck on a list that we
1086 * scan in a timer callback. that callback connects writes from
1087 * the host with reads from the device, and so on, based on the
1091 static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1093 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1096 if (!usb_endpoint_xfer_bulk(desc))
1099 index = dummy_get_ep_idx(desc);
1100 return (1 << index) & dum_hcd->stream_en_ep;
1104 * The max stream number is saved as a nibble so for the 30 possible endpoints
1105 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1106 * means we use only 1 stream). The maximum according to the spec is 16bit so
1107 * if the 16 stream limit is about to go, the array size should be incremented
1108 * to 30 elements of type u16.
1110 static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1115 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1116 if (usb_pipeout(pipe))
1124 static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1125 unsigned int pipe, unsigned int streams)
1130 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1131 if (usb_pipeout(pipe)) {
1135 max_streams &= 0xf0;
1137 max_streams |= streams;
1138 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1141 static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1143 unsigned int max_streams;
1146 enabled = dummy_ep_stream_en(dum_hcd, urb);
1147 if (!urb->stream_id) {
1155 max_streams = get_max_streams_for_pipe(dum_hcd,
1156 usb_pipeendpoint(urb->pipe));
1157 if (urb->stream_id > max_streams) {
1158 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1166 static int dummy_urb_enqueue(
1167 struct usb_hcd *hcd,
1171 struct dummy_hcd *dum_hcd;
1173 unsigned long flags;
1176 urbp = kmalloc(sizeof *urbp, mem_flags);
1180 urbp->miter_started = 0;
1182 dum_hcd = hcd_to_dummy_hcd(hcd);
1183 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1185 rc = dummy_validate_stream(dum_hcd, urb);
1191 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1197 if (!dum_hcd->udev) {
1198 dum_hcd->udev = urb->dev;
1199 usb_get_dev(dum_hcd->udev);
1200 } else if (unlikely(dum_hcd->udev != urb->dev))
1201 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1203 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1205 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
1206 urb->error_count = 1; /* mark as a new urb */
1208 /* kick the scheduler, it'll do the rest */
1209 if (!timer_pending(&dum_hcd->timer))
1210 mod_timer(&dum_hcd->timer, jiffies + 1);
1213 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1217 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1219 struct dummy_hcd *dum_hcd;
1220 unsigned long flags;
1223 /* giveback happens automatically in timer callback,
1224 * so make sure the callback happens */
1225 dum_hcd = hcd_to_dummy_hcd(hcd);
1226 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1228 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1229 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1230 !list_empty(&dum_hcd->urbp_list))
1231 mod_timer(&dum_hcd->timer, jiffies);
1233 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1237 static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1241 struct urbp *urbp = urb->hcpriv;
1243 struct sg_mapping_iter *miter = &urbp->miter;
1248 to_host = usb_pipein(urb->pipe);
1249 rbuf = req->req.buf + req->req.actual;
1251 if (!urb->num_sgs) {
1252 ubuf = urb->transfer_buffer + urb->actual_length;
1254 memcpy(ubuf, rbuf, len);
1256 memcpy(rbuf, ubuf, len);
1260 if (!urbp->miter_started) {
1261 u32 flags = SG_MITER_ATOMIC;
1264 flags |= SG_MITER_TO_SG;
1266 flags |= SG_MITER_FROM_SG;
1268 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1269 urbp->miter_started = 1;
1271 next_sg = sg_miter_next(miter);
1272 if (next_sg == false) {
1278 this_sg = min_t(u32, len, miter->length);
1279 miter->consumed = this_sg;
1283 memcpy(ubuf, rbuf, this_sg);
1285 memcpy(rbuf, ubuf, this_sg);
1290 next_sg = sg_miter_next(miter);
1291 if (next_sg == false) {
1299 sg_miter_stop(miter);
1303 /* transfer up to a frame's worth; caller must own lock */
1304 static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1305 struct dummy_ep *ep, int limit, int *status)
1307 struct dummy *dum = dum_hcd->dum;
1308 struct dummy_request *req;
1311 /* if there's no request queued, the device is NAKing; return */
1312 list_for_each_entry(req, &ep->queue, queue) {
1313 unsigned host_len, dev_len, len;
1314 int is_short, to_host;
1317 if (dummy_ep_stream_en(dum_hcd, urb)) {
1318 if ((urb->stream_id != req->req.stream_id))
1322 /* 1..N packets of ep->ep.maxpacket each ... the last one
1323 * may be short (including zero length).
1325 * writer can send a zlp explicitly (length 0) or implicitly
1326 * (length mod maxpacket zero, and 'zero' flag); they always
1329 host_len = urb->transfer_buffer_length - urb->actual_length;
1330 dev_len = req->req.length - req->req.actual;
1331 len = min(host_len, dev_len);
1333 /* FIXME update emulated data toggle too */
1335 to_host = usb_pipein(urb->pipe);
1336 if (unlikely(len == 0))
1339 /* not enough bandwidth left? */
1340 if (limit < ep->ep.maxpacket && limit < len)
1342 len = min_t(unsigned, len, limit);
1346 /* use an extra pass for the final short packet */
1347 if (len > ep->ep.maxpacket) {
1349 len -= (len % ep->ep.maxpacket);
1351 is_short = (len % ep->ep.maxpacket) != 0;
1353 len = dummy_perform_transfer(urb, req, len);
1355 ep->last_io = jiffies;
1357 req->req.status = len;
1360 urb->actual_length += len;
1361 req->req.actual += len;
1365 /* short packets terminate, maybe with overflow/underflow.
1366 * it's only really an error to write too much.
1368 * partially filling a buffer optionally blocks queue advances
1369 * (so completion handlers can clean up the queue) but we don't
1370 * need to emulate such data-in-flight.
1373 if (host_len == dev_len) {
1374 req->req.status = 0;
1376 } else if (to_host) {
1377 req->req.status = 0;
1378 if (dev_len > host_len)
1379 *status = -EOVERFLOW;
1382 } else if (!to_host) {
1384 if (host_len > dev_len)
1385 req->req.status = -EOVERFLOW;
1387 req->req.status = 0;
1390 /* many requests terminate without a short packet */
1392 if (req->req.length == req->req.actual
1394 req->req.status = 0;
1395 if (urb->transfer_buffer_length == urb->actual_length
1396 && !(urb->transfer_flags
1401 /* device side completion --> continuable */
1402 if (req->req.status != -EINPROGRESS) {
1403 list_del_init(&req->queue);
1405 spin_unlock(&dum->lock);
1406 req->req.complete(&ep->ep, &req->req);
1407 spin_lock(&dum->lock);
1409 /* requests might have been unlinked... */
1413 /* host side completion --> terminate */
1414 if (*status != -EINPROGRESS)
1417 /* rescan to continue with any other queued i/o */
1424 static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
1426 int limit = ep->ep.maxpacket;
1428 if (dum->gadget.speed == USB_SPEED_HIGH) {
1431 /* high bandwidth mode */
1432 tmp = usb_endpoint_maxp(ep->desc);
1433 tmp = (tmp >> 11) & 0x03;
1434 tmp *= 8 /* applies to entire frame */;
1435 limit += limit * tmp;
1437 if (dum->gadget.speed == USB_SPEED_SUPER) {
1438 switch (usb_endpoint_type(ep->desc)) {
1439 case USB_ENDPOINT_XFER_ISOC:
1440 /* Sec. 4.4.8.2 USB3.0 Spec */
1441 limit = 3 * 16 * 1024 * 8;
1443 case USB_ENDPOINT_XFER_INT:
1444 /* Sec. 4.4.7.2 USB3.0 Spec */
1445 limit = 3 * 1024 * 8;
1447 case USB_ENDPOINT_XFER_BULK:
1455 #define is_active(dum_hcd) ((dum_hcd->port_status & \
1456 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1457 USB_PORT_STAT_SUSPEND)) \
1458 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1460 static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
1464 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1465 dum->ss_hcd : dum->hs_hcd)))
1467 if ((address & ~USB_DIR_IN) == 0)
1469 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1470 struct dummy_ep *ep = &dum->ep[i];
1474 if (ep->desc->bEndpointAddress == address)
1482 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1483 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1484 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1485 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1486 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1487 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1491 * handle_control_request() - handles all control transfers
1492 * @dum: pointer to dummy (the_controller)
1493 * @urb: the urb request to handle
1494 * @setup: pointer to the setup data for a USB device control
1496 * @status: pointer to request handling status
1498 * Return 0 - if the request was handled
1499 * 1 - if the request wasn't handles
1500 * error code on error
1502 static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1503 struct usb_ctrlrequest *setup,
1506 struct dummy_ep *ep2;
1507 struct dummy *dum = dum_hcd->dum;
1512 w_index = le16_to_cpu(setup->wIndex);
1513 w_value = le16_to_cpu(setup->wValue);
1514 switch (setup->bRequest) {
1515 case USB_REQ_SET_ADDRESS:
1516 if (setup->bRequestType != Dev_Request)
1518 dum->address = w_value;
1520 dev_dbg(udc_dev(dum), "set_address = %d\n",
1524 case USB_REQ_SET_FEATURE:
1525 if (setup->bRequestType == Dev_Request) {
1528 case USB_DEVICE_REMOTE_WAKEUP:
1530 case USB_DEVICE_B_HNP_ENABLE:
1531 dum->gadget.b_hnp_enable = 1;
1533 case USB_DEVICE_A_HNP_SUPPORT:
1534 dum->gadget.a_hnp_support = 1;
1536 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1537 dum->gadget.a_alt_hnp_support = 1;
1539 case USB_DEVICE_U1_ENABLE:
1540 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1542 w_value = USB_DEV_STAT_U1_ENABLED;
1544 ret_val = -EOPNOTSUPP;
1546 case USB_DEVICE_U2_ENABLE:
1547 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1549 w_value = USB_DEV_STAT_U2_ENABLED;
1551 ret_val = -EOPNOTSUPP;
1553 case USB_DEVICE_LTM_ENABLE:
1554 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1556 w_value = USB_DEV_STAT_LTM_ENABLED;
1558 ret_val = -EOPNOTSUPP;
1561 ret_val = -EOPNOTSUPP;
1564 dum->devstatus |= (1 << w_value);
1567 } else if (setup->bRequestType == Ep_Request) {
1569 ep2 = find_endpoint(dum, w_index);
1570 if (!ep2 || ep2->ep.name == ep0name) {
1571 ret_val = -EOPNOTSUPP;
1579 case USB_REQ_CLEAR_FEATURE:
1580 if (setup->bRequestType == Dev_Request) {
1583 case USB_DEVICE_REMOTE_WAKEUP:
1584 w_value = USB_DEVICE_REMOTE_WAKEUP;
1586 case USB_DEVICE_U1_ENABLE:
1587 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1589 w_value = USB_DEV_STAT_U1_ENABLED;
1591 ret_val = -EOPNOTSUPP;
1593 case USB_DEVICE_U2_ENABLE:
1594 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1596 w_value = USB_DEV_STAT_U2_ENABLED;
1598 ret_val = -EOPNOTSUPP;
1600 case USB_DEVICE_LTM_ENABLE:
1601 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1603 w_value = USB_DEV_STAT_LTM_ENABLED;
1605 ret_val = -EOPNOTSUPP;
1608 ret_val = -EOPNOTSUPP;
1612 dum->devstatus &= ~(1 << w_value);
1615 } else if (setup->bRequestType == Ep_Request) {
1617 ep2 = find_endpoint(dum, w_index);
1619 ret_val = -EOPNOTSUPP;
1628 case USB_REQ_GET_STATUS:
1629 if (setup->bRequestType == Dev_InRequest
1630 || setup->bRequestType == Intf_InRequest
1631 || setup->bRequestType == Ep_InRequest) {
1634 * device: remote wakeup, selfpowered
1635 * interface: nothing
1638 buf = (char *)urb->transfer_buffer;
1639 if (urb->transfer_buffer_length > 0) {
1640 if (setup->bRequestType == Ep_InRequest) {
1641 ep2 = find_endpoint(dum, w_index);
1643 ret_val = -EOPNOTSUPP;
1646 buf[0] = ep2->halted;
1647 } else if (setup->bRequestType ==
1649 buf[0] = (u8)dum->devstatus;
1653 if (urb->transfer_buffer_length > 1)
1655 urb->actual_length = min_t(u32, 2,
1656 urb->transfer_buffer_length);
1665 /* drive both sides of the transfers; looks like irq handlers to
1666 * both drivers except the callbacks aren't in_irq().
1668 static void dummy_timer(unsigned long _dum_hcd)
1670 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1671 struct dummy *dum = dum_hcd->dum;
1672 struct urbp *urbp, *tmp;
1673 unsigned long flags;
1677 /* simplistic model for one frame's bandwidth */
1678 switch (dum->gadget.speed) {
1680 total = 8/*bytes*/ * 12/*packets*/;
1682 case USB_SPEED_FULL:
1683 total = 64/*bytes*/ * 19/*packets*/;
1685 case USB_SPEED_HIGH:
1686 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1688 case USB_SPEED_SUPER:
1689 /* Bus speed is 500000 bytes/ms, so use a little less */
1693 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1697 /* FIXME if HZ != 1000 this will probably misbehave ... */
1699 /* look at each urb queued by the host side driver */
1700 spin_lock_irqsave(&dum->lock, flags);
1702 if (!dum_hcd->udev) {
1703 dev_err(dummy_dev(dum_hcd),
1704 "timer fired with no URBs pending?\n");
1705 spin_unlock_irqrestore(&dum->lock, flags);
1709 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1712 dum->ep[i].already_seen = 0;
1716 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1718 struct dummy_request *req;
1720 struct dummy_ep *ep = NULL;
1722 int status = -EINPROGRESS;
1727 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1729 type = usb_pipetype(urb->pipe);
1731 /* used up this frame's non-periodic bandwidth?
1732 * FIXME there's infinite bandwidth for control and
1733 * periodic transfers ... unrealistic.
1735 if (total <= 0 && type == PIPE_BULK)
1738 /* find the gadget's ep for this request (if configured) */
1739 address = usb_pipeendpoint (urb->pipe);
1740 if (usb_pipein(urb->pipe))
1741 address |= USB_DIR_IN;
1742 ep = find_endpoint(dum, address);
1744 /* set_configuration() disagreement */
1745 dev_dbg(dummy_dev(dum_hcd),
1746 "no ep configured for urb %p\n",
1752 if (ep->already_seen)
1754 ep->already_seen = 1;
1755 if (ep == &dum->ep[0] && urb->error_count) {
1756 ep->setup_stage = 1; /* a new urb */
1757 urb->error_count = 0;
1759 if (ep->halted && !ep->setup_stage) {
1760 /* NOTE: must not be iso! */
1761 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1766 /* FIXME make sure both ends agree on maxpacket */
1768 /* handle control requests */
1769 if (ep == &dum->ep[0] && ep->setup_stage) {
1770 struct usb_ctrlrequest setup;
1773 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
1774 /* paranoia, in case of stale queued data */
1775 list_for_each_entry(req, &ep->queue, queue) {
1776 list_del_init(&req->queue);
1777 req->req.status = -EOVERFLOW;
1778 dev_dbg(udc_dev(dum), "stale req = %p\n",
1781 spin_unlock(&dum->lock);
1782 req->req.complete(&ep->ep, &req->req);
1783 spin_lock(&dum->lock);
1784 ep->already_seen = 0;
1788 /* gadget driver never sees set_address or operations
1789 * on standard feature flags. some hardware doesn't
1792 ep->last_io = jiffies;
1793 ep->setup_stage = 0;
1796 value = handle_control_request(dum_hcd, urb, &setup,
1799 /* gadget driver handles all other requests. block
1800 * until setup() returns; no reentrancy issues etc.
1803 spin_unlock(&dum->lock);
1804 value = dum->driver->setup(&dum->gadget,
1806 spin_lock(&dum->lock);
1809 /* no delays (max 64KB data stage) */
1811 goto treat_control_like_bulk;
1813 /* error, see below */
1817 if (value != -EOPNOTSUPP)
1818 dev_dbg(udc_dev(dum),
1822 urb->actual_length = 0;
1828 /* non-control requests */
1830 switch (usb_pipetype(urb->pipe)) {
1831 case PIPE_ISOCHRONOUS:
1832 /* FIXME is it urb->interval since the last xfer?
1833 * use urb->iso_frame_desc[i].
1834 * complete whether or not ep has requests queued.
1835 * report random errors, to debug drivers.
1837 limit = max(limit, periodic_bytes(dum, ep));
1841 case PIPE_INTERRUPT:
1842 /* FIXME is it urb->interval since the last xfer?
1843 * this almost certainly polls too fast.
1845 limit = max(limit, periodic_bytes(dum, ep));
1849 treat_control_like_bulk:
1850 ep->last_io = jiffies;
1851 total = transfer(dum_hcd, urb, ep, limit, &status);
1855 /* incomplete transfer? */
1856 if (status == -EINPROGRESS)
1860 list_del(&urbp->urbp_list);
1863 ep->already_seen = ep->setup_stage = 0;
1865 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1866 spin_unlock(&dum->lock);
1867 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1868 spin_lock(&dum->lock);
1873 if (list_empty(&dum_hcd->urbp_list)) {
1874 usb_put_dev(dum_hcd->udev);
1875 dum_hcd->udev = NULL;
1876 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1877 /* want a 1 msec delay here */
1878 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1881 spin_unlock_irqrestore(&dum->lock, flags);
1884 /*-------------------------------------------------------------------------*/
1886 #define PORT_C_MASK \
1887 ((USB_PORT_STAT_C_CONNECTION \
1888 | USB_PORT_STAT_C_ENABLE \
1889 | USB_PORT_STAT_C_SUSPEND \
1890 | USB_PORT_STAT_C_OVERCURRENT \
1891 | USB_PORT_STAT_C_RESET) << 16)
1893 static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
1895 struct dummy_hcd *dum_hcd;
1896 unsigned long flags;
1899 dum_hcd = hcd_to_dummy_hcd(hcd);
1901 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1902 if (!HCD_HW_ACCESSIBLE(hcd))
1905 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1906 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1907 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1908 set_link_state(dum_hcd);
1911 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1913 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1914 dum_hcd->port_status);
1916 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1917 usb_hcd_resume_root_hub(hcd);
1920 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1924 /* usb 3.0 root hub device descriptor */
1926 struct usb_bos_descriptor bos;
1927 struct usb_ss_cap_descriptor ss_cap;
1928 } __packed usb3_bos_desc = {
1931 .bLength = USB_DT_BOS_SIZE,
1932 .bDescriptorType = USB_DT_BOS,
1933 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1934 .bNumDeviceCaps = 1,
1937 .bLength = USB_DT_USB_SS_CAP_SIZE,
1938 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1939 .bDevCapabilityType = USB_SS_CAP_TYPE,
1940 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1941 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1946 ss_hub_descriptor(struct usb_hub_descriptor *desc)
1948 memset(desc, 0, sizeof *desc);
1949 desc->bDescriptorType = 0x2a;
1950 desc->bDescLength = 12;
1951 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1952 desc->bNbrPorts = 1;
1953 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1954 desc->u.ss.DeviceRemovable = 0xffff;
1957 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
1959 memset(desc, 0, sizeof *desc);
1960 desc->bDescriptorType = 0x29;
1961 desc->bDescLength = 9;
1962 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1963 desc->bNbrPorts = 1;
1964 desc->u.hs.DeviceRemovable[0] = 0xff;
1965 desc->u.hs.DeviceRemovable[1] = 0xff;
1968 static int dummy_hub_control(
1969 struct usb_hcd *hcd,
1976 struct dummy_hcd *dum_hcd;
1978 unsigned long flags;
1980 if (!HCD_HW_ACCESSIBLE(hcd))
1983 dum_hcd = hcd_to_dummy_hcd(hcd);
1985 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1987 case ClearHubFeature:
1989 case ClearPortFeature:
1991 case USB_PORT_FEAT_SUSPEND:
1992 if (hcd->speed == HCD_USB3) {
1993 dev_dbg(dummy_dev(dum_hcd),
1994 "USB_PORT_FEAT_SUSPEND req not "
1995 "supported for USB 3.0 roothub\n");
1998 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1999 /* 20msec resume signaling */
2000 dum_hcd->resuming = 1;
2001 dum_hcd->re_timeout = jiffies +
2002 msecs_to_jiffies(20);
2005 case USB_PORT_FEAT_POWER:
2006 if (hcd->speed == HCD_USB3) {
2007 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
2008 dev_dbg(dummy_dev(dum_hcd),
2011 if (dum_hcd->port_status &
2012 USB_SS_PORT_STAT_POWER)
2013 dev_dbg(dummy_dev(dum_hcd),
2017 dum_hcd->port_status &= ~(1 << wValue);
2018 set_link_state(dum_hcd);
2021 case GetHubDescriptor:
2022 if (hcd->speed == HCD_USB3 &&
2023 (wLength < USB_DT_SS_HUB_SIZE ||
2024 wValue != (USB_DT_SS_HUB << 8))) {
2025 dev_dbg(dummy_dev(dum_hcd),
2026 "Wrong hub descriptor type for "
2027 "USB 3.0 roothub.\n");
2030 if (hcd->speed == HCD_USB3)
2031 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2033 hub_descriptor((struct usb_hub_descriptor *) buf);
2036 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2037 if (hcd->speed != HCD_USB3)
2040 if ((wValue >> 8) != USB_DT_BOS)
2043 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2044 retval = sizeof(usb3_bos_desc);
2048 *(__le32 *) buf = cpu_to_le32(0);
2054 /* whoever resets or resumes must GetPortStatus to
2057 if (dum_hcd->resuming &&
2058 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2059 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2060 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
2062 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2063 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2064 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2065 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2066 if (dum_hcd->dum->pullup) {
2067 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
2069 if (hcd->speed < HCD_USB3) {
2070 switch (dum_hcd->dum->gadget.speed) {
2071 case USB_SPEED_HIGH:
2072 dum_hcd->port_status |=
2073 USB_PORT_STAT_HIGH_SPEED;
2076 dum_hcd->dum->gadget.ep0->
2078 dum_hcd->port_status |=
2079 USB_PORT_STAT_LOW_SPEED;
2082 dum_hcd->dum->gadget.speed =
2089 set_link_state(dum_hcd);
2090 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2091 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
2096 case SetPortFeature:
2098 case USB_PORT_FEAT_LINK_STATE:
2099 if (hcd->speed != HCD_USB3) {
2100 dev_dbg(dummy_dev(dum_hcd),
2101 "USB_PORT_FEAT_LINK_STATE req not "
2102 "supported for USB 2.0 roothub\n");
2106 * Since this is dummy we don't have an actual link so
2107 * there is nothing to do for the SET_LINK_STATE cmd
2110 case USB_PORT_FEAT_U1_TIMEOUT:
2111 case USB_PORT_FEAT_U2_TIMEOUT:
2112 /* TODO: add suspend/resume support! */
2113 if (hcd->speed != HCD_USB3) {
2114 dev_dbg(dummy_dev(dum_hcd),
2115 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2116 "supported for USB 2.0 roothub\n");
2120 case USB_PORT_FEAT_SUSPEND:
2121 /* Applicable only for USB2.0 hub */
2122 if (hcd->speed == HCD_USB3) {
2123 dev_dbg(dummy_dev(dum_hcd),
2124 "USB_PORT_FEAT_SUSPEND req not "
2125 "supported for USB 3.0 roothub\n");
2128 if (dum_hcd->active) {
2129 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
2131 /* HNP would happen here; for now we
2132 * assume b_bus_req is always true.
2134 set_link_state(dum_hcd);
2135 if (((1 << USB_DEVICE_B_HNP_ENABLE)
2136 & dum_hcd->dum->devstatus) != 0)
2137 dev_dbg(dummy_dev(dum_hcd),
2141 case USB_PORT_FEAT_POWER:
2142 if (hcd->speed == HCD_USB3)
2143 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2145 dum_hcd->port_status |= USB_PORT_STAT_POWER;
2146 set_link_state(dum_hcd);
2148 case USB_PORT_FEAT_BH_PORT_RESET:
2149 /* Applicable only for USB3.0 hub */
2150 if (hcd->speed != HCD_USB3) {
2151 dev_dbg(dummy_dev(dum_hcd),
2152 "USB_PORT_FEAT_BH_PORT_RESET req not "
2153 "supported for USB 2.0 roothub\n");
2157 case USB_PORT_FEAT_RESET:
2158 /* if it's already enabled, disable */
2159 if (hcd->speed == HCD_USB3) {
2160 dum_hcd->port_status = 0;
2161 dum_hcd->port_status =
2162 (USB_SS_PORT_STAT_POWER |
2163 USB_PORT_STAT_CONNECTION |
2164 USB_PORT_STAT_RESET);
2166 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
2167 | USB_PORT_STAT_LOW_SPEED
2168 | USB_PORT_STAT_HIGH_SPEED);
2170 * We want to reset device status. All but the
2171 * Self powered feature
2173 dum_hcd->dum->devstatus &=
2174 (1 << USB_DEVICE_SELF_POWERED);
2176 * FIXME USB3.0: what is the correct reset signaling
2177 * interval? Is it still 50msec as for HS?
2179 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2182 if (hcd->speed == HCD_USB3) {
2183 if ((dum_hcd->port_status &
2184 USB_SS_PORT_STAT_POWER) != 0) {
2185 dum_hcd->port_status |= (1 << wValue);
2186 set_link_state(dum_hcd);
2189 if ((dum_hcd->port_status &
2190 USB_PORT_STAT_POWER) != 0) {
2191 dum_hcd->port_status |= (1 << wValue);
2192 set_link_state(dum_hcd);
2196 case GetPortErrorCount:
2197 if (hcd->speed != HCD_USB3) {
2198 dev_dbg(dummy_dev(dum_hcd),
2199 "GetPortErrorCount req not "
2200 "supported for USB 2.0 roothub\n");
2203 /* We'll always return 0 since this is a dummy hub */
2204 *(__le32 *) buf = cpu_to_le32(0);
2207 if (hcd->speed != HCD_USB3) {
2208 dev_dbg(dummy_dev(dum_hcd),
2209 "SetHubDepth req not supported for "
2210 "USB 2.0 roothub\n");
2215 dev_dbg(dummy_dev(dum_hcd),
2216 "hub control req%04x v%04x i%04x l%d\n",
2217 typeReq, wValue, wIndex, wLength);
2219 /* "protocol stall" on error */
2222 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2224 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2225 usb_hcd_poll_rh_status(hcd);
2229 static int dummy_bus_suspend(struct usb_hcd *hcd)
2231 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2233 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2235 spin_lock_irq(&dum_hcd->dum->lock);
2236 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2237 set_link_state(dum_hcd);
2238 hcd->state = HC_STATE_SUSPENDED;
2239 spin_unlock_irq(&dum_hcd->dum->lock);
2243 static int dummy_bus_resume(struct usb_hcd *hcd)
2245 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2248 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2250 spin_lock_irq(&dum_hcd->dum->lock);
2251 if (!HCD_HW_ACCESSIBLE(hcd)) {
2254 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2255 set_link_state(dum_hcd);
2256 if (!list_empty(&dum_hcd->urbp_list))
2257 mod_timer(&dum_hcd->timer, jiffies);
2258 hcd->state = HC_STATE_RUNNING;
2260 spin_unlock_irq(&dum_hcd->dum->lock);
2264 /*-------------------------------------------------------------------------*/
2266 static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
2268 int ep = usb_pipeendpoint(urb->pipe);
2270 return snprintf(buf, size,
2271 "urb/%p %s ep%d%s%s len %d/%d\n",
2274 switch (urb->dev->speed) {
2278 case USB_SPEED_FULL:
2281 case USB_SPEED_HIGH:
2284 case USB_SPEED_SUPER:
2291 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
2293 switch (usb_pipetype(urb->pipe)) { \
2294 case PIPE_CONTROL: \
2300 case PIPE_INTERRUPT: \
2307 urb->actual_length, urb->transfer_buffer_length);
2310 static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2313 struct usb_hcd *hcd = dev_get_drvdata(dev);
2314 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2317 unsigned long flags;
2319 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2320 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2323 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
2327 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2331 static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
2333 static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2335 init_timer(&dum_hcd->timer);
2336 dum_hcd->timer.function = dummy_timer;
2337 dum_hcd->timer.data = (unsigned long)dum_hcd;
2338 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2339 dum_hcd->stream_en_ep = 0;
2340 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2341 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2342 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2343 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2344 #ifdef CONFIG_USB_OTG
2345 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2349 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2350 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2353 static int dummy_start(struct usb_hcd *hcd)
2355 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2358 * MASTER side init ... we emulate a root hub that'll only ever
2359 * talk to one device (the slave side). Also appears in sysfs,
2360 * just like more familiar pci-based HCDs.
2362 if (!usb_hcd_is_primary_hcd(hcd))
2363 return dummy_start_ss(dum_hcd);
2365 spin_lock_init(&dum_hcd->dum->lock);
2366 init_timer(&dum_hcd->timer);
2367 dum_hcd->timer.function = dummy_timer;
2368 dum_hcd->timer.data = (unsigned long)dum_hcd;
2369 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2371 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2373 hcd->power_budget = POWER_BUDGET;
2374 hcd->state = HC_STATE_RUNNING;
2375 hcd->uses_new_polling = 1;
2377 #ifdef CONFIG_USB_OTG
2378 hcd->self.otg_port = 1;
2381 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2382 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2385 static void dummy_stop(struct usb_hcd *hcd)
2389 dum = hcd_to_dummy_hcd(hcd)->dum;
2390 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2391 usb_gadget_unregister_driver(dum->driver);
2392 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2395 /*-------------------------------------------------------------------------*/
2397 static int dummy_h_get_frame(struct usb_hcd *hcd)
2399 return dummy_g_get_frame(NULL);
2402 static int dummy_setup(struct usb_hcd *hcd)
2406 dum = *((void **)dev_get_platdata(hcd->self.controller));
2407 hcd->self.sg_tablesize = ~0;
2408 if (usb_hcd_is_primary_hcd(hcd)) {
2409 dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2410 dum->hs_hcd->dum = dum;
2412 * Mark the first roothub as being USB 2.0.
2413 * The USB 3.0 roothub will be registered later by
2416 hcd->speed = HCD_USB2;
2417 hcd->self.root_hub->speed = USB_SPEED_HIGH;
2419 dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2420 dum->ss_hcd->dum = dum;
2421 hcd->speed = HCD_USB3;
2422 hcd->self.root_hub->speed = USB_SPEED_SUPER;
2427 /* Change a group of bulk endpoints to support multiple stream IDs */
2428 static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2429 struct usb_host_endpoint **eps, unsigned int num_eps,
2430 unsigned int num_streams, gfp_t mem_flags)
2432 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2433 unsigned long flags;
2435 int ret_streams = num_streams;
2442 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2443 for (i = 0; i < num_eps; i++) {
2444 index = dummy_get_ep_idx(&eps[i]->desc);
2445 if ((1 << index) & dum_hcd->stream_en_ep) {
2446 ret_streams = -EINVAL;
2449 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2451 ret_streams = -EINVAL;
2454 if (max_stream < ret_streams) {
2455 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2457 eps[i]->desc.bEndpointAddress,
2459 ret_streams = max_stream;
2463 for (i = 0; i < num_eps; i++) {
2464 index = dummy_get_ep_idx(&eps[i]->desc);
2465 dum_hcd->stream_en_ep |= 1 << index;
2466 set_max_streams_for_pipe(dum_hcd,
2467 usb_endpoint_num(&eps[i]->desc), ret_streams);
2470 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2474 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2475 static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2476 struct usb_host_endpoint **eps, unsigned int num_eps,
2479 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2480 unsigned long flags;
2485 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2486 for (i = 0; i < num_eps; i++) {
2487 index = dummy_get_ep_idx(&eps[i]->desc);
2488 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2494 for (i = 0; i < num_eps; i++) {
2495 index = dummy_get_ep_idx(&eps[i]->desc);
2496 dum_hcd->stream_en_ep &= ~(1 << index);
2497 set_max_streams_for_pipe(dum_hcd,
2498 usb_endpoint_num(&eps[i]->desc), 0);
2502 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2506 static struct hc_driver dummy_hcd = {
2507 .description = (char *) driver_name,
2508 .product_desc = "Dummy host controller",
2509 .hcd_priv_size = sizeof(struct dummy_hcd),
2511 .flags = HCD_USB3 | HCD_SHARED,
2513 .reset = dummy_setup,
2514 .start = dummy_start,
2517 .urb_enqueue = dummy_urb_enqueue,
2518 .urb_dequeue = dummy_urb_dequeue,
2520 .get_frame_number = dummy_h_get_frame,
2522 .hub_status_data = dummy_hub_status,
2523 .hub_control = dummy_hub_control,
2524 .bus_suspend = dummy_bus_suspend,
2525 .bus_resume = dummy_bus_resume,
2527 .alloc_streams = dummy_alloc_streams,
2528 .free_streams = dummy_free_streams,
2531 static int dummy_hcd_probe(struct platform_device *pdev)
2534 struct usb_hcd *hs_hcd;
2535 struct usb_hcd *ss_hcd;
2538 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2539 dum = *((void **)dev_get_platdata(&pdev->dev));
2541 if (!mod_data.is_super_speed)
2542 dummy_hcd.flags = HCD_USB2;
2543 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2548 retval = usb_add_hcd(hs_hcd, 0, 0);
2552 if (mod_data.is_super_speed) {
2553 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2554 dev_name(&pdev->dev), hs_hcd);
2557 goto dealloc_usb2_hcd;
2560 retval = usb_add_hcd(ss_hcd, 0, 0);
2567 usb_put_hcd(ss_hcd);
2569 usb_remove_hcd(hs_hcd);
2571 usb_put_hcd(hs_hcd);
2572 dum->hs_hcd = dum->ss_hcd = NULL;
2576 static int dummy_hcd_remove(struct platform_device *pdev)
2580 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
2583 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2584 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2587 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2588 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2596 static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
2598 struct usb_hcd *hcd;
2599 struct dummy_hcd *dum_hcd;
2602 dev_dbg(&pdev->dev, "%s\n", __func__);
2604 hcd = platform_get_drvdata(pdev);
2605 dum_hcd = hcd_to_dummy_hcd(hcd);
2606 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2607 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2610 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2614 static int dummy_hcd_resume(struct platform_device *pdev)
2616 struct usb_hcd *hcd;
2618 dev_dbg(&pdev->dev, "%s\n", __func__);
2620 hcd = platform_get_drvdata(pdev);
2621 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2622 usb_hcd_poll_rh_status(hcd);
2626 static struct platform_driver dummy_hcd_driver = {
2627 .probe = dummy_hcd_probe,
2628 .remove = dummy_hcd_remove,
2629 .suspend = dummy_hcd_suspend,
2630 .resume = dummy_hcd_resume,
2632 .name = (char *) driver_name,
2633 .owner = THIS_MODULE,
2637 /*-------------------------------------------------------------------------*/
2638 #define MAX_NUM_UDC 2
2639 static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2640 static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
2642 static int __init init(void)
2644 int retval = -ENOMEM;
2646 struct dummy *dum[MAX_NUM_UDC];
2651 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2654 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2655 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2660 for (i = 0; i < mod_data.num; i++) {
2661 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2662 if (!the_hcd_pdev[i]) {
2665 platform_device_put(the_hcd_pdev[i--]);
2669 for (i = 0; i < mod_data.num; i++) {
2670 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2671 if (!the_udc_pdev[i]) {
2674 platform_device_put(the_udc_pdev[i--]);
2678 for (i = 0; i < mod_data.num; i++) {
2679 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
2682 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2686 retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2692 retval = platform_driver_register(&dummy_hcd_driver);
2695 retval = platform_driver_register(&dummy_udc_driver);
2697 goto err_register_udc_driver;
2699 for (i = 0; i < mod_data.num; i++) {
2700 retval = platform_device_add(the_hcd_pdev[i]);
2704 platform_device_del(the_hcd_pdev[i--]);
2708 for (i = 0; i < mod_data.num; i++) {
2709 if (!dum[i]->hs_hcd ||
2710 (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2712 * The hcd was added successfully but its probe
2713 * function failed for some reason.
2720 for (i = 0; i < mod_data.num; i++) {
2721 retval = platform_device_add(the_udc_pdev[i]);
2725 platform_device_del(the_udc_pdev[i]);
2730 for (i = 0; i < mod_data.num; i++) {
2731 if (!platform_get_drvdata(the_udc_pdev[i])) {
2733 * The udc was added successfully but its probe
2734 * function failed for some reason.
2743 for (i = 0; i < mod_data.num; i++)
2744 platform_device_del(the_udc_pdev[i]);
2746 for (i = 0; i < mod_data.num; i++)
2747 platform_device_del(the_hcd_pdev[i]);
2749 platform_driver_unregister(&dummy_udc_driver);
2750 err_register_udc_driver:
2751 platform_driver_unregister(&dummy_hcd_driver);
2753 for (i = 0; i < mod_data.num; i++)
2755 for (i = 0; i < mod_data.num; i++)
2756 platform_device_put(the_udc_pdev[i]);
2758 for (i = 0; i < mod_data.num; i++)
2759 platform_device_put(the_hcd_pdev[i]);
2764 static void __exit cleanup(void)
2768 for (i = 0; i < mod_data.num; i++) {
2771 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2773 platform_device_unregister(the_udc_pdev[i]);
2774 platform_device_unregister(the_hcd_pdev[i]);
2777 platform_driver_unregister(&dummy_udc_driver);
2778 platform_driver_unregister(&dummy_hcd_driver);
2780 module_exit(cleanup);