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/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 "17 Dec 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 *))
148 struct list_head urbp_list;
155 * SLAVE/GADGET side support
157 struct dummy_ep ep [DUMMY_ENDPOINTS];
159 struct usb_gadget gadget;
160 struct usb_gadget_driver *driver;
161 struct dummy_request fifo_req;
162 u8 fifo_buf [FIFO_SIZE];
166 * MASTER/HOST side support
168 struct timer_list timer;
171 unsigned long re_timeout;
173 struct usb_device *udev;
174 struct list_head urbp_list;
177 static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
179 return (struct dummy *) (hcd->hcd_priv);
182 static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
184 return container_of((void *) dum, struct usb_hcd, hcd_priv);
187 static inline struct device *dummy_dev (struct dummy *dum)
189 return dummy_to_hcd(dum)->self.controller;
192 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
194 return container_of (ep->gadget, struct dummy, gadget);
197 static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
199 return container_of (gadget, struct dummy, gadget);
202 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
204 return container_of (dev, struct dummy, gadget.dev);
207 static struct dummy *the_controller;
209 /*-------------------------------------------------------------------------*/
212 * This "hardware" may look a bit odd in diagnostics since it's got both
213 * host and device sides; and it binds different drivers to each side.
215 static struct platform_device the_pdev;
217 static struct device_driver dummy_driver = {
218 .name = (char *) driver_name,
219 .bus = &platform_bus_type,
222 /*-------------------------------------------------------------------------*/
224 /* SLAVE/GADGET SIDE DRIVER
226 * This only tracks gadget state. All the work is done when the host
227 * side tries some (emulated) i/o operation. Real device controller
228 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
231 #define is_enabled(dum) \
232 (dum->port_status & USB_PORT_STAT_ENABLE)
235 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
242 ep = usb_ep_to_dummy_ep (_ep);
243 if (!_ep || !desc || ep->desc || _ep->name == ep0name
244 || desc->bDescriptorType != USB_DT_ENDPOINT)
246 dum = ep_to_dummy (ep);
247 if (!dum->driver || !is_enabled (dum))
249 max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
251 /* drivers must not request bad settings, since lower levels
252 * (hardware or its drivers) may not check. some endpoints
253 * can't do iso, many have maxpacket limitations, etc.
255 * since this "hardware" driver is here to help debugging, we
256 * have some extra sanity checks. (there could be more though,
257 * especially for "ep9out" style fixed function ones.)
260 switch (desc->bmAttributes & 0x03) {
261 case USB_ENDPOINT_XFER_BULK:
262 if (strstr (ep->ep.name, "-iso")
263 || strstr (ep->ep.name, "-int")) {
266 switch (dum->gadget.speed) {
270 /* conserve return statements */
273 case 8: case 16: case 32: case 64:
274 /* we'll fake any legal size */
282 case USB_ENDPOINT_XFER_INT:
283 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
285 /* real hardware might not handle all packet sizes */
286 switch (dum->gadget.speed) {
290 /* save a return statement */
294 /* save a return statement */
301 case USB_ENDPOINT_XFER_ISOC:
302 if (strstr (ep->ep.name, "-bulk")
303 || strstr (ep->ep.name, "-int"))
305 /* real hardware might not handle all packet sizes */
306 switch (dum->gadget.speed) {
310 /* save a return statement */
314 /* save a return statement */
320 /* few chips support control except on ep0 */
324 _ep->maxpacket = max;
327 dev_dbg (dummy_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
329 desc->bEndpointAddress & 0x0f,
330 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
332 switch (desc->bmAttributes & 0x03) {
333 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
334 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
335 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
336 default: val = "ctrl"; break;
340 /* at this point real hardware should be NAKing transfers
341 * to that endpoint, until a buffer is queued to it.
348 /* called with spinlock held */
349 static void nuke (struct dummy *dum, struct dummy_ep *ep)
351 while (!list_empty (&ep->queue)) {
352 struct dummy_request *req;
354 req = list_entry (ep->queue.next, struct dummy_request, queue);
355 list_del_init (&req->queue);
356 req->req.status = -ESHUTDOWN;
358 spin_unlock (&dum->lock);
359 req->req.complete (&ep->ep, &req->req);
360 spin_lock (&dum->lock);
364 static int dummy_disable (struct usb_ep *_ep)
371 ep = usb_ep_to_dummy_ep (_ep);
372 if (!_ep || !ep->desc || _ep->name == ep0name)
374 dum = ep_to_dummy (ep);
376 spin_lock_irqsave (&dum->lock, flags);
380 spin_unlock_irqrestore (&dum->lock, flags);
382 dev_dbg (dummy_dev(dum), "disabled %s\n", _ep->name);
386 static struct usb_request *
387 dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
390 struct dummy_request *req;
394 ep = usb_ep_to_dummy_ep (_ep);
396 req = kmalloc (sizeof *req, mem_flags);
399 memset (req, 0, sizeof *req);
400 INIT_LIST_HEAD (&req->queue);
405 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
408 struct dummy_request *req;
410 ep = usb_ep_to_dummy_ep (_ep);
411 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
414 req = usb_request_to_dummy_request (_req);
415 WARN_ON (!list_empty (&req->queue));
430 ep = usb_ep_to_dummy_ep (_ep);
431 dum = ep_to_dummy (ep);
435 retval = kmalloc (bytes, mem_flags);
436 *dma = (dma_addr_t) retval;
452 fifo_complete (struct usb_ep *ep, struct usb_request *req)
457 dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
460 struct dummy_request *req;
464 req = usb_request_to_dummy_request (_req);
465 if (!_req || !list_empty (&req->queue) || !_req->complete)
468 ep = usb_ep_to_dummy_ep (_ep);
469 if (!_ep || (!ep->desc && _ep->name != ep0name))
472 dum = ep_to_dummy (ep);
473 if (!dum->driver || !is_enabled (dum))
477 dev_dbg (dummy_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
478 ep, _req, _ep->name, _req->length, _req->buf);
481 _req->status = -EINPROGRESS;
483 spin_lock_irqsave (&dum->lock, flags);
485 /* implement an emulated single-request FIFO */
486 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
487 list_empty (&dum->fifo_req.queue) &&
488 list_empty (&ep->queue) &&
489 _req->length <= FIFO_SIZE) {
490 req = &dum->fifo_req;
492 req->req.buf = dum->fifo_buf;
493 memcpy (dum->fifo_buf, _req->buf, _req->length);
494 req->req.context = dum;
495 req->req.complete = fifo_complete;
497 spin_unlock (&dum->lock);
498 _req->actual = _req->length;
500 _req->complete (_ep, _req);
501 spin_lock (&dum->lock);
503 list_add_tail (&req->queue, &ep->queue);
504 spin_unlock_irqrestore (&dum->lock, flags);
506 /* real hardware would likely enable transfers here, in case
507 * it'd been left NAKing.
512 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
516 int retval = -EINVAL;
518 struct dummy_request *req = NULL;
522 ep = usb_ep_to_dummy_ep (_ep);
523 dum = ep_to_dummy (ep);
528 spin_lock_irqsave (&dum->lock, flags);
529 list_for_each_entry (req, &ep->queue, queue) {
530 if (&req->req == _req) {
531 list_del_init (&req->queue);
532 _req->status = -ECONNRESET;
537 spin_unlock_irqrestore (&dum->lock, flags);
540 dev_dbg (dummy_dev(dum),
541 "dequeued req %p from %s, len %d buf %p\n",
542 req, _ep->name, _req->length, _req->buf);
543 _req->complete (_ep, _req);
549 dummy_set_halt (struct usb_ep *_ep, int value)
556 ep = usb_ep_to_dummy_ep (_ep);
557 dum = ep_to_dummy (ep);
562 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
563 !list_empty (&ep->queue))
567 /* FIXME clear emulated data toggle too */
571 static const struct usb_ep_ops dummy_ep_ops = {
572 .enable = dummy_enable,
573 .disable = dummy_disable,
575 .alloc_request = dummy_alloc_request,
576 .free_request = dummy_free_request,
578 .alloc_buffer = dummy_alloc_buffer,
579 .free_buffer = dummy_free_buffer,
580 /* map, unmap, ... eventually hook the "generic" dma calls */
582 .queue = dummy_queue,
583 .dequeue = dummy_dequeue,
585 .set_halt = dummy_set_halt,
588 /*-------------------------------------------------------------------------*/
590 /* there are both host and device side versions of this call ... */
591 static int dummy_g_get_frame (struct usb_gadget *_gadget)
595 do_gettimeofday (&tv);
596 return tv.tv_usec / 1000;
599 static int dummy_wakeup (struct usb_gadget *_gadget)
603 dum = gadget_to_dummy (_gadget);
604 if ((dum->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) == 0
605 || !(dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)))
608 /* hub notices our request, issues downstream resume, etc */
610 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
614 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
618 dum = gadget_to_dummy (_gadget);
620 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
622 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
626 static const struct usb_gadget_ops dummy_ops = {
627 .get_frame = dummy_g_get_frame,
628 .wakeup = dummy_wakeup,
629 .set_selfpowered = dummy_set_selfpowered,
632 /*-------------------------------------------------------------------------*/
634 /* "function" sysfs attribute */
636 show_function (struct device *dev, struct device_attribute *attr, char *buf)
638 struct dummy *dum = gadget_dev_to_dummy (dev);
640 if (!dum->driver || !dum->driver->function)
642 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
644 DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
646 /*-------------------------------------------------------------------------*/
649 * Driver registration/unregistration.
651 * This is basically hardware-specific; there's usually only one real USB
652 * device (not host) controller since that's how USB devices are intended
653 * to work. So most implementations of these api calls will rely on the
654 * fact that only one driver will ever bind to the hardware. But curious
655 * hardware can be built with discrete components, so the gadget API doesn't
656 * require that assumption.
658 * For this emulator, it might be convenient to create a usb slave device
659 * for each driver that registers: just add to a big root hub.
663 dummy_udc_release (struct device *dev)
668 dummy_pdev_release (struct device *dev)
673 dummy_register_udc (struct dummy *dum)
677 strcpy (dum->gadget.dev.bus_id, "udc");
678 dum->gadget.dev.parent = dummy_dev(dum);
679 dum->gadget.dev.release = dummy_udc_release;
681 rc = device_register (&dum->gadget.dev);
683 device_create_file (&dum->gadget.dev, &dev_attr_function);
688 dummy_unregister_udc (struct dummy *dum)
690 device_remove_file (&dum->gadget.dev, &dev_attr_function);
691 device_unregister (&dum->gadget.dev);
695 usb_gadget_register_driver (struct usb_gadget_driver *driver)
697 struct dummy *dum = the_controller;
704 if (!driver->bind || !driver->unbind || !driver->setup
705 || driver->speed == USB_SPEED_UNKNOWN)
709 * SLAVE side init ... the layer above hardware, which
710 * can't enumerate without help from the driver we're binding.
712 dum->gadget.name = gadget_name;
713 dum->gadget.ops = &dummy_ops;
714 dum->gadget.is_dualspeed = 1;
719 INIT_LIST_HEAD (&dum->gadget.ep_list);
720 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
721 struct dummy_ep *ep = &dum->ep [i];
725 ep->ep.name = ep_name [i];
726 ep->ep.ops = &dummy_ep_ops;
727 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
728 ep->halted = ep->already_seen = ep->setup_stage = 0;
729 ep->ep.maxpacket = ~0;
730 ep->last_io = jiffies;
731 ep->gadget = &dum->gadget;
733 INIT_LIST_HEAD (&ep->queue);
736 dum->gadget.ep0 = &dum->ep [0].ep;
737 dum->ep [0].ep.maxpacket = 64;
738 list_del_init (&dum->ep [0].ep.ep_list);
739 INIT_LIST_HEAD(&dum->fifo_req.queue);
741 dum->driver = driver;
742 dum->gadget.dev.driver = &driver->driver;
743 dev_dbg (dummy_dev(dum), "binding gadget driver '%s'\n",
744 driver->driver.name);
745 if ((retval = driver->bind (&dum->gadget)) != 0) {
747 dum->gadget.dev.driver = NULL;
751 // FIXME: Check these calls for errors and re-order
752 driver->driver.bus = dum->gadget.dev.parent->bus;
753 driver_register (&driver->driver);
755 device_bind_driver (&dum->gadget.dev);
757 /* khubd will enumerate this in a while */
758 dum->port_status |= USB_PORT_STAT_CONNECTION
759 | (1 << USB_PORT_FEAT_C_CONNECTION);
762 EXPORT_SYMBOL (usb_gadget_register_driver);
764 /* caller must hold lock */
766 stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
770 /* prevent any more requests */
773 /* The timer is left running so that outstanding URBs can fail */
775 /* nuke any pending requests first, so driver i/o is quiesced */
776 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
779 /* driver now does any non-usb quiescing necessary */
781 spin_unlock (&dum->lock);
782 driver->disconnect (&dum->gadget);
783 spin_lock (&dum->lock);
788 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
790 struct dummy *dum = the_controller;
795 if (!driver || driver != dum->driver)
798 dev_dbg (dummy_dev(dum), "unregister gadget driver '%s'\n",
799 driver->driver.name);
801 spin_lock_irqsave (&dum->lock, flags);
802 stop_activity (dum, driver);
803 dum->port_status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE |
804 USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
805 dum->port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
806 spin_unlock_irqrestore (&dum->lock, flags);
808 driver->unbind (&dum->gadget);
811 device_release_driver (&dum->gadget.dev);
813 driver_unregister (&driver->driver);
817 EXPORT_SYMBOL (usb_gadget_unregister_driver);
821 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
825 EXPORT_SYMBOL (net2280_set_fifo_mode);
827 /*-------------------------------------------------------------------------*/
829 /* MASTER/HOST SIDE DRIVER
831 * this uses the hcd framework to hook up to host side drivers.
832 * its root hub will only have one device, otherwise it acts like
833 * a normal host controller.
835 * when urbs are queued, they're just stuck on a list that we
836 * scan in a timer callback. that callback connects writes from
837 * the host with reads from the device, and so on, based on the
841 static int dummy_urb_enqueue (
843 struct usb_host_endpoint *ep,
851 if (!urb->transfer_buffer && urb->transfer_buffer_length)
854 urbp = kmalloc (sizeof *urbp, mem_flags);
859 dum = hcd_to_dummy (hcd);
860 spin_lock_irqsave (&dum->lock, flags);
863 dum->udev = urb->dev;
864 usb_get_dev (dum->udev);
865 } else if (unlikely (dum->udev != urb->dev))
866 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
868 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
870 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
871 urb->error_count = 1; /* mark as a new urb */
873 /* kick the scheduler, it'll do the rest */
874 if (!timer_pending (&dum->timer))
875 mod_timer (&dum->timer, jiffies + 1);
877 spin_unlock_irqrestore (&dum->lock, flags);
881 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
883 /* giveback happens automatically in timer callback */
887 static void maybe_set_status (struct urb *urb, int status)
889 spin_lock (&urb->lock);
890 if (urb->status == -EINPROGRESS)
891 urb->status = status;
892 spin_unlock (&urb->lock);
895 /* transfer up to a frame's worth; caller must own lock */
897 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
899 struct dummy_request *req;
902 /* if there's no request queued, the device is NAKing; return */
903 list_for_each_entry (req, &ep->queue, queue) {
904 unsigned host_len, dev_len, len;
905 int is_short, to_host;
908 /* 1..N packets of ep->ep.maxpacket each ... the last one
909 * may be short (including zero length).
911 * writer can send a zlp explicitly (length 0) or implicitly
912 * (length mod maxpacket zero, and 'zero' flag); they always
915 host_len = urb->transfer_buffer_length - urb->actual_length;
916 dev_len = req->req.length - req->req.actual;
917 len = min (host_len, dev_len);
919 /* FIXME update emulated data toggle too */
921 to_host = usb_pipein (urb->pipe);
922 if (unlikely (len == 0))
927 /* not enough bandwidth left? */
928 if (limit < ep->ep.maxpacket && limit < len)
930 len = min (len, (unsigned) limit);
934 /* use an extra pass for the final short packet */
935 if (len > ep->ep.maxpacket) {
937 len -= (len % ep->ep.maxpacket);
939 is_short = (len % ep->ep.maxpacket) != 0;
941 /* else transfer packet(s) */
942 ubuf = urb->transfer_buffer + urb->actual_length;
943 rbuf = req->req.buf + req->req.actual;
945 memcpy (ubuf, rbuf, len);
947 memcpy (rbuf, ubuf, len);
948 ep->last_io = jiffies;
951 urb->actual_length += len;
952 req->req.actual += len;
955 /* short packets terminate, maybe with overflow/underflow.
956 * it's only really an error to write too much.
958 * partially filling a buffer optionally blocks queue advances
959 * (so completion handlers can clean up the queue) but we don't
960 * need to emulate such data-in-flight. so we only show part
961 * of the URB_SHORT_NOT_OK effect: completion status.
964 if (host_len == dev_len) {
966 maybe_set_status (urb, 0);
967 } else if (to_host) {
969 if (dev_len > host_len)
970 maybe_set_status (urb, -EOVERFLOW);
972 maybe_set_status (urb,
976 } else if (!to_host) {
977 maybe_set_status (urb, 0);
978 if (host_len > dev_len)
979 req->req.status = -EOVERFLOW;
984 /* many requests terminate without a short packet */
986 if (req->req.length == req->req.actual
989 if (urb->transfer_buffer_length == urb->actual_length
990 && !(urb->transfer_flags
991 & URB_ZERO_PACKET)) {
992 maybe_set_status (urb, 0);
996 /* device side completion --> continuable */
997 if (req->req.status != -EINPROGRESS) {
998 list_del_init (&req->queue);
1000 spin_unlock (&dum->lock);
1001 req->req.complete (&ep->ep, &req->req);
1002 spin_lock (&dum->lock);
1004 /* requests might have been unlinked... */
1008 /* host side completion --> terminate */
1009 if (urb->status != -EINPROGRESS)
1012 /* rescan to continue with any other queued i/o */
1019 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1021 int limit = ep->ep.maxpacket;
1023 if (dum->gadget.speed == USB_SPEED_HIGH) {
1026 /* high bandwidth mode */
1027 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1028 tmp = le16_to_cpu (tmp);
1029 tmp = (tmp >> 11) & 0x03;
1030 tmp *= 8 /* applies to entire frame */;
1031 limit += limit * tmp;
1036 #define is_active(dum) ((dum->port_status & \
1037 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1038 USB_PORT_STAT_SUSPEND)) \
1039 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1041 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1045 if (!is_active (dum))
1047 if ((address & ~USB_DIR_IN) == 0)
1048 return &dum->ep [0];
1049 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1050 struct dummy_ep *ep = &dum->ep [i];
1054 if (ep->desc->bEndpointAddress == address)
1062 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1063 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1064 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1065 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1066 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1067 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1069 /* drive both sides of the transfers; looks like irq handlers to
1070 * both drivers except the callbacks aren't in_irq().
1072 static void dummy_timer (unsigned long _dum)
1074 struct dummy *dum = (struct dummy *) _dum;
1075 struct urbp *urbp, *tmp;
1076 unsigned long flags;
1080 /* simplistic model for one frame's bandwidth */
1081 switch (dum->gadget.speed) {
1083 total = 8/*bytes*/ * 12/*packets*/;
1085 case USB_SPEED_FULL:
1086 total = 64/*bytes*/ * 19/*packets*/;
1088 case USB_SPEED_HIGH:
1089 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1092 dev_err (dummy_dev(dum), "bogus device speed\n");
1096 /* FIXME if HZ != 1000 this will probably misbehave ... */
1098 /* look at each urb queued by the host side driver */
1099 spin_lock_irqsave (&dum->lock, flags);
1102 dev_err (dummy_dev(dum),
1103 "timer fired with no URBs pending?\n");
1104 spin_unlock_irqrestore (&dum->lock, flags);
1108 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1111 dum->ep [i].already_seen = 0;
1115 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1117 struct dummy_request *req;
1119 struct dummy_ep *ep = NULL;
1123 if (urb->status != -EINPROGRESS) {
1124 /* likely it was just unlinked */
1127 type = usb_pipetype (urb->pipe);
1129 /* used up this frame's non-periodic bandwidth?
1130 * FIXME there's infinite bandwidth for control and
1131 * periodic transfers ... unrealistic.
1133 if (total <= 0 && type == PIPE_BULK)
1136 /* find the gadget's ep for this request (if configured) */
1137 address = usb_pipeendpoint (urb->pipe);
1138 if (usb_pipein (urb->pipe))
1139 address |= USB_DIR_IN;
1140 ep = find_endpoint(dum, address);
1142 /* set_configuration() disagreement */
1143 dev_dbg (dummy_dev(dum),
1144 "no ep configured for urb %p\n",
1146 maybe_set_status (urb, -EPROTO);
1150 if (ep->already_seen)
1152 ep->already_seen = 1;
1153 if (ep == &dum->ep [0] && urb->error_count) {
1154 ep->setup_stage = 1; /* a new urb */
1155 urb->error_count = 0;
1157 if (ep->halted && !ep->setup_stage) {
1158 /* NOTE: must not be iso! */
1159 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1161 maybe_set_status (urb, -EPIPE);
1164 /* FIXME make sure both ends agree on maxpacket */
1166 /* handle control requests */
1167 if (ep == &dum->ep [0] && ep->setup_stage) {
1168 struct usb_ctrlrequest setup;
1170 struct dummy_ep *ep2;
1172 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1173 le16_to_cpus (&setup.wIndex);
1174 le16_to_cpus (&setup.wValue);
1175 le16_to_cpus (&setup.wLength);
1176 if (setup.wLength != urb->transfer_buffer_length) {
1177 maybe_set_status (urb, -EOVERFLOW);
1181 /* paranoia, in case of stale queued data */
1182 list_for_each_entry (req, &ep->queue, queue) {
1183 list_del_init (&req->queue);
1184 req->req.status = -EOVERFLOW;
1185 dev_dbg (dummy_dev(dum), "stale req = %p\n",
1188 spin_unlock (&dum->lock);
1189 req->req.complete (&ep->ep, &req->req);
1190 spin_lock (&dum->lock);
1191 ep->already_seen = 0;
1195 /* gadget driver never sees set_address or operations
1196 * on standard feature flags. some hardware doesn't
1199 ep->last_io = jiffies;
1200 ep->setup_stage = 0;
1202 switch (setup.bRequest) {
1203 case USB_REQ_SET_ADDRESS:
1204 if (setup.bRequestType != Dev_Request)
1206 dum->address = setup.wValue;
1207 maybe_set_status (urb, 0);
1208 dev_dbg (dummy_dev(dum), "set_address = %d\n",
1212 case USB_REQ_SET_FEATURE:
1213 if (setup.bRequestType == Dev_Request) {
1215 switch (setup.wValue) {
1216 case USB_DEVICE_REMOTE_WAKEUP:
1219 value = -EOPNOTSUPP;
1223 (1 << setup.wValue);
1224 maybe_set_status (urb, 0);
1227 } else if (setup.bRequestType == Ep_Request) {
1229 ep2 = find_endpoint (dum,
1232 value = -EOPNOTSUPP;
1237 maybe_set_status (urb, 0);
1240 case USB_REQ_CLEAR_FEATURE:
1241 if (setup.bRequestType == Dev_Request) {
1242 switch (setup.wValue) {
1243 case USB_DEVICE_REMOTE_WAKEUP:
1244 dum->devstatus &= ~(1 <<
1245 USB_DEVICE_REMOTE_WAKEUP);
1247 maybe_set_status (urb, 0);
1250 value = -EOPNOTSUPP;
1253 } else if (setup.bRequestType == Ep_Request) {
1255 ep2 = find_endpoint (dum,
1258 value = -EOPNOTSUPP;
1263 maybe_set_status (urb, 0);
1266 case USB_REQ_GET_STATUS:
1267 if (setup.bRequestType == Dev_InRequest
1268 || setup.bRequestType
1270 || setup.bRequestType
1275 // device: remote wakeup, selfpowered
1276 // interface: nothing
1278 buf = (char *)urb->transfer_buffer;
1279 if (urb->transfer_buffer_length > 0) {
1280 if (setup.bRequestType ==
1282 ep2 = find_endpoint (dum, setup.wIndex);
1284 value = -EOPNOTSUPP;
1287 buf [0] = ep2->halted;
1288 } else if (setup.bRequestType ==
1295 if (urb->transfer_buffer_length > 1)
1297 urb->actual_length = min (2,
1298 urb->transfer_buffer_length);
1300 maybe_set_status (urb, 0);
1305 /* gadget driver handles all other requests. block
1306 * until setup() returns; no reentrancy issues etc.
1309 spin_unlock (&dum->lock);
1310 value = dum->driver->setup (&dum->gadget,
1312 spin_lock (&dum->lock);
1315 /* no delays (max 64KB data stage) */
1317 goto treat_control_like_bulk;
1319 /* error, see below */
1323 if (value != -EOPNOTSUPP)
1324 dev_dbg (dummy_dev(dum),
1327 maybe_set_status (urb, -EPIPE);
1328 urb->actual_length = 0;
1334 /* non-control requests */
1336 switch (usb_pipetype (urb->pipe)) {
1337 case PIPE_ISOCHRONOUS:
1338 /* FIXME is it urb->interval since the last xfer?
1339 * use urb->iso_frame_desc[i].
1340 * complete whether or not ep has requests queued.
1341 * report random errors, to debug drivers.
1343 limit = max (limit, periodic_bytes (dum, ep));
1344 maybe_set_status (urb, -ENOSYS);
1347 case PIPE_INTERRUPT:
1348 /* FIXME is it urb->interval since the last xfer?
1349 * this almost certainly polls too fast.
1351 limit = max (limit, periodic_bytes (dum, ep));
1354 // case PIPE_BULK: case PIPE_CONTROL:
1356 treat_control_like_bulk:
1357 ep->last_io = jiffies;
1358 total = transfer (dum, urb, ep, limit);
1362 /* incomplete transfer? */
1363 if (urb->status == -EINPROGRESS)
1368 list_del (&urbp->urbp_list);
1371 ep->already_seen = ep->setup_stage = 0;
1373 spin_unlock (&dum->lock);
1374 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1375 spin_lock (&dum->lock);
1380 /* want a 1 msec delay here */
1381 if (!list_empty (&dum->urbp_list))
1382 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1384 usb_put_dev (dum->udev);
1388 spin_unlock_irqrestore (&dum->lock, flags);
1391 /*-------------------------------------------------------------------------*/
1393 #define PORT_C_MASK \
1394 ((1 << USB_PORT_FEAT_C_CONNECTION) \
1395 | (1 << USB_PORT_FEAT_C_ENABLE) \
1396 | (1 << USB_PORT_FEAT_C_SUSPEND) \
1397 | (1 << USB_PORT_FEAT_C_OVER_CURRENT) \
1398 | (1 << USB_PORT_FEAT_C_RESET))
1400 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1403 unsigned long flags;
1406 dum = hcd_to_dummy (hcd);
1408 spin_lock_irqsave (&dum->lock, flags);
1409 if (!(dum->port_status & PORT_C_MASK))
1413 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1417 spin_unlock_irqrestore (&dum->lock, flags);
1422 hub_descriptor (struct usb_hub_descriptor *desc)
1424 memset (desc, 0, sizeof *desc);
1425 desc->bDescriptorType = 0x29;
1426 desc->bDescLength = 9;
1427 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1428 desc->bNbrPorts = 1;
1429 desc->bitmap [0] = 0xff;
1430 desc->bitmap [1] = 0xff;
1433 static int dummy_hub_control (
1434 struct usb_hcd *hcd,
1443 unsigned long flags;
1445 dum = hcd_to_dummy (hcd);
1446 spin_lock_irqsave (&dum->lock, flags);
1448 case ClearHubFeature:
1450 case ClearPortFeature:
1452 case USB_PORT_FEAT_SUSPEND:
1453 if (dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)) {
1454 /* 20msec resume signaling */
1456 dum->re_timeout = jiffies +
1457 msecs_to_jiffies(20);
1460 case USB_PORT_FEAT_POWER:
1461 dum->port_status = 0;
1463 stop_activity(dum, dum->driver);
1466 dum->port_status &= ~(1 << wValue);
1469 case GetHubDescriptor:
1470 hub_descriptor ((struct usb_hub_descriptor *) buf);
1473 *(u32 *) buf = __constant_cpu_to_le32 (0);
1479 /* whoever resets or resumes must GetPortStatus to
1482 if (dum->resuming && time_after (jiffies, dum->re_timeout)) {
1483 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
1484 dum->port_status &= ~(1 << USB_PORT_FEAT_SUSPEND);
1486 dum->re_timeout = 0;
1487 if (dum->driver && dum->driver->resume) {
1488 spin_unlock (&dum->lock);
1489 dum->driver->resume (&dum->gadget);
1490 spin_lock (&dum->lock);
1493 if ((dum->port_status & (1 << USB_PORT_FEAT_RESET)) != 0
1494 && time_after (jiffies, dum->re_timeout)) {
1495 dum->port_status |= (1 << USB_PORT_FEAT_C_RESET);
1496 dum->port_status &= ~(1 << USB_PORT_FEAT_RESET);
1497 dum->re_timeout = 0;
1499 dum->port_status |= USB_PORT_STAT_ENABLE;
1500 /* give it the best speed we agree on */
1501 dum->gadget.speed = dum->driver->speed;
1502 dum->gadget.ep0->maxpacket = 64;
1503 switch (dum->gadget.speed) {
1504 case USB_SPEED_HIGH:
1506 USB_PORT_STAT_HIGH_SPEED;
1509 dum->gadget.ep0->maxpacket = 8;
1511 USB_PORT_STAT_LOW_SPEED;
1514 dum->gadget.speed = USB_SPEED_FULL;
1519 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1520 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1525 case SetPortFeature:
1527 case USB_PORT_FEAT_SUSPEND:
1528 if ((dum->port_status & (1 << USB_PORT_FEAT_SUSPEND))
1531 (1 << USB_PORT_FEAT_SUSPEND);
1532 if (dum->driver && dum->driver->suspend) {
1533 spin_unlock (&dum->lock);
1534 dum->driver->suspend (&dum->gadget);
1535 spin_lock (&dum->lock);
1539 case USB_PORT_FEAT_RESET:
1540 /* if it's already running, disconnect first */
1541 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1542 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1543 | USB_PORT_STAT_LOW_SPEED
1544 | USB_PORT_STAT_HIGH_SPEED);
1546 dev_dbg (dummy_dev(dum),
1548 stop_activity (dum, dum->driver);
1551 /* FIXME test that code path! */
1553 /* 50msec reset signaling */
1554 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1557 dum->port_status |= (1 << wValue);
1562 dev_dbg (dummy_dev(dum),
1563 "hub control req%04x v%04x i%04x l%d\n",
1564 typeReq, wValue, wIndex, wLength);
1566 /* "protocol stall" on error */
1569 spin_unlock_irqrestore (&dum->lock, flags);
1574 /*-------------------------------------------------------------------------*/
1576 static inline ssize_t
1577 show_urb (char *buf, size_t size, struct urb *urb)
1579 int ep = usb_pipeendpoint (urb->pipe);
1581 return snprintf (buf, size,
1582 "urb/%p %s ep%d%s%s len %d/%d\n",
1585 switch (urb->dev->speed) {
1586 case USB_SPEED_LOW: s = "ls"; break;
1587 case USB_SPEED_FULL: s = "fs"; break;
1588 case USB_SPEED_HIGH: s = "hs"; break;
1589 default: s = "?"; break;
1591 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1593 switch (usb_pipetype (urb->pipe)) { \
1594 case PIPE_CONTROL: s = ""; break; \
1595 case PIPE_BULK: s = "-bulk"; break; \
1596 case PIPE_INTERRUPT: s = "-int"; break; \
1597 default: s = "-iso"; break; \
1599 urb->actual_length, urb->transfer_buffer_length);
1603 show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
1605 struct usb_hcd *hcd = dev_get_drvdata (dev);
1606 struct dummy *dum = hcd_to_dummy (hcd);
1609 unsigned long flags;
1611 spin_lock_irqsave (&dum->lock, flags);
1612 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1615 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1619 spin_unlock_irqrestore (&dum->lock, flags);
1623 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1625 static int dummy_start (struct usb_hcd *hcd)
1628 struct usb_device *root;
1631 dum = hcd_to_dummy (hcd);
1634 * MASTER side init ... we emulate a root hub that'll only ever
1635 * talk to one device (the slave side). Also appears in sysfs,
1636 * just like more familiar pci-based HCDs.
1638 spin_lock_init (&dum->lock);
1639 init_timer (&dum->timer);
1640 dum->timer.function = dummy_timer;
1641 dum->timer.data = (unsigned long) dum;
1643 INIT_LIST_HEAD (&dum->urbp_list);
1645 root = usb_alloc_dev (NULL, &hcd->self, 0);
1649 /* root hub enters addressed state... */
1650 hcd->state = HC_STATE_RUNNING;
1651 root->speed = USB_SPEED_HIGH;
1653 /* ...then configured, so khubd sees us. */
1654 if ((retval = usb_hcd_register_root_hub (root, hcd)) != 0) {
1658 /* only show a low-power port: just 8mA */
1659 hub_set_power_budget (root, 8);
1661 if ((retval = dummy_register_udc (dum)) != 0)
1664 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1665 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1669 usb_disconnect (&hcd->self.root_hub);
1672 hcd->state = HC_STATE_QUIESCING;
1676 static void dummy_stop (struct usb_hcd *hcd)
1680 dum = hcd_to_dummy (hcd);
1682 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1684 usb_gadget_unregister_driver (dum->driver);
1685 dummy_unregister_udc (dum);
1687 dev_info (dummy_dev(dum), "stopped\n");
1690 /*-------------------------------------------------------------------------*/
1692 static int dummy_h_get_frame (struct usb_hcd *hcd)
1694 return dummy_g_get_frame (NULL);
1697 static const struct hc_driver dummy_hcd = {
1698 .description = (char *) driver_name,
1699 .product_desc = "Dummy host controller",
1700 .hcd_priv_size = sizeof(struct dummy),
1704 .start = dummy_start,
1707 .urb_enqueue = dummy_urb_enqueue,
1708 .urb_dequeue = dummy_urb_dequeue,
1710 .get_frame_number = dummy_h_get_frame,
1712 .hub_status_data = dummy_hub_status,
1713 .hub_control = dummy_hub_control,
1716 static int dummy_probe (struct device *dev)
1718 struct usb_hcd *hcd;
1721 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1723 hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id);
1726 the_controller = hcd_to_dummy (hcd);
1728 retval = usb_add_hcd(hcd, 0, 0);
1731 the_controller = NULL;
1736 static void dummy_remove (struct device *dev)
1738 struct usb_hcd *hcd;
1740 hcd = dev_get_drvdata (dev);
1741 usb_remove_hcd (hcd);
1743 the_controller = NULL;
1746 /*-------------------------------------------------------------------------*/
1748 static int dummy_pdev_detect (void)
1752 retval = driver_register (&dummy_driver);
1756 the_pdev.name = "hc";
1757 the_pdev.dev.driver = &dummy_driver;
1758 the_pdev.dev.release = dummy_pdev_release;
1760 retval = platform_device_register (&the_pdev);
1762 driver_unregister (&dummy_driver);
1766 static void dummy_pdev_remove (void)
1768 platform_device_unregister (&the_pdev);
1769 driver_unregister (&dummy_driver);
1772 /*-------------------------------------------------------------------------*/
1774 static int __init init (void)
1778 if (usb_disabled ())
1780 if ((retval = dummy_pdev_detect ()) != 0)
1782 if ((retval = dummy_probe (&the_pdev.dev)) != 0)
1783 dummy_pdev_remove ();
1788 static void __exit cleanup (void)
1790 dummy_remove (&the_pdev.dev);
1791 dummy_pdev_remove ();
1793 module_exit (cleanup);