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/system.h>
48 #include <asm/unaligned.h>
50 #define DRIVER_DESC "USB Host+Gadget Emulator"
51 #define DRIVER_VERSION "02 May 2005"
53 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
55 static const char driver_name[] = "dummy_hcd";
56 static const char driver_desc[] = "USB Host+Gadget Emulator";
58 static const char gadget_name[] = "dummy_udc";
60 MODULE_DESCRIPTION(DRIVER_DESC);
61 MODULE_AUTHOR("David Brownell");
62 MODULE_LICENSE("GPL");
64 struct dummy_hcd_module_parameters {
69 static struct dummy_hcd_module_parameters mod_data = {
70 .is_super_speed = false,
71 .is_high_speed = true,
73 module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
74 MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
75 module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
76 MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
77 /*-------------------------------------------------------------------------*/
79 /* gadget side driver data structres */
81 struct list_head queue;
82 unsigned long last_io; /* jiffies timestamp */
83 struct usb_gadget *gadget;
84 const struct usb_endpoint_descriptor *desc;
88 unsigned already_seen:1;
89 unsigned setup_stage:1;
93 struct dummy_request {
94 struct list_head queue; /* ep's requests */
95 struct usb_request req;
98 static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
100 return container_of(_ep, struct dummy_ep, ep);
103 static inline struct dummy_request *usb_request_to_dummy_request
104 (struct usb_request *_req)
106 return container_of(_req, struct dummy_request, req);
109 /*-------------------------------------------------------------------------*/
112 * Every device has ep0 for control requests, plus up to 30 more endpoints,
113 * in one of two types:
115 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
116 * number can be changed. Names like "ep-a" are used for this type.
118 * - Fixed Function: in other cases. some characteristics may be mutable;
119 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
121 * Gadget drivers are responsible for not setting up conflicting endpoint
122 * configurations, illegal or unsupported packet lengths, and so on.
125 static const char ep0name[] = "ep0";
127 static const char *const ep_name[] = {
128 ep0name, /* everyone has ep0 */
130 /* act like a net2280: high speed, six configurable endpoints */
131 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
133 /* or like pxa250: fifteen fixed function endpoints */
134 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
135 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
136 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
139 /* or like sa1100: two fixed function endpoints */
140 "ep1out-bulk", "ep2in-bulk",
142 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
144 /*-------------------------------------------------------------------------*/
150 struct list_head urbp_list;
151 struct sg_mapping_iter miter;
156 enum dummy_rh_state {
164 enum dummy_rh_state rh_state;
165 struct timer_list timer;
168 unsigned long re_timeout;
170 struct usb_device *udev;
171 struct list_head urbp_list;
173 u8 num_stream[30 / 2];
176 unsigned old_active:1;
184 * SLAVE/GADGET side support
186 struct dummy_ep ep[DUMMY_ENDPOINTS];
188 struct usb_gadget gadget;
189 struct usb_gadget_driver *driver;
190 struct dummy_request fifo_req;
191 u8 fifo_buf[FIFO_SIZE];
193 unsigned udc_suspended:1;
197 * MASTER/HOST side support
199 struct dummy_hcd *hs_hcd;
200 struct dummy_hcd *ss_hcd;
203 static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
205 return (struct dummy_hcd *) (hcd->hcd_priv);
208 static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
210 return container_of((void *) dum, struct usb_hcd, hcd_priv);
213 static inline struct device *dummy_dev(struct dummy_hcd *dum)
215 return dummy_hcd_to_hcd(dum)->self.controller;
218 static inline struct device *udc_dev(struct dummy *dum)
220 return dum->gadget.dev.parent;
223 static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
225 return container_of(ep->gadget, struct dummy, gadget);
228 static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
230 struct dummy *dum = container_of(gadget, struct dummy, gadget);
231 if (dum->gadget.speed == USB_SPEED_SUPER)
237 static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
239 return container_of(dev, struct dummy, gadget.dev);
242 static struct dummy the_controller;
244 /*-------------------------------------------------------------------------*/
246 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
248 /* called with spinlock held */
249 static void nuke(struct dummy *dum, struct dummy_ep *ep)
251 while (!list_empty(&ep->queue)) {
252 struct dummy_request *req;
254 req = list_entry(ep->queue.next, struct dummy_request, queue);
255 list_del_init(&req->queue);
256 req->req.status = -ESHUTDOWN;
258 spin_unlock(&dum->lock);
259 req->req.complete(&ep->ep, &req->req);
260 spin_lock(&dum->lock);
264 /* caller must hold lock */
265 static void stop_activity(struct dummy *dum)
269 /* prevent any more requests */
272 /* The timer is left running so that outstanding URBs can fail */
274 /* nuke any pending requests first, so driver i/o is quiesced */
275 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
278 /* driver now does any non-usb quiescing necessary */
282 * set_link_state_by_speed() - Sets the current state of the link according to
284 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
286 * This function updates the port_status according to the link state and the
289 static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
291 struct dummy *dum = dum_hcd->dum;
293 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
294 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
295 dum_hcd->port_status = 0;
296 } else if (!dum->pullup || dum->udc_suspended) {
297 /* UDC suspend must cause a disconnect */
298 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
299 USB_PORT_STAT_ENABLE);
300 if ((dum_hcd->old_status &
301 USB_PORT_STAT_CONNECTION) != 0)
302 dum_hcd->port_status |=
303 (USB_PORT_STAT_C_CONNECTION << 16);
305 /* device is connected and not suspended */
306 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
307 USB_PORT_STAT_SPEED_5GBPS) ;
308 if ((dum_hcd->old_status &
309 USB_PORT_STAT_CONNECTION) == 0)
310 dum_hcd->port_status |=
311 (USB_PORT_STAT_C_CONNECTION << 16);
312 if ((dum_hcd->port_status &
313 USB_PORT_STAT_ENABLE) == 1 &&
314 (dum_hcd->port_status &
315 USB_SS_PORT_LS_U0) == 1 &&
316 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
320 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
321 dum_hcd->port_status = 0;
322 } else if (!dum->pullup || dum->udc_suspended) {
323 /* UDC suspend must cause a disconnect */
324 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
325 USB_PORT_STAT_ENABLE |
326 USB_PORT_STAT_LOW_SPEED |
327 USB_PORT_STAT_HIGH_SPEED |
328 USB_PORT_STAT_SUSPEND);
329 if ((dum_hcd->old_status &
330 USB_PORT_STAT_CONNECTION) != 0)
331 dum_hcd->port_status |=
332 (USB_PORT_STAT_C_CONNECTION << 16);
334 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
335 if ((dum_hcd->old_status &
336 USB_PORT_STAT_CONNECTION) == 0)
337 dum_hcd->port_status |=
338 (USB_PORT_STAT_C_CONNECTION << 16);
339 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
340 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
341 else if ((dum_hcd->port_status &
342 USB_PORT_STAT_SUSPEND) == 0 &&
343 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
349 /* caller must hold lock */
350 static void set_link_state(struct dummy_hcd *dum_hcd)
352 struct dummy *dum = dum_hcd->dum;
356 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
357 dum->gadget.speed != USB_SPEED_SUPER) ||
358 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
359 dum->gadget.speed == USB_SPEED_SUPER))
362 set_link_state_by_speed(dum_hcd);
364 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
366 dum_hcd->resuming = 0;
368 /* if !connected or reset */
369 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
370 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
372 * We're connected and not reset (reset occurred now),
373 * and driver attached - disconnect!
375 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
376 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
379 spin_unlock(&dum->lock);
380 dum->driver->disconnect(&dum->gadget);
381 spin_lock(&dum->lock);
383 } else if (dum_hcd->active != dum_hcd->old_active) {
384 if (dum_hcd->old_active && dum->driver->suspend) {
385 spin_unlock(&dum->lock);
386 dum->driver->suspend(&dum->gadget);
387 spin_lock(&dum->lock);
388 } else if (!dum_hcd->old_active && dum->driver->resume) {
389 spin_unlock(&dum->lock);
390 dum->driver->resume(&dum->gadget);
391 spin_lock(&dum->lock);
395 dum_hcd->old_status = dum_hcd->port_status;
396 dum_hcd->old_active = dum_hcd->active;
399 /*-------------------------------------------------------------------------*/
401 /* SLAVE/GADGET SIDE DRIVER
403 * This only tracks gadget state. All the work is done when the host
404 * side tries some (emulated) i/o operation. Real device controller
405 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
408 #define is_enabled(dum) \
409 (dum->port_status & USB_PORT_STAT_ENABLE)
411 static int dummy_enable(struct usb_ep *_ep,
412 const struct usb_endpoint_descriptor *desc)
415 struct dummy_hcd *dum_hcd;
420 ep = usb_ep_to_dummy_ep(_ep);
421 if (!_ep || !desc || ep->desc || _ep->name == ep0name
422 || desc->bDescriptorType != USB_DT_ENDPOINT)
424 dum = ep_to_dummy(ep);
428 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
429 if (!is_enabled(dum_hcd))
433 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
434 * maximum packet size.
435 * For SS devices the wMaxPacketSize is limited by 1024.
437 max = usb_endpoint_maxp(desc) & 0x7ff;
439 /* drivers must not request bad settings, since lower levels
440 * (hardware or its drivers) may not check. some endpoints
441 * can't do iso, many have maxpacket limitations, etc.
443 * since this "hardware" driver is here to help debugging, we
444 * have some extra sanity checks. (there could be more though,
445 * especially for "ep9out" style fixed function ones.)
448 switch (usb_endpoint_type(desc)) {
449 case USB_ENDPOINT_XFER_BULK:
450 if (strstr(ep->ep.name, "-iso")
451 || strstr(ep->ep.name, "-int")) {
454 switch (dum->gadget.speed) {
455 case USB_SPEED_SUPER:
464 if (max == 8 || max == 16 || max == 32 || max == 64)
465 /* we'll fake any legal size */
467 /* save a return statement */
472 case USB_ENDPOINT_XFER_INT:
473 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
475 /* real hardware might not handle all packet sizes */
476 switch (dum->gadget.speed) {
477 case USB_SPEED_SUPER:
481 /* save a return statement */
485 /* save a return statement */
492 case USB_ENDPOINT_XFER_ISOC:
493 if (strstr(ep->ep.name, "-bulk")
494 || strstr(ep->ep.name, "-int"))
496 /* real hardware might not handle all packet sizes */
497 switch (dum->gadget.speed) {
498 case USB_SPEED_SUPER:
502 /* save a return statement */
506 /* save a return statement */
512 /* few chips support control except on ep0 */
516 _ep->maxpacket = max;
517 if (usb_ss_max_streams(_ep->comp_desc)) {
518 if (!usb_endpoint_xfer_bulk(desc)) {
519 dev_err(udc_dev(dum), "Can't enable stream support on "
520 "non-bulk ep %s\n", _ep->name);
527 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
529 desc->bEndpointAddress & 0x0f,
530 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
532 switch (usb_endpoint_type(desc)) {
533 case USB_ENDPOINT_XFER_BULK:
536 case USB_ENDPOINT_XFER_ISOC:
539 case USB_ENDPOINT_XFER_INT:
546 max, ep->stream_en ? "enabled" : "disabled");
548 /* at this point real hardware should be NAKing transfers
549 * to that endpoint, until a buffer is queued to it.
551 ep->halted = ep->wedged = 0;
557 static int dummy_disable(struct usb_ep *_ep)
564 ep = usb_ep_to_dummy_ep(_ep);
565 if (!_ep || !ep->desc || _ep->name == ep0name)
567 dum = ep_to_dummy(ep);
569 spin_lock_irqsave(&dum->lock, flags);
574 spin_unlock_irqrestore(&dum->lock, flags);
576 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
580 static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
584 struct dummy_request *req;
588 ep = usb_ep_to_dummy_ep(_ep);
590 req = kzalloc(sizeof(*req), mem_flags);
593 INIT_LIST_HEAD(&req->queue);
597 static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
600 struct dummy_request *req;
604 ep = usb_ep_to_dummy_ep(_ep);
605 if (!ep->desc && _ep->name != ep0name)
608 req = usb_request_to_dummy_request(_req);
609 WARN_ON(!list_empty(&req->queue));
613 static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
617 static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
621 struct dummy_request *req;
623 struct dummy_hcd *dum_hcd;
626 req = usb_request_to_dummy_request(_req);
627 if (!_req || !list_empty(&req->queue) || !_req->complete)
630 ep = usb_ep_to_dummy_ep(_ep);
631 if (!_ep || (!ep->desc && _ep->name != ep0name))
634 dum = ep_to_dummy(ep);
635 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
636 if (!dum->driver || !is_enabled(dum_hcd))
640 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
641 ep, _req, _ep->name, _req->length, _req->buf);
643 _req->status = -EINPROGRESS;
645 spin_lock_irqsave(&dum->lock, flags);
647 /* implement an emulated single-request FIFO */
648 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
649 list_empty(&dum->fifo_req.queue) &&
650 list_empty(&ep->queue) &&
651 _req->length <= FIFO_SIZE) {
652 req = &dum->fifo_req;
654 req->req.buf = dum->fifo_buf;
655 memcpy(dum->fifo_buf, _req->buf, _req->length);
656 req->req.context = dum;
657 req->req.complete = fifo_complete;
659 list_add_tail(&req->queue, &ep->queue);
660 spin_unlock(&dum->lock);
661 _req->actual = _req->length;
663 _req->complete(_ep, _req);
664 spin_lock(&dum->lock);
666 list_add_tail(&req->queue, &ep->queue);
667 spin_unlock_irqrestore(&dum->lock, flags);
669 /* real hardware would likely enable transfers here, in case
670 * it'd been left NAKing.
675 static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
679 int retval = -EINVAL;
681 struct dummy_request *req = NULL;
685 ep = usb_ep_to_dummy_ep(_ep);
686 dum = ep_to_dummy(ep);
691 local_irq_save(flags);
692 spin_lock(&dum->lock);
693 list_for_each_entry(req, &ep->queue, queue) {
694 if (&req->req == _req) {
695 list_del_init(&req->queue);
696 _req->status = -ECONNRESET;
701 spin_unlock(&dum->lock);
704 dev_dbg(udc_dev(dum),
705 "dequeued req %p from %s, len %d buf %p\n",
706 req, _ep->name, _req->length, _req->buf);
707 _req->complete(_ep, _req);
709 local_irq_restore(flags);
714 dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
721 ep = usb_ep_to_dummy_ep(_ep);
722 dum = ep_to_dummy(ep);
726 ep->halted = ep->wedged = 0;
727 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
728 !list_empty(&ep->queue))
735 /* FIXME clear emulated data toggle too */
740 dummy_set_halt(struct usb_ep *_ep, int value)
742 return dummy_set_halt_and_wedge(_ep, value, 0);
745 static int dummy_set_wedge(struct usb_ep *_ep)
747 if (!_ep || _ep->name == ep0name)
749 return dummy_set_halt_and_wedge(_ep, 1, 1);
752 static const struct usb_ep_ops dummy_ep_ops = {
753 .enable = dummy_enable,
754 .disable = dummy_disable,
756 .alloc_request = dummy_alloc_request,
757 .free_request = dummy_free_request,
759 .queue = dummy_queue,
760 .dequeue = dummy_dequeue,
762 .set_halt = dummy_set_halt,
763 .set_wedge = dummy_set_wedge,
766 /*-------------------------------------------------------------------------*/
768 /* there are both host and device side versions of this call ... */
769 static int dummy_g_get_frame(struct usb_gadget *_gadget)
773 do_gettimeofday(&tv);
774 return tv.tv_usec / 1000;
777 static int dummy_wakeup(struct usb_gadget *_gadget)
779 struct dummy_hcd *dum_hcd;
781 dum_hcd = gadget_to_dummy_hcd(_gadget);
782 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
783 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
785 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
787 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
788 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
791 /* FIXME: What if the root hub is suspended but the port isn't? */
793 /* hub notices our request, issues downstream resume, etc */
794 dum_hcd->resuming = 1;
795 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
796 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
800 static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
804 dum = gadget_to_dummy_hcd(_gadget)->dum;
806 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
808 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
812 static void dummy_udc_update_ep0(struct dummy *dum)
814 if (dum->gadget.speed == USB_SPEED_SUPER)
815 dum->ep[0].ep.maxpacket = 9;
817 dum->ep[0].ep.maxpacket = 64;
820 static int dummy_pullup(struct usb_gadget *_gadget, int value)
822 struct dummy_hcd *dum_hcd;
826 dum = gadget_dev_to_dummy(&_gadget->dev);
828 if (value && dum->driver) {
829 if (mod_data.is_super_speed)
830 dum->gadget.speed = dum->driver->max_speed;
831 else if (mod_data.is_high_speed)
832 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
833 dum->driver->max_speed);
835 dum->gadget.speed = USB_SPEED_FULL;
836 dummy_udc_update_ep0(dum);
838 if (dum->gadget.speed < dum->driver->max_speed)
839 dev_dbg(udc_dev(dum), "This device can perform faster"
840 " if you connect it to a %s port...\n",
841 usb_speed_string(dum->driver->max_speed));
843 dum_hcd = gadget_to_dummy_hcd(_gadget);
845 spin_lock_irqsave(&dum->lock, flags);
846 dum->pullup = (value != 0);
847 set_link_state(dum_hcd);
848 spin_unlock_irqrestore(&dum->lock, flags);
850 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
854 static int dummy_udc_start(struct usb_gadget *g,
855 struct usb_gadget_driver *driver);
856 static int dummy_udc_stop(struct usb_gadget *g,
857 struct usb_gadget_driver *driver);
859 static const struct usb_gadget_ops dummy_ops = {
860 .get_frame = dummy_g_get_frame,
861 .wakeup = dummy_wakeup,
862 .set_selfpowered = dummy_set_selfpowered,
863 .pullup = dummy_pullup,
864 .udc_start = dummy_udc_start,
865 .udc_stop = dummy_udc_stop,
868 /*-------------------------------------------------------------------------*/
870 /* "function" sysfs attribute */
871 static ssize_t show_function(struct device *dev, struct device_attribute *attr,
874 struct dummy *dum = gadget_dev_to_dummy(dev);
876 if (!dum->driver || !dum->driver->function)
878 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
880 static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
882 /*-------------------------------------------------------------------------*/
885 * Driver registration/unregistration.
887 * This is basically hardware-specific; there's usually only one real USB
888 * device (not host) controller since that's how USB devices are intended
889 * to work. So most implementations of these api calls will rely on the
890 * fact that only one driver will ever bind to the hardware. But curious
891 * hardware can be built with discrete components, so the gadget API doesn't
892 * require that assumption.
894 * For this emulator, it might be convenient to create a usb slave device
895 * for each driver that registers: just add to a big root hub.
898 static int dummy_udc_start(struct usb_gadget *g,
899 struct usb_gadget_driver *driver)
901 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
902 struct dummy *dum = dum_hcd->dum;
904 if (driver->max_speed == USB_SPEED_UNKNOWN)
908 * SLAVE side init ... the layer above hardware, which
909 * can't enumerate without help from the driver we're binding.
914 dum->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);
931 dummy_pullup(&dum->gadget, 0);
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)
978 struct dummy *dum = &the_controller;
981 dum->gadget.name = gadget_name;
982 dum->gadget.ops = &dummy_ops;
983 dum->gadget.max_speed = USB_SPEED_SUPER;
985 dev_set_name(&dum->gadget.dev, "gadget");
986 dum->gadget.dev.parent = &pdev->dev;
987 dum->gadget.dev.release = dummy_gadget_release;
988 rc = device_register(&dum->gadget.dev);
990 put_device(&dum->gadget.dev);
994 init_dummy_udc_hw(dum);
996 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1000 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
1003 platform_set_drvdata(pdev, dum);
1007 usb_del_gadget_udc(&dum->gadget);
1009 device_unregister(&dum->gadget.dev);
1013 static int dummy_udc_remove(struct platform_device *pdev)
1015 struct dummy *dum = platform_get_drvdata(pdev);
1017 usb_del_gadget_udc(&dum->gadget);
1018 platform_set_drvdata(pdev, NULL);
1019 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1020 device_unregister(&dum->gadget.dev);
1024 static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1027 spin_lock_irq(&dum->lock);
1028 dum->udc_suspended = suspend;
1029 set_link_state(dum_hcd);
1030 spin_unlock_irq(&dum->lock);
1033 static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1035 struct dummy *dum = platform_get_drvdata(pdev);
1036 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1038 dev_dbg(&pdev->dev, "%s\n", __func__);
1039 dummy_udc_pm(dum, dum_hcd, 1);
1040 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1044 static int dummy_udc_resume(struct platform_device *pdev)
1046 struct dummy *dum = platform_get_drvdata(pdev);
1047 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1049 dev_dbg(&pdev->dev, "%s\n", __func__);
1050 dummy_udc_pm(dum, dum_hcd, 0);
1051 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1055 static struct platform_driver dummy_udc_driver = {
1056 .probe = dummy_udc_probe,
1057 .remove = dummy_udc_remove,
1058 .suspend = dummy_udc_suspend,
1059 .resume = dummy_udc_resume,
1061 .name = (char *) gadget_name,
1062 .owner = THIS_MODULE,
1066 /*-------------------------------------------------------------------------*/
1068 static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1072 index = usb_endpoint_num(desc) << 1;
1073 if (usb_endpoint_dir_in(desc))
1078 /* MASTER/HOST SIDE DRIVER
1080 * this uses the hcd framework to hook up to host side drivers.
1081 * its root hub will only have one device, otherwise it acts like
1082 * a normal host controller.
1084 * when urbs are queued, they're just stuck on a list that we
1085 * scan in a timer callback. that callback connects writes from
1086 * the host with reads from the device, and so on, based on the
1090 static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1092 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1095 if (!usb_endpoint_xfer_bulk(desc))
1098 index = dummy_get_ep_idx(desc);
1099 return (1 << index) & dum_hcd->stream_en_ep;
1103 * The max stream number is saved as a nibble so for the 30 possible endpoints
1104 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1105 * means we use only 1 stream). The maximum according to the spec is 16bit so
1106 * if the 16 stream limit is about to go, the array size should be incremented
1107 * to 30 elements of type u16.
1109 static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1114 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1115 if (usb_pipeout(pipe))
1123 static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1124 unsigned int pipe, unsigned int streams)
1129 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1130 if (usb_pipeout(pipe)) {
1134 max_streams &= 0xf0;
1136 max_streams |= streams;
1137 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1140 static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1142 unsigned int max_streams;
1145 enabled = dummy_ep_stream_en(dum_hcd, urb);
1146 if (!urb->stream_id) {
1154 max_streams = get_max_streams_for_pipe(dum_hcd,
1155 usb_pipeendpoint(urb->pipe));
1156 if (urb->stream_id > max_streams) {
1157 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1165 static int dummy_urb_enqueue(
1166 struct usb_hcd *hcd,
1170 struct dummy_hcd *dum_hcd;
1172 unsigned long flags;
1175 urbp = kmalloc(sizeof *urbp, mem_flags);
1179 urbp->miter_started = 0;
1181 dum_hcd = hcd_to_dummy_hcd(hcd);
1182 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1184 rc = dummy_validate_stream(dum_hcd, urb);
1190 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1196 if (!dum_hcd->udev) {
1197 dum_hcd->udev = urb->dev;
1198 usb_get_dev(dum_hcd->udev);
1199 } else if (unlikely(dum_hcd->udev != urb->dev))
1200 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1202 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1204 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
1205 urb->error_count = 1; /* mark as a new urb */
1207 /* kick the scheduler, it'll do the rest */
1208 if (!timer_pending(&dum_hcd->timer))
1209 mod_timer(&dum_hcd->timer, jiffies + 1);
1212 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1216 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1218 struct dummy_hcd *dum_hcd;
1219 unsigned long flags;
1222 /* giveback happens automatically in timer callback,
1223 * so make sure the callback happens */
1224 dum_hcd = hcd_to_dummy_hcd(hcd);
1225 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1227 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1228 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1229 !list_empty(&dum_hcd->urbp_list))
1230 mod_timer(&dum_hcd->timer, jiffies);
1232 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1236 static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1240 struct urbp *urbp = urb->hcpriv;
1242 struct sg_mapping_iter *miter = &urbp->miter;
1247 to_host = usb_pipein(urb->pipe);
1248 rbuf = req->req.buf + req->req.actual;
1250 if (!urb->num_sgs) {
1251 ubuf = urb->transfer_buffer + urb->actual_length;
1253 memcpy(ubuf, rbuf, len);
1255 memcpy(rbuf, ubuf, len);
1259 if (!urbp->miter_started) {
1260 u32 flags = SG_MITER_ATOMIC;
1263 flags |= SG_MITER_TO_SG;
1265 flags |= SG_MITER_FROM_SG;
1267 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1268 urbp->miter_started = 1;
1270 next_sg = sg_miter_next(miter);
1271 if (next_sg == false) {
1277 this_sg = min_t(u32, len, miter->length);
1278 miter->consumed = this_sg;
1282 memcpy(ubuf, rbuf, this_sg);
1284 memcpy(rbuf, ubuf, this_sg);
1289 next_sg = sg_miter_next(miter);
1290 if (next_sg == false) {
1298 sg_miter_stop(miter);
1302 /* transfer up to a frame's worth; caller must own lock */
1303 static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1304 struct dummy_ep *ep, int limit, int *status)
1306 struct dummy *dum = dum_hcd->dum;
1307 struct dummy_request *req;
1310 /* if there's no request queued, the device is NAKing; return */
1311 list_for_each_entry(req, &ep->queue, queue) {
1312 unsigned host_len, dev_len, len;
1313 int is_short, to_host;
1316 if (dummy_ep_stream_en(dum_hcd, urb)) {
1317 if ((urb->stream_id != req->req.stream_id))
1321 /* 1..N packets of ep->ep.maxpacket each ... the last one
1322 * may be short (including zero length).
1324 * writer can send a zlp explicitly (length 0) or implicitly
1325 * (length mod maxpacket zero, and 'zero' flag); they always
1328 host_len = urb->transfer_buffer_length - urb->actual_length;
1329 dev_len = req->req.length - req->req.actual;
1330 len = min(host_len, dev_len);
1332 /* FIXME update emulated data toggle too */
1334 to_host = usb_pipein(urb->pipe);
1335 if (unlikely(len == 0))
1338 /* not enough bandwidth left? */
1339 if (limit < ep->ep.maxpacket && limit < len)
1341 len = min_t(unsigned, len, limit);
1345 /* use an extra pass for the final short packet */
1346 if (len > ep->ep.maxpacket) {
1348 len -= (len % ep->ep.maxpacket);
1350 is_short = (len % ep->ep.maxpacket) != 0;
1352 len = dummy_perform_transfer(urb, req, len);
1354 ep->last_io = jiffies;
1356 req->req.status = len;
1359 urb->actual_length += len;
1360 req->req.actual += len;
1364 /* short packets terminate, maybe with overflow/underflow.
1365 * it's only really an error to write too much.
1367 * partially filling a buffer optionally blocks queue advances
1368 * (so completion handlers can clean up the queue) but we don't
1369 * need to emulate such data-in-flight.
1372 if (host_len == dev_len) {
1373 req->req.status = 0;
1375 } else if (to_host) {
1376 req->req.status = 0;
1377 if (dev_len > host_len)
1378 *status = -EOVERFLOW;
1381 } else if (!to_host) {
1383 if (host_len > dev_len)
1384 req->req.status = -EOVERFLOW;
1386 req->req.status = 0;
1389 /* many requests terminate without a short packet */
1391 if (req->req.length == req->req.actual
1393 req->req.status = 0;
1394 if (urb->transfer_buffer_length == urb->actual_length
1395 && !(urb->transfer_flags
1400 /* device side completion --> continuable */
1401 if (req->req.status != -EINPROGRESS) {
1402 list_del_init(&req->queue);
1404 spin_unlock(&dum->lock);
1405 req->req.complete(&ep->ep, &req->req);
1406 spin_lock(&dum->lock);
1408 /* requests might have been unlinked... */
1412 /* host side completion --> terminate */
1413 if (*status != -EINPROGRESS)
1416 /* rescan to continue with any other queued i/o */
1423 static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
1425 int limit = ep->ep.maxpacket;
1427 if (dum->gadget.speed == USB_SPEED_HIGH) {
1430 /* high bandwidth mode */
1431 tmp = usb_endpoint_maxp(ep->desc);
1432 tmp = (tmp >> 11) & 0x03;
1433 tmp *= 8 /* applies to entire frame */;
1434 limit += limit * tmp;
1436 if (dum->gadget.speed == USB_SPEED_SUPER) {
1437 switch (usb_endpoint_type(ep->desc)) {
1438 case USB_ENDPOINT_XFER_ISOC:
1439 /* Sec. 4.4.8.2 USB3.0 Spec */
1440 limit = 3 * 16 * 1024 * 8;
1442 case USB_ENDPOINT_XFER_INT:
1443 /* Sec. 4.4.7.2 USB3.0 Spec */
1444 limit = 3 * 1024 * 8;
1446 case USB_ENDPOINT_XFER_BULK:
1454 #define is_active(dum_hcd) ((dum_hcd->port_status & \
1455 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1456 USB_PORT_STAT_SUSPEND)) \
1457 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1459 static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
1463 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1464 dum->ss_hcd : dum->hs_hcd)))
1466 if ((address & ~USB_DIR_IN) == 0)
1468 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1469 struct dummy_ep *ep = &dum->ep[i];
1473 if (ep->desc->bEndpointAddress == address)
1481 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1482 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1483 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1484 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1485 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1486 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1490 * handle_control_request() - handles all control transfers
1491 * @dum: pointer to dummy (the_controller)
1492 * @urb: the urb request to handle
1493 * @setup: pointer to the setup data for a USB device control
1495 * @status: pointer to request handling status
1497 * Return 0 - if the request was handled
1498 * 1 - if the request wasn't handles
1499 * error code on error
1501 static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1502 struct usb_ctrlrequest *setup,
1505 struct dummy_ep *ep2;
1506 struct dummy *dum = dum_hcd->dum;
1511 w_index = le16_to_cpu(setup->wIndex);
1512 w_value = le16_to_cpu(setup->wValue);
1513 switch (setup->bRequest) {
1514 case USB_REQ_SET_ADDRESS:
1515 if (setup->bRequestType != Dev_Request)
1517 dum->address = w_value;
1519 dev_dbg(udc_dev(dum), "set_address = %d\n",
1523 case USB_REQ_SET_FEATURE:
1524 if (setup->bRequestType == Dev_Request) {
1527 case USB_DEVICE_REMOTE_WAKEUP:
1529 case USB_DEVICE_B_HNP_ENABLE:
1530 dum->gadget.b_hnp_enable = 1;
1532 case USB_DEVICE_A_HNP_SUPPORT:
1533 dum->gadget.a_hnp_support = 1;
1535 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1536 dum->gadget.a_alt_hnp_support = 1;
1538 case USB_DEVICE_U1_ENABLE:
1539 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1541 w_value = USB_DEV_STAT_U1_ENABLED;
1543 ret_val = -EOPNOTSUPP;
1545 case USB_DEVICE_U2_ENABLE:
1546 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1548 w_value = USB_DEV_STAT_U2_ENABLED;
1550 ret_val = -EOPNOTSUPP;
1552 case USB_DEVICE_LTM_ENABLE:
1553 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1555 w_value = USB_DEV_STAT_LTM_ENABLED;
1557 ret_val = -EOPNOTSUPP;
1560 ret_val = -EOPNOTSUPP;
1563 dum->devstatus |= (1 << w_value);
1566 } else if (setup->bRequestType == Ep_Request) {
1568 ep2 = find_endpoint(dum, w_index);
1569 if (!ep2 || ep2->ep.name == ep0name) {
1570 ret_val = -EOPNOTSUPP;
1578 case USB_REQ_CLEAR_FEATURE:
1579 if (setup->bRequestType == Dev_Request) {
1582 case USB_DEVICE_REMOTE_WAKEUP:
1583 w_value = USB_DEVICE_REMOTE_WAKEUP;
1585 case USB_DEVICE_U1_ENABLE:
1586 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1588 w_value = USB_DEV_STAT_U1_ENABLED;
1590 ret_val = -EOPNOTSUPP;
1592 case USB_DEVICE_U2_ENABLE:
1593 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1595 w_value = USB_DEV_STAT_U2_ENABLED;
1597 ret_val = -EOPNOTSUPP;
1599 case USB_DEVICE_LTM_ENABLE:
1600 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1602 w_value = USB_DEV_STAT_LTM_ENABLED;
1604 ret_val = -EOPNOTSUPP;
1607 ret_val = -EOPNOTSUPP;
1611 dum->devstatus &= ~(1 << w_value);
1614 } else if (setup->bRequestType == Ep_Request) {
1616 ep2 = find_endpoint(dum, w_index);
1618 ret_val = -EOPNOTSUPP;
1627 case USB_REQ_GET_STATUS:
1628 if (setup->bRequestType == Dev_InRequest
1629 || setup->bRequestType == Intf_InRequest
1630 || setup->bRequestType == Ep_InRequest) {
1633 * device: remote wakeup, selfpowered
1634 * interface: nothing
1637 buf = (char *)urb->transfer_buffer;
1638 if (urb->transfer_buffer_length > 0) {
1639 if (setup->bRequestType == Ep_InRequest) {
1640 ep2 = find_endpoint(dum, w_index);
1642 ret_val = -EOPNOTSUPP;
1645 buf[0] = ep2->halted;
1646 } else if (setup->bRequestType ==
1648 buf[0] = (u8)dum->devstatus;
1652 if (urb->transfer_buffer_length > 1)
1654 urb->actual_length = min_t(u32, 2,
1655 urb->transfer_buffer_length);
1664 /* drive both sides of the transfers; looks like irq handlers to
1665 * both drivers except the callbacks aren't in_irq().
1667 static void dummy_timer(unsigned long _dum_hcd)
1669 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1670 struct dummy *dum = dum_hcd->dum;
1671 struct urbp *urbp, *tmp;
1672 unsigned long flags;
1676 /* simplistic model for one frame's bandwidth */
1677 switch (dum->gadget.speed) {
1679 total = 8/*bytes*/ * 12/*packets*/;
1681 case USB_SPEED_FULL:
1682 total = 64/*bytes*/ * 19/*packets*/;
1684 case USB_SPEED_HIGH:
1685 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1687 case USB_SPEED_SUPER:
1688 /* Bus speed is 500000 bytes/ms, so use a little less */
1692 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1696 /* FIXME if HZ != 1000 this will probably misbehave ... */
1698 /* look at each urb queued by the host side driver */
1699 spin_lock_irqsave(&dum->lock, flags);
1701 if (!dum_hcd->udev) {
1702 dev_err(dummy_dev(dum_hcd),
1703 "timer fired with no URBs pending?\n");
1704 spin_unlock_irqrestore(&dum->lock, flags);
1708 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1711 dum->ep[i].already_seen = 0;
1715 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1717 struct dummy_request *req;
1719 struct dummy_ep *ep = NULL;
1721 int status = -EINPROGRESS;
1726 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1728 type = usb_pipetype(urb->pipe);
1730 /* used up this frame's non-periodic bandwidth?
1731 * FIXME there's infinite bandwidth for control and
1732 * periodic transfers ... unrealistic.
1734 if (total <= 0 && type == PIPE_BULK)
1737 /* find the gadget's ep for this request (if configured) */
1738 address = usb_pipeendpoint (urb->pipe);
1739 if (usb_pipein(urb->pipe))
1740 address |= USB_DIR_IN;
1741 ep = find_endpoint(dum, address);
1743 /* set_configuration() disagreement */
1744 dev_dbg(dummy_dev(dum_hcd),
1745 "no ep configured for urb %p\n",
1751 if (ep->already_seen)
1753 ep->already_seen = 1;
1754 if (ep == &dum->ep[0] && urb->error_count) {
1755 ep->setup_stage = 1; /* a new urb */
1756 urb->error_count = 0;
1758 if (ep->halted && !ep->setup_stage) {
1759 /* NOTE: must not be iso! */
1760 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1765 /* FIXME make sure both ends agree on maxpacket */
1767 /* handle control requests */
1768 if (ep == &dum->ep[0] && ep->setup_stage) {
1769 struct usb_ctrlrequest setup;
1772 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
1773 /* paranoia, in case of stale queued data */
1774 list_for_each_entry(req, &ep->queue, queue) {
1775 list_del_init(&req->queue);
1776 req->req.status = -EOVERFLOW;
1777 dev_dbg(udc_dev(dum), "stale req = %p\n",
1780 spin_unlock(&dum->lock);
1781 req->req.complete(&ep->ep, &req->req);
1782 spin_lock(&dum->lock);
1783 ep->already_seen = 0;
1787 /* gadget driver never sees set_address or operations
1788 * on standard feature flags. some hardware doesn't
1791 ep->last_io = jiffies;
1792 ep->setup_stage = 0;
1795 value = handle_control_request(dum_hcd, urb, &setup,
1798 /* gadget driver handles all other requests. block
1799 * until setup() returns; no reentrancy issues etc.
1802 spin_unlock(&dum->lock);
1803 value = dum->driver->setup(&dum->gadget,
1805 spin_lock(&dum->lock);
1808 /* no delays (max 64KB data stage) */
1810 goto treat_control_like_bulk;
1812 /* error, see below */
1816 if (value != -EOPNOTSUPP)
1817 dev_dbg(udc_dev(dum),
1821 urb->actual_length = 0;
1827 /* non-control requests */
1829 switch (usb_pipetype(urb->pipe)) {
1830 case PIPE_ISOCHRONOUS:
1831 /* FIXME is it urb->interval since the last xfer?
1832 * use urb->iso_frame_desc[i].
1833 * complete whether or not ep has requests queued.
1834 * report random errors, to debug drivers.
1836 limit = max(limit, periodic_bytes(dum, ep));
1840 case PIPE_INTERRUPT:
1841 /* FIXME is it urb->interval since the last xfer?
1842 * this almost certainly polls too fast.
1844 limit = max(limit, periodic_bytes(dum, ep));
1848 treat_control_like_bulk:
1849 ep->last_io = jiffies;
1850 total = transfer(dum_hcd, urb, ep, limit, &status);
1854 /* incomplete transfer? */
1855 if (status == -EINPROGRESS)
1859 list_del(&urbp->urbp_list);
1862 ep->already_seen = ep->setup_stage = 0;
1864 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1865 spin_unlock(&dum->lock);
1866 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1867 spin_lock(&dum->lock);
1872 if (list_empty(&dum_hcd->urbp_list)) {
1873 usb_put_dev(dum_hcd->udev);
1874 dum_hcd->udev = NULL;
1875 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1876 /* want a 1 msec delay here */
1877 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1880 spin_unlock_irqrestore(&dum->lock, flags);
1883 /*-------------------------------------------------------------------------*/
1885 #define PORT_C_MASK \
1886 ((USB_PORT_STAT_C_CONNECTION \
1887 | USB_PORT_STAT_C_ENABLE \
1888 | USB_PORT_STAT_C_SUSPEND \
1889 | USB_PORT_STAT_C_OVERCURRENT \
1890 | USB_PORT_STAT_C_RESET) << 16)
1892 static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
1894 struct dummy_hcd *dum_hcd;
1895 unsigned long flags;
1898 dum_hcd = hcd_to_dummy_hcd(hcd);
1900 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1901 if (!HCD_HW_ACCESSIBLE(hcd))
1904 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1905 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1906 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1907 set_link_state(dum_hcd);
1910 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1912 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1913 dum_hcd->port_status);
1915 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1916 usb_hcd_resume_root_hub(hcd);
1919 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1924 ss_hub_descriptor(struct usb_hub_descriptor *desc)
1926 memset(desc, 0, sizeof *desc);
1927 desc->bDescriptorType = 0x2a;
1928 desc->bDescLength = 12;
1929 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1930 desc->bNbrPorts = 1;
1931 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1932 desc->u.ss.DeviceRemovable = 0xffff;
1935 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
1937 memset(desc, 0, sizeof *desc);
1938 desc->bDescriptorType = 0x29;
1939 desc->bDescLength = 9;
1940 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1941 desc->bNbrPorts = 1;
1942 desc->u.hs.DeviceRemovable[0] = 0xff;
1943 desc->u.hs.DeviceRemovable[1] = 0xff;
1946 static int dummy_hub_control(
1947 struct usb_hcd *hcd,
1954 struct dummy_hcd *dum_hcd;
1956 unsigned long flags;
1958 if (!HCD_HW_ACCESSIBLE(hcd))
1961 dum_hcd = hcd_to_dummy_hcd(hcd);
1963 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1965 case ClearHubFeature:
1967 case ClearPortFeature:
1969 case USB_PORT_FEAT_SUSPEND:
1970 if (hcd->speed == HCD_USB3) {
1971 dev_dbg(dummy_dev(dum_hcd),
1972 "USB_PORT_FEAT_SUSPEND req not "
1973 "supported for USB 3.0 roothub\n");
1976 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1977 /* 20msec resume signaling */
1978 dum_hcd->resuming = 1;
1979 dum_hcd->re_timeout = jiffies +
1980 msecs_to_jiffies(20);
1983 case USB_PORT_FEAT_POWER:
1984 if (hcd->speed == HCD_USB3) {
1985 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1986 dev_dbg(dummy_dev(dum_hcd),
1989 if (dum_hcd->port_status &
1990 USB_SS_PORT_STAT_POWER)
1991 dev_dbg(dummy_dev(dum_hcd),
1995 dum_hcd->port_status &= ~(1 << wValue);
1996 set_link_state(dum_hcd);
1999 case GetHubDescriptor:
2000 if (hcd->speed == HCD_USB3 &&
2001 (wLength < USB_DT_SS_HUB_SIZE ||
2002 wValue != (USB_DT_SS_HUB << 8))) {
2003 dev_dbg(dummy_dev(dum_hcd),
2004 "Wrong hub descriptor type for "
2005 "USB 3.0 roothub.\n");
2008 if (hcd->speed == HCD_USB3)
2009 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2011 hub_descriptor((struct usb_hub_descriptor *) buf);
2014 *(__le32 *) buf = cpu_to_le32(0);
2020 /* whoever resets or resumes must GetPortStatus to
2023 if (dum_hcd->resuming &&
2024 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2025 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2026 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
2028 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2029 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2030 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2031 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2032 if (dum_hcd->dum->pullup) {
2033 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
2035 if (hcd->speed < HCD_USB3) {
2036 switch (dum_hcd->dum->gadget.speed) {
2037 case USB_SPEED_HIGH:
2038 dum_hcd->port_status |=
2039 USB_PORT_STAT_HIGH_SPEED;
2042 dum_hcd->dum->gadget.ep0->
2044 dum_hcd->port_status |=
2045 USB_PORT_STAT_LOW_SPEED;
2048 dum_hcd->dum->gadget.speed =
2055 set_link_state(dum_hcd);
2056 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2057 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
2062 case SetPortFeature:
2064 case USB_PORT_FEAT_LINK_STATE:
2065 if (hcd->speed != HCD_USB3) {
2066 dev_dbg(dummy_dev(dum_hcd),
2067 "USB_PORT_FEAT_LINK_STATE req not "
2068 "supported for USB 2.0 roothub\n");
2072 * Since this is dummy we don't have an actual link so
2073 * there is nothing to do for the SET_LINK_STATE cmd
2076 case USB_PORT_FEAT_U1_TIMEOUT:
2077 case USB_PORT_FEAT_U2_TIMEOUT:
2078 /* TODO: add suspend/resume support! */
2079 if (hcd->speed != HCD_USB3) {
2080 dev_dbg(dummy_dev(dum_hcd),
2081 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2082 "supported for USB 2.0 roothub\n");
2086 case USB_PORT_FEAT_SUSPEND:
2087 /* Applicable only for USB2.0 hub */
2088 if (hcd->speed == HCD_USB3) {
2089 dev_dbg(dummy_dev(dum_hcd),
2090 "USB_PORT_FEAT_SUSPEND req not "
2091 "supported for USB 3.0 roothub\n");
2094 if (dum_hcd->active) {
2095 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
2097 /* HNP would happen here; for now we
2098 * assume b_bus_req is always true.
2100 set_link_state(dum_hcd);
2101 if (((1 << USB_DEVICE_B_HNP_ENABLE)
2102 & dum_hcd->dum->devstatus) != 0)
2103 dev_dbg(dummy_dev(dum_hcd),
2107 case USB_PORT_FEAT_POWER:
2108 if (hcd->speed == HCD_USB3)
2109 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2111 dum_hcd->port_status |= USB_PORT_STAT_POWER;
2112 set_link_state(dum_hcd);
2114 case USB_PORT_FEAT_BH_PORT_RESET:
2115 /* Applicable only for USB3.0 hub */
2116 if (hcd->speed != HCD_USB3) {
2117 dev_dbg(dummy_dev(dum_hcd),
2118 "USB_PORT_FEAT_BH_PORT_RESET req not "
2119 "supported for USB 2.0 roothub\n");
2123 case USB_PORT_FEAT_RESET:
2124 /* if it's already enabled, disable */
2125 if (hcd->speed == HCD_USB3) {
2126 dum_hcd->port_status = 0;
2127 dum_hcd->port_status =
2128 (USB_SS_PORT_STAT_POWER |
2129 USB_PORT_STAT_CONNECTION |
2130 USB_PORT_STAT_RESET);
2132 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
2133 | USB_PORT_STAT_LOW_SPEED
2134 | USB_PORT_STAT_HIGH_SPEED);
2136 * We want to reset device status. All but the
2137 * Self powered feature
2139 dum_hcd->dum->devstatus &=
2140 (1 << USB_DEVICE_SELF_POWERED);
2142 * FIXME USB3.0: what is the correct reset signaling
2143 * interval? Is it still 50msec as for HS?
2145 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2148 if (hcd->speed == HCD_USB3) {
2149 if ((dum_hcd->port_status &
2150 USB_SS_PORT_STAT_POWER) != 0) {
2151 dum_hcd->port_status |= (1 << wValue);
2152 set_link_state(dum_hcd);
2155 if ((dum_hcd->port_status &
2156 USB_PORT_STAT_POWER) != 0) {
2157 dum_hcd->port_status |= (1 << wValue);
2158 set_link_state(dum_hcd);
2162 case GetPortErrorCount:
2163 if (hcd->speed != HCD_USB3) {
2164 dev_dbg(dummy_dev(dum_hcd),
2165 "GetPortErrorCount req not "
2166 "supported for USB 2.0 roothub\n");
2169 /* We'll always return 0 since this is a dummy hub */
2170 *(__le32 *) buf = cpu_to_le32(0);
2173 if (hcd->speed != HCD_USB3) {
2174 dev_dbg(dummy_dev(dum_hcd),
2175 "SetHubDepth req not supported for "
2176 "USB 2.0 roothub\n");
2181 dev_dbg(dummy_dev(dum_hcd),
2182 "hub control req%04x v%04x i%04x l%d\n",
2183 typeReq, wValue, wIndex, wLength);
2185 /* "protocol stall" on error */
2188 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2190 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2191 usb_hcd_poll_rh_status(hcd);
2195 static int dummy_bus_suspend(struct usb_hcd *hcd)
2197 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2199 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2201 spin_lock_irq(&dum_hcd->dum->lock);
2202 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2203 set_link_state(dum_hcd);
2204 hcd->state = HC_STATE_SUSPENDED;
2205 spin_unlock_irq(&dum_hcd->dum->lock);
2209 static int dummy_bus_resume(struct usb_hcd *hcd)
2211 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2214 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2216 spin_lock_irq(&dum_hcd->dum->lock);
2217 if (!HCD_HW_ACCESSIBLE(hcd)) {
2220 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2221 set_link_state(dum_hcd);
2222 if (!list_empty(&dum_hcd->urbp_list))
2223 mod_timer(&dum_hcd->timer, jiffies);
2224 hcd->state = HC_STATE_RUNNING;
2226 spin_unlock_irq(&dum_hcd->dum->lock);
2230 /*-------------------------------------------------------------------------*/
2232 static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
2234 int ep = usb_pipeendpoint(urb->pipe);
2236 return snprintf(buf, size,
2237 "urb/%p %s ep%d%s%s len %d/%d\n",
2240 switch (urb->dev->speed) {
2244 case USB_SPEED_FULL:
2247 case USB_SPEED_HIGH:
2250 case USB_SPEED_SUPER:
2257 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
2259 switch (usb_pipetype(urb->pipe)) { \
2260 case PIPE_CONTROL: \
2266 case PIPE_INTERRUPT: \
2273 urb->actual_length, urb->transfer_buffer_length);
2276 static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2279 struct usb_hcd *hcd = dev_get_drvdata(dev);
2280 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2283 unsigned long flags;
2285 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2286 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2289 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
2293 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2297 static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
2299 static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2301 init_timer(&dum_hcd->timer);
2302 dum_hcd->timer.function = dummy_timer;
2303 dum_hcd->timer.data = (unsigned long)dum_hcd;
2304 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2305 dum_hcd->stream_en_ep = 0;
2306 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2307 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2308 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2309 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2310 #ifdef CONFIG_USB_OTG
2311 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2315 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2316 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2319 static int dummy_start(struct usb_hcd *hcd)
2321 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2324 * MASTER side init ... we emulate a root hub that'll only ever
2325 * talk to one device (the slave side). Also appears in sysfs,
2326 * just like more familiar pci-based HCDs.
2328 if (!usb_hcd_is_primary_hcd(hcd))
2329 return dummy_start_ss(dum_hcd);
2331 spin_lock_init(&dum_hcd->dum->lock);
2332 init_timer(&dum_hcd->timer);
2333 dum_hcd->timer.function = dummy_timer;
2334 dum_hcd->timer.data = (unsigned long)dum_hcd;
2335 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2337 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2339 hcd->power_budget = POWER_BUDGET;
2340 hcd->state = HC_STATE_RUNNING;
2341 hcd->uses_new_polling = 1;
2343 #ifdef CONFIG_USB_OTG
2344 hcd->self.otg_port = 1;
2347 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2348 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2351 static void dummy_stop(struct usb_hcd *hcd)
2355 dum = hcd_to_dummy_hcd(hcd)->dum;
2356 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2357 usb_gadget_unregister_driver(dum->driver);
2358 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2361 /*-------------------------------------------------------------------------*/
2363 static int dummy_h_get_frame(struct usb_hcd *hcd)
2365 return dummy_g_get_frame(NULL);
2368 static int dummy_setup(struct usb_hcd *hcd)
2370 hcd->self.sg_tablesize = ~0;
2371 if (usb_hcd_is_primary_hcd(hcd)) {
2372 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2373 the_controller.hs_hcd->dum = &the_controller;
2375 * Mark the first roothub as being USB 2.0.
2376 * The USB 3.0 roothub will be registered later by
2379 hcd->speed = HCD_USB2;
2380 hcd->self.root_hub->speed = USB_SPEED_HIGH;
2382 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2383 the_controller.ss_hcd->dum = &the_controller;
2384 hcd->speed = HCD_USB3;
2385 hcd->self.root_hub->speed = USB_SPEED_SUPER;
2390 /* Change a group of bulk endpoints to support multiple stream IDs */
2391 static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2392 struct usb_host_endpoint **eps, unsigned int num_eps,
2393 unsigned int num_streams, gfp_t mem_flags)
2395 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2396 unsigned long flags;
2398 int ret_streams = num_streams;
2405 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2406 for (i = 0; i < num_eps; i++) {
2407 index = dummy_get_ep_idx(&eps[i]->desc);
2408 if ((1 << index) & dum_hcd->stream_en_ep) {
2409 ret_streams = -EINVAL;
2412 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2414 ret_streams = -EINVAL;
2417 if (max_stream < ret_streams) {
2418 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2420 eps[i]->desc.bEndpointAddress,
2422 ret_streams = max_stream;
2426 for (i = 0; i < num_eps; i++) {
2427 index = dummy_get_ep_idx(&eps[i]->desc);
2428 dum_hcd->stream_en_ep |= 1 << index;
2429 set_max_streams_for_pipe(dum_hcd,
2430 usb_endpoint_num(&eps[i]->desc), ret_streams);
2433 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2437 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2438 static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2439 struct usb_host_endpoint **eps, unsigned int num_eps,
2442 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2443 unsigned long flags;
2448 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2449 for (i = 0; i < num_eps; i++) {
2450 index = dummy_get_ep_idx(&eps[i]->desc);
2451 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2457 for (i = 0; i < num_eps; i++) {
2458 index = dummy_get_ep_idx(&eps[i]->desc);
2459 dum_hcd->stream_en_ep &= ~(1 << index);
2460 set_max_streams_for_pipe(dum_hcd,
2461 usb_endpoint_num(&eps[i]->desc), 0);
2465 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2469 static struct hc_driver dummy_hcd = {
2470 .description = (char *) driver_name,
2471 .product_desc = "Dummy host controller",
2472 .hcd_priv_size = sizeof(struct dummy_hcd),
2474 .flags = HCD_USB3 | HCD_SHARED,
2476 .reset = dummy_setup,
2477 .start = dummy_start,
2480 .urb_enqueue = dummy_urb_enqueue,
2481 .urb_dequeue = dummy_urb_dequeue,
2483 .get_frame_number = dummy_h_get_frame,
2485 .hub_status_data = dummy_hub_status,
2486 .hub_control = dummy_hub_control,
2487 .bus_suspend = dummy_bus_suspend,
2488 .bus_resume = dummy_bus_resume,
2490 .alloc_streams = dummy_alloc_streams,
2491 .free_streams = dummy_free_streams,
2494 static int dummy_hcd_probe(struct platform_device *pdev)
2496 struct usb_hcd *hs_hcd;
2497 struct usb_hcd *ss_hcd;
2500 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2502 if (!mod_data.is_super_speed)
2503 dummy_hcd.flags = HCD_USB2;
2504 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2509 retval = usb_add_hcd(hs_hcd, 0, 0);
2511 usb_put_hcd(hs_hcd);
2515 if (mod_data.is_super_speed) {
2516 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2517 dev_name(&pdev->dev), hs_hcd);
2520 goto dealloc_usb2_hcd;
2523 retval = usb_add_hcd(ss_hcd, 0, 0);
2530 usb_put_hcd(ss_hcd);
2532 usb_put_hcd(hs_hcd);
2533 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
2537 static int dummy_hcd_remove(struct platform_device *pdev)
2541 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
2544 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2545 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2548 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2549 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2551 the_controller.hs_hcd = NULL;
2552 the_controller.ss_hcd = NULL;
2557 static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
2559 struct usb_hcd *hcd;
2560 struct dummy_hcd *dum_hcd;
2563 dev_dbg(&pdev->dev, "%s\n", __func__);
2565 hcd = platform_get_drvdata(pdev);
2566 dum_hcd = hcd_to_dummy_hcd(hcd);
2567 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2568 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2571 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2575 static int dummy_hcd_resume(struct platform_device *pdev)
2577 struct usb_hcd *hcd;
2579 dev_dbg(&pdev->dev, "%s\n", __func__);
2581 hcd = platform_get_drvdata(pdev);
2582 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2583 usb_hcd_poll_rh_status(hcd);
2587 static struct platform_driver dummy_hcd_driver = {
2588 .probe = dummy_hcd_probe,
2589 .remove = dummy_hcd_remove,
2590 .suspend = dummy_hcd_suspend,
2591 .resume = dummy_hcd_resume,
2593 .name = (char *) driver_name,
2594 .owner = THIS_MODULE,
2598 /*-------------------------------------------------------------------------*/
2600 static struct platform_device *the_udc_pdev;
2601 static struct platform_device *the_hcd_pdev;
2603 static int __init init(void)
2605 int retval = -ENOMEM;
2610 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2613 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2616 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2620 retval = platform_driver_register(&dummy_hcd_driver);
2622 goto err_register_hcd_driver;
2623 retval = platform_driver_register(&dummy_udc_driver);
2625 goto err_register_udc_driver;
2627 retval = platform_device_add(the_hcd_pdev);
2630 if (!the_controller.hs_hcd ||
2631 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
2633 * The hcd was added successfully but its probe function failed
2639 retval = platform_device_add(the_udc_pdev);
2642 if (!platform_get_drvdata(the_udc_pdev)) {
2644 * The udc was added successfully but its probe function failed
2653 platform_device_del(the_udc_pdev);
2655 platform_device_del(the_hcd_pdev);
2657 platform_driver_unregister(&dummy_udc_driver);
2658 err_register_udc_driver:
2659 platform_driver_unregister(&dummy_hcd_driver);
2660 err_register_hcd_driver:
2661 platform_device_put(the_udc_pdev);
2663 platform_device_put(the_hcd_pdev);
2668 static void __exit cleanup(void)
2670 platform_device_unregister(the_udc_pdev);
2671 platform_device_unregister(the_hcd_pdev);
2672 platform_driver_unregister(&dummy_udc_driver);
2673 platform_driver_unregister(&dummy_hcd_driver);
2675 module_exit(cleanup);