Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / dummy_hcd.c
blob8ef8a9cd9ac4fff558b3874bb298cd7e75c9c1b4
1 /*
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.
37 #define DEBUG
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>
58 #include <asm/io.h>
59 #include <asm/irq.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 */
82 struct dummy_ep {
83 struct list_head queue;
84 unsigned long last_io; /* jiffies timestamp */
85 struct usb_gadget *gadget;
86 const struct usb_endpoint_descriptor *desc;
87 struct usb_ep ep;
88 unsigned halted : 1;
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",
137 "ep15in-int",
139 /* or like sa1100: two fixed function endpoints */
140 "ep1out-bulk", "ep2in-bulk",
142 #define DUMMY_ENDPOINTS (sizeof(ep_name)/sizeof(char *))
144 #define FIFO_SIZE 64
146 struct urbp {
147 struct urb *urb;
148 struct list_head urbp_list;
151 struct dummy {
152 spinlock_t lock;
155 * SLAVE/GADGET side support
157 struct dummy_ep ep [DUMMY_ENDPOINTS];
158 int address;
159 struct usb_gadget gadget;
160 struct usb_gadget_driver *driver;
161 struct dummy_request fifo_req;
162 u8 fifo_buf [FIFO_SIZE];
163 u16 devstatus;
166 * MASTER/HOST side support
168 struct timer_list timer;
169 u32 port_status;
170 unsigned resuming:1;
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)
234 static int
235 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
237 struct dummy *dum;
238 struct dummy_ep *ep;
239 unsigned max;
240 int retval;
242 ep = usb_ep_to_dummy_ep (_ep);
243 if (!_ep || !desc || ep->desc || _ep->name == ep0name
244 || desc->bDescriptorType != USB_DT_ENDPOINT)
245 return -EINVAL;
246 dum = ep_to_dummy (ep);
247 if (!dum->driver || !is_enabled (dum))
248 return -ESHUTDOWN;
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.)
259 retval = -EINVAL;
260 switch (desc->bmAttributes & 0x03) {
261 case USB_ENDPOINT_XFER_BULK:
262 if (strstr (ep->ep.name, "-iso")
263 || strstr (ep->ep.name, "-int")) {
264 goto done;
266 switch (dum->gadget.speed) {
267 case USB_SPEED_HIGH:
268 if (max == 512)
269 break;
270 /* conserve return statements */
271 default:
272 switch (max) {
273 case 8: case 16: case 32: case 64:
274 /* we'll fake any legal size */
275 break;
276 default:
277 case USB_SPEED_LOW:
278 goto done;
281 break;
282 case USB_ENDPOINT_XFER_INT:
283 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
284 goto done;
285 /* real hardware might not handle all packet sizes */
286 switch (dum->gadget.speed) {
287 case USB_SPEED_HIGH:
288 if (max <= 1024)
289 break;
290 /* save a return statement */
291 case USB_SPEED_FULL:
292 if (max <= 64)
293 break;
294 /* save a return statement */
295 default:
296 if (max <= 8)
297 break;
298 goto done;
300 break;
301 case USB_ENDPOINT_XFER_ISOC:
302 if (strstr (ep->ep.name, "-bulk")
303 || strstr (ep->ep.name, "-int"))
304 goto done;
305 /* real hardware might not handle all packet sizes */
306 switch (dum->gadget.speed) {
307 case USB_SPEED_HIGH:
308 if (max <= 1024)
309 break;
310 /* save a return statement */
311 case USB_SPEED_FULL:
312 if (max <= 1023)
313 break;
314 /* save a return statement */
315 default:
316 goto done;
318 break;
319 default:
320 /* few chips support control except on ep0 */
321 goto done;
324 _ep->maxpacket = max;
325 ep->desc = desc;
327 dev_dbg (dummy_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
328 _ep->name,
329 desc->bEndpointAddress & 0x0f,
330 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
331 ({ char *val;
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;
337 }; val; }),
338 max);
340 /* at this point real hardware should be NAKing transfers
341 * to that endpoint, until a buffer is queued to it.
343 retval = 0;
344 done:
345 return retval;
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)
366 struct dummy_ep *ep;
367 struct dummy *dum;
368 unsigned long flags;
369 int retval;
371 ep = usb_ep_to_dummy_ep (_ep);
372 if (!_ep || !ep->desc || _ep->name == ep0name)
373 return -EINVAL;
374 dum = ep_to_dummy (ep);
376 spin_lock_irqsave (&dum->lock, flags);
377 ep->desc = NULL;
378 retval = 0;
379 nuke (dum, ep);
380 spin_unlock_irqrestore (&dum->lock, flags);
382 dev_dbg (dummy_dev(dum), "disabled %s\n", _ep->name);
383 return retval;
386 static struct usb_request *
387 dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
389 struct dummy_ep *ep;
390 struct dummy_request *req;
392 if (!_ep)
393 return NULL;
394 ep = usb_ep_to_dummy_ep (_ep);
396 req = kmalloc (sizeof *req, mem_flags);
397 if (!req)
398 return NULL;
399 memset (req, 0, sizeof *req);
400 INIT_LIST_HEAD (&req->queue);
401 return &req->req;
404 static void
405 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
407 struct dummy_ep *ep;
408 struct dummy_request *req;
410 ep = usb_ep_to_dummy_ep (_ep);
411 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
412 return;
414 req = usb_request_to_dummy_request (_req);
415 WARN_ON (!list_empty (&req->queue));
416 kfree (req);
419 static void *
420 dummy_alloc_buffer (
421 struct usb_ep *_ep,
422 unsigned bytes,
423 dma_addr_t *dma,
424 int mem_flags
426 char *retval;
427 struct dummy_ep *ep;
428 struct dummy *dum;
430 ep = usb_ep_to_dummy_ep (_ep);
431 dum = ep_to_dummy (ep);
433 if (!dum->driver)
434 return NULL;
435 retval = kmalloc (bytes, mem_flags);
436 *dma = (dma_addr_t) retval;
437 return retval;
440 static void
441 dummy_free_buffer (
442 struct usb_ep *_ep,
443 void *buf,
444 dma_addr_t dma,
445 unsigned bytes
447 if (bytes)
448 kfree (buf);
451 static void
452 fifo_complete (struct usb_ep *ep, struct usb_request *req)
456 static int
457 dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
459 struct dummy_ep *ep;
460 struct dummy_request *req;
461 struct dummy *dum;
462 unsigned long flags;
464 req = usb_request_to_dummy_request (_req);
465 if (!_req || !list_empty (&req->queue) || !_req->complete)
466 return -EINVAL;
468 ep = usb_ep_to_dummy_ep (_ep);
469 if (!_ep || (!ep->desc && _ep->name != ep0name))
470 return -EINVAL;
472 dum = ep_to_dummy (ep);
473 if (!dum->driver || !is_enabled (dum))
474 return -ESHUTDOWN;
476 #if 0
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);
479 #endif
481 _req->status = -EINPROGRESS;
482 _req->actual = 0;
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;
491 req->req = *_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;
499 _req->status = 0;
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.
509 return 0;
512 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
514 struct dummy_ep *ep;
515 struct dummy *dum;
516 int retval = -EINVAL;
517 unsigned long flags;
518 struct dummy_request *req = NULL;
520 if (!_ep || !_req)
521 return retval;
522 ep = usb_ep_to_dummy_ep (_ep);
523 dum = ep_to_dummy (ep);
525 if (!dum->driver)
526 return -ESHUTDOWN;
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;
533 retval = 0;
534 break;
537 spin_unlock_irqrestore (&dum->lock, flags);
539 if (retval == 0) {
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);
545 return retval;
548 static int
549 dummy_set_halt (struct usb_ep *_ep, int value)
551 struct dummy_ep *ep;
552 struct dummy *dum;
554 if (!_ep)
555 return -EINVAL;
556 ep = usb_ep_to_dummy_ep (_ep);
557 dum = ep_to_dummy (ep);
558 if (!dum->driver)
559 return -ESHUTDOWN;
560 if (!value)
561 ep->halted = 0;
562 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
563 !list_empty (&ep->queue))
564 return -EAGAIN;
565 else
566 ep->halted = 1;
567 /* FIXME clear emulated data toggle too */
568 return 0;
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)
593 struct timeval tv;
595 do_gettimeofday (&tv);
596 return tv.tv_usec / 1000;
599 static int dummy_wakeup (struct usb_gadget *_gadget)
601 struct dummy *dum;
603 dum = gadget_to_dummy (_gadget);
604 if ((dum->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) == 0
605 || !(dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)))
606 return -EINVAL;
608 /* hub notices our request, issues downstream resume, etc */
609 dum->resuming = 1;
610 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
611 return 0;
614 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
616 struct dummy *dum;
618 dum = gadget_to_dummy (_gadget);
619 if (value)
620 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
621 else
622 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
623 return 0;
626 static const struct usb_gadget_ops dummy_ops = {
627 .get_frame = dummy_g_get_frame,
628 .wakeup = dummy_wakeup,
629 .set_selfpowered = dummy_set_selfpowered,
632 /*-------------------------------------------------------------------------*/
634 /* "function" sysfs attribute */
635 static ssize_t
636 show_function (struct device *dev, char *buf)
638 struct dummy *dum = gadget_dev_to_dummy (dev);
640 if (!dum->driver || !dum->driver->function)
641 return 0;
642 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
644 DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
646 /*-------------------------------------------------------------------------*/
649 * Driver registration/unregistration.
651 * This is basically hardware-specific; there's usually only one real USB
652 * device (not host) controller since that's how USB devices are intended
653 * to work. So most implementations of these api calls will rely on the
654 * fact that only one driver will ever bind to the hardware. But curious
655 * hardware can be built with discrete components, so the gadget API doesn't
656 * require that assumption.
658 * For this emulator, it might be convenient to create a usb slave device
659 * for each driver that registers: just add to a big root hub.
662 static void
663 dummy_udc_release (struct device *dev)
667 static void
668 dummy_pdev_release (struct device *dev)
672 static int
673 dummy_register_udc (struct dummy *dum)
675 int rc;
677 strcpy (dum->gadget.dev.bus_id, "udc");
678 dum->gadget.dev.parent = dummy_dev(dum);
679 dum->gadget.dev.release = dummy_udc_release;
681 rc = device_register (&dum->gadget.dev);
682 if (rc == 0)
683 device_create_file (&dum->gadget.dev, &dev_attr_function);
684 return rc;
687 static void
688 dummy_unregister_udc (struct dummy *dum)
690 device_remove_file (&dum->gadget.dev, &dev_attr_function);
691 device_unregister (&dum->gadget.dev);
695 usb_gadget_register_driver (struct usb_gadget_driver *driver)
697 struct dummy *dum = the_controller;
698 int retval, i;
700 if (!dum)
701 return -EINVAL;
702 if (dum->driver)
703 return -EBUSY;
704 if (!driver->bind || !driver->unbind || !driver->setup
705 || driver->speed == USB_SPEED_UNKNOWN)
706 return -EINVAL;
709 * SLAVE side init ... the layer above hardware, which
710 * can't enumerate without help from the driver we're binding.
712 dum->gadget.name = gadget_name;
713 dum->gadget.ops = &dummy_ops;
714 dum->gadget.is_dualspeed = 1;
716 dum->devstatus = 0;
717 dum->resuming = 0;
719 INIT_LIST_HEAD (&dum->gadget.ep_list);
720 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
721 struct dummy_ep *ep = &dum->ep [i];
723 if (!ep_name [i])
724 break;
725 ep->ep.name = ep_name [i];
726 ep->ep.ops = &dummy_ep_ops;
727 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
728 ep->halted = ep->already_seen = ep->setup_stage = 0;
729 ep->ep.maxpacket = ~0;
730 ep->last_io = jiffies;
731 ep->gadget = &dum->gadget;
732 ep->desc = NULL;
733 INIT_LIST_HEAD (&ep->queue);
736 dum->gadget.ep0 = &dum->ep [0].ep;
737 dum->ep [0].ep.maxpacket = 64;
738 list_del_init (&dum->ep [0].ep.ep_list);
739 INIT_LIST_HEAD(&dum->fifo_req.queue);
741 dum->driver = driver;
742 dum->gadget.dev.driver = &driver->driver;
743 dev_dbg (dummy_dev(dum), "binding gadget driver '%s'\n",
744 driver->driver.name);
745 if ((retval = driver->bind (&dum->gadget)) != 0) {
746 dum->driver = NULL;
747 dum->gadget.dev.driver = NULL;
748 return retval;
751 // FIXME: Check these calls for errors and re-order
752 driver->driver.bus = dum->gadget.dev.parent->bus;
753 driver_register (&driver->driver);
755 device_bind_driver (&dum->gadget.dev);
757 /* khubd will enumerate this in a while */
758 dum->port_status |= USB_PORT_STAT_CONNECTION
759 | (1 << USB_PORT_FEAT_C_CONNECTION);
760 return 0;
762 EXPORT_SYMBOL (usb_gadget_register_driver);
764 /* caller must hold lock */
765 static void
766 stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
768 struct dummy_ep *ep;
770 /* prevent any more requests */
771 dum->address = 0;
773 /* The timer is left running so that outstanding URBs can fail */
775 /* nuke any pending requests first, so driver i/o is quiesced */
776 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
777 nuke (dum, ep);
779 /* driver now does any non-usb quiescing necessary */
780 if (driver) {
781 spin_unlock (&dum->lock);
782 driver->disconnect (&dum->gadget);
783 spin_lock (&dum->lock);
788 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
790 struct dummy *dum = the_controller;
791 unsigned long flags;
793 if (!dum)
794 return -ENODEV;
795 if (!driver || driver != dum->driver)
796 return -EINVAL;
798 dev_dbg (dummy_dev(dum), "unregister gadget driver '%s'\n",
799 driver->driver.name);
801 spin_lock_irqsave (&dum->lock, flags);
802 stop_activity (dum, driver);
803 dum->port_status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE |
804 USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
805 dum->port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
806 spin_unlock_irqrestore (&dum->lock, flags);
808 driver->unbind (&dum->gadget);
809 dum->driver = NULL;
811 device_release_driver (&dum->gadget.dev);
813 driver_unregister (&driver->driver);
815 return 0;
817 EXPORT_SYMBOL (usb_gadget_unregister_driver);
819 #undef is_enabled
821 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
823 return -ENOSYS;
825 EXPORT_SYMBOL (net2280_set_fifo_mode);
827 /*-------------------------------------------------------------------------*/
829 /* MASTER/HOST SIDE DRIVER
831 * this uses the hcd framework to hook up to host side drivers.
832 * its root hub will only have one device, otherwise it acts like
833 * a normal host controller.
835 * when urbs are queued, they're just stuck on a list that we
836 * scan in a timer callback. that callback connects writes from
837 * the host with reads from the device, and so on, based on the
838 * usb 2.0 rules.
841 static int dummy_urb_enqueue (
842 struct usb_hcd *hcd,
843 struct usb_host_endpoint *ep,
844 struct urb *urb,
845 int mem_flags
847 struct dummy *dum;
848 struct urbp *urbp;
849 unsigned long flags;
851 if (!urb->transfer_buffer && urb->transfer_buffer_length)
852 return -EINVAL;
854 urbp = kmalloc (sizeof *urbp, mem_flags);
855 if (!urbp)
856 return -ENOMEM;
857 urbp->urb = urb;
859 dum = hcd_to_dummy (hcd);
860 spin_lock_irqsave (&dum->lock, flags);
862 if (!dum->udev) {
863 dum->udev = urb->dev;
864 usb_get_dev (dum->udev);
865 } else if (unlikely (dum->udev != urb->dev))
866 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
868 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
869 urb->hcpriv = urbp;
870 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
871 urb->error_count = 1; /* mark as a new urb */
873 /* kick the scheduler, it'll do the rest */
874 if (!timer_pending (&dum->timer))
875 mod_timer (&dum->timer, jiffies + 1);
877 spin_unlock_irqrestore (&dum->lock, flags);
878 return 0;
881 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
883 /* giveback happens automatically in timer callback */
884 return 0;
887 static void maybe_set_status (struct urb *urb, int status)
889 spin_lock (&urb->lock);
890 if (urb->status == -EINPROGRESS)
891 urb->status = status;
892 spin_unlock (&urb->lock);
895 /* transfer up to a frame's worth; caller must own lock */
896 static int
897 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
899 struct dummy_request *req;
901 top:
902 /* if there's no request queued, the device is NAKing; return */
903 list_for_each_entry (req, &ep->queue, queue) {
904 unsigned host_len, dev_len, len;
905 int is_short, to_host;
906 int rescan = 0;
908 /* 1..N packets of ep->ep.maxpacket each ... the last one
909 * may be short (including zero length).
911 * writer can send a zlp explicitly (length 0) or implicitly
912 * (length mod maxpacket zero, and 'zero' flag); they always
913 * terminate reads.
915 host_len = urb->transfer_buffer_length - urb->actual_length;
916 dev_len = req->req.length - req->req.actual;
917 len = min (host_len, dev_len);
919 /* FIXME update emulated data toggle too */
921 to_host = usb_pipein (urb->pipe);
922 if (unlikely (len == 0))
923 is_short = 1;
924 else {
925 char *ubuf, *rbuf;
927 /* not enough bandwidth left? */
928 if (limit < ep->ep.maxpacket && limit < len)
929 break;
930 len = min (len, (unsigned) limit);
931 if (len == 0)
932 break;
934 /* use an extra pass for the final short packet */
935 if (len > ep->ep.maxpacket) {
936 rescan = 1;
937 len -= (len % ep->ep.maxpacket);
939 is_short = (len % ep->ep.maxpacket) != 0;
941 /* else transfer packet(s) */
942 ubuf = urb->transfer_buffer + urb->actual_length;
943 rbuf = req->req.buf + req->req.actual;
944 if (to_host)
945 memcpy (ubuf, rbuf, len);
946 else
947 memcpy (rbuf, ubuf, len);
948 ep->last_io = jiffies;
950 limit -= len;
951 urb->actual_length += len;
952 req->req.actual += len;
955 /* short packets terminate, maybe with overflow/underflow.
956 * it's only really an error to write too much.
958 * partially filling a buffer optionally blocks queue advances
959 * (so completion handlers can clean up the queue) but we don't
960 * need to emulate such data-in-flight. so we only show part
961 * of the URB_SHORT_NOT_OK effect: completion status.
963 if (is_short) {
964 if (host_len == dev_len) {
965 req->req.status = 0;
966 maybe_set_status (urb, 0);
967 } else if (to_host) {
968 req->req.status = 0;
969 if (dev_len > host_len)
970 maybe_set_status (urb, -EOVERFLOW);
971 else
972 maybe_set_status (urb,
973 (urb->transfer_flags
974 & URB_SHORT_NOT_OK)
975 ? -EREMOTEIO : 0);
976 } else if (!to_host) {
977 maybe_set_status (urb, 0);
978 if (host_len > dev_len)
979 req->req.status = -EOVERFLOW;
980 else
981 req->req.status = 0;
984 /* many requests terminate without a short packet */
985 } else {
986 if (req->req.length == req->req.actual
987 && !req->req.zero)
988 req->req.status = 0;
989 if (urb->transfer_buffer_length == urb->actual_length
990 && !(urb->transfer_flags
991 & URB_ZERO_PACKET)) {
992 maybe_set_status (urb, 0);
996 /* device side completion --> continuable */
997 if (req->req.status != -EINPROGRESS) {
998 list_del_init (&req->queue);
1000 spin_unlock (&dum->lock);
1001 req->req.complete (&ep->ep, &req->req);
1002 spin_lock (&dum->lock);
1004 /* requests might have been unlinked... */
1005 rescan = 1;
1008 /* host side completion --> terminate */
1009 if (urb->status != -EINPROGRESS)
1010 break;
1012 /* rescan to continue with any other queued i/o */
1013 if (rescan)
1014 goto top;
1016 return limit;
1019 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1021 int limit = ep->ep.maxpacket;
1023 if (dum->gadget.speed == USB_SPEED_HIGH) {
1024 int tmp;
1026 /* high bandwidth mode */
1027 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1028 tmp = le16_to_cpu (tmp);
1029 tmp = (tmp >> 11) & 0x03;
1030 tmp *= 8 /* applies to entire frame */;
1031 limit += limit * tmp;
1033 return limit;
1036 #define is_active(dum) ((dum->port_status & \
1037 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1038 USB_PORT_STAT_SUSPEND)) \
1039 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1041 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1043 int i;
1045 if (!is_active (dum))
1046 return NULL;
1047 if ((address & ~USB_DIR_IN) == 0)
1048 return &dum->ep [0];
1049 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1050 struct dummy_ep *ep = &dum->ep [i];
1052 if (!ep->desc)
1053 continue;
1054 if (ep->desc->bEndpointAddress == address)
1055 return ep;
1057 return NULL;
1060 #undef is_active
1062 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1063 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1064 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1065 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1066 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1067 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1069 /* drive both sides of the transfers; looks like irq handlers to
1070 * both drivers except the callbacks aren't in_irq().
1072 static void dummy_timer (unsigned long _dum)
1074 struct dummy *dum = (struct dummy *) _dum;
1075 struct urbp *urbp, *tmp;
1076 unsigned long flags;
1077 int limit, total;
1078 int i;
1080 /* simplistic model for one frame's bandwidth */
1081 switch (dum->gadget.speed) {
1082 case USB_SPEED_LOW:
1083 total = 8/*bytes*/ * 12/*packets*/;
1084 break;
1085 case USB_SPEED_FULL:
1086 total = 64/*bytes*/ * 19/*packets*/;
1087 break;
1088 case USB_SPEED_HIGH:
1089 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1090 break;
1091 default:
1092 dev_err (dummy_dev(dum), "bogus device speed\n");
1093 return;
1096 /* FIXME if HZ != 1000 this will probably misbehave ... */
1098 /* look at each urb queued by the host side driver */
1099 spin_lock_irqsave (&dum->lock, flags);
1101 if (!dum->udev) {
1102 dev_err (dummy_dev(dum),
1103 "timer fired with no URBs pending?\n");
1104 spin_unlock_irqrestore (&dum->lock, flags);
1105 return;
1108 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1109 if (!ep_name [i])
1110 break;
1111 dum->ep [i].already_seen = 0;
1114 restart:
1115 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1116 struct urb *urb;
1117 struct dummy_request *req;
1118 u8 address;
1119 struct dummy_ep *ep = NULL;
1120 int type;
1122 urb = urbp->urb;
1123 if (urb->status != -EINPROGRESS) {
1124 /* likely it was just unlinked */
1125 goto return_urb;
1127 type = usb_pipetype (urb->pipe);
1129 /* used up this frame's non-periodic bandwidth?
1130 * FIXME there's infinite bandwidth for control and
1131 * periodic transfers ... unrealistic.
1133 if (total <= 0 && type == PIPE_BULK)
1134 continue;
1136 /* find the gadget's ep for this request (if configured) */
1137 address = usb_pipeendpoint (urb->pipe);
1138 if (usb_pipein (urb->pipe))
1139 address |= USB_DIR_IN;
1140 ep = find_endpoint(dum, address);
1141 if (!ep) {
1142 /* set_configuration() disagreement */
1143 dev_dbg (dummy_dev(dum),
1144 "no ep configured for urb %p\n",
1145 urb);
1146 maybe_set_status (urb, -EPROTO);
1147 goto return_urb;
1150 if (ep->already_seen)
1151 continue;
1152 ep->already_seen = 1;
1153 if (ep == &dum->ep [0] && urb->error_count) {
1154 ep->setup_stage = 1; /* a new urb */
1155 urb->error_count = 0;
1157 if (ep->halted && !ep->setup_stage) {
1158 /* NOTE: must not be iso! */
1159 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1160 ep->ep.name, urb);
1161 maybe_set_status (urb, -EPIPE);
1162 goto return_urb;
1164 /* FIXME make sure both ends agree on maxpacket */
1166 /* handle control requests */
1167 if (ep == &dum->ep [0] && ep->setup_stage) {
1168 struct usb_ctrlrequest setup;
1169 int value = 1;
1170 struct dummy_ep *ep2;
1172 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1173 le16_to_cpus (&setup.wIndex);
1174 le16_to_cpus (&setup.wValue);
1175 le16_to_cpus (&setup.wLength);
1176 if (setup.wLength != urb->transfer_buffer_length) {
1177 maybe_set_status (urb, -EOVERFLOW);
1178 goto return_urb;
1181 /* paranoia, in case of stale queued data */
1182 list_for_each_entry (req, &ep->queue, queue) {
1183 list_del_init (&req->queue);
1184 req->req.status = -EOVERFLOW;
1185 dev_dbg (dummy_dev(dum), "stale req = %p\n",
1186 req);
1188 spin_unlock (&dum->lock);
1189 req->req.complete (&ep->ep, &req->req);
1190 spin_lock (&dum->lock);
1191 ep->already_seen = 0;
1192 goto restart;
1195 /* gadget driver never sees set_address or operations
1196 * on standard feature flags. some hardware doesn't
1197 * even expose them.
1199 ep->last_io = jiffies;
1200 ep->setup_stage = 0;
1201 ep->halted = 0;
1202 switch (setup.bRequest) {
1203 case USB_REQ_SET_ADDRESS:
1204 if (setup.bRequestType != Dev_Request)
1205 break;
1206 dum->address = setup.wValue;
1207 maybe_set_status (urb, 0);
1208 dev_dbg (dummy_dev(dum), "set_address = %d\n",
1209 setup.wValue);
1210 value = 0;
1211 break;
1212 case USB_REQ_SET_FEATURE:
1213 if (setup.bRequestType == Dev_Request) {
1214 value = 0;
1215 switch (setup.wValue) {
1216 case USB_DEVICE_REMOTE_WAKEUP:
1217 break;
1218 default:
1219 value = -EOPNOTSUPP;
1221 if (value == 0) {
1222 dum->devstatus |=
1223 (1 << setup.wValue);
1224 maybe_set_status (urb, 0);
1227 } else if (setup.bRequestType == Ep_Request) {
1228 // endpoint halt
1229 ep2 = find_endpoint (dum,
1230 setup.wIndex);
1231 if (!ep2) {
1232 value = -EOPNOTSUPP;
1233 break;
1235 ep2->halted = 1;
1236 value = 0;
1237 maybe_set_status (urb, 0);
1239 break;
1240 case USB_REQ_CLEAR_FEATURE:
1241 if (setup.bRequestType == Dev_Request) {
1242 switch (setup.wValue) {
1243 case USB_DEVICE_REMOTE_WAKEUP:
1244 dum->devstatus &= ~(1 <<
1245 USB_DEVICE_REMOTE_WAKEUP);
1246 value = 0;
1247 maybe_set_status (urb, 0);
1248 break;
1249 default:
1250 value = -EOPNOTSUPP;
1251 break;
1253 } else if (setup.bRequestType == Ep_Request) {
1254 // endpoint halt
1255 ep2 = find_endpoint (dum,
1256 setup.wIndex);
1257 if (!ep2) {
1258 value = -EOPNOTSUPP;
1259 break;
1261 ep2->halted = 0;
1262 value = 0;
1263 maybe_set_status (urb, 0);
1265 break;
1266 case USB_REQ_GET_STATUS:
1267 if (setup.bRequestType == Dev_InRequest
1268 || setup.bRequestType
1269 == Intf_InRequest
1270 || setup.bRequestType
1271 == Ep_InRequest
1273 char *buf;
1275 // device: remote wakeup, selfpowered
1276 // interface: nothing
1277 // endpoint: halt
1278 buf = (char *)urb->transfer_buffer;
1279 if (urb->transfer_buffer_length > 0) {
1280 if (setup.bRequestType ==
1281 Ep_InRequest) {
1282 ep2 = find_endpoint (dum, setup.wIndex);
1283 if (!ep2) {
1284 value = -EOPNOTSUPP;
1285 break;
1287 buf [0] = ep2->halted;
1288 } else if (setup.bRequestType ==
1289 Dev_InRequest) {
1290 buf [0] = (u8)
1291 dum->devstatus;
1292 } else
1293 buf [0] = 0;
1295 if (urb->transfer_buffer_length > 1)
1296 buf [1] = 0;
1297 urb->actual_length = min (2,
1298 urb->transfer_buffer_length);
1299 value = 0;
1300 maybe_set_status (urb, 0);
1302 break;
1305 /* gadget driver handles all other requests. block
1306 * until setup() returns; no reentrancy issues etc.
1308 if (value > 0) {
1309 spin_unlock (&dum->lock);
1310 value = dum->driver->setup (&dum->gadget,
1311 &setup);
1312 spin_lock (&dum->lock);
1314 if (value >= 0) {
1315 /* no delays (max 64KB data stage) */
1316 limit = 64*1024;
1317 goto treat_control_like_bulk;
1319 /* error, see below */
1322 if (value < 0) {
1323 if (value != -EOPNOTSUPP)
1324 dev_dbg (dummy_dev(dum),
1325 "setup --> %d\n",
1326 value);
1327 maybe_set_status (urb, -EPIPE);
1328 urb->actual_length = 0;
1331 goto return_urb;
1334 /* non-control requests */
1335 limit = total;
1336 switch (usb_pipetype (urb->pipe)) {
1337 case PIPE_ISOCHRONOUS:
1338 /* FIXME is it urb->interval since the last xfer?
1339 * use urb->iso_frame_desc[i].
1340 * complete whether or not ep has requests queued.
1341 * report random errors, to debug drivers.
1343 limit = max (limit, periodic_bytes (dum, ep));
1344 maybe_set_status (urb, -ENOSYS);
1345 break;
1347 case PIPE_INTERRUPT:
1348 /* FIXME is it urb->interval since the last xfer?
1349 * this almost certainly polls too fast.
1351 limit = max (limit, periodic_bytes (dum, ep));
1352 /* FALLTHROUGH */
1354 // case PIPE_BULK: case PIPE_CONTROL:
1355 default:
1356 treat_control_like_bulk:
1357 ep->last_io = jiffies;
1358 total = transfer (dum, urb, ep, limit);
1359 break;
1362 /* incomplete transfer? */
1363 if (urb->status == -EINPROGRESS)
1364 continue;
1366 return_urb:
1367 urb->hcpriv = NULL;
1368 list_del (&urbp->urbp_list);
1369 kfree (urbp);
1370 if (ep)
1371 ep->already_seen = ep->setup_stage = 0;
1373 spin_unlock (&dum->lock);
1374 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1375 spin_lock (&dum->lock);
1377 goto restart;
1380 /* want a 1 msec delay here */
1381 if (!list_empty (&dum->urbp_list))
1382 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1383 else {
1384 usb_put_dev (dum->udev);
1385 dum->udev = NULL;
1388 spin_unlock_irqrestore (&dum->lock, flags);
1391 /*-------------------------------------------------------------------------*/
1393 #define PORT_C_MASK \
1394 ((1 << USB_PORT_FEAT_C_CONNECTION) \
1395 | (1 << USB_PORT_FEAT_C_ENABLE) \
1396 | (1 << USB_PORT_FEAT_C_SUSPEND) \
1397 | (1 << USB_PORT_FEAT_C_OVER_CURRENT) \
1398 | (1 << USB_PORT_FEAT_C_RESET))
1400 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1402 struct dummy *dum;
1403 unsigned long flags;
1404 int retval;
1406 dum = hcd_to_dummy (hcd);
1408 spin_lock_irqsave (&dum->lock, flags);
1409 if (!(dum->port_status & PORT_C_MASK))
1410 retval = 0;
1411 else {
1412 *buf = (1 << 1);
1413 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1414 dum->port_status);
1415 retval = 1;
1417 spin_unlock_irqrestore (&dum->lock, flags);
1418 return retval;
1421 static inline void
1422 hub_descriptor (struct usb_hub_descriptor *desc)
1424 memset (desc, 0, sizeof *desc);
1425 desc->bDescriptorType = 0x29;
1426 desc->bDescLength = 9;
1427 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1428 desc->bNbrPorts = 1;
1429 desc->bitmap [0] = 0xff;
1430 desc->bitmap [1] = 0xff;
1433 static int dummy_hub_control (
1434 struct usb_hcd *hcd,
1435 u16 typeReq,
1436 u16 wValue,
1437 u16 wIndex,
1438 char *buf,
1439 u16 wLength
1441 struct dummy *dum;
1442 int retval = 0;
1443 unsigned long flags;
1445 dum = hcd_to_dummy (hcd);
1446 spin_lock_irqsave (&dum->lock, flags);
1447 switch (typeReq) {
1448 case ClearHubFeature:
1449 break;
1450 case ClearPortFeature:
1451 switch (wValue) {
1452 case USB_PORT_FEAT_SUSPEND:
1453 if (dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)) {
1454 /* 20msec resume signaling */
1455 dum->resuming = 1;
1456 dum->re_timeout = jiffies +
1457 msecs_to_jiffies(20);
1459 break;
1460 case USB_PORT_FEAT_POWER:
1461 dum->port_status = 0;
1462 dum->resuming = 0;
1463 stop_activity(dum, dum->driver);
1464 break;
1465 default:
1466 dum->port_status &= ~(1 << wValue);
1468 break;
1469 case GetHubDescriptor:
1470 hub_descriptor ((struct usb_hub_descriptor *) buf);
1471 break;
1472 case GetHubStatus:
1473 *(u32 *) buf = __constant_cpu_to_le32 (0);
1474 break;
1475 case GetPortStatus:
1476 if (wIndex != 1)
1477 retval = -EPIPE;
1479 /* whoever resets or resumes must GetPortStatus to
1480 * complete it!!
1482 if (dum->resuming && time_after (jiffies, dum->re_timeout)) {
1483 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
1484 dum->port_status &= ~(1 << USB_PORT_FEAT_SUSPEND);
1485 dum->resuming = 0;
1486 dum->re_timeout = 0;
1487 if (dum->driver && dum->driver->resume) {
1488 spin_unlock (&dum->lock);
1489 dum->driver->resume (&dum->gadget);
1490 spin_lock (&dum->lock);
1493 if ((dum->port_status & (1 << USB_PORT_FEAT_RESET)) != 0
1494 && time_after (jiffies, dum->re_timeout)) {
1495 dum->port_status |= (1 << USB_PORT_FEAT_C_RESET);
1496 dum->port_status &= ~(1 << USB_PORT_FEAT_RESET);
1497 dum->re_timeout = 0;
1498 if (dum->driver) {
1499 dum->port_status |= USB_PORT_STAT_ENABLE;
1500 /* give it the best speed we agree on */
1501 dum->gadget.speed = dum->driver->speed;
1502 dum->gadget.ep0->maxpacket = 64;
1503 switch (dum->gadget.speed) {
1504 case USB_SPEED_HIGH:
1505 dum->port_status |=
1506 USB_PORT_STAT_HIGH_SPEED;
1507 break;
1508 case USB_SPEED_LOW:
1509 dum->gadget.ep0->maxpacket = 8;
1510 dum->port_status |=
1511 USB_PORT_STAT_LOW_SPEED;
1512 break;
1513 default:
1514 dum->gadget.speed = USB_SPEED_FULL;
1515 break;
1519 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1520 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1521 break;
1522 case SetHubFeature:
1523 retval = -EPIPE;
1524 break;
1525 case SetPortFeature:
1526 switch (wValue) {
1527 case USB_PORT_FEAT_SUSPEND:
1528 if ((dum->port_status & (1 << USB_PORT_FEAT_SUSPEND))
1529 == 0) {
1530 dum->port_status |=
1531 (1 << USB_PORT_FEAT_SUSPEND);
1532 if (dum->driver && dum->driver->suspend) {
1533 spin_unlock (&dum->lock);
1534 dum->driver->suspend (&dum->gadget);
1535 spin_lock (&dum->lock);
1538 break;
1539 case USB_PORT_FEAT_RESET:
1540 /* if it's already running, disconnect first */
1541 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1542 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1543 | USB_PORT_STAT_LOW_SPEED
1544 | USB_PORT_STAT_HIGH_SPEED);
1545 if (dum->driver) {
1546 dev_dbg (dummy_dev(dum),
1547 "disconnect\n");
1548 stop_activity (dum, dum->driver);
1551 /* FIXME test that code path! */
1553 /* 50msec reset signaling */
1554 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1555 /* FALLTHROUGH */
1556 default:
1557 dum->port_status |= (1 << wValue);
1559 break;
1561 default:
1562 dev_dbg (dummy_dev(dum),
1563 "hub control req%04x v%04x i%04x l%d\n",
1564 typeReq, wValue, wIndex, wLength);
1566 /* "protocol stall" on error */
1567 retval = -EPIPE;
1569 spin_unlock_irqrestore (&dum->lock, flags);
1570 return retval;
1574 /*-------------------------------------------------------------------------*/
1576 static inline ssize_t
1577 show_urb (char *buf, size_t size, struct urb *urb)
1579 int ep = usb_pipeendpoint (urb->pipe);
1581 return snprintf (buf, size,
1582 "urb/%p %s ep%d%s%s len %d/%d\n",
1583 urb,
1584 ({ char *s;
1585 switch (urb->dev->speed) {
1586 case USB_SPEED_LOW: s = "ls"; break;
1587 case USB_SPEED_FULL: s = "fs"; break;
1588 case USB_SPEED_HIGH: s = "hs"; break;
1589 default: s = "?"; break;
1590 }; s; }),
1591 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1592 ({ char *s; \
1593 switch (usb_pipetype (urb->pipe)) { \
1594 case PIPE_CONTROL: s = ""; break; \
1595 case PIPE_BULK: s = "-bulk"; break; \
1596 case PIPE_INTERRUPT: s = "-int"; break; \
1597 default: s = "-iso"; break; \
1598 }; s;}),
1599 urb->actual_length, urb->transfer_buffer_length);
1602 static ssize_t
1603 show_urbs (struct device *dev, char *buf)
1605 struct usb_hcd *hcd = dev_get_drvdata (dev);
1606 struct dummy *dum = hcd_to_dummy (hcd);
1607 struct urbp *urbp;
1608 size_t size = 0;
1609 unsigned long flags;
1611 spin_lock_irqsave (&dum->lock, flags);
1612 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1613 size_t temp;
1615 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1616 buf += temp;
1617 size += temp;
1619 spin_unlock_irqrestore (&dum->lock, flags);
1621 return size;
1623 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1625 static int dummy_start (struct usb_hcd *hcd)
1627 struct dummy *dum;
1628 struct usb_device *root;
1629 int retval;
1631 dum = hcd_to_dummy (hcd);
1634 * MASTER side init ... we emulate a root hub that'll only ever
1635 * talk to one device (the slave side). Also appears in sysfs,
1636 * just like more familiar pci-based HCDs.
1638 spin_lock_init (&dum->lock);
1639 init_timer (&dum->timer);
1640 dum->timer.function = dummy_timer;
1641 dum->timer.data = (unsigned long) dum;
1643 INIT_LIST_HEAD (&dum->urbp_list);
1645 root = usb_alloc_dev (NULL, &hcd->self, 0);
1646 if (!root)
1647 return -ENOMEM;
1649 /* root hub enters addressed state... */
1650 hcd->state = HC_STATE_RUNNING;
1651 root->speed = USB_SPEED_HIGH;
1653 /* ...then configured, so khubd sees us. */
1654 if ((retval = usb_hcd_register_root_hub (root, hcd)) != 0) {
1655 goto err1;
1658 /* only show a low-power port: just 8mA */
1659 hub_set_power_budget (root, 8);
1661 if ((retval = dummy_register_udc (dum)) != 0)
1662 goto err2;
1664 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1665 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1666 return 0;
1668 err2:
1669 usb_disconnect (&hcd->self.root_hub);
1670 err1:
1671 usb_put_dev (root);
1672 hcd->state = HC_STATE_QUIESCING;
1673 return retval;
1676 static void dummy_stop (struct usb_hcd *hcd)
1678 struct dummy *dum;
1680 dum = hcd_to_dummy (hcd);
1682 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1684 usb_gadget_unregister_driver (dum->driver);
1685 dummy_unregister_udc (dum);
1687 dev_info (dummy_dev(dum), "stopped\n");
1690 /*-------------------------------------------------------------------------*/
1692 static int dummy_h_get_frame (struct usb_hcd *hcd)
1694 return dummy_g_get_frame (NULL);
1697 static const struct hc_driver dummy_hcd = {
1698 .description = (char *) driver_name,
1699 .product_desc = "Dummy host controller",
1700 .hcd_priv_size = sizeof(struct dummy),
1702 .flags = HCD_USB2,
1704 .start = dummy_start,
1705 .stop = dummy_stop,
1707 .urb_enqueue = dummy_urb_enqueue,
1708 .urb_dequeue = dummy_urb_dequeue,
1710 .get_frame_number = dummy_h_get_frame,
1712 .hub_status_data = dummy_hub_status,
1713 .hub_control = dummy_hub_control,
1716 static int dummy_probe (struct device *dev)
1718 struct usb_hcd *hcd;
1719 int retval;
1721 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1723 hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id);
1724 if (!hcd)
1725 return -ENOMEM;
1726 the_controller = hcd_to_dummy (hcd);
1728 retval = usb_add_hcd(hcd, 0, 0);
1729 if (retval != 0) {
1730 usb_put_hcd (hcd);
1731 the_controller = NULL;
1733 return retval;
1736 static void dummy_remove (struct device *dev)
1738 struct usb_hcd *hcd;
1740 hcd = dev_get_drvdata (dev);
1741 usb_remove_hcd (hcd);
1742 usb_put_hcd (hcd);
1743 the_controller = NULL;
1746 /*-------------------------------------------------------------------------*/
1748 static int dummy_pdev_detect (void)
1750 int retval;
1752 retval = driver_register (&dummy_driver);
1753 if (retval < 0)
1754 return retval;
1756 the_pdev.name = "hc";
1757 the_pdev.dev.driver = &dummy_driver;
1758 the_pdev.dev.release = dummy_pdev_release;
1760 retval = platform_device_register (&the_pdev);
1761 if (retval < 0)
1762 driver_unregister (&dummy_driver);
1763 return retval;
1766 static void dummy_pdev_remove (void)
1768 platform_device_unregister (&the_pdev);
1769 driver_unregister (&dummy_driver);
1772 /*-------------------------------------------------------------------------*/
1774 static int __init init (void)
1776 int retval;
1778 if (usb_disabled ())
1779 return -ENODEV;
1780 if ((retval = dummy_pdev_detect ()) != 0)
1781 return retval;
1782 if ((retval = dummy_probe (&the_pdev.dev)) != 0)
1783 dummy_pdev_remove ();
1784 return retval;
1786 module_init (init);
1788 static void __exit cleanup (void)
1790 dummy_remove (&the_pdev.dev);
1791 dummy_pdev_remove ();
1793 module_exit (cleanup);