Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / usb / gadget / dummy_hcd.c
blob909864204f56d5d57039c0656862093f26965578
1 /*
2 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
6 * Copyright (C) 2003 David Brownell
7 * Copyright (C) 2003-2005 Alan Stern
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
17 * This exposes a device side "USB gadget" API, driven by requests to a
18 * Linux-USB host controller driver. USB traffic is simulated; there's
19 * no need for USB hardware. Use this with two other drivers:
21 * - Gadget driver, responding to requests (slave);
22 * - Host-side device driver, as already familiar in Linux.
24 * Having this all in one kernel can help some stages of development,
25 * bypassing some hardware (and driver) issues. UML could help too.
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/timer.h>
36 #include <linux/list.h>
37 #include <linux/interrupt.h>
38 #include <linux/platform_device.h>
39 #include <linux/usb.h>
40 #include <linux/usb/gadget.h>
41 #include <linux/usb/hcd.h>
43 #include <asm/byteorder.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/system.h>
47 #include <asm/unaligned.h>
50 #define DRIVER_DESC "USB Host+Gadget Emulator"
51 #define DRIVER_VERSION "02 May 2005"
53 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
55 static const char driver_name [] = "dummy_hcd";
56 static const char driver_desc [] = "USB Host+Gadget Emulator";
58 static const char gadget_name [] = "dummy_udc";
60 MODULE_DESCRIPTION (DRIVER_DESC);
61 MODULE_AUTHOR ("David Brownell");
62 MODULE_LICENSE ("GPL");
64 struct dummy_hcd_module_parameters {
65 bool is_super_speed;
66 bool is_high_speed;
69 static struct dummy_hcd_module_parameters mod_data = {
70 .is_super_speed = false,
71 .is_high_speed = true,
73 module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
74 MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
75 module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
76 MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
77 /*-------------------------------------------------------------------------*/
79 /* gadget side driver data structres */
80 struct dummy_ep {
81 struct list_head queue;
82 unsigned long last_io; /* jiffies timestamp */
83 struct usb_gadget *gadget;
84 const struct usb_endpoint_descriptor *desc;
85 struct usb_ep ep;
86 unsigned halted : 1;
87 unsigned wedged : 1;
88 unsigned already_seen : 1;
89 unsigned setup_stage : 1;
92 struct dummy_request {
93 struct list_head queue; /* ep's requests */
94 struct usb_request req;
97 static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
99 return container_of (_ep, struct dummy_ep, ep);
102 static inline struct dummy_request *usb_request_to_dummy_request
103 (struct usb_request *_req)
105 return container_of (_req, struct dummy_request, req);
108 /*-------------------------------------------------------------------------*/
111 * Every device has ep0 for control requests, plus up to 30 more endpoints,
112 * in one of two types:
114 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
115 * number can be changed. Names like "ep-a" are used for this type.
117 * - Fixed Function: in other cases. some characteristics may be mutable;
118 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
120 * Gadget drivers are responsible for not setting up conflicting endpoint
121 * configurations, illegal or unsupported packet lengths, and so on.
124 static const char ep0name [] = "ep0";
126 static const char *const ep_name [] = {
127 ep0name, /* everyone has ep0 */
129 /* act like a net2280: high speed, six configurable endpoints */
130 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
132 /* or like pxa250: fifteen fixed function endpoints */
133 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
134 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
135 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
136 "ep15in-int",
138 /* or like sa1100: two fixed function endpoints */
139 "ep1out-bulk", "ep2in-bulk",
141 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
143 /*-------------------------------------------------------------------------*/
145 #define FIFO_SIZE 64
147 struct urbp {
148 struct urb *urb;
149 struct list_head urbp_list;
153 enum dummy_rh_state {
154 DUMMY_RH_RESET,
155 DUMMY_RH_SUSPENDED,
156 DUMMY_RH_RUNNING
159 struct dummy_hcd {
160 struct dummy *dum;
161 enum dummy_rh_state rh_state;
162 struct timer_list timer;
163 u32 port_status;
164 u32 old_status;
165 unsigned long re_timeout;
167 struct usb_device *udev;
168 struct list_head urbp_list;
170 unsigned active:1;
171 unsigned old_active:1;
172 unsigned resuming:1;
175 struct dummy {
176 spinlock_t lock;
179 * SLAVE/GADGET side support
181 struct dummy_ep ep [DUMMY_ENDPOINTS];
182 int address;
183 struct usb_gadget gadget;
184 struct usb_gadget_driver *driver;
185 struct dummy_request fifo_req;
186 u8 fifo_buf [FIFO_SIZE];
187 u16 devstatus;
188 unsigned udc_suspended:1;
189 unsigned pullup:1;
192 * MASTER/HOST side support
194 struct dummy_hcd *hs_hcd;
195 struct dummy_hcd *ss_hcd;
198 static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
200 return (struct dummy_hcd *) (hcd->hcd_priv);
203 static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
205 return container_of((void *) dum, struct usb_hcd, hcd_priv);
208 static inline struct device *dummy_dev(struct dummy_hcd *dum)
210 return dummy_hcd_to_hcd(dum)->self.controller;
213 static inline struct device *udc_dev (struct dummy *dum)
215 return dum->gadget.dev.parent;
218 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
220 return container_of (ep->gadget, struct dummy, gadget);
223 static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
225 struct dummy *dum = container_of(gadget, struct dummy, gadget);
226 if (dum->gadget.speed == USB_SPEED_SUPER)
227 return dum->ss_hcd;
228 else
229 return dum->hs_hcd;
232 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
234 return container_of (dev, struct dummy, gadget.dev);
237 static struct dummy the_controller;
239 /*-------------------------------------------------------------------------*/
241 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
243 /* called with spinlock held */
244 static void nuke (struct dummy *dum, struct dummy_ep *ep)
246 while (!list_empty (&ep->queue)) {
247 struct dummy_request *req;
249 req = list_entry (ep->queue.next, struct dummy_request, queue);
250 list_del_init (&req->queue);
251 req->req.status = -ESHUTDOWN;
253 spin_unlock (&dum->lock);
254 req->req.complete (&ep->ep, &req->req);
255 spin_lock (&dum->lock);
259 /* caller must hold lock */
260 static void
261 stop_activity (struct dummy *dum)
263 struct dummy_ep *ep;
265 /* prevent any more requests */
266 dum->address = 0;
268 /* The timer is left running so that outstanding URBs can fail */
270 /* nuke any pending requests first, so driver i/o is quiesced */
271 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
272 nuke (dum, ep);
274 /* driver now does any non-usb quiescing necessary */
278 * set_link_state_by_speed() - Sets the current state of the link according to
279 * the hcd speed
280 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
282 * This function updates the port_status according to the link state and the
283 * speed of the hcd.
285 static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
287 struct dummy *dum = dum_hcd->dum;
289 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
290 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
291 dum_hcd->port_status = 0;
292 } else if (!dum->pullup || dum->udc_suspended) {
293 /* UDC suspend must cause a disconnect */
294 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
295 USB_PORT_STAT_ENABLE);
296 if ((dum_hcd->old_status &
297 USB_PORT_STAT_CONNECTION) != 0)
298 dum_hcd->port_status |=
299 (USB_PORT_STAT_C_CONNECTION << 16);
300 } else {
301 /* device is connected and not suspended */
302 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
303 USB_PORT_STAT_SPEED_5GBPS) ;
304 if ((dum_hcd->old_status &
305 USB_PORT_STAT_CONNECTION) == 0)
306 dum_hcd->port_status |=
307 (USB_PORT_STAT_C_CONNECTION << 16);
308 if ((dum_hcd->port_status &
309 USB_PORT_STAT_ENABLE) == 1 &&
310 (dum_hcd->port_status &
311 USB_SS_PORT_LS_U0) == 1 &&
312 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
313 dum_hcd->active = 1;
315 } else {
316 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
317 dum_hcd->port_status = 0;
318 } else if (!dum->pullup || dum->udc_suspended) {
319 /* UDC suspend must cause a disconnect */
320 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
321 USB_PORT_STAT_ENABLE |
322 USB_PORT_STAT_LOW_SPEED |
323 USB_PORT_STAT_HIGH_SPEED |
324 USB_PORT_STAT_SUSPEND);
325 if ((dum_hcd->old_status &
326 USB_PORT_STAT_CONNECTION) != 0)
327 dum_hcd->port_status |=
328 (USB_PORT_STAT_C_CONNECTION << 16);
329 } else {
330 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
331 if ((dum_hcd->old_status &
332 USB_PORT_STAT_CONNECTION) == 0)
333 dum_hcd->port_status |=
334 (USB_PORT_STAT_C_CONNECTION << 16);
335 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
336 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
337 else if ((dum_hcd->port_status &
338 USB_PORT_STAT_SUSPEND) == 0 &&
339 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
340 dum_hcd->active = 1;
345 /* caller must hold lock */
346 static void set_link_state(struct dummy_hcd *dum_hcd)
348 struct dummy *dum = dum_hcd->dum;
350 dum_hcd->active = 0;
351 if (dum->pullup)
352 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
353 dum->gadget.speed != USB_SPEED_SUPER) ||
354 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
355 dum->gadget.speed == USB_SPEED_SUPER))
356 return;
358 set_link_state_by_speed(dum_hcd);
360 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
361 dum_hcd->active)
362 dum_hcd->resuming = 0;
364 /* if !connected or reset */
365 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
366 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
368 * We're connected and not reset (reset occurred now),
369 * and driver attached - disconnect!
371 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
372 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
373 dum->driver) {
374 stop_activity(dum);
375 spin_unlock(&dum->lock);
376 dum->driver->disconnect(&dum->gadget);
377 spin_lock(&dum->lock);
379 } else if (dum_hcd->active != dum_hcd->old_active) {
380 if (dum_hcd->old_active && dum->driver->suspend) {
381 spin_unlock(&dum->lock);
382 dum->driver->suspend(&dum->gadget);
383 spin_lock(&dum->lock);
384 } else if (!dum_hcd->old_active && dum->driver->resume) {
385 spin_unlock(&dum->lock);
386 dum->driver->resume(&dum->gadget);
387 spin_lock(&dum->lock);
391 dum_hcd->old_status = dum_hcd->port_status;
392 dum_hcd->old_active = dum_hcd->active;
395 /*-------------------------------------------------------------------------*/
397 /* SLAVE/GADGET SIDE DRIVER
399 * This only tracks gadget state. All the work is done when the host
400 * side tries some (emulated) i/o operation. Real device controller
401 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
404 #define is_enabled(dum) \
405 (dum->port_status & USB_PORT_STAT_ENABLE)
407 static int
408 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
410 struct dummy *dum;
411 struct dummy_hcd *dum_hcd;
412 struct dummy_ep *ep;
413 unsigned max;
414 int retval;
416 ep = usb_ep_to_dummy_ep (_ep);
417 if (!_ep || !desc || ep->desc || _ep->name == ep0name
418 || desc->bDescriptorType != USB_DT_ENDPOINT)
419 return -EINVAL;
420 dum = ep_to_dummy (ep);
421 if (!dum->driver)
422 return -ESHUTDOWN;
424 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
425 if (!is_enabled(dum_hcd))
426 return -ESHUTDOWN;
429 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
430 * maximum packet size.
431 * For SS devices the wMaxPacketSize is limited by 1024.
433 max = usb_endpoint_maxp(desc) & 0x7ff;
435 /* drivers must not request bad settings, since lower levels
436 * (hardware or its drivers) may not check. some endpoints
437 * can't do iso, many have maxpacket limitations, etc.
439 * since this "hardware" driver is here to help debugging, we
440 * have some extra sanity checks. (there could be more though,
441 * especially for "ep9out" style fixed function ones.)
443 retval = -EINVAL;
444 switch (desc->bmAttributes & 0x03) {
445 case USB_ENDPOINT_XFER_BULK:
446 if (strstr (ep->ep.name, "-iso")
447 || strstr (ep->ep.name, "-int")) {
448 goto done;
450 switch (dum->gadget.speed) {
451 case USB_SPEED_SUPER:
452 if (max == 1024)
453 break;
454 goto done;
455 case USB_SPEED_HIGH:
456 if (max == 512)
457 break;
458 goto done;
459 case USB_SPEED_FULL:
460 if (max == 8 || max == 16 || max == 32 || max == 64)
461 /* we'll fake any legal size */
462 break;
463 /* save a return statement */
464 default:
465 goto done;
467 break;
468 case USB_ENDPOINT_XFER_INT:
469 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
470 goto done;
471 /* real hardware might not handle all packet sizes */
472 switch (dum->gadget.speed) {
473 case USB_SPEED_SUPER:
474 case USB_SPEED_HIGH:
475 if (max <= 1024)
476 break;
477 /* save a return statement */
478 case USB_SPEED_FULL:
479 if (max <= 64)
480 break;
481 /* save a return statement */
482 default:
483 if (max <= 8)
484 break;
485 goto done;
487 break;
488 case USB_ENDPOINT_XFER_ISOC:
489 if (strstr (ep->ep.name, "-bulk")
490 || strstr (ep->ep.name, "-int"))
491 goto done;
492 /* real hardware might not handle all packet sizes */
493 switch (dum->gadget.speed) {
494 case USB_SPEED_SUPER:
495 case USB_SPEED_HIGH:
496 if (max <= 1024)
497 break;
498 /* save a return statement */
499 case USB_SPEED_FULL:
500 if (max <= 1023)
501 break;
502 /* save a return statement */
503 default:
504 goto done;
506 break;
507 default:
508 /* few chips support control except on ep0 */
509 goto done;
512 _ep->maxpacket = max;
513 ep->desc = desc;
515 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
516 _ep->name,
517 desc->bEndpointAddress & 0x0f,
518 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
519 ({ char *val;
520 switch (desc->bmAttributes & 0x03) {
521 case USB_ENDPOINT_XFER_BULK:
522 val = "bulk";
523 break;
524 case USB_ENDPOINT_XFER_ISOC:
525 val = "iso";
526 break;
527 case USB_ENDPOINT_XFER_INT:
528 val = "intr";
529 break;
530 default:
531 val = "ctrl";
532 break;
533 }; val; }),
534 max);
536 /* at this point real hardware should be NAKing transfers
537 * to that endpoint, until a buffer is queued to it.
539 ep->halted = ep->wedged = 0;
540 retval = 0;
541 done:
542 return retval;
545 static int dummy_disable (struct usb_ep *_ep)
547 struct dummy_ep *ep;
548 struct dummy *dum;
549 unsigned long flags;
550 int retval;
552 ep = usb_ep_to_dummy_ep (_ep);
553 if (!_ep || !ep->desc || _ep->name == ep0name)
554 return -EINVAL;
555 dum = ep_to_dummy (ep);
557 spin_lock_irqsave (&dum->lock, flags);
558 ep->desc = NULL;
559 retval = 0;
560 nuke (dum, ep);
561 spin_unlock_irqrestore (&dum->lock, flags);
563 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
564 return retval;
567 static struct usb_request *
568 dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
570 struct dummy_ep *ep;
571 struct dummy_request *req;
573 if (!_ep)
574 return NULL;
575 ep = usb_ep_to_dummy_ep (_ep);
577 req = kzalloc(sizeof(*req), mem_flags);
578 if (!req)
579 return NULL;
580 INIT_LIST_HEAD (&req->queue);
581 return &req->req;
584 static void
585 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
587 struct dummy_ep *ep;
588 struct dummy_request *req;
590 ep = usb_ep_to_dummy_ep (_ep);
591 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
592 return;
594 req = usb_request_to_dummy_request (_req);
595 WARN_ON (!list_empty (&req->queue));
596 kfree (req);
599 static void
600 fifo_complete (struct usb_ep *ep, struct usb_request *req)
604 static int
605 dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
606 gfp_t mem_flags)
608 struct dummy_ep *ep;
609 struct dummy_request *req;
610 struct dummy *dum;
611 struct dummy_hcd *dum_hcd;
612 unsigned long flags;
614 req = usb_request_to_dummy_request (_req);
615 if (!_req || !list_empty (&req->queue) || !_req->complete)
616 return -EINVAL;
618 ep = usb_ep_to_dummy_ep (_ep);
619 if (!_ep || (!ep->desc && _ep->name != ep0name))
620 return -EINVAL;
622 dum = ep_to_dummy (ep);
623 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
624 if (!dum->driver || !is_enabled(dum_hcd))
625 return -ESHUTDOWN;
627 #if 0
628 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
629 ep, _req, _ep->name, _req->length, _req->buf);
630 #endif
632 _req->status = -EINPROGRESS;
633 _req->actual = 0;
634 spin_lock_irqsave (&dum->lock, flags);
636 /* implement an emulated single-request FIFO */
637 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
638 list_empty (&dum->fifo_req.queue) &&
639 list_empty (&ep->queue) &&
640 _req->length <= FIFO_SIZE) {
641 req = &dum->fifo_req;
642 req->req = *_req;
643 req->req.buf = dum->fifo_buf;
644 memcpy (dum->fifo_buf, _req->buf, _req->length);
645 req->req.context = dum;
646 req->req.complete = fifo_complete;
648 list_add_tail(&req->queue, &ep->queue);
649 spin_unlock (&dum->lock);
650 _req->actual = _req->length;
651 _req->status = 0;
652 _req->complete (_ep, _req);
653 spin_lock (&dum->lock);
654 } else
655 list_add_tail(&req->queue, &ep->queue);
656 spin_unlock_irqrestore (&dum->lock, flags);
658 /* real hardware would likely enable transfers here, in case
659 * it'd been left NAKing.
661 return 0;
664 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
666 struct dummy_ep *ep;
667 struct dummy *dum;
668 int retval = -EINVAL;
669 unsigned long flags;
670 struct dummy_request *req = NULL;
672 if (!_ep || !_req)
673 return retval;
674 ep = usb_ep_to_dummy_ep (_ep);
675 dum = ep_to_dummy (ep);
677 if (!dum->driver)
678 return -ESHUTDOWN;
680 local_irq_save (flags);
681 spin_lock (&dum->lock);
682 list_for_each_entry (req, &ep->queue, queue) {
683 if (&req->req == _req) {
684 list_del_init (&req->queue);
685 _req->status = -ECONNRESET;
686 retval = 0;
687 break;
690 spin_unlock (&dum->lock);
692 if (retval == 0) {
693 dev_dbg (udc_dev(dum),
694 "dequeued req %p from %s, len %d buf %p\n",
695 req, _ep->name, _req->length, _req->buf);
696 _req->complete (_ep, _req);
698 local_irq_restore (flags);
699 return retval;
702 static int
703 dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
705 struct dummy_ep *ep;
706 struct dummy *dum;
708 if (!_ep)
709 return -EINVAL;
710 ep = usb_ep_to_dummy_ep (_ep);
711 dum = ep_to_dummy (ep);
712 if (!dum->driver)
713 return -ESHUTDOWN;
714 if (!value)
715 ep->halted = ep->wedged = 0;
716 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
717 !list_empty (&ep->queue))
718 return -EAGAIN;
719 else {
720 ep->halted = 1;
721 if (wedged)
722 ep->wedged = 1;
724 /* FIXME clear emulated data toggle too */
725 return 0;
728 static int
729 dummy_set_halt(struct usb_ep *_ep, int value)
731 return dummy_set_halt_and_wedge(_ep, value, 0);
734 static int dummy_set_wedge(struct usb_ep *_ep)
736 if (!_ep || _ep->name == ep0name)
737 return -EINVAL;
738 return dummy_set_halt_and_wedge(_ep, 1, 1);
741 static const struct usb_ep_ops dummy_ep_ops = {
742 .enable = dummy_enable,
743 .disable = dummy_disable,
745 .alloc_request = dummy_alloc_request,
746 .free_request = dummy_free_request,
748 .queue = dummy_queue,
749 .dequeue = dummy_dequeue,
751 .set_halt = dummy_set_halt,
752 .set_wedge = dummy_set_wedge,
755 /*-------------------------------------------------------------------------*/
757 /* there are both host and device side versions of this call ... */
758 static int dummy_g_get_frame (struct usb_gadget *_gadget)
760 struct timeval tv;
762 do_gettimeofday (&tv);
763 return tv.tv_usec / 1000;
766 static int dummy_wakeup (struct usb_gadget *_gadget)
768 struct dummy_hcd *dum_hcd;
770 dum_hcd = gadget_to_dummy_hcd(_gadget);
771 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
772 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
773 return -EINVAL;
774 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
775 return -ENOLINK;
776 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
777 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
778 return -EIO;
780 /* FIXME: What if the root hub is suspended but the port isn't? */
782 /* hub notices our request, issues downstream resume, etc */
783 dum_hcd->resuming = 1;
784 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
785 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
786 return 0;
789 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
791 struct dummy *dum;
793 dum = (gadget_to_dummy_hcd(_gadget))->dum;
794 if (value)
795 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
796 else
797 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
798 return 0;
801 static void dummy_udc_udpate_ep0(struct dummy *dum)
803 u32 i;
805 if (dum->gadget.speed == USB_SPEED_SUPER) {
806 for (i = 0; i < DUMMY_ENDPOINTS; i++)
807 dum->ep[i].ep.max_streams = 0x10;
808 dum->ep[0].ep.maxpacket = 9;
809 } else {
810 for (i = 0; i < DUMMY_ENDPOINTS; i++)
811 dum->ep[i].ep.max_streams = 0;
812 dum->ep[0].ep.maxpacket = 64;
816 static int dummy_pullup (struct usb_gadget *_gadget, int value)
818 struct dummy_hcd *dum_hcd;
819 struct dummy *dum;
820 unsigned long flags;
822 dum = gadget_dev_to_dummy(&_gadget->dev);
824 if (value && dum->driver) {
825 if (mod_data.is_super_speed)
826 dum->gadget.speed = dum->driver->max_speed;
827 else if (mod_data.is_high_speed)
828 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
829 dum->driver->max_speed);
830 else
831 dum->gadget.speed = USB_SPEED_FULL;
832 dummy_udc_udpate_ep0(dum);
834 if (dum->gadget.speed < dum->driver->max_speed)
835 dev_dbg(udc_dev(dum), "This device can perform faster"
836 " if you connect it to a %s port...\n",
837 usb_speed_string(dum->driver->max_speed));
839 dum_hcd = gadget_to_dummy_hcd(_gadget);
841 spin_lock_irqsave (&dum->lock, flags);
842 dum->pullup = (value != 0);
843 set_link_state(dum_hcd);
844 spin_unlock_irqrestore (&dum->lock, flags);
846 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
847 return 0;
850 static int dummy_udc_start(struct usb_gadget *g,
851 struct usb_gadget_driver *driver);
852 static int dummy_udc_stop(struct usb_gadget *g,
853 struct usb_gadget_driver *driver);
855 static const struct usb_gadget_ops dummy_ops = {
856 .get_frame = dummy_g_get_frame,
857 .wakeup = dummy_wakeup,
858 .set_selfpowered = dummy_set_selfpowered,
859 .pullup = dummy_pullup,
860 .udc_start = dummy_udc_start,
861 .udc_stop = dummy_udc_stop,
864 /*-------------------------------------------------------------------------*/
866 /* "function" sysfs attribute */
867 static ssize_t
868 show_function (struct device *dev, struct device_attribute *attr, char *buf)
870 struct dummy *dum = gadget_dev_to_dummy (dev);
872 if (!dum->driver || !dum->driver->function)
873 return 0;
874 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
876 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
878 /*-------------------------------------------------------------------------*/
881 * Driver registration/unregistration.
883 * This is basically hardware-specific; there's usually only one real USB
884 * device (not host) controller since that's how USB devices are intended
885 * to work. So most implementations of these api calls will rely on the
886 * fact that only one driver will ever bind to the hardware. But curious
887 * hardware can be built with discrete components, so the gadget API doesn't
888 * require that assumption.
890 * For this emulator, it might be convenient to create a usb slave device
891 * for each driver that registers: just add to a big root hub.
894 static int dummy_udc_start(struct usb_gadget *g,
895 struct usb_gadget_driver *driver)
897 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
898 struct dummy *dum = dum_hcd->dum;
900 if (driver->max_speed == USB_SPEED_UNKNOWN)
901 return -EINVAL;
904 * SLAVE side init ... the layer above hardware, which
905 * can't enumerate without help from the driver we're binding.
908 dum->devstatus = 0;
910 dum->driver = driver;
911 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
912 driver->driver.name);
913 return 0;
916 static int dummy_udc_stop(struct usb_gadget *g,
917 struct usb_gadget_driver *driver)
919 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
920 struct dummy *dum = dum_hcd->dum;
922 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
923 driver->driver.name);
925 dum->driver = NULL;
927 return 0;
930 #undef is_enabled
932 /* The gadget structure is stored inside the hcd structure and will be
933 * released along with it. */
934 static void
935 dummy_gadget_release (struct device *dev)
937 return;
940 static void init_dummy_udc_hw(struct dummy *dum)
942 int i;
944 INIT_LIST_HEAD(&dum->gadget.ep_list);
945 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
946 struct dummy_ep *ep = &dum->ep[i];
948 if (!ep_name[i])
949 break;
950 ep->ep.name = ep_name[i];
951 ep->ep.ops = &dummy_ep_ops;
952 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
953 ep->halted = ep->wedged = ep->already_seen =
954 ep->setup_stage = 0;
955 ep->ep.maxpacket = ~0;
956 ep->last_io = jiffies;
957 ep->gadget = &dum->gadget;
958 ep->desc = NULL;
959 INIT_LIST_HEAD(&ep->queue);
962 dum->gadget.ep0 = &dum->ep[0].ep;
963 list_del_init(&dum->ep[0].ep.ep_list);
964 INIT_LIST_HEAD(&dum->fifo_req.queue);
966 #ifdef CONFIG_USB_OTG
967 dum->gadget.is_otg = 1;
968 #endif
971 static int dummy_udc_probe (struct platform_device *pdev)
973 struct dummy *dum = &the_controller;
974 int rc;
976 dum->gadget.name = gadget_name;
977 dum->gadget.ops = &dummy_ops;
978 dum->gadget.max_speed = USB_SPEED_SUPER;
980 dev_set_name(&dum->gadget.dev, "gadget");
981 dum->gadget.dev.parent = &pdev->dev;
982 dum->gadget.dev.release = dummy_gadget_release;
983 rc = device_register (&dum->gadget.dev);
984 if (rc < 0) {
985 put_device(&dum->gadget.dev);
986 return rc;
989 init_dummy_udc_hw(dum);
991 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
992 if (rc < 0)
993 goto err_udc;
995 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
996 if (rc < 0)
997 goto err_dev;
998 platform_set_drvdata(pdev, dum);
999 return rc;
1001 err_dev:
1002 usb_del_gadget_udc(&dum->gadget);
1003 err_udc:
1004 device_unregister(&dum->gadget.dev);
1005 return rc;
1008 static int dummy_udc_remove (struct platform_device *pdev)
1010 struct dummy *dum = platform_get_drvdata (pdev);
1012 usb_del_gadget_udc(&dum->gadget);
1013 platform_set_drvdata (pdev, NULL);
1014 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1015 device_unregister (&dum->gadget.dev);
1016 return 0;
1019 static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1020 int suspend)
1022 spin_lock_irq(&dum->lock);
1023 dum->udc_suspended = suspend;
1024 set_link_state(dum_hcd);
1025 spin_unlock_irq(&dum->lock);
1028 static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1030 struct dummy *dum = platform_get_drvdata(pdev);
1031 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1033 dev_dbg(&pdev->dev, "%s\n", __func__);
1034 dummy_udc_pm(dum, dum_hcd, 1);
1035 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1036 return 0;
1039 static int dummy_udc_resume(struct platform_device *pdev)
1041 struct dummy *dum = platform_get_drvdata(pdev);
1042 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1044 dev_dbg(&pdev->dev, "%s\n", __func__);
1045 dummy_udc_pm(dum, dum_hcd, 0);
1046 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1047 return 0;
1050 static struct platform_driver dummy_udc_driver = {
1051 .probe = dummy_udc_probe,
1052 .remove = dummy_udc_remove,
1053 .suspend = dummy_udc_suspend,
1054 .resume = dummy_udc_resume,
1055 .driver = {
1056 .name = (char *) gadget_name,
1057 .owner = THIS_MODULE,
1061 /*-------------------------------------------------------------------------*/
1063 /* MASTER/HOST SIDE DRIVER
1065 * this uses the hcd framework to hook up to host side drivers.
1066 * its root hub will only have one device, otherwise it acts like
1067 * a normal host controller.
1069 * when urbs are queued, they're just stuck on a list that we
1070 * scan in a timer callback. that callback connects writes from
1071 * the host with reads from the device, and so on, based on the
1072 * usb 2.0 rules.
1075 static int dummy_urb_enqueue (
1076 struct usb_hcd *hcd,
1077 struct urb *urb,
1078 gfp_t mem_flags
1080 struct dummy_hcd *dum_hcd;
1081 struct urbp *urbp;
1082 unsigned long flags;
1083 int rc;
1085 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1086 return -EINVAL;
1088 urbp = kmalloc (sizeof *urbp, mem_flags);
1089 if (!urbp)
1090 return -ENOMEM;
1091 urbp->urb = urb;
1093 dum_hcd = hcd_to_dummy_hcd(hcd);
1094 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1095 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1096 if (rc) {
1097 kfree(urbp);
1098 goto done;
1101 if (!dum_hcd->udev) {
1102 dum_hcd->udev = urb->dev;
1103 usb_get_dev(dum_hcd->udev);
1104 } else if (unlikely(dum_hcd->udev != urb->dev))
1105 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1107 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1108 urb->hcpriv = urbp;
1109 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1110 urb->error_count = 1; /* mark as a new urb */
1112 /* kick the scheduler, it'll do the rest */
1113 if (!timer_pending(&dum_hcd->timer))
1114 mod_timer(&dum_hcd->timer, jiffies + 1);
1116 done:
1117 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1118 return rc;
1121 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1123 struct dummy_hcd *dum_hcd;
1124 unsigned long flags;
1125 int rc;
1127 /* giveback happens automatically in timer callback,
1128 * so make sure the callback happens */
1129 dum_hcd = hcd_to_dummy_hcd(hcd);
1130 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1132 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1133 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1134 !list_empty(&dum_hcd->urbp_list))
1135 mod_timer(&dum_hcd->timer, jiffies);
1137 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1138 return rc;
1141 /* transfer up to a frame's worth; caller must own lock */
1142 static int
1143 transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1144 int *status)
1146 struct dummy_request *req;
1148 top:
1149 /* if there's no request queued, the device is NAKing; return */
1150 list_for_each_entry (req, &ep->queue, queue) {
1151 unsigned host_len, dev_len, len;
1152 int is_short, to_host;
1153 int rescan = 0;
1155 /* 1..N packets of ep->ep.maxpacket each ... the last one
1156 * may be short (including zero length).
1158 * writer can send a zlp explicitly (length 0) or implicitly
1159 * (length mod maxpacket zero, and 'zero' flag); they always
1160 * terminate reads.
1162 host_len = urb->transfer_buffer_length - urb->actual_length;
1163 dev_len = req->req.length - req->req.actual;
1164 len = min (host_len, dev_len);
1166 /* FIXME update emulated data toggle too */
1168 to_host = usb_pipein (urb->pipe);
1169 if (unlikely (len == 0))
1170 is_short = 1;
1171 else {
1172 char *ubuf, *rbuf;
1174 /* not enough bandwidth left? */
1175 if (limit < ep->ep.maxpacket && limit < len)
1176 break;
1177 len = min (len, (unsigned) limit);
1178 if (len == 0)
1179 break;
1181 /* use an extra pass for the final short packet */
1182 if (len > ep->ep.maxpacket) {
1183 rescan = 1;
1184 len -= (len % ep->ep.maxpacket);
1186 is_short = (len % ep->ep.maxpacket) != 0;
1188 /* else transfer packet(s) */
1189 ubuf = urb->transfer_buffer + urb->actual_length;
1190 rbuf = req->req.buf + req->req.actual;
1191 if (to_host)
1192 memcpy (ubuf, rbuf, len);
1193 else
1194 memcpy (rbuf, ubuf, len);
1195 ep->last_io = jiffies;
1197 limit -= len;
1198 urb->actual_length += len;
1199 req->req.actual += len;
1202 /* short packets terminate, maybe with overflow/underflow.
1203 * it's only really an error to write too much.
1205 * partially filling a buffer optionally blocks queue advances
1206 * (so completion handlers can clean up the queue) but we don't
1207 * need to emulate such data-in-flight.
1209 if (is_short) {
1210 if (host_len == dev_len) {
1211 req->req.status = 0;
1212 *status = 0;
1213 } else if (to_host) {
1214 req->req.status = 0;
1215 if (dev_len > host_len)
1216 *status = -EOVERFLOW;
1217 else
1218 *status = 0;
1219 } else if (!to_host) {
1220 *status = 0;
1221 if (host_len > dev_len)
1222 req->req.status = -EOVERFLOW;
1223 else
1224 req->req.status = 0;
1227 /* many requests terminate without a short packet */
1228 } else {
1229 if (req->req.length == req->req.actual
1230 && !req->req.zero)
1231 req->req.status = 0;
1232 if (urb->transfer_buffer_length == urb->actual_length
1233 && !(urb->transfer_flags
1234 & URB_ZERO_PACKET))
1235 *status = 0;
1238 /* device side completion --> continuable */
1239 if (req->req.status != -EINPROGRESS) {
1240 list_del_init (&req->queue);
1242 spin_unlock (&dum->lock);
1243 req->req.complete (&ep->ep, &req->req);
1244 spin_lock (&dum->lock);
1246 /* requests might have been unlinked... */
1247 rescan = 1;
1250 /* host side completion --> terminate */
1251 if (*status != -EINPROGRESS)
1252 break;
1254 /* rescan to continue with any other queued i/o */
1255 if (rescan)
1256 goto top;
1258 return limit;
1261 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1263 int limit = ep->ep.maxpacket;
1265 if (dum->gadget.speed == USB_SPEED_HIGH) {
1266 int tmp;
1268 /* high bandwidth mode */
1269 tmp = usb_endpoint_maxp(ep->desc);
1270 tmp = (tmp >> 11) & 0x03;
1271 tmp *= 8 /* applies to entire frame */;
1272 limit += limit * tmp;
1274 if (dum->gadget.speed == USB_SPEED_SUPER) {
1275 switch (ep->desc->bmAttributes & 0x03) {
1276 case USB_ENDPOINT_XFER_ISOC:
1277 /* Sec. 4.4.8.2 USB3.0 Spec */
1278 limit = 3 * 16 * 1024 * 8;
1279 break;
1280 case USB_ENDPOINT_XFER_INT:
1281 /* Sec. 4.4.7.2 USB3.0 Spec */
1282 limit = 3 * 1024 * 8;
1283 break;
1284 case USB_ENDPOINT_XFER_BULK:
1285 default:
1286 break;
1289 return limit;
1292 #define is_active(dum_hcd) ((dum_hcd->port_status & \
1293 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1294 USB_PORT_STAT_SUSPEND)) \
1295 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1297 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1299 int i;
1301 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1302 dum->ss_hcd : dum->hs_hcd)))
1303 return NULL;
1304 if ((address & ~USB_DIR_IN) == 0)
1305 return &dum->ep [0];
1306 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1307 struct dummy_ep *ep = &dum->ep [i];
1309 if (!ep->desc)
1310 continue;
1311 if (ep->desc->bEndpointAddress == address)
1312 return ep;
1314 return NULL;
1317 #undef is_active
1319 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1320 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1321 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1322 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1323 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1324 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1328 * handle_control_request() - handles all control transfers
1329 * @dum: pointer to dummy (the_controller)
1330 * @urb: the urb request to handle
1331 * @setup: pointer to the setup data for a USB device control
1332 * request
1333 * @status: pointer to request handling status
1335 * Return 0 - if the request was handled
1336 * 1 - if the request wasn't handles
1337 * error code on error
1339 static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1340 struct usb_ctrlrequest *setup,
1341 int *status)
1343 struct dummy_ep *ep2;
1344 struct dummy *dum = dum_hcd->dum;
1345 int ret_val = 1;
1346 unsigned w_index;
1347 unsigned w_value;
1349 w_index = le16_to_cpu(setup->wIndex);
1350 w_value = le16_to_cpu(setup->wValue);
1351 switch (setup->bRequest) {
1352 case USB_REQ_SET_ADDRESS:
1353 if (setup->bRequestType != Dev_Request)
1354 break;
1355 dum->address = w_value;
1356 *status = 0;
1357 dev_dbg(udc_dev(dum), "set_address = %d\n",
1358 w_value);
1359 ret_val = 0;
1360 break;
1361 case USB_REQ_SET_FEATURE:
1362 if (setup->bRequestType == Dev_Request) {
1363 ret_val = 0;
1364 switch (w_value) {
1365 case USB_DEVICE_REMOTE_WAKEUP:
1366 break;
1367 case USB_DEVICE_B_HNP_ENABLE:
1368 dum->gadget.b_hnp_enable = 1;
1369 break;
1370 case USB_DEVICE_A_HNP_SUPPORT:
1371 dum->gadget.a_hnp_support = 1;
1372 break;
1373 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1374 dum->gadget.a_alt_hnp_support = 1;
1375 break;
1376 case USB_DEVICE_U1_ENABLE:
1377 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1378 HCD_USB3)
1379 w_value = USB_DEV_STAT_U1_ENABLED;
1380 else
1381 ret_val = -EOPNOTSUPP;
1382 break;
1383 case USB_DEVICE_U2_ENABLE:
1384 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1385 HCD_USB3)
1386 w_value = USB_DEV_STAT_U2_ENABLED;
1387 else
1388 ret_val = -EOPNOTSUPP;
1389 break;
1390 case USB_DEVICE_LTM_ENABLE:
1391 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1392 HCD_USB3)
1393 w_value = USB_DEV_STAT_LTM_ENABLED;
1394 else
1395 ret_val = -EOPNOTSUPP;
1396 break;
1397 default:
1398 ret_val = -EOPNOTSUPP;
1400 if (ret_val == 0) {
1401 dum->devstatus |= (1 << w_value);
1402 *status = 0;
1404 } else if (setup->bRequestType == Ep_Request) {
1405 /* endpoint halt */
1406 ep2 = find_endpoint(dum, w_index);
1407 if (!ep2 || ep2->ep.name == ep0name) {
1408 ret_val = -EOPNOTSUPP;
1409 break;
1411 ep2->halted = 1;
1412 ret_val = 0;
1413 *status = 0;
1415 break;
1416 case USB_REQ_CLEAR_FEATURE:
1417 if (setup->bRequestType == Dev_Request) {
1418 ret_val = 0;
1419 switch (w_value) {
1420 case USB_DEVICE_REMOTE_WAKEUP:
1421 w_value = USB_DEVICE_REMOTE_WAKEUP;
1422 break;
1423 case USB_DEVICE_U1_ENABLE:
1424 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1425 HCD_USB3)
1426 w_value = USB_DEV_STAT_U1_ENABLED;
1427 else
1428 ret_val = -EOPNOTSUPP;
1429 break;
1430 case USB_DEVICE_U2_ENABLE:
1431 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1432 HCD_USB3)
1433 w_value = USB_DEV_STAT_U2_ENABLED;
1434 else
1435 ret_val = -EOPNOTSUPP;
1436 break;
1437 case USB_DEVICE_LTM_ENABLE:
1438 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1439 HCD_USB3)
1440 w_value = USB_DEV_STAT_LTM_ENABLED;
1441 else
1442 ret_val = -EOPNOTSUPP;
1443 break;
1444 default:
1445 ret_val = -EOPNOTSUPP;
1446 break;
1448 if (ret_val == 0) {
1449 dum->devstatus &= ~(1 << w_value);
1450 *status = 0;
1452 } else if (setup->bRequestType == Ep_Request) {
1453 /* endpoint halt */
1454 ep2 = find_endpoint(dum, w_index);
1455 if (!ep2) {
1456 ret_val = -EOPNOTSUPP;
1457 break;
1459 if (!ep2->wedged)
1460 ep2->halted = 0;
1461 ret_val = 0;
1462 *status = 0;
1464 break;
1465 case USB_REQ_GET_STATUS:
1466 if (setup->bRequestType == Dev_InRequest
1467 || setup->bRequestType == Intf_InRequest
1468 || setup->bRequestType == Ep_InRequest) {
1469 char *buf;
1471 * device: remote wakeup, selfpowered
1472 * interface: nothing
1473 * endpoint: halt
1475 buf = (char *)urb->transfer_buffer;
1476 if (urb->transfer_buffer_length > 0) {
1477 if (setup->bRequestType == Ep_InRequest) {
1478 ep2 = find_endpoint(dum, w_index);
1479 if (!ep2) {
1480 ret_val = -EOPNOTSUPP;
1481 break;
1483 buf[0] = ep2->halted;
1484 } else if (setup->bRequestType ==
1485 Dev_InRequest) {
1486 buf[0] = (u8)dum->devstatus;
1487 } else
1488 buf[0] = 0;
1490 if (urb->transfer_buffer_length > 1)
1491 buf[1] = 0;
1492 urb->actual_length = min_t(u32, 2,
1493 urb->transfer_buffer_length);
1494 ret_val = 0;
1495 *status = 0;
1497 break;
1499 return ret_val;
1502 /* drive both sides of the transfers; looks like irq handlers to
1503 * both drivers except the callbacks aren't in_irq().
1505 static void dummy_timer(unsigned long _dum_hcd)
1507 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1508 struct dummy *dum = dum_hcd->dum;
1509 struct urbp *urbp, *tmp;
1510 unsigned long flags;
1511 int limit, total;
1512 int i;
1514 /* simplistic model for one frame's bandwidth */
1515 switch (dum->gadget.speed) {
1516 case USB_SPEED_LOW:
1517 total = 8/*bytes*/ * 12/*packets*/;
1518 break;
1519 case USB_SPEED_FULL:
1520 total = 64/*bytes*/ * 19/*packets*/;
1521 break;
1522 case USB_SPEED_HIGH:
1523 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1524 break;
1525 case USB_SPEED_SUPER:
1526 /* Bus speed is 500000 bytes/ms, so use a little less */
1527 total = 490000;
1528 break;
1529 default:
1530 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1531 return;
1534 /* FIXME if HZ != 1000 this will probably misbehave ... */
1536 /* look at each urb queued by the host side driver */
1537 spin_lock_irqsave (&dum->lock, flags);
1539 if (!dum_hcd->udev) {
1540 dev_err(dummy_dev(dum_hcd),
1541 "timer fired with no URBs pending?\n");
1542 spin_unlock_irqrestore (&dum->lock, flags);
1543 return;
1546 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1547 if (!ep_name [i])
1548 break;
1549 dum->ep [i].already_seen = 0;
1552 restart:
1553 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1554 struct urb *urb;
1555 struct dummy_request *req;
1556 u8 address;
1557 struct dummy_ep *ep = NULL;
1558 int type;
1559 int status = -EINPROGRESS;
1561 urb = urbp->urb;
1562 if (urb->unlinked)
1563 goto return_urb;
1564 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1565 continue;
1566 type = usb_pipetype (urb->pipe);
1568 /* used up this frame's non-periodic bandwidth?
1569 * FIXME there's infinite bandwidth for control and
1570 * periodic transfers ... unrealistic.
1572 if (total <= 0 && type == PIPE_BULK)
1573 continue;
1575 /* find the gadget's ep for this request (if configured) */
1576 address = usb_pipeendpoint (urb->pipe);
1577 if (usb_pipein (urb->pipe))
1578 address |= USB_DIR_IN;
1579 ep = find_endpoint(dum, address);
1580 if (!ep) {
1581 /* set_configuration() disagreement */
1582 dev_dbg(dummy_dev(dum_hcd),
1583 "no ep configured for urb %p\n",
1584 urb);
1585 status = -EPROTO;
1586 goto return_urb;
1589 if (ep->already_seen)
1590 continue;
1591 ep->already_seen = 1;
1592 if (ep == &dum->ep [0] && urb->error_count) {
1593 ep->setup_stage = 1; /* a new urb */
1594 urb->error_count = 0;
1596 if (ep->halted && !ep->setup_stage) {
1597 /* NOTE: must not be iso! */
1598 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1599 ep->ep.name, urb);
1600 status = -EPIPE;
1601 goto return_urb;
1603 /* FIXME make sure both ends agree on maxpacket */
1605 /* handle control requests */
1606 if (ep == &dum->ep [0] && ep->setup_stage) {
1607 struct usb_ctrlrequest setup;
1608 int value = 1;
1610 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1611 /* paranoia, in case of stale queued data */
1612 list_for_each_entry (req, &ep->queue, queue) {
1613 list_del_init (&req->queue);
1614 req->req.status = -EOVERFLOW;
1615 dev_dbg (udc_dev(dum), "stale req = %p\n",
1616 req);
1618 spin_unlock (&dum->lock);
1619 req->req.complete (&ep->ep, &req->req);
1620 spin_lock (&dum->lock);
1621 ep->already_seen = 0;
1622 goto restart;
1625 /* gadget driver never sees set_address or operations
1626 * on standard feature flags. some hardware doesn't
1627 * even expose them.
1629 ep->last_io = jiffies;
1630 ep->setup_stage = 0;
1631 ep->halted = 0;
1633 value = handle_control_request(dum_hcd, urb, &setup,
1634 &status);
1636 /* gadget driver handles all other requests. block
1637 * until setup() returns; no reentrancy issues etc.
1639 if (value > 0) {
1640 spin_unlock (&dum->lock);
1641 value = dum->driver->setup (&dum->gadget,
1642 &setup);
1643 spin_lock (&dum->lock);
1645 if (value >= 0) {
1646 /* no delays (max 64KB data stage) */
1647 limit = 64*1024;
1648 goto treat_control_like_bulk;
1650 /* error, see below */
1653 if (value < 0) {
1654 if (value != -EOPNOTSUPP)
1655 dev_dbg (udc_dev(dum),
1656 "setup --> %d\n",
1657 value);
1658 status = -EPIPE;
1659 urb->actual_length = 0;
1662 goto return_urb;
1665 /* non-control requests */
1666 limit = total;
1667 switch (usb_pipetype (urb->pipe)) {
1668 case PIPE_ISOCHRONOUS:
1669 /* FIXME is it urb->interval since the last xfer?
1670 * use urb->iso_frame_desc[i].
1671 * complete whether or not ep has requests queued.
1672 * report random errors, to debug drivers.
1674 limit = max (limit, periodic_bytes (dum, ep));
1675 status = -ENOSYS;
1676 break;
1678 case PIPE_INTERRUPT:
1679 /* FIXME is it urb->interval since the last xfer?
1680 * this almost certainly polls too fast.
1682 limit = max (limit, periodic_bytes (dum, ep));
1683 /* FALLTHROUGH */
1685 // case PIPE_BULK: case PIPE_CONTROL:
1686 default:
1687 treat_control_like_bulk:
1688 ep->last_io = jiffies;
1689 total = transfer(dum, urb, ep, limit, &status);
1690 break;
1693 /* incomplete transfer? */
1694 if (status == -EINPROGRESS)
1695 continue;
1697 return_urb:
1698 list_del (&urbp->urbp_list);
1699 kfree (urbp);
1700 if (ep)
1701 ep->already_seen = ep->setup_stage = 0;
1703 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1704 spin_unlock (&dum->lock);
1705 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1706 spin_lock (&dum->lock);
1708 goto restart;
1711 if (list_empty(&dum_hcd->urbp_list)) {
1712 usb_put_dev(dum_hcd->udev);
1713 dum_hcd->udev = NULL;
1714 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1715 /* want a 1 msec delay here */
1716 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1719 spin_unlock_irqrestore (&dum->lock, flags);
1722 /*-------------------------------------------------------------------------*/
1724 #define PORT_C_MASK \
1725 ((USB_PORT_STAT_C_CONNECTION \
1726 | USB_PORT_STAT_C_ENABLE \
1727 | USB_PORT_STAT_C_SUSPEND \
1728 | USB_PORT_STAT_C_OVERCURRENT \
1729 | USB_PORT_STAT_C_RESET) << 16)
1731 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1733 struct dummy_hcd *dum_hcd;
1734 unsigned long flags;
1735 int retval = 0;
1737 dum_hcd = hcd_to_dummy_hcd(hcd);
1739 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1740 if (!HCD_HW_ACCESSIBLE(hcd))
1741 goto done;
1743 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1744 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1745 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1746 set_link_state(dum_hcd);
1749 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1750 *buf = (1 << 1);
1751 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1752 dum_hcd->port_status);
1753 retval = 1;
1754 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1755 usb_hcd_resume_root_hub (hcd);
1757 done:
1758 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1759 return retval;
1762 static inline void
1763 ss_hub_descriptor(struct usb_hub_descriptor *desc)
1765 memset(desc, 0, sizeof *desc);
1766 desc->bDescriptorType = 0x2a;
1767 desc->bDescLength = 12;
1768 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1769 desc->bNbrPorts = 1;
1770 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1771 desc->u.ss.DeviceRemovable = 0xffff;
1774 static inline void
1775 hub_descriptor (struct usb_hub_descriptor *desc)
1777 memset (desc, 0, sizeof *desc);
1778 desc->bDescriptorType = 0x29;
1779 desc->bDescLength = 9;
1780 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1781 desc->bNbrPorts = 1;
1782 desc->u.hs.DeviceRemovable[0] = 0xff;
1783 desc->u.hs.DeviceRemovable[1] = 0xff;
1786 static int dummy_hub_control (
1787 struct usb_hcd *hcd,
1788 u16 typeReq,
1789 u16 wValue,
1790 u16 wIndex,
1791 char *buf,
1792 u16 wLength
1794 struct dummy_hcd *dum_hcd;
1795 int retval = 0;
1796 unsigned long flags;
1798 if (!HCD_HW_ACCESSIBLE(hcd))
1799 return -ETIMEDOUT;
1801 dum_hcd = hcd_to_dummy_hcd(hcd);
1803 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1804 switch (typeReq) {
1805 case ClearHubFeature:
1806 break;
1807 case ClearPortFeature:
1808 switch (wValue) {
1809 case USB_PORT_FEAT_SUSPEND:
1810 if (hcd->speed == HCD_USB3) {
1811 dev_dbg(dummy_dev(dum_hcd),
1812 "USB_PORT_FEAT_SUSPEND req not "
1813 "supported for USB 3.0 roothub\n");
1814 goto error;
1816 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1817 /* 20msec resume signaling */
1818 dum_hcd->resuming = 1;
1819 dum_hcd->re_timeout = jiffies +
1820 msecs_to_jiffies(20);
1822 break;
1823 case USB_PORT_FEAT_POWER:
1824 if (hcd->speed == HCD_USB3) {
1825 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1826 dev_dbg(dummy_dev(dum_hcd),
1827 "power-off\n");
1828 } else
1829 if (dum_hcd->port_status &
1830 USB_SS_PORT_STAT_POWER)
1831 dev_dbg(dummy_dev(dum_hcd),
1832 "power-off\n");
1833 /* FALLS THROUGH */
1834 default:
1835 dum_hcd->port_status &= ~(1 << wValue);
1836 set_link_state(dum_hcd);
1838 break;
1839 case GetHubDescriptor:
1840 if (hcd->speed == HCD_USB3 &&
1841 (wLength < USB_DT_SS_HUB_SIZE ||
1842 wValue != (USB_DT_SS_HUB << 8))) {
1843 dev_dbg(dummy_dev(dum_hcd),
1844 "Wrong hub descriptor type for "
1845 "USB 3.0 roothub.\n");
1846 goto error;
1848 if (hcd->speed == HCD_USB3)
1849 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1850 else
1851 hub_descriptor((struct usb_hub_descriptor *) buf);
1852 break;
1853 case GetHubStatus:
1854 *(__le32 *) buf = cpu_to_le32 (0);
1855 break;
1856 case GetPortStatus:
1857 if (wIndex != 1)
1858 retval = -EPIPE;
1860 /* whoever resets or resumes must GetPortStatus to
1861 * complete it!!
1863 if (dum_hcd->resuming &&
1864 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1865 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1866 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1868 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1869 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1870 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1871 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1872 if (dum_hcd->dum->pullup) {
1873 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
1875 if (hcd->speed < HCD_USB3) {
1876 switch (dum_hcd->dum->gadget.speed) {
1877 case USB_SPEED_HIGH:
1878 dum_hcd->port_status |=
1879 USB_PORT_STAT_HIGH_SPEED;
1880 break;
1881 case USB_SPEED_LOW:
1882 dum_hcd->dum->gadget.ep0->
1883 maxpacket = 8;
1884 dum_hcd->port_status |=
1885 USB_PORT_STAT_LOW_SPEED;
1886 break;
1887 default:
1888 dum_hcd->dum->gadget.speed =
1889 USB_SPEED_FULL;
1890 break;
1895 set_link_state(dum_hcd);
1896 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1897 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
1898 break;
1899 case SetHubFeature:
1900 retval = -EPIPE;
1901 break;
1902 case SetPortFeature:
1903 switch (wValue) {
1904 case USB_PORT_FEAT_LINK_STATE:
1905 if (hcd->speed != HCD_USB3) {
1906 dev_dbg(dummy_dev(dum_hcd),
1907 "USB_PORT_FEAT_LINK_STATE req not "
1908 "supported for USB 2.0 roothub\n");
1909 goto error;
1912 * Since this is dummy we don't have an actual link so
1913 * there is nothing to do for the SET_LINK_STATE cmd
1915 break;
1916 case USB_PORT_FEAT_U1_TIMEOUT:
1917 case USB_PORT_FEAT_U2_TIMEOUT:
1918 /* TODO: add suspend/resume support! */
1919 if (hcd->speed != HCD_USB3) {
1920 dev_dbg(dummy_dev(dum_hcd),
1921 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1922 "supported for USB 2.0 roothub\n");
1923 goto error;
1925 break;
1926 case USB_PORT_FEAT_SUSPEND:
1927 /* Applicable only for USB2.0 hub */
1928 if (hcd->speed == HCD_USB3) {
1929 dev_dbg(dummy_dev(dum_hcd),
1930 "USB_PORT_FEAT_SUSPEND req not "
1931 "supported for USB 3.0 roothub\n");
1932 goto error;
1934 if (dum_hcd->active) {
1935 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
1937 /* HNP would happen here; for now we
1938 * assume b_bus_req is always true.
1940 set_link_state(dum_hcd);
1941 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1942 & dum_hcd->dum->devstatus) != 0)
1943 dev_dbg(dummy_dev(dum_hcd),
1944 "no HNP yet!\n");
1946 break;
1947 case USB_PORT_FEAT_POWER:
1948 if (hcd->speed == HCD_USB3)
1949 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
1950 else
1951 dum_hcd->port_status |= USB_PORT_STAT_POWER;
1952 set_link_state(dum_hcd);
1953 break;
1954 case USB_PORT_FEAT_BH_PORT_RESET:
1955 /* Applicable only for USB3.0 hub */
1956 if (hcd->speed != HCD_USB3) {
1957 dev_dbg(dummy_dev(dum_hcd),
1958 "USB_PORT_FEAT_BH_PORT_RESET req not "
1959 "supported for USB 2.0 roothub\n");
1960 goto error;
1962 /* FALLS THROUGH */
1963 case USB_PORT_FEAT_RESET:
1964 /* if it's already enabled, disable */
1965 if (hcd->speed == HCD_USB3) {
1966 dum_hcd->port_status = 0;
1967 dum_hcd->port_status =
1968 (USB_SS_PORT_STAT_POWER |
1969 USB_PORT_STAT_CONNECTION |
1970 USB_PORT_STAT_RESET);
1971 } else
1972 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
1973 | USB_PORT_STAT_LOW_SPEED
1974 | USB_PORT_STAT_HIGH_SPEED);
1976 * We want to reset device status. All but the
1977 * Self powered feature
1979 dum_hcd->dum->devstatus &=
1980 (1 << USB_DEVICE_SELF_POWERED);
1982 * FIXME USB3.0: what is the correct reset signaling
1983 * interval? Is it still 50msec as for HS?
1985 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
1986 /* FALLS THROUGH */
1987 default:
1988 if (hcd->speed == HCD_USB3) {
1989 if ((dum_hcd->port_status &
1990 USB_SS_PORT_STAT_POWER) != 0) {
1991 dum_hcd->port_status |= (1 << wValue);
1992 set_link_state(dum_hcd);
1994 } else
1995 if ((dum_hcd->port_status &
1996 USB_PORT_STAT_POWER) != 0) {
1997 dum_hcd->port_status |= (1 << wValue);
1998 set_link_state(dum_hcd);
2001 break;
2002 case GetPortErrorCount:
2003 if (hcd->speed != HCD_USB3) {
2004 dev_dbg(dummy_dev(dum_hcd),
2005 "GetPortErrorCount req not "
2006 "supported for USB 2.0 roothub\n");
2007 goto error;
2009 /* We'll always return 0 since this is a dummy hub */
2010 *(__le32 *) buf = cpu_to_le32(0);
2011 break;
2012 case SetHubDepth:
2013 if (hcd->speed != HCD_USB3) {
2014 dev_dbg(dummy_dev(dum_hcd),
2015 "SetHubDepth req not supported for "
2016 "USB 2.0 roothub\n");
2017 goto error;
2019 break;
2020 default:
2021 dev_dbg(dummy_dev(dum_hcd),
2022 "hub control req%04x v%04x i%04x l%d\n",
2023 typeReq, wValue, wIndex, wLength);
2024 error:
2025 /* "protocol stall" on error */
2026 retval = -EPIPE;
2028 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2030 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2031 usb_hcd_poll_rh_status (hcd);
2032 return retval;
2035 static int dummy_bus_suspend (struct usb_hcd *hcd)
2037 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2039 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
2041 spin_lock_irq(&dum_hcd->dum->lock);
2042 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2043 set_link_state(dum_hcd);
2044 hcd->state = HC_STATE_SUSPENDED;
2045 spin_unlock_irq(&dum_hcd->dum->lock);
2046 return 0;
2049 static int dummy_bus_resume (struct usb_hcd *hcd)
2051 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2052 int rc = 0;
2054 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
2056 spin_lock_irq(&dum_hcd->dum->lock);
2057 if (!HCD_HW_ACCESSIBLE(hcd)) {
2058 rc = -ESHUTDOWN;
2059 } else {
2060 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2061 set_link_state(dum_hcd);
2062 if (!list_empty(&dum_hcd->urbp_list))
2063 mod_timer(&dum_hcd->timer, jiffies);
2064 hcd->state = HC_STATE_RUNNING;
2066 spin_unlock_irq(&dum_hcd->dum->lock);
2067 return rc;
2070 /*-------------------------------------------------------------------------*/
2072 static inline ssize_t
2073 show_urb (char *buf, size_t size, struct urb *urb)
2075 int ep = usb_pipeendpoint (urb->pipe);
2077 return snprintf (buf, size,
2078 "urb/%p %s ep%d%s%s len %d/%d\n",
2079 urb,
2080 ({ char *s;
2081 switch (urb->dev->speed) {
2082 case USB_SPEED_LOW:
2083 s = "ls";
2084 break;
2085 case USB_SPEED_FULL:
2086 s = "fs";
2087 break;
2088 case USB_SPEED_HIGH:
2089 s = "hs";
2090 break;
2091 case USB_SPEED_SUPER:
2092 s = "ss";
2093 break;
2094 default:
2095 s = "?";
2096 break;
2097 }; s; }),
2098 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2099 ({ char *s; \
2100 switch (usb_pipetype (urb->pipe)) { \
2101 case PIPE_CONTROL: \
2102 s = ""; \
2103 break; \
2104 case PIPE_BULK: \
2105 s = "-bulk"; \
2106 break; \
2107 case PIPE_INTERRUPT: \
2108 s = "-int"; \
2109 break; \
2110 default: \
2111 s = "-iso"; \
2112 break; \
2113 }; s;}),
2114 urb->actual_length, urb->transfer_buffer_length);
2117 static ssize_t
2118 show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
2120 struct usb_hcd *hcd = dev_get_drvdata (dev);
2121 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2122 struct urbp *urbp;
2123 size_t size = 0;
2124 unsigned long flags;
2126 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2127 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2128 size_t temp;
2130 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2131 buf += temp;
2132 size += temp;
2134 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2136 return size;
2138 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2140 static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2142 init_timer(&dum_hcd->timer);
2143 dum_hcd->timer.function = dummy_timer;
2144 dum_hcd->timer.data = (unsigned long)dum_hcd;
2145 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2146 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2147 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2148 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2149 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2150 #ifdef CONFIG_USB_OTG
2151 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2152 #endif
2153 return 0;
2155 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2156 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2159 static int dummy_start(struct usb_hcd *hcd)
2161 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2164 * MASTER side init ... we emulate a root hub that'll only ever
2165 * talk to one device (the slave side). Also appears in sysfs,
2166 * just like more familiar pci-based HCDs.
2168 if (!usb_hcd_is_primary_hcd(hcd))
2169 return dummy_start_ss(dum_hcd);
2171 spin_lock_init(&dum_hcd->dum->lock);
2172 init_timer(&dum_hcd->timer);
2173 dum_hcd->timer.function = dummy_timer;
2174 dum_hcd->timer.data = (unsigned long)dum_hcd;
2175 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2177 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2179 hcd->power_budget = POWER_BUDGET;
2180 hcd->state = HC_STATE_RUNNING;
2181 hcd->uses_new_polling = 1;
2183 #ifdef CONFIG_USB_OTG
2184 hcd->self.otg_port = 1;
2185 #endif
2187 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2188 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2191 static void dummy_stop (struct usb_hcd *hcd)
2193 struct dummy *dum;
2195 dum = (hcd_to_dummy_hcd(hcd))->dum;
2196 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2197 usb_gadget_unregister_driver(dum->driver);
2198 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2201 /*-------------------------------------------------------------------------*/
2203 static int dummy_h_get_frame (struct usb_hcd *hcd)
2205 return dummy_g_get_frame (NULL);
2208 static int dummy_setup(struct usb_hcd *hcd)
2210 if (usb_hcd_is_primary_hcd(hcd)) {
2211 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2212 the_controller.hs_hcd->dum = &the_controller;
2214 * Mark the first roothub as being USB 2.0.
2215 * The USB 3.0 roothub will be registered later by
2216 * dummy_hcd_probe()
2218 hcd->speed = HCD_USB2;
2219 hcd->self.root_hub->speed = USB_SPEED_HIGH;
2220 } else {
2221 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2222 the_controller.ss_hcd->dum = &the_controller;
2223 hcd->speed = HCD_USB3;
2224 hcd->self.root_hub->speed = USB_SPEED_SUPER;
2226 return 0;
2229 /* Change a group of bulk endpoints to support multiple stream IDs */
2230 int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2231 struct usb_host_endpoint **eps, unsigned int num_eps,
2232 unsigned int num_streams, gfp_t mem_flags)
2234 if (hcd->speed != HCD_USB3)
2235 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2236 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2237 __func__);
2238 return 0;
2241 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2242 int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2243 struct usb_host_endpoint **eps, unsigned int num_eps,
2244 gfp_t mem_flags)
2246 if (hcd->speed != HCD_USB3)
2247 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2248 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2249 __func__);
2250 return 0;
2253 static struct hc_driver dummy_hcd = {
2254 .description = (char *) driver_name,
2255 .product_desc = "Dummy host controller",
2256 .hcd_priv_size = sizeof(struct dummy_hcd),
2258 .flags = HCD_USB3 | HCD_SHARED,
2260 .reset = dummy_setup,
2261 .start = dummy_start,
2262 .stop = dummy_stop,
2264 .urb_enqueue = dummy_urb_enqueue,
2265 .urb_dequeue = dummy_urb_dequeue,
2267 .get_frame_number = dummy_h_get_frame,
2269 .hub_status_data = dummy_hub_status,
2270 .hub_control = dummy_hub_control,
2271 .bus_suspend = dummy_bus_suspend,
2272 .bus_resume = dummy_bus_resume,
2274 .alloc_streams = dummy_alloc_streams,
2275 .free_streams = dummy_free_streams,
2278 static int dummy_hcd_probe(struct platform_device *pdev)
2280 struct usb_hcd *hs_hcd;
2281 struct usb_hcd *ss_hcd;
2282 int retval;
2284 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2286 if (!mod_data.is_super_speed)
2287 dummy_hcd.flags = HCD_USB2;
2288 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2289 if (!hs_hcd)
2290 return -ENOMEM;
2291 hs_hcd->has_tt = 1;
2293 retval = usb_add_hcd(hs_hcd, 0, 0);
2294 if (retval != 0) {
2295 usb_put_hcd(hs_hcd);
2296 return retval;
2299 if (mod_data.is_super_speed) {
2300 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2301 dev_name(&pdev->dev), hs_hcd);
2302 if (!ss_hcd) {
2303 retval = -ENOMEM;
2304 goto dealloc_usb2_hcd;
2307 retval = usb_add_hcd(ss_hcd, 0, 0);
2308 if (retval)
2309 goto put_usb3_hcd;
2311 return 0;
2313 put_usb3_hcd:
2314 usb_put_hcd(ss_hcd);
2315 dealloc_usb2_hcd:
2316 usb_put_hcd(hs_hcd);
2317 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
2318 return retval;
2321 static int dummy_hcd_remove(struct platform_device *pdev)
2323 struct dummy *dum;
2325 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
2327 if (dum->ss_hcd) {
2328 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2329 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2332 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2333 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2335 the_controller.hs_hcd = NULL;
2336 the_controller.ss_hcd = NULL;
2338 return 0;
2341 static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
2343 struct usb_hcd *hcd;
2344 struct dummy_hcd *dum_hcd;
2345 int rc = 0;
2347 dev_dbg (&pdev->dev, "%s\n", __func__);
2349 hcd = platform_get_drvdata (pdev);
2350 dum_hcd = hcd_to_dummy_hcd(hcd);
2351 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2352 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2353 rc = -EBUSY;
2354 } else
2355 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2356 return rc;
2359 static int dummy_hcd_resume (struct platform_device *pdev)
2361 struct usb_hcd *hcd;
2363 dev_dbg (&pdev->dev, "%s\n", __func__);
2365 hcd = platform_get_drvdata (pdev);
2366 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2367 usb_hcd_poll_rh_status (hcd);
2368 return 0;
2371 static struct platform_driver dummy_hcd_driver = {
2372 .probe = dummy_hcd_probe,
2373 .remove = dummy_hcd_remove,
2374 .suspend = dummy_hcd_suspend,
2375 .resume = dummy_hcd_resume,
2376 .driver = {
2377 .name = (char *) driver_name,
2378 .owner = THIS_MODULE,
2382 /*-------------------------------------------------------------------------*/
2384 static struct platform_device *the_udc_pdev;
2385 static struct platform_device *the_hcd_pdev;
2387 static int __init init (void)
2389 int retval = -ENOMEM;
2391 if (usb_disabled ())
2392 return -ENODEV;
2394 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2395 return -EINVAL;
2397 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2398 if (!the_hcd_pdev)
2399 return retval;
2400 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2401 if (!the_udc_pdev)
2402 goto err_alloc_udc;
2404 retval = platform_driver_register(&dummy_hcd_driver);
2405 if (retval < 0)
2406 goto err_register_hcd_driver;
2407 retval = platform_driver_register(&dummy_udc_driver);
2408 if (retval < 0)
2409 goto err_register_udc_driver;
2411 retval = platform_device_add(the_hcd_pdev);
2412 if (retval < 0)
2413 goto err_add_hcd;
2414 if (!the_controller.hs_hcd ||
2415 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
2417 * The hcd was added successfully but its probe function failed
2418 * for some reason.
2420 retval = -EINVAL;
2421 goto err_add_udc;
2423 retval = platform_device_add(the_udc_pdev);
2424 if (retval < 0)
2425 goto err_add_udc;
2426 if (!platform_get_drvdata(the_udc_pdev)) {
2428 * The udc was added successfully but its probe function failed
2429 * for some reason.
2431 retval = -EINVAL;
2432 goto err_probe_udc;
2434 return retval;
2436 err_probe_udc:
2437 platform_device_del(the_udc_pdev);
2438 err_add_udc:
2439 platform_device_del(the_hcd_pdev);
2440 err_add_hcd:
2441 platform_driver_unregister(&dummy_udc_driver);
2442 err_register_udc_driver:
2443 platform_driver_unregister(&dummy_hcd_driver);
2444 err_register_hcd_driver:
2445 platform_device_put(the_udc_pdev);
2446 err_alloc_udc:
2447 platform_device_put(the_hcd_pdev);
2448 return retval;
2450 module_init (init);
2452 static void __exit cleanup (void)
2454 platform_device_unregister(the_udc_pdev);
2455 platform_device_unregister(the_hcd_pdev);
2456 platform_driver_unregister(&dummy_udc_driver);
2457 platform_driver_unregister(&dummy_hcd_driver);
2459 module_exit (cleanup);