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->port_status & USB_PORT_STAT_SUSPEND)
605 || !(dum->devstatus &
606 ( (1 << USB_DEVICE_B_HNP_ENABLE)
607 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
610 /* hub notices our request, issues downstream resume, etc */
612 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
616 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
620 dum = gadget_to_dummy (_gadget);
622 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
624 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
628 static const struct usb_gadget_ops dummy_ops = {
629 .get_frame = dummy_g_get_frame,
630 .wakeup = dummy_wakeup,
631 .set_selfpowered = dummy_set_selfpowered,
634 /*-------------------------------------------------------------------------*/
636 /* "function" sysfs attribute */
638 show_function (struct device *dev, struct device_attribute *attr, char *buf)
640 struct dummy *dum = gadget_dev_to_dummy (dev);
642 if (!dum->driver || !dum->driver->function)
644 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
646 DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
648 /*-------------------------------------------------------------------------*/
651 * Driver registration/unregistration.
653 * This is basically hardware-specific; there's usually only one real USB
654 * device (not host) controller since that's how USB devices are intended
655 * to work. So most implementations of these api calls will rely on the
656 * fact that only one driver will ever bind to the hardware. But curious
657 * hardware can be built with discrete components, so the gadget API doesn't
658 * require that assumption.
660 * For this emulator, it might be convenient to create a usb slave device
661 * for each driver that registers: just add to a big root hub.
664 /* This doesn't need to do anything because the udc device structure is
665 * stored inside the hcd and will be deallocated along with it. */
667 dummy_udc_release (struct device *dev) {}
669 /* This doesn't need to do anything because the pdev structure is
670 * statically allocated. */
672 dummy_pdev_release (struct device *dev) {}
675 dummy_register_udc (struct dummy *dum)
679 strcpy (dum->gadget.dev.bus_id, "udc");
680 dum->gadget.dev.parent = dummy_dev(dum);
681 dum->gadget.dev.release = dummy_udc_release;
683 rc = device_register (&dum->gadget.dev);
685 device_create_file (&dum->gadget.dev, &dev_attr_function);
690 dummy_unregister_udc (struct dummy *dum)
692 device_remove_file (&dum->gadget.dev, &dev_attr_function);
693 device_unregister (&dum->gadget.dev);
697 usb_gadget_register_driver (struct usb_gadget_driver *driver)
699 struct dummy *dum = the_controller;
706 if (!driver->bind || !driver->unbind || !driver->setup
707 || driver->speed == USB_SPEED_UNKNOWN)
711 * SLAVE side init ... the layer above hardware, which
712 * can't enumerate without help from the driver we're binding.
714 dum->gadget.name = gadget_name;
715 dum->gadget.ops = &dummy_ops;
716 dum->gadget.is_dualspeed = 1;
718 /* maybe claim OTG support, though we won't complete HNP */
719 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
724 INIT_LIST_HEAD (&dum->gadget.ep_list);
725 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
726 struct dummy_ep *ep = &dum->ep [i];
730 ep->ep.name = ep_name [i];
731 ep->ep.ops = &dummy_ep_ops;
732 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
733 ep->halted = ep->already_seen = ep->setup_stage = 0;
734 ep->ep.maxpacket = ~0;
735 ep->last_io = jiffies;
736 ep->gadget = &dum->gadget;
738 INIT_LIST_HEAD (&ep->queue);
741 dum->gadget.ep0 = &dum->ep [0].ep;
742 dum->ep [0].ep.maxpacket = 64;
743 list_del_init (&dum->ep [0].ep.ep_list);
744 INIT_LIST_HEAD(&dum->fifo_req.queue);
746 dum->driver = driver;
747 dum->gadget.dev.driver = &driver->driver;
748 dev_dbg (dummy_dev(dum), "binding gadget driver '%s'\n",
749 driver->driver.name);
750 if ((retval = driver->bind (&dum->gadget)) != 0) {
752 dum->gadget.dev.driver = NULL;
756 driver->driver.bus = dum->gadget.dev.parent->bus;
757 driver_register (&driver->driver);
758 device_bind_driver (&dum->gadget.dev);
760 /* khubd will enumerate this in a while */
761 dum->port_status |= USB_PORT_STAT_CONNECTION
762 | (USB_PORT_STAT_C_CONNECTION << 16);
765 EXPORT_SYMBOL (usb_gadget_register_driver);
767 /* caller must hold lock */
769 stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
773 /* prevent any more requests */
776 /* The timer is left running so that outstanding URBs can fail */
778 /* nuke any pending requests first, so driver i/o is quiesced */
779 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
782 /* driver now does any non-usb quiescing necessary */
784 spin_unlock (&dum->lock);
785 driver->disconnect (&dum->gadget);
786 spin_lock (&dum->lock);
791 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
793 struct dummy *dum = the_controller;
798 if (!driver || driver != dum->driver)
801 dev_dbg (dummy_dev(dum), "unregister gadget driver '%s'\n",
802 driver->driver.name);
804 spin_lock_irqsave (&dum->lock, flags);
805 stop_activity (dum, driver);
806 dum->port_status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE |
807 USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
808 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
809 spin_unlock_irqrestore (&dum->lock, flags);
811 driver->unbind (&dum->gadget);
814 device_release_driver (&dum->gadget.dev);
815 driver_unregister (&driver->driver);
819 EXPORT_SYMBOL (usb_gadget_unregister_driver);
823 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
827 EXPORT_SYMBOL (net2280_set_fifo_mode);
829 /*-------------------------------------------------------------------------*/
831 /* MASTER/HOST SIDE DRIVER
833 * this uses the hcd framework to hook up to host side drivers.
834 * its root hub will only have one device, otherwise it acts like
835 * a normal host controller.
837 * when urbs are queued, they're just stuck on a list that we
838 * scan in a timer callback. that callback connects writes from
839 * the host with reads from the device, and so on, based on the
843 static int dummy_urb_enqueue (
845 struct usb_host_endpoint *ep,
853 if (!urb->transfer_buffer && urb->transfer_buffer_length)
856 urbp = kmalloc (sizeof *urbp, mem_flags);
861 dum = hcd_to_dummy (hcd);
862 spin_lock_irqsave (&dum->lock, flags);
865 dum->udev = urb->dev;
866 usb_get_dev (dum->udev);
867 } else if (unlikely (dum->udev != urb->dev))
868 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
870 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
872 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
873 urb->error_count = 1; /* mark as a new urb */
875 /* kick the scheduler, it'll do the rest */
876 if (!timer_pending (&dum->timer))
877 mod_timer (&dum->timer, jiffies + 1);
879 spin_unlock_irqrestore (&dum->lock, flags);
883 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
885 /* giveback happens automatically in timer callback */
889 static void maybe_set_status (struct urb *urb, int status)
891 spin_lock (&urb->lock);
892 if (urb->status == -EINPROGRESS)
893 urb->status = status;
894 spin_unlock (&urb->lock);
897 /* transfer up to a frame's worth; caller must own lock */
899 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
901 struct dummy_request *req;
904 /* if there's no request queued, the device is NAKing; return */
905 list_for_each_entry (req, &ep->queue, queue) {
906 unsigned host_len, dev_len, len;
907 int is_short, to_host;
910 /* 1..N packets of ep->ep.maxpacket each ... the last one
911 * may be short (including zero length).
913 * writer can send a zlp explicitly (length 0) or implicitly
914 * (length mod maxpacket zero, and 'zero' flag); they always
917 host_len = urb->transfer_buffer_length - urb->actual_length;
918 dev_len = req->req.length - req->req.actual;
919 len = min (host_len, dev_len);
921 /* FIXME update emulated data toggle too */
923 to_host = usb_pipein (urb->pipe);
924 if (unlikely (len == 0))
929 /* not enough bandwidth left? */
930 if (limit < ep->ep.maxpacket && limit < len)
932 len = min (len, (unsigned) limit);
936 /* use an extra pass for the final short packet */
937 if (len > ep->ep.maxpacket) {
939 len -= (len % ep->ep.maxpacket);
941 is_short = (len % ep->ep.maxpacket) != 0;
943 /* else transfer packet(s) */
944 ubuf = urb->transfer_buffer + urb->actual_length;
945 rbuf = req->req.buf + req->req.actual;
947 memcpy (ubuf, rbuf, len);
949 memcpy (rbuf, ubuf, len);
950 ep->last_io = jiffies;
953 urb->actual_length += len;
954 req->req.actual += len;
957 /* short packets terminate, maybe with overflow/underflow.
958 * it's only really an error to write too much.
960 * partially filling a buffer optionally blocks queue advances
961 * (so completion handlers can clean up the queue) but we don't
962 * need to emulate such data-in-flight. so we only show part
963 * of the URB_SHORT_NOT_OK effect: completion status.
966 if (host_len == dev_len) {
968 maybe_set_status (urb, 0);
969 } else if (to_host) {
971 if (dev_len > host_len)
972 maybe_set_status (urb, -EOVERFLOW);
974 maybe_set_status (urb,
978 } else if (!to_host) {
979 maybe_set_status (urb, 0);
980 if (host_len > dev_len)
981 req->req.status = -EOVERFLOW;
986 /* many requests terminate without a short packet */
988 if (req->req.length == req->req.actual
991 if (urb->transfer_buffer_length == urb->actual_length
992 && !(urb->transfer_flags
993 & URB_ZERO_PACKET)) {
994 maybe_set_status (urb, 0);
998 /* device side completion --> continuable */
999 if (req->req.status != -EINPROGRESS) {
1000 list_del_init (&req->queue);
1002 spin_unlock (&dum->lock);
1003 req->req.complete (&ep->ep, &req->req);
1004 spin_lock (&dum->lock);
1006 /* requests might have been unlinked... */
1010 /* host side completion --> terminate */
1011 if (urb->status != -EINPROGRESS)
1014 /* rescan to continue with any other queued i/o */
1021 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1023 int limit = ep->ep.maxpacket;
1025 if (dum->gadget.speed == USB_SPEED_HIGH) {
1028 /* high bandwidth mode */
1029 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1030 tmp = le16_to_cpu (tmp);
1031 tmp = (tmp >> 11) & 0x03;
1032 tmp *= 8 /* applies to entire frame */;
1033 limit += limit * tmp;
1038 #define is_active(dum) ((dum->port_status & \
1039 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1040 USB_PORT_STAT_SUSPEND)) \
1041 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1043 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1047 if (!is_active (dum))
1049 if ((address & ~USB_DIR_IN) == 0)
1050 return &dum->ep [0];
1051 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1052 struct dummy_ep *ep = &dum->ep [i];
1056 if (ep->desc->bEndpointAddress == address)
1064 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1065 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1066 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1067 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1068 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1069 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1071 /* drive both sides of the transfers; looks like irq handlers to
1072 * both drivers except the callbacks aren't in_irq().
1074 static void dummy_timer (unsigned long _dum)
1076 struct dummy *dum = (struct dummy *) _dum;
1077 struct urbp *urbp, *tmp;
1078 unsigned long flags;
1082 /* simplistic model for one frame's bandwidth */
1083 switch (dum->gadget.speed) {
1085 total = 8/*bytes*/ * 12/*packets*/;
1087 case USB_SPEED_FULL:
1088 total = 64/*bytes*/ * 19/*packets*/;
1090 case USB_SPEED_HIGH:
1091 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1094 dev_err (dummy_dev(dum), "bogus device speed\n");
1098 /* FIXME if HZ != 1000 this will probably misbehave ... */
1100 /* look at each urb queued by the host side driver */
1101 spin_lock_irqsave (&dum->lock, flags);
1104 dev_err (dummy_dev(dum),
1105 "timer fired with no URBs pending?\n");
1106 spin_unlock_irqrestore (&dum->lock, flags);
1110 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1113 dum->ep [i].already_seen = 0;
1117 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1119 struct dummy_request *req;
1121 struct dummy_ep *ep = NULL;
1125 if (urb->status != -EINPROGRESS) {
1126 /* likely it was just unlinked */
1129 type = usb_pipetype (urb->pipe);
1131 /* used up this frame's non-periodic bandwidth?
1132 * FIXME there's infinite bandwidth for control and
1133 * periodic transfers ... unrealistic.
1135 if (total <= 0 && type == PIPE_BULK)
1138 /* find the gadget's ep for this request (if configured) */
1139 address = usb_pipeendpoint (urb->pipe);
1140 if (usb_pipein (urb->pipe))
1141 address |= USB_DIR_IN;
1142 ep = find_endpoint(dum, address);
1144 /* set_configuration() disagreement */
1145 dev_dbg (dummy_dev(dum),
1146 "no ep configured for urb %p\n",
1148 maybe_set_status (urb, -EPROTO);
1152 if (ep->already_seen)
1154 ep->already_seen = 1;
1155 if (ep == &dum->ep [0] && urb->error_count) {
1156 ep->setup_stage = 1; /* a new urb */
1157 urb->error_count = 0;
1159 if (ep->halted && !ep->setup_stage) {
1160 /* NOTE: must not be iso! */
1161 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1163 maybe_set_status (urb, -EPIPE);
1166 /* FIXME make sure both ends agree on maxpacket */
1168 /* handle control requests */
1169 if (ep == &dum->ep [0] && ep->setup_stage) {
1170 struct usb_ctrlrequest setup;
1172 struct dummy_ep *ep2;
1174 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1175 le16_to_cpus (&setup.wIndex);
1176 le16_to_cpus (&setup.wValue);
1177 le16_to_cpus (&setup.wLength);
1178 if (setup.wLength != urb->transfer_buffer_length) {
1179 maybe_set_status (urb, -EOVERFLOW);
1183 /* paranoia, in case of stale queued data */
1184 list_for_each_entry (req, &ep->queue, queue) {
1185 list_del_init (&req->queue);
1186 req->req.status = -EOVERFLOW;
1187 dev_dbg (dummy_dev(dum), "stale req = %p\n",
1190 spin_unlock (&dum->lock);
1191 req->req.complete (&ep->ep, &req->req);
1192 spin_lock (&dum->lock);
1193 ep->already_seen = 0;
1197 /* gadget driver never sees set_address or operations
1198 * on standard feature flags. some hardware doesn't
1201 ep->last_io = jiffies;
1202 ep->setup_stage = 0;
1204 switch (setup.bRequest) {
1205 case USB_REQ_SET_ADDRESS:
1206 if (setup.bRequestType != Dev_Request)
1208 dum->address = setup.wValue;
1209 maybe_set_status (urb, 0);
1210 dev_dbg (dummy_dev(dum), "set_address = %d\n",
1214 case USB_REQ_SET_FEATURE:
1215 if (setup.bRequestType == Dev_Request) {
1217 switch (setup.wValue) {
1218 case USB_DEVICE_REMOTE_WAKEUP:
1220 case USB_DEVICE_B_HNP_ENABLE:
1221 dum->gadget.b_hnp_enable = 1;
1223 case USB_DEVICE_A_HNP_SUPPORT:
1224 dum->gadget.a_hnp_support = 1;
1226 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1227 dum->gadget.a_alt_hnp_support
1231 value = -EOPNOTSUPP;
1235 (1 << setup.wValue);
1236 maybe_set_status (urb, 0);
1239 } else if (setup.bRequestType == Ep_Request) {
1241 ep2 = find_endpoint (dum,
1244 value = -EOPNOTSUPP;
1249 maybe_set_status (urb, 0);
1252 case USB_REQ_CLEAR_FEATURE:
1253 if (setup.bRequestType == Dev_Request) {
1254 switch (setup.wValue) {
1255 case USB_DEVICE_REMOTE_WAKEUP:
1256 dum->devstatus &= ~(1 <<
1257 USB_DEVICE_REMOTE_WAKEUP);
1259 maybe_set_status (urb, 0);
1262 value = -EOPNOTSUPP;
1265 } else if (setup.bRequestType == Ep_Request) {
1267 ep2 = find_endpoint (dum,
1270 value = -EOPNOTSUPP;
1275 maybe_set_status (urb, 0);
1278 case USB_REQ_GET_STATUS:
1279 if (setup.bRequestType == Dev_InRequest
1280 || setup.bRequestType
1282 || setup.bRequestType
1287 // device: remote wakeup, selfpowered
1288 // interface: nothing
1290 buf = (char *)urb->transfer_buffer;
1291 if (urb->transfer_buffer_length > 0) {
1292 if (setup.bRequestType ==
1294 ep2 = find_endpoint (dum, setup.wIndex);
1296 value = -EOPNOTSUPP;
1299 buf [0] = ep2->halted;
1300 } else if (setup.bRequestType ==
1307 if (urb->transfer_buffer_length > 1)
1309 urb->actual_length = min (2,
1310 urb->transfer_buffer_length);
1312 maybe_set_status (urb, 0);
1317 /* gadget driver handles all other requests. block
1318 * until setup() returns; no reentrancy issues etc.
1321 spin_unlock (&dum->lock);
1322 value = dum->driver->setup (&dum->gadget,
1324 spin_lock (&dum->lock);
1327 /* no delays (max 64KB data stage) */
1329 goto treat_control_like_bulk;
1331 /* error, see below */
1335 if (value != -EOPNOTSUPP)
1336 dev_dbg (dummy_dev(dum),
1339 maybe_set_status (urb, -EPIPE);
1340 urb->actual_length = 0;
1346 /* non-control requests */
1348 switch (usb_pipetype (urb->pipe)) {
1349 case PIPE_ISOCHRONOUS:
1350 /* FIXME is it urb->interval since the last xfer?
1351 * use urb->iso_frame_desc[i].
1352 * complete whether or not ep has requests queued.
1353 * report random errors, to debug drivers.
1355 limit = max (limit, periodic_bytes (dum, ep));
1356 maybe_set_status (urb, -ENOSYS);
1359 case PIPE_INTERRUPT:
1360 /* FIXME is it urb->interval since the last xfer?
1361 * this almost certainly polls too fast.
1363 limit = max (limit, periodic_bytes (dum, ep));
1366 // case PIPE_BULK: case PIPE_CONTROL:
1368 treat_control_like_bulk:
1369 ep->last_io = jiffies;
1370 total = transfer (dum, urb, ep, limit);
1374 /* incomplete transfer? */
1375 if (urb->status == -EINPROGRESS)
1380 list_del (&urbp->urbp_list);
1383 ep->already_seen = ep->setup_stage = 0;
1385 spin_unlock (&dum->lock);
1386 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1387 spin_lock (&dum->lock);
1392 /* want a 1 msec delay here */
1393 if (!list_empty (&dum->urbp_list))
1394 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1396 usb_put_dev (dum->udev);
1400 spin_unlock_irqrestore (&dum->lock, flags);
1403 /*-------------------------------------------------------------------------*/
1405 #define PORT_C_MASK \
1406 ((USB_PORT_STAT_C_CONNECTION \
1407 | USB_PORT_STAT_C_ENABLE \
1408 | USB_PORT_STAT_C_SUSPEND \
1409 | USB_PORT_STAT_C_OVERCURRENT \
1410 | USB_PORT_STAT_C_RESET) << 16)
1412 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1415 unsigned long flags;
1418 dum = hcd_to_dummy (hcd);
1420 spin_lock_irqsave (&dum->lock, flags);
1421 if (!(dum->port_status & PORT_C_MASK))
1425 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1429 spin_unlock_irqrestore (&dum->lock, flags);
1434 hub_descriptor (struct usb_hub_descriptor *desc)
1436 memset (desc, 0, sizeof *desc);
1437 desc->bDescriptorType = 0x29;
1438 desc->bDescLength = 9;
1439 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1440 desc->bNbrPorts = 1;
1441 desc->bitmap [0] = 0xff;
1442 desc->bitmap [1] = 0xff;
1445 static int dummy_hub_control (
1446 struct usb_hcd *hcd,
1455 unsigned long flags;
1457 dum = hcd_to_dummy (hcd);
1458 spin_lock_irqsave (&dum->lock, flags);
1460 case ClearHubFeature:
1462 case ClearPortFeature:
1464 case USB_PORT_FEAT_SUSPEND:
1465 if (dum->port_status & USB_PORT_STAT_SUSPEND) {
1466 /* 20msec resume signaling */
1468 dum->re_timeout = jiffies +
1469 msecs_to_jiffies(20);
1472 case USB_PORT_FEAT_POWER:
1473 dum->port_status = 0;
1475 stop_activity(dum, dum->driver);
1478 dum->port_status &= ~(1 << wValue);
1481 case GetHubDescriptor:
1482 hub_descriptor ((struct usb_hub_descriptor *) buf);
1485 *(u32 *) buf = __constant_cpu_to_le32 (0);
1491 /* whoever resets or resumes must GetPortStatus to
1494 if (dum->resuming && time_after (jiffies, dum->re_timeout)) {
1495 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1496 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1498 dum->re_timeout = 0;
1499 if (dum->driver && dum->driver->resume) {
1500 spin_unlock (&dum->lock);
1501 dum->driver->resume (&dum->gadget);
1502 spin_lock (&dum->lock);
1505 if ((dum->port_status & USB_PORT_STAT_RESET) != 0
1506 && time_after (jiffies, dum->re_timeout)) {
1507 dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
1508 dum->port_status &= ~USB_PORT_STAT_RESET;
1509 dum->re_timeout = 0;
1511 dum->port_status |= USB_PORT_STAT_ENABLE;
1512 /* give it the best speed we agree on */
1513 dum->gadget.speed = dum->driver->speed;
1514 dum->gadget.ep0->maxpacket = 64;
1515 switch (dum->gadget.speed) {
1516 case USB_SPEED_HIGH:
1518 USB_PORT_STAT_HIGH_SPEED;
1521 dum->gadget.ep0->maxpacket = 8;
1523 USB_PORT_STAT_LOW_SPEED;
1526 dum->gadget.speed = USB_SPEED_FULL;
1531 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1532 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1537 case SetPortFeature:
1539 case USB_PORT_FEAT_SUSPEND:
1540 if ((dum->port_status & USB_PORT_STAT_SUSPEND)
1542 dum->port_status |= USB_PORT_STAT_SUSPEND;
1543 if (dum->driver && dum->driver->suspend) {
1544 spin_unlock (&dum->lock);
1545 dum->driver->suspend (&dum->gadget);
1546 spin_lock (&dum->lock);
1547 /* HNP would happen here; for now we
1548 * assume b_bus_req is always true.
1550 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1551 & dum->devstatus) != 0)
1552 dev_dbg (dummy_dev(dum),
1557 case USB_PORT_FEAT_RESET:
1558 /* if it's already running, disconnect first */
1559 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1560 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1561 | USB_PORT_STAT_LOW_SPEED
1562 | USB_PORT_STAT_HIGH_SPEED);
1564 dev_dbg (dummy_dev(dum),
1566 stop_activity (dum, dum->driver);
1569 /* FIXME test that code path! */
1571 /* 50msec reset signaling */
1572 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1575 dum->port_status |= (1 << wValue);
1580 dev_dbg (dummy_dev(dum),
1581 "hub control req%04x v%04x i%04x l%d\n",
1582 typeReq, wValue, wIndex, wLength);
1584 /* "protocol stall" on error */
1587 spin_unlock_irqrestore (&dum->lock, flags);
1592 /*-------------------------------------------------------------------------*/
1594 static inline ssize_t
1595 show_urb (char *buf, size_t size, struct urb *urb)
1597 int ep = usb_pipeendpoint (urb->pipe);
1599 return snprintf (buf, size,
1600 "urb/%p %s ep%d%s%s len %d/%d\n",
1603 switch (urb->dev->speed) {
1604 case USB_SPEED_LOW: s = "ls"; break;
1605 case USB_SPEED_FULL: s = "fs"; break;
1606 case USB_SPEED_HIGH: s = "hs"; break;
1607 default: s = "?"; break;
1609 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1611 switch (usb_pipetype (urb->pipe)) { \
1612 case PIPE_CONTROL: s = ""; break; \
1613 case PIPE_BULK: s = "-bulk"; break; \
1614 case PIPE_INTERRUPT: s = "-int"; break; \
1615 default: s = "-iso"; break; \
1617 urb->actual_length, urb->transfer_buffer_length);
1621 show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
1623 struct usb_hcd *hcd = dev_get_drvdata (dev);
1624 struct dummy *dum = hcd_to_dummy (hcd);
1627 unsigned long flags;
1629 spin_lock_irqsave (&dum->lock, flags);
1630 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1633 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1637 spin_unlock_irqrestore (&dum->lock, flags);
1641 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1643 static int dummy_start (struct usb_hcd *hcd)
1648 dum = hcd_to_dummy (hcd);
1651 * MASTER side init ... we emulate a root hub that'll only ever
1652 * talk to one device (the slave side). Also appears in sysfs,
1653 * just like more familiar pci-based HCDs.
1655 spin_lock_init (&dum->lock);
1656 init_timer (&dum->timer);
1657 dum->timer.function = dummy_timer;
1658 dum->timer.data = (unsigned long) dum;
1660 INIT_LIST_HEAD (&dum->urbp_list);
1662 if ((retval = dummy_register_udc (dum)) != 0)
1665 /* only show a low-power port: just 8mA */
1666 hcd->power_budget = 8;
1667 hcd->state = HC_STATE_RUNNING;
1669 #ifdef CONFIG_USB_OTG
1670 hcd->self.otg_port = 1;
1673 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1674 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1678 static void dummy_stop (struct usb_hcd *hcd)
1682 dum = hcd_to_dummy (hcd);
1684 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1686 usb_gadget_unregister_driver (dum->driver);
1687 dummy_unregister_udc (dum);
1689 dev_info (dummy_dev(dum), "stopped\n");
1692 /*-------------------------------------------------------------------------*/
1694 static int dummy_h_get_frame (struct usb_hcd *hcd)
1696 return dummy_g_get_frame (NULL);
1699 static const struct hc_driver dummy_hcd = {
1700 .description = (char *) driver_name,
1701 .product_desc = "Dummy host controller",
1702 .hcd_priv_size = sizeof(struct dummy),
1706 .start = dummy_start,
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 int dummy_probe (struct device *dev)
1720 struct usb_hcd *hcd;
1723 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1725 hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id);
1728 the_controller = hcd_to_dummy (hcd);
1730 retval = usb_add_hcd(hcd, 0, 0);
1733 the_controller = NULL;
1738 static void dummy_remove (struct device *dev)
1740 struct usb_hcd *hcd;
1742 hcd = dev_get_drvdata (dev);
1743 usb_remove_hcd (hcd);
1745 the_controller = NULL;
1748 /*-------------------------------------------------------------------------*/
1750 static int dummy_pdev_detect (void)
1754 retval = driver_register (&dummy_driver);
1758 the_pdev.name = "hc";
1759 the_pdev.dev.driver = &dummy_driver;
1760 the_pdev.dev.release = dummy_pdev_release;
1762 retval = platform_device_register (&the_pdev);
1764 driver_unregister (&dummy_driver);
1768 static void dummy_pdev_remove (void)
1770 platform_device_unregister (&the_pdev);
1771 driver_unregister (&dummy_driver);
1774 /*-------------------------------------------------------------------------*/
1776 static int __init init (void)
1780 if (usb_disabled ())
1782 if ((retval = dummy_pdev_detect ()) != 0)
1784 if ((retval = dummy_probe (&the_pdev.dev)) != 0)
1785 dummy_pdev_remove ();
1790 static void __exit cleanup (void)
1792 dummy_remove (&the_pdev.dev);
1793 dummy_pdev_remove ();
1795 module_exit (cleanup);