[PATCH] USB dummy_hcd: Partial OTG emulation
[deliverable/linux.git] / drivers / usb / gadget / dummy_hcd.c
CommitLineData
1da177e4
LT
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);
5742b0c9
AS
604 if (!(dum->port_status & (1 << USB_PORT_FEAT_SUSPEND))
605 || !(dum->devstatus &
606 ( (1 << USB_DEVICE_B_HNP_ENABLE)
607 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
1da177e4
LT
608 return -EINVAL;
609
610 /* hub notices our request, issues downstream resume, etc */
611 dum->resuming = 1;
612 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
613 return 0;
614}
615
616static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
617{
618 struct dummy *dum;
619
620 dum = gadget_to_dummy (_gadget);
621 if (value)
622 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
623 else
624 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
625 return 0;
626}
627
628static const struct usb_gadget_ops dummy_ops = {
629 .get_frame = dummy_g_get_frame,
630 .wakeup = dummy_wakeup,
631 .set_selfpowered = dummy_set_selfpowered,
632};
633
634/*-------------------------------------------------------------------------*/
635
636/* "function" sysfs attribute */
637static ssize_t
10523b3b 638show_function (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
639{
640 struct dummy *dum = gadget_dev_to_dummy (dev);
641
642 if (!dum->driver || !dum->driver->function)
643 return 0;
644 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
645}
646DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
647
648/*-------------------------------------------------------------------------*/
649
650/*
651 * Driver registration/unregistration.
652 *
653 * This is basically hardware-specific; there's usually only one real USB
654 * device (not host) controller since that's how USB devices are intended
655 * to work. So most implementations of these api calls will rely on the
656 * fact that only one driver will ever bind to the hardware. But curious
657 * hardware can be built with discrete components, so the gadget API doesn't
658 * require that assumption.
659 *
660 * For this emulator, it might be convenient to create a usb slave device
661 * for each driver that registers: just add to a big root hub.
662 */
663
664static void
665dummy_udc_release (struct device *dev)
666{
667}
668
669static void
670dummy_pdev_release (struct device *dev)
671{
672}
673
674static int
675dummy_register_udc (struct dummy *dum)
676{
677 int rc;
678
679 strcpy (dum->gadget.dev.bus_id, "udc");
680 dum->gadget.dev.parent = dummy_dev(dum);
681 dum->gadget.dev.release = dummy_udc_release;
682
683 rc = device_register (&dum->gadget.dev);
684 if (rc == 0)
685 device_create_file (&dum->gadget.dev, &dev_attr_function);
686 return rc;
687}
688
689static void
690dummy_unregister_udc (struct dummy *dum)
691{
692 device_remove_file (&dum->gadget.dev, &dev_attr_function);
693 device_unregister (&dum->gadget.dev);
694}
695
696int
697usb_gadget_register_driver (struct usb_gadget_driver *driver)
698{
699 struct dummy *dum = the_controller;
700 int retval, i;
701
702 if (!dum)
703 return -EINVAL;
704 if (dum->driver)
705 return -EBUSY;
706 if (!driver->bind || !driver->unbind || !driver->setup
707 || driver->speed == USB_SPEED_UNKNOWN)
708 return -EINVAL;
709
710 /*
711 * SLAVE side init ... the layer above hardware, which
712 * can't enumerate without help from the driver we're binding.
713 */
714 dum->gadget.name = gadget_name;
715 dum->gadget.ops = &dummy_ops;
716 dum->gadget.is_dualspeed = 1;
717
5742b0c9
AS
718 /* maybe claim OTG support, though we won't complete HNP */
719 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
720
1da177e4
LT
721 dum->devstatus = 0;
722 dum->resuming = 0;
723
724 INIT_LIST_HEAD (&dum->gadget.ep_list);
725 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
726 struct dummy_ep *ep = &dum->ep [i];
727
728 if (!ep_name [i])
729 break;
730 ep->ep.name = ep_name [i];
731 ep->ep.ops = &dummy_ep_ops;
732 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
733 ep->halted = ep->already_seen = ep->setup_stage = 0;
734 ep->ep.maxpacket = ~0;
735 ep->last_io = jiffies;
736 ep->gadget = &dum->gadget;
737 ep->desc = NULL;
738 INIT_LIST_HEAD (&ep->queue);
739 }
740
741 dum->gadget.ep0 = &dum->ep [0].ep;
742 dum->ep [0].ep.maxpacket = 64;
743 list_del_init (&dum->ep [0].ep.ep_list);
744 INIT_LIST_HEAD(&dum->fifo_req.queue);
745
746 dum->driver = driver;
747 dum->gadget.dev.driver = &driver->driver;
748 dev_dbg (dummy_dev(dum), "binding gadget driver '%s'\n",
749 driver->driver.name);
750 if ((retval = driver->bind (&dum->gadget)) != 0) {
751 dum->driver = NULL;
752 dum->gadget.dev.driver = NULL;
753 return retval;
754 }
755
756 // FIXME: Check these calls for errors and re-order
757 driver->driver.bus = dum->gadget.dev.parent->bus;
758 driver_register (&driver->driver);
759
760 device_bind_driver (&dum->gadget.dev);
761
762 /* khubd will enumerate this in a while */
763 dum->port_status |= USB_PORT_STAT_CONNECTION
764 | (1 << USB_PORT_FEAT_C_CONNECTION);
765 return 0;
766}
767EXPORT_SYMBOL (usb_gadget_register_driver);
768
769/* caller must hold lock */
770static void
771stop_activity (struct dummy *dum, struct usb_gadget_driver *driver)
772{
773 struct dummy_ep *ep;
774
775 /* prevent any more requests */
776 dum->address = 0;
777
778 /* The timer is left running so that outstanding URBs can fail */
779
780 /* nuke any pending requests first, so driver i/o is quiesced */
781 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
782 nuke (dum, ep);
783
784 /* driver now does any non-usb quiescing necessary */
785 if (driver) {
786 spin_unlock (&dum->lock);
787 driver->disconnect (&dum->gadget);
788 spin_lock (&dum->lock);
789 }
790}
791
792int
793usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
794{
795 struct dummy *dum = the_controller;
796 unsigned long flags;
797
798 if (!dum)
799 return -ENODEV;
800 if (!driver || driver != dum->driver)
801 return -EINVAL;
802
803 dev_dbg (dummy_dev(dum), "unregister gadget driver '%s'\n",
804 driver->driver.name);
805
806 spin_lock_irqsave (&dum->lock, flags);
807 stop_activity (dum, driver);
808 dum->port_status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE |
809 USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
810 dum->port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
811 spin_unlock_irqrestore (&dum->lock, flags);
812
813 driver->unbind (&dum->gadget);
814 dum->driver = NULL;
815
816 device_release_driver (&dum->gadget.dev);
817
818 driver_unregister (&driver->driver);
819
820 return 0;
821}
822EXPORT_SYMBOL (usb_gadget_unregister_driver);
823
824#undef is_enabled
825
826int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
827{
828 return -ENOSYS;
829}
830EXPORT_SYMBOL (net2280_set_fifo_mode);
831
832/*-------------------------------------------------------------------------*/
833
834/* MASTER/HOST SIDE DRIVER
835 *
836 * this uses the hcd framework to hook up to host side drivers.
837 * its root hub will only have one device, otherwise it acts like
838 * a normal host controller.
839 *
840 * when urbs are queued, they're just stuck on a list that we
841 * scan in a timer callback. that callback connects writes from
842 * the host with reads from the device, and so on, based on the
843 * usb 2.0 rules.
844 */
845
846static int dummy_urb_enqueue (
847 struct usb_hcd *hcd,
848 struct usb_host_endpoint *ep,
849 struct urb *urb,
850 int mem_flags
851) {
852 struct dummy *dum;
853 struct urbp *urbp;
854 unsigned long flags;
855
856 if (!urb->transfer_buffer && urb->transfer_buffer_length)
857 return -EINVAL;
858
859 urbp = kmalloc (sizeof *urbp, mem_flags);
860 if (!urbp)
861 return -ENOMEM;
862 urbp->urb = urb;
863
864 dum = hcd_to_dummy (hcd);
865 spin_lock_irqsave (&dum->lock, flags);
866
867 if (!dum->udev) {
868 dum->udev = urb->dev;
869 usb_get_dev (dum->udev);
870 } else if (unlikely (dum->udev != urb->dev))
871 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
872
873 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
874 urb->hcpriv = urbp;
875 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
876 urb->error_count = 1; /* mark as a new urb */
877
878 /* kick the scheduler, it'll do the rest */
879 if (!timer_pending (&dum->timer))
880 mod_timer (&dum->timer, jiffies + 1);
881
882 spin_unlock_irqrestore (&dum->lock, flags);
883 return 0;
884}
885
886static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
887{
888 /* giveback happens automatically in timer callback */
889 return 0;
890}
891
892static void maybe_set_status (struct urb *urb, int status)
893{
894 spin_lock (&urb->lock);
895 if (urb->status == -EINPROGRESS)
896 urb->status = status;
897 spin_unlock (&urb->lock);
898}
899
900/* transfer up to a frame's worth; caller must own lock */
901static int
902transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
903{
904 struct dummy_request *req;
905
906top:
907 /* if there's no request queued, the device is NAKing; return */
908 list_for_each_entry (req, &ep->queue, queue) {
909 unsigned host_len, dev_len, len;
910 int is_short, to_host;
911 int rescan = 0;
912
913 /* 1..N packets of ep->ep.maxpacket each ... the last one
914 * may be short (including zero length).
915 *
916 * writer can send a zlp explicitly (length 0) or implicitly
917 * (length mod maxpacket zero, and 'zero' flag); they always
918 * terminate reads.
919 */
920 host_len = urb->transfer_buffer_length - urb->actual_length;
921 dev_len = req->req.length - req->req.actual;
922 len = min (host_len, dev_len);
923
924 /* FIXME update emulated data toggle too */
925
926 to_host = usb_pipein (urb->pipe);
927 if (unlikely (len == 0))
928 is_short = 1;
929 else {
930 char *ubuf, *rbuf;
931
932 /* not enough bandwidth left? */
933 if (limit < ep->ep.maxpacket && limit < len)
934 break;
935 len = min (len, (unsigned) limit);
936 if (len == 0)
937 break;
938
939 /* use an extra pass for the final short packet */
940 if (len > ep->ep.maxpacket) {
941 rescan = 1;
942 len -= (len % ep->ep.maxpacket);
943 }
944 is_short = (len % ep->ep.maxpacket) != 0;
945
946 /* else transfer packet(s) */
947 ubuf = urb->transfer_buffer + urb->actual_length;
948 rbuf = req->req.buf + req->req.actual;
949 if (to_host)
950 memcpy (ubuf, rbuf, len);
951 else
952 memcpy (rbuf, ubuf, len);
953 ep->last_io = jiffies;
954
955 limit -= len;
956 urb->actual_length += len;
957 req->req.actual += len;
958 }
959
960 /* short packets terminate, maybe with overflow/underflow.
961 * it's only really an error to write too much.
962 *
963 * partially filling a buffer optionally blocks queue advances
964 * (so completion handlers can clean up the queue) but we don't
965 * need to emulate such data-in-flight. so we only show part
966 * of the URB_SHORT_NOT_OK effect: completion status.
967 */
968 if (is_short) {
969 if (host_len == dev_len) {
970 req->req.status = 0;
971 maybe_set_status (urb, 0);
972 } else if (to_host) {
973 req->req.status = 0;
974 if (dev_len > host_len)
975 maybe_set_status (urb, -EOVERFLOW);
976 else
977 maybe_set_status (urb,
978 (urb->transfer_flags
979 & URB_SHORT_NOT_OK)
980 ? -EREMOTEIO : 0);
981 } else if (!to_host) {
982 maybe_set_status (urb, 0);
983 if (host_len > dev_len)
984 req->req.status = -EOVERFLOW;
985 else
986 req->req.status = 0;
987 }
988
989 /* many requests terminate without a short packet */
990 } else {
991 if (req->req.length == req->req.actual
992 && !req->req.zero)
993 req->req.status = 0;
994 if (urb->transfer_buffer_length == urb->actual_length
995 && !(urb->transfer_flags
996 & URB_ZERO_PACKET)) {
997 maybe_set_status (urb, 0);
998 }
999 }
1000
1001 /* device side completion --> continuable */
1002 if (req->req.status != -EINPROGRESS) {
1003 list_del_init (&req->queue);
1004
1005 spin_unlock (&dum->lock);
1006 req->req.complete (&ep->ep, &req->req);
1007 spin_lock (&dum->lock);
1008
1009 /* requests might have been unlinked... */
1010 rescan = 1;
1011 }
1012
1013 /* host side completion --> terminate */
1014 if (urb->status != -EINPROGRESS)
1015 break;
1016
1017 /* rescan to continue with any other queued i/o */
1018 if (rescan)
1019 goto top;
1020 }
1021 return limit;
1022}
1023
1024static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1025{
1026 int limit = ep->ep.maxpacket;
1027
1028 if (dum->gadget.speed == USB_SPEED_HIGH) {
1029 int tmp;
1030
1031 /* high bandwidth mode */
1032 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1033 tmp = le16_to_cpu (tmp);
1034 tmp = (tmp >> 11) & 0x03;
1035 tmp *= 8 /* applies to entire frame */;
1036 limit += limit * tmp;
1037 }
1038 return limit;
1039}
1040
1041#define is_active(dum) ((dum->port_status & \
1042 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1043 USB_PORT_STAT_SUSPEND)) \
1044 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1045
1046static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1047{
1048 int i;
1049
1050 if (!is_active (dum))
1051 return NULL;
1052 if ((address & ~USB_DIR_IN) == 0)
1053 return &dum->ep [0];
1054 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1055 struct dummy_ep *ep = &dum->ep [i];
1056
1057 if (!ep->desc)
1058 continue;
1059 if (ep->desc->bEndpointAddress == address)
1060 return ep;
1061 }
1062 return NULL;
1063}
1064
1065#undef is_active
1066
1067#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1068#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1069#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1070#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1071#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1072#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1073
1074/* drive both sides of the transfers; looks like irq handlers to
1075 * both drivers except the callbacks aren't in_irq().
1076 */
1077static void dummy_timer (unsigned long _dum)
1078{
1079 struct dummy *dum = (struct dummy *) _dum;
1080 struct urbp *urbp, *tmp;
1081 unsigned long flags;
1082 int limit, total;
1083 int i;
1084
1085 /* simplistic model for one frame's bandwidth */
1086 switch (dum->gadget.speed) {
1087 case USB_SPEED_LOW:
1088 total = 8/*bytes*/ * 12/*packets*/;
1089 break;
1090 case USB_SPEED_FULL:
1091 total = 64/*bytes*/ * 19/*packets*/;
1092 break;
1093 case USB_SPEED_HIGH:
1094 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1095 break;
1096 default:
1097 dev_err (dummy_dev(dum), "bogus device speed\n");
1098 return;
1099 }
1100
1101 /* FIXME if HZ != 1000 this will probably misbehave ... */
1102
1103 /* look at each urb queued by the host side driver */
1104 spin_lock_irqsave (&dum->lock, flags);
1105
1106 if (!dum->udev) {
1107 dev_err (dummy_dev(dum),
1108 "timer fired with no URBs pending?\n");
1109 spin_unlock_irqrestore (&dum->lock, flags);
1110 return;
1111 }
1112
1113 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1114 if (!ep_name [i])
1115 break;
1116 dum->ep [i].already_seen = 0;
1117 }
1118
1119restart:
1120 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1121 struct urb *urb;
1122 struct dummy_request *req;
1123 u8 address;
1124 struct dummy_ep *ep = NULL;
1125 int type;
1126
1127 urb = urbp->urb;
1128 if (urb->status != -EINPROGRESS) {
1129 /* likely it was just unlinked */
1130 goto return_urb;
1131 }
1132 type = usb_pipetype (urb->pipe);
1133
1134 /* used up this frame's non-periodic bandwidth?
1135 * FIXME there's infinite bandwidth for control and
1136 * periodic transfers ... unrealistic.
1137 */
1138 if (total <= 0 && type == PIPE_BULK)
1139 continue;
1140
1141 /* find the gadget's ep for this request (if configured) */
1142 address = usb_pipeendpoint (urb->pipe);
1143 if (usb_pipein (urb->pipe))
1144 address |= USB_DIR_IN;
1145 ep = find_endpoint(dum, address);
1146 if (!ep) {
1147 /* set_configuration() disagreement */
1148 dev_dbg (dummy_dev(dum),
1149 "no ep configured for urb %p\n",
1150 urb);
1151 maybe_set_status (urb, -EPROTO);
1152 goto return_urb;
1153 }
1154
1155 if (ep->already_seen)
1156 continue;
1157 ep->already_seen = 1;
1158 if (ep == &dum->ep [0] && urb->error_count) {
1159 ep->setup_stage = 1; /* a new urb */
1160 urb->error_count = 0;
1161 }
1162 if (ep->halted && !ep->setup_stage) {
1163 /* NOTE: must not be iso! */
1164 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1165 ep->ep.name, urb);
1166 maybe_set_status (urb, -EPIPE);
1167 goto return_urb;
1168 }
1169 /* FIXME make sure both ends agree on maxpacket */
1170
1171 /* handle control requests */
1172 if (ep == &dum->ep [0] && ep->setup_stage) {
1173 struct usb_ctrlrequest setup;
1174 int value = 1;
1175 struct dummy_ep *ep2;
1176
1177 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1178 le16_to_cpus (&setup.wIndex);
1179 le16_to_cpus (&setup.wValue);
1180 le16_to_cpus (&setup.wLength);
1181 if (setup.wLength != urb->transfer_buffer_length) {
1182 maybe_set_status (urb, -EOVERFLOW);
1183 goto return_urb;
1184 }
1185
1186 /* paranoia, in case of stale queued data */
1187 list_for_each_entry (req, &ep->queue, queue) {
1188 list_del_init (&req->queue);
1189 req->req.status = -EOVERFLOW;
1190 dev_dbg (dummy_dev(dum), "stale req = %p\n",
1191 req);
1192
1193 spin_unlock (&dum->lock);
1194 req->req.complete (&ep->ep, &req->req);
1195 spin_lock (&dum->lock);
1196 ep->already_seen = 0;
1197 goto restart;
1198 }
1199
1200 /* gadget driver never sees set_address or operations
1201 * on standard feature flags. some hardware doesn't
1202 * even expose them.
1203 */
1204 ep->last_io = jiffies;
1205 ep->setup_stage = 0;
1206 ep->halted = 0;
1207 switch (setup.bRequest) {
1208 case USB_REQ_SET_ADDRESS:
1209 if (setup.bRequestType != Dev_Request)
1210 break;
1211 dum->address = setup.wValue;
1212 maybe_set_status (urb, 0);
1213 dev_dbg (dummy_dev(dum), "set_address = %d\n",
1214 setup.wValue);
1215 value = 0;
1216 break;
1217 case USB_REQ_SET_FEATURE:
1218 if (setup.bRequestType == Dev_Request) {
1219 value = 0;
1220 switch (setup.wValue) {
1221 case USB_DEVICE_REMOTE_WAKEUP:
1222 break;
5742b0c9
AS
1223 case USB_DEVICE_B_HNP_ENABLE:
1224 dum->gadget.b_hnp_enable = 1;
1225 break;
1226 case USB_DEVICE_A_HNP_SUPPORT:
1227 dum->gadget.a_hnp_support = 1;
1228 break;
1229 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1230 dum->gadget.a_alt_hnp_support
1231 = 1;
1232 break;
1da177e4
LT
1233 default:
1234 value = -EOPNOTSUPP;
1235 }
1236 if (value == 0) {
1237 dum->devstatus |=
1238 (1 << setup.wValue);
1239 maybe_set_status (urb, 0);
1240 }
1241
1242 } else if (setup.bRequestType == Ep_Request) {
1243 // endpoint halt
1244 ep2 = find_endpoint (dum,
1245 setup.wIndex);
1246 if (!ep2) {
1247 value = -EOPNOTSUPP;
1248 break;
1249 }
1250 ep2->halted = 1;
1251 value = 0;
1252 maybe_set_status (urb, 0);
1253 }
1254 break;
1255 case USB_REQ_CLEAR_FEATURE:
1256 if (setup.bRequestType == Dev_Request) {
1257 switch (setup.wValue) {
1258 case USB_DEVICE_REMOTE_WAKEUP:
1259 dum->devstatus &= ~(1 <<
1260 USB_DEVICE_REMOTE_WAKEUP);
1261 value = 0;
1262 maybe_set_status (urb, 0);
1263 break;
1264 default:
1265 value = -EOPNOTSUPP;
1266 break;
1267 }
1268 } else if (setup.bRequestType == Ep_Request) {
1269 // endpoint halt
1270 ep2 = find_endpoint (dum,
1271 setup.wIndex);
1272 if (!ep2) {
1273 value = -EOPNOTSUPP;
1274 break;
1275 }
1276 ep2->halted = 0;
1277 value = 0;
1278 maybe_set_status (urb, 0);
1279 }
1280 break;
1281 case USB_REQ_GET_STATUS:
1282 if (setup.bRequestType == Dev_InRequest
1283 || setup.bRequestType
1284 == Intf_InRequest
1285 || setup.bRequestType
1286 == Ep_InRequest
1287 ) {
1288 char *buf;
1289
1290 // device: remote wakeup, selfpowered
1291 // interface: nothing
1292 // endpoint: halt
1293 buf = (char *)urb->transfer_buffer;
1294 if (urb->transfer_buffer_length > 0) {
1295 if (setup.bRequestType ==
1296 Ep_InRequest) {
1297 ep2 = find_endpoint (dum, setup.wIndex);
1298 if (!ep2) {
1299 value = -EOPNOTSUPP;
1300 break;
1301 }
1302 buf [0] = ep2->halted;
1303 } else if (setup.bRequestType ==
1304 Dev_InRequest) {
1305 buf [0] = (u8)
1306 dum->devstatus;
1307 } else
1308 buf [0] = 0;
1309 }
1310 if (urb->transfer_buffer_length > 1)
1311 buf [1] = 0;
1312 urb->actual_length = min (2,
1313 urb->transfer_buffer_length);
1314 value = 0;
1315 maybe_set_status (urb, 0);
1316 }
1317 break;
1318 }
1319
1320 /* gadget driver handles all other requests. block
1321 * until setup() returns; no reentrancy issues etc.
1322 */
1323 if (value > 0) {
1324 spin_unlock (&dum->lock);
1325 value = dum->driver->setup (&dum->gadget,
1326 &setup);
1327 spin_lock (&dum->lock);
1328
1329 if (value >= 0) {
1330 /* no delays (max 64KB data stage) */
1331 limit = 64*1024;
1332 goto treat_control_like_bulk;
1333 }
1334 /* error, see below */
1335 }
1336
1337 if (value < 0) {
1338 if (value != -EOPNOTSUPP)
1339 dev_dbg (dummy_dev(dum),
1340 "setup --> %d\n",
1341 value);
1342 maybe_set_status (urb, -EPIPE);
1343 urb->actual_length = 0;
1344 }
1345
1346 goto return_urb;
1347 }
1348
1349 /* non-control requests */
1350 limit = total;
1351 switch (usb_pipetype (urb->pipe)) {
1352 case PIPE_ISOCHRONOUS:
1353 /* FIXME is it urb->interval since the last xfer?
1354 * use urb->iso_frame_desc[i].
1355 * complete whether or not ep has requests queued.
1356 * report random errors, to debug drivers.
1357 */
1358 limit = max (limit, periodic_bytes (dum, ep));
1359 maybe_set_status (urb, -ENOSYS);
1360 break;
1361
1362 case PIPE_INTERRUPT:
1363 /* FIXME is it urb->interval since the last xfer?
1364 * this almost certainly polls too fast.
1365 */
1366 limit = max (limit, periodic_bytes (dum, ep));
1367 /* FALLTHROUGH */
1368
1369 // case PIPE_BULK: case PIPE_CONTROL:
1370 default:
1371 treat_control_like_bulk:
1372 ep->last_io = jiffies;
1373 total = transfer (dum, urb, ep, limit);
1374 break;
1375 }
1376
1377 /* incomplete transfer? */
1378 if (urb->status == -EINPROGRESS)
1379 continue;
1380
1381return_urb:
1382 urb->hcpriv = NULL;
1383 list_del (&urbp->urbp_list);
1384 kfree (urbp);
1385 if (ep)
1386 ep->already_seen = ep->setup_stage = 0;
1387
1388 spin_unlock (&dum->lock);
1389 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1390 spin_lock (&dum->lock);
1391
1392 goto restart;
1393 }
1394
1395 /* want a 1 msec delay here */
1396 if (!list_empty (&dum->urbp_list))
1397 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1398 else {
1399 usb_put_dev (dum->udev);
1400 dum->udev = NULL;
1401 }
1402
1403 spin_unlock_irqrestore (&dum->lock, flags);
1404}
1405
1406/*-------------------------------------------------------------------------*/
1407
1408#define PORT_C_MASK \
1409 ((1 << USB_PORT_FEAT_C_CONNECTION) \
1410 | (1 << USB_PORT_FEAT_C_ENABLE) \
1411 | (1 << USB_PORT_FEAT_C_SUSPEND) \
1412 | (1 << USB_PORT_FEAT_C_OVER_CURRENT) \
1413 | (1 << USB_PORT_FEAT_C_RESET))
1414
1415static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1416{
1417 struct dummy *dum;
1418 unsigned long flags;
1419 int retval;
1420
1421 dum = hcd_to_dummy (hcd);
1422
1423 spin_lock_irqsave (&dum->lock, flags);
1424 if (!(dum->port_status & PORT_C_MASK))
1425 retval = 0;
1426 else {
1427 *buf = (1 << 1);
1428 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1429 dum->port_status);
1430 retval = 1;
1431 }
1432 spin_unlock_irqrestore (&dum->lock, flags);
1433 return retval;
1434}
1435
1436static inline void
1437hub_descriptor (struct usb_hub_descriptor *desc)
1438{
1439 memset (desc, 0, sizeof *desc);
1440 desc->bDescriptorType = 0x29;
1441 desc->bDescLength = 9;
1442 desc->wHubCharacteristics = __constant_cpu_to_le16 (0x0001);
1443 desc->bNbrPorts = 1;
1444 desc->bitmap [0] = 0xff;
1445 desc->bitmap [1] = 0xff;
1446}
1447
1448static int dummy_hub_control (
1449 struct usb_hcd *hcd,
1450 u16 typeReq,
1451 u16 wValue,
1452 u16 wIndex,
1453 char *buf,
1454 u16 wLength
1455) {
1456 struct dummy *dum;
1457 int retval = 0;
1458 unsigned long flags;
1459
1460 dum = hcd_to_dummy (hcd);
1461 spin_lock_irqsave (&dum->lock, flags);
1462 switch (typeReq) {
1463 case ClearHubFeature:
1464 break;
1465 case ClearPortFeature:
1466 switch (wValue) {
1467 case USB_PORT_FEAT_SUSPEND:
1468 if (dum->port_status & (1 << USB_PORT_FEAT_SUSPEND)) {
1469 /* 20msec resume signaling */
1470 dum->resuming = 1;
1471 dum->re_timeout = jiffies +
1472 msecs_to_jiffies(20);
1473 }
1474 break;
1475 case USB_PORT_FEAT_POWER:
1476 dum->port_status = 0;
1477 dum->resuming = 0;
1478 stop_activity(dum, dum->driver);
1479 break;
1480 default:
1481 dum->port_status &= ~(1 << wValue);
1482 }
1483 break;
1484 case GetHubDescriptor:
1485 hub_descriptor ((struct usb_hub_descriptor *) buf);
1486 break;
1487 case GetHubStatus:
1488 *(u32 *) buf = __constant_cpu_to_le32 (0);
1489 break;
1490 case GetPortStatus:
1491 if (wIndex != 1)
1492 retval = -EPIPE;
1493
1494 /* whoever resets or resumes must GetPortStatus to
1495 * complete it!!
1496 */
1497 if (dum->resuming && time_after (jiffies, dum->re_timeout)) {
1498 dum->port_status |= (1 << USB_PORT_FEAT_C_SUSPEND);
1499 dum->port_status &= ~(1 << USB_PORT_FEAT_SUSPEND);
1500 dum->resuming = 0;
1501 dum->re_timeout = 0;
1502 if (dum->driver && dum->driver->resume) {
1503 spin_unlock (&dum->lock);
1504 dum->driver->resume (&dum->gadget);
1505 spin_lock (&dum->lock);
1506 }
1507 }
1508 if ((dum->port_status & (1 << USB_PORT_FEAT_RESET)) != 0
1509 && time_after (jiffies, dum->re_timeout)) {
1510 dum->port_status |= (1 << USB_PORT_FEAT_C_RESET);
1511 dum->port_status &= ~(1 << USB_PORT_FEAT_RESET);
1512 dum->re_timeout = 0;
1513 if (dum->driver) {
1514 dum->port_status |= USB_PORT_STAT_ENABLE;
1515 /* give it the best speed we agree on */
1516 dum->gadget.speed = dum->driver->speed;
1517 dum->gadget.ep0->maxpacket = 64;
1518 switch (dum->gadget.speed) {
1519 case USB_SPEED_HIGH:
1520 dum->port_status |=
1521 USB_PORT_STAT_HIGH_SPEED;
1522 break;
1523 case USB_SPEED_LOW:
1524 dum->gadget.ep0->maxpacket = 8;
1525 dum->port_status |=
1526 USB_PORT_STAT_LOW_SPEED;
1527 break;
1528 default:
1529 dum->gadget.speed = USB_SPEED_FULL;
1530 break;
1531 }
1532 }
1533 }
1534 ((u16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1535 ((u16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1536 break;
1537 case SetHubFeature:
1538 retval = -EPIPE;
1539 break;
1540 case SetPortFeature:
1541 switch (wValue) {
1542 case USB_PORT_FEAT_SUSPEND:
1543 if ((dum->port_status & (1 << USB_PORT_FEAT_SUSPEND))
1544 == 0) {
1545 dum->port_status |=
1546 (1 << USB_PORT_FEAT_SUSPEND);
1547 if (dum->driver && dum->driver->suspend) {
1548 spin_unlock (&dum->lock);
1549 dum->driver->suspend (&dum->gadget);
1550 spin_lock (&dum->lock);
5742b0c9
AS
1551 /* HNP would happen here; for now we
1552 * assume b_bus_req is always true.
1553 */
1554 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1555 & dum->devstatus) != 0)
1556 dev_dbg (dummy_dev(dum),
1557 "no HNP yet!\n");
1da177e4
LT
1558 }
1559 }
1560 break;
1561 case USB_PORT_FEAT_RESET:
1562 /* if it's already running, disconnect first */
1563 if (dum->port_status & USB_PORT_STAT_ENABLE) {
1564 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1565 | USB_PORT_STAT_LOW_SPEED
1566 | USB_PORT_STAT_HIGH_SPEED);
1567 if (dum->driver) {
1568 dev_dbg (dummy_dev(dum),
1569 "disconnect\n");
1570 stop_activity (dum, dum->driver);
1571 }
1572
1573 /* FIXME test that code path! */
1574 }
1575 /* 50msec reset signaling */
1576 dum->re_timeout = jiffies + msecs_to_jiffies(50);
1577 /* FALLTHROUGH */
1578 default:
1579 dum->port_status |= (1 << wValue);
1580 }
1581 break;
1582
1583 default:
1584 dev_dbg (dummy_dev(dum),
1585 "hub control req%04x v%04x i%04x l%d\n",
1586 typeReq, wValue, wIndex, wLength);
1587
1588 /* "protocol stall" on error */
1589 retval = -EPIPE;
1590 }
1591 spin_unlock_irqrestore (&dum->lock, flags);
1592 return retval;
1593}
1594
1595
1596/*-------------------------------------------------------------------------*/
1597
1598static inline ssize_t
1599show_urb (char *buf, size_t size, struct urb *urb)
1600{
1601 int ep = usb_pipeendpoint (urb->pipe);
1602
1603 return snprintf (buf, size,
1604 "urb/%p %s ep%d%s%s len %d/%d\n",
1605 urb,
1606 ({ char *s;
1607 switch (urb->dev->speed) {
1608 case USB_SPEED_LOW: s = "ls"; break;
1609 case USB_SPEED_FULL: s = "fs"; break;
1610 case USB_SPEED_HIGH: s = "hs"; break;
1611 default: s = "?"; break;
1612 }; s; }),
1613 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1614 ({ char *s; \
1615 switch (usb_pipetype (urb->pipe)) { \
1616 case PIPE_CONTROL: s = ""; break; \
1617 case PIPE_BULK: s = "-bulk"; break; \
1618 case PIPE_INTERRUPT: s = "-int"; break; \
1619 default: s = "-iso"; break; \
1620 }; s;}),
1621 urb->actual_length, urb->transfer_buffer_length);
1622}
1623
1624static ssize_t
10523b3b 1625show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
1626{
1627 struct usb_hcd *hcd = dev_get_drvdata (dev);
1628 struct dummy *dum = hcd_to_dummy (hcd);
1629 struct urbp *urbp;
1630 size_t size = 0;
1631 unsigned long flags;
1632
1633 spin_lock_irqsave (&dum->lock, flags);
1634 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1635 size_t temp;
1636
1637 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1638 buf += temp;
1639 size += temp;
1640 }
1641 spin_unlock_irqrestore (&dum->lock, flags);
1642
1643 return size;
1644}
1645static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1646
1647static int dummy_start (struct usb_hcd *hcd)
1648{
1649 struct dummy *dum;
1da177e4
LT
1650 int retval;
1651
1652 dum = hcd_to_dummy (hcd);
1653
1654 /*
1655 * MASTER side init ... we emulate a root hub that'll only ever
1656 * talk to one device (the slave side). Also appears in sysfs,
1657 * just like more familiar pci-based HCDs.
1658 */
1659 spin_lock_init (&dum->lock);
1660 init_timer (&dum->timer);
1661 dum->timer.function = dummy_timer;
1662 dum->timer.data = (unsigned long) dum;
1663
1664 INIT_LIST_HEAD (&dum->urbp_list);
1665
247f3105
AS
1666 if ((retval = dummy_register_udc (dum)) != 0)
1667 return retval;
1da177e4 1668
bc96c0ad
AS
1669 /* only show a low-power port: just 8mA */
1670 hcd->power_budget = 8;
1da177e4 1671 hcd->state = HC_STATE_RUNNING;
1da177e4 1672
5742b0c9
AS
1673#ifdef CONFIG_USB_OTG
1674 hcd->self.otg_port = 1;
1675#endif
1676
1da177e4
LT
1677 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1678 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1679 return 0;
1da177e4
LT
1680}
1681
1682static void dummy_stop (struct usb_hcd *hcd)
1683{
1684 struct dummy *dum;
1685
1686 dum = hcd_to_dummy (hcd);
1687
1688 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1689
1690 usb_gadget_unregister_driver (dum->driver);
1691 dummy_unregister_udc (dum);
1692
1693 dev_info (dummy_dev(dum), "stopped\n");
1694}
1695
1696/*-------------------------------------------------------------------------*/
1697
1698static int dummy_h_get_frame (struct usb_hcd *hcd)
1699{
1700 return dummy_g_get_frame (NULL);
1701}
1702
1703static const struct hc_driver dummy_hcd = {
1704 .description = (char *) driver_name,
1705 .product_desc = "Dummy host controller",
1706 .hcd_priv_size = sizeof(struct dummy),
1707
1708 .flags = HCD_USB2,
1709
1710 .start = dummy_start,
1711 .stop = dummy_stop,
1712
1713 .urb_enqueue = dummy_urb_enqueue,
1714 .urb_dequeue = dummy_urb_dequeue,
1715
1716 .get_frame_number = dummy_h_get_frame,
1717
1718 .hub_status_data = dummy_hub_status,
1719 .hub_control = dummy_hub_control,
1720};
1721
1722static int dummy_probe (struct device *dev)
1723{
1724 struct usb_hcd *hcd;
1725 int retval;
1726
1727 dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1728
1729 hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id);
1730 if (!hcd)
1731 return -ENOMEM;
1732 the_controller = hcd_to_dummy (hcd);
1733
1734 retval = usb_add_hcd(hcd, 0, 0);
1735 if (retval != 0) {
1736 usb_put_hcd (hcd);
1737 the_controller = NULL;
1738 }
1739 return retval;
1740}
1741
1742static void dummy_remove (struct device *dev)
1743{
1744 struct usb_hcd *hcd;
1745
1746 hcd = dev_get_drvdata (dev);
1747 usb_remove_hcd (hcd);
1748 usb_put_hcd (hcd);
1749 the_controller = NULL;
1750}
1751
1752/*-------------------------------------------------------------------------*/
1753
1754static int dummy_pdev_detect (void)
1755{
1756 int retval;
1757
1758 retval = driver_register (&dummy_driver);
1759 if (retval < 0)
1760 return retval;
1761
1762 the_pdev.name = "hc";
1763 the_pdev.dev.driver = &dummy_driver;
1764 the_pdev.dev.release = dummy_pdev_release;
1765
1766 retval = platform_device_register (&the_pdev);
1767 if (retval < 0)
1768 driver_unregister (&dummy_driver);
1769 return retval;
1770}
1771
1772static void dummy_pdev_remove (void)
1773{
1774 platform_device_unregister (&the_pdev);
1775 driver_unregister (&dummy_driver);
1776}
1777
1778/*-------------------------------------------------------------------------*/
1779
1780static int __init init (void)
1781{
1782 int retval;
1783
1784 if (usb_disabled ())
1785 return -ENODEV;
1786 if ((retval = dummy_pdev_detect ()) != 0)
1787 return retval;
1788 if ((retval = dummy_probe (&the_pdev.dev)) != 0)
1789 dummy_pdev_remove ();
1790 return retval;
1791}
1792module_init (init);
1793
1794static void __exit cleanup (void)
1795{
1796 dummy_remove (&the_pdev.dev);
1797 dummy_pdev_remove ();
1798}
1799module_exit (cleanup);
This page took 0.119281 seconds and 5 git commands to generate.