aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/dummy_hcd.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget/dummy_hcd.c')
-rw-r--r--drivers/usb/gadget/dummy_hcd.c1793
1 files changed, 1793 insertions, 0 deletions
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
new file mode 100644
index 000000000000..8ef8a9cd9ac4
--- /dev/null
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -0,0 +1,1793 @@
1/*
2 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
3 *
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5 *
6 * Copyright (C) 2003 David Brownell
7 * Copyright (C) 2003-2005 Alan Stern
8 *
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.
13 *
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.
18 *
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
22 */
23
24
25/*
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:
29 *
30 * - Gadget driver, responding to requests (slave);
31 * - Host-side device driver, as already familiar in Linux.
32 *
33 * Having this all in one kernel can help some stages of development,
34 * bypassing some hardware (and driver) issues. UML could help too.
35 */
36
37#define DEBUG
38
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>
53
54#include <linux/usb.h>
55#include <linux/usb_gadget.h>
56
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>
62
63
64#include "../core/hcd.h"
65
66
67#define DRIVER_DESC "USB Host+Gadget Emulator"
68#define DRIVER_VERSION "17 Dec 2004"
69
70static const char driver_name [] = "dummy_hcd";
71static const char driver_desc [] = "USB Host+Gadget Emulator";
72
73static const char gadget_name [] = "dummy_udc";
74
75MODULE_DESCRIPTION (DRIVER_DESC);
76MODULE_AUTHOR ("David Brownell");
77MODULE_LICENSE ("GPL");
78
79/*-------------------------------------------------------------------------*/
80
81/* gadget side driver data structres */
82struct 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;
91};
92
93struct dummy_request {
94 struct list_head queue; /* ep's requests */
95 struct usb_request req;
96};
97
98static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
99{
100 return container_of (_ep, struct dummy_ep, ep);
101}
102
103static inline struct dummy_request *usb_request_to_dummy_request
104 (struct usb_request *_req)
105{
106 return container_of (_req, struct dummy_request, req);
107}
108
109/*-------------------------------------------------------------------------*/
110
111/*
112 * Every device has ep0 for control requests, plus up to 30 more endpoints,
113 * in one of two types:
114 *
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.
117 *
118 * - Fixed Function: in other cases. some characteristics may be mutable;
119 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
120 *
121 * Gadget drivers are responsible for not setting up conflicting endpoint
122 * configurations, illegal or unsupported packet lengths, and so on.
123 */
124
125static const char ep0name [] = "ep0";
126
127static const char *const ep_name [] = {
128 ep0name, /* everyone has ep0 */
129
130 /* act like a net2280: high speed, six configurable endpoints */
131 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
132
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",
138
139 /* or like sa1100: two fixed function endpoints */
140 "ep1out-bulk", "ep2in-bulk",
141};
142#define DUMMY_ENDPOINTS (sizeof(ep_name)/sizeof(char *))
143
144#define FIFO_SIZE 64
145
146struct urbp {
147 struct urb *urb;
148 struct list_head urbp_list;
149};
150
151struct dummy {
152 spinlock_t lock;
153
154 /*
155 * SLAVE/GADGET side support
156 */
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;
164
165 /*
166 * MASTER/HOST side support
167 */
168 struct timer_list timer;
169 u32 port_status;
170 unsigned resuming:1;
171 unsigned long re_timeout;
172
173 struct usb_device *udev;
174 struct list_head urbp_list;
175};
176
177static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
178{
179 return (struct dummy *) (hcd->hcd_priv);
180}
181
182static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
183{
184 return container_of((void *) dum, struct usb_hcd, hcd_priv);
185}
186
187static inline struct device *dummy_dev (struct dummy *dum)
188{
189 return dummy_to_hcd(dum)->self.controller;
190}
191
192static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
193{
194 return container_of (ep->gadget, struct dummy, gadget);
195}
196
197static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
198{
199 return container_of (gadget, struct dummy, gadget);
200}
201
202static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
203{
204 return container_of (dev, struct dummy, gadget.dev);
205}
206
207static struct dummy *the_controller;
208
209/*-------------------------------------------------------------------------*/
210
211/*
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.
214 */
215static struct platform_device the_pdev;
216
217static struct device_driver dummy_driver = {
218 .name = (char *) driver_name,
219 .bus = &platform_bus_type,
220};
221
222/*-------------------------------------------------------------------------*/
223
224/* SLAVE/GADGET SIDE DRIVER
225 *
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.
229 */
230
231#define is_enabled(dum) \
232 (dum->port_status & USB_PORT_STAT_ENABLE)
233
234static int
235dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
236{
237 struct dummy *dum;
238 struct dummy_ep *ep;
239 unsigned max;
240 int retval;
241
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;
250
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.
254 *
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.)
258 */
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;
265 }
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;
279 }
280 }
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;
299 }
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;
317 }
318 break;
319 default:
320 /* few chips support control except on ep0 */
321 goto done;
322 }
323
324 _ep->maxpacket = max;
325 ep->desc = desc;
326
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);
339
340 /* at this point real hardware should be NAKing transfers
341 * to that endpoint, until a buffer is queued to it.
342 */
343 retval = 0;
344done:
345 return retval;
346}
347
348/* called with spinlock held */
349static void nuke (struct dummy *dum, struct dummy_ep *ep)
350{
351 while (!list_empty (&ep->queue)) {
352 struct dummy_request *req;
353
354 req = list_entry (ep->queue.next, struct dummy_request, queue);
355 list_del_init (&req->queue);
356 req->req.status = -ESHUTDOWN;
357
358 spin_unlock (&dum->lock);
359 req->req.complete (&ep->ep, &req->req);
360 spin_lock (&dum->lock);
361 }
362}
363
364static int dummy_disable (struct usb_ep *_ep)
365{
366 struct dummy_ep *ep;
367 struct dummy *dum;
368 unsigned long flags;
369 int retval;
370
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);
375
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);
381
382 dev_dbg (dummy_dev(dum), "disabled %s\n", _ep->name);
383 return retval;
384}
385
386static struct usb_request *
387dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
388{
389 struct dummy_ep *ep;
390 struct dummy_request *req;
391
392 if (!_ep)
393 return NULL;
394 ep = usb_ep_to_dummy_ep (_ep);
395
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;
402}
403
404static void
405dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
406{
407 struct dummy_ep *ep;
408 struct dummy_request *req;
409
410 ep = usb_ep_to_dummy_ep (_ep);
411 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
412 return;
413
414 req = usb_request_to_dummy_request (_req);
415 WARN_ON (!list_empty (&req->queue));
416 kfree (req);
417}
418
419static void *
420dummy_alloc_buffer (
421 struct usb_ep *_ep,
422 unsigned bytes,
423 dma_addr_t *dma,
424 int mem_flags
425) {
426 char *retval;
427 struct dummy_ep *ep;
428 struct dummy *dum;
429
430 ep = usb_ep_to_dummy_ep (_ep);
431 dum = ep_to_dummy (ep);
432
433 if (!dum->driver)
434 return NULL;
435 retval = kmalloc (bytes, mem_flags);
436 *dma = (dma_addr_t) retval;
437 return retval;
438}
439
440static void
441dummy_free_buffer (
442 struct usb_ep *_ep,
443 void *buf,
444 dma_addr_t dma,
445 unsigned bytes
446) {
447 if (bytes)
448 kfree (buf);
449}
450
451static void
452fifo_complete (struct usb_ep *ep, struct usb_request *req)
453{
454}
455
456static int
457dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
458{
459 struct dummy_ep *ep;
460 struct dummy_request *req;
461 struct dummy *dum;
462 unsigned long flags;
463
464 req = usb_request_to_dummy_request (_req);
465 if (!_req || !list_empty (&req->queue) || !_req->complete)
466 return -EINVAL;
467
468 ep = usb_ep_to_dummy_ep (_ep);
469 if (!_ep || (!ep->desc && _ep->name != ep0name))
470 return -EINVAL;
471
472 dum = ep_to_dummy (ep);
473 if (!dum->driver || !is_enabled (dum))
474 return -ESHUTDOWN;
475
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
480
481 _req->status = -EINPROGRESS;
482 _req->actual = 0;
483 spin_lock_irqsave (&dum->lock, flags);
484
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;
496
497 spin_unlock (&dum->lock);
498 _req->actual = _req->length;
499 _req->status = 0;
500 _req->complete (_ep, _req);
501 spin_lock (&dum->lock);
502 }
503 list_add_tail (&req->queue, &ep->queue);
504 spin_unlock_irqrestore (&dum->lock, flags);
505
506 /* real hardware would likely enable transfers here, in case
507 * it'd been left NAKing.
508 */
509 return 0;
510}
511
512static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
513{
514 struct dummy_ep *ep;
515 struct dummy *dum;
516 int retval = -EINVAL;
517 unsigned long flags;
518 struct dummy_request *req = NULL;
519
520 if (!_ep || !_req)
521 return retval;
522 ep = usb_ep_to_dummy_ep (_ep);
523 dum = ep_to_dummy (ep);
524
525 if (!dum->driver)
526 return -ESHUTDOWN;
527
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;
535 }
536 }
537 spin_unlock_irqrestore (&dum->lock, flags);
538
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);
544 }
545 return retval;
546}
547
548static int
549dummy_set_halt (struct usb_ep *_ep, int value)
550{
551 struct dummy_ep *ep;
552 struct dummy *dum;
553
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;
569}
570
571static const struct usb_ep_ops dummy_ep_ops = {
572 .enable = dummy_enable,
573 .disable = dummy_disable,
574
575 .alloc_request = dummy_alloc_request,
576 .free_request = dummy_free_request,
577
578 .alloc_buffer = dummy_alloc_buffer,
579 .free_buffer = dummy_free_buffer,
580 /* map, unmap, ... eventually hook the "generic" dma calls */
581
582 .queue = dummy_queue,
583 .dequeue = dummy_dequeue,
584
585 .set_halt = dummy_set_halt,
586};
587
588/*-------------------------------------------------------------------------*/
589
590/* there are both host and device side versions of this call ... */
591static int dummy_g_get_frame (struct usb_gadget *_gadget)
592{
593 struct timeval tv;
594
595 do_gettimeofday (&tv);
596 return tv.tv_usec / 1000;
597}
598
599static int dummy_wakeup (struct usb_gadget *_gadget)
600{
601 struct dummy *dum;
602
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;
607
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;
612}
613
614static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
615{
616 struct dummy *dum;
617
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;
624}
625
626static const struct usb_gadget_ops dummy_ops = {
627 .get_frame = dummy_g_get_frame,
628 .wakeup = dummy_wakeup,
629 .set_selfpowered = dummy_set_selfpowered,
630};
631
632/*-------------------------------------------------------------------------*/
633
634/* "function" sysfs attribute */
635static ssize_t
636show_function (struct device *dev, char *buf)
637{
638 struct dummy *dum = gadget_dev_to_dummy (dev);
639
640 if (!dum->driver || !dum->driver->function)
641 return 0;
642 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
643}
644DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
645
646/*-------------------------------------------------------------------------*/
647
648/*
649 * Driver registration/unregistration.
650 *
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.
657 *
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.
660 */
661
662static void
663dummy_udc_release (struct device *dev)
664{
665}
666
667static void
668dummy_pdev_release (struct device *dev)
669{
670}
671
672static int
673dummy_register_udc (struct dummy *dum)
674{
675 int rc;
676
677 strcpy (dum->gadget.dev.bus_id, "udc");
678 dum->gadget.dev.parent = dummy_dev(dum);
679 dum->gadget.dev.release = dummy_udc_release;
680
681 rc = device_register (&dum->gadget.dev);
682 if (rc == 0)
683 device_create_file (&dum->gadget.dev, &dev_attr_function);
684 return rc;
685}
686
687static void
688dummy_unregister_udc (struct dummy *dum)
689{
690 device_remove_file (&dum->gadget.dev, &dev_attr_function);
691 device_unregister (&dum->gadget.dev);
692}
693
694int
695usb_gadget_register_driver (struct usb_gadget_driver *driver)
696{
697 struct dummy *dum = the_controller;
698 int retval, i;
699
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;
707
708 /*
709 * SLAVE side init ... the layer above hardware, which
710 * can't enumerate without help from the driver we're binding.
711 */
712 dum->gadget.name = gadget_name;
713 dum->gadget.ops = &dummy_ops;
714 dum->gadget.is_dualspeed = 1;
715
716 dum->devstatus = 0;
717 dum->resuming = 0;
718
719 INIT_LIST_HEAD (&dum->gadget.ep_list);
720 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
721 struct dummy_ep *ep = &dum->ep [i];
722
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);
734 }
735
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);
740
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;
749 }
750
751 // FIXME: Check these calls for errors and re-order
752 driver->driver.bus = dum->gadget.dev.parent->bus;
753 driver_register (&driver->driver);
754
755 device_bind_driver (&dum->gadget.dev);
756
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;
761}
762EXPORT_SYMBOL (usb_gadget_register_driver);
763
764/* caller must hold lock */
765static void
766stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
767{
768 struct dummy_ep *ep;
769
770 /* prevent any more requests */
771 dum->address = 0;
772
773 /* The timer is left running so that outstanding URBs can fail */
774
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);
778
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);
784 }
785}
786
787int
788usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
789{
790 struct dummy *dum = the_controller;
791 unsigned long flags;
792
793 if (!dum)
794 return -ENODEV;
795 if (!driver || driver != dum->driver)
796 return -EINVAL;
797
798 dev_dbg (dummy_dev(dum), "unregister gadget driver '%s'\n",
799 driver->driver.name);
800
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);
807
808 driver->unbind (&dum->gadget);
809 dum->driver = NULL;
810
811 device_release_driver (&dum->gadget.dev);
812
813 driver_unregister (&driver->driver);
814
815 return 0;
816}
817EXPORT_SYMBOL (usb_gadget_unregister_driver);
818
819#undef is_enabled
820
821int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
822{
823 return -ENOSYS;
824}
825EXPORT_SYMBOL (net2280_set_fifo_mode);
826
827/*-------------------------------------------------------------------------*/
828
829/* MASTER/HOST SIDE DRIVER
830 *
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.
834 *
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.
839 */
840
841static int dummy_urb_enqueue (
842 struct usb_hcd *hcd,
843 struct usb_host_endpoint *ep,
844 struct urb *urb,
845 int mem_flags
846) {
847 struct dummy *dum;
848 struct urbp *urbp;
849 unsigned long flags;
850
851 if (!urb->transfer_buffer && urb->transfer_buffer_length)
852 return -EINVAL;
853
854 urbp = kmalloc (sizeof *urbp, mem_flags);
855 if (!urbp)
856 return -ENOMEM;
857 urbp->urb = urb;
858
859 dum = hcd_to_dummy (hcd);
860 spin_lock_irqsave (&dum->lock, flags);
861
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");
867
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 */
872
873 /* kick the scheduler, it'll do the rest */
874 if (!timer_pending (&dum->timer))
875 mod_timer (&dum->timer, jiffies + 1);
876
877 spin_unlock_irqrestore (&dum->lock, flags);
878 return 0;
879}
880
881static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
882{
883 /* giveback happens automatically in timer callback */
884 return 0;
885}
886
887static void maybe_set_status (struct urb *urb, int status)
888{
889 spin_lock (&urb->lock);
890 if (urb->status == -EINPROGRESS)
891 urb->status = status;
892 spin_unlock (&urb->lock);
893}
894
895/* transfer up to a frame's worth; caller must own lock */
896static int
897transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
898{
899 struct dummy_request *req;
900
901top:
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;
907
908 /* 1..N packets of ep->ep.maxpacket each ... the last one
909 * may be short (including zero length).
910 *
911 * writer can send a zlp explicitly (length 0) or implicitly
912 * (length mod maxpacket zero, and 'zero' flag); they always
913 * terminate reads.
914 */
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);
918
919 /* FIXME update emulated data toggle too */
920
921 to_host = usb_pipein (urb->pipe);
922 if (unlikely (len == 0))
923 is_short = 1;
924 else {
925 char *ubuf, *rbuf;
926
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;
933
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);
938 }
939 is_short = (len % ep->ep.maxpacket) != 0;
940
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;
949
950 limit -= len;
951 urb->actual_length += len;
952 req->req.actual += len;
953 }
954
955 /* short packets terminate, maybe with overflow/underflow.
956 * it's only really an error to write too much.
957 *
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.
962 */
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;
982 }
983
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);
993 }
994 }
995
996 /* device side completion --> continuable */
997 if (req->req.status != -EINPROGRESS) {
998 list_del_init (&req->queue);
999
1000 spin_unlock (&dum->lock);
1001 req->req.complete (&ep->ep, &req->req);
1002 spin_lock (&dum->lock);
1003
1004 /* requests might have been unlinked... */
1005 rescan = 1;
1006 }
1007
1008 /* host side completion --> terminate */
1009 if (urb->status != -EINPROGRESS)
1010 break;
1011
1012 /* rescan to continue with any other queued i/o */
1013 if (rescan)
1014 goto top;
1015 }
1016 return limit;
1017}
1018
1019static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1020{
1021 int limit = ep->ep.maxpacket;
1022
1023 if (dum->gadget.speed == USB_SPEED_HIGH) {
1024 int tmp;
1025
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;
1032 }
1033 return limit;
1034}
1035
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))
1040
1041static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1042{
1043 int i;
1044
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];
1051
1052 if (!ep->desc)
1053 continue;
1054 if (ep->desc->bEndpointAddress == address)
1055 return ep;
1056 }
1057 return NULL;
1058}
1059
1060#undef is_active
1061
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)
1068
1069/* drive both sides of the transfers; looks like irq handlers to
1070 * both drivers except the callbacks aren't in_irq().
1071 */
1072static void dummy_timer (unsigned long _dum)
1073{
1074 struct dummy *dum = (struct dummy *) _dum;
1075 struct urbp *urbp, *tmp;
1076 unsigned long flags;
1077 int limit, total;
1078 int i;
1079
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;
1094 }
1095
1096 /* FIXME if HZ != 1000 this will probably misbehave ... */
1097
1098 /* look at each urb queued by the host side driver */
1099 spin_lock_irqsave (&dum->lock, flags);
1100
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;
1106 }
1107
1108 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1109 if (!ep_name [i])
1110 break;
1111 dum->ep [i].already_seen = 0;
1112 }
1113
1114restart:
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;
1121
1122 urb = urbp->urb;
1123 if (urb->status != -EINPROGRESS) {
1124 /* likely it was just unlinked */
1125 goto return_urb;
1126 }
1127 type = usb_pipetype (urb->pipe);
1128
1129 /* used up this frame's non-periodic bandwidth?
1130 * FIXME there's infinite bandwidth for control and
1131 * periodic transfers ... unrealistic.
1132 */
1133 if (total <= 0 && type == PIPE_BULK)
1134 continue;
1135
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;
1148 }
1149
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;
1156 }
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;
1163 }
1164 /* FIXME make sure both ends agree on maxpacket */
1165
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;
1171
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;
1179 }
1180
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);
1187
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;
1193 }
1194
1195 /* gadget driver never sees set_address or operations
1196 * on standard feature flags. some hardware doesn't
1197 * even expose them.
1198 */
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;
1220 }
1221 if (value == 0) {
1222 dum->devstatus |=
1223 (1 << setup.wValue);
1224 maybe_set_status (urb, 0);
1225 }
1226
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;
1234 }
1235 ep2->halted = 1;
1236 value = 0;
1237 maybe_set_status (urb, 0);
1238 }
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;
1252 }
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;
1260 }
1261 ep2->halted = 0;
1262 value = 0;
1263 maybe_set_status (urb, 0);
1264 }
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
1272 ) {
1273 char *buf;
1274
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;
1286 }
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;
1294 }
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);
1301 }
1302 break;
1303 }
1304
1305 /* gadget driver handles all other requests. block
1306 * until setup() returns; no reentrancy issues etc.
1307 */
1308 if (value > 0) {
1309 spin_unlock (&dum->lock);
1310 value = dum->driver->setup (&dum->gadget,
1311 &setup);
1312 spin_lock (&dum->lock);
1313
1314 if (value >= 0) {
1315 /* no delays (max 64KB data stage) */
1316 limit = 64*1024;
1317 goto treat_control_like_bulk;
1318 }
1319 /* error, see below */
1320 }
1321
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;
1329 }
1330
1331 goto return_urb;
1332 }
1333
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.
1342 */
1343 limit = max (limit, periodic_bytes (dum, ep));
1344 maybe_set_status (urb, -ENOSYS);
1345 break;
1346
1347 case PIPE_INTERRUPT:
1348 /* FIXME is it urb->interval since the last xfer?
1349 * this almost certainly polls too fast.
1350 */
1351 limit = max (limit, periodic_bytes (dum, ep));
1352 /* FALLTHROUGH */
1353
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;
1360 }
1361
1362 /* incomplete transfer? */
1363 if (urb->status == -EINPROGRESS)
1364 continue;
1365
1366return_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;
1372
1373 spin_unlock (&dum->lock);
1374 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1375 spin_lock (&dum->lock);
1376
1377 goto restart;
1378 }
1379
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;
1386 }
1387
1388 spin_unlock_irqrestore (&dum->lock, flags);
1389}
1390
1391/*-------------------------------------------------------------------------*/
1392
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))
1399
1400static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1401{
1402 struct dummy *dum;
1403 unsigned long flags;
1404 int retval;
1405
1406 dum = hcd_to_dummy (hcd);
1407
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;
1416 }
1417 spin_unlock_irqrestore (&dum->lock, flags);
1418 return retval;
1419}
1420
1421static inline void
1422hub_descriptor (struct usb_hub_descriptor *desc)
1423{
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;
1431}
1432
1433static int dummy_hub_control (
1434 struct usb_hcd *hcd,
1435 u16 typeReq,
1436 u16 wValue,
1437 u16 wIndex,
1438 char *buf,
1439 u16 wLength
1440) {
1441 struct dummy *dum;
1442 int retval = 0;
1443 unsigned long flags;
1444
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);
1458 }
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);
1467 }
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;
1478
1479 /* whoever resets or resumes must GetPortStatus to
1480 * complete it!!
1481 */
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);
1491 }
1492 }
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;
1516 }
1517 }
1518 }
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);
1536 }
1537 }
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);
1549 }
1550
1551 /* FIXME test that code path! */
1552 }
1553 /* 50msec reset signaling */
1554 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1555 /* FALLTHROUGH */
1556 default:
1557 dum->port_status |= (1 << wValue);
1558 }
1559 break;
1560
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);
1565
1566 /* "protocol stall" on error */
1567 retval = -EPIPE;
1568 }
1569 spin_unlock_irqrestore (&dum->lock, flags);
1570 return retval;
1571}
1572
1573
1574/*-------------------------------------------------------------------------*/
1575
1576static inline ssize_t
1577show_urb (char *buf, size_t size, struct urb *urb)
1578{
1579 int ep = usb_pipeendpoint (urb->pipe);
1580
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);
1600}
1601
1602static ssize_t
1603show_urbs (struct device *dev, char *buf)
1604{
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;
1610
1611 spin_lock_irqsave (&dum->lock, flags);
1612 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1613 size_t temp;
1614
1615 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1616 buf += temp;
1617 size += temp;
1618 }
1619 spin_unlock_irqrestore (&dum->lock, flags);
1620
1621 return size;
1622}
1623static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1624
1625static int dummy_start (struct usb_hcd *hcd)
1626{
1627 struct dummy *dum;
1628 struct usb_device *root;
1629 int retval;
1630
1631 dum = hcd_to_dummy (hcd);
1632
1633 /*
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.
1637 */
1638 spin_lock_init (&dum->lock);
1639 init_timer (&dum->timer);
1640 dum->timer.function = dummy_timer;
1641 dum->timer.data = (unsigned long) dum;
1642
1643 INIT_LIST_HEAD (&dum->urbp_list);
1644
1645 root = usb_alloc_dev (NULL, &hcd->self, 0);
1646 if (!root)
1647 return -ENOMEM;
1648
1649 /* root hub enters addressed state... */
1650 hcd->state = HC_STATE_RUNNING;
1651 root->speed = USB_SPEED_HIGH;
1652
1653 /* ...then configured, so khubd sees us. */
1654 if ((retval = usb_hcd_register_root_hub (root, hcd)) != 0) {
1655 goto err1;
1656 }
1657
1658 /* only show a low-power port: just 8mA */
1659 hub_set_power_budget (root, 8);
1660
1661 if ((retval = dummy_register_udc (dum)) != 0)
1662 goto err2;
1663
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;
1667
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;
1674}
1675
1676static void dummy_stop (struct usb_hcd *hcd)
1677{
1678 struct dummy *dum;
1679
1680 dum = hcd_to_dummy (hcd);
1681
1682 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1683
1684 usb_gadget_unregister_driver (dum->driver);
1685 dummy_unregister_udc (dum);
1686
1687 dev_info (dummy_dev(dum), "stopped\n");
1688}
1689
1690/*-------------------------------------------------------------------------*/
1691
1692static int dummy_h_get_frame (struct usb_hcd *hcd)
1693{
1694 return dummy_g_get_frame (NULL);
1695}
1696
1697static const struct hc_driver dummy_hcd = {
1698 .description = (char *) driver_name,
1699 .product_desc = "Dummy host controller",
1700 .hcd_priv_size = sizeof(struct dummy),
1701
1702 .flags = HCD_USB2,
1703
1704 .start = dummy_start,
1705 .stop = dummy_stop,
1706
1707 .urb_enqueue = dummy_urb_enqueue,
1708 .urb_dequeue = dummy_urb_dequeue,
1709
1710 .get_frame_number = dummy_h_get_frame,
1711
1712 .hub_status_data = dummy_hub_status,
1713 .hub_control = dummy_hub_control,
1714};
1715
1716static int dummy_probe (struct device *dev)
1717{
1718 struct usb_hcd *hcd;
1719 int retval;
1720
1721 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1722
1723 hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id);
1724 if (!hcd)
1725 return -ENOMEM;
1726 the_controller = hcd_to_dummy (hcd);
1727
1728 retval = usb_add_hcd(hcd, 0, 0);
1729 if (retval != 0) {
1730 usb_put_hcd (hcd);
1731 the_controller = NULL;
1732 }
1733 return retval;
1734}
1735
1736static void dummy_remove (struct device *dev)
1737{
1738 struct usb_hcd *hcd;
1739
1740 hcd = dev_get_drvdata (dev);
1741 usb_remove_hcd (hcd);
1742 usb_put_hcd (hcd);
1743 the_controller = NULL;
1744}
1745
1746/*-------------------------------------------------------------------------*/
1747
1748static int dummy_pdev_detect (void)
1749{
1750 int retval;
1751
1752 retval = driver_register (&dummy_driver);
1753 if (retval < 0)
1754 return retval;
1755
1756 the_pdev.name = "hc";
1757 the_pdev.dev.driver = &dummy_driver;
1758 the_pdev.dev.release = dummy_pdev_release;
1759
1760 retval = platform_device_register (&the_pdev);
1761 if (retval < 0)
1762 driver_unregister (&dummy_driver);
1763 return retval;
1764}
1765
1766static void dummy_pdev_remove (void)
1767{
1768 platform_device_unregister (&the_pdev);
1769 driver_unregister (&dummy_driver);
1770}
1771
1772/*-------------------------------------------------------------------------*/
1773
1774static int __init init (void)
1775{
1776 int retval;
1777
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;
1785}
1786module_init (init);
1787
1788static void __exit cleanup (void)
1789{
1790 dummy_remove (&the_pdev.dev);
1791 dummy_pdev_remove ();
1792}
1793module_exit (cleanup);