include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / usb / gadget / dummy_hcd.c
blobe755a9d267fc6a9028a89051d98161a85475e31d
1 /*
2 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
6 * Copyright (C) 2003 David Brownell
7 * Copyright (C) 2003-2005 Alan Stern
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * This exposes a device side "USB gadget" API, driven by requests to a
27 * Linux-USB host controller driver. USB traffic is simulated; there's
28 * no need for USB hardware. Use this with two other drivers:
30 * - Gadget driver, responding to requests (slave);
31 * - Host-side device driver, as already familiar in Linux.
33 * Having this all in one kernel can help some stages of development,
34 * bypassing some hardware (and driver) issues. UML could help too.
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/delay.h>
40 #include <linux/ioport.h>
41 #include <linux/slab.h>
42 #include <linux/errno.h>
43 #include <linux/init.h>
44 #include <linux/timer.h>
45 #include <linux/list.h>
46 #include <linux/interrupt.h>
47 #include <linux/platform_device.h>
48 #include <linux/usb.h>
49 #include <linux/usb/gadget.h>
50 #include <linux/usb/hcd.h>
52 #include <asm/byteorder.h>
53 #include <asm/io.h>
54 #include <asm/irq.h>
55 #include <asm/system.h>
56 #include <asm/unaligned.h>
59 #define DRIVER_DESC "USB Host+Gadget Emulator"
60 #define DRIVER_VERSION "02 May 2005"
62 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
64 static const char driver_name [] = "dummy_hcd";
65 static const char driver_desc [] = "USB Host+Gadget Emulator";
67 static const char gadget_name [] = "dummy_udc";
69 MODULE_DESCRIPTION (DRIVER_DESC);
70 MODULE_AUTHOR ("David Brownell");
71 MODULE_LICENSE ("GPL");
73 struct dummy_hcd_module_parameters {
74 bool is_super_speed;
75 bool is_high_speed;
78 static struct dummy_hcd_module_parameters mod_data = {
79 .is_super_speed = false,
80 .is_high_speed = true,
82 module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
83 MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
84 module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
85 MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
86 /*-------------------------------------------------------------------------*/
88 /* gadget side driver data structres */
89 struct dummy_ep {
90 struct list_head queue;
91 unsigned long last_io; /* jiffies timestamp */
92 struct usb_gadget *gadget;
93 const struct usb_endpoint_descriptor *desc;
94 struct usb_ep ep;
95 unsigned halted : 1;
96 unsigned wedged : 1;
97 unsigned already_seen : 1;
98 unsigned setup_stage : 1;
101 struct dummy_request {
102 struct list_head queue; /* ep's requests */
103 struct usb_request req;
106 static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
108 return container_of (_ep, struct dummy_ep, ep);
111 static inline struct dummy_request *usb_request_to_dummy_request
112 (struct usb_request *_req)
114 return container_of (_req, struct dummy_request, req);
117 /*-------------------------------------------------------------------------*/
120 * Every device has ep0 for control requests, plus up to 30 more endpoints,
121 * in one of two types:
123 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
124 * number can be changed. Names like "ep-a" are used for this type.
126 * - Fixed Function: in other cases. some characteristics may be mutable;
127 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
129 * Gadget drivers are responsible for not setting up conflicting endpoint
130 * configurations, illegal or unsupported packet lengths, and so on.
133 static const char ep0name [] = "ep0";
135 static const char *const ep_name [] = {
136 ep0name, /* everyone has ep0 */
138 /* act like a net2280: high speed, six configurable endpoints */
139 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
141 /* or like pxa250: fifteen fixed function endpoints */
142 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
143 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
144 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
145 "ep15in-int",
147 /* or like sa1100: two fixed function endpoints */
148 "ep1out-bulk", "ep2in-bulk",
150 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
152 /*-------------------------------------------------------------------------*/
154 #define FIFO_SIZE 64
156 struct urbp {
157 struct urb *urb;
158 struct list_head urbp_list;
162 enum dummy_rh_state {
163 DUMMY_RH_RESET,
164 DUMMY_RH_SUSPENDED,
165 DUMMY_RH_RUNNING
168 struct dummy_hcd {
169 struct dummy *dum;
170 enum dummy_rh_state rh_state;
171 struct timer_list timer;
172 u32 port_status;
173 u32 old_status;
174 unsigned long re_timeout;
176 struct usb_device *udev;
177 struct list_head urbp_list;
179 unsigned active:1;
180 unsigned old_active:1;
181 unsigned resuming:1;
184 struct dummy {
185 spinlock_t lock;
188 * SLAVE/GADGET side support
190 struct dummy_ep ep [DUMMY_ENDPOINTS];
191 int address;
192 struct usb_gadget gadget;
193 struct usb_gadget_driver *driver;
194 struct dummy_request fifo_req;
195 u8 fifo_buf [FIFO_SIZE];
196 u16 devstatus;
197 unsigned udc_suspended:1;
198 unsigned pullup:1;
201 * MASTER/HOST side support
203 struct dummy_hcd *hs_hcd;
204 struct dummy_hcd *ss_hcd;
207 static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
209 return (struct dummy_hcd *) (hcd->hcd_priv);
212 static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
214 return container_of((void *) dum, struct usb_hcd, hcd_priv);
217 static inline struct device *dummy_dev(struct dummy_hcd *dum)
219 return dummy_hcd_to_hcd(dum)->self.controller;
222 static inline struct device *udc_dev (struct dummy *dum)
224 return dum->gadget.dev.parent;
227 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
229 return container_of (ep->gadget, struct dummy, gadget);
232 static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
234 struct dummy *dum = container_of(gadget, struct dummy, gadget);
235 if (dum->gadget.speed == USB_SPEED_SUPER)
236 return dum->ss_hcd;
237 else
238 return dum->hs_hcd;
241 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
243 return container_of (dev, struct dummy, gadget.dev);
246 static struct dummy the_controller;
248 /*-------------------------------------------------------------------------*/
250 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
252 /* called with spinlock held */
253 static void nuke (struct dummy *dum, struct dummy_ep *ep)
255 while (!list_empty (&ep->queue)) {
256 struct dummy_request *req;
258 req = list_entry (ep->queue.next, struct dummy_request, queue);
259 list_del_init (&req->queue);
260 req->req.status = -ESHUTDOWN;
262 spin_unlock (&dum->lock);
263 req->req.complete (&ep->ep, &req->req);
264 spin_lock (&dum->lock);
268 /* caller must hold lock */
269 static void
270 stop_activity (struct dummy *dum)
272 struct dummy_ep *ep;
274 /* prevent any more requests */
275 dum->address = 0;
277 /* The timer is left running so that outstanding URBs can fail */
279 /* nuke any pending requests first, so driver i/o is quiesced */
280 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
281 nuke (dum, ep);
283 /* driver now does any non-usb quiescing necessary */
287 * set_link_state_by_speed() - Sets the current state of the link according to
288 * the hcd speed
289 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
291 * This function updates the port_status according to the link state and the
292 * speed of the hcd.
294 static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
296 struct dummy *dum = dum_hcd->dum;
298 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
299 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
300 dum_hcd->port_status = 0;
301 } else if (!dum->pullup || dum->udc_suspended) {
302 /* UDC suspend must cause a disconnect */
303 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
304 USB_PORT_STAT_ENABLE);
305 if ((dum_hcd->old_status &
306 USB_PORT_STAT_CONNECTION) != 0)
307 dum_hcd->port_status |=
308 (USB_PORT_STAT_C_CONNECTION << 16);
309 } else {
310 /* device is connected and not suspended */
311 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
312 USB_PORT_STAT_SPEED_5GBPS) ;
313 if ((dum_hcd->old_status &
314 USB_PORT_STAT_CONNECTION) == 0)
315 dum_hcd->port_status |=
316 (USB_PORT_STAT_C_CONNECTION << 16);
317 if ((dum_hcd->port_status &
318 USB_PORT_STAT_ENABLE) == 1 &&
319 (dum_hcd->port_status &
320 USB_SS_PORT_LS_U0) == 1 &&
321 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
322 dum_hcd->active = 1;
324 } else {
325 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
326 dum_hcd->port_status = 0;
327 } else if (!dum->pullup || dum->udc_suspended) {
328 /* UDC suspend must cause a disconnect */
329 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
330 USB_PORT_STAT_ENABLE |
331 USB_PORT_STAT_LOW_SPEED |
332 USB_PORT_STAT_HIGH_SPEED |
333 USB_PORT_STAT_SUSPEND);
334 if ((dum_hcd->old_status &
335 USB_PORT_STAT_CONNECTION) != 0)
336 dum_hcd->port_status |=
337 (USB_PORT_STAT_C_CONNECTION << 16);
338 } else {
339 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
340 if ((dum_hcd->old_status &
341 USB_PORT_STAT_CONNECTION) == 0)
342 dum_hcd->port_status |=
343 (USB_PORT_STAT_C_CONNECTION << 16);
344 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
345 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
346 else if ((dum_hcd->port_status &
347 USB_PORT_STAT_SUSPEND) == 0 &&
348 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
349 dum_hcd->active = 1;
354 /* caller must hold lock */
355 static void set_link_state(struct dummy_hcd *dum_hcd)
357 struct dummy *dum = dum_hcd->dum;
359 dum_hcd->active = 0;
360 if (dum->pullup)
361 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
362 dum->gadget.speed != USB_SPEED_SUPER) ||
363 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
364 dum->gadget.speed == USB_SPEED_SUPER))
365 return;
367 set_link_state_by_speed(dum_hcd);
369 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
370 dum_hcd->active)
371 dum_hcd->resuming = 0;
373 /* if !connected or reset */
374 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
375 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
377 * We're connected and not reset (reset occurred now),
378 * and driver attached - disconnect!
380 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
381 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
382 dum->driver) {
383 stop_activity(dum);
384 spin_unlock(&dum->lock);
385 dum->driver->disconnect(&dum->gadget);
386 spin_lock(&dum->lock);
388 } else if (dum_hcd->active != dum_hcd->old_active) {
389 if (dum_hcd->old_active && dum->driver->suspend) {
390 spin_unlock(&dum->lock);
391 dum->driver->suspend(&dum->gadget);
392 spin_lock(&dum->lock);
393 } else if (!dum_hcd->old_active && dum->driver->resume) {
394 spin_unlock(&dum->lock);
395 dum->driver->resume(&dum->gadget);
396 spin_lock(&dum->lock);
400 dum_hcd->old_status = dum_hcd->port_status;
401 dum_hcd->old_active = dum_hcd->active;
404 /*-------------------------------------------------------------------------*/
406 /* SLAVE/GADGET SIDE DRIVER
408 * This only tracks gadget state. All the work is done when the host
409 * side tries some (emulated) i/o operation. Real device controller
410 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
413 #define is_enabled(dum) \
414 (dum->port_status & USB_PORT_STAT_ENABLE)
416 static int
417 dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
419 struct dummy *dum;
420 struct dummy_hcd *dum_hcd;
421 struct dummy_ep *ep;
422 unsigned max;
423 int retval;
425 ep = usb_ep_to_dummy_ep (_ep);
426 if (!_ep || !desc || ep->desc || _ep->name == ep0name
427 || desc->bDescriptorType != USB_DT_ENDPOINT)
428 return -EINVAL;
429 dum = ep_to_dummy (ep);
430 if (!dum->driver)
431 return -ESHUTDOWN;
433 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
434 if (!is_enabled(dum_hcd))
435 return -ESHUTDOWN;
438 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
439 * maximum packet size.
440 * For SS devices the wMaxPacketSize is limited by 1024.
442 max = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff;
444 /* drivers must not request bad settings, since lower levels
445 * (hardware or its drivers) may not check. some endpoints
446 * can't do iso, many have maxpacket limitations, etc.
448 * since this "hardware" driver is here to help debugging, we
449 * have some extra sanity checks. (there could be more though,
450 * especially for "ep9out" style fixed function ones.)
452 retval = -EINVAL;
453 switch (desc->bmAttributes & 0x03) {
454 case USB_ENDPOINT_XFER_BULK:
455 if (strstr (ep->ep.name, "-iso")
456 || strstr (ep->ep.name, "-int")) {
457 goto done;
459 switch (dum->gadget.speed) {
460 case USB_SPEED_SUPER:
461 if (max == 1024)
462 break;
463 goto done;
464 case USB_SPEED_HIGH:
465 if (max == 512)
466 break;
467 goto done;
468 case USB_SPEED_FULL:
469 if (max == 8 || max == 16 || max == 32 || max == 64)
470 /* we'll fake any legal size */
471 break;
472 /* save a return statement */
473 default:
474 goto done;
476 break;
477 case USB_ENDPOINT_XFER_INT:
478 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
479 goto done;
480 /* real hardware might not handle all packet sizes */
481 switch (dum->gadget.speed) {
482 case USB_SPEED_SUPER:
483 case USB_SPEED_HIGH:
484 if (max <= 1024)
485 break;
486 /* save a return statement */
487 case USB_SPEED_FULL:
488 if (max <= 64)
489 break;
490 /* save a return statement */
491 default:
492 if (max <= 8)
493 break;
494 goto done;
496 break;
497 case USB_ENDPOINT_XFER_ISOC:
498 if (strstr (ep->ep.name, "-bulk")
499 || strstr (ep->ep.name, "-int"))
500 goto done;
501 /* real hardware might not handle all packet sizes */
502 switch (dum->gadget.speed) {
503 case USB_SPEED_SUPER:
504 case USB_SPEED_HIGH:
505 if (max <= 1024)
506 break;
507 /* save a return statement */
508 case USB_SPEED_FULL:
509 if (max <= 1023)
510 break;
511 /* save a return statement */
512 default:
513 goto done;
515 break;
516 default:
517 /* few chips support control except on ep0 */
518 goto done;
521 _ep->maxpacket = max;
522 ep->desc = desc;
524 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
525 _ep->name,
526 desc->bEndpointAddress & 0x0f,
527 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
528 ({ char *val;
529 switch (desc->bmAttributes & 0x03) {
530 case USB_ENDPOINT_XFER_BULK:
531 val = "bulk";
532 break;
533 case USB_ENDPOINT_XFER_ISOC:
534 val = "iso";
535 break;
536 case USB_ENDPOINT_XFER_INT:
537 val = "intr";
538 break;
539 default:
540 val = "ctrl";
541 break;
542 }; val; }),
543 max);
545 /* at this point real hardware should be NAKing transfers
546 * to that endpoint, until a buffer is queued to it.
548 ep->halted = ep->wedged = 0;
549 retval = 0;
550 done:
551 return retval;
554 static int dummy_disable (struct usb_ep *_ep)
556 struct dummy_ep *ep;
557 struct dummy *dum;
558 unsigned long flags;
559 int retval;
561 ep = usb_ep_to_dummy_ep (_ep);
562 if (!_ep || !ep->desc || _ep->name == ep0name)
563 return -EINVAL;
564 dum = ep_to_dummy (ep);
566 spin_lock_irqsave (&dum->lock, flags);
567 ep->desc = NULL;
568 retval = 0;
569 nuke (dum, ep);
570 spin_unlock_irqrestore (&dum->lock, flags);
572 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
573 return retval;
576 static struct usb_request *
577 dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
579 struct dummy_ep *ep;
580 struct dummy_request *req;
582 if (!_ep)
583 return NULL;
584 ep = usb_ep_to_dummy_ep (_ep);
586 req = kzalloc(sizeof(*req), mem_flags);
587 if (!req)
588 return NULL;
589 INIT_LIST_HEAD (&req->queue);
590 return &req->req;
593 static void
594 dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
596 struct dummy_ep *ep;
597 struct dummy_request *req;
599 ep = usb_ep_to_dummy_ep (_ep);
600 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
601 return;
603 req = usb_request_to_dummy_request (_req);
604 WARN_ON (!list_empty (&req->queue));
605 kfree (req);
608 static void
609 fifo_complete (struct usb_ep *ep, struct usb_request *req)
613 static int
614 dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
615 gfp_t mem_flags)
617 struct dummy_ep *ep;
618 struct dummy_request *req;
619 struct dummy *dum;
620 struct dummy_hcd *dum_hcd;
621 unsigned long flags;
623 req = usb_request_to_dummy_request (_req);
624 if (!_req || !list_empty (&req->queue) || !_req->complete)
625 return -EINVAL;
627 ep = usb_ep_to_dummy_ep (_ep);
628 if (!_ep || (!ep->desc && _ep->name != ep0name))
629 return -EINVAL;
631 dum = ep_to_dummy (ep);
632 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
633 if (!dum->driver || !is_enabled(dum_hcd))
634 return -ESHUTDOWN;
636 #if 0
637 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
638 ep, _req, _ep->name, _req->length, _req->buf);
639 #endif
641 _req->status = -EINPROGRESS;
642 _req->actual = 0;
643 spin_lock_irqsave (&dum->lock, flags);
645 /* implement an emulated single-request FIFO */
646 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
647 list_empty (&dum->fifo_req.queue) &&
648 list_empty (&ep->queue) &&
649 _req->length <= FIFO_SIZE) {
650 req = &dum->fifo_req;
651 req->req = *_req;
652 req->req.buf = dum->fifo_buf;
653 memcpy (dum->fifo_buf, _req->buf, _req->length);
654 req->req.context = dum;
655 req->req.complete = fifo_complete;
657 list_add_tail(&req->queue, &ep->queue);
658 spin_unlock (&dum->lock);
659 _req->actual = _req->length;
660 _req->status = 0;
661 _req->complete (_ep, _req);
662 spin_lock (&dum->lock);
663 } else
664 list_add_tail(&req->queue, &ep->queue);
665 spin_unlock_irqrestore (&dum->lock, flags);
667 /* real hardware would likely enable transfers here, in case
668 * it'd been left NAKing.
670 return 0;
673 static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
675 struct dummy_ep *ep;
676 struct dummy *dum;
677 int retval = -EINVAL;
678 unsigned long flags;
679 struct dummy_request *req = NULL;
681 if (!_ep || !_req)
682 return retval;
683 ep = usb_ep_to_dummy_ep (_ep);
684 dum = ep_to_dummy (ep);
686 if (!dum->driver)
687 return -ESHUTDOWN;
689 local_irq_save (flags);
690 spin_lock (&dum->lock);
691 list_for_each_entry (req, &ep->queue, queue) {
692 if (&req->req == _req) {
693 list_del_init (&req->queue);
694 _req->status = -ECONNRESET;
695 retval = 0;
696 break;
699 spin_unlock (&dum->lock);
701 if (retval == 0) {
702 dev_dbg (udc_dev(dum),
703 "dequeued req %p from %s, len %d buf %p\n",
704 req, _ep->name, _req->length, _req->buf);
705 _req->complete (_ep, _req);
707 local_irq_restore (flags);
708 return retval;
711 static int
712 dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
714 struct dummy_ep *ep;
715 struct dummy *dum;
717 if (!_ep)
718 return -EINVAL;
719 ep = usb_ep_to_dummy_ep (_ep);
720 dum = ep_to_dummy (ep);
721 if (!dum->driver)
722 return -ESHUTDOWN;
723 if (!value)
724 ep->halted = ep->wedged = 0;
725 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
726 !list_empty (&ep->queue))
727 return -EAGAIN;
728 else {
729 ep->halted = 1;
730 if (wedged)
731 ep->wedged = 1;
733 /* FIXME clear emulated data toggle too */
734 return 0;
737 static int
738 dummy_set_halt(struct usb_ep *_ep, int value)
740 return dummy_set_halt_and_wedge(_ep, value, 0);
743 static int dummy_set_wedge(struct usb_ep *_ep)
745 if (!_ep || _ep->name == ep0name)
746 return -EINVAL;
747 return dummy_set_halt_and_wedge(_ep, 1, 1);
750 static const struct usb_ep_ops dummy_ep_ops = {
751 .enable = dummy_enable,
752 .disable = dummy_disable,
754 .alloc_request = dummy_alloc_request,
755 .free_request = dummy_free_request,
757 .queue = dummy_queue,
758 .dequeue = dummy_dequeue,
760 .set_halt = dummy_set_halt,
761 .set_wedge = dummy_set_wedge,
764 /*-------------------------------------------------------------------------*/
766 /* there are both host and device side versions of this call ... */
767 static int dummy_g_get_frame (struct usb_gadget *_gadget)
769 struct timeval tv;
771 do_gettimeofday (&tv);
772 return tv.tv_usec / 1000;
775 static int dummy_wakeup (struct usb_gadget *_gadget)
777 struct dummy_hcd *dum_hcd;
779 dum_hcd = gadget_to_dummy_hcd(_gadget);
780 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
781 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
782 return -EINVAL;
783 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
784 return -ENOLINK;
785 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
786 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
787 return -EIO;
789 /* FIXME: What if the root hub is suspended but the port isn't? */
791 /* hub notices our request, issues downstream resume, etc */
792 dum_hcd->resuming = 1;
793 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
794 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
795 return 0;
798 static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
800 struct dummy *dum;
802 dum = (gadget_to_dummy_hcd(_gadget))->dum;
803 if (value)
804 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
805 else
806 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
807 return 0;
810 static void dummy_udc_udpate_ep0(struct dummy *dum)
812 u32 i;
814 if (dum->gadget.speed == USB_SPEED_SUPER) {
815 for (i = 0; i < DUMMY_ENDPOINTS; i++)
816 dum->ep[i].ep.max_streams = 0x10;
817 dum->ep[0].ep.maxpacket = 9;
818 } else {
819 for (i = 0; i < DUMMY_ENDPOINTS; i++)
820 dum->ep[i].ep.max_streams = 0;
821 dum->ep[0].ep.maxpacket = 64;
825 static int dummy_pullup (struct usb_gadget *_gadget, int value)
827 struct dummy_hcd *dum_hcd;
828 struct dummy *dum;
829 unsigned long flags;
831 dum = gadget_dev_to_dummy(&_gadget->dev);
833 if (value && dum->driver) {
834 if (mod_data.is_super_speed)
835 dum->gadget.speed = dum->driver->speed;
836 else if (mod_data.is_high_speed)
837 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
838 dum->driver->speed);
839 else
840 dum->gadget.speed = USB_SPEED_FULL;
841 dummy_udc_udpate_ep0(dum);
843 if (dum->gadget.speed < dum->driver->speed)
844 dev_dbg(udc_dev(dum), "This device can perform faster"
845 " if you connect it to a %s port...\n",
846 (dum->driver->speed == USB_SPEED_SUPER ?
847 "SuperSpeed" : "HighSpeed"));
849 dum_hcd = gadget_to_dummy_hcd(_gadget);
851 spin_lock_irqsave (&dum->lock, flags);
852 dum->pullup = (value != 0);
853 set_link_state(dum_hcd);
854 spin_unlock_irqrestore (&dum->lock, flags);
856 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
857 return 0;
860 static int dummy_udc_start(struct usb_gadget *g,
861 struct usb_gadget_driver *driver);
862 static int dummy_udc_stop(struct usb_gadget *g,
863 struct usb_gadget_driver *driver);
865 static const struct usb_gadget_ops dummy_ops = {
866 .get_frame = dummy_g_get_frame,
867 .wakeup = dummy_wakeup,
868 .set_selfpowered = dummy_set_selfpowered,
869 .pullup = dummy_pullup,
870 .udc_start = dummy_udc_start,
871 .udc_stop = dummy_udc_stop,
874 /*-------------------------------------------------------------------------*/
876 /* "function" sysfs attribute */
877 static ssize_t
878 show_function (struct device *dev, struct device_attribute *attr, char *buf)
880 struct dummy *dum = gadget_dev_to_dummy (dev);
882 if (!dum->driver || !dum->driver->function)
883 return 0;
884 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
886 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
888 /*-------------------------------------------------------------------------*/
891 * Driver registration/unregistration.
893 * This is basically hardware-specific; there's usually only one real USB
894 * device (not host) controller since that's how USB devices are intended
895 * to work. So most implementations of these api calls will rely on the
896 * fact that only one driver will ever bind to the hardware. But curious
897 * hardware can be built with discrete components, so the gadget API doesn't
898 * require that assumption.
900 * For this emulator, it might be convenient to create a usb slave device
901 * for each driver that registers: just add to a big root hub.
904 static int dummy_udc_start(struct usb_gadget *g,
905 struct usb_gadget_driver *driver)
907 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
908 struct dummy *dum = dum_hcd->dum;
910 if (driver->speed == USB_SPEED_UNKNOWN)
911 return -EINVAL;
914 * SLAVE side init ... the layer above hardware, which
915 * can't enumerate without help from the driver we're binding.
918 dum->devstatus = 0;
920 dum->driver = driver;
921 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
922 driver->driver.name);
923 return 0;
926 static int dummy_udc_stop(struct usb_gadget *g,
927 struct usb_gadget_driver *driver)
929 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
930 struct dummy *dum = dum_hcd->dum;
932 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
933 driver->driver.name);
935 dum->driver = NULL;
937 dummy_pullup(&dum->gadget, 0);
938 return 0;
941 #undef is_enabled
943 /* The gadget structure is stored inside the hcd structure and will be
944 * released along with it. */
945 static void
946 dummy_gadget_release (struct device *dev)
948 return;
951 static void init_dummy_udc_hw(struct dummy *dum)
953 int i;
955 INIT_LIST_HEAD(&dum->gadget.ep_list);
956 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
957 struct dummy_ep *ep = &dum->ep[i];
959 if (!ep_name[i])
960 break;
961 ep->ep.name = ep_name[i];
962 ep->ep.ops = &dummy_ep_ops;
963 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
964 ep->halted = ep->wedged = ep->already_seen =
965 ep->setup_stage = 0;
966 ep->ep.maxpacket = ~0;
967 ep->last_io = jiffies;
968 ep->gadget = &dum->gadget;
969 ep->desc = NULL;
970 INIT_LIST_HEAD(&ep->queue);
973 dum->gadget.ep0 = &dum->ep[0].ep;
974 list_del_init(&dum->ep[0].ep.ep_list);
975 INIT_LIST_HEAD(&dum->fifo_req.queue);
977 #ifdef CONFIG_USB_OTG
978 dum->gadget.is_otg = 1;
979 #endif
982 static int dummy_udc_probe (struct platform_device *pdev)
984 struct dummy *dum = &the_controller;
985 int rc;
987 dum->gadget.name = gadget_name;
988 dum->gadget.ops = &dummy_ops;
989 dum->gadget.is_dualspeed = 1;
991 dev_set_name(&dum->gadget.dev, "gadget");
992 dum->gadget.dev.parent = &pdev->dev;
993 dum->gadget.dev.release = dummy_gadget_release;
994 rc = device_register (&dum->gadget.dev);
995 if (rc < 0) {
996 put_device(&dum->gadget.dev);
997 return rc;
1000 init_dummy_udc_hw(dum);
1002 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1003 if (rc < 0)
1004 goto err_udc;
1006 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
1007 if (rc < 0)
1008 goto err_dev;
1009 platform_set_drvdata(pdev, dum);
1010 return rc;
1012 err_dev:
1013 usb_del_gadget_udc(&dum->gadget);
1014 err_udc:
1015 device_unregister(&dum->gadget.dev);
1016 return rc;
1019 static int dummy_udc_remove (struct platform_device *pdev)
1021 struct dummy *dum = platform_get_drvdata (pdev);
1023 usb_del_gadget_udc(&dum->gadget);
1024 platform_set_drvdata (pdev, NULL);
1025 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1026 device_unregister (&dum->gadget.dev);
1027 return 0;
1030 static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1031 int suspend)
1033 spin_lock_irq(&dum->lock);
1034 dum->udc_suspended = suspend;
1035 set_link_state(dum_hcd);
1036 spin_unlock_irq(&dum->lock);
1039 static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
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, 1);
1046 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1047 return 0;
1050 static int dummy_udc_resume(struct platform_device *pdev)
1052 struct dummy *dum = platform_get_drvdata(pdev);
1053 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1055 dev_dbg(&pdev->dev, "%s\n", __func__);
1056 dummy_udc_pm(dum, dum_hcd, 0);
1057 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1058 return 0;
1061 static struct platform_driver dummy_udc_driver = {
1062 .probe = dummy_udc_probe,
1063 .remove = dummy_udc_remove,
1064 .suspend = dummy_udc_suspend,
1065 .resume = dummy_udc_resume,
1066 .driver = {
1067 .name = (char *) gadget_name,
1068 .owner = THIS_MODULE,
1072 /*-------------------------------------------------------------------------*/
1074 /* MASTER/HOST SIDE DRIVER
1076 * this uses the hcd framework to hook up to host side drivers.
1077 * its root hub will only have one device, otherwise it acts like
1078 * a normal host controller.
1080 * when urbs are queued, they're just stuck on a list that we
1081 * scan in a timer callback. that callback connects writes from
1082 * the host with reads from the device, and so on, based on the
1083 * usb 2.0 rules.
1086 static int dummy_urb_enqueue (
1087 struct usb_hcd *hcd,
1088 struct urb *urb,
1089 gfp_t mem_flags
1091 struct dummy_hcd *dum_hcd;
1092 struct urbp *urbp;
1093 unsigned long flags;
1094 int rc;
1096 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1097 return -EINVAL;
1099 urbp = kmalloc (sizeof *urbp, mem_flags);
1100 if (!urbp)
1101 return -ENOMEM;
1102 urbp->urb = urb;
1104 dum_hcd = hcd_to_dummy_hcd(hcd);
1105 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1106 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1107 if (rc) {
1108 kfree(urbp);
1109 goto done;
1112 if (!dum_hcd->udev) {
1113 dum_hcd->udev = urb->dev;
1114 usb_get_dev(dum_hcd->udev);
1115 } else if (unlikely(dum_hcd->udev != urb->dev))
1116 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1118 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1119 urb->hcpriv = urbp;
1120 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1121 urb->error_count = 1; /* mark as a new urb */
1123 /* kick the scheduler, it'll do the rest */
1124 if (!timer_pending(&dum_hcd->timer))
1125 mod_timer(&dum_hcd->timer, jiffies + 1);
1127 done:
1128 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1129 return rc;
1132 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1134 struct dummy_hcd *dum_hcd;
1135 unsigned long flags;
1136 int rc;
1138 /* giveback happens automatically in timer callback,
1139 * so make sure the callback happens */
1140 dum_hcd = hcd_to_dummy_hcd(hcd);
1141 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1143 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1144 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1145 !list_empty(&dum_hcd->urbp_list))
1146 mod_timer(&dum_hcd->timer, jiffies);
1148 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1149 return rc;
1152 /* transfer up to a frame's worth; caller must own lock */
1153 static int
1154 transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1155 int *status)
1157 struct dummy_request *req;
1159 top:
1160 /* if there's no request queued, the device is NAKing; return */
1161 list_for_each_entry (req, &ep->queue, queue) {
1162 unsigned host_len, dev_len, len;
1163 int is_short, to_host;
1164 int rescan = 0;
1166 /* 1..N packets of ep->ep.maxpacket each ... the last one
1167 * may be short (including zero length).
1169 * writer can send a zlp explicitly (length 0) or implicitly
1170 * (length mod maxpacket zero, and 'zero' flag); they always
1171 * terminate reads.
1173 host_len = urb->transfer_buffer_length - urb->actual_length;
1174 dev_len = req->req.length - req->req.actual;
1175 len = min (host_len, dev_len);
1177 /* FIXME update emulated data toggle too */
1179 to_host = usb_pipein (urb->pipe);
1180 if (unlikely (len == 0))
1181 is_short = 1;
1182 else {
1183 char *ubuf, *rbuf;
1185 /* not enough bandwidth left? */
1186 if (limit < ep->ep.maxpacket && limit < len)
1187 break;
1188 len = min (len, (unsigned) limit);
1189 if (len == 0)
1190 break;
1192 /* use an extra pass for the final short packet */
1193 if (len > ep->ep.maxpacket) {
1194 rescan = 1;
1195 len -= (len % ep->ep.maxpacket);
1197 is_short = (len % ep->ep.maxpacket) != 0;
1199 /* else transfer packet(s) */
1200 ubuf = urb->transfer_buffer + urb->actual_length;
1201 rbuf = req->req.buf + req->req.actual;
1202 if (to_host)
1203 memcpy (ubuf, rbuf, len);
1204 else
1205 memcpy (rbuf, ubuf, len);
1206 ep->last_io = jiffies;
1208 limit -= len;
1209 urb->actual_length += len;
1210 req->req.actual += len;
1213 /* short packets terminate, maybe with overflow/underflow.
1214 * it's only really an error to write too much.
1216 * partially filling a buffer optionally blocks queue advances
1217 * (so completion handlers can clean up the queue) but we don't
1218 * need to emulate such data-in-flight.
1220 if (is_short) {
1221 if (host_len == dev_len) {
1222 req->req.status = 0;
1223 *status = 0;
1224 } else if (to_host) {
1225 req->req.status = 0;
1226 if (dev_len > host_len)
1227 *status = -EOVERFLOW;
1228 else
1229 *status = 0;
1230 } else if (!to_host) {
1231 *status = 0;
1232 if (host_len > dev_len)
1233 req->req.status = -EOVERFLOW;
1234 else
1235 req->req.status = 0;
1238 /* many requests terminate without a short packet */
1239 } else {
1240 if (req->req.length == req->req.actual
1241 && !req->req.zero)
1242 req->req.status = 0;
1243 if (urb->transfer_buffer_length == urb->actual_length
1244 && !(urb->transfer_flags
1245 & URB_ZERO_PACKET))
1246 *status = 0;
1249 /* device side completion --> continuable */
1250 if (req->req.status != -EINPROGRESS) {
1251 list_del_init (&req->queue);
1253 spin_unlock (&dum->lock);
1254 req->req.complete (&ep->ep, &req->req);
1255 spin_lock (&dum->lock);
1257 /* requests might have been unlinked... */
1258 rescan = 1;
1261 /* host side completion --> terminate */
1262 if (*status != -EINPROGRESS)
1263 break;
1265 /* rescan to continue with any other queued i/o */
1266 if (rescan)
1267 goto top;
1269 return limit;
1272 static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1274 int limit = ep->ep.maxpacket;
1276 if (dum->gadget.speed == USB_SPEED_HIGH) {
1277 int tmp;
1279 /* high bandwidth mode */
1280 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1281 tmp = (tmp >> 11) & 0x03;
1282 tmp *= 8 /* applies to entire frame */;
1283 limit += limit * tmp;
1285 if (dum->gadget.speed == USB_SPEED_SUPER) {
1286 switch (ep->desc->bmAttributes & 0x03) {
1287 case USB_ENDPOINT_XFER_ISOC:
1288 /* Sec. 4.4.8.2 USB3.0 Spec */
1289 limit = 3 * 16 * 1024 * 8;
1290 break;
1291 case USB_ENDPOINT_XFER_INT:
1292 /* Sec. 4.4.7.2 USB3.0 Spec */
1293 limit = 3 * 1024 * 8;
1294 break;
1295 case USB_ENDPOINT_XFER_BULK:
1296 default:
1297 break;
1300 return limit;
1303 #define is_active(dum_hcd) ((dum_hcd->port_status & \
1304 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1305 USB_PORT_STAT_SUSPEND)) \
1306 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1308 static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1310 int i;
1312 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1313 dum->ss_hcd : dum->hs_hcd)))
1314 return NULL;
1315 if ((address & ~USB_DIR_IN) == 0)
1316 return &dum->ep [0];
1317 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1318 struct dummy_ep *ep = &dum->ep [i];
1320 if (!ep->desc)
1321 continue;
1322 if (ep->desc->bEndpointAddress == address)
1323 return ep;
1325 return NULL;
1328 #undef is_active
1330 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1331 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1332 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1333 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1334 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1335 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1339 * handle_control_request() - handles all control transfers
1340 * @dum: pointer to dummy (the_controller)
1341 * @urb: the urb request to handle
1342 * @setup: pointer to the setup data for a USB device control
1343 * request
1344 * @status: pointer to request handling status
1346 * Return 0 - if the request was handled
1347 * 1 - if the request wasn't handles
1348 * error code on error
1350 static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1351 struct usb_ctrlrequest *setup,
1352 int *status)
1354 struct dummy_ep *ep2;
1355 struct dummy *dum = dum_hcd->dum;
1356 int ret_val = 1;
1357 unsigned w_index;
1358 unsigned w_value;
1360 w_index = le16_to_cpu(setup->wIndex);
1361 w_value = le16_to_cpu(setup->wValue);
1362 switch (setup->bRequest) {
1363 case USB_REQ_SET_ADDRESS:
1364 if (setup->bRequestType != Dev_Request)
1365 break;
1366 dum->address = w_value;
1367 *status = 0;
1368 dev_dbg(udc_dev(dum), "set_address = %d\n",
1369 w_value);
1370 ret_val = 0;
1371 break;
1372 case USB_REQ_SET_FEATURE:
1373 if (setup->bRequestType == Dev_Request) {
1374 ret_val = 0;
1375 switch (w_value) {
1376 case USB_DEVICE_REMOTE_WAKEUP:
1377 break;
1378 case USB_DEVICE_B_HNP_ENABLE:
1379 dum->gadget.b_hnp_enable = 1;
1380 break;
1381 case USB_DEVICE_A_HNP_SUPPORT:
1382 dum->gadget.a_hnp_support = 1;
1383 break;
1384 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1385 dum->gadget.a_alt_hnp_support = 1;
1386 break;
1387 case USB_DEVICE_U1_ENABLE:
1388 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1389 HCD_USB3)
1390 w_value = USB_DEV_STAT_U1_ENABLED;
1391 else
1392 ret_val = -EOPNOTSUPP;
1393 break;
1394 case USB_DEVICE_U2_ENABLE:
1395 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1396 HCD_USB3)
1397 w_value = USB_DEV_STAT_U2_ENABLED;
1398 else
1399 ret_val = -EOPNOTSUPP;
1400 break;
1401 case USB_DEVICE_LTM_ENABLE:
1402 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1403 HCD_USB3)
1404 w_value = USB_DEV_STAT_LTM_ENABLED;
1405 else
1406 ret_val = -EOPNOTSUPP;
1407 break;
1408 default:
1409 ret_val = -EOPNOTSUPP;
1411 if (ret_val == 0) {
1412 dum->devstatus |= (1 << w_value);
1413 *status = 0;
1415 } else if (setup->bRequestType == Ep_Request) {
1416 /* endpoint halt */
1417 ep2 = find_endpoint(dum, w_index);
1418 if (!ep2 || ep2->ep.name == ep0name) {
1419 ret_val = -EOPNOTSUPP;
1420 break;
1422 ep2->halted = 1;
1423 ret_val = 0;
1424 *status = 0;
1426 break;
1427 case USB_REQ_CLEAR_FEATURE:
1428 if (setup->bRequestType == Dev_Request) {
1429 ret_val = 0;
1430 switch (w_value) {
1431 case USB_DEVICE_REMOTE_WAKEUP:
1432 w_value = USB_DEVICE_REMOTE_WAKEUP;
1433 break;
1434 case USB_DEVICE_U1_ENABLE:
1435 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1436 HCD_USB3)
1437 w_value = USB_DEV_STAT_U1_ENABLED;
1438 else
1439 ret_val = -EOPNOTSUPP;
1440 break;
1441 case USB_DEVICE_U2_ENABLE:
1442 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1443 HCD_USB3)
1444 w_value = USB_DEV_STAT_U2_ENABLED;
1445 else
1446 ret_val = -EOPNOTSUPP;
1447 break;
1448 case USB_DEVICE_LTM_ENABLE:
1449 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1450 HCD_USB3)
1451 w_value = USB_DEV_STAT_LTM_ENABLED;
1452 else
1453 ret_val = -EOPNOTSUPP;
1454 break;
1455 default:
1456 ret_val = -EOPNOTSUPP;
1457 break;
1459 if (ret_val == 0) {
1460 dum->devstatus &= ~(1 << w_value);
1461 *status = 0;
1463 } else if (setup->bRequestType == Ep_Request) {
1464 /* endpoint halt */
1465 ep2 = find_endpoint(dum, w_index);
1466 if (!ep2) {
1467 ret_val = -EOPNOTSUPP;
1468 break;
1470 if (!ep2->wedged)
1471 ep2->halted = 0;
1472 ret_val = 0;
1473 *status = 0;
1475 break;
1476 case USB_REQ_GET_STATUS:
1477 if (setup->bRequestType == Dev_InRequest
1478 || setup->bRequestType == Intf_InRequest
1479 || setup->bRequestType == Ep_InRequest) {
1480 char *buf;
1482 * device: remote wakeup, selfpowered
1483 * interface: nothing
1484 * endpoint: halt
1486 buf = (char *)urb->transfer_buffer;
1487 if (urb->transfer_buffer_length > 0) {
1488 if (setup->bRequestType == Ep_InRequest) {
1489 ep2 = find_endpoint(dum, w_index);
1490 if (!ep2) {
1491 ret_val = -EOPNOTSUPP;
1492 break;
1494 buf[0] = ep2->halted;
1495 } else if (setup->bRequestType ==
1496 Dev_InRequest) {
1497 buf[0] = (u8)dum->devstatus;
1498 } else
1499 buf[0] = 0;
1501 if (urb->transfer_buffer_length > 1)
1502 buf[1] = 0;
1503 urb->actual_length = min_t(u32, 2,
1504 urb->transfer_buffer_length);
1505 ret_val = 0;
1506 *status = 0;
1508 break;
1510 return ret_val;
1513 /* drive both sides of the transfers; looks like irq handlers to
1514 * both drivers except the callbacks aren't in_irq().
1516 static void dummy_timer(unsigned long _dum_hcd)
1518 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1519 struct dummy *dum = dum_hcd->dum;
1520 struct urbp *urbp, *tmp;
1521 unsigned long flags;
1522 int limit, total;
1523 int i;
1525 /* simplistic model for one frame's bandwidth */
1526 switch (dum->gadget.speed) {
1527 case USB_SPEED_LOW:
1528 total = 8/*bytes*/ * 12/*packets*/;
1529 break;
1530 case USB_SPEED_FULL:
1531 total = 64/*bytes*/ * 19/*packets*/;
1532 break;
1533 case USB_SPEED_HIGH:
1534 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1535 break;
1536 case USB_SPEED_SUPER:
1537 /* Bus speed is 500000 bytes/ms, so use a little less */
1538 total = 490000;
1539 break;
1540 default:
1541 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1542 return;
1545 /* FIXME if HZ != 1000 this will probably misbehave ... */
1547 /* look at each urb queued by the host side driver */
1548 spin_lock_irqsave (&dum->lock, flags);
1550 if (!dum_hcd->udev) {
1551 dev_err(dummy_dev(dum_hcd),
1552 "timer fired with no URBs pending?\n");
1553 spin_unlock_irqrestore (&dum->lock, flags);
1554 return;
1557 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1558 if (!ep_name [i])
1559 break;
1560 dum->ep [i].already_seen = 0;
1563 restart:
1564 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1565 struct urb *urb;
1566 struct dummy_request *req;
1567 u8 address;
1568 struct dummy_ep *ep = NULL;
1569 int type;
1570 int status = -EINPROGRESS;
1572 urb = urbp->urb;
1573 if (urb->unlinked)
1574 goto return_urb;
1575 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1576 continue;
1577 type = usb_pipetype (urb->pipe);
1579 /* used up this frame's non-periodic bandwidth?
1580 * FIXME there's infinite bandwidth for control and
1581 * periodic transfers ... unrealistic.
1583 if (total <= 0 && type == PIPE_BULK)
1584 continue;
1586 /* find the gadget's ep for this request (if configured) */
1587 address = usb_pipeendpoint (urb->pipe);
1588 if (usb_pipein (urb->pipe))
1589 address |= USB_DIR_IN;
1590 ep = find_endpoint(dum, address);
1591 if (!ep) {
1592 /* set_configuration() disagreement */
1593 dev_dbg(dummy_dev(dum_hcd),
1594 "no ep configured for urb %p\n",
1595 urb);
1596 status = -EPROTO;
1597 goto return_urb;
1600 if (ep->already_seen)
1601 continue;
1602 ep->already_seen = 1;
1603 if (ep == &dum->ep [0] && urb->error_count) {
1604 ep->setup_stage = 1; /* a new urb */
1605 urb->error_count = 0;
1607 if (ep->halted && !ep->setup_stage) {
1608 /* NOTE: must not be iso! */
1609 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1610 ep->ep.name, urb);
1611 status = -EPIPE;
1612 goto return_urb;
1614 /* FIXME make sure both ends agree on maxpacket */
1616 /* handle control requests */
1617 if (ep == &dum->ep [0] && ep->setup_stage) {
1618 struct usb_ctrlrequest setup;
1619 int value = 1;
1621 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1622 /* paranoia, in case of stale queued data */
1623 list_for_each_entry (req, &ep->queue, queue) {
1624 list_del_init (&req->queue);
1625 req->req.status = -EOVERFLOW;
1626 dev_dbg (udc_dev(dum), "stale req = %p\n",
1627 req);
1629 spin_unlock (&dum->lock);
1630 req->req.complete (&ep->ep, &req->req);
1631 spin_lock (&dum->lock);
1632 ep->already_seen = 0;
1633 goto restart;
1636 /* gadget driver never sees set_address or operations
1637 * on standard feature flags. some hardware doesn't
1638 * even expose them.
1640 ep->last_io = jiffies;
1641 ep->setup_stage = 0;
1642 ep->halted = 0;
1644 value = handle_control_request(dum_hcd, urb, &setup,
1645 &status);
1647 /* gadget driver handles all other requests. block
1648 * until setup() returns; no reentrancy issues etc.
1650 if (value > 0) {
1651 spin_unlock (&dum->lock);
1652 value = dum->driver->setup (&dum->gadget,
1653 &setup);
1654 spin_lock (&dum->lock);
1656 if (value >= 0) {
1657 /* no delays (max 64KB data stage) */
1658 limit = 64*1024;
1659 goto treat_control_like_bulk;
1661 /* error, see below */
1664 if (value < 0) {
1665 if (value != -EOPNOTSUPP)
1666 dev_dbg (udc_dev(dum),
1667 "setup --> %d\n",
1668 value);
1669 status = -EPIPE;
1670 urb->actual_length = 0;
1673 goto return_urb;
1676 /* non-control requests */
1677 limit = total;
1678 switch (usb_pipetype (urb->pipe)) {
1679 case PIPE_ISOCHRONOUS:
1680 /* FIXME is it urb->interval since the last xfer?
1681 * use urb->iso_frame_desc[i].
1682 * complete whether or not ep has requests queued.
1683 * report random errors, to debug drivers.
1685 limit = max (limit, periodic_bytes (dum, ep));
1686 status = -ENOSYS;
1687 break;
1689 case PIPE_INTERRUPT:
1690 /* FIXME is it urb->interval since the last xfer?
1691 * this almost certainly polls too fast.
1693 limit = max (limit, periodic_bytes (dum, ep));
1694 /* FALLTHROUGH */
1696 // case PIPE_BULK: case PIPE_CONTROL:
1697 default:
1698 treat_control_like_bulk:
1699 ep->last_io = jiffies;
1700 total = transfer(dum, urb, ep, limit, &status);
1701 break;
1704 /* incomplete transfer? */
1705 if (status == -EINPROGRESS)
1706 continue;
1708 return_urb:
1709 list_del (&urbp->urbp_list);
1710 kfree (urbp);
1711 if (ep)
1712 ep->already_seen = ep->setup_stage = 0;
1714 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1715 spin_unlock (&dum->lock);
1716 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1717 spin_lock (&dum->lock);
1719 goto restart;
1722 if (list_empty(&dum_hcd->urbp_list)) {
1723 usb_put_dev(dum_hcd->udev);
1724 dum_hcd->udev = NULL;
1725 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1726 /* want a 1 msec delay here */
1727 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1730 spin_unlock_irqrestore (&dum->lock, flags);
1733 /*-------------------------------------------------------------------------*/
1735 #define PORT_C_MASK \
1736 ((USB_PORT_STAT_C_CONNECTION \
1737 | USB_PORT_STAT_C_ENABLE \
1738 | USB_PORT_STAT_C_SUSPEND \
1739 | USB_PORT_STAT_C_OVERCURRENT \
1740 | USB_PORT_STAT_C_RESET) << 16)
1742 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1744 struct dummy_hcd *dum_hcd;
1745 unsigned long flags;
1746 int retval = 0;
1748 dum_hcd = hcd_to_dummy_hcd(hcd);
1750 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1751 if (!HCD_HW_ACCESSIBLE(hcd))
1752 goto done;
1754 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1755 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1756 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1757 set_link_state(dum_hcd);
1760 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1761 *buf = (1 << 1);
1762 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1763 dum_hcd->port_status);
1764 retval = 1;
1765 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1766 usb_hcd_resume_root_hub (hcd);
1768 done:
1769 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1770 return retval;
1773 static inline void
1774 ss_hub_descriptor(struct usb_hub_descriptor *desc)
1776 memset(desc, 0, sizeof *desc);
1777 desc->bDescriptorType = 0x2a;
1778 desc->bDescLength = 12;
1779 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1780 desc->bNbrPorts = 1;
1781 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1782 desc->u.ss.DeviceRemovable = 0xffff;
1785 static inline void
1786 hub_descriptor (struct usb_hub_descriptor *desc)
1788 memset (desc, 0, sizeof *desc);
1789 desc->bDescriptorType = 0x29;
1790 desc->bDescLength = 9;
1791 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1792 desc->bNbrPorts = 1;
1793 desc->u.hs.DeviceRemovable[0] = 0xff;
1794 desc->u.hs.DeviceRemovable[1] = 0xff;
1797 static int dummy_hub_control (
1798 struct usb_hcd *hcd,
1799 u16 typeReq,
1800 u16 wValue,
1801 u16 wIndex,
1802 char *buf,
1803 u16 wLength
1805 struct dummy_hcd *dum_hcd;
1806 int retval = 0;
1807 unsigned long flags;
1809 if (!HCD_HW_ACCESSIBLE(hcd))
1810 return -ETIMEDOUT;
1812 dum_hcd = hcd_to_dummy_hcd(hcd);
1814 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1815 switch (typeReq) {
1816 case ClearHubFeature:
1817 break;
1818 case ClearPortFeature:
1819 switch (wValue) {
1820 case USB_PORT_FEAT_SUSPEND:
1821 if (hcd->speed == HCD_USB3) {
1822 dev_dbg(dummy_dev(dum_hcd),
1823 "USB_PORT_FEAT_SUSPEND req not "
1824 "supported for USB 3.0 roothub\n");
1825 goto error;
1827 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1828 /* 20msec resume signaling */
1829 dum_hcd->resuming = 1;
1830 dum_hcd->re_timeout = jiffies +
1831 msecs_to_jiffies(20);
1833 break;
1834 case USB_PORT_FEAT_POWER:
1835 if (hcd->speed == HCD_USB3) {
1836 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1837 dev_dbg(dummy_dev(dum_hcd),
1838 "power-off\n");
1839 } else
1840 if (dum_hcd->port_status &
1841 USB_SS_PORT_STAT_POWER)
1842 dev_dbg(dummy_dev(dum_hcd),
1843 "power-off\n");
1844 /* FALLS THROUGH */
1845 default:
1846 dum_hcd->port_status &= ~(1 << wValue);
1847 set_link_state(dum_hcd);
1849 break;
1850 case GetHubDescriptor:
1851 if (hcd->speed == HCD_USB3 &&
1852 (wLength < USB_DT_SS_HUB_SIZE ||
1853 wValue != (USB_DT_SS_HUB << 8))) {
1854 dev_dbg(dummy_dev(dum_hcd),
1855 "Wrong hub descriptor type for "
1856 "USB 3.0 roothub.\n");
1857 goto error;
1859 if (hcd->speed == HCD_USB3)
1860 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1861 else
1862 hub_descriptor((struct usb_hub_descriptor *) buf);
1863 break;
1864 case GetHubStatus:
1865 *(__le32 *) buf = cpu_to_le32 (0);
1866 break;
1867 case GetPortStatus:
1868 if (wIndex != 1)
1869 retval = -EPIPE;
1871 /* whoever resets or resumes must GetPortStatus to
1872 * complete it!!
1874 if (dum_hcd->resuming &&
1875 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1876 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1877 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1879 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1880 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1881 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1882 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1883 if (dum_hcd->dum->pullup) {
1884 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
1886 if (hcd->speed < HCD_USB3) {
1887 switch (dum_hcd->dum->gadget.speed) {
1888 case USB_SPEED_HIGH:
1889 dum_hcd->port_status |=
1890 USB_PORT_STAT_HIGH_SPEED;
1891 break;
1892 case USB_SPEED_LOW:
1893 dum_hcd->dum->gadget.ep0->
1894 maxpacket = 8;
1895 dum_hcd->port_status |=
1896 USB_PORT_STAT_LOW_SPEED;
1897 break;
1898 default:
1899 dum_hcd->dum->gadget.speed =
1900 USB_SPEED_FULL;
1901 break;
1906 set_link_state(dum_hcd);
1907 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1908 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
1909 break;
1910 case SetHubFeature:
1911 retval = -EPIPE;
1912 break;
1913 case SetPortFeature:
1914 switch (wValue) {
1915 case USB_PORT_FEAT_LINK_STATE:
1916 if (hcd->speed != HCD_USB3) {
1917 dev_dbg(dummy_dev(dum_hcd),
1918 "USB_PORT_FEAT_LINK_STATE req not "
1919 "supported for USB 2.0 roothub\n");
1920 goto error;
1923 * Since this is dummy we don't have an actual link so
1924 * there is nothing to do for the SET_LINK_STATE cmd
1926 break;
1927 case USB_PORT_FEAT_U1_TIMEOUT:
1928 case USB_PORT_FEAT_U2_TIMEOUT:
1929 /* TODO: add suspend/resume support! */
1930 if (hcd->speed != HCD_USB3) {
1931 dev_dbg(dummy_dev(dum_hcd),
1932 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1933 "supported for USB 2.0 roothub\n");
1934 goto error;
1936 break;
1937 case USB_PORT_FEAT_SUSPEND:
1938 /* Applicable only for USB2.0 hub */
1939 if (hcd->speed == HCD_USB3) {
1940 dev_dbg(dummy_dev(dum_hcd),
1941 "USB_PORT_FEAT_SUSPEND req not "
1942 "supported for USB 3.0 roothub\n");
1943 goto error;
1945 if (dum_hcd->active) {
1946 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
1948 /* HNP would happen here; for now we
1949 * assume b_bus_req is always true.
1951 set_link_state(dum_hcd);
1952 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1953 & dum_hcd->dum->devstatus) != 0)
1954 dev_dbg(dummy_dev(dum_hcd),
1955 "no HNP yet!\n");
1957 break;
1958 case USB_PORT_FEAT_POWER:
1959 if (hcd->speed == HCD_USB3)
1960 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
1961 else
1962 dum_hcd->port_status |= USB_PORT_STAT_POWER;
1963 set_link_state(dum_hcd);
1964 break;
1965 case USB_PORT_FEAT_BH_PORT_RESET:
1966 /* Applicable only for USB3.0 hub */
1967 if (hcd->speed != HCD_USB3) {
1968 dev_dbg(dummy_dev(dum_hcd),
1969 "USB_PORT_FEAT_BH_PORT_RESET req not "
1970 "supported for USB 2.0 roothub\n");
1971 goto error;
1973 /* FALLS THROUGH */
1974 case USB_PORT_FEAT_RESET:
1975 /* if it's already enabled, disable */
1976 if (hcd->speed == HCD_USB3) {
1977 dum_hcd->port_status = 0;
1978 dum_hcd->port_status =
1979 (USB_SS_PORT_STAT_POWER |
1980 USB_PORT_STAT_CONNECTION |
1981 USB_PORT_STAT_RESET);
1982 } else
1983 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
1984 | USB_PORT_STAT_LOW_SPEED
1985 | USB_PORT_STAT_HIGH_SPEED);
1987 * We want to reset device status. All but the
1988 * Self powered feature
1990 dum_hcd->dum->devstatus &=
1991 (1 << USB_DEVICE_SELF_POWERED);
1993 * FIXME USB3.0: what is the correct reset signaling
1994 * interval? Is it still 50msec as for HS?
1996 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
1997 /* FALLS THROUGH */
1998 default:
1999 if (hcd->speed == HCD_USB3) {
2000 if ((dum_hcd->port_status &
2001 USB_SS_PORT_STAT_POWER) != 0) {
2002 dum_hcd->port_status |= (1 << wValue);
2003 set_link_state(dum_hcd);
2005 } else
2006 if ((dum_hcd->port_status &
2007 USB_PORT_STAT_POWER) != 0) {
2008 dum_hcd->port_status |= (1 << wValue);
2009 set_link_state(dum_hcd);
2012 break;
2013 case GetPortErrorCount:
2014 if (hcd->speed != HCD_USB3) {
2015 dev_dbg(dummy_dev(dum_hcd),
2016 "GetPortErrorCount req not "
2017 "supported for USB 2.0 roothub\n");
2018 goto error;
2020 /* We'll always return 0 since this is a dummy hub */
2021 *(__le32 *) buf = cpu_to_le32(0);
2022 break;
2023 case SetHubDepth:
2024 if (hcd->speed != HCD_USB3) {
2025 dev_dbg(dummy_dev(dum_hcd),
2026 "SetHubDepth req not supported for "
2027 "USB 2.0 roothub\n");
2028 goto error;
2030 break;
2031 default:
2032 dev_dbg(dummy_dev(dum_hcd),
2033 "hub control req%04x v%04x i%04x l%d\n",
2034 typeReq, wValue, wIndex, wLength);
2035 error:
2036 /* "protocol stall" on error */
2037 retval = -EPIPE;
2039 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2041 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2042 usb_hcd_poll_rh_status (hcd);
2043 return retval;
2046 static int dummy_bus_suspend (struct usb_hcd *hcd)
2048 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2050 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
2052 spin_lock_irq(&dum_hcd->dum->lock);
2053 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2054 set_link_state(dum_hcd);
2055 hcd->state = HC_STATE_SUSPENDED;
2056 spin_unlock_irq(&dum_hcd->dum->lock);
2057 return 0;
2060 static int dummy_bus_resume (struct usb_hcd *hcd)
2062 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2063 int rc = 0;
2065 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
2067 spin_lock_irq(&dum_hcd->dum->lock);
2068 if (!HCD_HW_ACCESSIBLE(hcd)) {
2069 rc = -ESHUTDOWN;
2070 } else {
2071 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2072 set_link_state(dum_hcd);
2073 if (!list_empty(&dum_hcd->urbp_list))
2074 mod_timer(&dum_hcd->timer, jiffies);
2075 hcd->state = HC_STATE_RUNNING;
2077 spin_unlock_irq(&dum_hcd->dum->lock);
2078 return rc;
2081 /*-------------------------------------------------------------------------*/
2083 static inline ssize_t
2084 show_urb (char *buf, size_t size, struct urb *urb)
2086 int ep = usb_pipeendpoint (urb->pipe);
2088 return snprintf (buf, size,
2089 "urb/%p %s ep%d%s%s len %d/%d\n",
2090 urb,
2091 ({ char *s;
2092 switch (urb->dev->speed) {
2093 case USB_SPEED_LOW:
2094 s = "ls";
2095 break;
2096 case USB_SPEED_FULL:
2097 s = "fs";
2098 break;
2099 case USB_SPEED_HIGH:
2100 s = "hs";
2101 break;
2102 case USB_SPEED_SUPER:
2103 s = "ss";
2104 break;
2105 default:
2106 s = "?";
2107 break;
2108 }; s; }),
2109 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2110 ({ char *s; \
2111 switch (usb_pipetype (urb->pipe)) { \
2112 case PIPE_CONTROL: \
2113 s = ""; \
2114 break; \
2115 case PIPE_BULK: \
2116 s = "-bulk"; \
2117 break; \
2118 case PIPE_INTERRUPT: \
2119 s = "-int"; \
2120 break; \
2121 default: \
2122 s = "-iso"; \
2123 break; \
2124 }; s;}),
2125 urb->actual_length, urb->transfer_buffer_length);
2128 static ssize_t
2129 show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
2131 struct usb_hcd *hcd = dev_get_drvdata (dev);
2132 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2133 struct urbp *urbp;
2134 size_t size = 0;
2135 unsigned long flags;
2137 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2138 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2139 size_t temp;
2141 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2142 buf += temp;
2143 size += temp;
2145 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2147 return size;
2149 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2151 static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2153 init_timer(&dum_hcd->timer);
2154 dum_hcd->timer.function = dummy_timer;
2155 dum_hcd->timer.data = (unsigned long)dum_hcd;
2156 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2157 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2158 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2159 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2160 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2161 #ifdef CONFIG_USB_OTG
2162 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2163 #endif
2164 return 0;
2166 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2167 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2170 static int dummy_start(struct usb_hcd *hcd)
2172 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2175 * MASTER side init ... we emulate a root hub that'll only ever
2176 * talk to one device (the slave side). Also appears in sysfs,
2177 * just like more familiar pci-based HCDs.
2179 if (!usb_hcd_is_primary_hcd(hcd))
2180 return dummy_start_ss(dum_hcd);
2182 spin_lock_init(&dum_hcd->dum->lock);
2183 init_timer(&dum_hcd->timer);
2184 dum_hcd->timer.function = dummy_timer;
2185 dum_hcd->timer.data = (unsigned long)dum_hcd;
2186 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2188 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2190 hcd->power_budget = POWER_BUDGET;
2191 hcd->state = HC_STATE_RUNNING;
2192 hcd->uses_new_polling = 1;
2194 #ifdef CONFIG_USB_OTG
2195 hcd->self.otg_port = 1;
2196 #endif
2198 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2199 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2202 static void dummy_stop (struct usb_hcd *hcd)
2204 struct dummy *dum;
2206 dum = (hcd_to_dummy_hcd(hcd))->dum;
2207 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2208 usb_gadget_unregister_driver(dum->driver);
2209 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2212 /*-------------------------------------------------------------------------*/
2214 static int dummy_h_get_frame (struct usb_hcd *hcd)
2216 return dummy_g_get_frame (NULL);
2219 static int dummy_setup(struct usb_hcd *hcd)
2221 if (usb_hcd_is_primary_hcd(hcd)) {
2222 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2223 the_controller.hs_hcd->dum = &the_controller;
2225 * Mark the first roothub as being USB 2.0.
2226 * The USB 3.0 roothub will be registered later by
2227 * dummy_hcd_probe()
2229 hcd->speed = HCD_USB2;
2230 hcd->self.root_hub->speed = USB_SPEED_HIGH;
2231 } else {
2232 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2233 the_controller.ss_hcd->dum = &the_controller;
2234 hcd->speed = HCD_USB3;
2235 hcd->self.root_hub->speed = USB_SPEED_SUPER;
2237 return 0;
2240 /* Change a group of bulk endpoints to support multiple stream IDs */
2241 int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2242 struct usb_host_endpoint **eps, unsigned int num_eps,
2243 unsigned int num_streams, gfp_t mem_flags)
2245 if (hcd->speed != HCD_USB3)
2246 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2247 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2248 __func__);
2249 return 0;
2252 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2253 int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2254 struct usb_host_endpoint **eps, unsigned int num_eps,
2255 gfp_t mem_flags)
2257 if (hcd->speed != HCD_USB3)
2258 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2259 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2260 __func__);
2261 return 0;
2264 static struct hc_driver dummy_hcd = {
2265 .description = (char *) driver_name,
2266 .product_desc = "Dummy host controller",
2267 .hcd_priv_size = sizeof(struct dummy_hcd),
2269 .flags = HCD_USB3 | HCD_SHARED,
2271 .reset = dummy_setup,
2272 .start = dummy_start,
2273 .stop = dummy_stop,
2275 .urb_enqueue = dummy_urb_enqueue,
2276 .urb_dequeue = dummy_urb_dequeue,
2278 .get_frame_number = dummy_h_get_frame,
2280 .hub_status_data = dummy_hub_status,
2281 .hub_control = dummy_hub_control,
2282 .bus_suspend = dummy_bus_suspend,
2283 .bus_resume = dummy_bus_resume,
2285 .alloc_streams = dummy_alloc_streams,
2286 .free_streams = dummy_free_streams,
2289 static int dummy_hcd_probe(struct platform_device *pdev)
2291 struct usb_hcd *hs_hcd;
2292 struct usb_hcd *ss_hcd;
2293 int retval;
2295 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2297 if (!mod_data.is_super_speed)
2298 dummy_hcd.flags = HCD_USB2;
2299 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2300 if (!hs_hcd)
2301 return -ENOMEM;
2302 hs_hcd->has_tt = 1;
2304 retval = usb_add_hcd(hs_hcd, 0, 0);
2305 if (retval != 0) {
2306 usb_put_hcd(hs_hcd);
2307 return retval;
2310 if (mod_data.is_super_speed) {
2311 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2312 dev_name(&pdev->dev), hs_hcd);
2313 if (!ss_hcd) {
2314 retval = -ENOMEM;
2315 goto dealloc_usb2_hcd;
2318 retval = usb_add_hcd(ss_hcd, 0, 0);
2319 if (retval)
2320 goto put_usb3_hcd;
2322 return 0;
2324 put_usb3_hcd:
2325 usb_put_hcd(ss_hcd);
2326 dealloc_usb2_hcd:
2327 usb_put_hcd(hs_hcd);
2328 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
2329 return retval;
2332 static int dummy_hcd_remove(struct platform_device *pdev)
2334 struct dummy *dum;
2336 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
2338 if (dum->ss_hcd) {
2339 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2340 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2343 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2344 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2346 the_controller.hs_hcd = NULL;
2347 the_controller.ss_hcd = NULL;
2349 return 0;
2352 static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
2354 struct usb_hcd *hcd;
2355 struct dummy_hcd *dum_hcd;
2356 int rc = 0;
2358 dev_dbg (&pdev->dev, "%s\n", __func__);
2360 hcd = platform_get_drvdata (pdev);
2361 dum_hcd = hcd_to_dummy_hcd(hcd);
2362 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2363 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2364 rc = -EBUSY;
2365 } else
2366 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2367 return rc;
2370 static int dummy_hcd_resume (struct platform_device *pdev)
2372 struct usb_hcd *hcd;
2374 dev_dbg (&pdev->dev, "%s\n", __func__);
2376 hcd = platform_get_drvdata (pdev);
2377 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2378 usb_hcd_poll_rh_status (hcd);
2379 return 0;
2382 static struct platform_driver dummy_hcd_driver = {
2383 .probe = dummy_hcd_probe,
2384 .remove = dummy_hcd_remove,
2385 .suspend = dummy_hcd_suspend,
2386 .resume = dummy_hcd_resume,
2387 .driver = {
2388 .name = (char *) driver_name,
2389 .owner = THIS_MODULE,
2393 /*-------------------------------------------------------------------------*/
2395 static struct platform_device *the_udc_pdev;
2396 static struct platform_device *the_hcd_pdev;
2398 static int __init init (void)
2400 int retval = -ENOMEM;
2402 if (usb_disabled ())
2403 return -ENODEV;
2405 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2406 return -EINVAL;
2408 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2409 if (!the_hcd_pdev)
2410 return retval;
2411 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2412 if (!the_udc_pdev)
2413 goto err_alloc_udc;
2415 retval = platform_driver_register(&dummy_hcd_driver);
2416 if (retval < 0)
2417 goto err_register_hcd_driver;
2418 retval = platform_driver_register(&dummy_udc_driver);
2419 if (retval < 0)
2420 goto err_register_udc_driver;
2422 retval = platform_device_add(the_hcd_pdev);
2423 if (retval < 0)
2424 goto err_add_hcd;
2425 if (!the_controller.hs_hcd ||
2426 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
2428 * The hcd was added successfully but its probe function failed
2429 * for some reason.
2431 retval = -EINVAL;
2432 goto err_add_udc;
2434 retval = platform_device_add(the_udc_pdev);
2435 if (retval < 0)
2436 goto err_add_udc;
2437 if (!platform_get_drvdata(the_udc_pdev)) {
2439 * The udc was added successfully but its probe function failed
2440 * for some reason.
2442 retval = -EINVAL;
2443 goto err_probe_udc;
2445 return retval;
2447 err_probe_udc:
2448 platform_device_del(the_udc_pdev);
2449 err_add_udc:
2450 platform_device_del(the_hcd_pdev);
2451 err_add_hcd:
2452 platform_driver_unregister(&dummy_udc_driver);
2453 err_register_udc_driver:
2454 platform_driver_unregister(&dummy_hcd_driver);
2455 err_register_hcd_driver:
2456 platform_device_put(the_udc_pdev);
2457 err_alloc_udc:
2458 platform_device_put(the_hcd_pdev);
2459 return retval;
2461 module_init (init);
2463 static void __exit cleanup (void)
2465 platform_device_unregister(the_udc_pdev);
2466 platform_device_unregister(the_hcd_pdev);
2467 platform_driver_unregister(&dummy_udc_driver);
2468 platform_driver_unregister(&dummy_hcd_driver);
2470 module_exit (cleanup);