blob: dc0e3233b0e942119acfaea1d77ec89ce4dba268 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Alan Sternd9b76252005-05-03 16:15:43 -0400144/*-------------------------------------------------------------------------*/
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define FIFO_SIZE 64
147
148struct urbp {
149 struct urb *urb;
150 struct list_head urbp_list;
151};
152
153struct dummy {
154 spinlock_t lock;
155
156 /*
157 * SLAVE/GADGET side support
158 */
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
167 /*
168 * MASTER/HOST side support
169 */
170 struct timer_list timer;
171 u32 port_status;
172 unsigned resuming:1;
173 unsigned long re_timeout;
174
175 struct usb_device *udev;
176 struct list_head urbp_list;
177};
178
179static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
180{
181 return (struct dummy *) (hcd->hcd_priv);
182}
183
184static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
185{
186 return container_of((void *) dum, struct usb_hcd, hcd_priv);
187}
188
189static inline struct device *dummy_dev (struct dummy *dum)
190{
191 return dummy_to_hcd(dum)->self.controller;
192}
193
Alan Sternd9b76252005-05-03 16:15:43 -0400194static inline struct device *udc_dev (struct dummy *dum)
195{
196 return dum->gadget.dev.parent;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
200{
201 return container_of (ep->gadget, struct dummy, gadget);
202}
203
204static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
205{
206 return container_of (gadget, struct dummy, gadget);
207}
208
209static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
210{
211 return container_of (dev, struct dummy, gadget.dev);
212}
213
214static struct dummy *the_controller;
215
216/*-------------------------------------------------------------------------*/
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/* SLAVE/GADGET SIDE DRIVER
219 *
220 * This only tracks gadget state. All the work is done when the host
221 * side tries some (emulated) i/o operation. Real device controller
222 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
223 */
224
225#define is_enabled(dum) \
226 (dum->port_status & USB_PORT_STAT_ENABLE)
227
228static int
229dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
230{
231 struct dummy *dum;
232 struct dummy_ep *ep;
233 unsigned max;
234 int retval;
235
236 ep = usb_ep_to_dummy_ep (_ep);
237 if (!_ep || !desc || ep->desc || _ep->name == ep0name
238 || desc->bDescriptorType != USB_DT_ENDPOINT)
239 return -EINVAL;
240 dum = ep_to_dummy (ep);
241 if (!dum->driver || !is_enabled (dum))
242 return -ESHUTDOWN;
243 max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
244
245 /* drivers must not request bad settings, since lower levels
246 * (hardware or its drivers) may not check. some endpoints
247 * can't do iso, many have maxpacket limitations, etc.
248 *
249 * since this "hardware" driver is here to help debugging, we
250 * have some extra sanity checks. (there could be more though,
251 * especially for "ep9out" style fixed function ones.)
252 */
253 retval = -EINVAL;
254 switch (desc->bmAttributes & 0x03) {
255 case USB_ENDPOINT_XFER_BULK:
256 if (strstr (ep->ep.name, "-iso")
257 || strstr (ep->ep.name, "-int")) {
258 goto done;
259 }
260 switch (dum->gadget.speed) {
261 case USB_SPEED_HIGH:
262 if (max == 512)
263 break;
264 /* conserve return statements */
265 default:
266 switch (max) {
267 case 8: case 16: case 32: case 64:
268 /* we'll fake any legal size */
269 break;
270 default:
271 case USB_SPEED_LOW:
272 goto done;
273 }
274 }
275 break;
276 case USB_ENDPOINT_XFER_INT:
277 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
278 goto done;
279 /* real hardware might not handle all packet sizes */
280 switch (dum->gadget.speed) {
281 case USB_SPEED_HIGH:
282 if (max <= 1024)
283 break;
284 /* save a return statement */
285 case USB_SPEED_FULL:
286 if (max <= 64)
287 break;
288 /* save a return statement */
289 default:
290 if (max <= 8)
291 break;
292 goto done;
293 }
294 break;
295 case USB_ENDPOINT_XFER_ISOC:
296 if (strstr (ep->ep.name, "-bulk")
297 || strstr (ep->ep.name, "-int"))
298 goto done;
299 /* real hardware might not handle all packet sizes */
300 switch (dum->gadget.speed) {
301 case USB_SPEED_HIGH:
302 if (max <= 1024)
303 break;
304 /* save a return statement */
305 case USB_SPEED_FULL:
306 if (max <= 1023)
307 break;
308 /* save a return statement */
309 default:
310 goto done;
311 }
312 break;
313 default:
314 /* few chips support control except on ep0 */
315 goto done;
316 }
317
318 _ep->maxpacket = max;
319 ep->desc = desc;
320
Alan Sternd9b76252005-05-03 16:15:43 -0400321 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 _ep->name,
323 desc->bEndpointAddress & 0x0f,
324 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
325 ({ char *val;
326 switch (desc->bmAttributes & 0x03) {
327 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
328 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
329 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
330 default: val = "ctrl"; break;
331 }; val; }),
332 max);
333
334 /* at this point real hardware should be NAKing transfers
335 * to that endpoint, until a buffer is queued to it.
336 */
337 retval = 0;
338done:
339 return retval;
340}
341
342/* called with spinlock held */
343static void nuke (struct dummy *dum, struct dummy_ep *ep)
344{
345 while (!list_empty (&ep->queue)) {
346 struct dummy_request *req;
347
348 req = list_entry (ep->queue.next, struct dummy_request, queue);
349 list_del_init (&req->queue);
350 req->req.status = -ESHUTDOWN;
351
352 spin_unlock (&dum->lock);
353 req->req.complete (&ep->ep, &req->req);
354 spin_lock (&dum->lock);
355 }
356}
357
358static int dummy_disable (struct usb_ep *_ep)
359{
360 struct dummy_ep *ep;
361 struct dummy *dum;
362 unsigned long flags;
363 int retval;
364
365 ep = usb_ep_to_dummy_ep (_ep);
366 if (!_ep || !ep->desc || _ep->name == ep0name)
367 return -EINVAL;
368 dum = ep_to_dummy (ep);
369
370 spin_lock_irqsave (&dum->lock, flags);
371 ep->desc = NULL;
372 retval = 0;
373 nuke (dum, ep);
374 spin_unlock_irqrestore (&dum->lock, flags);
375
Alan Sternd9b76252005-05-03 16:15:43 -0400376 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return retval;
378}
379
380static struct usb_request *
381dummy_alloc_request (struct usb_ep *_ep, int mem_flags)
382{
383 struct dummy_ep *ep;
384 struct dummy_request *req;
385
386 if (!_ep)
387 return NULL;
388 ep = usb_ep_to_dummy_ep (_ep);
389
390 req = kmalloc (sizeof *req, mem_flags);
391 if (!req)
392 return NULL;
393 memset (req, 0, sizeof *req);
394 INIT_LIST_HEAD (&req->queue);
395 return &req->req;
396}
397
398static void
399dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
400{
401 struct dummy_ep *ep;
402 struct dummy_request *req;
403
404 ep = usb_ep_to_dummy_ep (_ep);
405 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
406 return;
407
408 req = usb_request_to_dummy_request (_req);
409 WARN_ON (!list_empty (&req->queue));
410 kfree (req);
411}
412
413static void *
414dummy_alloc_buffer (
415 struct usb_ep *_ep,
416 unsigned bytes,
417 dma_addr_t *dma,
418 int mem_flags
419) {
420 char *retval;
421 struct dummy_ep *ep;
422 struct dummy *dum;
423
424 ep = usb_ep_to_dummy_ep (_ep);
425 dum = ep_to_dummy (ep);
426
427 if (!dum->driver)
428 return NULL;
429 retval = kmalloc (bytes, mem_flags);
430 *dma = (dma_addr_t) retval;
431 return retval;
432}
433
434static void
435dummy_free_buffer (
436 struct usb_ep *_ep,
437 void *buf,
438 dma_addr_t dma,
439 unsigned bytes
440) {
441 if (bytes)
442 kfree (buf);
443}
444
445static void
446fifo_complete (struct usb_ep *ep, struct usb_request *req)
447{
448}
449
450static int
451dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags)
452{
453 struct dummy_ep *ep;
454 struct dummy_request *req;
455 struct dummy *dum;
456 unsigned long flags;
457
458 req = usb_request_to_dummy_request (_req);
459 if (!_req || !list_empty (&req->queue) || !_req->complete)
460 return -EINVAL;
461
462 ep = usb_ep_to_dummy_ep (_ep);
463 if (!_ep || (!ep->desc && _ep->name != ep0name))
464 return -EINVAL;
465
466 dum = ep_to_dummy (ep);
467 if (!dum->driver || !is_enabled (dum))
468 return -ESHUTDOWN;
469
470#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400471 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 ep, _req, _ep->name, _req->length, _req->buf);
473#endif
474
475 _req->status = -EINPROGRESS;
476 _req->actual = 0;
477 spin_lock_irqsave (&dum->lock, flags);
478
479 /* implement an emulated single-request FIFO */
480 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
481 list_empty (&dum->fifo_req.queue) &&
482 list_empty (&ep->queue) &&
483 _req->length <= FIFO_SIZE) {
484 req = &dum->fifo_req;
485 req->req = *_req;
486 req->req.buf = dum->fifo_buf;
487 memcpy (dum->fifo_buf, _req->buf, _req->length);
488 req->req.context = dum;
489 req->req.complete = fifo_complete;
490
491 spin_unlock (&dum->lock);
492 _req->actual = _req->length;
493 _req->status = 0;
494 _req->complete (_ep, _req);
495 spin_lock (&dum->lock);
496 }
497 list_add_tail (&req->queue, &ep->queue);
498 spin_unlock_irqrestore (&dum->lock, flags);
499
500 /* real hardware would likely enable transfers here, in case
501 * it'd been left NAKing.
502 */
503 return 0;
504}
505
506static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
507{
508 struct dummy_ep *ep;
509 struct dummy *dum;
510 int retval = -EINVAL;
511 unsigned long flags;
512 struct dummy_request *req = NULL;
513
514 if (!_ep || !_req)
515 return retval;
516 ep = usb_ep_to_dummy_ep (_ep);
517 dum = ep_to_dummy (ep);
518
519 if (!dum->driver)
520 return -ESHUTDOWN;
521
522 spin_lock_irqsave (&dum->lock, flags);
523 list_for_each_entry (req, &ep->queue, queue) {
524 if (&req->req == _req) {
525 list_del_init (&req->queue);
526 _req->status = -ECONNRESET;
527 retval = 0;
528 break;
529 }
530 }
531 spin_unlock_irqrestore (&dum->lock, flags);
532
533 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400534 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 "dequeued req %p from %s, len %d buf %p\n",
536 req, _ep->name, _req->length, _req->buf);
537 _req->complete (_ep, _req);
538 }
539 return retval;
540}
541
542static int
543dummy_set_halt (struct usb_ep *_ep, int value)
544{
545 struct dummy_ep *ep;
546 struct dummy *dum;
547
548 if (!_ep)
549 return -EINVAL;
550 ep = usb_ep_to_dummy_ep (_ep);
551 dum = ep_to_dummy (ep);
552 if (!dum->driver)
553 return -ESHUTDOWN;
554 if (!value)
555 ep->halted = 0;
556 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
557 !list_empty (&ep->queue))
558 return -EAGAIN;
559 else
560 ep->halted = 1;
561 /* FIXME clear emulated data toggle too */
562 return 0;
563}
564
565static const struct usb_ep_ops dummy_ep_ops = {
566 .enable = dummy_enable,
567 .disable = dummy_disable,
568
569 .alloc_request = dummy_alloc_request,
570 .free_request = dummy_free_request,
571
572 .alloc_buffer = dummy_alloc_buffer,
573 .free_buffer = dummy_free_buffer,
574 /* map, unmap, ... eventually hook the "generic" dma calls */
575
576 .queue = dummy_queue,
577 .dequeue = dummy_dequeue,
578
579 .set_halt = dummy_set_halt,
580};
581
582/*-------------------------------------------------------------------------*/
583
584/* there are both host and device side versions of this call ... */
585static int dummy_g_get_frame (struct usb_gadget *_gadget)
586{
587 struct timeval tv;
588
589 do_gettimeofday (&tv);
590 return tv.tv_usec / 1000;
591}
592
593static int dummy_wakeup (struct usb_gadget *_gadget)
594{
595 struct dummy *dum;
596
597 dum = gadget_to_dummy (_gadget);
Alan Sternc2db8b52005-04-29 16:30:48 -0400598 if (!(dum->port_status & USB_PORT_STAT_SUSPEND)
Alan Stern5742b0c2005-05-02 11:25:17 -0400599 || !(dum->devstatus &
600 ( (1 << USB_DEVICE_B_HNP_ENABLE)
601 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EINVAL;
603
604 /* hub notices our request, issues downstream resume, etc */
605 dum->resuming = 1;
Alan Sternc2db8b52005-04-29 16:30:48 -0400606 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
608}
609
610static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
611{
612 struct dummy *dum;
613
614 dum = gadget_to_dummy (_gadget);
615 if (value)
616 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
617 else
618 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
619 return 0;
620}
621
622static const struct usb_gadget_ops dummy_ops = {
623 .get_frame = dummy_g_get_frame,
624 .wakeup = dummy_wakeup,
625 .set_selfpowered = dummy_set_selfpowered,
626};
627
628/*-------------------------------------------------------------------------*/
629
630/* "function" sysfs attribute */
631static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400632show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct dummy *dum = gadget_dev_to_dummy (dev);
635
636 if (!dum->driver || !dum->driver->function)
637 return 0;
638 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
639}
640DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
641
642/*-------------------------------------------------------------------------*/
643
644/*
645 * Driver registration/unregistration.
646 *
647 * This is basically hardware-specific; there's usually only one real USB
648 * device (not host) controller since that's how USB devices are intended
649 * to work. So most implementations of these api calls will rely on the
650 * fact that only one driver will ever bind to the hardware. But curious
651 * hardware can be built with discrete components, so the gadget API doesn't
652 * require that assumption.
653 *
654 * For this emulator, it might be convenient to create a usb slave device
655 * for each driver that registers: just add to a big root hub.
656 */
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658int
659usb_gadget_register_driver (struct usb_gadget_driver *driver)
660{
661 struct dummy *dum = the_controller;
662 int retval, i;
663
664 if (!dum)
665 return -EINVAL;
666 if (dum->driver)
667 return -EBUSY;
668 if (!driver->bind || !driver->unbind || !driver->setup
669 || driver->speed == USB_SPEED_UNKNOWN)
670 return -EINVAL;
671
672 /*
673 * SLAVE side init ... the layer above hardware, which
674 * can't enumerate without help from the driver we're binding.
675 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 dum->devstatus = 0;
678 dum->resuming = 0;
679
680 INIT_LIST_HEAD (&dum->gadget.ep_list);
681 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
682 struct dummy_ep *ep = &dum->ep [i];
683
684 if (!ep_name [i])
685 break;
686 ep->ep.name = ep_name [i];
687 ep->ep.ops = &dummy_ep_ops;
688 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
689 ep->halted = ep->already_seen = ep->setup_stage = 0;
690 ep->ep.maxpacket = ~0;
691 ep->last_io = jiffies;
692 ep->gadget = &dum->gadget;
693 ep->desc = NULL;
694 INIT_LIST_HEAD (&ep->queue);
695 }
696
697 dum->gadget.ep0 = &dum->ep [0].ep;
698 dum->ep [0].ep.maxpacket = 64;
699 list_del_init (&dum->ep [0].ep.ep_list);
700 INIT_LIST_HEAD(&dum->fifo_req.queue);
701
702 dum->driver = driver;
703 dum->gadget.dev.driver = &driver->driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400704 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 driver->driver.name);
706 if ((retval = driver->bind (&dum->gadget)) != 0) {
707 dum->driver = NULL;
708 dum->gadget.dev.driver = NULL;
709 return retval;
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 driver->driver.bus = dum->gadget.dev.parent->bus;
713 driver_register (&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 device_bind_driver (&dum->gadget.dev);
715
716 /* khubd will enumerate this in a while */
717 dum->port_status |= USB_PORT_STAT_CONNECTION
Alan Sternc2db8b52005-04-29 16:30:48 -0400718 | (USB_PORT_STAT_C_CONNECTION << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720}
721EXPORT_SYMBOL (usb_gadget_register_driver);
722
723/* caller must hold lock */
724static void
725stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
726{
727 struct dummy_ep *ep;
728
729 /* prevent any more requests */
730 dum->address = 0;
731
732 /* The timer is left running so that outstanding URBs can fail */
733
734 /* nuke any pending requests first, so driver i/o is quiesced */
735 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
736 nuke (dum, ep);
737
738 /* driver now does any non-usb quiescing necessary */
739 if (driver) {
740 spin_unlock (&dum->lock);
741 driver->disconnect (&dum->gadget);
742 spin_lock (&dum->lock);
743 }
744}
745
746int
747usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
748{
749 struct dummy *dum = the_controller;
750 unsigned long flags;
751
752 if (!dum)
753 return -ENODEV;
754 if (!driver || driver != dum->driver)
755 return -EINVAL;
756
Alan Sternd9b76252005-05-03 16:15:43 -0400757 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 driver->driver.name);
759
760 spin_lock_irqsave (&dum->lock, flags);
761 stop_activity (dum, driver);
762 dum->port_status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE |
763 USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
Alan Sternc2db8b52005-04-29 16:30:48 -0400764 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 spin_unlock_irqrestore (&dum->lock, flags);
766
767 driver->unbind (&dum->gadget);
768 dum->driver = NULL;
769
770 device_release_driver (&dum->gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 driver_unregister (&driver->driver);
772
773 return 0;
774}
775EXPORT_SYMBOL (usb_gadget_unregister_driver);
776
777#undef is_enabled
778
779int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
780{
781 return -ENOSYS;
782}
783EXPORT_SYMBOL (net2280_set_fifo_mode);
784
Alan Sternd9b76252005-05-03 16:15:43 -0400785
786/* The gadget structure is stored inside the hcd structure and will be
787 * released along with it. */
788static void
789dummy_gadget_release (struct device *dev)
790{
791#if 0 /* usb_bus_put isn't EXPORTed! */
792 struct dummy *dum = gadget_dev_to_dummy (dev);
793
794 usb_bus_put (&dummy_to_hcd (dum)->self);
795#endif
796}
797
798static int dummy_udc_probe (struct device *dev)
799{
800 struct dummy *dum = the_controller;
801 int rc;
802
803 dum->gadget.name = gadget_name;
804 dum->gadget.ops = &dummy_ops;
805 dum->gadget.is_dualspeed = 1;
806
807 /* maybe claim OTG support, though we won't complete HNP */
808 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
809
810 strcpy (dum->gadget.dev.bus_id, "gadget");
811 dum->gadget.dev.parent = dev;
812 dum->gadget.dev.release = dummy_gadget_release;
813 rc = device_register (&dum->gadget.dev);
814 if (rc < 0)
815 return rc;
816
817#if 0 /* usb_bus_get isn't EXPORTed! */
818 usb_bus_get (&dummy_to_hcd (dum)->self);
819#endif
820
821 dev_set_drvdata (dev, dum);
822 device_create_file (&dum->gadget.dev, &dev_attr_function);
823 return rc;
824}
825
826static int dummy_udc_remove (struct device *dev)
827{
828 struct dummy *dum = dev_get_drvdata (dev);
829
830 dev_set_drvdata (dev, NULL);
831 device_remove_file (&dum->gadget.dev, &dev_attr_function);
832 device_unregister (&dum->gadget.dev);
833 return 0;
834}
835
836static struct device_driver dummy_udc_driver = {
837 .name = (char *) gadget_name,
838 .bus = &platform_bus_type,
839 .probe = dummy_udc_probe,
840 .remove = dummy_udc_remove,
841};
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/*-------------------------------------------------------------------------*/
844
845/* MASTER/HOST SIDE DRIVER
846 *
847 * this uses the hcd framework to hook up to host side drivers.
848 * its root hub will only have one device, otherwise it acts like
849 * a normal host controller.
850 *
851 * when urbs are queued, they're just stuck on a list that we
852 * scan in a timer callback. that callback connects writes from
853 * the host with reads from the device, and so on, based on the
854 * usb 2.0 rules.
855 */
856
857static int dummy_urb_enqueue (
858 struct usb_hcd *hcd,
859 struct usb_host_endpoint *ep,
860 struct urb *urb,
861 int mem_flags
862) {
863 struct dummy *dum;
864 struct urbp *urbp;
865 unsigned long flags;
866
867 if (!urb->transfer_buffer && urb->transfer_buffer_length)
868 return -EINVAL;
869
870 urbp = kmalloc (sizeof *urbp, mem_flags);
871 if (!urbp)
872 return -ENOMEM;
873 urbp->urb = urb;
874
875 dum = hcd_to_dummy (hcd);
876 spin_lock_irqsave (&dum->lock, flags);
877
878 if (!dum->udev) {
879 dum->udev = urb->dev;
880 usb_get_dev (dum->udev);
881 } else if (unlikely (dum->udev != urb->dev))
882 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
883
884 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
885 urb->hcpriv = urbp;
886 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
887 urb->error_count = 1; /* mark as a new urb */
888
889 /* kick the scheduler, it'll do the rest */
890 if (!timer_pending (&dum->timer))
891 mod_timer (&dum->timer, jiffies + 1);
892
893 spin_unlock_irqrestore (&dum->lock, flags);
894 return 0;
895}
896
897static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
898{
899 /* giveback happens automatically in timer callback */
900 return 0;
901}
902
903static void maybe_set_status (struct urb *urb, int status)
904{
905 spin_lock (&urb->lock);
906 if (urb->status == -EINPROGRESS)
907 urb->status = status;
908 spin_unlock (&urb->lock);
909}
910
911/* transfer up to a frame's worth; caller must own lock */
912static int
913transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
914{
915 struct dummy_request *req;
916
917top:
918 /* if there's no request queued, the device is NAKing; return */
919 list_for_each_entry (req, &ep->queue, queue) {
920 unsigned host_len, dev_len, len;
921 int is_short, to_host;
922 int rescan = 0;
923
924 /* 1..N packets of ep->ep.maxpacket each ... the last one
925 * may be short (including zero length).
926 *
927 * writer can send a zlp explicitly (length 0) or implicitly
928 * (length mod maxpacket zero, and 'zero' flag); they always
929 * terminate reads.
930 */
931 host_len = urb->transfer_buffer_length - urb->actual_length;
932 dev_len = req->req.length - req->req.actual;
933 len = min (host_len, dev_len);
934
935 /* FIXME update emulated data toggle too */
936
937 to_host = usb_pipein (urb->pipe);
938 if (unlikely (len == 0))
939 is_short = 1;
940 else {
941 char *ubuf, *rbuf;
942
943 /* not enough bandwidth left? */
944 if (limit < ep->ep.maxpacket && limit < len)
945 break;
946 len = min (len, (unsigned) limit);
947 if (len == 0)
948 break;
949
950 /* use an extra pass for the final short packet */
951 if (len > ep->ep.maxpacket) {
952 rescan = 1;
953 len -= (len % ep->ep.maxpacket);
954 }
955 is_short = (len % ep->ep.maxpacket) != 0;
956
957 /* else transfer packet(s) */
958 ubuf = urb->transfer_buffer + urb->actual_length;
959 rbuf = req->req.buf + req->req.actual;
960 if (to_host)
961 memcpy (ubuf, rbuf, len);
962 else
963 memcpy (rbuf, ubuf, len);
964 ep->last_io = jiffies;
965
966 limit -= len;
967 urb->actual_length += len;
968 req->req.actual += len;
969 }
970
971 /* short packets terminate, maybe with overflow/underflow.
972 * it's only really an error to write too much.
973 *
974 * partially filling a buffer optionally blocks queue advances
975 * (so completion handlers can clean up the queue) but we don't
976 * need to emulate such data-in-flight. so we only show part
977 * of the URB_SHORT_NOT_OK effect: completion status.
978 */
979 if (is_short) {
980 if (host_len == dev_len) {
981 req->req.status = 0;
982 maybe_set_status (urb, 0);
983 } else if (to_host) {
984 req->req.status = 0;
985 if (dev_len > host_len)
986 maybe_set_status (urb, -EOVERFLOW);
987 else
988 maybe_set_status (urb,
989 (urb->transfer_flags
990 & URB_SHORT_NOT_OK)
991 ? -EREMOTEIO : 0);
992 } else if (!to_host) {
993 maybe_set_status (urb, 0);
994 if (host_len > dev_len)
995 req->req.status = -EOVERFLOW;
996 else
997 req->req.status = 0;
998 }
999
1000 /* many requests terminate without a short packet */
1001 } else {
1002 if (req->req.length == req->req.actual
1003 && !req->req.zero)
1004 req->req.status = 0;
1005 if (urb->transfer_buffer_length == urb->actual_length
1006 && !(urb->transfer_flags
1007 & URB_ZERO_PACKET)) {
1008 maybe_set_status (urb, 0);
1009 }
1010 }
1011
1012 /* device side completion --> continuable */
1013 if (req->req.status != -EINPROGRESS) {
1014 list_del_init (&req->queue);
1015
1016 spin_unlock (&dum->lock);
1017 req->req.complete (&ep->ep, &req->req);
1018 spin_lock (&dum->lock);
1019
1020 /* requests might have been unlinked... */
1021 rescan = 1;
1022 }
1023
1024 /* host side completion --> terminate */
1025 if (urb->status != -EINPROGRESS)
1026 break;
1027
1028 /* rescan to continue with any other queued i/o */
1029 if (rescan)
1030 goto top;
1031 }
1032 return limit;
1033}
1034
1035static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1036{
1037 int limit = ep->ep.maxpacket;
1038
1039 if (dum->gadget.speed == USB_SPEED_HIGH) {
1040 int tmp;
1041
1042 /* high bandwidth mode */
1043 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1044 tmp = le16_to_cpu (tmp);
1045 tmp = (tmp >> 11) & 0x03;
1046 tmp *= 8 /* applies to entire frame */;
1047 limit += limit * tmp;
1048 }
1049 return limit;
1050}
1051
1052#define is_active(dum) ((dum->port_status & \
1053 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1054 USB_PORT_STAT_SUSPEND)) \
1055 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1056
1057static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1058{
1059 int i;
1060
1061 if (!is_active (dum))
1062 return NULL;
1063 if ((address & ~USB_DIR_IN) == 0)
1064 return &dum->ep [0];
1065 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1066 struct dummy_ep *ep = &dum->ep [i];
1067
1068 if (!ep->desc)
1069 continue;
1070 if (ep->desc->bEndpointAddress == address)
1071 return ep;
1072 }
1073 return NULL;
1074}
1075
1076#undef is_active
1077
1078#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1079#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1080#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1081#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1082#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1083#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1084
1085/* drive both sides of the transfers; looks like irq handlers to
1086 * both drivers except the callbacks aren't in_irq().
1087 */
1088static void dummy_timer (unsigned long _dum)
1089{
1090 struct dummy *dum = (struct dummy *) _dum;
1091 struct urbp *urbp, *tmp;
1092 unsigned long flags;
1093 int limit, total;
1094 int i;
1095
1096 /* simplistic model for one frame's bandwidth */
1097 switch (dum->gadget.speed) {
1098 case USB_SPEED_LOW:
1099 total = 8/*bytes*/ * 12/*packets*/;
1100 break;
1101 case USB_SPEED_FULL:
1102 total = 64/*bytes*/ * 19/*packets*/;
1103 break;
1104 case USB_SPEED_HIGH:
1105 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1106 break;
1107 default:
1108 dev_err (dummy_dev(dum), "bogus device speed\n");
1109 return;
1110 }
1111
1112 /* FIXME if HZ != 1000 this will probably misbehave ... */
1113
1114 /* look at each urb queued by the host side driver */
1115 spin_lock_irqsave (&dum->lock, flags);
1116
1117 if (!dum->udev) {
1118 dev_err (dummy_dev(dum),
1119 "timer fired with no URBs pending?\n");
1120 spin_unlock_irqrestore (&dum->lock, flags);
1121 return;
1122 }
1123
1124 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1125 if (!ep_name [i])
1126 break;
1127 dum->ep [i].already_seen = 0;
1128 }
1129
1130restart:
1131 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1132 struct urb *urb;
1133 struct dummy_request *req;
1134 u8 address;
1135 struct dummy_ep *ep = NULL;
1136 int type;
1137
1138 urb = urbp->urb;
1139 if (urb->status != -EINPROGRESS) {
1140 /* likely it was just unlinked */
1141 goto return_urb;
1142 }
1143 type = usb_pipetype (urb->pipe);
1144
1145 /* used up this frame's non-periodic bandwidth?
1146 * FIXME there's infinite bandwidth for control and
1147 * periodic transfers ... unrealistic.
1148 */
1149 if (total <= 0 && type == PIPE_BULK)
1150 continue;
1151
1152 /* find the gadget's ep for this request (if configured) */
1153 address = usb_pipeendpoint (urb->pipe);
1154 if (usb_pipein (urb->pipe))
1155 address |= USB_DIR_IN;
1156 ep = find_endpoint(dum, address);
1157 if (!ep) {
1158 /* set_configuration() disagreement */
1159 dev_dbg (dummy_dev(dum),
1160 "no ep configured for urb %p\n",
1161 urb);
1162 maybe_set_status (urb, -EPROTO);
1163 goto return_urb;
1164 }
1165
1166 if (ep->already_seen)
1167 continue;
1168 ep->already_seen = 1;
1169 if (ep == &dum->ep [0] && urb->error_count) {
1170 ep->setup_stage = 1; /* a new urb */
1171 urb->error_count = 0;
1172 }
1173 if (ep->halted && !ep->setup_stage) {
1174 /* NOTE: must not be iso! */
1175 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1176 ep->ep.name, urb);
1177 maybe_set_status (urb, -EPIPE);
1178 goto return_urb;
1179 }
1180 /* FIXME make sure both ends agree on maxpacket */
1181
1182 /* handle control requests */
1183 if (ep == &dum->ep [0] && ep->setup_stage) {
1184 struct usb_ctrlrequest setup;
1185 int value = 1;
1186 struct dummy_ep *ep2;
1187
1188 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1189 le16_to_cpus (&setup.wIndex);
1190 le16_to_cpus (&setup.wValue);
1191 le16_to_cpus (&setup.wLength);
1192 if (setup.wLength != urb->transfer_buffer_length) {
1193 maybe_set_status (urb, -EOVERFLOW);
1194 goto return_urb;
1195 }
1196
1197 /* paranoia, in case of stale queued data */
1198 list_for_each_entry (req, &ep->queue, queue) {
1199 list_del_init (&req->queue);
1200 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001201 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 req);
1203
1204 spin_unlock (&dum->lock);
1205 req->req.complete (&ep->ep, &req->req);
1206 spin_lock (&dum->lock);
1207 ep->already_seen = 0;
1208 goto restart;
1209 }
1210
1211 /* gadget driver never sees set_address or operations
1212 * on standard feature flags. some hardware doesn't
1213 * even expose them.
1214 */
1215 ep->last_io = jiffies;
1216 ep->setup_stage = 0;
1217 ep->halted = 0;
1218 switch (setup.bRequest) {
1219 case USB_REQ_SET_ADDRESS:
1220 if (setup.bRequestType != Dev_Request)
1221 break;
1222 dum->address = setup.wValue;
1223 maybe_set_status (urb, 0);
Alan Sternd9b76252005-05-03 16:15:43 -04001224 dev_dbg (udc_dev(dum), "set_address = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 setup.wValue);
1226 value = 0;
1227 break;
1228 case USB_REQ_SET_FEATURE:
1229 if (setup.bRequestType == Dev_Request) {
1230 value = 0;
1231 switch (setup.wValue) {
1232 case USB_DEVICE_REMOTE_WAKEUP:
1233 break;
Alan Stern5742b0c2005-05-02 11:25:17 -04001234 case USB_DEVICE_B_HNP_ENABLE:
1235 dum->gadget.b_hnp_enable = 1;
1236 break;
1237 case USB_DEVICE_A_HNP_SUPPORT:
1238 dum->gadget.a_hnp_support = 1;
1239 break;
1240 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1241 dum->gadget.a_alt_hnp_support
1242 = 1;
1243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 default:
1245 value = -EOPNOTSUPP;
1246 }
1247 if (value == 0) {
1248 dum->devstatus |=
1249 (1 << setup.wValue);
1250 maybe_set_status (urb, 0);
1251 }
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 = 1;
1262 value = 0;
1263 maybe_set_status (urb, 0);
1264 }
1265 break;
1266 case USB_REQ_CLEAR_FEATURE:
1267 if (setup.bRequestType == Dev_Request) {
1268 switch (setup.wValue) {
1269 case USB_DEVICE_REMOTE_WAKEUP:
1270 dum->devstatus &= ~(1 <<
1271 USB_DEVICE_REMOTE_WAKEUP);
1272 value = 0;
1273 maybe_set_status (urb, 0);
1274 break;
1275 default:
1276 value = -EOPNOTSUPP;
1277 break;
1278 }
1279 } else if (setup.bRequestType == Ep_Request) {
1280 // endpoint halt
1281 ep2 = find_endpoint (dum,
1282 setup.wIndex);
1283 if (!ep2) {
1284 value = -EOPNOTSUPP;
1285 break;
1286 }
1287 ep2->halted = 0;
1288 value = 0;
1289 maybe_set_status (urb, 0);
1290 }
1291 break;
1292 case USB_REQ_GET_STATUS:
1293 if (setup.bRequestType == Dev_InRequest
1294 || setup.bRequestType
1295 == Intf_InRequest
1296 || setup.bRequestType
1297 == Ep_InRequest
1298 ) {
1299 char *buf;
1300
1301 // device: remote wakeup, selfpowered
1302 // interface: nothing
1303 // endpoint: halt
1304 buf = (char *)urb->transfer_buffer;
1305 if (urb->transfer_buffer_length > 0) {
1306 if (setup.bRequestType ==
1307 Ep_InRequest) {
1308 ep2 = find_endpoint (dum, setup.wIndex);
1309 if (!ep2) {
1310 value = -EOPNOTSUPP;
1311 break;
1312 }
1313 buf [0] = ep2->halted;
1314 } else if (setup.bRequestType ==
1315 Dev_InRequest) {
1316 buf [0] = (u8)
1317 dum->devstatus;
1318 } else
1319 buf [0] = 0;
1320 }
1321 if (urb->transfer_buffer_length > 1)
1322 buf [1] = 0;
1323 urb->actual_length = min (2,
1324 urb->transfer_buffer_length);
1325 value = 0;
1326 maybe_set_status (urb, 0);
1327 }
1328 break;
1329 }
1330
1331 /* gadget driver handles all other requests. block
1332 * until setup() returns; no reentrancy issues etc.
1333 */
1334 if (value > 0) {
1335 spin_unlock (&dum->lock);
1336 value = dum->driver->setup (&dum->gadget,
1337 &setup);
1338 spin_lock (&dum->lock);
1339
1340 if (value >= 0) {
1341 /* no delays (max 64KB data stage) */
1342 limit = 64*1024;
1343 goto treat_control_like_bulk;
1344 }
1345 /* error, see below */
1346 }
1347
1348 if (value < 0) {
1349 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001350 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 "setup --> %d\n",
1352 value);
1353 maybe_set_status (urb, -EPIPE);
1354 urb->actual_length = 0;
1355 }
1356
1357 goto return_urb;
1358 }
1359
1360 /* non-control requests */
1361 limit = total;
1362 switch (usb_pipetype (urb->pipe)) {
1363 case PIPE_ISOCHRONOUS:
1364 /* FIXME is it urb->interval since the last xfer?
1365 * use urb->iso_frame_desc[i].
1366 * complete whether or not ep has requests queued.
1367 * report random errors, to debug drivers.
1368 */
1369 limit = max (limit, periodic_bytes (dum, ep));
1370 maybe_set_status (urb, -ENOSYS);
1371 break;
1372
1373 case PIPE_INTERRUPT:
1374 /* FIXME is it urb->interval since the last xfer?
1375 * this almost certainly polls too fast.
1376 */
1377 limit = max (limit, periodic_bytes (dum, ep));
1378 /* FALLTHROUGH */
1379
1380 // case PIPE_BULK: case PIPE_CONTROL:
1381 default:
1382 treat_control_like_bulk:
1383 ep->last_io = jiffies;
1384 total = transfer (dum, urb, ep, limit);
1385 break;
1386 }
1387
1388 /* incomplete transfer? */
1389 if (urb->status == -EINPROGRESS)
1390 continue;
1391
1392return_urb:
1393 urb->hcpriv = NULL;
1394 list_del (&urbp->urbp_list);
1395 kfree (urbp);
1396 if (ep)
1397 ep->already_seen = ep->setup_stage = 0;
1398
1399 spin_unlock (&dum->lock);
1400 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1401 spin_lock (&dum->lock);
1402
1403 goto restart;
1404 }
1405
1406 /* want a 1 msec delay here */
1407 if (!list_empty (&dum->urbp_list))
1408 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1409 else {
1410 usb_put_dev (dum->udev);
1411 dum->udev = NULL;
1412 }
1413
1414 spin_unlock_irqrestore (&dum->lock, flags);
1415}
1416
1417/*-------------------------------------------------------------------------*/
1418
1419#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001420 ((USB_PORT_STAT_C_CONNECTION \
1421 | USB_PORT_STAT_C_ENABLE \
1422 | USB_PORT_STAT_C_SUSPEND \
1423 | USB_PORT_STAT_C_OVERCURRENT \
1424 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1427{
1428 struct dummy *dum;
1429 unsigned long flags;
1430 int retval;
1431
1432 dum = hcd_to_dummy (hcd);
1433
1434 spin_lock_irqsave (&dum->lock, flags);
1435 if (!(dum->port_status & PORT_C_MASK))
1436 retval = 0;
1437 else {
1438 *buf = (1 << 1);
1439 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1440 dum->port_status);
1441 retval = 1;
1442 }
1443 spin_unlock_irqrestore (&dum->lock, flags);
1444 return retval;
1445}
1446
1447static inline void
1448hub_descriptor (struct usb_hub_descriptor *desc)
1449{
1450 memset (desc, 0, sizeof *desc);
1451 desc->bDescriptorType = 0x29;
1452 desc->bDescLength = 9;
1453 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1454 desc->bNbrPorts = 1;
1455 desc->bitmap [0] = 0xff;
1456 desc->bitmap [1] = 0xff;
1457}
1458
1459static int dummy_hub_control (
1460 struct usb_hcd *hcd,
1461 u16 typeReq,
1462 u16 wValue,
1463 u16 wIndex,
1464 char *buf,
1465 u16 wLength
1466) {
1467 struct dummy *dum;
1468 int retval = 0;
1469 unsigned long flags;
1470
1471 dum = hcd_to_dummy (hcd);
1472 spin_lock_irqsave (&dum->lock, flags);
1473 switch (typeReq) {
1474 case ClearHubFeature:
1475 break;
1476 case ClearPortFeature:
1477 switch (wValue) {
1478 case USB_PORT_FEAT_SUSPEND:
Alan Sternc2db8b52005-04-29 16:30:48 -04001479 if (dum->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* 20msec resume signaling */
1481 dum->resuming = 1;
1482 dum->re_timeout = jiffies +
1483 msecs_to_jiffies(20);
1484 }
1485 break;
1486 case USB_PORT_FEAT_POWER:
1487 dum->port_status = 0;
1488 dum->resuming = 0;
1489 stop_activity(dum, dum->driver);
1490 break;
1491 default:
1492 dum->port_status &= ~(1 << wValue);
1493 }
1494 break;
1495 case GetHubDescriptor:
1496 hub_descriptor ((struct usb_hub_descriptor *) buf);
1497 break;
1498 case GetHubStatus:
1499 *(u32 *) buf = __constant_cpu_to_le32 (0);
1500 break;
1501 case GetPortStatus:
1502 if (wIndex != 1)
1503 retval = -EPIPE;
1504
1505 /* whoever resets or resumes must GetPortStatus to
1506 * complete it!!
1507 */
1508 if (dum->resuming && time_after (jiffies, dum->re_timeout)) {
Alan Sternc2db8b52005-04-29 16:30:48 -04001509 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1510 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 dum->resuming = 0;
1512 dum->re_timeout = 0;
1513 if (dum->driver && dum->driver->resume) {
1514 spin_unlock (&dum->lock);
1515 dum->driver->resume (&dum->gadget);
1516 spin_lock (&dum->lock);
1517 }
1518 }
Alan Sternc2db8b52005-04-29 16:30:48 -04001519 if ((dum->port_status & USB_PORT_STAT_RESET) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 && time_after (jiffies, dum->re_timeout)) {
Alan Sternc2db8b52005-04-29 16:30:48 -04001521 dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
1522 dum->port_status &= ~USB_PORT_STAT_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 dum->re_timeout = 0;
1524 if (dum->driver) {
1525 dum->port_status |= USB_PORT_STAT_ENABLE;
1526 /* give it the best speed we agree on */
1527 dum->gadget.speed = dum->driver->speed;
1528 dum->gadget.ep0->maxpacket = 64;
1529 switch (dum->gadget.speed) {
1530 case USB_SPEED_HIGH:
1531 dum->port_status |=
1532 USB_PORT_STAT_HIGH_SPEED;
1533 break;
1534 case USB_SPEED_LOW:
1535 dum->gadget.ep0->maxpacket = 8;
1536 dum->port_status |=
1537 USB_PORT_STAT_LOW_SPEED;
1538 break;
1539 default:
1540 dum->gadget.speed = USB_SPEED_FULL;
1541 break;
1542 }
1543 }
1544 }
1545 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1546 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1547 break;
1548 case SetHubFeature:
1549 retval = -EPIPE;
1550 break;
1551 case SetPortFeature:
1552 switch (wValue) {
1553 case USB_PORT_FEAT_SUSPEND:
Alan Sternc2db8b52005-04-29 16:30:48 -04001554 if ((dum->port_status & USB_PORT_STAT_SUSPEND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 == 0) {
Alan Sternc2db8b52005-04-29 16:30:48 -04001556 dum->port_status |= USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (dum->driver && dum->driver->suspend) {
1558 spin_unlock (&dum->lock);
1559 dum->driver->suspend (&dum->gadget);
1560 spin_lock (&dum->lock);
Alan Stern5742b0c2005-05-02 11:25:17 -04001561 /* HNP would happen here; for now we
1562 * assume b_bus_req is always true.
1563 */
1564 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1565 & dum->devstatus) != 0)
1566 dev_dbg (dummy_dev(dum),
1567 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569 }
1570 break;
1571 case USB_PORT_FEAT_RESET:
1572 /* if it's already running, disconnect first */
1573 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1574 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1575 | USB_PORT_STAT_LOW_SPEED
1576 | USB_PORT_STAT_HIGH_SPEED);
1577 if (dum->driver) {
Alan Sternd9b76252005-05-03 16:15:43 -04001578 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 "disconnect\n");
1580 stop_activity (dum, dum->driver);
1581 }
1582
1583 /* FIXME test that code path! */
1584 }
1585 /* 50msec reset signaling */
1586 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1587 /* FALLTHROUGH */
1588 default:
1589 dum->port_status |= (1 << wValue);
1590 }
1591 break;
1592
1593 default:
1594 dev_dbg (dummy_dev(dum),
1595 "hub control req%04x v%04x i%04x l%d\n",
1596 typeReq, wValue, wIndex, wLength);
1597
1598 /* "protocol stall" on error */
1599 retval = -EPIPE;
1600 }
1601 spin_unlock_irqrestore (&dum->lock, flags);
1602 return retval;
1603}
1604
1605
1606/*-------------------------------------------------------------------------*/
1607
1608static inline ssize_t
1609show_urb (char *buf, size_t size, struct urb *urb)
1610{
1611 int ep = usb_pipeendpoint (urb->pipe);
1612
1613 return snprintf (buf, size,
1614 "urb/%p %s ep%d%s%s len %d/%d\n",
1615 urb,
1616 ({ char *s;
1617 switch (urb->dev->speed) {
1618 case USB_SPEED_LOW: s = "ls"; break;
1619 case USB_SPEED_FULL: s = "fs"; break;
1620 case USB_SPEED_HIGH: s = "hs"; break;
1621 default: s = "?"; break;
1622 }; s; }),
1623 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1624 ({ char *s; \
1625 switch (usb_pipetype (urb->pipe)) { \
1626 case PIPE_CONTROL: s = ""; break; \
1627 case PIPE_BULK: s = "-bulk"; break; \
1628 case PIPE_INTERRUPT: s = "-int"; break; \
1629 default: s = "-iso"; break; \
1630 }; s;}),
1631 urb->actual_length, urb->transfer_buffer_length);
1632}
1633
1634static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04001635show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 struct usb_hcd *hcd = dev_get_drvdata (dev);
1638 struct dummy *dum = hcd_to_dummy (hcd);
1639 struct urbp *urbp;
1640 size_t size = 0;
1641 unsigned long flags;
1642
1643 spin_lock_irqsave (&dum->lock, flags);
1644 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1645 size_t temp;
1646
1647 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1648 buf += temp;
1649 size += temp;
1650 }
1651 spin_unlock_irqrestore (&dum->lock, flags);
1652
1653 return size;
1654}
1655static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1656
1657static int dummy_start (struct usb_hcd *hcd)
1658{
1659 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 dum = hcd_to_dummy (hcd);
1662
1663 /*
1664 * MASTER side init ... we emulate a root hub that'll only ever
1665 * talk to one device (the slave side). Also appears in sysfs,
1666 * just like more familiar pci-based HCDs.
1667 */
1668 spin_lock_init (&dum->lock);
1669 init_timer (&dum->timer);
1670 dum->timer.function = dummy_timer;
1671 dum->timer.data = (unsigned long) dum;
1672
1673 INIT_LIST_HEAD (&dum->urbp_list);
1674
Alan Sternbc96c0a2005-04-25 11:21:31 -04001675 /* only show a low-power port: just 8mA */
1676 hcd->power_budget = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 hcd->state = HC_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Alan Stern5742b0c2005-05-02 11:25:17 -04001679#ifdef CONFIG_USB_OTG
1680 hcd->self.otg_port = 1;
1681#endif
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1684 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688static void dummy_stop (struct usb_hcd *hcd)
1689{
1690 struct dummy *dum;
1691
1692 dum = hcd_to_dummy (hcd);
1693
1694 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 usb_gadget_unregister_driver (dum->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 dev_info (dummy_dev(dum), "stopped\n");
1697}
1698
1699/*-------------------------------------------------------------------------*/
1700
1701static int dummy_h_get_frame (struct usb_hcd *hcd)
1702{
1703 return dummy_g_get_frame (NULL);
1704}
1705
1706static const struct hc_driver dummy_hcd = {
1707 .description = (char *) driver_name,
1708 .product_desc = "Dummy host controller",
1709 .hcd_priv_size = sizeof(struct dummy),
1710
1711 .flags = HCD_USB2,
1712
1713 .start = dummy_start,
1714 .stop = dummy_stop,
1715
1716 .urb_enqueue = dummy_urb_enqueue,
1717 .urb_dequeue = dummy_urb_dequeue,
1718
1719 .get_frame_number = dummy_h_get_frame,
1720
1721 .hub_status_data = dummy_hub_status,
1722 .hub_control = dummy_hub_control,
1723};
1724
Alan Sternd9b76252005-05-03 16:15:43 -04001725static int dummy_hcd_probe (struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct usb_hcd *hcd;
1728 int retval;
1729
1730 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1731
1732 hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id);
1733 if (!hcd)
1734 return -ENOMEM;
1735 the_controller = hcd_to_dummy (hcd);
1736
1737 retval = usb_add_hcd(hcd, 0, 0);
1738 if (retval != 0) {
1739 usb_put_hcd (hcd);
1740 the_controller = NULL;
1741 }
1742 return retval;
1743}
1744
Alan Sternd9b76252005-05-03 16:15:43 -04001745static int dummy_hcd_remove (struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 struct usb_hcd *hcd;
1748
1749 hcd = dev_get_drvdata (dev);
1750 usb_remove_hcd (hcd);
1751 usb_put_hcd (hcd);
1752 the_controller = NULL;
Alan Sternd9b76252005-05-03 16:15:43 -04001753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Alan Sternd9b76252005-05-03 16:15:43 -04001756static struct device_driver dummy_hcd_driver = {
1757 .name = (char *) driver_name,
1758 .bus = &platform_bus_type,
1759 .probe = dummy_hcd_probe,
1760 .remove = dummy_hcd_remove,
1761};
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763/*-------------------------------------------------------------------------*/
1764
Alan Sternd9b76252005-05-03 16:15:43 -04001765/* These don't need to do anything because the pdev structures are
1766 * statically allocated. */
1767static void
1768dummy_udc_release (struct device *dev) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Alan Sternd9b76252005-05-03 16:15:43 -04001770static void
1771dummy_hcd_release (struct device *dev) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Alan Sternd9b76252005-05-03 16:15:43 -04001773static struct platform_device the_udc_pdev = {
1774 .name = (char *) gadget_name,
1775 .id = -1,
1776 .dev = {
1777 .release = dummy_udc_release,
1778 },
1779};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Alan Sternd9b76252005-05-03 16:15:43 -04001781static struct platform_device the_hcd_pdev = {
1782 .name = (char *) driver_name,
1783 .id = -1,
1784 .dev = {
1785 .release = dummy_hcd_release,
1786 },
1787};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789static int __init init (void)
1790{
1791 int retval;
1792
1793 if (usb_disabled ())
1794 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04001795
1796 retval = driver_register (&dummy_hcd_driver);
1797 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return retval;
Alan Sternd9b76252005-05-03 16:15:43 -04001799
1800 retval = driver_register (&dummy_udc_driver);
1801 if (retval < 0)
1802 goto err_register_udc_driver;
1803
1804 retval = platform_device_register (&the_hcd_pdev);
1805 if (retval < 0)
1806 goto err_register_hcd;
1807
1808 retval = platform_device_register (&the_udc_pdev);
1809 if (retval < 0)
1810 goto err_register_udc;
1811 return retval;
1812
1813err_register_udc:
1814 platform_device_unregister (&the_hcd_pdev);
1815err_register_hcd:
1816 driver_unregister (&dummy_udc_driver);
1817err_register_udc_driver:
1818 driver_unregister (&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return retval;
1820}
1821module_init (init);
1822
1823static void __exit cleanup (void)
1824{
Alan Sternd9b76252005-05-03 16:15:43 -04001825 platform_device_unregister (&the_udc_pdev);
1826 platform_device_unregister (&the_hcd_pdev);
1827 driver_unregister (&dummy_udc_driver);
1828 driver_unregister (&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830module_exit (cleanup);