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, 2004 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/config.h>
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/delay.h>
43 #include <linux/ioport.h>
44 #include <linux/sched.h>
45 #include <linux/slab.h>
46 #include <linux/smp_lock.h>
47 #include <linux/errno.h>
48 #include <linux/init.h>
49 #include <linux/timer.h>
50 #include <linux/list.h>
51 #include <linux/interrupt.h>
52 #include <linux/version.h>
54 #include <linux/usb.h>
55 #include <linux/usb_gadget.h>
57 #include <asm/byteorder.h>
60 #include <asm/system.h>
61 #include <asm/unaligned.h>
64 #include "../core/hcd.h"
67 #define DRIVER_DESC "USB Host+Gadget Emulator"
68 #define DRIVER_VERSION "14 Mar 2004"
70 static const char driver_name [] = "dummy_hcd";
71 static const char driver_desc [] = "USB Host+Gadget Emulator";
73 static const char gadget_name [] = "dummy_udc";
75 MODULE_DESCRIPTION (DRIVER_DESC);
76 MODULE_AUTHOR ("David Brownell");
77 MODULE_LICENSE ("GPL");
79 /*-------------------------------------------------------------------------*/
81 /* gadget side driver data structres */
83 struct list_head queue;
84 unsigned long last_io; /* jiffies timestamp */
85 struct usb_gadget *gadget;
86 const struct usb_endpoint_descriptor *desc;
89 unsigned already_seen : 1;
90 unsigned setup_stage : 1;
93 struct dummy_request {
94 struct list_head queue; /* ep's requests */
95 struct usb_request req;
98 /*-------------------------------------------------------------------------*/
101 * Every device has ep0 for control requests, plus up to 30 more endpoints,
102 * in one of two types:
104 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
105 * number can be changed. Names like "ep-a" are used for this type.
107 * - Fixed Function: in other cases. some characteristics may be mutable;
108 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
110 * Gadget drivers are responsible for not setting up conflicting endpoint
111 * configurations, illegal or unsupported packet lengths, and so on.
114 static const char ep0name [] = "ep0";
116 static const char *const ep_name [] = {
117 ep0name, /* everyone has ep0 */
119 /* act like a net2280: high speed, six configurable endpoints */
120 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
122 /* or like pxa250: fifteen fixed function endpoints */
123 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
124 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
125 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
128 /* or like sa1100: two fixed function endpoints */
129 "ep1out-bulk", "ep2in-bulk",
131 #define DUMMY_ENDPOINTS (sizeof(ep_name)/sizeof(char *))
139 * SLAVE/GADGET side support
141 struct dummy_ep ep [DUMMY_ENDPOINTS];
143 struct usb_gadget gadget;
144 struct usb_gadget_driver *driver;
145 struct dummy_request fifo_req;
146 u8 fifo_buf [FIFO_SIZE];
148 struct hcd_dev *hdev;
151 * MASTER/HOST side support
154 struct platform_device pdev;
155 struct timer_list timer;
158 struct completion released;
161 static struct dummy *the_controller;
163 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
165 return container_of (ep->gadget, struct dummy, gadget);
168 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
170 return container_of (dev, struct dummy, gadget.dev);
174 * This "hardware" may look a bit odd in diagnostics since it's got both
175 * host and device sides; and it binds different drivers to each side.
177 #define hardware (&the_controller->pdev.dev)
179 /*-------------------------------------------------------------------------*/
181 static struct device_driver dummy_driver = {
182 .name = (char *) driver_name,
183 .bus = &platform_bus_type,
186 /*-------------------------------------------------------------------------*/
188 /* SLAVE/GADGET SIDE DRIVER
190 * This only tracks gadget state. All the work is done when the host
191 * side tries some (emulated) i/o operation. Real device controller
192 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
195 #define is_enabled() \
196 (the_controller->port_status & USB_PORT_STAT_ENABLE)
199 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
206 ep = container_of (_ep, struct dummy_ep, ep);
207 if (!_ep || !desc || ep->desc || _ep->name == ep0name
208 || desc->bDescriptorType != USB_DT_ENDPOINT)
209 if (!the_controller->driver || !is_enabled ())
211 max = desc->wMaxPacketSize & 0x3ff;
213 /* drivers must not request bad settings, since lower levels
214 * (hardware or its drivers) may not check. some endpoints
215 * can't do iso, many have maxpacket limitations, etc.
217 * since this "hardware" driver is here to help debugging, we
218 * have some extra sanity checks. (there could be more though,
219 * especially for "ep9out" style fixed function ones.)
221 dum = container_of (ep->gadget, struct dummy, gadget);
223 switch (desc->bmAttributes & 0x03) {
224 case USB_ENDPOINT_XFER_BULK:
225 if (strstr (ep->ep.name, "-iso")
226 || strstr (ep->ep.name, "-int")) {
229 switch (dum->gadget.speed) {
233 /* conserve return statements */
236 case 8: case 16: case 32: case 64:
237 /* we'll fake any legal size */
245 case USB_ENDPOINT_XFER_INT:
246 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
248 /* real hardware might not handle all packet sizes */
249 switch (dum->gadget.speed) {
253 /* save a return statement */
257 /* save a return statement */
264 case USB_ENDPOINT_XFER_ISOC:
265 if (strstr (ep->ep.name, "-bulk")
266 || strstr (ep->ep.name, "-int"))
268 /* real hardware might not handle all packet sizes */
269 switch (dum->gadget.speed) {
273 /* save a return statement */
277 /* save a return statement */
283 /* few chips support control except on ep0 */
287 _ep->maxpacket = max;
290 dev_dbg (hardware, "enabled %s (ep%d%s-%s) maxpacket %d\n",
292 desc->bEndpointAddress & 0x0f,
293 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
295 switch (desc->bmAttributes & 0x03) {
296 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
297 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
298 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
299 default: val = "ctrl"; break;
303 /* at this point real hardware should be NAKing transfers
304 * to that endpoint, until a buffer is queued to it.
311 /* called with spinlock held */
312 static void nuke (struct dummy *dum, struct dummy_ep *ep)
314 while (!list_empty (&ep->queue)) {
315 struct dummy_request *req;
317 req = list_entry (ep->queue.next, struct dummy_request, queue);
318 list_del_init (&req->queue);
319 req->req.status = -ESHUTDOWN;
321 spin_unlock (&dum->lock);
322 req->req.complete (&ep->ep, &req->req);
323 spin_lock (&dum->lock);
327 static int dummy_disable (struct usb_ep *_ep)
334 ep = container_of (_ep, struct dummy_ep, ep);
335 if (!_ep || !ep->desc || _ep->name == ep0name)
337 dum = ep_to_dummy (ep);
339 spin_lock_irqsave (&dum->lock, flags);
343 spin_unlock_irqrestore (&dum->lock, flags);
345 dev_dbg (hardware, "disabled %s\n", _ep->name);
349 static struct usb_request *
350 dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
353 struct dummy_request *req;
355 ep = container_of (_ep, struct dummy_ep, ep);
359 req = kmalloc (sizeof *req, mem_flags);
362 memset (req, 0, sizeof *req);
363 INIT_LIST_HEAD (&req->queue);
368 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
371 struct dummy_request *req;
373 ep = container_of (_ep, struct dummy_ep, ep);
374 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
377 req = container_of (_req, struct dummy_request, req);
378 WARN_ON (!list_empty (&req->queue));
391 if (!the_controller->driver)
393 retval = kmalloc (bytes, mem_flags);
394 *dma = (dma_addr_t) retval;
410 fifo_complete (struct usb_ep *ep, struct usb_request *req)
413 dev_dbg (hardware, "fifo_complete: %d\n", req->status);
418 dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
421 struct dummy_request *req;
425 req = container_of (_req, struct dummy_request, req);
426 if (!_req || !list_empty (&req->queue) || !_req->complete)
429 ep = container_of (_ep, struct dummy_ep, ep);
430 if (!_ep || (!ep->desc && _ep->name != ep0name))
433 if (!the_controller->driver || !is_enabled ())
436 dum = container_of (ep->gadget, struct dummy, gadget);
439 dev_dbg (hardware, "ep %p queue req %p to %s, len %d buf %p\n",
440 ep, _req, _ep->name, _req->length, _req->buf);
443 _req->status = -EINPROGRESS;
445 spin_lock_irqsave (&dum->lock, flags);
447 /* implement an emulated single-request FIFO */
448 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
449 list_empty (&dum->fifo_req.queue) &&
450 list_empty (&ep->queue) &&
451 _req->length <= FIFO_SIZE) {
452 req = &dum->fifo_req;
454 req->req.buf = dum->fifo_buf;
455 memcpy (dum->fifo_buf, _req->buf, _req->length);
456 req->req.context = dum;
457 req->req.complete = fifo_complete;
459 spin_unlock (&dum->lock);
460 _req->actual = _req->length;
462 _req->complete (_ep, _req);
463 spin_lock (&dum->lock);
465 list_add_tail (&req->queue, &ep->queue);
466 spin_unlock_irqrestore (&dum->lock, flags);
468 /* real hardware would likely enable transfers here, in case
469 * it'd been left NAKing.
474 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
478 int retval = -EINVAL;
480 struct dummy_request *req = 0;
482 if (!the_controller->driver)
487 ep = container_of (_ep, struct dummy_ep, ep);
488 dum = container_of (ep->gadget, struct dummy, gadget);
490 spin_lock_irqsave (&dum->lock, flags);
491 list_for_each_entry (req, &ep->queue, queue) {
492 if (&req->req == _req) {
493 list_del_init (&req->queue);
494 _req->status = -ECONNRESET;
499 spin_unlock_irqrestore (&dum->lock, flags);
502 dev_dbg (hardware, "dequeued req %p from %s, len %d buf %p\n",
503 req, _ep->name, _req->length, _req->buf);
505 _req->complete (_ep, _req);
511 dummy_set_halt (struct usb_ep *_ep, int value)
517 if (!the_controller->driver)
519 ep = container_of (_ep, struct dummy_ep, ep);
522 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
523 !list_empty (&ep->queue))
527 /* FIXME clear emulated data toggle too */
531 static const struct usb_ep_ops dummy_ep_ops = {
532 .enable = dummy_enable,
533 .disable = dummy_disable,
535 .alloc_request = dummy_alloc_request,
536 .free_request = dummy_free_request,
538 .alloc_buffer = dummy_alloc_buffer,
539 .free_buffer = dummy_free_buffer,
540 /* map, unmap, ... eventually hook the "generic" dma calls */
542 .queue = dummy_queue,
543 .dequeue = dummy_dequeue,
545 .set_halt = dummy_set_halt,
548 /*-------------------------------------------------------------------------*/
550 /* there are both host and device side versions of this call ... */
551 static int dummy_g_get_frame (struct usb_gadget *_gadget)
555 do_gettimeofday (&tv);
556 return tv.tv_usec / 1000;
559 static const struct usb_gadget_ops dummy_ops = {
560 .get_frame = dummy_g_get_frame,
563 /*-------------------------------------------------------------------------*/
565 /* "function" sysfs attribute */
567 show_function (struct device *_dev, char *buf)
569 struct dummy *dum = the_controller;
571 if (!dum->driver->function
572 || strlen (dum->driver->function) > PAGE_SIZE)
574 return snprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
576 DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
578 /*-------------------------------------------------------------------------*/
581 * Driver registration/unregistration.
583 * This is basically hardware-specific; there's usually only one real USB
584 * device (not host) controller since that's how USB devices are intended
585 * to work. So most implementations of these api calls will rely on the
586 * fact that only one driver will ever bind to the hardware. But curious
587 * hardware can be built with discrete components, so the gadget API doesn't
588 * require that assumption.
590 * For this emulator, it might be convenient to create a usb slave device
591 * for each driver that registers: just add to a big root hub.
595 dummy_udc_release (struct device *dev)
597 struct dummy *dum = gadget_dev_to_dummy (dev);
599 complete (&dum->released);
603 dummy_hc_release (struct device *dev)
605 struct dummy *dum = dev_get_drvdata (dev);
607 complete (&dum->released);
611 dummy_register_udc (struct dummy *dum)
615 strcpy (dum->gadget.dev.bus_id, "udc");
616 dum->gadget.dev.parent = &dum->pdev.dev;
617 dum->gadget.dev.release = dummy_udc_release;
619 rc = device_register (&dum->gadget.dev);
621 device_create_file (&dum->gadget.dev, &dev_attr_function);
626 dummy_unregister_udc (struct dummy *dum)
628 device_remove_file (&dum->gadget.dev, &dev_attr_function);
629 init_completion (&dum->released);
630 device_unregister (&dum->gadget.dev);
631 wait_for_completion (&dum->released);
635 usb_gadget_register_driver (struct usb_gadget_driver *driver)
637 struct dummy *dum = the_controller;
644 if (!driver->bind || !driver->unbind || !driver->setup
645 || driver->speed == USB_SPEED_UNKNOWN)
649 * SLAVE side init ... the layer above hardware, which
650 * can't enumerate without help from the driver we're binding.
652 dum->gadget.name = gadget_name;
653 dum->gadget.ops = &dummy_ops;
654 dum->gadget.is_dualspeed = 1;
656 INIT_LIST_HEAD (&dum->gadget.ep_list);
657 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
658 struct dummy_ep *ep = &dum->ep [i];
662 ep->ep.name = ep_name [i];
663 ep->ep.ops = &dummy_ep_ops;
664 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
665 ep->halted = ep->already_seen = ep->setup_stage = 0;
666 ep->ep.maxpacket = ~0;
667 ep->last_io = jiffies;
668 ep->gadget = &dum->gadget;
670 INIT_LIST_HEAD (&ep->queue);
673 dum->gadget.ep0 = &dum->ep [0].ep;
674 dum->ep [0].ep.maxpacket = 64;
675 list_del_init (&dum->ep [0].ep.ep_list);
676 INIT_LIST_HEAD(&dum->fifo_req.queue);
678 dum->driver = driver;
679 dum->gadget.dev.driver = &driver->driver;
680 dev_dbg (hardware, "binding gadget driver '%s'\n", driver->driver.name);
681 if ((retval = driver->bind (&dum->gadget)) != 0) {
683 dum->gadget.dev.driver = 0;
687 // FIXME: Check these calls for errors and re-order
688 driver->driver.bus = dum->pdev.dev.bus;
689 driver_register (&driver->driver);
691 device_bind_driver (&dum->gadget.dev);
693 /* khubd will enumerate this in a while */
694 dum->port_status |= USB_PORT_STAT_CONNECTION
695 | (1 << USB_PORT_FEAT_C_CONNECTION);
698 EXPORT_SYMBOL (usb_gadget_register_driver);
700 /* caller must hold lock */
702 stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
706 /* prevent any more requests */
710 /* this might not succeed ... */
711 del_timer (&dum->timer);
713 /* nuke any pending requests first, so driver i/o is quiesced */
714 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
717 /* driver now does any non-usb quiescing necessary */
719 spin_unlock (&dum->lock);
720 driver->disconnect (&dum->gadget);
721 spin_lock (&dum->lock);
726 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
728 struct dummy *dum = the_controller;
733 if (!driver || driver != dum->driver)
736 dev_dbg (hardware, "unregister gadget driver '%s'\n",
737 driver->driver.name);
739 spin_lock_irqsave (&dum->lock, flags);
740 stop_activity (dum, driver);
741 dum->port_status &= ~USB_PORT_STAT_CONNECTION;
742 dum->port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
743 spin_unlock_irqrestore (&dum->lock, flags);
745 driver->unbind (&dum->gadget);
748 device_release_driver (&dum->gadget.dev);
750 driver_unregister (&driver->driver);
752 del_timer_sync (&dum->timer);
755 EXPORT_SYMBOL (usb_gadget_unregister_driver);
759 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
763 EXPORT_SYMBOL (net2280_set_fifo_mode);
765 /*-------------------------------------------------------------------------*/
767 /* MASTER/HOST SIDE DRIVER
769 * this uses the hcd framework to hook up to host side drivers.
770 * its root hub will only have one device, otherwise it acts like
771 * a normal host controller.
773 * when urbs are queued, they're just stuck on a list that we
774 * scan in a timer callback. that callback connects writes from
775 * the host with reads from the device, and so on, based on the
779 static int dummy_urb_enqueue (
787 /* patch to usb_sg_init() is in 2.5.60 */
788 BUG_ON (!urb->transfer_buffer && urb->transfer_buffer_length);
790 dum = container_of (hcd, struct dummy, hcd);
791 spin_lock_irqsave (&dum->lock, flags);
794 dum->hdev = urb->dev->hcpriv;
796 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
797 urb->error_count = 1; /* mark as a new urb */
799 /* kick the scheduler, it'll do the rest */
800 if (!timer_pending (&dum->timer))
801 mod_timer (&dum->timer, jiffies + 1);
803 spin_unlock_irqrestore (&dum->lock, flags);
807 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
809 /* giveback happens automatically in timer callback */
813 static void maybe_set_status (struct urb *urb, int status)
815 spin_lock (&urb->lock);
816 if (urb->status == -EINPROGRESS)
817 urb->status = status;
818 spin_unlock (&urb->lock);
821 /* transfer up to a frame's worth; caller must own lock */
823 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
825 struct dummy_request *req;
828 /* if there's no request queued, the device is NAKing; return */
829 list_for_each_entry (req, &ep->queue, queue) {
830 unsigned host_len, dev_len, len;
831 int is_short, to_host;
834 /* 1..N packets of ep->ep.maxpacket each ... the last one
835 * may be short (including zero length).
837 * writer can send a zlp explicitly (length 0) or implicitly
838 * (length mod maxpacket zero, and 'zero' flag); they always
841 host_len = urb->transfer_buffer_length - urb->actual_length;
842 dev_len = req->req.length - req->req.actual;
843 len = min (host_len, dev_len);
845 /* FIXME update emulated data toggle too */
847 to_host = usb_pipein (urb->pipe);
848 if (unlikely (len == 0))
853 /* not enough bandwidth left? */
854 if (limit < ep->ep.maxpacket && limit < len)
856 len = min (len, (unsigned) limit);
860 /* use an extra pass for the final short packet */
861 if (len > ep->ep.maxpacket) {
863 len -= (len % ep->ep.maxpacket);
865 is_short = (len % ep->ep.maxpacket) != 0;
867 /* else transfer packet(s) */
868 ubuf = urb->transfer_buffer + urb->actual_length;
869 rbuf = req->req.buf + req->req.actual;
871 memcpy (ubuf, rbuf, len);
873 memcpy (rbuf, ubuf, len);
874 ep->last_io = jiffies;
877 urb->actual_length += len;
878 req->req.actual += len;
881 /* short packets terminate, maybe with overflow/underflow.
882 * it's only really an error to write too much.
884 * partially filling a buffer optionally blocks queue advances
885 * (so completion handlers can clean up the queue) but we don't
886 * need to emulate such data-in-flight. so we only show part
887 * of the URB_SHORT_NOT_OK effect: completion status.
890 if (host_len == dev_len) {
892 maybe_set_status (urb, 0);
893 } else if (to_host) {
895 if (dev_len > host_len)
896 maybe_set_status (urb, -EOVERFLOW);
898 maybe_set_status (urb,
902 } else if (!to_host) {
903 maybe_set_status (urb, 0);
904 if (host_len > dev_len)
905 req->req.status = -EOVERFLOW;
910 /* many requests terminate without a short packet */
912 if (req->req.length == req->req.actual
915 if (urb->transfer_buffer_length == urb->actual_length
916 && !(urb->transfer_flags
917 & URB_ZERO_PACKET)) {
918 maybe_set_status (urb, 0);
922 /* device side completion --> continuable */
923 if (req->req.status != -EINPROGRESS) {
924 list_del_init (&req->queue);
926 spin_unlock (&dum->lock);
927 req->req.complete (&ep->ep, &req->req);
928 spin_lock (&dum->lock);
930 /* requests might have been unlinked... */
934 /* host side completion --> terminate */
935 if (urb->status != -EINPROGRESS)
938 /* rescan to continue with any other queued i/o */
945 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
947 int limit = ep->ep.maxpacket;
949 if (dum->gadget.speed == USB_SPEED_HIGH) {
952 /* high bandwidth mode */
953 tmp = ep->desc->wMaxPacketSize;
954 tmp = le16_to_cpu (tmp);
955 tmp = (tmp >> 11) & 0x03;
956 tmp *= 8 /* applies to entire frame */;
957 limit += limit * tmp;
962 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
966 if ((address & ~USB_DIR_IN) == 0)
968 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
969 struct dummy_ep *ep = &dum->ep [i];
973 if (ep->desc->bEndpointAddress == address)
979 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
980 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
981 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
982 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
983 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
984 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
986 /* drive both sides of the transfers; looks like irq handlers to
987 * both drivers except the callbacks aren't in_irq().
989 static void dummy_timer (unsigned long _dum)
991 struct dummy *dum = (struct dummy *) _dum;
992 struct hcd_dev *hdev = dum->hdev;
993 struct list_head *entry, *tmp;
999 dev_err (hardware, "timer fired with device gone?\n");
1003 /* simplistic model for one frame's bandwidth */
1004 switch (dum->gadget.speed) {
1006 total = 8/*bytes*/ * 12/*packets*/;
1008 case USB_SPEED_FULL:
1009 total = 64/*bytes*/ * 19/*packets*/;
1011 case USB_SPEED_HIGH:
1012 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1015 dev_err (hardware, "bogus device speed\n");
1019 /* FIXME if HZ != 1000 this will probably misbehave ... */
1021 /* look at each urb queued by the host side driver */
1022 spin_lock_irqsave (&dum->lock, flags);
1023 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1026 dum->ep [i].already_seen = 0;
1030 list_for_each_safe (entry, tmp, &hdev->urb_list) {
1032 struct dummy_request *req;
1034 struct dummy_ep *ep = 0;
1037 urb = list_entry (entry, struct urb, urb_list);
1038 if (urb->status != -EINPROGRESS) {
1039 /* likely it was just unlinked */
1042 type = usb_pipetype (urb->pipe);
1044 /* used up this frame's non-periodic bandwidth?
1045 * FIXME there's infinite bandwidth for control and
1046 * periodic transfers ... unrealistic.
1048 if (total <= 0 && type == PIPE_BULK)
1051 /* find the gadget's ep for this request (if configured) */
1052 address = usb_pipeendpoint (urb->pipe);
1053 if (usb_pipein (urb->pipe))
1054 address |= USB_DIR_IN;
1055 ep = find_endpoint(dum, address);
1057 /* set_configuration() disagreement */
1059 "no ep configured for urb %p\n",
1061 maybe_set_status (urb, -ETIMEDOUT);
1065 if (ep->already_seen)
1067 ep->already_seen = 1;
1068 if (ep == &dum->ep [0] && urb->error_count) {
1069 ep->setup_stage = 1; /* a new urb */
1070 urb->error_count = 0;
1072 if (ep->halted && !ep->setup_stage) {
1073 /* NOTE: must not be iso! */
1074 dev_dbg (hardware, "ep %s halted, urb %p\n",
1076 maybe_set_status (urb, -EPIPE);
1079 /* FIXME make sure both ends agree on maxpacket */
1081 /* handle control requests */
1082 if (ep == &dum->ep [0] && ep->setup_stage) {
1083 struct usb_ctrlrequest setup;
1085 struct dummy_ep *ep2;
1087 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1088 le16_to_cpus (&setup.wIndex);
1089 le16_to_cpus (&setup.wValue);
1090 le16_to_cpus (&setup.wLength);
1091 if (setup.wLength != urb->transfer_buffer_length) {
1092 maybe_set_status (urb, -EOVERFLOW);
1096 /* paranoia, in case of stale queued data */
1097 list_for_each_entry (req, &ep->queue, queue) {
1098 list_del_init (&req->queue);
1099 req->req.status = -EOVERFLOW;
1100 dev_dbg (hardware, "stale req = %p\n", req);
1102 spin_unlock (&dum->lock);
1103 req->req.complete (&ep->ep, &req->req);
1104 spin_lock (&dum->lock);
1105 ep->already_seen = 0;
1109 /* gadget driver never sees set_address or operations
1110 * on standard feature flags. some hardware doesn't
1113 ep->last_io = jiffies;
1114 ep->setup_stage = 0;
1116 switch (setup.bRequest) {
1117 case USB_REQ_SET_ADDRESS:
1118 if (setup.bRequestType != Dev_Request)
1120 if (dum->address != 0) {
1121 maybe_set_status (urb, -ETIMEDOUT);
1122 urb->actual_length = 0;
1125 dum->address = setup.wValue;
1126 maybe_set_status (urb, 0);
1127 dev_dbg (hardware, "set_address = %d\n",
1131 case USB_REQ_SET_FEATURE:
1132 if (setup.bRequestType == Dev_Request) {
1133 // remote wakeup, and (hs) test mode
1134 value = -EOPNOTSUPP;
1135 } else if (setup.bRequestType == Ep_Request) {
1137 ep2 = find_endpoint (dum,
1140 value = -EOPNOTSUPP;
1145 maybe_set_status (urb, 0);
1148 case USB_REQ_CLEAR_FEATURE:
1149 if (setup.bRequestType == Dev_Request) {
1152 maybe_set_status (urb, 0);
1153 } else if (setup.bRequestType == Ep_Request) {
1155 ep2 = find_endpoint (dum,
1158 value = -EOPNOTSUPP;
1163 maybe_set_status (urb, 0);
1166 case USB_REQ_GET_STATUS:
1167 if (setup.bRequestType == Dev_InRequest
1168 || setup.bRequestType
1170 || setup.bRequestType
1175 // device: remote wakeup, selfpowered
1176 // interface: nothing
1178 buf = (char *)urb->transfer_buffer;
1179 if (urb->transfer_buffer_length > 0) {
1180 if (setup.bRequestType ==
1182 ep2 = find_endpoint (dum, setup.wIndex);
1184 value = -EOPNOTSUPP;
1187 buf [0] = ep2->halted;
1191 if (urb->transfer_buffer_length > 1)
1193 urb->actual_length = min (2,
1194 urb->transfer_buffer_length);
1196 maybe_set_status (urb, 0);
1201 /* gadget driver handles all other requests. block
1202 * until setup() returns; no reentrancy issues etc.
1205 spin_unlock (&dum->lock);
1206 value = dum->driver->setup (&dum->gadget,
1208 spin_lock (&dum->lock);
1211 /* no delays (max 64KB data stage) */
1213 goto treat_control_like_bulk;
1215 /* error, see below */
1219 if (value != -EOPNOTSUPP)
1223 maybe_set_status (urb, -EPIPE);
1224 urb->actual_length = 0;
1230 /* non-control requests */
1232 switch (usb_pipetype (urb->pipe)) {
1233 case PIPE_ISOCHRONOUS:
1234 /* FIXME is it urb->interval since the last xfer?
1235 * use urb->iso_frame_desc[i].
1236 * complete whether or not ep has requests queued.
1237 * report random errors, to debug drivers.
1239 limit = max (limit, periodic_bytes (dum, ep));
1240 maybe_set_status (urb, -ENOSYS);
1243 case PIPE_INTERRUPT:
1244 /* FIXME is it urb->interval since the last xfer?
1245 * this almost certainly polls too fast.
1247 limit = max (limit, periodic_bytes (dum, ep));
1250 // case PIPE_BULK: case PIPE_CONTROL:
1252 treat_control_like_bulk:
1253 ep->last_io = jiffies;
1254 total = transfer (dum, urb, ep, limit);
1258 /* incomplete transfer? */
1259 if (urb->status == -EINPROGRESS)
1265 ep->already_seen = ep->setup_stage = 0;
1267 spin_unlock (&dum->lock);
1268 usb_hcd_giveback_urb (&dum->hcd, urb, 0);
1269 spin_lock (&dum->lock);
1274 /* want a 1 msec delay here */
1275 if (!list_empty (&hdev->urb_list))
1276 mod_timer (&dum->timer, jiffies + 1);
1278 spin_unlock_irqrestore (&dum->lock, flags);
1281 /*-------------------------------------------------------------------------*/
1283 #define PORT_C_MASK \
1284 ((1 << USB_PORT_FEAT_C_CONNECTION) \
1285 | (1 << USB_PORT_FEAT_C_ENABLE) \
1286 | (1 << USB_PORT_FEAT_C_SUSPEND) \
1287 | (1 << USB_PORT_FEAT_C_OVER_CURRENT) \
1288 | (1 << USB_PORT_FEAT_C_RESET))
1290 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1293 unsigned long flags;
1296 dum = container_of (hcd, struct dummy, hcd);
1298 spin_lock_irqsave (&dum->lock, flags);
1299 if (!(dum->port_status & PORT_C_MASK))
1303 dev_dbg (hardware, "port status 0x%08x has changes\n",
1307 spin_unlock_irqrestore (&dum->lock, flags);
1312 hub_descriptor (struct usb_hub_descriptor *desc)
1314 memset (desc, 0, sizeof *desc);
1315 desc->bDescriptorType = 0x29;
1316 desc->bDescLength = 9;
1317 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1318 desc->bNbrPorts = 1;
1319 desc->bitmap [0] = 0xff;
1320 desc->bitmap [1] = 0xff;
1323 static int dummy_hub_control (
1324 struct usb_hcd *hcd,
1333 unsigned long flags;
1335 dum = container_of (hcd, struct dummy, hcd);
1336 spin_lock_irqsave (&dum->lock, flags);
1338 case ClearHubFeature:
1340 case ClearPortFeature:
1341 // FIXME won't some of these need special handling?
1342 dum->port_status &= ~(1 << wValue);
1344 case GetHubDescriptor:
1345 hub_descriptor ((struct usb_hub_descriptor *) buf);
1348 *(u32 *) buf = __constant_cpu_to_le32 (0);
1353 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1354 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1359 case SetPortFeature:
1360 if (wValue == USB_PORT_FEAT_RESET) {
1361 /* if it's already running, disconnect first */
1362 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1363 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1364 | USB_PORT_STAT_LOW_SPEED
1365 | USB_PORT_STAT_HIGH_SPEED);
1367 dev_dbg (hardware, "disconnect\n");
1368 stop_activity (dum, dum->driver);
1371 /* FIXME test that code path! */
1374 (1 << USB_PORT_FEAT_C_ENABLE);
1376 dum->port_status |= USB_PORT_STAT_ENABLE |
1377 (1 << USB_PORT_FEAT_C_RESET);
1380 /* give it the best speed we agree on */
1381 dum->gadget.speed = dum->driver->speed;
1382 dum->gadget.ep0->maxpacket = 64;
1383 switch (dum->gadget.speed) {
1384 case USB_SPEED_HIGH:
1386 USB_PORT_STAT_HIGH_SPEED;
1389 dum->gadget.ep0->maxpacket = 8;
1391 USB_PORT_STAT_LOW_SPEED;
1394 dum->gadget.speed = USB_SPEED_FULL;
1399 dum->port_status |= (1 << wValue);
1404 "hub control req%04x v%04x i%04x l%d\n",
1405 typeReq, wValue, wIndex, wLength);
1407 /* "protocol stall" on error */
1410 spin_unlock_irqrestore (&dum->lock, flags);
1415 /*-------------------------------------------------------------------------*/
1417 static struct usb_hcd *dummy_alloc (void)
1421 dum = kmalloc (sizeof *dum, SLAB_KERNEL);
1424 memset (dum, 0, sizeof *dum);
1428 static void dummy_free (struct usb_hcd *hcd)
1432 dum = container_of (hcd, struct dummy, hcd);
1433 WARN_ON (dum->driver != 0);
1437 /*-------------------------------------------------------------------------*/
1439 static inline ssize_t
1440 show_urb (char *buf, size_t size, struct urb *urb)
1442 int ep = usb_pipeendpoint (urb->pipe);
1444 return snprintf (buf, size,
1445 "urb/%p %s ep%d%s%s len %d/%d\n",
1448 switch (urb->dev->speed) {
1449 case USB_SPEED_LOW: s = "ls"; break;
1450 case USB_SPEED_FULL: s = "fs"; break;
1451 case USB_SPEED_HIGH: s = "hs"; break;
1452 default: s = "?"; break;
1454 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1456 switch (usb_pipetype (urb->pipe)) { \
1457 case PIPE_CONTROL: s = ""; break; \
1458 case PIPE_BULK: s = "-bulk"; break; \
1459 case PIPE_INTERRUPT: s = "-int"; break; \
1460 default: s = "-iso"; break; \
1462 urb->actual_length, urb->transfer_buffer_length);
1466 show_urbs (struct device *dev, char *buf)
1468 struct dummy *dum = dev_get_drvdata(dev);
1471 unsigned long flags;
1473 spin_lock_irqsave (&dum->lock, flags);
1475 list_for_each_entry (urb, &dum->hdev->urb_list, urb_list) {
1478 temp = show_urb (buf, PAGE_SIZE - size, urb);
1483 spin_unlock_irqrestore (&dum->lock, flags);
1487 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1490 static const struct hc_driver dummy_hcd;
1492 static int dummy_start (struct usb_hcd *hcd)
1495 struct usb_bus *bus;
1496 struct usb_device *root;
1499 dum = container_of (hcd, struct dummy, hcd);
1502 * MASTER side init ... we emulate a root hub that'll only ever
1503 * talk to one device (the slave side). Also appears in sysfs,
1504 * just like more familiar pci-based HCDs.
1506 spin_lock_init (&dum->lock);
1508 retval = driver_register (&dummy_driver);
1512 dum->pdev.name = "hc";
1513 dum->pdev.dev.driver = &dummy_driver;
1514 dev_set_drvdata(&dum->pdev.dev, dum);
1515 dum->pdev.dev.release = dummy_hc_release;
1516 retval = platform_device_register (&dum->pdev);
1518 driver_unregister (&dummy_driver);
1521 dev_info (&dum->pdev.dev, "%s, driver " DRIVER_VERSION "\n",
1524 hcd->self.controller = &dum->pdev.dev;
1526 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1527 device_create_file (hcd->self.controller, &dev_attr_urbs);
1529 init_timer (&dum->timer);
1530 dum->timer.function = dummy_timer;
1531 dum->timer.data = (unsigned long) dum;
1533 /* root hub will appear as another device */
1534 dum->hcd.driver = (struct hc_driver *) &dummy_hcd;
1535 dum->hcd.description = dummy_hcd.description;
1536 dum->hcd.product_desc = "Dummy host controller";
1538 bus = hcd_to_bus (&dum->hcd);
1539 bus->bus_name = dum->pdev.dev.bus_id;
1541 bus->op = &usb_hcd_operations;
1542 bus->hcpriv = &dum->hcd;
1544 /* FIXME don't require the pci-based buffer/alloc impls;
1545 * the "generic dma" implementation still requires them,
1546 * it's not very generic yet.
1548 if ((retval = hcd_buffer_create (&dum->hcd)) != 0) {
1550 init_completion (&dum->released);
1551 platform_device_unregister (&dum->pdev);
1552 wait_for_completion (&dum->released);
1553 driver_unregister (&dummy_driver);
1557 INIT_LIST_HEAD (&hcd->dev_list);
1558 usb_register_bus (bus);
1560 bus->root_hub = root = usb_alloc_dev (0, bus, 0);
1564 hcd_buffer_destroy (&dum->hcd);
1565 usb_deregister_bus (bus);
1569 /* root hub enters addressed state... */
1570 dum->hcd.state = USB_STATE_RUNNING;
1571 root->speed = USB_SPEED_HIGH;
1573 /* ...then configured, so khubd sees us. */
1574 if ((retval = hcd_register_root (&dum->hcd)) != 0) {
1578 dum->hcd.state = USB_STATE_QUIESCING;
1584 if ((retval = dummy_register_udc (dum)) != 0) {
1586 usb_disconnect (&bus->root_hub);
1592 static void dummy_stop (struct usb_hcd *hcd)
1595 struct usb_bus *bus;
1597 dum = container_of (hcd, struct dummy, hcd);
1602 usb_gadget_unregister_driver (dum->driver);
1603 dummy_unregister_udc (dum);
1605 bus = hcd_to_bus (&dum->hcd);
1606 hcd->state = USB_STATE_QUIESCING;
1607 dev_dbg (hardware, "remove root hub\n");
1608 usb_disconnect (&bus->root_hub);
1610 hcd_buffer_destroy (&dum->hcd);
1611 usb_deregister_bus (bus);
1613 dev_info (hardware, "stopped\n");
1615 device_remove_file (hcd->self.controller, &dev_attr_urbs);
1616 init_completion (&dum->released);
1617 platform_device_unregister (&dum->pdev);
1618 wait_for_completion (&dum->released);
1620 driver_unregister (&dummy_driver);
1623 /*-------------------------------------------------------------------------*/
1625 static int dummy_h_get_frame (struct usb_hcd *hcd)
1627 return dummy_g_get_frame (0);
1630 static const struct hc_driver dummy_hcd = {
1631 .description = (char *) driver_name,
1634 .start = dummy_start,
1637 .hcd_alloc = dummy_alloc,
1638 .hcd_free = dummy_free,
1640 .urb_enqueue = dummy_urb_enqueue,
1641 .urb_dequeue = dummy_urb_dequeue,
1643 .get_frame_number = dummy_h_get_frame,
1645 .hub_status_data = dummy_hub_status,
1646 .hub_control = dummy_hub_control,
1649 /*-------------------------------------------------------------------------*/
1651 static int __init init (void)
1653 struct usb_hcd *hcd;
1656 if (usb_disabled ())
1658 if ((hcd = dummy_alloc ()) == 0)
1661 the_controller = container_of (hcd, struct dummy, hcd);
1662 value = dummy_start (hcd);
1672 static void __exit cleanup (void)
1674 dummy_stop (&the_controller->hcd);
1675 dummy_free (&the_controller->hcd);
1678 module_exit (cleanup);