[PATCH] USB: gadget driver updates (SETUP api change)
[linux-2.6.22.y-op.git] / drivers / usb / gadget / dummy_hcd.c
blobf9540adf2a4f75530ec19379e76735a280e1bfcc
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 /*-------------------------------------------------------------------------*/
146 #define FIFO_SIZE 64
148 struct urbp {
149 struct urb *urb;
150 struct list_head urbp_list;
153 struct dummy {
154 spinlock_t lock;
157 * SLAVE/GADGET side support
159 struct dummy_ep ep [DUMMY_ENDPOINTS];
160 int address;
161 struct usb_gadget gadget;
162 struct usb_gadget_driver *driver;
163 struct dummy_request fifo_req;
164 u8 fifo_buf [FIFO_SIZE];
165 u16 devstatus;
166 unsigned pullup:1;
167 unsigned active:1;
168 unsigned old_active:1;
171 * MASTER/HOST side support
173 struct timer_list timer;
174 u32 port_status;
175 u32 old_status;
176 unsigned resuming:1;
177 unsigned long re_timeout;
179 struct usb_device *udev;
180 struct list_head urbp_list;
183 static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
185 return (struct dummy *) (hcd->hcd_priv);
188 static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
190 return container_of((void *) dum, struct usb_hcd, hcd_priv);
193 static inline struct device *dummy_dev (struct dummy *dum)
195 return dummy_to_hcd(dum)->self.controller;
198 static inline struct device *udc_dev (struct dummy *dum)
200 return dum->gadget.dev.parent;
203 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
205 return container_of (ep->gadget, struct dummy, gadget);
208 static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
210 return container_of (gadget, struct dummy, gadget);
213 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
215 return container_of (dev, struct dummy, gadget.dev);
218 static struct dummy *the_controller;
220 /*-------------------------------------------------------------------------*/
222 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
224 /* called with spinlock held */
225 static void nuke (struct dummy *dum, struct dummy_ep *ep)
227 while (!list_empty (&ep->queue)) {
228 struct dummy_request *req;
230 req = list_entry (ep->queue.next, struct dummy_request, queue);
231 list_del_init (&req->queue);
232 req->req.status = -ESHUTDOWN;
234 spin_unlock (&dum->lock);
235 req->req.complete (&ep->ep, &req->req);
236 spin_lock (&dum->lock);
240 /* caller must hold lock */
241 static void
242 stop_activity (struct dummy *dum)
244 struct dummy_ep *ep;
246 /* prevent any more requests */
247 dum->address = 0;
249 /* The timer is left running so that outstanding URBs can fail */
251 /* nuke any pending requests first, so driver i/o is quiesced */
252 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
253 nuke (dum, ep);
255 /* driver now does any non-usb quiescing necessary */
258 /* caller must hold lock */
259 static void
260 set_link_state (struct dummy *dum)
262 dum->active = 0;
263 if ((dum->port_status & USB_PORT_STAT_POWER) == 0)
264 dum->port_status = 0;
265 else if (!dum->pullup) {
266 dum->port_status &= ~(USB_PORT_STAT_CONNECTION |
267 USB_PORT_STAT_ENABLE |
268 USB_PORT_STAT_LOW_SPEED |
269 USB_PORT_STAT_HIGH_SPEED |
270 USB_PORT_STAT_SUSPEND);
271 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0)
272 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
273 } else {
274 dum->port_status |= USB_PORT_STAT_CONNECTION;
275 if ((dum->old_status & USB_PORT_STAT_CONNECTION) == 0)
276 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
277 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0)
278 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
279 else if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0)
280 dum->active = 1;
283 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0 || dum->active)
284 dum->resuming = 0;
286 if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
287 (dum->port_status & USB_PORT_STAT_RESET) != 0) {
288 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
289 (dum->old_status & USB_PORT_STAT_RESET) == 0 &&
290 dum->driver) {
291 stop_activity (dum);
292 spin_unlock (&dum->lock);
293 dum->driver->disconnect (&dum->gadget);
294 spin_lock (&dum->lock);
296 } else if (dum->active != dum->old_active) {
297 if (dum->old_active && dum->driver->suspend) {
298 spin_unlock (&dum->lock);
299 dum->driver->suspend (&dum->gadget);
300 spin_lock (&dum->lock);
301 } else if (!dum->old_active && dum->driver->resume) {
302 spin_unlock (&dum->lock);
303 dum->driver->resume (&dum->gadget);
304 spin_lock (&dum->lock);
308 dum->old_status = dum->port_status;
309 dum->old_active = dum->active;
312 /*-------------------------------------------------------------------------*/
314 /* SLAVE/GADGET SIDE DRIVER
316 * This only tracks gadget state. All the work is done when the host
317 * side tries some (emulated) i/o operation. Real device controller
318 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
321 #define is_enabled(dum) \
322 (dum->port_status & USB_PORT_STAT_ENABLE)
324 static int
325 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
327 struct dummy *dum;
328 struct dummy_ep *ep;
329 unsigned max;
330 int retval;
332 ep = usb_ep_to_dummy_ep (_ep);
333 if (!_ep || !desc || ep->desc || _ep->name == ep0name
334 || desc->bDescriptorType != USB_DT_ENDPOINT)
335 return -EINVAL;
336 dum = ep_to_dummy (ep);
337 if (!dum->driver || !is_enabled (dum))
338 return -ESHUTDOWN;
339 max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
341 /* drivers must not request bad settings, since lower levels
342 * (hardware or its drivers) may not check. some endpoints
343 * can't do iso, many have maxpacket limitations, etc.
345 * since this "hardware" driver is here to help debugging, we
346 * have some extra sanity checks. (there could be more though,
347 * especially for "ep9out" style fixed function ones.)
349 retval = -EINVAL;
350 switch (desc->bmAttributes & 0x03) {
351 case USB_ENDPOINT_XFER_BULK:
352 if (strstr (ep->ep.name, "-iso")
353 || strstr (ep->ep.name, "-int")) {
354 goto done;
356 switch (dum->gadget.speed) {
357 case USB_SPEED_HIGH:
358 if (max == 512)
359 break;
360 /* conserve return statements */
361 default:
362 switch (max) {
363 case 8: case 16: case 32: case 64:
364 /* we'll fake any legal size */
365 break;
366 default:
367 case USB_SPEED_LOW:
368 goto done;
371 break;
372 case USB_ENDPOINT_XFER_INT:
373 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
374 goto done;
375 /* real hardware might not handle all packet sizes */
376 switch (dum->gadget.speed) {
377 case USB_SPEED_HIGH:
378 if (max <= 1024)
379 break;
380 /* save a return statement */
381 case USB_SPEED_FULL:
382 if (max <= 64)
383 break;
384 /* save a return statement */
385 default:
386 if (max <= 8)
387 break;
388 goto done;
390 break;
391 case USB_ENDPOINT_XFER_ISOC:
392 if (strstr (ep->ep.name, "-bulk")
393 || strstr (ep->ep.name, "-int"))
394 goto done;
395 /* real hardware might not handle all packet sizes */
396 switch (dum->gadget.speed) {
397 case USB_SPEED_HIGH:
398 if (max <= 1024)
399 break;
400 /* save a return statement */
401 case USB_SPEED_FULL:
402 if (max <= 1023)
403 break;
404 /* save a return statement */
405 default:
406 goto done;
408 break;
409 default:
410 /* few chips support control except on ep0 */
411 goto done;
414 _ep->maxpacket = max;
415 ep->desc = desc;
417 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
418 _ep->name,
419 desc->bEndpointAddress & 0x0f,
420 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
421 ({ char *val;
422 switch (desc->bmAttributes & 0x03) {
423 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
424 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
425 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
426 default: val = "ctrl"; break;
427 }; val; }),
428 max);
430 /* at this point real hardware should be NAKing transfers
431 * to that endpoint, until a buffer is queued to it.
433 retval = 0;
434 done:
435 return retval;
438 static int dummy_disable (struct usb_ep *_ep)
440 struct dummy_ep *ep;
441 struct dummy *dum;
442 unsigned long flags;
443 int retval;
445 ep = usb_ep_to_dummy_ep (_ep);
446 if (!_ep || !ep->desc || _ep->name == ep0name)
447 return -EINVAL;
448 dum = ep_to_dummy (ep);
450 spin_lock_irqsave (&dum->lock, flags);
451 ep->desc = NULL;
452 retval = 0;
453 nuke (dum, ep);
454 spin_unlock_irqrestore (&dum->lock, flags);
456 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
457 return retval;
460 static struct usb_request *
461 dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
463 struct dummy_ep *ep;
464 struct dummy_request *req;
466 if (!_ep)
467 return NULL;
468 ep = usb_ep_to_dummy_ep (_ep);
470 req = kmalloc (sizeof *req, mem_flags);
471 if (!req)
472 return NULL;
473 memset (req, 0, sizeof *req);
474 INIT_LIST_HEAD (&req->queue);
475 return &req->req;
478 static void
479 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
481 struct dummy_ep *ep;
482 struct dummy_request *req;
484 ep = usb_ep_to_dummy_ep (_ep);
485 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
486 return;
488 req = usb_request_to_dummy_request (_req);
489 WARN_ON (!list_empty (&req->queue));
490 kfree (req);
493 static void *
494 dummy_alloc_buffer (
495 struct usb_ep *_ep,
496 unsigned bytes,
497 dma_addr_t *dma,
498 int mem_flags
500 char *retval;
501 struct dummy_ep *ep;
502 struct dummy *dum;
504 ep = usb_ep_to_dummy_ep (_ep);
505 dum = ep_to_dummy (ep);
507 if (!dum->driver)
508 return NULL;
509 retval = kmalloc (bytes, mem_flags);
510 *dma = (dma_addr_t) retval;
511 return retval;
514 static void
515 dummy_free_buffer (
516 struct usb_ep *_ep,
517 void *buf,
518 dma_addr_t dma,
519 unsigned bytes
521 if (bytes)
522 kfree (buf);
525 static void
526 fifo_complete (struct usb_ep *ep, struct usb_request *req)
530 static int
531 dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
533 struct dummy_ep *ep;
534 struct dummy_request *req;
535 struct dummy *dum;
536 unsigned long flags;
538 req = usb_request_to_dummy_request (_req);
539 if (!_req || !list_empty (&req->queue) || !_req->complete)
540 return -EINVAL;
542 ep = usb_ep_to_dummy_ep (_ep);
543 if (!_ep || (!ep->desc && _ep->name != ep0name))
544 return -EINVAL;
546 dum = ep_to_dummy (ep);
547 if (!dum->driver || !is_enabled (dum))
548 return -ESHUTDOWN;
550 #if 0
551 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
552 ep, _req, _ep->name, _req->length, _req->buf);
553 #endif
555 _req->status = -EINPROGRESS;
556 _req->actual = 0;
557 spin_lock_irqsave (&dum->lock, flags);
559 /* implement an emulated single-request FIFO */
560 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
561 list_empty (&dum->fifo_req.queue) &&
562 list_empty (&ep->queue) &&
563 _req->length <= FIFO_SIZE) {
564 req = &dum->fifo_req;
565 req->req = *_req;
566 req->req.buf = dum->fifo_buf;
567 memcpy (dum->fifo_buf, _req->buf, _req->length);
568 req->req.context = dum;
569 req->req.complete = fifo_complete;
571 spin_unlock (&dum->lock);
572 _req->actual = _req->length;
573 _req->status = 0;
574 _req->complete (_ep, _req);
575 spin_lock (&dum->lock);
577 list_add_tail (&req->queue, &ep->queue);
578 spin_unlock_irqrestore (&dum->lock, flags);
580 /* real hardware would likely enable transfers here, in case
581 * it'd been left NAKing.
583 return 0;
586 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
588 struct dummy_ep *ep;
589 struct dummy *dum;
590 int retval = -EINVAL;
591 unsigned long flags;
592 struct dummy_request *req = NULL;
594 if (!_ep || !_req)
595 return retval;
596 ep = usb_ep_to_dummy_ep (_ep);
597 dum = ep_to_dummy (ep);
599 if (!dum->driver)
600 return -ESHUTDOWN;
602 spin_lock_irqsave (&dum->lock, flags);
603 list_for_each_entry (req, &ep->queue, queue) {
604 if (&req->req == _req) {
605 list_del_init (&req->queue);
606 _req->status = -ECONNRESET;
607 retval = 0;
608 break;
611 spin_unlock_irqrestore (&dum->lock, flags);
613 if (retval == 0) {
614 dev_dbg (udc_dev(dum),
615 "dequeued req %p from %s, len %d buf %p\n",
616 req, _ep->name, _req->length, _req->buf);
617 _req->complete (_ep, _req);
619 return retval;
622 static int
623 dummy_set_halt (struct usb_ep *_ep, int value)
625 struct dummy_ep *ep;
626 struct dummy *dum;
628 if (!_ep)
629 return -EINVAL;
630 ep = usb_ep_to_dummy_ep (_ep);
631 dum = ep_to_dummy (ep);
632 if (!dum->driver)
633 return -ESHUTDOWN;
634 if (!value)
635 ep->halted = 0;
636 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
637 !list_empty (&ep->queue))
638 return -EAGAIN;
639 else
640 ep->halted = 1;
641 /* FIXME clear emulated data toggle too */
642 return 0;
645 static const struct usb_ep_ops dummy_ep_ops = {
646 .enable = dummy_enable,
647 .disable = dummy_disable,
649 .alloc_request = dummy_alloc_request,
650 .free_request = dummy_free_request,
652 .alloc_buffer = dummy_alloc_buffer,
653 .free_buffer = dummy_free_buffer,
654 /* map, unmap, ... eventually hook the "generic" dma calls */
656 .queue = dummy_queue,
657 .dequeue = dummy_dequeue,
659 .set_halt = dummy_set_halt,
662 /*-------------------------------------------------------------------------*/
664 /* there are both host and device side versions of this call ... */
665 static int dummy_g_get_frame (struct usb_gadget *_gadget)
667 struct timeval tv;
669 do_gettimeofday (&tv);
670 return tv.tv_usec / 1000;
673 static int dummy_wakeup (struct usb_gadget *_gadget)
675 struct dummy *dum;
677 dum = gadget_to_dummy (_gadget);
678 if (!(dum->port_status & USB_PORT_STAT_SUSPEND)
679 || !(dum->devstatus &
680 ( (1 << USB_DEVICE_B_HNP_ENABLE)
681 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
682 return -EINVAL;
684 /* hub notices our request, issues downstream resume, etc */
685 dum->resuming = 1;
686 dum->re_timeout = jiffies + msecs_to_jiffies(20);
687 mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout);
688 return 0;
691 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
693 struct dummy *dum;
695 dum = gadget_to_dummy (_gadget);
696 if (value)
697 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
698 else
699 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
700 return 0;
703 static int dummy_pullup (struct usb_gadget *_gadget, int value)
705 struct dummy *dum;
706 unsigned long flags;
708 dum = gadget_to_dummy (_gadget);
709 spin_lock_irqsave (&dum->lock, flags);
710 dum->pullup = (value != 0);
711 set_link_state (dum);
712 spin_unlock_irqrestore (&dum->lock, flags);
714 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
715 return 0;
718 static const struct usb_gadget_ops dummy_ops = {
719 .get_frame = dummy_g_get_frame,
720 .wakeup = dummy_wakeup,
721 .set_selfpowered = dummy_set_selfpowered,
722 .pullup = dummy_pullup,
725 /*-------------------------------------------------------------------------*/
727 /* "function" sysfs attribute */
728 static ssize_t
729 show_function (struct device *dev, struct device_attribute *attr, char *buf)
731 struct dummy *dum = gadget_dev_to_dummy (dev);
733 if (!dum->driver || !dum->driver->function)
734 return 0;
735 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
737 DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
739 /*-------------------------------------------------------------------------*/
742 * Driver registration/unregistration.
744 * This is basically hardware-specific; there's usually only one real USB
745 * device (not host) controller since that's how USB devices are intended
746 * to work. So most implementations of these api calls will rely on the
747 * fact that only one driver will ever bind to the hardware. But curious
748 * hardware can be built with discrete components, so the gadget API doesn't
749 * require that assumption.
751 * For this emulator, it might be convenient to create a usb slave device
752 * for each driver that registers: just add to a big root hub.
756 usb_gadget_register_driver (struct usb_gadget_driver *driver)
758 struct dummy *dum = the_controller;
759 int retval, i;
761 if (!dum)
762 return -EINVAL;
763 if (dum->driver)
764 return -EBUSY;
765 if (!driver->bind || !driver->unbind || !driver->setup
766 || driver->speed == USB_SPEED_UNKNOWN)
767 return -EINVAL;
770 * SLAVE side init ... the layer above hardware, which
771 * can't enumerate without help from the driver we're binding.
774 dum->devstatus = 0;
776 INIT_LIST_HEAD (&dum->gadget.ep_list);
777 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
778 struct dummy_ep *ep = &dum->ep [i];
780 if (!ep_name [i])
781 break;
782 ep->ep.name = ep_name [i];
783 ep->ep.ops = &dummy_ep_ops;
784 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
785 ep->halted = ep->already_seen = ep->setup_stage = 0;
786 ep->ep.maxpacket = ~0;
787 ep->last_io = jiffies;
788 ep->gadget = &dum->gadget;
789 ep->desc = NULL;
790 INIT_LIST_HEAD (&ep->queue);
793 dum->gadget.ep0 = &dum->ep [0].ep;
794 dum->ep [0].ep.maxpacket = 64;
795 list_del_init (&dum->ep [0].ep.ep_list);
796 INIT_LIST_HEAD(&dum->fifo_req.queue);
798 dum->driver = driver;
799 dum->gadget.dev.driver = &driver->driver;
800 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
801 driver->driver.name);
802 if ((retval = driver->bind (&dum->gadget)) != 0) {
803 dum->driver = NULL;
804 dum->gadget.dev.driver = NULL;
805 return retval;
808 driver->driver.bus = dum->gadget.dev.parent->bus;
809 driver_register (&driver->driver);
810 device_bind_driver (&dum->gadget.dev);
812 /* khubd will enumerate this in a while */
813 spin_lock_irq (&dum->lock);
814 dum->pullup = 1;
815 set_link_state (dum);
816 spin_unlock_irq (&dum->lock);
818 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
819 return 0;
821 EXPORT_SYMBOL (usb_gadget_register_driver);
824 usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
826 struct dummy *dum = the_controller;
827 unsigned long flags;
829 if (!dum)
830 return -ENODEV;
831 if (!driver || driver != dum->driver)
832 return -EINVAL;
834 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
835 driver->driver.name);
837 spin_lock_irqsave (&dum->lock, flags);
838 dum->pullup = 0;
839 set_link_state (dum);
840 spin_unlock_irqrestore (&dum->lock, flags);
842 driver->unbind (&dum->gadget);
843 dum->driver = NULL;
845 device_release_driver (&dum->gadget.dev);
846 driver_unregister (&driver->driver);
848 spin_lock_irqsave (&dum->lock, flags);
849 dum->pullup = 0;
850 set_link_state (dum);
851 spin_unlock_irqrestore (&dum->lock, flags);
853 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
854 return 0;
856 EXPORT_SYMBOL (usb_gadget_unregister_driver);
858 #undef is_enabled
860 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
862 return -ENOSYS;
864 EXPORT_SYMBOL (net2280_set_fifo_mode);
867 /* The gadget structure is stored inside the hcd structure and will be
868 * released along with it. */
869 static void
870 dummy_gadget_release (struct device *dev)
872 #if 0 /* usb_bus_put isn't EXPORTed! */
873 struct dummy *dum = gadget_dev_to_dummy (dev);
875 usb_bus_put (&dummy_to_hcd (dum)->self);
876 #endif
879 static int dummy_udc_probe (struct device *dev)
881 struct dummy *dum = the_controller;
882 int rc;
884 dum->gadget.name = gadget_name;
885 dum->gadget.ops = &dummy_ops;
886 dum->gadget.is_dualspeed = 1;
888 /* maybe claim OTG support, though we won't complete HNP */
889 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
891 strcpy (dum->gadget.dev.bus_id, "gadget");
892 dum->gadget.dev.parent = dev;
893 dum->gadget.dev.release = dummy_gadget_release;
894 rc = device_register (&dum->gadget.dev);
895 if (rc < 0)
896 return rc;
898 #if 0 /* usb_bus_get isn't EXPORTed! */
899 usb_bus_get (&dummy_to_hcd (dum)->self);
900 #endif
902 dev_set_drvdata (dev, dum);
903 device_create_file (&dum->gadget.dev, &dev_attr_function);
904 return rc;
907 static int dummy_udc_remove (struct device *dev)
909 struct dummy *dum = dev_get_drvdata (dev);
911 dev_set_drvdata (dev, NULL);
912 device_remove_file (&dum->gadget.dev, &dev_attr_function);
913 device_unregister (&dum->gadget.dev);
914 return 0;
917 static struct device_driver dummy_udc_driver = {
918 .name = (char *) gadget_name,
919 .bus = &platform_bus_type,
920 .probe = dummy_udc_probe,
921 .remove = dummy_udc_remove,
924 /*-------------------------------------------------------------------------*/
926 /* MASTER/HOST SIDE DRIVER
928 * this uses the hcd framework to hook up to host side drivers.
929 * its root hub will only have one device, otherwise it acts like
930 * a normal host controller.
932 * when urbs are queued, they're just stuck on a list that we
933 * scan in a timer callback. that callback connects writes from
934 * the host with reads from the device, and so on, based on the
935 * usb 2.0 rules.
938 static int dummy_urb_enqueue (
939 struct usb_hcd *hcd,
940 struct usb_host_endpoint *ep,
941 struct urb *urb,
942 int mem_flags
944 struct dummy *dum;
945 struct urbp *urbp;
946 unsigned long flags;
948 if (!urb->transfer_buffer && urb->transfer_buffer_length)
949 return -EINVAL;
951 urbp = kmalloc (sizeof *urbp, mem_flags);
952 if (!urbp)
953 return -ENOMEM;
954 urbp->urb = urb;
956 dum = hcd_to_dummy (hcd);
957 spin_lock_irqsave (&dum->lock, flags);
959 if (!dum->udev) {
960 dum->udev = urb->dev;
961 usb_get_dev (dum->udev);
962 } else if (unlikely (dum->udev != urb->dev))
963 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
965 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
966 urb->hcpriv = urbp;
967 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
968 urb->error_count = 1; /* mark as a new urb */
970 /* kick the scheduler, it'll do the rest */
971 if (!timer_pending (&dum->timer))
972 mod_timer (&dum->timer, jiffies + 1);
974 spin_unlock_irqrestore (&dum->lock, flags);
975 return 0;
978 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
980 /* giveback happens automatically in timer callback */
981 return 0;
984 static void maybe_set_status (struct urb *urb, int status)
986 spin_lock (&urb->lock);
987 if (urb->status == -EINPROGRESS)
988 urb->status = status;
989 spin_unlock (&urb->lock);
992 /* transfer up to a frame's worth; caller must own lock */
993 static int
994 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
996 struct dummy_request *req;
998 top:
999 /* if there's no request queued, the device is NAKing; return */
1000 list_for_each_entry (req, &ep->queue, queue) {
1001 unsigned host_len, dev_len, len;
1002 int is_short, to_host;
1003 int rescan = 0;
1005 /* 1..N packets of ep->ep.maxpacket each ... the last one
1006 * may be short (including zero length).
1008 * writer can send a zlp explicitly (length 0) or implicitly
1009 * (length mod maxpacket zero, and 'zero' flag); they always
1010 * terminate reads.
1012 host_len = urb->transfer_buffer_length - urb->actual_length;
1013 dev_len = req->req.length - req->req.actual;
1014 len = min (host_len, dev_len);
1016 /* FIXME update emulated data toggle too */
1018 to_host = usb_pipein (urb->pipe);
1019 if (unlikely (len == 0))
1020 is_short = 1;
1021 else {
1022 char *ubuf, *rbuf;
1024 /* not enough bandwidth left? */
1025 if (limit < ep->ep.maxpacket && limit < len)
1026 break;
1027 len = min (len, (unsigned) limit);
1028 if (len == 0)
1029 break;
1031 /* use an extra pass for the final short packet */
1032 if (len > ep->ep.maxpacket) {
1033 rescan = 1;
1034 len -= (len % ep->ep.maxpacket);
1036 is_short = (len % ep->ep.maxpacket) != 0;
1038 /* else transfer packet(s) */
1039 ubuf = urb->transfer_buffer + urb->actual_length;
1040 rbuf = req->req.buf + req->req.actual;
1041 if (to_host)
1042 memcpy (ubuf, rbuf, len);
1043 else
1044 memcpy (rbuf, ubuf, len);
1045 ep->last_io = jiffies;
1047 limit -= len;
1048 urb->actual_length += len;
1049 req->req.actual += len;
1052 /* short packets terminate, maybe with overflow/underflow.
1053 * it's only really an error to write too much.
1055 * partially filling a buffer optionally blocks queue advances
1056 * (so completion handlers can clean up the queue) but we don't
1057 * need to emulate such data-in-flight. so we only show part
1058 * of the URB_SHORT_NOT_OK effect: completion status.
1060 if (is_short) {
1061 if (host_len == dev_len) {
1062 req->req.status = 0;
1063 maybe_set_status (urb, 0);
1064 } else if (to_host) {
1065 req->req.status = 0;
1066 if (dev_len > host_len)
1067 maybe_set_status (urb, -EOVERFLOW);
1068 else
1069 maybe_set_status (urb,
1070 (urb->transfer_flags
1071 & URB_SHORT_NOT_OK)
1072 ? -EREMOTEIO : 0);
1073 } else if (!to_host) {
1074 maybe_set_status (urb, 0);
1075 if (host_len > dev_len)
1076 req->req.status = -EOVERFLOW;
1077 else
1078 req->req.status = 0;
1081 /* many requests terminate without a short packet */
1082 } else {
1083 if (req->req.length == req->req.actual
1084 && !req->req.zero)
1085 req->req.status = 0;
1086 if (urb->transfer_buffer_length == urb->actual_length
1087 && !(urb->transfer_flags
1088 & URB_ZERO_PACKET)) {
1089 maybe_set_status (urb, 0);
1093 /* device side completion --> continuable */
1094 if (req->req.status != -EINPROGRESS) {
1095 list_del_init (&req->queue);
1097 spin_unlock (&dum->lock);
1098 req->req.complete (&ep->ep, &req->req);
1099 spin_lock (&dum->lock);
1101 /* requests might have been unlinked... */
1102 rescan = 1;
1105 /* host side completion --> terminate */
1106 if (urb->status != -EINPROGRESS)
1107 break;
1109 /* rescan to continue with any other queued i/o */
1110 if (rescan)
1111 goto top;
1113 return limit;
1116 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1118 int limit = ep->ep.maxpacket;
1120 if (dum->gadget.speed == USB_SPEED_HIGH) {
1121 int tmp;
1123 /* high bandwidth mode */
1124 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1125 tmp = le16_to_cpu (tmp);
1126 tmp = (tmp >> 11) & 0x03;
1127 tmp *= 8 /* applies to entire frame */;
1128 limit += limit * tmp;
1130 return limit;
1133 #define is_active(dum) ((dum->port_status & \
1134 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1135 USB_PORT_STAT_SUSPEND)) \
1136 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1138 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1140 int i;
1142 if (!is_active (dum))
1143 return NULL;
1144 if ((address & ~USB_DIR_IN) == 0)
1145 return &dum->ep [0];
1146 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1147 struct dummy_ep *ep = &dum->ep [i];
1149 if (!ep->desc)
1150 continue;
1151 if (ep->desc->bEndpointAddress == address)
1152 return ep;
1154 return NULL;
1157 #undef is_active
1159 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1160 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1161 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1162 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1163 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1164 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1166 /* drive both sides of the transfers; looks like irq handlers to
1167 * both drivers except the callbacks aren't in_irq().
1169 static void dummy_timer (unsigned long _dum)
1171 struct dummy *dum = (struct dummy *) _dum;
1172 struct urbp *urbp, *tmp;
1173 unsigned long flags;
1174 int limit, total;
1175 int i;
1177 /* simplistic model for one frame's bandwidth */
1178 switch (dum->gadget.speed) {
1179 case USB_SPEED_LOW:
1180 total = 8/*bytes*/ * 12/*packets*/;
1181 break;
1182 case USB_SPEED_FULL:
1183 total = 64/*bytes*/ * 19/*packets*/;
1184 break;
1185 case USB_SPEED_HIGH:
1186 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1187 break;
1188 default:
1189 dev_err (dummy_dev(dum), "bogus device speed\n");
1190 return;
1193 /* FIXME if HZ != 1000 this will probably misbehave ... */
1195 /* look at each urb queued by the host side driver */
1196 spin_lock_irqsave (&dum->lock, flags);
1198 if (!dum->udev) {
1199 dev_err (dummy_dev(dum),
1200 "timer fired with no URBs pending?\n");
1201 spin_unlock_irqrestore (&dum->lock, flags);
1202 return;
1205 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1206 if (!ep_name [i])
1207 break;
1208 dum->ep [i].already_seen = 0;
1211 restart:
1212 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1213 struct urb *urb;
1214 struct dummy_request *req;
1215 u8 address;
1216 struct dummy_ep *ep = NULL;
1217 int type;
1219 urb = urbp->urb;
1220 if (urb->status != -EINPROGRESS) {
1221 /* likely it was just unlinked */
1222 goto return_urb;
1224 type = usb_pipetype (urb->pipe);
1226 /* used up this frame's non-periodic bandwidth?
1227 * FIXME there's infinite bandwidth for control and
1228 * periodic transfers ... unrealistic.
1230 if (total <= 0 && type == PIPE_BULK)
1231 continue;
1233 /* find the gadget's ep for this request (if configured) */
1234 address = usb_pipeendpoint (urb->pipe);
1235 if (usb_pipein (urb->pipe))
1236 address |= USB_DIR_IN;
1237 ep = find_endpoint(dum, address);
1238 if (!ep) {
1239 /* set_configuration() disagreement */
1240 dev_dbg (dummy_dev(dum),
1241 "no ep configured for urb %p\n",
1242 urb);
1243 maybe_set_status (urb, -EPROTO);
1244 goto return_urb;
1247 if (ep->already_seen)
1248 continue;
1249 ep->already_seen = 1;
1250 if (ep == &dum->ep [0] && urb->error_count) {
1251 ep->setup_stage = 1; /* a new urb */
1252 urb->error_count = 0;
1254 if (ep->halted && !ep->setup_stage) {
1255 /* NOTE: must not be iso! */
1256 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1257 ep->ep.name, urb);
1258 maybe_set_status (urb, -EPIPE);
1259 goto return_urb;
1261 /* FIXME make sure both ends agree on maxpacket */
1263 /* handle control requests */
1264 if (ep == &dum->ep [0] && ep->setup_stage) {
1265 struct usb_ctrlrequest setup;
1266 int value = 1;
1267 struct dummy_ep *ep2;
1269 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1270 if (setup.wLength != urb->transfer_buffer_length) {
1271 maybe_set_status (urb, -EOVERFLOW);
1272 goto return_urb;
1275 /* paranoia, in case of stale queued data */
1276 list_for_each_entry (req, &ep->queue, queue) {
1277 list_del_init (&req->queue);
1278 req->req.status = -EOVERFLOW;
1279 dev_dbg (udc_dev(dum), "stale req = %p\n",
1280 req);
1282 spin_unlock (&dum->lock);
1283 req->req.complete (&ep->ep, &req->req);
1284 spin_lock (&dum->lock);
1285 ep->already_seen = 0;
1286 goto restart;
1289 /* gadget driver never sees set_address or operations
1290 * on standard feature flags. some hardware doesn't
1291 * even expose them.
1293 ep->last_io = jiffies;
1294 ep->setup_stage = 0;
1295 ep->halted = 0;
1296 switch (setup.bRequest) {
1297 case USB_REQ_SET_ADDRESS:
1298 if (setup.bRequestType != Dev_Request)
1299 break;
1300 dum->address = setup.wValue;
1301 maybe_set_status (urb, 0);
1302 dev_dbg (udc_dev(dum), "set_address = %d\n",
1303 setup.wValue);
1304 value = 0;
1305 break;
1306 case USB_REQ_SET_FEATURE:
1307 if (setup.bRequestType == Dev_Request) {
1308 value = 0;
1309 switch (setup.wValue) {
1310 case USB_DEVICE_REMOTE_WAKEUP:
1311 break;
1312 case USB_DEVICE_B_HNP_ENABLE:
1313 dum->gadget.b_hnp_enable = 1;
1314 break;
1315 case USB_DEVICE_A_HNP_SUPPORT:
1316 dum->gadget.a_hnp_support = 1;
1317 break;
1318 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1319 dum->gadget.a_alt_hnp_support
1320 = 1;
1321 break;
1322 default:
1323 value = -EOPNOTSUPP;
1325 if (value == 0) {
1326 dum->devstatus |=
1327 (1 << setup.wValue);
1328 maybe_set_status (urb, 0);
1331 } else if (setup.bRequestType == Ep_Request) {
1332 // endpoint halt
1333 ep2 = find_endpoint (dum,
1334 setup.wIndex);
1335 if (!ep2) {
1336 value = -EOPNOTSUPP;
1337 break;
1339 ep2->halted = 1;
1340 value = 0;
1341 maybe_set_status (urb, 0);
1343 break;
1344 case USB_REQ_CLEAR_FEATURE:
1345 if (setup.bRequestType == Dev_Request) {
1346 switch (setup.wValue) {
1347 case USB_DEVICE_REMOTE_WAKEUP:
1348 dum->devstatus &= ~(1 <<
1349 USB_DEVICE_REMOTE_WAKEUP);
1350 value = 0;
1351 maybe_set_status (urb, 0);
1352 break;
1353 default:
1354 value = -EOPNOTSUPP;
1355 break;
1357 } else if (setup.bRequestType == Ep_Request) {
1358 // endpoint halt
1359 ep2 = find_endpoint (dum,
1360 setup.wIndex);
1361 if (!ep2) {
1362 value = -EOPNOTSUPP;
1363 break;
1365 ep2->halted = 0;
1366 value = 0;
1367 maybe_set_status (urb, 0);
1369 break;
1370 case USB_REQ_GET_STATUS:
1371 if (setup.bRequestType == Dev_InRequest
1372 || setup.bRequestType
1373 == Intf_InRequest
1374 || setup.bRequestType
1375 == Ep_InRequest
1377 char *buf;
1379 // device: remote wakeup, selfpowered
1380 // interface: nothing
1381 // endpoint: halt
1382 buf = (char *)urb->transfer_buffer;
1383 if (urb->transfer_buffer_length > 0) {
1384 if (setup.bRequestType ==
1385 Ep_InRequest) {
1386 ep2 = find_endpoint (dum, setup.wIndex);
1387 if (!ep2) {
1388 value = -EOPNOTSUPP;
1389 break;
1391 buf [0] = ep2->halted;
1392 } else if (setup.bRequestType ==
1393 Dev_InRequest) {
1394 buf [0] = (u8)
1395 dum->devstatus;
1396 } else
1397 buf [0] = 0;
1399 if (urb->transfer_buffer_length > 1)
1400 buf [1] = 0;
1401 urb->actual_length = min (2,
1402 urb->transfer_buffer_length);
1403 value = 0;
1404 maybe_set_status (urb, 0);
1406 break;
1409 /* gadget driver handles all other requests. block
1410 * until setup() returns; no reentrancy issues etc.
1412 if (value > 0) {
1413 spin_unlock (&dum->lock);
1414 value = dum->driver->setup (&dum->gadget,
1415 &setup);
1416 spin_lock (&dum->lock);
1418 if (value >= 0) {
1419 /* no delays (max 64KB data stage) */
1420 limit = 64*1024;
1421 goto treat_control_like_bulk;
1423 /* error, see below */
1426 if (value < 0) {
1427 if (value != -EOPNOTSUPP)
1428 dev_dbg (udc_dev(dum),
1429 "setup --> %d\n",
1430 value);
1431 maybe_set_status (urb, -EPIPE);
1432 urb->actual_length = 0;
1435 goto return_urb;
1438 /* non-control requests */
1439 limit = total;
1440 switch (usb_pipetype (urb->pipe)) {
1441 case PIPE_ISOCHRONOUS:
1442 /* FIXME is it urb->interval since the last xfer?
1443 * use urb->iso_frame_desc[i].
1444 * complete whether or not ep has requests queued.
1445 * report random errors, to debug drivers.
1447 limit = max (limit, periodic_bytes (dum, ep));
1448 maybe_set_status (urb, -ENOSYS);
1449 break;
1451 case PIPE_INTERRUPT:
1452 /* FIXME is it urb->interval since the last xfer?
1453 * this almost certainly polls too fast.
1455 limit = max (limit, periodic_bytes (dum, ep));
1456 /* FALLTHROUGH */
1458 // case PIPE_BULK: case PIPE_CONTROL:
1459 default:
1460 treat_control_like_bulk:
1461 ep->last_io = jiffies;
1462 total = transfer (dum, urb, ep, limit);
1463 break;
1466 /* incomplete transfer? */
1467 if (urb->status == -EINPROGRESS)
1468 continue;
1470 return_urb:
1471 urb->hcpriv = NULL;
1472 list_del (&urbp->urbp_list);
1473 kfree (urbp);
1474 if (ep)
1475 ep->already_seen = ep->setup_stage = 0;
1477 spin_unlock (&dum->lock);
1478 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1479 spin_lock (&dum->lock);
1481 goto restart;
1484 /* want a 1 msec delay here */
1485 if (!list_empty (&dum->urbp_list))
1486 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1487 else {
1488 usb_put_dev (dum->udev);
1489 dum->udev = NULL;
1492 spin_unlock_irqrestore (&dum->lock, flags);
1495 /*-------------------------------------------------------------------------*/
1497 #define PORT_C_MASK \
1498 ((USB_PORT_STAT_C_CONNECTION \
1499 | USB_PORT_STAT_C_ENABLE \
1500 | USB_PORT_STAT_C_SUSPEND \
1501 | USB_PORT_STAT_C_OVERCURRENT \
1502 | USB_PORT_STAT_C_RESET) << 16)
1504 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1506 struct dummy *dum;
1507 unsigned long flags;
1508 int retval;
1510 dum = hcd_to_dummy (hcd);
1512 spin_lock_irqsave (&dum->lock, flags);
1514 if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
1515 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1516 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1517 set_link_state (dum);
1520 if (!(dum->port_status & PORT_C_MASK))
1521 retval = 0;
1522 else {
1523 *buf = (1 << 1);
1524 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1525 dum->port_status);
1526 retval = 1;
1528 spin_unlock_irqrestore (&dum->lock, flags);
1529 return retval;
1532 static inline void
1533 hub_descriptor (struct usb_hub_descriptor *desc)
1535 memset (desc, 0, sizeof *desc);
1536 desc->bDescriptorType = 0x29;
1537 desc->bDescLength = 9;
1538 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1539 desc->bNbrPorts = 1;
1540 desc->bitmap [0] = 0xff;
1541 desc->bitmap [1] = 0xff;
1544 static int dummy_hub_control (
1545 struct usb_hcd *hcd,
1546 u16 typeReq,
1547 u16 wValue,
1548 u16 wIndex,
1549 char *buf,
1550 u16 wLength
1552 struct dummy *dum;
1553 int retval = 0;
1554 unsigned long flags;
1556 dum = hcd_to_dummy (hcd);
1557 spin_lock_irqsave (&dum->lock, flags);
1558 switch (typeReq) {
1559 case ClearHubFeature:
1560 break;
1561 case ClearPortFeature:
1562 switch (wValue) {
1563 case USB_PORT_FEAT_SUSPEND:
1564 if (dum->port_status & USB_PORT_STAT_SUSPEND) {
1565 /* 20msec resume signaling */
1566 dum->resuming = 1;
1567 dum->re_timeout = jiffies +
1568 msecs_to_jiffies(20);
1570 break;
1571 case USB_PORT_FEAT_POWER:
1572 if (dum->port_status & USB_PORT_STAT_POWER)
1573 dev_dbg (dummy_dev(dum), "power-off\n");
1574 /* FALLS THROUGH */
1575 default:
1576 dum->port_status &= ~(1 << wValue);
1577 set_link_state (dum);
1579 break;
1580 case GetHubDescriptor:
1581 hub_descriptor ((struct usb_hub_descriptor *) buf);
1582 break;
1583 case GetHubStatus:
1584 *(u32 *) buf = __constant_cpu_to_le32 (0);
1585 break;
1586 case GetPortStatus:
1587 if (wIndex != 1)
1588 retval = -EPIPE;
1590 /* whoever resets or resumes must GetPortStatus to
1591 * complete it!!
1593 if (dum->resuming &&
1594 time_after_eq (jiffies, dum->re_timeout)) {
1595 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1596 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1598 if ((dum->port_status & USB_PORT_STAT_RESET) != 0 &&
1599 time_after_eq (jiffies, dum->re_timeout)) {
1600 dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
1601 dum->port_status &= ~USB_PORT_STAT_RESET;
1602 if (dum->pullup) {
1603 dum->port_status |= USB_PORT_STAT_ENABLE;
1604 /* give it the best speed we agree on */
1605 dum->gadget.speed = dum->driver->speed;
1606 dum->gadget.ep0->maxpacket = 64;
1607 switch (dum->gadget.speed) {
1608 case USB_SPEED_HIGH:
1609 dum->port_status |=
1610 USB_PORT_STAT_HIGH_SPEED;
1611 break;
1612 case USB_SPEED_LOW:
1613 dum->gadget.ep0->maxpacket = 8;
1614 dum->port_status |=
1615 USB_PORT_STAT_LOW_SPEED;
1616 break;
1617 default:
1618 dum->gadget.speed = USB_SPEED_FULL;
1619 break;
1623 set_link_state (dum);
1624 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1625 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1626 break;
1627 case SetHubFeature:
1628 retval = -EPIPE;
1629 break;
1630 case SetPortFeature:
1631 switch (wValue) {
1632 case USB_PORT_FEAT_SUSPEND:
1633 if (dum->active) {
1634 dum->port_status |= USB_PORT_STAT_SUSPEND;
1636 /* HNP would happen here; for now we
1637 * assume b_bus_req is always true.
1639 set_link_state (dum);
1640 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1641 & dum->devstatus) != 0)
1642 dev_dbg (dummy_dev(dum),
1643 "no HNP yet!\n");
1645 break;
1646 case USB_PORT_FEAT_POWER:
1647 dum->port_status |= USB_PORT_STAT_POWER;
1648 set_link_state (dum);
1649 break;
1650 case USB_PORT_FEAT_RESET:
1651 /* if it's already enabled, disable */
1652 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1653 | USB_PORT_STAT_LOW_SPEED
1654 | USB_PORT_STAT_HIGH_SPEED);
1655 /* 50msec reset signaling */
1656 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1657 /* FALLS THROUGH */
1658 default:
1659 if ((dum->port_status & USB_PORT_STAT_POWER) != 0) {
1660 dum->port_status |= (1 << wValue);
1661 set_link_state (dum);
1664 break;
1666 default:
1667 dev_dbg (dummy_dev(dum),
1668 "hub control req%04x v%04x i%04x l%d\n",
1669 typeReq, wValue, wIndex, wLength);
1671 /* "protocol stall" on error */
1672 retval = -EPIPE;
1674 spin_unlock_irqrestore (&dum->lock, flags);
1676 if ((dum->port_status & PORT_C_MASK) != 0)
1677 usb_hcd_poll_rh_status (hcd);
1678 return retval;
1682 /*-------------------------------------------------------------------------*/
1684 static inline ssize_t
1685 show_urb (char *buf, size_t size, struct urb *urb)
1687 int ep = usb_pipeendpoint (urb->pipe);
1689 return snprintf (buf, size,
1690 "urb/%p %s ep%d%s%s len %d/%d\n",
1691 urb,
1692 ({ char *s;
1693 switch (urb->dev->speed) {
1694 case USB_SPEED_LOW: s = "ls"; break;
1695 case USB_SPEED_FULL: s = "fs"; break;
1696 case USB_SPEED_HIGH: s = "hs"; break;
1697 default: s = "?"; break;
1698 }; s; }),
1699 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1700 ({ char *s; \
1701 switch (usb_pipetype (urb->pipe)) { \
1702 case PIPE_CONTROL: s = ""; break; \
1703 case PIPE_BULK: s = "-bulk"; break; \
1704 case PIPE_INTERRUPT: s = "-int"; break; \
1705 default: s = "-iso"; break; \
1706 }; s;}),
1707 urb->actual_length, urb->transfer_buffer_length);
1710 static ssize_t
1711 show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
1713 struct usb_hcd *hcd = dev_get_drvdata (dev);
1714 struct dummy *dum = hcd_to_dummy (hcd);
1715 struct urbp *urbp;
1716 size_t size = 0;
1717 unsigned long flags;
1719 spin_lock_irqsave (&dum->lock, flags);
1720 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1721 size_t temp;
1723 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1724 buf += temp;
1725 size += temp;
1727 spin_unlock_irqrestore (&dum->lock, flags);
1729 return size;
1731 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1733 static int dummy_start (struct usb_hcd *hcd)
1735 struct dummy *dum;
1737 dum = hcd_to_dummy (hcd);
1740 * MASTER side init ... we emulate a root hub that'll only ever
1741 * talk to one device (the slave side). Also appears in sysfs,
1742 * just like more familiar pci-based HCDs.
1744 spin_lock_init (&dum->lock);
1745 init_timer (&dum->timer);
1746 dum->timer.function = dummy_timer;
1747 dum->timer.data = (unsigned long) dum;
1749 INIT_LIST_HEAD (&dum->urbp_list);
1751 /* only show a low-power port: just 8mA */
1752 hcd->power_budget = 8;
1753 hcd->state = HC_STATE_RUNNING;
1754 hcd->uses_new_polling = 1;
1756 #ifdef CONFIG_USB_OTG
1757 hcd->self.otg_port = 1;
1758 #endif
1760 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1761 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1762 return 0;
1765 static void dummy_stop (struct usb_hcd *hcd)
1767 struct dummy *dum;
1769 dum = hcd_to_dummy (hcd);
1771 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1772 usb_gadget_unregister_driver (dum->driver);
1773 dev_info (dummy_dev(dum), "stopped\n");
1776 /*-------------------------------------------------------------------------*/
1778 static int dummy_h_get_frame (struct usb_hcd *hcd)
1780 return dummy_g_get_frame (NULL);
1783 static const struct hc_driver dummy_hcd = {
1784 .description = (char *) driver_name,
1785 .product_desc = "Dummy host controller",
1786 .hcd_priv_size = sizeof(struct dummy),
1788 .flags = HCD_USB2,
1790 .start = dummy_start,
1791 .stop = dummy_stop,
1793 .urb_enqueue = dummy_urb_enqueue,
1794 .urb_dequeue = dummy_urb_dequeue,
1796 .get_frame_number = dummy_h_get_frame,
1798 .hub_status_data = dummy_hub_status,
1799 .hub_control = dummy_hub_control,
1802 static int dummy_hcd_probe (struct device *dev)
1804 struct usb_hcd *hcd;
1805 int retval;
1807 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1809 hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id);
1810 if (!hcd)
1811 return -ENOMEM;
1812 the_controller = hcd_to_dummy (hcd);
1814 retval = usb_add_hcd(hcd, 0, 0);
1815 if (retval != 0) {
1816 usb_put_hcd (hcd);
1817 the_controller = NULL;
1819 return retval;
1822 static int dummy_hcd_remove (struct device *dev)
1824 struct usb_hcd *hcd;
1826 hcd = dev_get_drvdata (dev);
1827 usb_remove_hcd (hcd);
1828 usb_put_hcd (hcd);
1829 the_controller = NULL;
1830 return 0;
1833 static struct device_driver dummy_hcd_driver = {
1834 .name = (char *) driver_name,
1835 .bus = &platform_bus_type,
1836 .probe = dummy_hcd_probe,
1837 .remove = dummy_hcd_remove,
1840 /*-------------------------------------------------------------------------*/
1842 /* These don't need to do anything because the pdev structures are
1843 * statically allocated. */
1844 static void
1845 dummy_udc_release (struct device *dev) {}
1847 static void
1848 dummy_hcd_release (struct device *dev) {}
1850 static struct platform_device the_udc_pdev = {
1851 .name = (char *) gadget_name,
1852 .id = -1,
1853 .dev = {
1854 .release = dummy_udc_release,
1858 static struct platform_device the_hcd_pdev = {
1859 .name = (char *) driver_name,
1860 .id = -1,
1861 .dev = {
1862 .release = dummy_hcd_release,
1866 static int __init init (void)
1868 int retval;
1870 if (usb_disabled ())
1871 return -ENODEV;
1873 retval = driver_register (&dummy_hcd_driver);
1874 if (retval < 0)
1875 return retval;
1877 retval = driver_register (&dummy_udc_driver);
1878 if (retval < 0)
1879 goto err_register_udc_driver;
1881 retval = platform_device_register (&the_hcd_pdev);
1882 if (retval < 0)
1883 goto err_register_hcd;
1885 retval = platform_device_register (&the_udc_pdev);
1886 if (retval < 0)
1887 goto err_register_udc;
1888 return retval;
1890 err_register_udc:
1891 platform_device_unregister (&the_hcd_pdev);
1892 err_register_hcd:
1893 driver_unregister (&dummy_udc_driver);
1894 err_register_udc_driver:
1895 driver_unregister (&dummy_hcd_driver);
1896 return retval;
1898 module_init (init);
1900 static void __exit cleanup (void)
1902 platform_device_unregister (&the_udc_pdev);
1903 platform_device_unregister (&the_hcd_pdev);
1904 driver_unregister (&dummy_udc_driver);
1905 driver_unregister (&dummy_hcd_driver);
1907 module_exit (cleanup);