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 "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;
172 unsigned long re_timeout;
174 struct usb_device *udev;
175 struct list_head urbp_list;
178 static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
180 return (struct dummy *) (hcd->hcd_priv);
183 static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
185 return container_of((void *) dum, struct usb_hcd, hcd_priv);
188 static inline struct device *dummy_dev (struct dummy *dum)
190 return dummy_to_hcd(dum)->self.controller;
193 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
195 return container_of (ep->gadget, struct dummy, gadget);
198 static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
200 return container_of (gadget, struct dummy, gadget);
203 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
205 return container_of (dev, struct dummy, gadget.dev);
208 static struct dummy *the_controller;
210 /*-------------------------------------------------------------------------*/
213 * This "hardware" may look a bit odd in diagnostics since it's got both
214 * host and device sides; and it binds different drivers to each side.
216 static struct platform_device the_pdev;
218 static struct device_driver dummy_driver = {
219 .name = (char *) driver_name,
220 .bus = &platform_bus_type,
223 /*-------------------------------------------------------------------------*/
225 /* SLAVE/GADGET SIDE DRIVER
227 * This only tracks gadget state. All the work is done when the host
228 * side tries some (emulated) i/o operation. Real device controller
229 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
232 #define is_enabled(dum) \
233 (dum->port_status & USB_PORT_STAT_ENABLE)
236 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
243 ep = usb_ep_to_dummy_ep (_ep);
244 if (!_ep || !desc || ep->desc || _ep->name == ep0name
245 || desc->bDescriptorType != USB_DT_ENDPOINT)
247 dum = ep_to_dummy (ep);
248 if (!dum->driver || !is_enabled (dum))
250 max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
252 /* drivers must not request bad settings, since lower levels
253 * (hardware or its drivers) may not check. some endpoints
254 * can't do iso, many have maxpacket limitations, etc.
256 * since this "hardware" driver is here to help debugging, we
257 * have some extra sanity checks. (there could be more though,
258 * especially for "ep9out" style fixed function ones.)
261 switch (desc->bmAttributes & 0x03) {
262 case USB_ENDPOINT_XFER_BULK:
263 if (strstr (ep->ep.name, "-iso")
264 || strstr (ep->ep.name, "-int")) {
267 switch (dum->gadget.speed) {
271 /* conserve return statements */
274 case 8: case 16: case 32: case 64:
275 /* we'll fake any legal size */
283 case USB_ENDPOINT_XFER_INT:
284 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
286 /* real hardware might not handle all packet sizes */
287 switch (dum->gadget.speed) {
291 /* save a return statement */
295 /* save a return statement */
302 case USB_ENDPOINT_XFER_ISOC:
303 if (strstr (ep->ep.name, "-bulk")
304 || strstr (ep->ep.name, "-int"))
306 /* real hardware might not handle all packet sizes */
307 switch (dum->gadget.speed) {
311 /* save a return statement */
315 /* save a return statement */
321 /* few chips support control except on ep0 */
325 _ep->maxpacket = max;
328 dev_dbg (dummy_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
330 desc->bEndpointAddress & 0x0f,
331 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
333 switch (desc->bmAttributes & 0x03) {
334 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
335 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
336 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
337 default: val = "ctrl"; break;
341 /* at this point real hardware should be NAKing transfers
342 * to that endpoint, until a buffer is queued to it.
349 /* called with spinlock held */
350 static void nuke (struct dummy *dum, struct dummy_ep *ep)
352 while (!list_empty (&ep->queue)) {
353 struct dummy_request *req;
355 req = list_entry (ep->queue.next, struct dummy_request, queue);
356 list_del_init (&req->queue);
357 req->req.status = -ESHUTDOWN;
359 spin_unlock (&dum->lock);
360 req->req.complete (&ep->ep, &req->req);
361 spin_lock (&dum->lock);
365 static int dummy_disable (struct usb_ep *_ep)
372 ep = usb_ep_to_dummy_ep (_ep);
373 if (!_ep || !ep->desc || _ep->name == ep0name)
375 dum = ep_to_dummy (ep);
377 spin_lock_irqsave (&dum->lock, flags);
381 spin_unlock_irqrestore (&dum->lock, flags);
383 dev_dbg (dummy_dev(dum), "disabled %s\n", _ep->name);
387 static struct usb_request *
388 dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
391 struct dummy_request *req;
395 ep = usb_ep_to_dummy_ep (_ep);
397 req = kmalloc (sizeof *req, mem_flags);
400 memset (req, 0, sizeof *req);
401 INIT_LIST_HEAD (&req->queue);
406 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
409 struct dummy_request *req;
411 ep = usb_ep_to_dummy_ep (_ep);
412 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
415 req = usb_request_to_dummy_request (_req);
416 WARN_ON (!list_empty (&req->queue));
431 ep = usb_ep_to_dummy_ep (_ep);
432 dum = ep_to_dummy (ep);
436 retval = kmalloc (bytes, mem_flags);
437 *dma = (dma_addr_t) retval;
453 fifo_complete (struct usb_ep *ep, struct usb_request *req)
458 dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
461 struct dummy_request *req;
465 req = usb_request_to_dummy_request (_req);
466 if (!_req || !list_empty (&req->queue) || !_req->complete)
469 ep = usb_ep_to_dummy_ep (_ep);
470 if (!_ep || (!ep->desc && _ep->name != ep0name))
473 dum = ep_to_dummy (ep);
474 if (!dum->driver || !is_enabled (dum))
478 dev_dbg (dummy_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
479 ep, _req, _ep->name, _req->length, _req->buf);
482 _req->status = -EINPROGRESS;
484 spin_lock_irqsave (&dum->lock, flags);
486 /* implement an emulated single-request FIFO */
487 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
488 list_empty (&dum->fifo_req.queue) &&
489 list_empty (&ep->queue) &&
490 _req->length <= FIFO_SIZE) {
491 req = &dum->fifo_req;
493 req->req.buf = dum->fifo_buf;
494 memcpy (dum->fifo_buf, _req->buf, _req->length);
495 req->req.context = dum;
496 req->req.complete = fifo_complete;
498 spin_unlock (&dum->lock);
499 _req->actual = _req->length;
501 _req->complete (_ep, _req);
502 spin_lock (&dum->lock);
504 list_add_tail (&req->queue, &ep->queue);
505 spin_unlock_irqrestore (&dum->lock, flags);
507 /* real hardware would likely enable transfers here, in case
508 * it'd been left NAKing.
513 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
517 int retval = -EINVAL;
519 struct dummy_request *req = NULL;
523 ep = usb_ep_to_dummy_ep (_ep);
524 dum = ep_to_dummy (ep);
529 spin_lock_irqsave (&dum->lock, flags);
530 list_for_each_entry (req, &ep->queue, queue) {
531 if (&req->req == _req) {
532 list_del_init (&req->queue);
533 _req->status = -ECONNRESET;
538 spin_unlock_irqrestore (&dum->lock, flags);
541 dev_dbg (dummy_dev(dum),
542 "dequeued req %p from %s, len %d buf %p\n",
543 req, _ep->name, _req->length, _req->buf);
544 _req->complete (_ep, _req);
550 dummy_set_halt (struct usb_ep *_ep, int value)
557 ep = usb_ep_to_dummy_ep (_ep);
558 dum = ep_to_dummy (ep);
563 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
564 !list_empty (&ep->queue))
568 /* FIXME clear emulated data toggle too */
572 static const struct usb_ep_ops dummy_ep_ops = {
573 .enable = dummy_enable,
574 .disable = dummy_disable,
576 .alloc_request = dummy_alloc_request,
577 .free_request = dummy_free_request,
579 .alloc_buffer = dummy_alloc_buffer,
580 .free_buffer = dummy_free_buffer,
581 /* map, unmap, ... eventually hook the "generic" dma calls */
583 .queue = dummy_queue,
584 .dequeue = dummy_dequeue,
586 .set_halt = dummy_set_halt,
589 /*-------------------------------------------------------------------------*/
591 /* there are both host and device side versions of this call ... */
592 static int dummy_g_get_frame (struct usb_gadget *_gadget)
596 do_gettimeofday (&tv);
597 return tv.tv_usec / 1000;
600 static int dummy_wakeup (struct usb_gadget *_gadget)
604 dum = gadget_to_dummy (_gadget);
605 if ((dum->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) == 0
606 || !(dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)))
609 /* hub notices our request, issues downstream resume, etc */
611 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
615 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
619 dum = gadget_to_dummy (_gadget);
621 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
623 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
627 static const struct usb_gadget_ops dummy_ops = {
628 .get_frame = dummy_g_get_frame,
629 .wakeup = dummy_wakeup,
630 .set_selfpowered = dummy_set_selfpowered,
633 /*-------------------------------------------------------------------------*/
635 /* "function" sysfs attribute */
637 show_function (struct device *dev, char *buf)
639 struct dummy *dum = gadget_dev_to_dummy (dev);
641 if (!dum->driver || !dum->driver->function)
643 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
645 DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
647 /*-------------------------------------------------------------------------*/
650 * Driver registration/unregistration.
652 * This is basically hardware-specific; there's usually only one real USB
653 * device (not host) controller since that's how USB devices are intended
654 * to work. So most implementations of these api calls will rely on the
655 * fact that only one driver will ever bind to the hardware. But curious
656 * hardware can be built with discrete components, so the gadget API doesn't
657 * require that assumption.
659 * For this emulator, it might be convenient to create a usb slave device
660 * for each driver that registers: just add to a big root hub.
664 dummy_udc_release (struct device *dev)
669 dummy_pdev_release (struct device *dev)
674 dummy_register_udc (struct dummy *dum)
678 strcpy (dum->gadget.dev.bus_id, "udc");
679 dum->gadget.dev.parent = dummy_dev(dum);
680 dum->gadget.dev.release = dummy_udc_release;
682 rc = device_register (&dum->gadget.dev);
684 device_create_file (&dum->gadget.dev, &dev_attr_function);
689 dummy_unregister_udc (struct dummy *dum)
691 device_remove_file (&dum->gadget.dev, &dev_attr_function);
692 device_unregister (&dum->gadget.dev);
696 usb_gadget_register_driver (struct usb_gadget_driver *driver)
698 struct dummy *dum = the_controller;
705 if (!driver->bind || !driver->unbind || !driver->setup
706 || driver->speed == USB_SPEED_UNKNOWN)
710 * SLAVE side init ... the layer above hardware, which
711 * can't enumerate without help from the driver we're binding.
713 dum->gadget.name = gadget_name;
714 dum->gadget.ops = &dummy_ops;
715 dum->gadget.is_dualspeed = 1;
720 INIT_LIST_HEAD (&dum->gadget.ep_list);
721 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
722 struct dummy_ep *ep = &dum->ep [i];
726 ep->ep.name = ep_name [i];
727 ep->ep.ops = &dummy_ep_ops;
728 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
729 ep->halted = ep->already_seen = ep->setup_stage = 0;
730 ep->ep.maxpacket = ~0;
731 ep->last_io = jiffies;
732 ep->gadget = &dum->gadget;
734 INIT_LIST_HEAD (&ep->queue);
737 dum->gadget.ep0 = &dum->ep [0].ep;
738 dum->ep [0].ep.maxpacket = 64;
739 list_del_init (&dum->ep [0].ep.ep_list);
740 INIT_LIST_HEAD(&dum->fifo_req.queue);
742 dum->driver = driver;
743 dum->gadget.dev.driver = &driver->driver;
744 dev_dbg (dummy_dev(dum), "binding gadget driver '%s'\n",
745 driver->driver.name);
746 if ((retval = driver->bind (&dum->gadget)) != 0) {
748 dum->gadget.dev.driver = NULL;
752 // FIXME: Check these calls for errors and re-order
753 driver->driver.bus = dum->gadget.dev.parent->bus;
754 driver_register (&driver->driver);
756 device_bind_driver (&dum->gadget.dev);
758 /* khubd will enumerate this in a while */
759 dum->port_status |= USB_PORT_STAT_CONNECTION
760 | (1 << USB_PORT_FEAT_C_CONNECTION);
763 EXPORT_SYMBOL (usb_gadget_register_driver);
765 /* caller must hold lock */
767 stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
771 /* prevent any more requests */
774 /* The timer is left running so that outstanding URBs can fail */
776 /* nuke any pending requests first, so driver i/o is quiesced */
777 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
780 /* driver now does any non-usb quiescing necessary */
782 spin_unlock (&dum->lock);
783 driver->disconnect (&dum->gadget);
784 spin_lock (&dum->lock);
789 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
791 struct dummy *dum = the_controller;
796 if (!driver || driver != dum->driver)
799 dev_dbg (dummy_dev(dum), "unregister gadget driver '%s'\n",
800 driver->driver.name);
802 spin_lock_irqsave (&dum->lock, flags);
803 stop_activity (dum, driver);
804 dum->port_status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE |
805 USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
806 dum->port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
807 spin_unlock_irqrestore (&dum->lock, flags);
809 driver->unbind (&dum->gadget);
812 device_release_driver (&dum->gadget.dev);
814 driver_unregister (&driver->driver);
818 EXPORT_SYMBOL (usb_gadget_unregister_driver);
822 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
826 EXPORT_SYMBOL (net2280_set_fifo_mode);
828 /*-------------------------------------------------------------------------*/
830 /* MASTER/HOST SIDE DRIVER
832 * this uses the hcd framework to hook up to host side drivers.
833 * its root hub will only have one device, otherwise it acts like
834 * a normal host controller.
836 * when urbs are queued, they're just stuck on a list that we
837 * scan in a timer callback. that callback connects writes from
838 * the host with reads from the device, and so on, based on the
842 static int dummy_urb_enqueue (
844 struct usb_host_endpoint *ep,
852 if (!urb->transfer_buffer && urb->transfer_buffer_length)
855 urbp = kmalloc (sizeof *urbp, mem_flags);
860 dum = hcd_to_dummy (hcd);
861 spin_lock_irqsave (&dum->lock, flags);
864 dum->udev = urb->dev;
865 usb_get_dev (dum->udev);
866 } else if (unlikely (dum->udev != urb->dev))
867 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
869 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
871 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
872 urb->error_count = 1; /* mark as a new urb */
874 /* kick the scheduler, it'll do the rest */
875 if (!timer_pending (&dum->timer))
876 mod_timer (&dum->timer, jiffies + 1);
878 spin_unlock_irqrestore (&dum->lock, flags);
882 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
884 /* giveback happens automatically in timer callback */
888 static void maybe_set_status (struct urb *urb, int status)
890 spin_lock (&urb->lock);
891 if (urb->status == -EINPROGRESS)
892 urb->status = status;
893 spin_unlock (&urb->lock);
896 /* transfer up to a frame's worth; caller must own lock */
898 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
900 struct dummy_request *req;
903 /* if there's no request queued, the device is NAKing; return */
904 list_for_each_entry (req, &ep->queue, queue) {
905 unsigned host_len, dev_len, len;
906 int is_short, to_host;
909 /* 1..N packets of ep->ep.maxpacket each ... the last one
910 * may be short (including zero length).
912 * writer can send a zlp explicitly (length 0) or implicitly
913 * (length mod maxpacket zero, and 'zero' flag); they always
916 host_len = urb->transfer_buffer_length - urb->actual_length;
917 dev_len = req->req.length - req->req.actual;
918 len = min (host_len, dev_len);
920 /* FIXME update emulated data toggle too */
922 to_host = usb_pipein (urb->pipe);
923 if (unlikely (len == 0))
928 /* not enough bandwidth left? */
929 if (limit < ep->ep.maxpacket && limit < len)
931 len = min (len, (unsigned) limit);
935 /* use an extra pass for the final short packet */
936 if (len > ep->ep.maxpacket) {
938 len -= (len % ep->ep.maxpacket);
940 is_short = (len % ep->ep.maxpacket) != 0;
942 /* else transfer packet(s) */
943 ubuf = urb->transfer_buffer + urb->actual_length;
944 rbuf = req->req.buf + req->req.actual;
946 memcpy (ubuf, rbuf, len);
948 memcpy (rbuf, ubuf, len);
949 ep->last_io = jiffies;
952 urb->actual_length += len;
953 req->req.actual += len;
956 /* short packets terminate, maybe with overflow/underflow.
957 * it's only really an error to write too much.
959 * partially filling a buffer optionally blocks queue advances
960 * (so completion handlers can clean up the queue) but we don't
961 * need to emulate such data-in-flight. so we only show part
962 * of the URB_SHORT_NOT_OK effect: completion status.
965 if (host_len == dev_len) {
967 maybe_set_status (urb, 0);
968 } else if (to_host) {
970 if (dev_len > host_len)
971 maybe_set_status (urb, -EOVERFLOW);
973 maybe_set_status (urb,
977 } else if (!to_host) {
978 maybe_set_status (urb, 0);
979 if (host_len > dev_len)
980 req->req.status = -EOVERFLOW;
985 /* many requests terminate without a short packet */
987 if (req->req.length == req->req.actual
990 if (urb->transfer_buffer_length == urb->actual_length
991 && !(urb->transfer_flags
992 & URB_ZERO_PACKET)) {
993 maybe_set_status (urb, 0);
997 /* device side completion --> continuable */
998 if (req->req.status != -EINPROGRESS) {
999 list_del_init (&req->queue);
1001 spin_unlock (&dum->lock);
1002 req->req.complete (&ep->ep, &req->req);
1003 spin_lock (&dum->lock);
1005 /* requests might have been unlinked... */
1009 /* host side completion --> terminate */
1010 if (urb->status != -EINPROGRESS)
1013 /* rescan to continue with any other queued i/o */
1020 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1022 int limit = ep->ep.maxpacket;
1024 if (dum->gadget.speed == USB_SPEED_HIGH) {
1027 /* high bandwidth mode */
1028 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1029 tmp = le16_to_cpu (tmp);
1030 tmp = (tmp >> 11) & 0x03;
1031 tmp *= 8 /* applies to entire frame */;
1032 limit += limit * tmp;
1037 #define is_active(dum) ((dum->port_status & \
1038 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1039 USB_PORT_STAT_SUSPEND)) \
1040 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1042 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1046 if (!is_active (dum))
1048 if ((address & ~USB_DIR_IN) == 0)
1049 return &dum->ep [0];
1050 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1051 struct dummy_ep *ep = &dum->ep [i];
1055 if (ep->desc->bEndpointAddress == address)
1063 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1064 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1065 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1066 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1067 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1068 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1070 /* drive both sides of the transfers; looks like irq handlers to
1071 * both drivers except the callbacks aren't in_irq().
1073 static void dummy_timer (unsigned long _dum)
1075 struct dummy *dum = (struct dummy *) _dum;
1076 struct urbp *urbp, *tmp;
1077 unsigned long flags;
1081 /* simplistic model for one frame's bandwidth */
1082 switch (dum->gadget.speed) {
1084 total = 8/*bytes*/ * 12/*packets*/;
1086 case USB_SPEED_FULL:
1087 total = 64/*bytes*/ * 19/*packets*/;
1089 case USB_SPEED_HIGH:
1090 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1093 dev_err (dummy_dev(dum), "bogus device speed\n");
1097 /* FIXME if HZ != 1000 this will probably misbehave ... */
1099 /* look at each urb queued by the host side driver */
1100 spin_lock_irqsave (&dum->lock, flags);
1103 dev_err (dummy_dev(dum),
1104 "timer fired with no URBs pending?\n");
1105 spin_unlock_irqrestore (&dum->lock, flags);
1109 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1112 dum->ep [i].already_seen = 0;
1116 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1118 struct dummy_request *req;
1120 struct dummy_ep *ep = NULL;
1124 if (urb->status != -EINPROGRESS) {
1125 /* likely it was just unlinked */
1128 type = usb_pipetype (urb->pipe);
1130 /* used up this frame's non-periodic bandwidth?
1131 * FIXME there's infinite bandwidth for control and
1132 * periodic transfers ... unrealistic.
1134 if (total <= 0 && type == PIPE_BULK)
1137 /* find the gadget's ep for this request (if configured) */
1138 address = usb_pipeendpoint (urb->pipe);
1139 if (usb_pipein (urb->pipe))
1140 address |= USB_DIR_IN;
1141 ep = find_endpoint(dum, address);
1143 /* set_configuration() disagreement */
1144 dev_dbg (dummy_dev(dum),
1145 "no ep configured for urb %p\n",
1147 maybe_set_status (urb, -EPROTO);
1151 if (ep->already_seen)
1153 ep->already_seen = 1;
1154 if (ep == &dum->ep [0] && urb->error_count) {
1155 ep->setup_stage = 1; /* a new urb */
1156 urb->error_count = 0;
1158 if (ep->halted && !ep->setup_stage) {
1159 /* NOTE: must not be iso! */
1160 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1162 maybe_set_status (urb, -EPIPE);
1165 /* FIXME make sure both ends agree on maxpacket */
1167 /* handle control requests */
1168 if (ep == &dum->ep [0] && ep->setup_stage) {
1169 struct usb_ctrlrequest setup;
1171 struct dummy_ep *ep2;
1173 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1174 le16_to_cpus (&setup.wIndex);
1175 le16_to_cpus (&setup.wValue);
1176 le16_to_cpus (&setup.wLength);
1177 if (setup.wLength != urb->transfer_buffer_length) {
1178 maybe_set_status (urb, -EOVERFLOW);
1182 /* paranoia, in case of stale queued data */
1183 list_for_each_entry (req, &ep->queue, queue) {
1184 list_del_init (&req->queue);
1185 req->req.status = -EOVERFLOW;
1186 dev_dbg (dummy_dev(dum), "stale req = %p\n",
1189 spin_unlock (&dum->lock);
1190 req->req.complete (&ep->ep, &req->req);
1191 spin_lock (&dum->lock);
1192 ep->already_seen = 0;
1196 /* gadget driver never sees set_address or operations
1197 * on standard feature flags. some hardware doesn't
1200 ep->last_io = jiffies;
1201 ep->setup_stage = 0;
1203 switch (setup.bRequest) {
1204 case USB_REQ_SET_ADDRESS:
1205 if (setup.bRequestType != Dev_Request)
1207 dum->address = setup.wValue;
1208 maybe_set_status (urb, 0);
1209 dev_dbg (dummy_dev(dum), "set_address = %d\n",
1213 case USB_REQ_SET_FEATURE:
1214 if (setup.bRequestType == Dev_Request) {
1216 switch (setup.wValue) {
1217 case USB_DEVICE_REMOTE_WAKEUP:
1220 value = -EOPNOTSUPP;
1224 (1 << setup.wValue);
1225 maybe_set_status (urb, 0);
1228 } else if (setup.bRequestType == Ep_Request) {
1230 ep2 = find_endpoint (dum,
1233 value = -EOPNOTSUPP;
1238 maybe_set_status (urb, 0);
1241 case USB_REQ_CLEAR_FEATURE:
1242 if (setup.bRequestType == Dev_Request) {
1243 switch (setup.wValue) {
1244 case USB_DEVICE_REMOTE_WAKEUP:
1245 dum->devstatus &= ~(1 <<
1246 USB_DEVICE_REMOTE_WAKEUP);
1248 maybe_set_status (urb, 0);
1251 value = -EOPNOTSUPP;
1254 } else if (setup.bRequestType == Ep_Request) {
1256 ep2 = find_endpoint (dum,
1259 value = -EOPNOTSUPP;
1264 maybe_set_status (urb, 0);
1267 case USB_REQ_GET_STATUS:
1268 if (setup.bRequestType == Dev_InRequest
1269 || setup.bRequestType
1271 || setup.bRequestType
1276 // device: remote wakeup, selfpowered
1277 // interface: nothing
1279 buf = (char *)urb->transfer_buffer;
1280 if (urb->transfer_buffer_length > 0) {
1281 if (setup.bRequestType ==
1283 ep2 = find_endpoint (dum, setup.wIndex);
1285 value = -EOPNOTSUPP;
1288 buf [0] = ep2->halted;
1289 } else if (setup.bRequestType ==
1296 if (urb->transfer_buffer_length > 1)
1298 urb->actual_length = min (2,
1299 urb->transfer_buffer_length);
1301 maybe_set_status (urb, 0);
1306 /* gadget driver handles all other requests. block
1307 * until setup() returns; no reentrancy issues etc.
1310 spin_unlock (&dum->lock);
1311 value = dum->driver->setup (&dum->gadget,
1313 spin_lock (&dum->lock);
1316 /* no delays (max 64KB data stage) */
1318 goto treat_control_like_bulk;
1320 /* error, see below */
1324 if (value != -EOPNOTSUPP)
1325 dev_dbg (dummy_dev(dum),
1328 maybe_set_status (urb, -EPIPE);
1329 urb->actual_length = 0;
1335 /* non-control requests */
1337 switch (usb_pipetype (urb->pipe)) {
1338 case PIPE_ISOCHRONOUS:
1339 /* FIXME is it urb->interval since the last xfer?
1340 * use urb->iso_frame_desc[i].
1341 * complete whether or not ep has requests queued.
1342 * report random errors, to debug drivers.
1344 limit = max (limit, periodic_bytes (dum, ep));
1345 maybe_set_status (urb, -ENOSYS);
1348 case PIPE_INTERRUPT:
1349 /* FIXME is it urb->interval since the last xfer?
1350 * this almost certainly polls too fast.
1352 limit = max (limit, periodic_bytes (dum, ep));
1355 // case PIPE_BULK: case PIPE_CONTROL:
1357 treat_control_like_bulk:
1358 ep->last_io = jiffies;
1359 total = transfer (dum, urb, ep, limit);
1363 /* incomplete transfer? */
1364 if (urb->status == -EINPROGRESS)
1369 list_del (&urbp->urbp_list);
1372 ep->already_seen = ep->setup_stage = 0;
1374 spin_unlock (&dum->lock);
1375 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1376 spin_lock (&dum->lock);
1381 /* want a 1 msec delay here */
1382 if (!list_empty (&dum->urbp_list))
1383 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1385 usb_put_dev (dum->udev);
1389 spin_unlock_irqrestore (&dum->lock, flags);
1392 /*-------------------------------------------------------------------------*/
1394 #define PORT_C_MASK \
1395 ((1 << USB_PORT_FEAT_C_CONNECTION) \
1396 | (1 << USB_PORT_FEAT_C_ENABLE) \
1397 | (1 << USB_PORT_FEAT_C_SUSPEND) \
1398 | (1 << USB_PORT_FEAT_C_OVER_CURRENT) \
1399 | (1 << USB_PORT_FEAT_C_RESET))
1401 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1404 unsigned long flags;
1407 dum = hcd_to_dummy (hcd);
1409 spin_lock_irqsave (&dum->lock, flags);
1410 if (!(dum->port_status & PORT_C_MASK))
1414 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1418 spin_unlock_irqrestore (&dum->lock, flags);
1423 hub_descriptor (struct usb_hub_descriptor *desc)
1425 memset (desc, 0, sizeof *desc);
1426 desc->bDescriptorType = 0x29;
1427 desc->bDescLength = 9;
1428 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1429 desc->bNbrPorts = 1;
1430 desc->bitmap [0] = 0xff;
1431 desc->bitmap [1] = 0xff;
1434 static int dummy_hub_control (
1435 struct usb_hcd *hcd,
1444 unsigned long flags;
1446 dum = hcd_to_dummy (hcd);
1447 spin_lock_irqsave (&dum->lock, flags);
1449 case ClearHubFeature:
1451 case ClearPortFeature:
1453 case USB_PORT_FEAT_SUSPEND:
1454 if (dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)) {
1455 /* 20msec resume signaling */
1457 dum->re_timeout = jiffies +
1458 msecs_to_jiffies(20);
1461 case USB_PORT_FEAT_POWER:
1462 dum->port_status = 0;
1464 stop_activity(dum, dum->driver);
1467 dum->port_status &= ~(1 << wValue);
1470 case GetHubDescriptor:
1471 hub_descriptor ((struct usb_hub_descriptor *) buf);
1474 *(u32 *) buf = __constant_cpu_to_le32 (0);
1480 /* whoever resets or resumes must GetPortStatus to
1483 if (dum->resuming && time_after (jiffies, dum->re_timeout)) {
1484 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
1485 dum->port_status &= ~(1 << USB_PORT_FEAT_SUSPEND);
1487 dum->re_timeout = 0;
1488 if (dum->driver && dum->driver->resume) {
1489 spin_unlock (&dum->lock);
1490 dum->driver->resume (&dum->gadget);
1491 spin_lock (&dum->lock);
1494 if ((dum->port_status & (1 << USB_PORT_FEAT_RESET)) != 0
1495 && time_after (jiffies, dum->re_timeout)) {
1496 dum->port_status |= (1 << USB_PORT_FEAT_C_RESET);
1497 dum->port_status &= ~(1 << USB_PORT_FEAT_RESET);
1498 dum->re_timeout = 0;
1500 dum->port_status |= USB_PORT_STAT_ENABLE;
1501 /* give it the best speed we agree on */
1502 dum->gadget.speed = dum->driver->speed;
1503 dum->gadget.ep0->maxpacket = 64;
1504 switch (dum->gadget.speed) {
1505 case USB_SPEED_HIGH:
1507 USB_PORT_STAT_HIGH_SPEED;
1510 dum->gadget.ep0->maxpacket = 8;
1512 USB_PORT_STAT_LOW_SPEED;
1515 dum->gadget.speed = USB_SPEED_FULL;
1520 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1521 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1526 case SetPortFeature:
1528 case USB_PORT_FEAT_SUSPEND:
1529 if ((dum->port_status & (1 << USB_PORT_FEAT_SUSPEND))
1532 (1 << USB_PORT_FEAT_SUSPEND);
1533 if (dum->driver && dum->driver->suspend) {
1534 spin_unlock (&dum->lock);
1535 dum->driver->suspend (&dum->gadget);
1536 spin_lock (&dum->lock);
1540 case USB_PORT_FEAT_RESET:
1541 /* if it's already running, disconnect first */
1542 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1543 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1544 | USB_PORT_STAT_LOW_SPEED
1545 | USB_PORT_STAT_HIGH_SPEED);
1547 dev_dbg (dummy_dev(dum),
1549 stop_activity (dum, dum->driver);
1552 /* FIXME test that code path! */
1554 /* 50msec reset signaling */
1555 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1558 dum->port_status |= (1 << wValue);
1563 dev_dbg (dummy_dev(dum),
1564 "hub control req%04x v%04x i%04x l%d\n",
1565 typeReq, wValue, wIndex, wLength);
1567 /* "protocol stall" on error */
1570 spin_unlock_irqrestore (&dum->lock, flags);
1575 /*-------------------------------------------------------------------------*/
1577 static inline ssize_t
1578 show_urb (char *buf, size_t size, struct urb *urb)
1580 int ep = usb_pipeendpoint (urb->pipe);
1582 return snprintf (buf, size,
1583 "urb/%p %s ep%d%s%s len %d/%d\n",
1586 switch (urb->dev->speed) {
1587 case USB_SPEED_LOW: s = "ls"; break;
1588 case USB_SPEED_FULL: s = "fs"; break;
1589 case USB_SPEED_HIGH: s = "hs"; break;
1590 default: s = "?"; break;
1592 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1594 switch (usb_pipetype (urb->pipe)) { \
1595 case PIPE_CONTROL: s = ""; break; \
1596 case PIPE_BULK: s = "-bulk"; break; \
1597 case PIPE_INTERRUPT: s = "-int"; break; \
1598 default: s = "-iso"; break; \
1600 urb->actual_length, urb->transfer_buffer_length);
1604 show_urbs (struct device *dev, char *buf)
1606 struct usb_hcd *hcd = dev_get_drvdata (dev);
1607 struct dummy *dum = hcd_to_dummy (hcd);
1610 unsigned long flags;
1612 spin_lock_irqsave (&dum->lock, flags);
1613 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1616 temp = show_urb (buf, PAGE_SIZE - size, urbp->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 INIT_LIST_HEAD (&dum->urbp_list);
1647 root = usb_alloc_dev (NULL, &hcd->self, 0);
1651 /* root hub enters addressed state... */
1652 hcd->state = USB_STATE_RUNNING;
1653 root->speed = USB_SPEED_HIGH;
1655 /* ...then configured, so khubd sees us. */
1656 if ((retval = hcd_register_root (root, hcd)) != 0) {
1659 hcd->state = USB_STATE_QUIESCING;
1663 /* only show a low-power port: just 8mA */
1664 hub_set_power_budget (root, 8);
1666 if ((retval = dummy_register_udc (dum)) != 0) {
1667 usb_disconnect (&hcd->self.root_hub);
1671 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1672 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1678 static void dummy_stop (struct usb_hcd *hcd)
1682 dum = hcd_to_dummy (hcd);
1687 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1689 usb_gadget_unregister_driver (dum->driver);
1690 dummy_unregister_udc (dum);
1692 dev_info (dummy_dev(dum), "stopped\n");
1695 /*-------------------------------------------------------------------------*/
1697 static int dummy_h_get_frame (struct usb_hcd *hcd)
1699 return dummy_g_get_frame (NULL);
1702 static const struct hc_driver dummy_hcd = {
1703 .description = (char *) driver_name,
1704 .product_desc = "Dummy host controller",
1705 .hcd_priv_size = sizeof(struct dummy),
1709 .start = dummy_start,
1712 .urb_enqueue = dummy_urb_enqueue,
1713 .urb_dequeue = dummy_urb_dequeue,
1715 .get_frame_number = dummy_h_get_frame,
1717 .hub_status_data = dummy_hub_status,
1718 .hub_control = dummy_hub_control,
1721 static void dummy_remove (struct device *dev);
1723 static int dummy_probe (struct device *dev)
1725 struct usb_hcd *hcd;
1729 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1731 hcd = usb_create_hcd (&dummy_hcd);
1733 dev_dbg (dev, "hcd_alloc failed\n");
1737 dev_set_drvdata (dev, hcd);
1738 dum = hcd_to_dummy (hcd);
1739 the_controller = dum;
1741 hcd->self.controller = dev;
1743 /* FIXME don't require the pci-based buffer/alloc impls;
1744 * the "generic dma" implementation still requires them,
1745 * it's not very generic yet.
1747 retval = hcd_buffer_create (hcd);
1749 dev_dbg (dev, "pool alloc failed\n");
1753 hcd->self.bus_name = dev->bus_id;
1754 usb_register_bus (&hcd->self);
1756 if ((retval = dummy_start (hcd)) < 0)
1762 dev_set_drvdata (dev, NULL);
1766 static void dummy_remove (struct device *dev)
1768 struct usb_hcd *hcd;
1771 hcd = dev_get_drvdata (dev);
1772 dum = hcd_to_dummy (hcd);
1774 hcd->state = USB_STATE_QUIESCING;
1776 dev_dbg (dev, "roothub graceful disconnect\n");
1777 usb_disconnect (&hcd->self.root_hub);
1779 hcd->driver->stop (hcd);
1780 hcd->state = USB_STATE_HALT;
1782 hcd_buffer_destroy (hcd);
1784 dev_set_drvdata (dev, NULL);
1785 usb_deregister_bus (&hcd->self);
1786 the_controller = NULL;
1789 /*-------------------------------------------------------------------------*/
1791 static int dummy_pdev_detect (void)
1795 retval = driver_register (&dummy_driver);
1799 the_pdev.name = "hc";
1800 the_pdev.dev.driver = &dummy_driver;
1801 the_pdev.dev.release = dummy_pdev_release;
1803 retval = platform_device_register (&the_pdev);
1805 driver_unregister (&dummy_driver);
1809 static void dummy_pdev_remove (void)
1811 platform_device_unregister (&the_pdev);
1812 driver_unregister (&dummy_driver);
1815 /*-------------------------------------------------------------------------*/
1817 static int __init init (void)
1821 if (usb_disabled ())
1823 if ((retval = dummy_pdev_detect ()) != 0)
1825 if ((retval = dummy_probe (&the_pdev.dev)) != 0)
1826 dummy_pdev_remove ();
1831 static void __exit cleanup (void)
1833 dummy_remove (&the_pdev.dev);
1834 dummy_pdev_remove ();
1836 module_exit (cleanup);