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.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * This exposes a device side "USB gadget" API, driven by requests to a
27 * Linux-USB host controller driver. USB traffic is simulated; there's
28 * no need for USB hardware. Use this with two other drivers:
30 * - Gadget driver, responding to requests (slave);
31 * - Host-side device driver, as already familiar in Linux.
33 * Having this all in one kernel can help some stages of development,
34 * bypassing some hardware (and driver) issues. UML could help too.
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/delay.h>
42 #include <linux/ioport.h>
43 #include <linux/slab.h>
44 #include <linux/errno.h>
45 #include <linux/init.h>
46 #include <linux/timer.h>
47 #include <linux/list.h>
48 #include <linux/interrupt.h>
49 #include <linux/platform_device.h>
50 #include <linux/usb.h>
51 #include <linux/usb_gadget.h>
53 #include <asm/byteorder.h>
56 #include <asm/system.h>
57 #include <asm/unaligned.h>
60 #include "../core/hcd.h"
63 #define DRIVER_DESC "USB Host+Gadget Emulator"
64 #define DRIVER_VERSION "02 May 2005"
66 static const char driver_name [] = "dummy_hcd";
67 static const char driver_desc [] = "USB Host+Gadget Emulator";
69 static const char gadget_name [] = "dummy_udc";
71 MODULE_DESCRIPTION (DRIVER_DESC);
72 MODULE_AUTHOR ("David Brownell");
73 MODULE_LICENSE ("GPL");
75 /*-------------------------------------------------------------------------*/
77 /* gadget side driver data structres */
79 struct list_head queue;
80 unsigned long last_io; /* jiffies timestamp */
81 struct usb_gadget *gadget;
82 const struct usb_endpoint_descriptor *desc;
85 unsigned already_seen : 1;
86 unsigned setup_stage : 1;
89 struct dummy_request {
90 struct list_head queue; /* ep's requests */
91 struct usb_request req;
94 static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
96 return container_of (_ep, struct dummy_ep, ep);
99 static inline struct dummy_request *usb_request_to_dummy_request
100 (struct usb_request *_req)
102 return container_of (_req, struct dummy_request, req);
105 /*-------------------------------------------------------------------------*/
108 * Every device has ep0 for control requests, plus up to 30 more endpoints,
109 * in one of two types:
111 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
112 * number can be changed. Names like "ep-a" are used for this type.
114 * - Fixed Function: in other cases. some characteristics may be mutable;
115 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
117 * Gadget drivers are responsible for not setting up conflicting endpoint
118 * configurations, illegal or unsupported packet lengths, and so on.
121 static const char ep0name [] = "ep0";
123 static const char *const ep_name [] = {
124 ep0name, /* everyone has ep0 */
126 /* act like a net2280: high speed, six configurable endpoints */
127 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
129 /* or like pxa250: fifteen fixed function endpoints */
130 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
131 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
132 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
135 /* or like sa1100: two fixed function endpoints */
136 "ep1out-bulk", "ep2in-bulk",
138 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
140 /*-------------------------------------------------------------------------*/
146 struct list_head urbp_list;
150 enum dummy_rh_state {
160 * SLAVE/GADGET side support
162 struct dummy_ep ep [DUMMY_ENDPOINTS];
164 struct usb_gadget gadget;
165 struct usb_gadget_driver *driver;
166 struct dummy_request fifo_req;
167 u8 fifo_buf [FIFO_SIZE];
169 unsigned udc_suspended:1;
172 unsigned old_active:1;
175 * MASTER/HOST side support
177 enum dummy_rh_state rh_state;
178 struct timer_list timer;
182 unsigned long re_timeout;
184 struct usb_device *udev;
185 struct list_head urbp_list;
188 static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
190 return (struct dummy *) (hcd->hcd_priv);
193 static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
195 return container_of((void *) dum, struct usb_hcd, hcd_priv);
198 static inline struct device *dummy_dev (struct dummy *dum)
200 return dummy_to_hcd(dum)->self.controller;
203 static inline struct device *udc_dev (struct dummy *dum)
205 return dum->gadget.dev.parent;
208 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
210 return container_of (ep->gadget, struct dummy, gadget);
213 static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
215 return container_of (gadget, struct dummy, gadget);
218 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
220 return container_of (dev, struct dummy, gadget.dev);
223 static struct dummy *the_controller;
225 /*-------------------------------------------------------------------------*/
227 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
229 /* called with spinlock held */
230 static void nuke (struct dummy *dum, struct dummy_ep *ep)
232 while (!list_empty (&ep->queue)) {
233 struct dummy_request *req;
235 req = list_entry (ep->queue.next, struct dummy_request, queue);
236 list_del_init (&req->queue);
237 req->req.status = -ESHUTDOWN;
239 spin_unlock (&dum->lock);
240 req->req.complete (&ep->ep, &req->req);
241 spin_lock (&dum->lock);
245 /* caller must hold lock */
247 stop_activity (struct dummy *dum)
251 /* prevent any more requests */
254 /* The timer is left running so that outstanding URBs can fail */
256 /* nuke any pending requests first, so driver i/o is quiesced */
257 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
260 /* driver now does any non-usb quiescing necessary */
263 /* caller must hold lock */
265 set_link_state (struct dummy *dum)
268 if ((dum->port_status & USB_PORT_STAT_POWER) == 0)
269 dum->port_status = 0;
271 /* UDC suspend must cause a disconnect */
272 else if (!dum->pullup || dum->udc_suspended) {
273 dum->port_status &= ~(USB_PORT_STAT_CONNECTION |
274 USB_PORT_STAT_ENABLE |
275 USB_PORT_STAT_LOW_SPEED |
276 USB_PORT_STAT_HIGH_SPEED |
277 USB_PORT_STAT_SUSPEND);
278 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0)
279 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
281 dum->port_status |= USB_PORT_STAT_CONNECTION;
282 if ((dum->old_status & USB_PORT_STAT_CONNECTION) == 0)
283 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
284 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0)
285 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
286 else if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
287 dum->rh_state != DUMMY_RH_SUSPENDED)
291 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0 || dum->active)
294 if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
295 (dum->port_status & USB_PORT_STAT_RESET) != 0) {
296 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
297 (dum->old_status & USB_PORT_STAT_RESET) == 0 &&
300 spin_unlock (&dum->lock);
301 dum->driver->disconnect (&dum->gadget);
302 spin_lock (&dum->lock);
304 } else if (dum->active != dum->old_active) {
305 if (dum->old_active && dum->driver->suspend) {
306 spin_unlock (&dum->lock);
307 dum->driver->suspend (&dum->gadget);
308 spin_lock (&dum->lock);
309 } else if (!dum->old_active && dum->driver->resume) {
310 spin_unlock (&dum->lock);
311 dum->driver->resume (&dum->gadget);
312 spin_lock (&dum->lock);
316 dum->old_status = dum->port_status;
317 dum->old_active = dum->active;
320 /*-------------------------------------------------------------------------*/
322 /* SLAVE/GADGET SIDE DRIVER
324 * This only tracks gadget state. All the work is done when the host
325 * side tries some (emulated) i/o operation. Real device controller
326 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
329 #define is_enabled(dum) \
330 (dum->port_status & USB_PORT_STAT_ENABLE)
333 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
340 ep = usb_ep_to_dummy_ep (_ep);
341 if (!_ep || !desc || ep->desc || _ep->name == ep0name
342 || desc->bDescriptorType != USB_DT_ENDPOINT)
344 dum = ep_to_dummy (ep);
345 if (!dum->driver || !is_enabled (dum))
347 max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
349 /* drivers must not request bad settings, since lower levels
350 * (hardware or its drivers) may not check. some endpoints
351 * can't do iso, many have maxpacket limitations, etc.
353 * since this "hardware" driver is here to help debugging, we
354 * have some extra sanity checks. (there could be more though,
355 * especially for "ep9out" style fixed function ones.)
358 switch (desc->bmAttributes & 0x03) {
359 case USB_ENDPOINT_XFER_BULK:
360 if (strstr (ep->ep.name, "-iso")
361 || strstr (ep->ep.name, "-int")) {
364 switch (dum->gadget.speed) {
368 /* conserve return statements */
371 case 8: case 16: case 32: case 64:
372 /* we'll fake any legal size */
380 case USB_ENDPOINT_XFER_INT:
381 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
383 /* real hardware might not handle all packet sizes */
384 switch (dum->gadget.speed) {
388 /* save a return statement */
392 /* save a return statement */
399 case USB_ENDPOINT_XFER_ISOC:
400 if (strstr (ep->ep.name, "-bulk")
401 || strstr (ep->ep.name, "-int"))
403 /* real hardware might not handle all packet sizes */
404 switch (dum->gadget.speed) {
408 /* save a return statement */
412 /* save a return statement */
418 /* few chips support control except on ep0 */
422 _ep->maxpacket = max;
425 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
427 desc->bEndpointAddress & 0x0f,
428 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
430 switch (desc->bmAttributes & 0x03) {
431 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
432 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
433 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
434 default: val = "ctrl"; break;
438 /* at this point real hardware should be NAKing transfers
439 * to that endpoint, until a buffer is queued to it.
446 static int dummy_disable (struct usb_ep *_ep)
453 ep = usb_ep_to_dummy_ep (_ep);
454 if (!_ep || !ep->desc || _ep->name == ep0name)
456 dum = ep_to_dummy (ep);
458 spin_lock_irqsave (&dum->lock, flags);
462 spin_unlock_irqrestore (&dum->lock, flags);
464 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
468 static struct usb_request *
469 dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
472 struct dummy_request *req;
476 ep = usb_ep_to_dummy_ep (_ep);
478 req = kzalloc(sizeof(*req), mem_flags);
481 INIT_LIST_HEAD (&req->queue);
486 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
489 struct dummy_request *req;
491 ep = usb_ep_to_dummy_ep (_ep);
492 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
495 req = usb_request_to_dummy_request (_req);
496 WARN_ON (!list_empty (&req->queue));
511 ep = usb_ep_to_dummy_ep (_ep);
512 dum = ep_to_dummy (ep);
516 retval = kmalloc (bytes, mem_flags);
517 *dma = (dma_addr_t) retval;
533 fifo_complete (struct usb_ep *ep, struct usb_request *req)
538 dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
542 struct dummy_request *req;
546 req = usb_request_to_dummy_request (_req);
547 if (!_req || !list_empty (&req->queue) || !_req->complete)
550 ep = usb_ep_to_dummy_ep (_ep);
551 if (!_ep || (!ep->desc && _ep->name != ep0name))
554 dum = ep_to_dummy (ep);
555 if (!dum->driver || !is_enabled (dum))
559 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
560 ep, _req, _ep->name, _req->length, _req->buf);
563 _req->status = -EINPROGRESS;
565 spin_lock_irqsave (&dum->lock, flags);
567 /* implement an emulated single-request FIFO */
568 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
569 list_empty (&dum->fifo_req.queue) &&
570 list_empty (&ep->queue) &&
571 _req->length <= FIFO_SIZE) {
572 req = &dum->fifo_req;
574 req->req.buf = dum->fifo_buf;
575 memcpy (dum->fifo_buf, _req->buf, _req->length);
576 req->req.context = dum;
577 req->req.complete = fifo_complete;
579 spin_unlock (&dum->lock);
580 _req->actual = _req->length;
582 _req->complete (_ep, _req);
583 spin_lock (&dum->lock);
585 list_add_tail (&req->queue, &ep->queue);
586 spin_unlock_irqrestore (&dum->lock, flags);
588 /* real hardware would likely enable transfers here, in case
589 * it'd been left NAKing.
594 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
598 int retval = -EINVAL;
600 struct dummy_request *req = NULL;
604 ep = usb_ep_to_dummy_ep (_ep);
605 dum = ep_to_dummy (ep);
610 local_irq_save (flags);
611 spin_lock (&dum->lock);
612 list_for_each_entry (req, &ep->queue, queue) {
613 if (&req->req == _req) {
614 list_del_init (&req->queue);
615 _req->status = -ECONNRESET;
620 spin_unlock (&dum->lock);
623 dev_dbg (udc_dev(dum),
624 "dequeued req %p from %s, len %d buf %p\n",
625 req, _ep->name, _req->length, _req->buf);
626 _req->complete (_ep, _req);
628 local_irq_restore (flags);
633 dummy_set_halt (struct usb_ep *_ep, int value)
640 ep = usb_ep_to_dummy_ep (_ep);
641 dum = ep_to_dummy (ep);
646 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
647 !list_empty (&ep->queue))
651 /* FIXME clear emulated data toggle too */
655 static const struct usb_ep_ops dummy_ep_ops = {
656 .enable = dummy_enable,
657 .disable = dummy_disable,
659 .alloc_request = dummy_alloc_request,
660 .free_request = dummy_free_request,
662 .alloc_buffer = dummy_alloc_buffer,
663 .free_buffer = dummy_free_buffer,
664 /* map, unmap, ... eventually hook the "generic" dma calls */
666 .queue = dummy_queue,
667 .dequeue = dummy_dequeue,
669 .set_halt = dummy_set_halt,
672 /*-------------------------------------------------------------------------*/
674 /* there are both host and device side versions of this call ... */
675 static int dummy_g_get_frame (struct usb_gadget *_gadget)
679 do_gettimeofday (&tv);
680 return tv.tv_usec / 1000;
683 static int dummy_wakeup (struct usb_gadget *_gadget)
687 dum = gadget_to_dummy (_gadget);
688 if (!(dum->devstatus & ( (1 << USB_DEVICE_B_HNP_ENABLE)
689 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
691 if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0)
693 if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
694 dum->rh_state != DUMMY_RH_SUSPENDED)
697 /* FIXME: What if the root hub is suspended but the port isn't? */
699 /* hub notices our request, issues downstream resume, etc */
701 dum->re_timeout = jiffies + msecs_to_jiffies(20);
702 mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout);
706 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
710 dum = gadget_to_dummy (_gadget);
712 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
714 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
718 static int dummy_pullup (struct usb_gadget *_gadget, int value)
723 dum = gadget_to_dummy (_gadget);
724 spin_lock_irqsave (&dum->lock, flags);
725 dum->pullup = (value != 0);
726 set_link_state (dum);
727 spin_unlock_irqrestore (&dum->lock, flags);
729 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
733 static const struct usb_gadget_ops dummy_ops = {
734 .get_frame = dummy_g_get_frame,
735 .wakeup = dummy_wakeup,
736 .set_selfpowered = dummy_set_selfpowered,
737 .pullup = dummy_pullup,
740 /*-------------------------------------------------------------------------*/
742 /* "function" sysfs attribute */
744 show_function (struct device *dev, struct device_attribute *attr, char *buf)
746 struct dummy *dum = gadget_dev_to_dummy (dev);
748 if (!dum->driver || !dum->driver->function)
750 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
752 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
754 /*-------------------------------------------------------------------------*/
757 * Driver registration/unregistration.
759 * This is basically hardware-specific; there's usually only one real USB
760 * device (not host) controller since that's how USB devices are intended
761 * to work. So most implementations of these api calls will rely on the
762 * fact that only one driver will ever bind to the hardware. But curious
763 * hardware can be built with discrete components, so the gadget API doesn't
764 * require that assumption.
766 * For this emulator, it might be convenient to create a usb slave device
767 * for each driver that registers: just add to a big root hub.
771 usb_gadget_register_driver (struct usb_gadget_driver *driver)
773 struct dummy *dum = the_controller;
780 if (!driver->bind || !driver->setup
781 || driver->speed == USB_SPEED_UNKNOWN)
785 * SLAVE side init ... the layer above hardware, which
786 * can't enumerate without help from the driver we're binding.
791 INIT_LIST_HEAD (&dum->gadget.ep_list);
792 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
793 struct dummy_ep *ep = &dum->ep [i];
797 ep->ep.name = ep_name [i];
798 ep->ep.ops = &dummy_ep_ops;
799 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
800 ep->halted = ep->already_seen = ep->setup_stage = 0;
801 ep->ep.maxpacket = ~0;
802 ep->last_io = jiffies;
803 ep->gadget = &dum->gadget;
805 INIT_LIST_HEAD (&ep->queue);
808 dum->gadget.ep0 = &dum->ep [0].ep;
809 dum->ep [0].ep.maxpacket = 64;
810 list_del_init (&dum->ep [0].ep.ep_list);
811 INIT_LIST_HEAD(&dum->fifo_req.queue);
813 dum->driver = driver;
814 dum->gadget.dev.driver = &driver->driver;
815 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
816 driver->driver.name);
817 if ((retval = driver->bind (&dum->gadget)) != 0)
818 goto err_bind_gadget;
820 driver->driver.bus = dum->gadget.dev.parent->bus;
821 if ((retval = driver_register (&driver->driver)) != 0)
823 if ((retval = device_bind_driver (&dum->gadget.dev)) != 0)
824 goto err_bind_driver;
826 /* khubd will enumerate this in a while */
827 spin_lock_irq (&dum->lock);
829 set_link_state (dum);
830 spin_unlock_irq (&dum->lock);
832 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
836 driver_unregister (&driver->driver);
839 driver->unbind (&dum->gadget);
840 spin_lock_irq (&dum->lock);
842 set_link_state (dum);
843 spin_unlock_irq (&dum->lock);
846 dum->gadget.dev.driver = NULL;
849 EXPORT_SYMBOL (usb_gadget_register_driver);
852 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
854 struct dummy *dum = the_controller;
859 if (!driver || driver != dum->driver || !driver->unbind)
862 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
863 driver->driver.name);
865 spin_lock_irqsave (&dum->lock, flags);
867 set_link_state (dum);
868 spin_unlock_irqrestore (&dum->lock, flags);
870 driver->unbind (&dum->gadget);
873 device_release_driver (&dum->gadget.dev);
874 driver_unregister (&driver->driver);
876 spin_lock_irqsave (&dum->lock, flags);
878 set_link_state (dum);
879 spin_unlock_irqrestore (&dum->lock, flags);
881 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
884 EXPORT_SYMBOL (usb_gadget_unregister_driver);
888 /* just declare this in any driver that really need it */
889 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
891 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
895 EXPORT_SYMBOL (net2280_set_fifo_mode);
898 /* The gadget structure is stored inside the hcd structure and will be
899 * released along with it. */
901 dummy_gadget_release (struct device *dev)
903 struct dummy *dum = gadget_dev_to_dummy (dev);
905 usb_put_hcd (dummy_to_hcd (dum));
908 static int dummy_udc_probe (struct platform_device *pdev)
910 struct dummy *dum = the_controller;
913 dum->gadget.name = gadget_name;
914 dum->gadget.ops = &dummy_ops;
915 dum->gadget.is_dualspeed = 1;
917 /* maybe claim OTG support, though we won't complete HNP */
918 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
920 strcpy (dum->gadget.dev.bus_id, "gadget");
921 dum->gadget.dev.parent = &pdev->dev;
922 dum->gadget.dev.release = dummy_gadget_release;
923 rc = device_register (&dum->gadget.dev);
927 usb_get_hcd (dummy_to_hcd (dum));
929 platform_set_drvdata (pdev, dum);
930 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
932 device_unregister (&dum->gadget.dev);
936 static int dummy_udc_remove (struct platform_device *pdev)
938 struct dummy *dum = platform_get_drvdata (pdev);
940 platform_set_drvdata (pdev, NULL);
941 device_remove_file (&dum->gadget.dev, &dev_attr_function);
942 device_unregister (&dum->gadget.dev);
946 static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
948 struct dummy *dum = platform_get_drvdata(pdev);
950 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
951 spin_lock_irq (&dum->lock);
952 dum->udc_suspended = 1;
953 set_link_state (dum);
954 spin_unlock_irq (&dum->lock);
956 pdev->dev.power.power_state = state;
957 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
961 static int dummy_udc_resume (struct platform_device *pdev)
963 struct dummy *dum = platform_get_drvdata(pdev);
965 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
966 spin_lock_irq (&dum->lock);
967 dum->udc_suspended = 0;
968 set_link_state (dum);
969 spin_unlock_irq (&dum->lock);
971 pdev->dev.power.power_state = PMSG_ON;
972 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
976 static struct platform_driver dummy_udc_driver = {
977 .probe = dummy_udc_probe,
978 .remove = dummy_udc_remove,
979 .suspend = dummy_udc_suspend,
980 .resume = dummy_udc_resume,
982 .name = (char *) gadget_name,
983 .owner = THIS_MODULE,
987 /*-------------------------------------------------------------------------*/
989 /* MASTER/HOST SIDE DRIVER
991 * this uses the hcd framework to hook up to host side drivers.
992 * its root hub will only have one device, otherwise it acts like
993 * a normal host controller.
995 * when urbs are queued, they're just stuck on a list that we
996 * scan in a timer callback. that callback connects writes from
997 * the host with reads from the device, and so on, based on the
1001 static int dummy_urb_enqueue (
1002 struct usb_hcd *hcd,
1003 struct usb_host_endpoint *ep,
1009 unsigned long flags;
1011 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1014 urbp = kmalloc (sizeof *urbp, mem_flags);
1019 dum = hcd_to_dummy (hcd);
1020 spin_lock_irqsave (&dum->lock, flags);
1023 dum->udev = urb->dev;
1024 usb_get_dev (dum->udev);
1025 } else if (unlikely (dum->udev != urb->dev))
1026 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
1028 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
1030 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1031 urb->error_count = 1; /* mark as a new urb */
1033 /* kick the scheduler, it'll do the rest */
1034 if (!timer_pending (&dum->timer))
1035 mod_timer (&dum->timer, jiffies + 1);
1037 spin_unlock_irqrestore (&dum->lock, flags);
1041 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
1044 unsigned long flags;
1046 /* giveback happens automatically in timer callback,
1047 * so make sure the callback happens */
1048 dum = hcd_to_dummy (hcd);
1049 spin_lock_irqsave (&dum->lock, flags);
1050 if (dum->rh_state != DUMMY_RH_RUNNING && !list_empty(&dum->urbp_list))
1051 mod_timer (&dum->timer, jiffies);
1052 spin_unlock_irqrestore (&dum->lock, flags);
1056 static void maybe_set_status (struct urb *urb, int status)
1058 spin_lock (&urb->lock);
1059 if (urb->status == -EINPROGRESS)
1060 urb->status = status;
1061 spin_unlock (&urb->lock);
1064 /* transfer up to a frame's worth; caller must own lock */
1066 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
1068 struct dummy_request *req;
1071 /* if there's no request queued, the device is NAKing; return */
1072 list_for_each_entry (req, &ep->queue, queue) {
1073 unsigned host_len, dev_len, len;
1074 int is_short, to_host;
1077 /* 1..N packets of ep->ep.maxpacket each ... the last one
1078 * may be short (including zero length).
1080 * writer can send a zlp explicitly (length 0) or implicitly
1081 * (length mod maxpacket zero, and 'zero' flag); they always
1084 host_len = urb->transfer_buffer_length - urb->actual_length;
1085 dev_len = req->req.length - req->req.actual;
1086 len = min (host_len, dev_len);
1088 /* FIXME update emulated data toggle too */
1090 to_host = usb_pipein (urb->pipe);
1091 if (unlikely (len == 0))
1096 /* not enough bandwidth left? */
1097 if (limit < ep->ep.maxpacket && limit < len)
1099 len = min (len, (unsigned) limit);
1103 /* use an extra pass for the final short packet */
1104 if (len > ep->ep.maxpacket) {
1106 len -= (len % ep->ep.maxpacket);
1108 is_short = (len % ep->ep.maxpacket) != 0;
1110 /* else transfer packet(s) */
1111 ubuf = urb->transfer_buffer + urb->actual_length;
1112 rbuf = req->req.buf + req->req.actual;
1114 memcpy (ubuf, rbuf, len);
1116 memcpy (rbuf, ubuf, len);
1117 ep->last_io = jiffies;
1120 urb->actual_length += len;
1121 req->req.actual += len;
1124 /* short packets terminate, maybe with overflow/underflow.
1125 * it's only really an error to write too much.
1127 * partially filling a buffer optionally blocks queue advances
1128 * (so completion handlers can clean up the queue) but we don't
1129 * need to emulate such data-in-flight. so we only show part
1130 * of the URB_SHORT_NOT_OK effect: completion status.
1133 if (host_len == dev_len) {
1134 req->req.status = 0;
1135 maybe_set_status (urb, 0);
1136 } else if (to_host) {
1137 req->req.status = 0;
1138 if (dev_len > host_len)
1139 maybe_set_status (urb, -EOVERFLOW);
1141 maybe_set_status (urb,
1142 (urb->transfer_flags
1145 } else if (!to_host) {
1146 maybe_set_status (urb, 0);
1147 if (host_len > dev_len)
1148 req->req.status = -EOVERFLOW;
1150 req->req.status = 0;
1153 /* many requests terminate without a short packet */
1155 if (req->req.length == req->req.actual
1157 req->req.status = 0;
1158 if (urb->transfer_buffer_length == urb->actual_length
1159 && !(urb->transfer_flags
1160 & URB_ZERO_PACKET)) {
1161 maybe_set_status (urb, 0);
1165 /* device side completion --> continuable */
1166 if (req->req.status != -EINPROGRESS) {
1167 list_del_init (&req->queue);
1169 spin_unlock (&dum->lock);
1170 req->req.complete (&ep->ep, &req->req);
1171 spin_lock (&dum->lock);
1173 /* requests might have been unlinked... */
1177 /* host side completion --> terminate */
1178 if (urb->status != -EINPROGRESS)
1181 /* rescan to continue with any other queued i/o */
1188 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1190 int limit = ep->ep.maxpacket;
1192 if (dum->gadget.speed == USB_SPEED_HIGH) {
1195 /* high bandwidth mode */
1196 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1197 tmp = (tmp >> 11) & 0x03;
1198 tmp *= 8 /* applies to entire frame */;
1199 limit += limit * tmp;
1204 #define is_active(dum) ((dum->port_status & \
1205 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1206 USB_PORT_STAT_SUSPEND)) \
1207 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1209 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1213 if (!is_active (dum))
1215 if ((address & ~USB_DIR_IN) == 0)
1216 return &dum->ep [0];
1217 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1218 struct dummy_ep *ep = &dum->ep [i];
1222 if (ep->desc->bEndpointAddress == address)
1230 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1231 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1232 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1233 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1234 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1235 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1237 /* drive both sides of the transfers; looks like irq handlers to
1238 * both drivers except the callbacks aren't in_irq().
1240 static void dummy_timer (unsigned long _dum)
1242 struct dummy *dum = (struct dummy *) _dum;
1243 struct urbp *urbp, *tmp;
1244 unsigned long flags;
1248 /* simplistic model for one frame's bandwidth */
1249 switch (dum->gadget.speed) {
1251 total = 8/*bytes*/ * 12/*packets*/;
1253 case USB_SPEED_FULL:
1254 total = 64/*bytes*/ * 19/*packets*/;
1256 case USB_SPEED_HIGH:
1257 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1260 dev_err (dummy_dev(dum), "bogus device speed\n");
1264 /* FIXME if HZ != 1000 this will probably misbehave ... */
1266 /* look at each urb queued by the host side driver */
1267 spin_lock_irqsave (&dum->lock, flags);
1270 dev_err (dummy_dev(dum),
1271 "timer fired with no URBs pending?\n");
1272 spin_unlock_irqrestore (&dum->lock, flags);
1276 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1279 dum->ep [i].already_seen = 0;
1283 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1285 struct dummy_request *req;
1287 struct dummy_ep *ep = NULL;
1291 if (urb->status != -EINPROGRESS) {
1292 /* likely it was just unlinked */
1294 } else if (dum->rh_state != DUMMY_RH_RUNNING)
1296 type = usb_pipetype (urb->pipe);
1298 /* used up this frame's non-periodic bandwidth?
1299 * FIXME there's infinite bandwidth for control and
1300 * periodic transfers ... unrealistic.
1302 if (total <= 0 && type == PIPE_BULK)
1305 /* find the gadget's ep for this request (if configured) */
1306 address = usb_pipeendpoint (urb->pipe);
1307 if (usb_pipein (urb->pipe))
1308 address |= USB_DIR_IN;
1309 ep = find_endpoint(dum, address);
1311 /* set_configuration() disagreement */
1312 dev_dbg (dummy_dev(dum),
1313 "no ep configured for urb %p\n",
1315 maybe_set_status (urb, -EPROTO);
1319 if (ep->already_seen)
1321 ep->already_seen = 1;
1322 if (ep == &dum->ep [0] && urb->error_count) {
1323 ep->setup_stage = 1; /* a new urb */
1324 urb->error_count = 0;
1326 if (ep->halted && !ep->setup_stage) {
1327 /* NOTE: must not be iso! */
1328 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1330 maybe_set_status (urb, -EPIPE);
1333 /* FIXME make sure both ends agree on maxpacket */
1335 /* handle control requests */
1336 if (ep == &dum->ep [0] && ep->setup_stage) {
1337 struct usb_ctrlrequest setup;
1339 struct dummy_ep *ep2;
1343 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1344 w_index = le16_to_cpu(setup.wIndex);
1345 w_value = le16_to_cpu(setup.wValue);
1346 if (le16_to_cpu(setup.wLength) !=
1347 urb->transfer_buffer_length) {
1348 maybe_set_status (urb, -EOVERFLOW);
1352 /* paranoia, in case of stale queued data */
1353 list_for_each_entry (req, &ep->queue, queue) {
1354 list_del_init (&req->queue);
1355 req->req.status = -EOVERFLOW;
1356 dev_dbg (udc_dev(dum), "stale req = %p\n",
1359 spin_unlock (&dum->lock);
1360 req->req.complete (&ep->ep, &req->req);
1361 spin_lock (&dum->lock);
1362 ep->already_seen = 0;
1366 /* gadget driver never sees set_address or operations
1367 * on standard feature flags. some hardware doesn't
1370 ep->last_io = jiffies;
1371 ep->setup_stage = 0;
1373 switch (setup.bRequest) {
1374 case USB_REQ_SET_ADDRESS:
1375 if (setup.bRequestType != Dev_Request)
1377 dum->address = w_value;
1378 maybe_set_status (urb, 0);
1379 dev_dbg (udc_dev(dum), "set_address = %d\n",
1383 case USB_REQ_SET_FEATURE:
1384 if (setup.bRequestType == Dev_Request) {
1387 case USB_DEVICE_REMOTE_WAKEUP:
1389 case USB_DEVICE_B_HNP_ENABLE:
1390 dum->gadget.b_hnp_enable = 1;
1392 case USB_DEVICE_A_HNP_SUPPORT:
1393 dum->gadget.a_hnp_support = 1;
1395 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1396 dum->gadget.a_alt_hnp_support
1400 value = -EOPNOTSUPP;
1405 maybe_set_status (urb, 0);
1408 } else if (setup.bRequestType == Ep_Request) {
1410 ep2 = find_endpoint (dum, w_index);
1412 value = -EOPNOTSUPP;
1417 maybe_set_status (urb, 0);
1420 case USB_REQ_CLEAR_FEATURE:
1421 if (setup.bRequestType == Dev_Request) {
1423 case USB_DEVICE_REMOTE_WAKEUP:
1424 dum->devstatus &= ~(1 <<
1425 USB_DEVICE_REMOTE_WAKEUP);
1427 maybe_set_status (urb, 0);
1430 value = -EOPNOTSUPP;
1433 } else if (setup.bRequestType == Ep_Request) {
1435 ep2 = find_endpoint (dum, w_index);
1437 value = -EOPNOTSUPP;
1442 maybe_set_status (urb, 0);
1445 case USB_REQ_GET_STATUS:
1446 if (setup.bRequestType == Dev_InRequest
1447 || setup.bRequestType
1449 || setup.bRequestType
1454 // device: remote wakeup, selfpowered
1455 // interface: nothing
1457 buf = (char *)urb->transfer_buffer;
1458 if (urb->transfer_buffer_length > 0) {
1459 if (setup.bRequestType ==
1461 ep2 = find_endpoint (dum, w_index);
1463 value = -EOPNOTSUPP;
1466 buf [0] = ep2->halted;
1467 } else if (setup.bRequestType ==
1474 if (urb->transfer_buffer_length > 1)
1476 urb->actual_length = min (2,
1477 urb->transfer_buffer_length);
1479 maybe_set_status (urb, 0);
1484 /* gadget driver handles all other requests. block
1485 * until setup() returns; no reentrancy issues etc.
1488 spin_unlock (&dum->lock);
1489 value = dum->driver->setup (&dum->gadget,
1491 spin_lock (&dum->lock);
1494 /* no delays (max 64KB data stage) */
1496 goto treat_control_like_bulk;
1498 /* error, see below */
1502 if (value != -EOPNOTSUPP)
1503 dev_dbg (udc_dev(dum),
1506 maybe_set_status (urb, -EPIPE);
1507 urb->actual_length = 0;
1513 /* non-control requests */
1515 switch (usb_pipetype (urb->pipe)) {
1516 case PIPE_ISOCHRONOUS:
1517 /* FIXME is it urb->interval since the last xfer?
1518 * use urb->iso_frame_desc[i].
1519 * complete whether or not ep has requests queued.
1520 * report random errors, to debug drivers.
1522 limit = max (limit, periodic_bytes (dum, ep));
1523 maybe_set_status (urb, -ENOSYS);
1526 case PIPE_INTERRUPT:
1527 /* FIXME is it urb->interval since the last xfer?
1528 * this almost certainly polls too fast.
1530 limit = max (limit, periodic_bytes (dum, ep));
1533 // case PIPE_BULK: case PIPE_CONTROL:
1535 treat_control_like_bulk:
1536 ep->last_io = jiffies;
1537 total = transfer (dum, urb, ep, limit);
1541 /* incomplete transfer? */
1542 if (urb->status == -EINPROGRESS)
1547 list_del (&urbp->urbp_list);
1550 ep->already_seen = ep->setup_stage = 0;
1552 spin_unlock (&dum->lock);
1553 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb);
1554 spin_lock (&dum->lock);
1559 if (list_empty (&dum->urbp_list)) {
1560 usb_put_dev (dum->udev);
1562 } else if (dum->rh_state == DUMMY_RH_RUNNING) {
1563 /* want a 1 msec delay here */
1564 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1567 spin_unlock_irqrestore (&dum->lock, flags);
1570 /*-------------------------------------------------------------------------*/
1572 #define PORT_C_MASK \
1573 ((USB_PORT_STAT_C_CONNECTION \
1574 | USB_PORT_STAT_C_ENABLE \
1575 | USB_PORT_STAT_C_SUSPEND \
1576 | USB_PORT_STAT_C_OVERCURRENT \
1577 | USB_PORT_STAT_C_RESET) << 16)
1579 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1582 unsigned long flags;
1585 dum = hcd_to_dummy (hcd);
1587 spin_lock_irqsave (&dum->lock, flags);
1588 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
1591 if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
1592 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1593 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1594 set_link_state (dum);
1597 if ((dum->port_status & PORT_C_MASK) != 0) {
1599 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1602 if (dum->rh_state == DUMMY_RH_SUSPENDED)
1603 usb_hcd_resume_root_hub (hcd);
1606 spin_unlock_irqrestore (&dum->lock, flags);
1611 hub_descriptor (struct usb_hub_descriptor *desc)
1613 memset (desc, 0, sizeof *desc);
1614 desc->bDescriptorType = 0x29;
1615 desc->bDescLength = 9;
1616 desc->wHubCharacteristics = (__force __u16)
1617 (__constant_cpu_to_le16 (0x0001));
1618 desc->bNbrPorts = 1;
1619 desc->bitmap [0] = 0xff;
1620 desc->bitmap [1] = 0xff;
1623 static int dummy_hub_control (
1624 struct usb_hcd *hcd,
1633 unsigned long flags;
1635 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
1638 dum = hcd_to_dummy (hcd);
1639 spin_lock_irqsave (&dum->lock, flags);
1641 case ClearHubFeature:
1643 case ClearPortFeature:
1645 case USB_PORT_FEAT_SUSPEND:
1646 if (dum->port_status & USB_PORT_STAT_SUSPEND) {
1647 /* 20msec resume signaling */
1649 dum->re_timeout = jiffies +
1650 msecs_to_jiffies(20);
1653 case USB_PORT_FEAT_POWER:
1654 if (dum->port_status & USB_PORT_STAT_POWER)
1655 dev_dbg (dummy_dev(dum), "power-off\n");
1658 dum->port_status &= ~(1 << wValue);
1659 set_link_state (dum);
1662 case GetHubDescriptor:
1663 hub_descriptor ((struct usb_hub_descriptor *) buf);
1666 *(__le32 *) buf = __constant_cpu_to_le32 (0);
1672 /* whoever resets or resumes must GetPortStatus to
1675 if (dum->resuming &&
1676 time_after_eq (jiffies, dum->re_timeout)) {
1677 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1678 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1680 if ((dum->port_status & USB_PORT_STAT_RESET) != 0 &&
1681 time_after_eq (jiffies, dum->re_timeout)) {
1682 dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
1683 dum->port_status &= ~USB_PORT_STAT_RESET;
1685 dum->port_status |= USB_PORT_STAT_ENABLE;
1686 /* give it the best speed we agree on */
1687 dum->gadget.speed = dum->driver->speed;
1688 dum->gadget.ep0->maxpacket = 64;
1689 switch (dum->gadget.speed) {
1690 case USB_SPEED_HIGH:
1692 USB_PORT_STAT_HIGH_SPEED;
1695 dum->gadget.ep0->maxpacket = 8;
1697 USB_PORT_STAT_LOW_SPEED;
1700 dum->gadget.speed = USB_SPEED_FULL;
1705 set_link_state (dum);
1706 ((__le16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1707 ((__le16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1712 case SetPortFeature:
1714 case USB_PORT_FEAT_SUSPEND:
1716 dum->port_status |= USB_PORT_STAT_SUSPEND;
1718 /* HNP would happen here; for now we
1719 * assume b_bus_req is always true.
1721 set_link_state (dum);
1722 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1723 & dum->devstatus) != 0)
1724 dev_dbg (dummy_dev(dum),
1728 case USB_PORT_FEAT_POWER:
1729 dum->port_status |= USB_PORT_STAT_POWER;
1730 set_link_state (dum);
1732 case USB_PORT_FEAT_RESET:
1733 /* if it's already enabled, disable */
1734 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1735 | USB_PORT_STAT_LOW_SPEED
1736 | USB_PORT_STAT_HIGH_SPEED);
1738 /* 50msec reset signaling */
1739 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1742 if ((dum->port_status & USB_PORT_STAT_POWER) != 0) {
1743 dum->port_status |= (1 << wValue);
1744 set_link_state (dum);
1750 dev_dbg (dummy_dev(dum),
1751 "hub control req%04x v%04x i%04x l%d\n",
1752 typeReq, wValue, wIndex, wLength);
1754 /* "protocol stall" on error */
1757 spin_unlock_irqrestore (&dum->lock, flags);
1759 if ((dum->port_status & PORT_C_MASK) != 0)
1760 usb_hcd_poll_rh_status (hcd);
1764 static int dummy_bus_suspend (struct usb_hcd *hcd)
1766 struct dummy *dum = hcd_to_dummy (hcd);
1768 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
1770 spin_lock_irq (&dum->lock);
1771 dum->rh_state = DUMMY_RH_SUSPENDED;
1772 set_link_state (dum);
1773 hcd->state = HC_STATE_SUSPENDED;
1774 spin_unlock_irq (&dum->lock);
1778 static int dummy_bus_resume (struct usb_hcd *hcd)
1780 struct dummy *dum = hcd_to_dummy (hcd);
1783 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
1785 spin_lock_irq (&dum->lock);
1786 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1787 dev_warn (&hcd->self.root_hub->dev, "HC isn't running!\n");
1790 dum->rh_state = DUMMY_RH_RUNNING;
1791 set_link_state (dum);
1792 if (!list_empty(&dum->urbp_list))
1793 mod_timer (&dum->timer, jiffies);
1794 hcd->state = HC_STATE_RUNNING;
1796 spin_unlock_irq (&dum->lock);
1800 /*-------------------------------------------------------------------------*/
1802 static inline ssize_t
1803 show_urb (char *buf, size_t size, struct urb *urb)
1805 int ep = usb_pipeendpoint (urb->pipe);
1807 return snprintf (buf, size,
1808 "urb/%p %s ep%d%s%s len %d/%d\n",
1811 switch (urb->dev->speed) {
1812 case USB_SPEED_LOW: s = "ls"; break;
1813 case USB_SPEED_FULL: s = "fs"; break;
1814 case USB_SPEED_HIGH: s = "hs"; break;
1815 default: s = "?"; break;
1817 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1819 switch (usb_pipetype (urb->pipe)) { \
1820 case PIPE_CONTROL: s = ""; break; \
1821 case PIPE_BULK: s = "-bulk"; break; \
1822 case PIPE_INTERRUPT: s = "-int"; break; \
1823 default: s = "-iso"; break; \
1825 urb->actual_length, urb->transfer_buffer_length);
1829 show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
1831 struct usb_hcd *hcd = dev_get_drvdata (dev);
1832 struct dummy *dum = hcd_to_dummy (hcd);
1835 unsigned long flags;
1837 spin_lock_irqsave (&dum->lock, flags);
1838 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1841 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1845 spin_unlock_irqrestore (&dum->lock, flags);
1849 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1851 static int dummy_start (struct usb_hcd *hcd)
1855 dum = hcd_to_dummy (hcd);
1858 * MASTER side init ... we emulate a root hub that'll only ever
1859 * talk to one device (the slave side). Also appears in sysfs,
1860 * just like more familiar pci-based HCDs.
1862 spin_lock_init (&dum->lock);
1863 init_timer (&dum->timer);
1864 dum->timer.function = dummy_timer;
1865 dum->timer.data = (unsigned long) dum;
1866 dum->rh_state = DUMMY_RH_RUNNING;
1868 INIT_LIST_HEAD (&dum->urbp_list);
1870 /* only show a low-power port: just 8mA */
1871 hcd->power_budget = 8;
1872 hcd->state = HC_STATE_RUNNING;
1873 hcd->uses_new_polling = 1;
1875 #ifdef CONFIG_USB_OTG
1876 hcd->self.otg_port = 1;
1879 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1880 return device_create_file (dummy_dev(dum), &dev_attr_urbs);
1883 static void dummy_stop (struct usb_hcd *hcd)
1887 dum = hcd_to_dummy (hcd);
1889 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1890 usb_gadget_unregister_driver (dum->driver);
1891 dev_info (dummy_dev(dum), "stopped\n");
1894 /*-------------------------------------------------------------------------*/
1896 static int dummy_h_get_frame (struct usb_hcd *hcd)
1898 return dummy_g_get_frame (NULL);
1901 static const struct hc_driver dummy_hcd = {
1902 .description = (char *) driver_name,
1903 .product_desc = "Dummy host controller",
1904 .hcd_priv_size = sizeof(struct dummy),
1908 .start = dummy_start,
1911 .urb_enqueue = dummy_urb_enqueue,
1912 .urb_dequeue = dummy_urb_dequeue,
1914 .get_frame_number = dummy_h_get_frame,
1916 .hub_status_data = dummy_hub_status,
1917 .hub_control = dummy_hub_control,
1918 .bus_suspend = dummy_bus_suspend,
1919 .bus_resume = dummy_bus_resume,
1922 static int dummy_hcd_probe(struct platform_device *pdev)
1924 struct usb_hcd *hcd;
1927 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1929 hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, pdev->dev.bus_id);
1932 the_controller = hcd_to_dummy (hcd);
1934 retval = usb_add_hcd(hcd, 0, 0);
1937 the_controller = NULL;
1942 static int dummy_hcd_remove (struct platform_device *pdev)
1944 struct usb_hcd *hcd;
1946 hcd = platform_get_drvdata (pdev);
1947 usb_remove_hcd (hcd);
1949 the_controller = NULL;
1953 static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
1955 struct usb_hcd *hcd;
1959 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
1961 hcd = platform_get_drvdata (pdev);
1962 dum = hcd_to_dummy (hcd);
1963 if (dum->rh_state == DUMMY_RH_RUNNING) {
1964 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
1967 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1971 static int dummy_hcd_resume (struct platform_device *pdev)
1973 struct usb_hcd *hcd;
1975 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
1977 hcd = platform_get_drvdata (pdev);
1978 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1979 usb_hcd_poll_rh_status (hcd);
1983 static struct platform_driver dummy_hcd_driver = {
1984 .probe = dummy_hcd_probe,
1985 .remove = dummy_hcd_remove,
1986 .suspend = dummy_hcd_suspend,
1987 .resume = dummy_hcd_resume,
1989 .name = (char *) driver_name,
1990 .owner = THIS_MODULE,
1994 /*-------------------------------------------------------------------------*/
1996 /* These don't need to do anything because the pdev structures are
1997 * statically allocated. */
1999 dummy_udc_release (struct device *dev) {}
2002 dummy_hcd_release (struct device *dev) {}
2004 static struct platform_device the_udc_pdev = {
2005 .name = (char *) gadget_name,
2008 .release = dummy_udc_release,
2012 static struct platform_device the_hcd_pdev = {
2013 .name = (char *) driver_name,
2016 .release = dummy_hcd_release,
2020 static int __init init (void)
2024 if (usb_disabled ())
2027 retval = platform_driver_register (&dummy_hcd_driver);
2031 retval = platform_driver_register (&dummy_udc_driver);
2033 goto err_register_udc_driver;
2035 retval = platform_device_register (&the_hcd_pdev);
2037 goto err_register_hcd;
2039 retval = platform_device_register (&the_udc_pdev);
2041 goto err_register_udc;
2045 platform_device_unregister (&the_hcd_pdev);
2047 platform_driver_unregister (&dummy_udc_driver);
2048 err_register_udc_driver:
2049 platform_driver_unregister (&dummy_hcd_driver);
2054 static void __exit cleanup (void)
2056 platform_device_unregister (&the_udc_pdev);
2057 platform_device_unregister (&the_hcd_pdev);
2058 platform_driver_unregister (&dummy_udc_driver);
2059 platform_driver_unregister (&dummy_hcd_driver);
2061 module_exit (cleanup);