2 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
6 * Copyright (C) 2003 David Brownell
7 * Copyright (C) 2003, 2004 Alan Stern
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * This exposes a device side "USB gadget" API, driven by requests to a
27 * Linux-USB host controller driver. USB traffic is simulated; there's
28 * no need for USB hardware. Use this with two other drivers:
30 * - Gadget driver, responding to requests (slave);
31 * - Host-side device driver, as already familiar in Linux.
33 * Having this all in one kernel can help some stages of development,
34 * bypassing some hardware (and driver) issues. UML could help too.
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/delay.h>
43 #include <linux/ioport.h>
44 #include <linux/sched.h>
45 #include <linux/slab.h>
46 #include <linux/smp_lock.h>
47 #include <linux/errno.h>
48 #include <linux/init.h>
49 #include <linux/timer.h>
50 #include <linux/list.h>
51 #include <linux/interrupt.h>
52 #include <linux/version.h>
54 #include <linux/usb.h>
55 #include <linux/usb_gadget.h>
57 #include <asm/byteorder.h>
60 #include <asm/system.h>
61 #include <asm/unaligned.h>
64 #include "../core/hcd.h"
67 #define DRIVER_DESC "USB Host+Gadget Emulator"
68 #define DRIVER_VERSION "14 Mar 2004"
70 static const char driver_name [] = "dummy_hcd";
71 static const char driver_desc [] = "USB Host+Gadget Emulator";
73 static const char gadget_name [] = "dummy_udc";
75 MODULE_DESCRIPTION (DRIVER_DESC);
76 MODULE_AUTHOR ("David Brownell");
77 MODULE_LICENSE ("GPL");
79 /*-------------------------------------------------------------------------*/
81 /* gadget side driver data structres */
83 struct list_head queue;
84 unsigned long last_io; /* jiffies timestamp */
85 struct usb_gadget *gadget;
86 const struct usb_endpoint_descriptor *desc;
89 unsigned already_seen : 1;
90 unsigned setup_stage : 1;
93 struct dummy_request {
94 struct list_head queue; /* ep's requests */
95 struct usb_request req;
98 /*-------------------------------------------------------------------------*/
101 * Every device has ep0 for control requests, plus up to 30 more endpoints,
102 * in one of two types:
104 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
105 * number can be changed. Names like "ep-a" are used for this type.
107 * - Fixed Function: in other cases. some characteristics may be mutable;
108 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
110 * Gadget drivers are responsible for not setting up conflicting endpoint
111 * configurations, illegal or unsupported packet lengths, and so on.
114 static const char ep0name [] = "ep0";
116 static const char *const ep_name [] = {
117 ep0name, /* everyone has ep0 */
119 /* act like a net2280: high speed, six configurable endpoints */
120 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
122 /* or like pxa250: fifteen fixed function endpoints */
123 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
124 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
125 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
128 /* or like sa1100: two fixed function endpoints */
129 "ep1out-bulk", "ep2in-bulk",
131 #define DUMMY_ENDPOINTS (sizeof(ep_name)/sizeof(char *))
139 * SLAVE/GADGET side support
141 struct dummy_ep ep [DUMMY_ENDPOINTS];
143 struct usb_gadget gadget;
144 struct usb_gadget_driver *driver;
145 struct dummy_request fifo_req;
146 u8 fifo_buf [FIFO_SIZE];
150 * MASTER/HOST side support
153 struct platform_device pdev;
154 struct timer_list timer;
157 struct completion released;
159 unsigned long re_timeout;
161 struct usb_device *udev;
164 static struct dummy *the_controller;
166 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
168 return container_of (ep->gadget, struct dummy, gadget);
171 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
173 return container_of (dev, struct dummy, gadget.dev);
177 * This "hardware" may look a bit odd in diagnostics since it's got both
178 * host and device sides; and it binds different drivers to each side.
180 #define hardware (&the_controller->pdev.dev)
182 /*-------------------------------------------------------------------------*/
184 static struct device_driver dummy_driver = {
185 .name = (char *) driver_name,
186 .bus = &platform_bus_type,
189 /*-------------------------------------------------------------------------*/
191 /* SLAVE/GADGET SIDE DRIVER
193 * This only tracks gadget state. All the work is done when the host
194 * side tries some (emulated) i/o operation. Real device controller
195 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
198 #define is_enabled() \
199 (the_controller->port_status & USB_PORT_STAT_ENABLE)
202 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
209 ep = container_of (_ep, struct dummy_ep, ep);
210 if (!_ep || !desc || ep->desc || _ep->name == ep0name
211 || desc->bDescriptorType != USB_DT_ENDPOINT)
212 if (!the_controller->driver || !is_enabled ())
214 max = desc->wMaxPacketSize & 0x3ff;
216 /* drivers must not request bad settings, since lower levels
217 * (hardware or its drivers) may not check. some endpoints
218 * can't do iso, many have maxpacket limitations, etc.
220 * since this "hardware" driver is here to help debugging, we
221 * have some extra sanity checks. (there could be more though,
222 * especially for "ep9out" style fixed function ones.)
224 dum = container_of (ep->gadget, struct dummy, gadget);
226 switch (desc->bmAttributes & 0x03) {
227 case USB_ENDPOINT_XFER_BULK:
228 if (strstr (ep->ep.name, "-iso")
229 || strstr (ep->ep.name, "-int")) {
232 switch (dum->gadget.speed) {
236 /* conserve return statements */
239 case 8: case 16: case 32: case 64:
240 /* we'll fake any legal size */
248 case USB_ENDPOINT_XFER_INT:
249 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
251 /* real hardware might not handle all packet sizes */
252 switch (dum->gadget.speed) {
256 /* save a return statement */
260 /* save a return statement */
267 case USB_ENDPOINT_XFER_ISOC:
268 if (strstr (ep->ep.name, "-bulk")
269 || strstr (ep->ep.name, "-int"))
271 /* real hardware might not handle all packet sizes */
272 switch (dum->gadget.speed) {
276 /* save a return statement */
280 /* save a return statement */
286 /* few chips support control except on ep0 */
290 _ep->maxpacket = max;
293 dev_dbg (hardware, "enabled %s (ep%d%s-%s) maxpacket %d\n",
295 desc->bEndpointAddress & 0x0f,
296 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
298 switch (desc->bmAttributes & 0x03) {
299 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
300 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
301 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
302 default: val = "ctrl"; break;
306 /* at this point real hardware should be NAKing transfers
307 * to that endpoint, until a buffer is queued to it.
314 /* called with spinlock held */
315 static void nuke (struct dummy *dum, struct dummy_ep *ep)
317 while (!list_empty (&ep->queue)) {
318 struct dummy_request *req;
320 req = list_entry (ep->queue.next, struct dummy_request, queue);
321 list_del_init (&req->queue);
322 req->req.status = -ESHUTDOWN;
324 spin_unlock (&dum->lock);
325 req->req.complete (&ep->ep, &req->req);
326 spin_lock (&dum->lock);
330 static int dummy_disable (struct usb_ep *_ep)
337 ep = container_of (_ep, struct dummy_ep, ep);
338 if (!_ep || !ep->desc || _ep->name == ep0name)
340 dum = ep_to_dummy (ep);
342 spin_lock_irqsave (&dum->lock, flags);
346 spin_unlock_irqrestore (&dum->lock, flags);
348 dev_dbg (hardware, "disabled %s\n", _ep->name);
352 static struct usb_request *
353 dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
356 struct dummy_request *req;
358 ep = container_of (_ep, struct dummy_ep, ep);
362 req = kmalloc (sizeof *req, mem_flags);
365 memset (req, 0, sizeof *req);
366 INIT_LIST_HEAD (&req->queue);
371 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
374 struct dummy_request *req;
376 ep = container_of (_ep, struct dummy_ep, ep);
377 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
380 req = container_of (_req, struct dummy_request, req);
381 WARN_ON (!list_empty (&req->queue));
394 if (!the_controller->driver)
396 retval = kmalloc (bytes, mem_flags);
397 *dma = (dma_addr_t) retval;
413 fifo_complete (struct usb_ep *ep, struct usb_request *req)
416 dev_dbg (hardware, "fifo_complete: %d\n", req->status);
421 dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
424 struct dummy_request *req;
428 req = container_of (_req, struct dummy_request, req);
429 if (!_req || !list_empty (&req->queue) || !_req->complete)
432 ep = container_of (_ep, struct dummy_ep, ep);
433 if (!_ep || (!ep->desc && _ep->name != ep0name))
436 if (!the_controller->driver || !is_enabled ())
439 dum = container_of (ep->gadget, struct dummy, gadget);
442 dev_dbg (hardware, "ep %p queue req %p to %s, len %d buf %p\n",
443 ep, _req, _ep->name, _req->length, _req->buf);
446 _req->status = -EINPROGRESS;
448 spin_lock_irqsave (&dum->lock, flags);
450 /* implement an emulated single-request FIFO */
451 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
452 list_empty (&dum->fifo_req.queue) &&
453 list_empty (&ep->queue) &&
454 _req->length <= FIFO_SIZE) {
455 req = &dum->fifo_req;
457 req->req.buf = dum->fifo_buf;
458 memcpy (dum->fifo_buf, _req->buf, _req->length);
459 req->req.context = dum;
460 req->req.complete = fifo_complete;
462 spin_unlock (&dum->lock);
463 _req->actual = _req->length;
465 _req->complete (_ep, _req);
466 spin_lock (&dum->lock);
468 list_add_tail (&req->queue, &ep->queue);
469 spin_unlock_irqrestore (&dum->lock, flags);
471 /* real hardware would likely enable transfers here, in case
472 * it'd been left NAKing.
477 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
481 int retval = -EINVAL;
483 struct dummy_request *req = 0;
485 if (!the_controller->driver)
490 ep = container_of (_ep, struct dummy_ep, ep);
491 dum = container_of (ep->gadget, struct dummy, gadget);
493 spin_lock_irqsave (&dum->lock, flags);
494 list_for_each_entry (req, &ep->queue, queue) {
495 if (&req->req == _req) {
496 list_del_init (&req->queue);
497 _req->status = -ECONNRESET;
502 spin_unlock_irqrestore (&dum->lock, flags);
505 dev_dbg (hardware, "dequeued req %p from %s, len %d buf %p\n",
506 req, _ep->name, _req->length, _req->buf);
508 _req->complete (_ep, _req);
514 dummy_set_halt (struct usb_ep *_ep, int value)
520 if (!the_controller->driver)
522 ep = container_of (_ep, struct dummy_ep, ep);
525 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
526 !list_empty (&ep->queue))
530 /* FIXME clear emulated data toggle too */
534 static const struct usb_ep_ops dummy_ep_ops = {
535 .enable = dummy_enable,
536 .disable = dummy_disable,
538 .alloc_request = dummy_alloc_request,
539 .free_request = dummy_free_request,
541 .alloc_buffer = dummy_alloc_buffer,
542 .free_buffer = dummy_free_buffer,
543 /* map, unmap, ... eventually hook the "generic" dma calls */
545 .queue = dummy_queue,
546 .dequeue = dummy_dequeue,
548 .set_halt = dummy_set_halt,
551 /*-------------------------------------------------------------------------*/
553 /* there are both host and device side versions of this call ... */
554 static int dummy_g_get_frame (struct usb_gadget *_gadget)
558 do_gettimeofday (&tv);
559 return tv.tv_usec / 1000;
562 static int dummy_wakeup (struct usb_gadget *_gadget)
566 dum = container_of (_gadget, struct dummy, gadget);
567 if ((dum->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) == 0
568 || !(dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)))
571 /* hub notices our request, issues downstream resume, etc */
573 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
577 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
581 dum = container_of (_gadget, struct dummy, gadget);
583 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
585 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
589 static const struct usb_gadget_ops dummy_ops = {
590 .get_frame = dummy_g_get_frame,
591 .wakeup = dummy_wakeup,
592 .set_selfpowered = dummy_set_selfpowered,
595 /*-------------------------------------------------------------------------*/
597 /* "function" sysfs attribute */
599 show_function (struct device *_dev, char *buf)
601 struct dummy *dum = the_controller;
603 if (!dum->driver->function
604 || strlen (dum->driver->function) > PAGE_SIZE)
606 return snprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
608 DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
610 /*-------------------------------------------------------------------------*/
613 * Driver registration/unregistration.
615 * This is basically hardware-specific; there's usually only one real USB
616 * device (not host) controller since that's how USB devices are intended
617 * to work. So most implementations of these api calls will rely on the
618 * fact that only one driver will ever bind to the hardware. But curious
619 * hardware can be built with discrete components, so the gadget API doesn't
620 * require that assumption.
622 * For this emulator, it might be convenient to create a usb slave device
623 * for each driver that registers: just add to a big root hub.
627 dummy_udc_release (struct device *dev)
629 struct dummy *dum = gadget_dev_to_dummy (dev);
631 complete (&dum->released);
635 dummy_hc_release (struct device *dev)
637 struct dummy *dum = dev_get_drvdata (dev);
639 complete (&dum->released);
643 dummy_register_udc (struct dummy *dum)
647 strcpy (dum->gadget.dev.bus_id, "udc");
648 dum->gadget.dev.parent = &dum->pdev.dev;
649 dum->gadget.dev.release = dummy_udc_release;
651 rc = device_register (&dum->gadget.dev);
653 device_create_file (&dum->gadget.dev, &dev_attr_function);
658 dummy_unregister_udc (struct dummy *dum)
660 device_remove_file (&dum->gadget.dev, &dev_attr_function);
661 init_completion (&dum->released);
662 device_unregister (&dum->gadget.dev);
663 wait_for_completion (&dum->released);
667 usb_gadget_register_driver (struct usb_gadget_driver *driver)
669 struct dummy *dum = the_controller;
676 if (!driver->bind || !driver->unbind || !driver->setup
677 || driver->speed == USB_SPEED_UNKNOWN)
681 * SLAVE side init ... the layer above hardware, which
682 * can't enumerate without help from the driver we're binding.
684 dum->gadget.name = gadget_name;
685 dum->gadget.ops = &dummy_ops;
686 dum->gadget.is_dualspeed = 1;
691 INIT_LIST_HEAD (&dum->gadget.ep_list);
692 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
693 struct dummy_ep *ep = &dum->ep [i];
697 ep->ep.name = ep_name [i];
698 ep->ep.ops = &dummy_ep_ops;
699 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
700 ep->halted = ep->already_seen = ep->setup_stage = 0;
701 ep->ep.maxpacket = ~0;
702 ep->last_io = jiffies;
703 ep->gadget = &dum->gadget;
705 INIT_LIST_HEAD (&ep->queue);
708 dum->gadget.ep0 = &dum->ep [0].ep;
709 dum->ep [0].ep.maxpacket = 64;
710 list_del_init (&dum->ep [0].ep.ep_list);
711 INIT_LIST_HEAD(&dum->fifo_req.queue);
713 dum->driver = driver;
714 dum->gadget.dev.driver = &driver->driver;
715 dev_dbg (hardware, "binding gadget driver '%s'\n", driver->driver.name);
716 if ((retval = driver->bind (&dum->gadget)) != 0) {
718 dum->gadget.dev.driver = 0;
722 // FIXME: Check these calls for errors and re-order
723 driver->driver.bus = dum->pdev.dev.bus;
724 driver_register (&driver->driver);
726 device_bind_driver (&dum->gadget.dev);
728 /* khubd will enumerate this in a while */
729 dum->port_status |= USB_PORT_STAT_CONNECTION
730 | (1 << USB_PORT_FEAT_C_CONNECTION);
733 EXPORT_SYMBOL (usb_gadget_register_driver);
735 /* caller must hold lock */
737 stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
741 /* prevent any more requests */
744 /* The timer is left running so that outstanding URBs can fail */
746 /* nuke any pending requests first, so driver i/o is quiesced */
747 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
750 /* driver now does any non-usb quiescing necessary */
752 spin_unlock (&dum->lock);
753 driver->disconnect (&dum->gadget);
754 spin_lock (&dum->lock);
759 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
761 struct dummy *dum = the_controller;
766 if (!driver || driver != dum->driver)
769 dev_dbg (hardware, "unregister gadget driver '%s'\n",
770 driver->driver.name);
772 spin_lock_irqsave (&dum->lock, flags);
773 stop_activity (dum, driver);
774 dum->port_status &= ~USB_PORT_STAT_CONNECTION;
775 dum->port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
776 spin_unlock_irqrestore (&dum->lock, flags);
778 driver->unbind (&dum->gadget);
781 device_release_driver (&dum->gadget.dev);
783 driver_unregister (&driver->driver);
787 EXPORT_SYMBOL (usb_gadget_unregister_driver);
791 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
795 EXPORT_SYMBOL (net2280_set_fifo_mode);
797 /*-------------------------------------------------------------------------*/
799 /* MASTER/HOST SIDE DRIVER
801 * this uses the hcd framework to hook up to host side drivers.
802 * its root hub will only have one device, otherwise it acts like
803 * a normal host controller.
805 * when urbs are queued, they're just stuck on a list that we
806 * scan in a timer callback. that callback connects writes from
807 * the host with reads from the device, and so on, based on the
811 static int dummy_urb_enqueue (
819 /* patch to usb_sg_init() is in 2.5.60 */
820 BUG_ON (!urb->transfer_buffer && urb->transfer_buffer_length);
822 dum = container_of (hcd, struct dummy, hcd);
823 spin_lock_irqsave (&dum->lock, flags);
826 dum->udev = urb->dev;
827 usb_get_dev (dum->udev);
828 } else if (unlikely (dum->udev != urb->dev))
829 dev_err (hardware, "usb_device address has changed!\n");
832 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
833 urb->error_count = 1; /* mark as a new urb */
835 /* kick the scheduler, it'll do the rest */
836 if (!timer_pending (&dum->timer))
837 mod_timer (&dum->timer, jiffies + 1);
839 spin_unlock_irqrestore (&dum->lock, flags);
843 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
845 /* giveback happens automatically in timer callback */
849 static void maybe_set_status (struct urb *urb, int status)
851 spin_lock (&urb->lock);
852 if (urb->status == -EINPROGRESS)
853 urb->status = status;
854 spin_unlock (&urb->lock);
857 /* transfer up to a frame's worth; caller must own lock */
859 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
861 struct dummy_request *req;
864 /* if there's no request queued, the device is NAKing; return */
865 list_for_each_entry (req, &ep->queue, queue) {
866 unsigned host_len, dev_len, len;
867 int is_short, to_host;
870 /* 1..N packets of ep->ep.maxpacket each ... the last one
871 * may be short (including zero length).
873 * writer can send a zlp explicitly (length 0) or implicitly
874 * (length mod maxpacket zero, and 'zero' flag); they always
877 host_len = urb->transfer_buffer_length - urb->actual_length;
878 dev_len = req->req.length - req->req.actual;
879 len = min (host_len, dev_len);
881 /* FIXME update emulated data toggle too */
883 to_host = usb_pipein (urb->pipe);
884 if (unlikely (len == 0))
889 /* not enough bandwidth left? */
890 if (limit < ep->ep.maxpacket && limit < len)
892 len = min (len, (unsigned) limit);
896 /* use an extra pass for the final short packet */
897 if (len > ep->ep.maxpacket) {
899 len -= (len % ep->ep.maxpacket);
901 is_short = (len % ep->ep.maxpacket) != 0;
903 /* else transfer packet(s) */
904 ubuf = urb->transfer_buffer + urb->actual_length;
905 rbuf = req->req.buf + req->req.actual;
907 memcpy (ubuf, rbuf, len);
909 memcpy (rbuf, ubuf, len);
910 ep->last_io = jiffies;
913 urb->actual_length += len;
914 req->req.actual += len;
917 /* short packets terminate, maybe with overflow/underflow.
918 * it's only really an error to write too much.
920 * partially filling a buffer optionally blocks queue advances
921 * (so completion handlers can clean up the queue) but we don't
922 * need to emulate such data-in-flight. so we only show part
923 * of the URB_SHORT_NOT_OK effect: completion status.
926 if (host_len == dev_len) {
928 maybe_set_status (urb, 0);
929 } else if (to_host) {
931 if (dev_len > host_len)
932 maybe_set_status (urb, -EOVERFLOW);
934 maybe_set_status (urb,
938 } else if (!to_host) {
939 maybe_set_status (urb, 0);
940 if (host_len > dev_len)
941 req->req.status = -EOVERFLOW;
946 /* many requests terminate without a short packet */
948 if (req->req.length == req->req.actual
951 if (urb->transfer_buffer_length == urb->actual_length
952 && !(urb->transfer_flags
953 & URB_ZERO_PACKET)) {
954 maybe_set_status (urb, 0);
958 /* device side completion --> continuable */
959 if (req->req.status != -EINPROGRESS) {
960 list_del_init (&req->queue);
962 spin_unlock (&dum->lock);
963 req->req.complete (&ep->ep, &req->req);
964 spin_lock (&dum->lock);
966 /* requests might have been unlinked... */
970 /* host side completion --> terminate */
971 if (urb->status != -EINPROGRESS)
974 /* rescan to continue with any other queued i/o */
981 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
983 int limit = ep->ep.maxpacket;
985 if (dum->gadget.speed == USB_SPEED_HIGH) {
988 /* high bandwidth mode */
989 tmp = ep->desc->wMaxPacketSize;
990 tmp = le16_to_cpu (tmp);
991 tmp = (tmp >> 11) & 0x03;
992 tmp *= 8 /* applies to entire frame */;
993 limit += limit * tmp;
998 #define is_active(dum) ((dum->port_status & \
999 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1000 USB_PORT_STAT_SUSPEND)) \
1001 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1003 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1007 if (!is_active (dum))
1009 if ((address & ~USB_DIR_IN) == 0)
1010 return &dum->ep [0];
1011 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1012 struct dummy_ep *ep = &dum->ep [i];
1016 if (ep->desc->bEndpointAddress == address)
1024 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1025 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1026 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1027 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1028 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1029 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1031 /* drive both sides of the transfers; looks like irq handlers to
1032 * both drivers except the callbacks aren't in_irq().
1034 static void dummy_timer (unsigned long _dum)
1036 struct dummy *dum = (struct dummy *) _dum;
1037 struct hcd_dev *hdev;
1038 struct list_head *entry, *tmp;
1039 unsigned long flags;
1043 /* simplistic model for one frame's bandwidth */
1044 switch (dum->gadget.speed) {
1046 total = 8/*bytes*/ * 12/*packets*/;
1048 case USB_SPEED_FULL:
1049 total = 64/*bytes*/ * 19/*packets*/;
1051 case USB_SPEED_HIGH:
1052 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1055 dev_err (hardware, "bogus device speed\n");
1059 /* FIXME if HZ != 1000 this will probably misbehave ... */
1061 /* look at each urb queued by the host side driver */
1062 spin_lock_irqsave (&dum->lock, flags);
1065 dev_err (hardware, "timer fired with no URBs pending?\n");
1066 spin_unlock_irqrestore (&dum->lock, flags);
1069 hdev = dum->udev->hcpriv;
1071 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1074 dum->ep [i].already_seen = 0;
1078 list_for_each_safe (entry, tmp, &hdev->urb_list) {
1080 struct dummy_request *req;
1082 struct dummy_ep *ep = 0;
1085 urb = list_entry (entry, struct urb, urb_list);
1086 if (urb->status != -EINPROGRESS) {
1087 /* likely it was just unlinked */
1090 type = usb_pipetype (urb->pipe);
1092 /* used up this frame's non-periodic bandwidth?
1093 * FIXME there's infinite bandwidth for control and
1094 * periodic transfers ... unrealistic.
1096 if (total <= 0 && type == PIPE_BULK)
1099 /* find the gadget's ep for this request (if configured) */
1100 address = usb_pipeendpoint (urb->pipe);
1101 if (usb_pipein (urb->pipe))
1102 address |= USB_DIR_IN;
1103 ep = find_endpoint(dum, address);
1105 /* set_configuration() disagreement */
1107 "no ep configured for urb %p\n",
1109 maybe_set_status (urb, -ETIMEDOUT);
1113 if (ep->already_seen)
1115 ep->already_seen = 1;
1116 if (ep == &dum->ep [0] && urb->error_count) {
1117 ep->setup_stage = 1; /* a new urb */
1118 urb->error_count = 0;
1120 if (ep->halted && !ep->setup_stage) {
1121 /* NOTE: must not be iso! */
1122 dev_dbg (hardware, "ep %s halted, urb %p\n",
1124 maybe_set_status (urb, -EPIPE);
1127 /* FIXME make sure both ends agree on maxpacket */
1129 /* handle control requests */
1130 if (ep == &dum->ep [0] && ep->setup_stage) {
1131 struct usb_ctrlrequest setup;
1133 struct dummy_ep *ep2;
1135 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1136 le16_to_cpus (&setup.wIndex);
1137 le16_to_cpus (&setup.wValue);
1138 le16_to_cpus (&setup.wLength);
1139 if (setup.wLength != urb->transfer_buffer_length) {
1140 maybe_set_status (urb, -EOVERFLOW);
1144 /* paranoia, in case of stale queued data */
1145 list_for_each_entry (req, &ep->queue, queue) {
1146 list_del_init (&req->queue);
1147 req->req.status = -EOVERFLOW;
1148 dev_dbg (hardware, "stale req = %p\n", req);
1150 spin_unlock (&dum->lock);
1151 req->req.complete (&ep->ep, &req->req);
1152 spin_lock (&dum->lock);
1153 ep->already_seen = 0;
1157 /* gadget driver never sees set_address or operations
1158 * on standard feature flags. some hardware doesn't
1161 ep->last_io = jiffies;
1162 ep->setup_stage = 0;
1164 switch (setup.bRequest) {
1165 case USB_REQ_SET_ADDRESS:
1166 if (setup.bRequestType != Dev_Request)
1168 dum->address = setup.wValue;
1169 maybe_set_status (urb, 0);
1170 dev_dbg (hardware, "set_address = %d\n",
1174 case USB_REQ_SET_FEATURE:
1175 if (setup.bRequestType == Dev_Request) {
1177 switch (setup.wValue) {
1178 case USB_DEVICE_REMOTE_WAKEUP:
1181 value = -EOPNOTSUPP;
1185 (1 << setup.wValue);
1186 maybe_set_status (urb, 0);
1189 } else if (setup.bRequestType == Ep_Request) {
1191 ep2 = find_endpoint (dum,
1194 value = -EOPNOTSUPP;
1199 maybe_set_status (urb, 0);
1202 case USB_REQ_CLEAR_FEATURE:
1203 if (setup.bRequestType == Dev_Request) {
1204 switch (setup.wValue) {
1205 case USB_DEVICE_REMOTE_WAKEUP:
1206 dum->devstatus &= ~(1 <<
1207 USB_DEVICE_REMOTE_WAKEUP);
1209 maybe_set_status (urb, 0);
1212 value = -EOPNOTSUPP;
1215 } else if (setup.bRequestType == Ep_Request) {
1217 ep2 = find_endpoint (dum,
1220 value = -EOPNOTSUPP;
1225 maybe_set_status (urb, 0);
1228 case USB_REQ_GET_STATUS:
1229 if (setup.bRequestType == Dev_InRequest
1230 || setup.bRequestType
1232 || setup.bRequestType
1237 // device: remote wakeup, selfpowered
1238 // interface: nothing
1240 buf = (char *)urb->transfer_buffer;
1241 if (urb->transfer_buffer_length > 0) {
1242 if (setup.bRequestType ==
1244 ep2 = find_endpoint (dum, setup.wIndex);
1246 value = -EOPNOTSUPP;
1249 buf [0] = ep2->halted;
1250 } else if (setup.bRequestType ==
1257 if (urb->transfer_buffer_length > 1)
1259 urb->actual_length = min (2,
1260 urb->transfer_buffer_length);
1262 maybe_set_status (urb, 0);
1267 /* gadget driver handles all other requests. block
1268 * until setup() returns; no reentrancy issues etc.
1271 spin_unlock (&dum->lock);
1272 value = dum->driver->setup (&dum->gadget,
1274 spin_lock (&dum->lock);
1277 /* no delays (max 64KB data stage) */
1279 goto treat_control_like_bulk;
1281 /* error, see below */
1285 if (value != -EOPNOTSUPP)
1289 maybe_set_status (urb, -EPIPE);
1290 urb->actual_length = 0;
1296 /* non-control requests */
1298 switch (usb_pipetype (urb->pipe)) {
1299 case PIPE_ISOCHRONOUS:
1300 /* FIXME is it urb->interval since the last xfer?
1301 * use urb->iso_frame_desc[i].
1302 * complete whether or not ep has requests queued.
1303 * report random errors, to debug drivers.
1305 limit = max (limit, periodic_bytes (dum, ep));
1306 maybe_set_status (urb, -ENOSYS);
1309 case PIPE_INTERRUPT:
1310 /* FIXME is it urb->interval since the last xfer?
1311 * this almost certainly polls too fast.
1313 limit = max (limit, periodic_bytes (dum, ep));
1316 // case PIPE_BULK: case PIPE_CONTROL:
1318 treat_control_like_bulk:
1319 ep->last_io = jiffies;
1320 total = transfer (dum, urb, ep, limit);
1324 /* incomplete transfer? */
1325 if (urb->status == -EINPROGRESS)
1331 ep->already_seen = ep->setup_stage = 0;
1333 spin_unlock (&dum->lock);
1334 usb_hcd_giveback_urb (&dum->hcd, urb, 0);
1335 spin_lock (&dum->lock);
1340 /* want a 1 msec delay here */
1341 if (!list_empty (&hdev->urb_list))
1342 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1344 usb_put_dev (dum->udev);
1348 spin_unlock_irqrestore (&dum->lock, flags);
1351 /*-------------------------------------------------------------------------*/
1353 #define PORT_C_MASK \
1354 ((1 << USB_PORT_FEAT_C_CONNECTION) \
1355 | (1 << USB_PORT_FEAT_C_ENABLE) \
1356 | (1 << USB_PORT_FEAT_C_SUSPEND) \
1357 | (1 << USB_PORT_FEAT_C_OVER_CURRENT) \
1358 | (1 << USB_PORT_FEAT_C_RESET))
1360 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1363 unsigned long flags;
1366 dum = container_of (hcd, struct dummy, hcd);
1368 spin_lock_irqsave (&dum->lock, flags);
1369 if (!(dum->port_status & PORT_C_MASK))
1373 dev_dbg (hardware, "port status 0x%08x has changes\n",
1377 spin_unlock_irqrestore (&dum->lock, flags);
1382 hub_descriptor (struct usb_hub_descriptor *desc)
1384 memset (desc, 0, sizeof *desc);
1385 desc->bDescriptorType = 0x29;
1386 desc->bDescLength = 9;
1387 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1388 desc->bNbrPorts = 1;
1389 desc->bitmap [0] = 0xff;
1390 desc->bitmap [1] = 0xff;
1393 static int dummy_hub_control (
1394 struct usb_hcd *hcd,
1403 unsigned long flags;
1405 dum = container_of (hcd, struct dummy, hcd);
1406 spin_lock_irqsave (&dum->lock, flags);
1408 case ClearHubFeature:
1410 case ClearPortFeature:
1412 case USB_PORT_FEAT_SUSPEND:
1413 /* 20msec resume signaling */
1415 dum->re_timeout = jiffies + ((HZ * 20)/1000);
1417 case USB_PORT_FEAT_POWER:
1418 dum->port_status = 0;
1420 stop_activity(dum, dum->driver);
1423 dum->port_status &= ~(1 << wValue);
1426 case GetHubDescriptor:
1427 hub_descriptor ((struct usb_hub_descriptor *) buf);
1430 *(u32 *) buf = __constant_cpu_to_le32 (0);
1436 /* whoever resets or resumes must GetPortStatus to
1439 if (dum->resuming && time_after (jiffies, dum->re_timeout)) {
1440 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
1441 dum->port_status &= ~(1 << USB_PORT_FEAT_SUSPEND);
1443 dum->re_timeout = 0;
1444 if (dum->driver->resume) {
1445 spin_unlock (&dum->lock);
1446 dum->driver->resume (&dum->gadget);
1447 spin_lock (&dum->lock);
1450 if ((dum->port_status & (1 << USB_PORT_FEAT_RESET)) != 0
1451 && time_after (jiffies, dum->re_timeout)) {
1452 dum->port_status |= (1 << USB_PORT_FEAT_C_RESET);
1453 dum->port_status &= ~(1 << USB_PORT_FEAT_RESET);
1454 dum->re_timeout = 0;
1456 dum->port_status |= USB_PORT_STAT_ENABLE;
1457 /* give it the best speed we agree on */
1458 dum->gadget.speed = dum->driver->speed;
1459 dum->gadget.ep0->maxpacket = 64;
1460 switch (dum->gadget.speed) {
1461 case USB_SPEED_HIGH:
1463 USB_PORT_STAT_HIGH_SPEED;
1466 dum->gadget.ep0->maxpacket = 8;
1468 USB_PORT_STAT_LOW_SPEED;
1471 dum->gadget.speed = USB_SPEED_FULL;
1476 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1477 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1482 case SetPortFeature:
1484 case USB_PORT_FEAT_SUSPEND:
1485 dum->port_status |= (1 << USB_PORT_FEAT_SUSPEND);
1486 if (dum->driver->suspend) {
1487 spin_unlock (&dum->lock);
1488 dum->driver->suspend (&dum->gadget);
1489 spin_lock (&dum->lock);
1492 case USB_PORT_FEAT_RESET:
1493 /* if it's already running, disconnect first */
1494 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1495 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1496 | USB_PORT_STAT_LOW_SPEED
1497 | USB_PORT_STAT_HIGH_SPEED);
1499 dev_dbg (hardware, "disconnect\n");
1500 stop_activity (dum, dum->driver);
1503 /* FIXME test that code path! */
1505 /* 50msec reset signaling */
1506 dum->re_timeout = jiffies + ((HZ * 50)/1000);
1509 dum->port_status |= (1 << wValue);
1515 "hub control req%04x v%04x i%04x l%d\n",
1516 typeReq, wValue, wIndex, wLength);
1518 /* "protocol stall" on error */
1521 spin_unlock_irqrestore (&dum->lock, flags);
1526 /*-------------------------------------------------------------------------*/
1528 static struct usb_hcd *dummy_alloc (void)
1532 dum = kmalloc (sizeof *dum, SLAB_KERNEL);
1535 memset (dum, 0, sizeof *dum);
1539 static void dummy_free (struct usb_hcd *hcd)
1543 dum = container_of (hcd, struct dummy, hcd);
1544 WARN_ON (dum->driver != 0);
1548 /*-------------------------------------------------------------------------*/
1550 static inline ssize_t
1551 show_urb (char *buf, size_t size, struct urb *urb)
1553 int ep = usb_pipeendpoint (urb->pipe);
1555 return snprintf (buf, size,
1556 "urb/%p %s ep%d%s%s len %d/%d\n",
1559 switch (urb->dev->speed) {
1560 case USB_SPEED_LOW: s = "ls"; break;
1561 case USB_SPEED_FULL: s = "fs"; break;
1562 case USB_SPEED_HIGH: s = "hs"; break;
1563 default: s = "?"; break;
1565 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1567 switch (usb_pipetype (urb->pipe)) { \
1568 case PIPE_CONTROL: s = ""; break; \
1569 case PIPE_BULK: s = "-bulk"; break; \
1570 case PIPE_INTERRUPT: s = "-int"; break; \
1571 default: s = "-iso"; break; \
1573 urb->actual_length, urb->transfer_buffer_length);
1577 show_urbs (struct device *dev, char *buf)
1579 struct dummy *dum = dev_get_drvdata(dev);
1582 unsigned long flags;
1583 struct hcd_dev *hdev;
1585 spin_lock_irqsave (&dum->lock, flags);
1587 hdev = dum->udev->hcpriv;
1588 list_for_each_entry (urb, &hdev->urb_list, urb_list) {
1591 temp = show_urb (buf, PAGE_SIZE - size, urb);
1596 spin_unlock_irqrestore (&dum->lock, flags);
1600 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1603 static const struct hc_driver dummy_hcd;
1605 static int dummy_start (struct usb_hcd *hcd)
1608 struct usb_bus *bus;
1609 struct usb_device *root;
1612 dum = container_of (hcd, struct dummy, hcd);
1615 * MASTER side init ... we emulate a root hub that'll only ever
1616 * talk to one device (the slave side). Also appears in sysfs,
1617 * just like more familiar pci-based HCDs.
1619 spin_lock_init (&dum->lock);
1621 retval = driver_register (&dummy_driver);
1625 dum->pdev.name = "hc";
1626 dum->pdev.dev.driver = &dummy_driver;
1627 dev_set_drvdata(&dum->pdev.dev, dum);
1628 dum->pdev.dev.release = dummy_hc_release;
1629 retval = platform_device_register (&dum->pdev);
1631 driver_unregister (&dummy_driver);
1634 dev_info (&dum->pdev.dev, "%s, driver " DRIVER_VERSION "\n",
1637 hcd->self.controller = &dum->pdev.dev;
1639 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1640 device_create_file (hcd->self.controller, &dev_attr_urbs);
1642 init_timer (&dum->timer);
1643 dum->timer.function = dummy_timer;
1644 dum->timer.data = (unsigned long) dum;
1646 /* root hub will appear as another device */
1647 dum->hcd.driver = (struct hc_driver *) &dummy_hcd;
1648 dum->hcd.description = dummy_hcd.description;
1649 dum->hcd.product_desc = "Dummy host controller";
1651 bus = hcd_to_bus (&dum->hcd);
1652 bus->bus_name = dum->pdev.dev.bus_id;
1654 bus->op = &usb_hcd_operations;
1655 bus->hcpriv = &dum->hcd;
1657 /* FIXME don't require the pci-based buffer/alloc impls;
1658 * the "generic dma" implementation still requires them,
1659 * it's not very generic yet.
1661 if ((retval = hcd_buffer_create (&dum->hcd)) != 0) {
1663 init_completion (&dum->released);
1664 platform_device_unregister (&dum->pdev);
1665 wait_for_completion (&dum->released);
1666 driver_unregister (&dummy_driver);
1670 INIT_LIST_HEAD (&hcd->dev_list);
1671 usb_register_bus (bus);
1673 root = usb_alloc_dev (0, bus, 0);
1677 hcd_buffer_destroy (&dum->hcd);
1678 usb_deregister_bus (bus);
1682 /* root hub enters addressed state... */
1683 dum->hcd.state = USB_STATE_RUNNING;
1684 root->speed = USB_SPEED_HIGH;
1686 /* ...then configured, so khubd sees us. */
1687 if ((retval = hcd_register_root (root, &dum->hcd)) != 0) {
1690 dum->hcd.state = USB_STATE_QUIESCING;
1694 /* only show a low-power port: just 8mA */
1695 hub_set_power_budget (root, 8);
1699 if ((retval = dummy_register_udc (dum)) != 0) {
1701 usb_disconnect (&bus->root_hub);
1707 static void dummy_stop (struct usb_hcd *hcd)
1710 struct usb_bus *bus;
1712 dum = container_of (hcd, struct dummy, hcd);
1717 usb_gadget_unregister_driver (dum->driver);
1718 dummy_unregister_udc (dum);
1720 bus = hcd_to_bus (&dum->hcd);
1721 hcd->state = USB_STATE_QUIESCING;
1722 dev_dbg (hardware, "remove root hub\n");
1723 usb_disconnect (&bus->root_hub);
1725 hcd_buffer_destroy (&dum->hcd);
1726 usb_deregister_bus (bus);
1728 dev_info (hardware, "stopped\n");
1730 device_remove_file (hcd->self.controller, &dev_attr_urbs);
1731 init_completion (&dum->released);
1732 platform_device_unregister (&dum->pdev);
1733 wait_for_completion (&dum->released);
1735 driver_unregister (&dummy_driver);
1738 /*-------------------------------------------------------------------------*/
1740 static int dummy_h_get_frame (struct usb_hcd *hcd)
1742 return dummy_g_get_frame (0);
1745 static const struct hc_driver dummy_hcd = {
1746 .description = (char *) driver_name,
1749 .start = dummy_start,
1752 .hcd_alloc = dummy_alloc,
1753 .hcd_free = dummy_free,
1755 .urb_enqueue = dummy_urb_enqueue,
1756 .urb_dequeue = dummy_urb_dequeue,
1758 .get_frame_number = dummy_h_get_frame,
1760 .hub_status_data = dummy_hub_status,
1761 .hub_control = dummy_hub_control,
1764 /*-------------------------------------------------------------------------*/
1766 static int __init init (void)
1768 struct usb_hcd *hcd;
1771 if (usb_disabled ())
1773 if ((hcd = dummy_alloc ()) == 0)
1776 the_controller = container_of (hcd, struct dummy, hcd);
1777 value = dummy_start (hcd);
1787 static void __exit cleanup (void)
1789 dummy_stop (&the_controller->hcd);
1790 dummy_free (&the_controller->hcd);
1793 module_exit (cleanup);