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 "29 Oct 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 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 (sizeof(ep_name)/sizeof(char *))
147 struct usb_hcd hcd; /* must come first! */
151 * SLAVE/GADGET side support
153 struct dummy_ep ep [DUMMY_ENDPOINTS];
155 struct usb_gadget gadget;
156 struct usb_gadget_driver *driver;
157 struct dummy_request fifo_req;
158 u8 fifo_buf [FIFO_SIZE];
162 * MASTER/HOST side support
164 struct timer_list timer;
168 unsigned long re_timeout;
170 struct usb_device *udev;
173 static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
175 return container_of(hcd, struct dummy, hcd);
178 static inline struct device *dummy_dev (struct dummy *dum)
180 return dum->hcd.self.controller;
183 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
185 return container_of (ep->gadget, struct dummy, gadget);
188 static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
190 return container_of (gadget, struct dummy, gadget);
193 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
195 return container_of (dev, struct dummy, gadget.dev);
198 static struct dummy *the_controller;
200 /*-------------------------------------------------------------------------*/
203 * This "hardware" may look a bit odd in diagnostics since it's got both
204 * host and device sides; and it binds different drivers to each side.
206 static struct platform_device the_pdev;
208 static struct device_driver dummy_driver = {
209 .name = (char *) driver_name,
210 .bus = &platform_bus_type,
213 /*-------------------------------------------------------------------------*/
215 /* SLAVE/GADGET SIDE DRIVER
217 * This only tracks gadget state. All the work is done when the host
218 * side tries some (emulated) i/o operation. Real device controller
219 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
222 #define is_enabled(dum) \
223 (dum->port_status & USB_PORT_STAT_ENABLE)
226 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
233 ep = usb_ep_to_dummy_ep (_ep);
234 if (!_ep || !desc || ep->desc || _ep->name == ep0name
235 || desc->bDescriptorType != USB_DT_ENDPOINT)
237 dum = ep_to_dummy (ep);
238 if (!dum->driver || !is_enabled (dum))
240 max = desc->wMaxPacketSize & 0x3ff;
242 /* drivers must not request bad settings, since lower levels
243 * (hardware or its drivers) may not check. some endpoints
244 * can't do iso, many have maxpacket limitations, etc.
246 * since this "hardware" driver is here to help debugging, we
247 * have some extra sanity checks. (there could be more though,
248 * especially for "ep9out" style fixed function ones.)
251 switch (desc->bmAttributes & 0x03) {
252 case USB_ENDPOINT_XFER_BULK:
253 if (strstr (ep->ep.name, "-iso")
254 || strstr (ep->ep.name, "-int")) {
257 switch (dum->gadget.speed) {
261 /* conserve return statements */
264 case 8: case 16: case 32: case 64:
265 /* we'll fake any legal size */
273 case USB_ENDPOINT_XFER_INT:
274 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
276 /* real hardware might not handle all packet sizes */
277 switch (dum->gadget.speed) {
281 /* save a return statement */
285 /* save a return statement */
292 case USB_ENDPOINT_XFER_ISOC:
293 if (strstr (ep->ep.name, "-bulk")
294 || strstr (ep->ep.name, "-int"))
296 /* real hardware might not handle all packet sizes */
297 switch (dum->gadget.speed) {
301 /* save a return statement */
305 /* save a return statement */
311 /* few chips support control except on ep0 */
315 _ep->maxpacket = max;
318 dev_dbg (dummy_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
320 desc->bEndpointAddress & 0x0f,
321 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
323 switch (desc->bmAttributes & 0x03) {
324 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
325 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
326 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
327 default: val = "ctrl"; break;
331 /* at this point real hardware should be NAKing transfers
332 * to that endpoint, until a buffer is queued to it.
339 /* called with spinlock held */
340 static void nuke (struct dummy *dum, struct dummy_ep *ep)
342 while (!list_empty (&ep->queue)) {
343 struct dummy_request *req;
345 req = list_entry (ep->queue.next, struct dummy_request, queue);
346 list_del_init (&req->queue);
347 req->req.status = -ESHUTDOWN;
349 spin_unlock (&dum->lock);
350 req->req.complete (&ep->ep, &req->req);
351 spin_lock (&dum->lock);
355 static int dummy_disable (struct usb_ep *_ep)
362 ep = usb_ep_to_dummy_ep (_ep);
363 if (!_ep || !ep->desc || _ep->name == ep0name)
365 dum = ep_to_dummy (ep);
367 spin_lock_irqsave (&dum->lock, flags);
371 spin_unlock_irqrestore (&dum->lock, flags);
373 dev_dbg (dummy_dev(dum), "disabled %s\n", _ep->name);
377 static struct usb_request *
378 dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
381 struct dummy_request *req;
385 ep = usb_ep_to_dummy_ep (_ep);
387 req = kmalloc (sizeof *req, mem_flags);
390 memset (req, 0, sizeof *req);
391 INIT_LIST_HEAD (&req->queue);
396 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
399 struct dummy_request *req;
401 ep = usb_ep_to_dummy_ep (_ep);
402 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
405 req = usb_request_to_dummy_request (_req);
406 WARN_ON (!list_empty (&req->queue));
421 ep = usb_ep_to_dummy_ep (_ep);
422 dum = ep_to_dummy (ep);
426 retval = kmalloc (bytes, mem_flags);
427 *dma = (dma_addr_t) retval;
443 fifo_complete (struct usb_ep *ep, struct usb_request *req)
448 dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
451 struct dummy_request *req;
455 req = usb_request_to_dummy_request (_req);
456 if (!_req || !list_empty (&req->queue) || !_req->complete)
459 ep = usb_ep_to_dummy_ep (_ep);
460 if (!_ep || (!ep->desc && _ep->name != ep0name))
463 dum = ep_to_dummy (ep);
464 if (!dum->driver || !is_enabled (dum))
468 dev_dbg (dummy_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
469 ep, _req, _ep->name, _req->length, _req->buf);
472 _req->status = -EINPROGRESS;
474 spin_lock_irqsave (&dum->lock, flags);
476 /* implement an emulated single-request FIFO */
477 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
478 list_empty (&dum->fifo_req.queue) &&
479 list_empty (&ep->queue) &&
480 _req->length <= FIFO_SIZE) {
481 req = &dum->fifo_req;
483 req->req.buf = dum->fifo_buf;
484 memcpy (dum->fifo_buf, _req->buf, _req->length);
485 req->req.context = dum;
486 req->req.complete = fifo_complete;
488 spin_unlock (&dum->lock);
489 _req->actual = _req->length;
491 _req->complete (_ep, _req);
492 spin_lock (&dum->lock);
494 list_add_tail (&req->queue, &ep->queue);
495 spin_unlock_irqrestore (&dum->lock, flags);
497 /* real hardware would likely enable transfers here, in case
498 * it'd been left NAKing.
503 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
507 int retval = -EINVAL;
509 struct dummy_request *req = 0;
513 ep = usb_ep_to_dummy_ep (_ep);
514 dum = ep_to_dummy (ep);
519 spin_lock_irqsave (&dum->lock, flags);
520 list_for_each_entry (req, &ep->queue, queue) {
521 if (&req->req == _req) {
522 list_del_init (&req->queue);
523 _req->status = -ECONNRESET;
528 spin_unlock_irqrestore (&dum->lock, flags);
531 dev_dbg (dummy_dev(dum),
532 "dequeued req %p from %s, len %d buf %p\n",
533 req, _ep->name, _req->length, _req->buf);
534 _req->complete (_ep, _req);
540 dummy_set_halt (struct usb_ep *_ep, int value)
547 ep = usb_ep_to_dummy_ep (_ep);
548 dum = ep_to_dummy (ep);
553 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
554 !list_empty (&ep->queue))
558 /* FIXME clear emulated data toggle too */
562 static const struct usb_ep_ops dummy_ep_ops = {
563 .enable = dummy_enable,
564 .disable = dummy_disable,
566 .alloc_request = dummy_alloc_request,
567 .free_request = dummy_free_request,
569 .alloc_buffer = dummy_alloc_buffer,
570 .free_buffer = dummy_free_buffer,
571 /* map, unmap, ... eventually hook the "generic" dma calls */
573 .queue = dummy_queue,
574 .dequeue = dummy_dequeue,
576 .set_halt = dummy_set_halt,
579 /*-------------------------------------------------------------------------*/
581 /* there are both host and device side versions of this call ... */
582 static int dummy_g_get_frame (struct usb_gadget *_gadget)
586 do_gettimeofday (&tv);
587 return tv.tv_usec / 1000;
590 static int dummy_wakeup (struct usb_gadget *_gadget)
594 dum = gadget_to_dummy (_gadget);
595 if ((dum->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) == 0
596 || !(dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)))
599 /* hub notices our request, issues downstream resume, etc */
601 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
605 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
609 dum = gadget_to_dummy (_gadget);
611 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
613 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
617 static const struct usb_gadget_ops dummy_ops = {
618 .get_frame = dummy_g_get_frame,
619 .wakeup = dummy_wakeup,
620 .set_selfpowered = dummy_set_selfpowered,
623 /*-------------------------------------------------------------------------*/
625 /* "function" sysfs attribute */
627 show_function (struct device *dev, char *buf)
629 struct dummy *dum = gadget_dev_to_dummy (dev);
631 if (!dum->driver || !dum->driver->function)
633 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
635 DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
637 /*-------------------------------------------------------------------------*/
640 * Driver registration/unregistration.
642 * This is basically hardware-specific; there's usually only one real USB
643 * device (not host) controller since that's how USB devices are intended
644 * to work. So most implementations of these api calls will rely on the
645 * fact that only one driver will ever bind to the hardware. But curious
646 * hardware can be built with discrete components, so the gadget API doesn't
647 * require that assumption.
649 * For this emulator, it might be convenient to create a usb slave device
650 * for each driver that registers: just add to a big root hub.
654 dummy_udc_release (struct device *dev)
659 dummy_pdev_release (struct device *dev)
664 dummy_register_udc (struct dummy *dum)
668 strcpy (dum->gadget.dev.bus_id, "udc");
669 dum->gadget.dev.parent = dummy_dev(dum);
670 dum->gadget.dev.release = dummy_udc_release;
672 rc = device_register (&dum->gadget.dev);
674 device_create_file (&dum->gadget.dev, &dev_attr_function);
679 dummy_unregister_udc (struct dummy *dum)
681 device_remove_file (&dum->gadget.dev, &dev_attr_function);
682 device_unregister (&dum->gadget.dev);
686 usb_gadget_register_driver (struct usb_gadget_driver *driver)
688 struct dummy *dum = the_controller;
695 if (!driver->bind || !driver->unbind || !driver->setup
696 || driver->speed == USB_SPEED_UNKNOWN)
700 * SLAVE side init ... the layer above hardware, which
701 * can't enumerate without help from the driver we're binding.
703 dum->gadget.name = gadget_name;
704 dum->gadget.ops = &dummy_ops;
705 dum->gadget.is_dualspeed = 1;
710 INIT_LIST_HEAD (&dum->gadget.ep_list);
711 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
712 struct dummy_ep *ep = &dum->ep [i];
716 ep->ep.name = ep_name [i];
717 ep->ep.ops = &dummy_ep_ops;
718 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
719 ep->halted = ep->already_seen = ep->setup_stage = 0;
720 ep->ep.maxpacket = ~0;
721 ep->last_io = jiffies;
722 ep->gadget = &dum->gadget;
724 INIT_LIST_HEAD (&ep->queue);
727 dum->gadget.ep0 = &dum->ep [0].ep;
728 dum->ep [0].ep.maxpacket = 64;
729 list_del_init (&dum->ep [0].ep.ep_list);
730 INIT_LIST_HEAD(&dum->fifo_req.queue);
732 dum->driver = driver;
733 dum->gadget.dev.driver = &driver->driver;
734 dev_dbg (dummy_dev(dum), "binding gadget driver '%s'\n",
735 driver->driver.name);
736 if ((retval = driver->bind (&dum->gadget)) != 0) {
738 dum->gadget.dev.driver = 0;
742 // FIXME: Check these calls for errors and re-order
743 driver->driver.bus = dum->gadget.dev.parent->bus;
744 driver_register (&driver->driver);
746 device_bind_driver (&dum->gadget.dev);
748 /* khubd will enumerate this in a while */
749 dum->port_status |= USB_PORT_STAT_CONNECTION
750 | (1 << USB_PORT_FEAT_C_CONNECTION);
753 EXPORT_SYMBOL (usb_gadget_register_driver);
755 /* caller must hold lock */
757 stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
761 /* prevent any more requests */
764 /* The timer is left running so that outstanding URBs can fail */
766 /* nuke any pending requests first, so driver i/o is quiesced */
767 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
770 /* driver now does any non-usb quiescing necessary */
772 spin_unlock (&dum->lock);
773 driver->disconnect (&dum->gadget);
774 spin_lock (&dum->lock);
779 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
781 struct dummy *dum = the_controller;
786 if (!driver || driver != dum->driver)
789 dev_dbg (dummy_dev(dum), "unregister gadget driver '%s'\n",
790 driver->driver.name);
792 spin_lock_irqsave (&dum->lock, flags);
793 stop_activity (dum, driver);
794 dum->port_status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE |
795 USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
796 dum->port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
797 spin_unlock_irqrestore (&dum->lock, flags);
799 driver->unbind (&dum->gadget);
802 device_release_driver (&dum->gadget.dev);
804 driver_unregister (&driver->driver);
808 EXPORT_SYMBOL (usb_gadget_unregister_driver);
812 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
816 EXPORT_SYMBOL (net2280_set_fifo_mode);
818 /*-------------------------------------------------------------------------*/
820 /* MASTER/HOST SIDE DRIVER
822 * this uses the hcd framework to hook up to host side drivers.
823 * its root hub will only have one device, otherwise it acts like
824 * a normal host controller.
826 * when urbs are queued, they're just stuck on a list that we
827 * scan in a timer callback. that callback connects writes from
828 * the host with reads from the device, and so on, based on the
832 static int dummy_urb_enqueue (
840 if (!urb->transfer_buffer && urb->transfer_buffer_length)
843 dum = hcd_to_dummy (hcd);
844 spin_lock_irqsave (&dum->lock, flags);
847 dum->udev = urb->dev;
848 usb_get_dev (dum->udev);
849 } else if (unlikely (dum->udev != urb->dev))
850 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
853 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
854 urb->error_count = 1; /* mark as a new urb */
856 /* kick the scheduler, it'll do the rest */
857 if (!timer_pending (&dum->timer))
858 mod_timer (&dum->timer, jiffies + 1);
860 spin_unlock_irqrestore (&dum->lock, flags);
864 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
866 /* giveback happens automatically in timer callback */
870 static void maybe_set_status (struct urb *urb, int status)
872 spin_lock (&urb->lock);
873 if (urb->status == -EINPROGRESS)
874 urb->status = status;
875 spin_unlock (&urb->lock);
878 /* transfer up to a frame's worth; caller must own lock */
880 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
882 struct dummy_request *req;
885 /* if there's no request queued, the device is NAKing; return */
886 list_for_each_entry (req, &ep->queue, queue) {
887 unsigned host_len, dev_len, len;
888 int is_short, to_host;
891 /* 1..N packets of ep->ep.maxpacket each ... the last one
892 * may be short (including zero length).
894 * writer can send a zlp explicitly (length 0) or implicitly
895 * (length mod maxpacket zero, and 'zero' flag); they always
898 host_len = urb->transfer_buffer_length - urb->actual_length;
899 dev_len = req->req.length - req->req.actual;
900 len = min (host_len, dev_len);
902 /* FIXME update emulated data toggle too */
904 to_host = usb_pipein (urb->pipe);
905 if (unlikely (len == 0))
910 /* not enough bandwidth left? */
911 if (limit < ep->ep.maxpacket && limit < len)
913 len = min (len, (unsigned) limit);
917 /* use an extra pass for the final short packet */
918 if (len > ep->ep.maxpacket) {
920 len -= (len % ep->ep.maxpacket);
922 is_short = (len % ep->ep.maxpacket) != 0;
924 /* else transfer packet(s) */
925 ubuf = urb->transfer_buffer + urb->actual_length;
926 rbuf = req->req.buf + req->req.actual;
928 memcpy (ubuf, rbuf, len);
930 memcpy (rbuf, ubuf, len);
931 ep->last_io = jiffies;
934 urb->actual_length += len;
935 req->req.actual += len;
938 /* short packets terminate, maybe with overflow/underflow.
939 * it's only really an error to write too much.
941 * partially filling a buffer optionally blocks queue advances
942 * (so completion handlers can clean up the queue) but we don't
943 * need to emulate such data-in-flight. so we only show part
944 * of the URB_SHORT_NOT_OK effect: completion status.
947 if (host_len == dev_len) {
949 maybe_set_status (urb, 0);
950 } else if (to_host) {
952 if (dev_len > host_len)
953 maybe_set_status (urb, -EOVERFLOW);
955 maybe_set_status (urb,
959 } else if (!to_host) {
960 maybe_set_status (urb, 0);
961 if (host_len > dev_len)
962 req->req.status = -EOVERFLOW;
967 /* many requests terminate without a short packet */
969 if (req->req.length == req->req.actual
972 if (urb->transfer_buffer_length == urb->actual_length
973 && !(urb->transfer_flags
974 & URB_ZERO_PACKET)) {
975 maybe_set_status (urb, 0);
979 /* device side completion --> continuable */
980 if (req->req.status != -EINPROGRESS) {
981 list_del_init (&req->queue);
983 spin_unlock (&dum->lock);
984 req->req.complete (&ep->ep, &req->req);
985 spin_lock (&dum->lock);
987 /* requests might have been unlinked... */
991 /* host side completion --> terminate */
992 if (urb->status != -EINPROGRESS)
995 /* rescan to continue with any other queued i/o */
1002 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1004 int limit = ep->ep.maxpacket;
1006 if (dum->gadget.speed == USB_SPEED_HIGH) {
1009 /* high bandwidth mode */
1010 tmp = ep->desc->wMaxPacketSize;
1011 tmp = le16_to_cpu (tmp);
1012 tmp = (tmp >> 11) & 0x03;
1013 tmp *= 8 /* applies to entire frame */;
1014 limit += limit * tmp;
1019 #define is_active(dum) ((dum->port_status & \
1020 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1021 USB_PORT_STAT_SUSPEND)) \
1022 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1024 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1028 if (!is_active (dum))
1030 if ((address & ~USB_DIR_IN) == 0)
1031 return &dum->ep [0];
1032 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1033 struct dummy_ep *ep = &dum->ep [i];
1037 if (ep->desc->bEndpointAddress == address)
1045 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1046 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1047 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1048 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1049 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1050 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1052 /* drive both sides of the transfers; looks like irq handlers to
1053 * both drivers except the callbacks aren't in_irq().
1055 static void dummy_timer (unsigned long _dum)
1057 struct dummy *dum = (struct dummy *) _dum;
1058 struct hcd_dev *hdev;
1059 struct list_head *entry, *tmp;
1060 unsigned long flags;
1064 /* simplistic model for one frame's bandwidth */
1065 switch (dum->gadget.speed) {
1067 total = 8/*bytes*/ * 12/*packets*/;
1069 case USB_SPEED_FULL:
1070 total = 64/*bytes*/ * 19/*packets*/;
1072 case USB_SPEED_HIGH:
1073 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1076 dev_err (dummy_dev(dum), "bogus device speed\n");
1080 /* FIXME if HZ != 1000 this will probably misbehave ... */
1082 /* look at each urb queued by the host side driver */
1083 spin_lock_irqsave (&dum->lock, flags);
1086 dev_err (dummy_dev(dum),
1087 "timer fired with no URBs pending?\n");
1088 spin_unlock_irqrestore (&dum->lock, flags);
1091 hdev = dum->udev->hcpriv;
1093 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1096 dum->ep [i].already_seen = 0;
1100 list_for_each_safe (entry, tmp, &hdev->urb_list) {
1102 struct dummy_request *req;
1104 struct dummy_ep *ep = 0;
1107 urb = list_entry (entry, struct urb, urb_list);
1108 if (urb->status != -EINPROGRESS) {
1109 /* likely it was just unlinked */
1112 type = usb_pipetype (urb->pipe);
1114 /* used up this frame's non-periodic bandwidth?
1115 * FIXME there's infinite bandwidth for control and
1116 * periodic transfers ... unrealistic.
1118 if (total <= 0 && type == PIPE_BULK)
1121 /* find the gadget's ep for this request (if configured) */
1122 address = usb_pipeendpoint (urb->pipe);
1123 if (usb_pipein (urb->pipe))
1124 address |= USB_DIR_IN;
1125 ep = find_endpoint(dum, address);
1127 /* set_configuration() disagreement */
1128 dev_dbg (dummy_dev(dum),
1129 "no ep configured for urb %p\n",
1131 maybe_set_status (urb, -EPROTO);
1135 if (ep->already_seen)
1137 ep->already_seen = 1;
1138 if (ep == &dum->ep [0] && urb->error_count) {
1139 ep->setup_stage = 1; /* a new urb */
1140 urb->error_count = 0;
1142 if (ep->halted && !ep->setup_stage) {
1143 /* NOTE: must not be iso! */
1144 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1146 maybe_set_status (urb, -EPIPE);
1149 /* FIXME make sure both ends agree on maxpacket */
1151 /* handle control requests */
1152 if (ep == &dum->ep [0] && ep->setup_stage) {
1153 struct usb_ctrlrequest setup;
1155 struct dummy_ep *ep2;
1157 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1158 le16_to_cpus (&setup.wIndex);
1159 le16_to_cpus (&setup.wValue);
1160 le16_to_cpus (&setup.wLength);
1161 if (setup.wLength != urb->transfer_buffer_length) {
1162 maybe_set_status (urb, -EOVERFLOW);
1166 /* paranoia, in case of stale queued data */
1167 list_for_each_entry (req, &ep->queue, queue) {
1168 list_del_init (&req->queue);
1169 req->req.status = -EOVERFLOW;
1170 dev_dbg (dummy_dev(dum), "stale req = %p\n",
1173 spin_unlock (&dum->lock);
1174 req->req.complete (&ep->ep, &req->req);
1175 spin_lock (&dum->lock);
1176 ep->already_seen = 0;
1180 /* gadget driver never sees set_address or operations
1181 * on standard feature flags. some hardware doesn't
1184 ep->last_io = jiffies;
1185 ep->setup_stage = 0;
1187 switch (setup.bRequest) {
1188 case USB_REQ_SET_ADDRESS:
1189 if (setup.bRequestType != Dev_Request)
1191 dum->address = setup.wValue;
1192 maybe_set_status (urb, 0);
1193 dev_dbg (dummy_dev(dum), "set_address = %d\n",
1197 case USB_REQ_SET_FEATURE:
1198 if (setup.bRequestType == Dev_Request) {
1200 switch (setup.wValue) {
1201 case USB_DEVICE_REMOTE_WAKEUP:
1204 value = -EOPNOTSUPP;
1208 (1 << setup.wValue);
1209 maybe_set_status (urb, 0);
1212 } else if (setup.bRequestType == Ep_Request) {
1214 ep2 = find_endpoint (dum,
1217 value = -EOPNOTSUPP;
1222 maybe_set_status (urb, 0);
1225 case USB_REQ_CLEAR_FEATURE:
1226 if (setup.bRequestType == Dev_Request) {
1227 switch (setup.wValue) {
1228 case USB_DEVICE_REMOTE_WAKEUP:
1229 dum->devstatus &= ~(1 <<
1230 USB_DEVICE_REMOTE_WAKEUP);
1232 maybe_set_status (urb, 0);
1235 value = -EOPNOTSUPP;
1238 } else if (setup.bRequestType == Ep_Request) {
1240 ep2 = find_endpoint (dum,
1243 value = -EOPNOTSUPP;
1248 maybe_set_status (urb, 0);
1251 case USB_REQ_GET_STATUS:
1252 if (setup.bRequestType == Dev_InRequest
1253 || setup.bRequestType
1255 || setup.bRequestType
1260 // device: remote wakeup, selfpowered
1261 // interface: nothing
1263 buf = (char *)urb->transfer_buffer;
1264 if (urb->transfer_buffer_length > 0) {
1265 if (setup.bRequestType ==
1267 ep2 = find_endpoint (dum, setup.wIndex);
1269 value = -EOPNOTSUPP;
1272 buf [0] = ep2->halted;
1273 } else if (setup.bRequestType ==
1280 if (urb->transfer_buffer_length > 1)
1282 urb->actual_length = min (2,
1283 urb->transfer_buffer_length);
1285 maybe_set_status (urb, 0);
1290 /* gadget driver handles all other requests. block
1291 * until setup() returns; no reentrancy issues etc.
1294 spin_unlock (&dum->lock);
1295 value = dum->driver->setup (&dum->gadget,
1297 spin_lock (&dum->lock);
1300 /* no delays (max 64KB data stage) */
1302 goto treat_control_like_bulk;
1304 /* error, see below */
1308 if (value != -EOPNOTSUPP)
1309 dev_dbg (dummy_dev(dum),
1312 maybe_set_status (urb, -EPIPE);
1313 urb->actual_length = 0;
1319 /* non-control requests */
1321 switch (usb_pipetype (urb->pipe)) {
1322 case PIPE_ISOCHRONOUS:
1323 /* FIXME is it urb->interval since the last xfer?
1324 * use urb->iso_frame_desc[i].
1325 * complete whether or not ep has requests queued.
1326 * report random errors, to debug drivers.
1328 limit = max (limit, periodic_bytes (dum, ep));
1329 maybe_set_status (urb, -ENOSYS);
1332 case PIPE_INTERRUPT:
1333 /* FIXME is it urb->interval since the last xfer?
1334 * this almost certainly polls too fast.
1336 limit = max (limit, periodic_bytes (dum, ep));
1339 // case PIPE_BULK: case PIPE_CONTROL:
1341 treat_control_like_bulk:
1342 ep->last_io = jiffies;
1343 total = transfer (dum, urb, ep, limit);
1347 /* incomplete transfer? */
1348 if (urb->status == -EINPROGRESS)
1354 ep->already_seen = ep->setup_stage = 0;
1356 spin_unlock (&dum->lock);
1357 usb_hcd_giveback_urb (&dum->hcd, urb, 0);
1358 spin_lock (&dum->lock);
1363 /* want a 1 msec delay here */
1364 if (!list_empty (&hdev->urb_list))
1365 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1367 usb_put_dev (dum->udev);
1371 spin_unlock_irqrestore (&dum->lock, flags);
1374 /*-------------------------------------------------------------------------*/
1376 #define PORT_C_MASK \
1377 ((1 << USB_PORT_FEAT_C_CONNECTION) \
1378 | (1 << USB_PORT_FEAT_C_ENABLE) \
1379 | (1 << USB_PORT_FEAT_C_SUSPEND) \
1380 | (1 << USB_PORT_FEAT_C_OVER_CURRENT) \
1381 | (1 << USB_PORT_FEAT_C_RESET))
1383 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1386 unsigned long flags;
1389 dum = hcd_to_dummy (hcd);
1391 spin_lock_irqsave (&dum->lock, flags);
1392 if (!(dum->port_status & PORT_C_MASK))
1396 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1400 spin_unlock_irqrestore (&dum->lock, flags);
1405 hub_descriptor (struct usb_hub_descriptor *desc)
1407 memset (desc, 0, sizeof *desc);
1408 desc->bDescriptorType = 0x29;
1409 desc->bDescLength = 9;
1410 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1411 desc->bNbrPorts = 1;
1412 desc->bitmap [0] = 0xff;
1413 desc->bitmap [1] = 0xff;
1416 static int dummy_hub_control (
1417 struct usb_hcd *hcd,
1426 unsigned long flags;
1428 dum = hcd_to_dummy (hcd);
1429 spin_lock_irqsave (&dum->lock, flags);
1431 case ClearHubFeature:
1433 case ClearPortFeature:
1435 case USB_PORT_FEAT_SUSPEND:
1436 if (dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)) {
1437 /* 20msec resume signaling */
1439 dum->re_timeout = jiffies +
1440 msecs_to_jiffies(20);
1443 case USB_PORT_FEAT_POWER:
1444 dum->port_status = 0;
1446 stop_activity(dum, dum->driver);
1449 dum->port_status &= ~(1 << wValue);
1452 case GetHubDescriptor:
1453 hub_descriptor ((struct usb_hub_descriptor *) buf);
1456 *(u32 *) buf = __constant_cpu_to_le32 (0);
1462 /* whoever resets or resumes must GetPortStatus to
1465 if (dum->resuming && time_after (jiffies, dum->re_timeout)) {
1466 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
1467 dum->port_status &= ~(1 << USB_PORT_FEAT_SUSPEND);
1469 dum->re_timeout = 0;
1470 if (dum->driver && dum->driver->resume) {
1471 spin_unlock (&dum->lock);
1472 dum->driver->resume (&dum->gadget);
1473 spin_lock (&dum->lock);
1476 if ((dum->port_status & (1 << USB_PORT_FEAT_RESET)) != 0
1477 && time_after (jiffies, dum->re_timeout)) {
1478 dum->port_status |= (1 << USB_PORT_FEAT_C_RESET);
1479 dum->port_status &= ~(1 << USB_PORT_FEAT_RESET);
1480 dum->re_timeout = 0;
1482 dum->port_status |= USB_PORT_STAT_ENABLE;
1483 /* give it the best speed we agree on */
1484 dum->gadget.speed = dum->driver->speed;
1485 dum->gadget.ep0->maxpacket = 64;
1486 switch (dum->gadget.speed) {
1487 case USB_SPEED_HIGH:
1489 USB_PORT_STAT_HIGH_SPEED;
1492 dum->gadget.ep0->maxpacket = 8;
1494 USB_PORT_STAT_LOW_SPEED;
1497 dum->gadget.speed = USB_SPEED_FULL;
1502 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1503 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1508 case SetPortFeature:
1510 case USB_PORT_FEAT_SUSPEND:
1511 if ((dum->port_status & (1 << USB_PORT_FEAT_SUSPEND))
1514 (1 << USB_PORT_FEAT_SUSPEND);
1515 if (dum->driver && dum->driver->suspend) {
1516 spin_unlock (&dum->lock);
1517 dum->driver->suspend (&dum->gadget);
1518 spin_lock (&dum->lock);
1522 case USB_PORT_FEAT_RESET:
1523 /* if it's already running, disconnect first */
1524 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1525 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1526 | USB_PORT_STAT_LOW_SPEED
1527 | USB_PORT_STAT_HIGH_SPEED);
1529 dev_dbg (dummy_dev(dum),
1531 stop_activity (dum, dum->driver);
1534 /* FIXME test that code path! */
1536 /* 50msec reset signaling */
1537 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1540 dum->port_status |= (1 << wValue);
1545 dev_dbg (dummy_dev(dum),
1546 "hub control req%04x v%04x i%04x l%d\n",
1547 typeReq, wValue, wIndex, wLength);
1549 /* "protocol stall" on error */
1552 spin_unlock_irqrestore (&dum->lock, flags);
1557 /*-------------------------------------------------------------------------*/
1559 static struct usb_hcd *dummy_alloc (void)
1563 dum = kmalloc (sizeof *dum, SLAB_KERNEL);
1566 the_controller = dum;
1567 memset (dum, 0, sizeof *dum);
1571 /*-------------------------------------------------------------------------*/
1573 static inline ssize_t
1574 show_urb (char *buf, size_t size, struct urb *urb)
1576 int ep = usb_pipeendpoint (urb->pipe);
1578 return snprintf (buf, size,
1579 "urb/%p %s ep%d%s%s len %d/%d\n",
1582 switch (urb->dev->speed) {
1583 case USB_SPEED_LOW: s = "ls"; break;
1584 case USB_SPEED_FULL: s = "fs"; break;
1585 case USB_SPEED_HIGH: s = "hs"; break;
1586 default: s = "?"; break;
1588 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1590 switch (usb_pipetype (urb->pipe)) { \
1591 case PIPE_CONTROL: s = ""; break; \
1592 case PIPE_BULK: s = "-bulk"; break; \
1593 case PIPE_INTERRUPT: s = "-int"; break; \
1594 default: s = "-iso"; break; \
1596 urb->actual_length, urb->transfer_buffer_length);
1600 show_urbs (struct device *dev, char *buf)
1602 struct usb_hcd *hcd = dev_get_drvdata (dev);
1603 struct dummy *dum = hcd_to_dummy (hcd);
1606 unsigned long flags;
1607 struct hcd_dev *hdev;
1609 spin_lock_irqsave (&dum->lock, flags);
1611 hdev = dum->udev->hcpriv;
1612 list_for_each_entry (urb, &hdev->urb_list, urb_list) {
1615 temp = show_urb (buf, PAGE_SIZE - size, urb);
1620 spin_unlock_irqrestore (&dum->lock, flags);
1624 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1626 static int dummy_start (struct usb_hcd *hcd)
1629 struct usb_device *root;
1632 dum = hcd_to_dummy (hcd);
1635 * MASTER side init ... we emulate a root hub that'll only ever
1636 * talk to one device (the slave side). Also appears in sysfs,
1637 * just like more familiar pci-based HCDs.
1639 spin_lock_init (&dum->lock);
1641 init_timer (&dum->timer);
1642 dum->timer.function = dummy_timer;
1643 dum->timer.data = (unsigned long) dum;
1645 root = usb_alloc_dev (0, &hcd->self, 0);
1649 /* root hub enters addressed state... */
1650 hcd->state = USB_STATE_RUNNING;
1651 root->speed = USB_SPEED_HIGH;
1653 /* ...then configured, so khubd sees us. */
1654 if ((retval = hcd_register_root (root, hcd)) != 0) {
1657 hcd->state = USB_STATE_QUIESCING;
1661 /* only show a low-power port: just 8mA */
1662 hub_set_power_budget (root, 8);
1664 if ((retval = dummy_register_udc (dum)) != 0) {
1665 usb_disconnect (&hcd->self.root_hub);
1669 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1670 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1676 static void dummy_stop (struct usb_hcd *hcd)
1680 dum = hcd_to_dummy (hcd);
1685 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1687 usb_gadget_unregister_driver (dum->driver);
1688 dummy_unregister_udc (dum);
1690 dev_info (dummy_dev(dum), "stopped\n");
1693 /*-------------------------------------------------------------------------*/
1695 static int dummy_h_get_frame (struct usb_hcd *hcd)
1697 return dummy_g_get_frame (0);
1700 static const struct hc_driver dummy_hcd = {
1701 .description = (char *) driver_name,
1704 .start = dummy_start,
1707 .hcd_alloc = dummy_alloc,
1709 .urb_enqueue = dummy_urb_enqueue,
1710 .urb_dequeue = dummy_urb_dequeue,
1712 .get_frame_number = dummy_h_get_frame,
1714 .hub_status_data = dummy_hub_status,
1715 .hub_control = dummy_hub_control,
1718 static void dummy_remove (struct device *dev);
1720 static int dummy_probe (struct device *dev)
1722 struct usb_hcd *hcd;
1726 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1728 hcd = dummy_alloc ();
1730 dev_dbg (dev, "hcd_alloc failed\n");
1734 dev_set_drvdata (dev, hcd);
1735 dum = hcd_to_dummy (hcd);
1737 hcd->driver = (struct hc_driver *) &dummy_hcd;
1738 hcd->description = dummy_hcd.description;
1739 hcd->self.controller = dev;
1741 /* FIXME don't require the pci-based buffer/alloc impls;
1742 * the "generic dma" implementation still requires them,
1743 * it's not very generic yet.
1745 retval = hcd_buffer_create (hcd);
1747 dev_dbg (dev, "pool alloc failed\n");
1751 usb_bus_init (&hcd->self);
1752 hcd->self.op = &usb_hcd_operations;
1753 hcd->self.release = &usb_hcd_release;
1754 hcd->self.hcpriv = hcd;
1755 hcd->self.bus_name = dev->bus_id;
1756 hcd->product_desc = "Dummy host controller";
1758 INIT_LIST_HEAD (&hcd->dev_list);
1760 usb_register_bus (&hcd->self);
1762 if ((retval = dummy_start (hcd)) < 0)
1768 dev_set_drvdata (dev, NULL);
1772 static void dummy_remove (struct device *dev)
1774 struct usb_hcd *hcd;
1777 hcd = dev_get_drvdata (dev);
1778 dum = hcd_to_dummy (hcd);
1780 hcd->state = USB_STATE_QUIESCING;
1782 dev_dbg (dev, "roothub graceful disconnect\n");
1783 usb_disconnect (&hcd->self.root_hub);
1785 hcd->driver->stop (hcd);
1786 hcd->state = USB_STATE_HALT;
1788 hcd_buffer_destroy (hcd);
1790 dev_set_drvdata (dev, NULL);
1791 usb_deregister_bus (&hcd->self);
1792 the_controller = NULL;
1795 /*-------------------------------------------------------------------------*/
1797 static int dummy_pdev_detect (void)
1801 retval = driver_register (&dummy_driver);
1805 the_pdev.name = "hc";
1806 the_pdev.dev.driver = &dummy_driver;
1807 the_pdev.dev.release = dummy_pdev_release;
1809 retval = platform_device_register (&the_pdev);
1811 driver_unregister (&dummy_driver);
1815 static void dummy_pdev_remove (void)
1817 platform_device_unregister (&the_pdev);
1818 driver_unregister (&dummy_driver);
1821 /*-------------------------------------------------------------------------*/
1823 static int __init init (void)
1827 if (usb_disabled ())
1829 if ((retval = dummy_pdev_detect ()) != 0)
1831 if ((retval = dummy_probe (&the_pdev.dev)) != 0)
1832 dummy_pdev_remove ();
1837 static void __exit cleanup (void)
1839 dummy_remove (&the_pdev.dev);
1840 dummy_pdev_remove ();
1842 module_exit (cleanup);