Remove all #inclusions of asm/system.h
[linux-2.6.git] / drivers / usb / gadget / dummy_hcd.c
bloba6dfd21641661c08df7a524850ee6ccf670a4f3e
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>
42 #include <linux/scatterlist.h>
44 #include <asm/byteorder.h>
45 #include <linux/io.h>
46 #include <asm/irq.h>
47 #include <asm/unaligned.h>
49 #define DRIVER_DESC "USB Host+Gadget Emulator"
50 #define DRIVER_VERSION "02 May 2005"
52 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
54 static const char driver_name[] = "dummy_hcd";
55 static const char driver_desc[] = "USB Host+Gadget Emulator";
57 static const char gadget_name[] = "dummy_udc";
59 MODULE_DESCRIPTION(DRIVER_DESC);
60 MODULE_AUTHOR("David Brownell");
61 MODULE_LICENSE("GPL");
63 struct dummy_hcd_module_parameters {
64 bool is_super_speed;
65 bool is_high_speed;
68 static struct dummy_hcd_module_parameters mod_data = {
69 .is_super_speed = false,
70 .is_high_speed = true,
72 module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
73 MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
74 module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
75 MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
76 /*-------------------------------------------------------------------------*/
78 /* gadget side driver data structres */
79 struct dummy_ep {
80 struct list_head queue;
81 unsigned long last_io; /* jiffies timestamp */
82 struct usb_gadget *gadget;
83 const struct usb_endpoint_descriptor *desc;
84 struct usb_ep ep;
85 unsigned halted:1;
86 unsigned wedged:1;
87 unsigned already_seen:1;
88 unsigned setup_stage:1;
89 unsigned stream_en: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;
150 struct sg_mapping_iter miter;
151 u32 miter_started;
155 enum dummy_rh_state {
156 DUMMY_RH_RESET,
157 DUMMY_RH_SUSPENDED,
158 DUMMY_RH_RUNNING
161 struct dummy_hcd {
162 struct dummy *dum;
163 enum dummy_rh_state rh_state;
164 struct timer_list timer;
165 u32 port_status;
166 u32 old_status;
167 unsigned long re_timeout;
169 struct usb_device *udev;
170 struct list_head urbp_list;
171 u32 stream_en_ep;
172 u8 num_stream[30 / 2];
174 unsigned active:1;
175 unsigned old_active:1;
176 unsigned resuming:1;
179 struct dummy {
180 spinlock_t lock;
183 * SLAVE/GADGET side support
185 struct dummy_ep ep[DUMMY_ENDPOINTS];
186 int address;
187 struct usb_gadget gadget;
188 struct usb_gadget_driver *driver;
189 struct dummy_request fifo_req;
190 u8 fifo_buf[FIFO_SIZE];
191 u16 devstatus;
192 unsigned udc_suspended:1;
193 unsigned pullup:1;
196 * MASTER/HOST side support
198 struct dummy_hcd *hs_hcd;
199 struct dummy_hcd *ss_hcd;
202 static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
204 return (struct dummy_hcd *) (hcd->hcd_priv);
207 static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
209 return container_of((void *) dum, struct usb_hcd, hcd_priv);
212 static inline struct device *dummy_dev(struct dummy_hcd *dum)
214 return dummy_hcd_to_hcd(dum)->self.controller;
217 static inline struct device *udc_dev(struct dummy *dum)
219 return dum->gadget.dev.parent;
222 static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
224 return container_of(ep->gadget, struct dummy, gadget);
227 static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
229 struct dummy *dum = container_of(gadget, struct dummy, gadget);
230 if (dum->gadget.speed == USB_SPEED_SUPER)
231 return dum->ss_hcd;
232 else
233 return dum->hs_hcd;
236 static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
238 return container_of(dev, struct dummy, gadget.dev);
241 static struct dummy the_controller;
243 /*-------------------------------------------------------------------------*/
245 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
247 /* called with spinlock held */
248 static void nuke(struct dummy *dum, struct dummy_ep *ep)
250 while (!list_empty(&ep->queue)) {
251 struct dummy_request *req;
253 req = list_entry(ep->queue.next, struct dummy_request, queue);
254 list_del_init(&req->queue);
255 req->req.status = -ESHUTDOWN;
257 spin_unlock(&dum->lock);
258 req->req.complete(&ep->ep, &req->req);
259 spin_lock(&dum->lock);
263 /* caller must hold lock */
264 static void stop_activity(struct dummy *dum)
266 struct dummy_ep *ep;
268 /* prevent any more requests */
269 dum->address = 0;
271 /* The timer is left running so that outstanding URBs can fail */
273 /* nuke any pending requests first, so driver i/o is quiesced */
274 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
275 nuke(dum, ep);
277 /* driver now does any non-usb quiescing necessary */
281 * set_link_state_by_speed() - Sets the current state of the link according to
282 * the hcd speed
283 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
285 * This function updates the port_status according to the link state and the
286 * speed of the hcd.
288 static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
290 struct dummy *dum = dum_hcd->dum;
292 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
293 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
294 dum_hcd->port_status = 0;
295 } else if (!dum->pullup || dum->udc_suspended) {
296 /* UDC suspend must cause a disconnect */
297 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
298 USB_PORT_STAT_ENABLE);
299 if ((dum_hcd->old_status &
300 USB_PORT_STAT_CONNECTION) != 0)
301 dum_hcd->port_status |=
302 (USB_PORT_STAT_C_CONNECTION << 16);
303 } else {
304 /* device is connected and not suspended */
305 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
306 USB_PORT_STAT_SPEED_5GBPS) ;
307 if ((dum_hcd->old_status &
308 USB_PORT_STAT_CONNECTION) == 0)
309 dum_hcd->port_status |=
310 (USB_PORT_STAT_C_CONNECTION << 16);
311 if ((dum_hcd->port_status &
312 USB_PORT_STAT_ENABLE) == 1 &&
313 (dum_hcd->port_status &
314 USB_SS_PORT_LS_U0) == 1 &&
315 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
316 dum_hcd->active = 1;
318 } else {
319 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
320 dum_hcd->port_status = 0;
321 } else if (!dum->pullup || dum->udc_suspended) {
322 /* UDC suspend must cause a disconnect */
323 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
324 USB_PORT_STAT_ENABLE |
325 USB_PORT_STAT_LOW_SPEED |
326 USB_PORT_STAT_HIGH_SPEED |
327 USB_PORT_STAT_SUSPEND);
328 if ((dum_hcd->old_status &
329 USB_PORT_STAT_CONNECTION) != 0)
330 dum_hcd->port_status |=
331 (USB_PORT_STAT_C_CONNECTION << 16);
332 } else {
333 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
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 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
339 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
340 else if ((dum_hcd->port_status &
341 USB_PORT_STAT_SUSPEND) == 0 &&
342 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
343 dum_hcd->active = 1;
348 /* caller must hold lock */
349 static void set_link_state(struct dummy_hcd *dum_hcd)
351 struct dummy *dum = dum_hcd->dum;
353 dum_hcd->active = 0;
354 if (dum->pullup)
355 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
356 dum->gadget.speed != USB_SPEED_SUPER) ||
357 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
358 dum->gadget.speed == USB_SPEED_SUPER))
359 return;
361 set_link_state_by_speed(dum_hcd);
363 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
364 dum_hcd->active)
365 dum_hcd->resuming = 0;
367 /* if !connected or reset */
368 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
369 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
371 * We're connected and not reset (reset occurred now),
372 * and driver attached - disconnect!
374 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
375 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
376 dum->driver) {
377 stop_activity(dum);
378 spin_unlock(&dum->lock);
379 dum->driver->disconnect(&dum->gadget);
380 spin_lock(&dum->lock);
382 } else if (dum_hcd->active != dum_hcd->old_active) {
383 if (dum_hcd->old_active && dum->driver->suspend) {
384 spin_unlock(&dum->lock);
385 dum->driver->suspend(&dum->gadget);
386 spin_lock(&dum->lock);
387 } else if (!dum_hcd->old_active && dum->driver->resume) {
388 spin_unlock(&dum->lock);
389 dum->driver->resume(&dum->gadget);
390 spin_lock(&dum->lock);
394 dum_hcd->old_status = dum_hcd->port_status;
395 dum_hcd->old_active = dum_hcd->active;
398 /*-------------------------------------------------------------------------*/
400 /* SLAVE/GADGET SIDE DRIVER
402 * This only tracks gadget state. All the work is done when the host
403 * side tries some (emulated) i/o operation. Real device controller
404 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
407 #define is_enabled(dum) \
408 (dum->port_status & USB_PORT_STAT_ENABLE)
410 static int dummy_enable(struct usb_ep *_ep,
411 const struct usb_endpoint_descriptor *desc)
413 struct dummy *dum;
414 struct dummy_hcd *dum_hcd;
415 struct dummy_ep *ep;
416 unsigned max;
417 int retval;
419 ep = usb_ep_to_dummy_ep(_ep);
420 if (!_ep || !desc || ep->desc || _ep->name == ep0name
421 || desc->bDescriptorType != USB_DT_ENDPOINT)
422 return -EINVAL;
423 dum = ep_to_dummy(ep);
424 if (!dum->driver)
425 return -ESHUTDOWN;
427 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
428 if (!is_enabled(dum_hcd))
429 return -ESHUTDOWN;
432 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
433 * maximum packet size.
434 * For SS devices the wMaxPacketSize is limited by 1024.
436 max = usb_endpoint_maxp(desc) & 0x7ff;
438 /* drivers must not request bad settings, since lower levels
439 * (hardware or its drivers) may not check. some endpoints
440 * can't do iso, many have maxpacket limitations, etc.
442 * since this "hardware" driver is here to help debugging, we
443 * have some extra sanity checks. (there could be more though,
444 * especially for "ep9out" style fixed function ones.)
446 retval = -EINVAL;
447 switch (usb_endpoint_type(desc)) {
448 case USB_ENDPOINT_XFER_BULK:
449 if (strstr(ep->ep.name, "-iso")
450 || strstr(ep->ep.name, "-int")) {
451 goto done;
453 switch (dum->gadget.speed) {
454 case USB_SPEED_SUPER:
455 if (max == 1024)
456 break;
457 goto done;
458 case USB_SPEED_HIGH:
459 if (max == 512)
460 break;
461 goto done;
462 case USB_SPEED_FULL:
463 if (max == 8 || max == 16 || max == 32 || max == 64)
464 /* we'll fake any legal size */
465 break;
466 /* save a return statement */
467 default:
468 goto done;
470 break;
471 case USB_ENDPOINT_XFER_INT:
472 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
473 goto done;
474 /* real hardware might not handle all packet sizes */
475 switch (dum->gadget.speed) {
476 case USB_SPEED_SUPER:
477 case USB_SPEED_HIGH:
478 if (max <= 1024)
479 break;
480 /* save a return statement */
481 case USB_SPEED_FULL:
482 if (max <= 64)
483 break;
484 /* save a return statement */
485 default:
486 if (max <= 8)
487 break;
488 goto done;
490 break;
491 case USB_ENDPOINT_XFER_ISOC:
492 if (strstr(ep->ep.name, "-bulk")
493 || strstr(ep->ep.name, "-int"))
494 goto done;
495 /* real hardware might not handle all packet sizes */
496 switch (dum->gadget.speed) {
497 case USB_SPEED_SUPER:
498 case USB_SPEED_HIGH:
499 if (max <= 1024)
500 break;
501 /* save a return statement */
502 case USB_SPEED_FULL:
503 if (max <= 1023)
504 break;
505 /* save a return statement */
506 default:
507 goto done;
509 break;
510 default:
511 /* few chips support control except on ep0 */
512 goto done;
515 _ep->maxpacket = max;
516 if (usb_ss_max_streams(_ep->comp_desc)) {
517 if (!usb_endpoint_xfer_bulk(desc)) {
518 dev_err(udc_dev(dum), "Can't enable stream support on "
519 "non-bulk ep %s\n", _ep->name);
520 return -EINVAL;
522 ep->stream_en = 1;
524 ep->desc = desc;
526 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
527 _ep->name,
528 desc->bEndpointAddress & 0x0f,
529 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
530 ({ char *val;
531 switch (usb_endpoint_type(desc)) {
532 case USB_ENDPOINT_XFER_BULK:
533 val = "bulk";
534 break;
535 case USB_ENDPOINT_XFER_ISOC:
536 val = "iso";
537 break;
538 case USB_ENDPOINT_XFER_INT:
539 val = "intr";
540 break;
541 default:
542 val = "ctrl";
543 break;
544 }; val; }),
545 max, ep->stream_en ? "enabled" : "disabled");
547 /* at this point real hardware should be NAKing transfers
548 * to that endpoint, until a buffer is queued to it.
550 ep->halted = ep->wedged = 0;
551 retval = 0;
552 done:
553 return retval;
556 static int dummy_disable(struct usb_ep *_ep)
558 struct dummy_ep *ep;
559 struct dummy *dum;
560 unsigned long flags;
561 int retval;
563 ep = usb_ep_to_dummy_ep(_ep);
564 if (!_ep || !ep->desc || _ep->name == ep0name)
565 return -EINVAL;
566 dum = ep_to_dummy(ep);
568 spin_lock_irqsave(&dum->lock, flags);
569 ep->desc = NULL;
570 ep->stream_en = 0;
571 retval = 0;
572 nuke(dum, ep);
573 spin_unlock_irqrestore(&dum->lock, flags);
575 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
576 return retval;
579 static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
580 gfp_t mem_flags)
582 struct dummy_ep *ep;
583 struct dummy_request *req;
585 if (!_ep)
586 return NULL;
587 ep = usb_ep_to_dummy_ep(_ep);
589 req = kzalloc(sizeof(*req), mem_flags);
590 if (!req)
591 return NULL;
592 INIT_LIST_HEAD(&req->queue);
593 return &req->req;
596 static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
598 struct dummy_ep *ep;
599 struct dummy_request *req;
601 if (!_ep || !_req)
602 return;
603 ep = usb_ep_to_dummy_ep(_ep);
604 if (!ep->desc && _ep->name != ep0name)
605 return;
607 req = usb_request_to_dummy_request(_req);
608 WARN_ON(!list_empty(&req->queue));
609 kfree(req);
612 static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
616 static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
617 gfp_t mem_flags)
619 struct dummy_ep *ep;
620 struct dummy_request *req;
621 struct dummy *dum;
622 struct dummy_hcd *dum_hcd;
623 unsigned long flags;
625 req = usb_request_to_dummy_request(_req);
626 if (!_req || !list_empty(&req->queue) || !_req->complete)
627 return -EINVAL;
629 ep = usb_ep_to_dummy_ep(_ep);
630 if (!_ep || (!ep->desc && _ep->name != ep0name))
631 return -EINVAL;
633 dum = ep_to_dummy(ep);
634 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
635 if (!dum->driver || !is_enabled(dum_hcd))
636 return -ESHUTDOWN;
638 #if 0
639 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
640 ep, _req, _ep->name, _req->length, _req->buf);
641 #endif
642 _req->status = -EINPROGRESS;
643 _req->actual = 0;
644 spin_lock_irqsave(&dum->lock, flags);
646 /* implement an emulated single-request FIFO */
647 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
648 list_empty(&dum->fifo_req.queue) &&
649 list_empty(&ep->queue) &&
650 _req->length <= FIFO_SIZE) {
651 req = &dum->fifo_req;
652 req->req = *_req;
653 req->req.buf = dum->fifo_buf;
654 memcpy(dum->fifo_buf, _req->buf, _req->length);
655 req->req.context = dum;
656 req->req.complete = fifo_complete;
658 list_add_tail(&req->queue, &ep->queue);
659 spin_unlock(&dum->lock);
660 _req->actual = _req->length;
661 _req->status = 0;
662 _req->complete(_ep, _req);
663 spin_lock(&dum->lock);
664 } else
665 list_add_tail(&req->queue, &ep->queue);
666 spin_unlock_irqrestore(&dum->lock, flags);
668 /* real hardware would likely enable transfers here, in case
669 * it'd been left NAKing.
671 return 0;
674 static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
676 struct dummy_ep *ep;
677 struct dummy *dum;
678 int retval = -EINVAL;
679 unsigned long flags;
680 struct dummy_request *req = NULL;
682 if (!_ep || !_req)
683 return retval;
684 ep = usb_ep_to_dummy_ep(_ep);
685 dum = ep_to_dummy(ep);
687 if (!dum->driver)
688 return -ESHUTDOWN;
690 local_irq_save(flags);
691 spin_lock(&dum->lock);
692 list_for_each_entry(req, &ep->queue, queue) {
693 if (&req->req == _req) {
694 list_del_init(&req->queue);
695 _req->status = -ECONNRESET;
696 retval = 0;
697 break;
700 spin_unlock(&dum->lock);
702 if (retval == 0) {
703 dev_dbg(udc_dev(dum),
704 "dequeued req %p from %s, len %d buf %p\n",
705 req, _ep->name, _req->length, _req->buf);
706 _req->complete(_ep, _req);
708 local_irq_restore(flags);
709 return retval;
712 static int
713 dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
715 struct dummy_ep *ep;
716 struct dummy *dum;
718 if (!_ep)
719 return -EINVAL;
720 ep = usb_ep_to_dummy_ep(_ep);
721 dum = ep_to_dummy(ep);
722 if (!dum->driver)
723 return -ESHUTDOWN;
724 if (!value)
725 ep->halted = ep->wedged = 0;
726 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
727 !list_empty(&ep->queue))
728 return -EAGAIN;
729 else {
730 ep->halted = 1;
731 if (wedged)
732 ep->wedged = 1;
734 /* FIXME clear emulated data toggle too */
735 return 0;
738 static int
739 dummy_set_halt(struct usb_ep *_ep, int value)
741 return dummy_set_halt_and_wedge(_ep, value, 0);
744 static int dummy_set_wedge(struct usb_ep *_ep)
746 if (!_ep || _ep->name == ep0name)
747 return -EINVAL;
748 return dummy_set_halt_and_wedge(_ep, 1, 1);
751 static const struct usb_ep_ops dummy_ep_ops = {
752 .enable = dummy_enable,
753 .disable = dummy_disable,
755 .alloc_request = dummy_alloc_request,
756 .free_request = dummy_free_request,
758 .queue = dummy_queue,
759 .dequeue = dummy_dequeue,
761 .set_halt = dummy_set_halt,
762 .set_wedge = dummy_set_wedge,
765 /*-------------------------------------------------------------------------*/
767 /* there are both host and device side versions of this call ... */
768 static int dummy_g_get_frame(struct usb_gadget *_gadget)
770 struct timeval tv;
772 do_gettimeofday(&tv);
773 return tv.tv_usec / 1000;
776 static int dummy_wakeup(struct usb_gadget *_gadget)
778 struct dummy_hcd *dum_hcd;
780 dum_hcd = gadget_to_dummy_hcd(_gadget);
781 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
782 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
783 return -EINVAL;
784 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
785 return -ENOLINK;
786 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
787 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
788 return -EIO;
790 /* FIXME: What if the root hub is suspended but the port isn't? */
792 /* hub notices our request, issues downstream resume, etc */
793 dum_hcd->resuming = 1;
794 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
795 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
796 return 0;
799 static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
801 struct dummy *dum;
803 dum = gadget_to_dummy_hcd(_gadget)->dum;
804 if (value)
805 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
806 else
807 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
808 return 0;
811 static void dummy_udc_update_ep0(struct dummy *dum)
813 if (dum->gadget.speed == USB_SPEED_SUPER)
814 dum->ep[0].ep.maxpacket = 9;
815 else
816 dum->ep[0].ep.maxpacket = 64;
819 static int dummy_pullup(struct usb_gadget *_gadget, int value)
821 struct dummy_hcd *dum_hcd;
822 struct dummy *dum;
823 unsigned long flags;
825 dum = gadget_dev_to_dummy(&_gadget->dev);
827 if (value && dum->driver) {
828 if (mod_data.is_super_speed)
829 dum->gadget.speed = dum->driver->max_speed;
830 else if (mod_data.is_high_speed)
831 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
832 dum->driver->max_speed);
833 else
834 dum->gadget.speed = USB_SPEED_FULL;
835 dummy_udc_update_ep0(dum);
837 if (dum->gadget.speed < dum->driver->max_speed)
838 dev_dbg(udc_dev(dum), "This device can perform faster"
839 " if you connect it to a %s port...\n",
840 usb_speed_string(dum->driver->max_speed));
842 dum_hcd = gadget_to_dummy_hcd(_gadget);
844 spin_lock_irqsave(&dum->lock, flags);
845 dum->pullup = (value != 0);
846 set_link_state(dum_hcd);
847 spin_unlock_irqrestore(&dum->lock, flags);
849 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
850 return 0;
853 static int dummy_udc_start(struct usb_gadget *g,
854 struct usb_gadget_driver *driver);
855 static int dummy_udc_stop(struct usb_gadget *g,
856 struct usb_gadget_driver *driver);
858 static const struct usb_gadget_ops dummy_ops = {
859 .get_frame = dummy_g_get_frame,
860 .wakeup = dummy_wakeup,
861 .set_selfpowered = dummy_set_selfpowered,
862 .pullup = dummy_pullup,
863 .udc_start = dummy_udc_start,
864 .udc_stop = dummy_udc_stop,
867 /*-------------------------------------------------------------------------*/
869 /* "function" sysfs attribute */
870 static ssize_t show_function(struct device *dev, struct device_attribute *attr,
871 char *buf)
873 struct dummy *dum = gadget_dev_to_dummy(dev);
875 if (!dum->driver || !dum->driver->function)
876 return 0;
877 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
879 static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
881 /*-------------------------------------------------------------------------*/
884 * Driver registration/unregistration.
886 * This is basically hardware-specific; there's usually only one real USB
887 * device (not host) controller since that's how USB devices are intended
888 * to work. So most implementations of these api calls will rely on the
889 * fact that only one driver will ever bind to the hardware. But curious
890 * hardware can be built with discrete components, so the gadget API doesn't
891 * require that assumption.
893 * For this emulator, it might be convenient to create a usb slave device
894 * for each driver that registers: just add to a big root hub.
897 static int dummy_udc_start(struct usb_gadget *g,
898 struct usb_gadget_driver *driver)
900 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
901 struct dummy *dum = dum_hcd->dum;
903 if (driver->max_speed == USB_SPEED_UNKNOWN)
904 return -EINVAL;
907 * SLAVE side init ... the layer above hardware, which
908 * can't enumerate without help from the driver we're binding.
911 dum->devstatus = 0;
913 dum->driver = driver;
914 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
915 driver->driver.name);
916 return 0;
919 static int dummy_udc_stop(struct usb_gadget *g,
920 struct usb_gadget_driver *driver)
922 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
923 struct dummy *dum = dum_hcd->dum;
925 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
926 driver->driver.name);
928 dum->driver = NULL;
930 dummy_pullup(&dum->gadget, 0);
931 return 0;
934 #undef is_enabled
936 /* The gadget structure is stored inside the hcd structure and will be
937 * released along with it. */
938 static void dummy_gadget_release(struct device *dev)
940 return;
943 static void init_dummy_udc_hw(struct dummy *dum)
945 int i;
947 INIT_LIST_HEAD(&dum->gadget.ep_list);
948 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
949 struct dummy_ep *ep = &dum->ep[i];
951 if (!ep_name[i])
952 break;
953 ep->ep.name = ep_name[i];
954 ep->ep.ops = &dummy_ep_ops;
955 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
956 ep->halted = ep->wedged = ep->already_seen =
957 ep->setup_stage = 0;
958 ep->ep.maxpacket = ~0;
959 ep->ep.max_streams = 16;
960 ep->last_io = jiffies;
961 ep->gadget = &dum->gadget;
962 ep->desc = NULL;
963 INIT_LIST_HEAD(&ep->queue);
966 dum->gadget.ep0 = &dum->ep[0].ep;
967 list_del_init(&dum->ep[0].ep.ep_list);
968 INIT_LIST_HEAD(&dum->fifo_req.queue);
970 #ifdef CONFIG_USB_OTG
971 dum->gadget.is_otg = 1;
972 #endif
975 static int dummy_udc_probe(struct platform_device *pdev)
977 struct dummy *dum = &the_controller;
978 int rc;
980 dum->gadget.name = gadget_name;
981 dum->gadget.ops = &dummy_ops;
982 dum->gadget.max_speed = USB_SPEED_SUPER;
984 dev_set_name(&dum->gadget.dev, "gadget");
985 dum->gadget.dev.parent = &pdev->dev;
986 dum->gadget.dev.release = dummy_gadget_release;
987 rc = device_register(&dum->gadget.dev);
988 if (rc < 0) {
989 put_device(&dum->gadget.dev);
990 return rc;
993 init_dummy_udc_hw(dum);
995 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
996 if (rc < 0)
997 goto err_udc;
999 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
1000 if (rc < 0)
1001 goto err_dev;
1002 platform_set_drvdata(pdev, dum);
1003 return rc;
1005 err_dev:
1006 usb_del_gadget_udc(&dum->gadget);
1007 err_udc:
1008 device_unregister(&dum->gadget.dev);
1009 return rc;
1012 static int dummy_udc_remove(struct platform_device *pdev)
1014 struct dummy *dum = platform_get_drvdata(pdev);
1016 usb_del_gadget_udc(&dum->gadget);
1017 platform_set_drvdata(pdev, NULL);
1018 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1019 device_unregister(&dum->gadget.dev);
1020 return 0;
1023 static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1024 int suspend)
1026 spin_lock_irq(&dum->lock);
1027 dum->udc_suspended = suspend;
1028 set_link_state(dum_hcd);
1029 spin_unlock_irq(&dum->lock);
1032 static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1034 struct dummy *dum = platform_get_drvdata(pdev);
1035 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1037 dev_dbg(&pdev->dev, "%s\n", __func__);
1038 dummy_udc_pm(dum, dum_hcd, 1);
1039 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1040 return 0;
1043 static int dummy_udc_resume(struct platform_device *pdev)
1045 struct dummy *dum = platform_get_drvdata(pdev);
1046 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1048 dev_dbg(&pdev->dev, "%s\n", __func__);
1049 dummy_udc_pm(dum, dum_hcd, 0);
1050 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1051 return 0;
1054 static struct platform_driver dummy_udc_driver = {
1055 .probe = dummy_udc_probe,
1056 .remove = dummy_udc_remove,
1057 .suspend = dummy_udc_suspend,
1058 .resume = dummy_udc_resume,
1059 .driver = {
1060 .name = (char *) gadget_name,
1061 .owner = THIS_MODULE,
1065 /*-------------------------------------------------------------------------*/
1067 static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1069 unsigned int index;
1071 index = usb_endpoint_num(desc) << 1;
1072 if (usb_endpoint_dir_in(desc))
1073 index |= 1;
1074 return index;
1077 /* MASTER/HOST SIDE DRIVER
1079 * this uses the hcd framework to hook up to host side drivers.
1080 * its root hub will only have one device, otherwise it acts like
1081 * a normal host controller.
1083 * when urbs are queued, they're just stuck on a list that we
1084 * scan in a timer callback. that callback connects writes from
1085 * the host with reads from the device, and so on, based on the
1086 * usb 2.0 rules.
1089 static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1091 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1092 u32 index;
1094 if (!usb_endpoint_xfer_bulk(desc))
1095 return 0;
1097 index = dummy_get_ep_idx(desc);
1098 return (1 << index) & dum_hcd->stream_en_ep;
1102 * The max stream number is saved as a nibble so for the 30 possible endpoints
1103 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1104 * means we use only 1 stream). The maximum according to the spec is 16bit so
1105 * if the 16 stream limit is about to go, the array size should be incremented
1106 * to 30 elements of type u16.
1108 static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1109 unsigned int pipe)
1111 int max_streams;
1113 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1114 if (usb_pipeout(pipe))
1115 max_streams >>= 4;
1116 else
1117 max_streams &= 0xf;
1118 max_streams++;
1119 return max_streams;
1122 static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1123 unsigned int pipe, unsigned int streams)
1125 int max_streams;
1127 streams--;
1128 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1129 if (usb_pipeout(pipe)) {
1130 streams <<= 4;
1131 max_streams &= 0xf;
1132 } else {
1133 max_streams &= 0xf0;
1135 max_streams |= streams;
1136 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1139 static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1141 unsigned int max_streams;
1142 int enabled;
1144 enabled = dummy_ep_stream_en(dum_hcd, urb);
1145 if (!urb->stream_id) {
1146 if (enabled)
1147 return -EINVAL;
1148 return 0;
1150 if (!enabled)
1151 return -EINVAL;
1153 max_streams = get_max_streams_for_pipe(dum_hcd,
1154 usb_pipeendpoint(urb->pipe));
1155 if (urb->stream_id > max_streams) {
1156 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1157 urb->stream_id);
1158 BUG();
1159 return -EINVAL;
1161 return 0;
1164 static int dummy_urb_enqueue(
1165 struct usb_hcd *hcd,
1166 struct urb *urb,
1167 gfp_t mem_flags
1169 struct dummy_hcd *dum_hcd;
1170 struct urbp *urbp;
1171 unsigned long flags;
1172 int rc;
1174 urbp = kmalloc(sizeof *urbp, mem_flags);
1175 if (!urbp)
1176 return -ENOMEM;
1177 urbp->urb = urb;
1178 urbp->miter_started = 0;
1180 dum_hcd = hcd_to_dummy_hcd(hcd);
1181 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1183 rc = dummy_validate_stream(dum_hcd, urb);
1184 if (rc) {
1185 kfree(urbp);
1186 goto done;
1189 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1190 if (rc) {
1191 kfree(urbp);
1192 goto done;
1195 if (!dum_hcd->udev) {
1196 dum_hcd->udev = urb->dev;
1197 usb_get_dev(dum_hcd->udev);
1198 } else if (unlikely(dum_hcd->udev != urb->dev))
1199 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1201 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1202 urb->hcpriv = urbp;
1203 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
1204 urb->error_count = 1; /* mark as a new urb */
1206 /* kick the scheduler, it'll do the rest */
1207 if (!timer_pending(&dum_hcd->timer))
1208 mod_timer(&dum_hcd->timer, jiffies + 1);
1210 done:
1211 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1212 return rc;
1215 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1217 struct dummy_hcd *dum_hcd;
1218 unsigned long flags;
1219 int rc;
1221 /* giveback happens automatically in timer callback,
1222 * so make sure the callback happens */
1223 dum_hcd = hcd_to_dummy_hcd(hcd);
1224 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1226 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1227 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1228 !list_empty(&dum_hcd->urbp_list))
1229 mod_timer(&dum_hcd->timer, jiffies);
1231 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1232 return rc;
1235 static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1236 u32 len)
1238 void *ubuf, *rbuf;
1239 struct urbp *urbp = urb->hcpriv;
1240 int to_host;
1241 struct sg_mapping_iter *miter = &urbp->miter;
1242 u32 trans = 0;
1243 u32 this_sg;
1244 bool next_sg;
1246 to_host = usb_pipein(urb->pipe);
1247 rbuf = req->req.buf + req->req.actual;
1249 if (!urb->num_sgs) {
1250 ubuf = urb->transfer_buffer + urb->actual_length;
1251 if (to_host)
1252 memcpy(ubuf, rbuf, len);
1253 else
1254 memcpy(rbuf, ubuf, len);
1255 return len;
1258 if (!urbp->miter_started) {
1259 u32 flags = SG_MITER_ATOMIC;
1261 if (to_host)
1262 flags |= SG_MITER_TO_SG;
1263 else
1264 flags |= SG_MITER_FROM_SG;
1266 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1267 urbp->miter_started = 1;
1269 next_sg = sg_miter_next(miter);
1270 if (next_sg == false) {
1271 WARN_ON_ONCE(1);
1272 return -EINVAL;
1274 do {
1275 ubuf = miter->addr;
1276 this_sg = min_t(u32, len, miter->length);
1277 miter->consumed = this_sg;
1278 trans += this_sg;
1280 if (to_host)
1281 memcpy(ubuf, rbuf, this_sg);
1282 else
1283 memcpy(rbuf, ubuf, this_sg);
1284 len -= this_sg;
1286 if (!len)
1287 break;
1288 next_sg = sg_miter_next(miter);
1289 if (next_sg == false) {
1290 WARN_ON_ONCE(1);
1291 return -EINVAL;
1294 rbuf += this_sg;
1295 } while (1);
1297 sg_miter_stop(miter);
1298 return trans;
1301 /* transfer up to a frame's worth; caller must own lock */
1302 static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1303 struct dummy_ep *ep, int limit, int *status)
1305 struct dummy *dum = dum_hcd->dum;
1306 struct dummy_request *req;
1308 top:
1309 /* if there's no request queued, the device is NAKing; return */
1310 list_for_each_entry(req, &ep->queue, queue) {
1311 unsigned host_len, dev_len, len;
1312 int is_short, to_host;
1313 int rescan = 0;
1315 if (dummy_ep_stream_en(dum_hcd, urb)) {
1316 if ((urb->stream_id != req->req.stream_id))
1317 continue;
1320 /* 1..N packets of ep->ep.maxpacket each ... the last one
1321 * may be short (including zero length).
1323 * writer can send a zlp explicitly (length 0) or implicitly
1324 * (length mod maxpacket zero, and 'zero' flag); they always
1325 * terminate reads.
1327 host_len = urb->transfer_buffer_length - urb->actual_length;
1328 dev_len = req->req.length - req->req.actual;
1329 len = min(host_len, dev_len);
1331 /* FIXME update emulated data toggle too */
1333 to_host = usb_pipein(urb->pipe);
1334 if (unlikely(len == 0))
1335 is_short = 1;
1336 else {
1337 /* not enough bandwidth left? */
1338 if (limit < ep->ep.maxpacket && limit < len)
1339 break;
1340 len = min_t(unsigned, len, limit);
1341 if (len == 0)
1342 break;
1344 /* use an extra pass for the final short packet */
1345 if (len > ep->ep.maxpacket) {
1346 rescan = 1;
1347 len -= (len % ep->ep.maxpacket);
1349 is_short = (len % ep->ep.maxpacket) != 0;
1351 len = dummy_perform_transfer(urb, req, len);
1353 ep->last_io = jiffies;
1354 if ((int)len < 0) {
1355 req->req.status = len;
1356 } else {
1357 limit -= len;
1358 urb->actual_length += len;
1359 req->req.actual += len;
1363 /* short packets terminate, maybe with overflow/underflow.
1364 * it's only really an error to write too much.
1366 * partially filling a buffer optionally blocks queue advances
1367 * (so completion handlers can clean up the queue) but we don't
1368 * need to emulate such data-in-flight.
1370 if (is_short) {
1371 if (host_len == dev_len) {
1372 req->req.status = 0;
1373 *status = 0;
1374 } else if (to_host) {
1375 req->req.status = 0;
1376 if (dev_len > host_len)
1377 *status = -EOVERFLOW;
1378 else
1379 *status = 0;
1380 } else if (!to_host) {
1381 *status = 0;
1382 if (host_len > dev_len)
1383 req->req.status = -EOVERFLOW;
1384 else
1385 req->req.status = 0;
1388 /* many requests terminate without a short packet */
1389 } else {
1390 if (req->req.length == req->req.actual
1391 && !req->req.zero)
1392 req->req.status = 0;
1393 if (urb->transfer_buffer_length == urb->actual_length
1394 && !(urb->transfer_flags
1395 & URB_ZERO_PACKET))
1396 *status = 0;
1399 /* device side completion --> continuable */
1400 if (req->req.status != -EINPROGRESS) {
1401 list_del_init(&req->queue);
1403 spin_unlock(&dum->lock);
1404 req->req.complete(&ep->ep, &req->req);
1405 spin_lock(&dum->lock);
1407 /* requests might have been unlinked... */
1408 rescan = 1;
1411 /* host side completion --> terminate */
1412 if (*status != -EINPROGRESS)
1413 break;
1415 /* rescan to continue with any other queued i/o */
1416 if (rescan)
1417 goto top;
1419 return limit;
1422 static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
1424 int limit = ep->ep.maxpacket;
1426 if (dum->gadget.speed == USB_SPEED_HIGH) {
1427 int tmp;
1429 /* high bandwidth mode */
1430 tmp = usb_endpoint_maxp(ep->desc);
1431 tmp = (tmp >> 11) & 0x03;
1432 tmp *= 8 /* applies to entire frame */;
1433 limit += limit * tmp;
1435 if (dum->gadget.speed == USB_SPEED_SUPER) {
1436 switch (usb_endpoint_type(ep->desc)) {
1437 case USB_ENDPOINT_XFER_ISOC:
1438 /* Sec. 4.4.8.2 USB3.0 Spec */
1439 limit = 3 * 16 * 1024 * 8;
1440 break;
1441 case USB_ENDPOINT_XFER_INT:
1442 /* Sec. 4.4.7.2 USB3.0 Spec */
1443 limit = 3 * 1024 * 8;
1444 break;
1445 case USB_ENDPOINT_XFER_BULK:
1446 default:
1447 break;
1450 return limit;
1453 #define is_active(dum_hcd) ((dum_hcd->port_status & \
1454 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1455 USB_PORT_STAT_SUSPEND)) \
1456 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1458 static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
1460 int i;
1462 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1463 dum->ss_hcd : dum->hs_hcd)))
1464 return NULL;
1465 if ((address & ~USB_DIR_IN) == 0)
1466 return &dum->ep[0];
1467 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1468 struct dummy_ep *ep = &dum->ep[i];
1470 if (!ep->desc)
1471 continue;
1472 if (ep->desc->bEndpointAddress == address)
1473 return ep;
1475 return NULL;
1478 #undef is_active
1480 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1481 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1482 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1483 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1484 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1485 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1489 * handle_control_request() - handles all control transfers
1490 * @dum: pointer to dummy (the_controller)
1491 * @urb: the urb request to handle
1492 * @setup: pointer to the setup data for a USB device control
1493 * request
1494 * @status: pointer to request handling status
1496 * Return 0 - if the request was handled
1497 * 1 - if the request wasn't handles
1498 * error code on error
1500 static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1501 struct usb_ctrlrequest *setup,
1502 int *status)
1504 struct dummy_ep *ep2;
1505 struct dummy *dum = dum_hcd->dum;
1506 int ret_val = 1;
1507 unsigned w_index;
1508 unsigned w_value;
1510 w_index = le16_to_cpu(setup->wIndex);
1511 w_value = le16_to_cpu(setup->wValue);
1512 switch (setup->bRequest) {
1513 case USB_REQ_SET_ADDRESS:
1514 if (setup->bRequestType != Dev_Request)
1515 break;
1516 dum->address = w_value;
1517 *status = 0;
1518 dev_dbg(udc_dev(dum), "set_address = %d\n",
1519 w_value);
1520 ret_val = 0;
1521 break;
1522 case USB_REQ_SET_FEATURE:
1523 if (setup->bRequestType == Dev_Request) {
1524 ret_val = 0;
1525 switch (w_value) {
1526 case USB_DEVICE_REMOTE_WAKEUP:
1527 break;
1528 case USB_DEVICE_B_HNP_ENABLE:
1529 dum->gadget.b_hnp_enable = 1;
1530 break;
1531 case USB_DEVICE_A_HNP_SUPPORT:
1532 dum->gadget.a_hnp_support = 1;
1533 break;
1534 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1535 dum->gadget.a_alt_hnp_support = 1;
1536 break;
1537 case USB_DEVICE_U1_ENABLE:
1538 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1539 HCD_USB3)
1540 w_value = USB_DEV_STAT_U1_ENABLED;
1541 else
1542 ret_val = -EOPNOTSUPP;
1543 break;
1544 case USB_DEVICE_U2_ENABLE:
1545 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1546 HCD_USB3)
1547 w_value = USB_DEV_STAT_U2_ENABLED;
1548 else
1549 ret_val = -EOPNOTSUPP;
1550 break;
1551 case USB_DEVICE_LTM_ENABLE:
1552 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1553 HCD_USB3)
1554 w_value = USB_DEV_STAT_LTM_ENABLED;
1555 else
1556 ret_val = -EOPNOTSUPP;
1557 break;
1558 default:
1559 ret_val = -EOPNOTSUPP;
1561 if (ret_val == 0) {
1562 dum->devstatus |= (1 << w_value);
1563 *status = 0;
1565 } else if (setup->bRequestType == Ep_Request) {
1566 /* endpoint halt */
1567 ep2 = find_endpoint(dum, w_index);
1568 if (!ep2 || ep2->ep.name == ep0name) {
1569 ret_val = -EOPNOTSUPP;
1570 break;
1572 ep2->halted = 1;
1573 ret_val = 0;
1574 *status = 0;
1576 break;
1577 case USB_REQ_CLEAR_FEATURE:
1578 if (setup->bRequestType == Dev_Request) {
1579 ret_val = 0;
1580 switch (w_value) {
1581 case USB_DEVICE_REMOTE_WAKEUP:
1582 w_value = USB_DEVICE_REMOTE_WAKEUP;
1583 break;
1584 case USB_DEVICE_U1_ENABLE:
1585 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1586 HCD_USB3)
1587 w_value = USB_DEV_STAT_U1_ENABLED;
1588 else
1589 ret_val = -EOPNOTSUPP;
1590 break;
1591 case USB_DEVICE_U2_ENABLE:
1592 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1593 HCD_USB3)
1594 w_value = USB_DEV_STAT_U2_ENABLED;
1595 else
1596 ret_val = -EOPNOTSUPP;
1597 break;
1598 case USB_DEVICE_LTM_ENABLE:
1599 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1600 HCD_USB3)
1601 w_value = USB_DEV_STAT_LTM_ENABLED;
1602 else
1603 ret_val = -EOPNOTSUPP;
1604 break;
1605 default:
1606 ret_val = -EOPNOTSUPP;
1607 break;
1609 if (ret_val == 0) {
1610 dum->devstatus &= ~(1 << w_value);
1611 *status = 0;
1613 } else if (setup->bRequestType == Ep_Request) {
1614 /* endpoint halt */
1615 ep2 = find_endpoint(dum, w_index);
1616 if (!ep2) {
1617 ret_val = -EOPNOTSUPP;
1618 break;
1620 if (!ep2->wedged)
1621 ep2->halted = 0;
1622 ret_val = 0;
1623 *status = 0;
1625 break;
1626 case USB_REQ_GET_STATUS:
1627 if (setup->bRequestType == Dev_InRequest
1628 || setup->bRequestType == Intf_InRequest
1629 || setup->bRequestType == Ep_InRequest) {
1630 char *buf;
1632 * device: remote wakeup, selfpowered
1633 * interface: nothing
1634 * endpoint: halt
1636 buf = (char *)urb->transfer_buffer;
1637 if (urb->transfer_buffer_length > 0) {
1638 if (setup->bRequestType == Ep_InRequest) {
1639 ep2 = find_endpoint(dum, w_index);
1640 if (!ep2) {
1641 ret_val = -EOPNOTSUPP;
1642 break;
1644 buf[0] = ep2->halted;
1645 } else if (setup->bRequestType ==
1646 Dev_InRequest) {
1647 buf[0] = (u8)dum->devstatus;
1648 } else
1649 buf[0] = 0;
1651 if (urb->transfer_buffer_length > 1)
1652 buf[1] = 0;
1653 urb->actual_length = min_t(u32, 2,
1654 urb->transfer_buffer_length);
1655 ret_val = 0;
1656 *status = 0;
1658 break;
1660 return ret_val;
1663 /* drive both sides of the transfers; looks like irq handlers to
1664 * both drivers except the callbacks aren't in_irq().
1666 static void dummy_timer(unsigned long _dum_hcd)
1668 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1669 struct dummy *dum = dum_hcd->dum;
1670 struct urbp *urbp, *tmp;
1671 unsigned long flags;
1672 int limit, total;
1673 int i;
1675 /* simplistic model for one frame's bandwidth */
1676 switch (dum->gadget.speed) {
1677 case USB_SPEED_LOW:
1678 total = 8/*bytes*/ * 12/*packets*/;
1679 break;
1680 case USB_SPEED_FULL:
1681 total = 64/*bytes*/ * 19/*packets*/;
1682 break;
1683 case USB_SPEED_HIGH:
1684 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1685 break;
1686 case USB_SPEED_SUPER:
1687 /* Bus speed is 500000 bytes/ms, so use a little less */
1688 total = 490000;
1689 break;
1690 default:
1691 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1692 return;
1695 /* FIXME if HZ != 1000 this will probably misbehave ... */
1697 /* look at each urb queued by the host side driver */
1698 spin_lock_irqsave(&dum->lock, flags);
1700 if (!dum_hcd->udev) {
1701 dev_err(dummy_dev(dum_hcd),
1702 "timer fired with no URBs pending?\n");
1703 spin_unlock_irqrestore(&dum->lock, flags);
1704 return;
1707 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1708 if (!ep_name[i])
1709 break;
1710 dum->ep[i].already_seen = 0;
1713 restart:
1714 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1715 struct urb *urb;
1716 struct dummy_request *req;
1717 u8 address;
1718 struct dummy_ep *ep = NULL;
1719 int type;
1720 int status = -EINPROGRESS;
1722 urb = urbp->urb;
1723 if (urb->unlinked)
1724 goto return_urb;
1725 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1726 continue;
1727 type = usb_pipetype(urb->pipe);
1729 /* used up this frame's non-periodic bandwidth?
1730 * FIXME there's infinite bandwidth for control and
1731 * periodic transfers ... unrealistic.
1733 if (total <= 0 && type == PIPE_BULK)
1734 continue;
1736 /* find the gadget's ep for this request (if configured) */
1737 address = usb_pipeendpoint (urb->pipe);
1738 if (usb_pipein(urb->pipe))
1739 address |= USB_DIR_IN;
1740 ep = find_endpoint(dum, address);
1741 if (!ep) {
1742 /* set_configuration() disagreement */
1743 dev_dbg(dummy_dev(dum_hcd),
1744 "no ep configured for urb %p\n",
1745 urb);
1746 status = -EPROTO;
1747 goto return_urb;
1750 if (ep->already_seen)
1751 continue;
1752 ep->already_seen = 1;
1753 if (ep == &dum->ep[0] && urb->error_count) {
1754 ep->setup_stage = 1; /* a new urb */
1755 urb->error_count = 0;
1757 if (ep->halted && !ep->setup_stage) {
1758 /* NOTE: must not be iso! */
1759 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1760 ep->ep.name, urb);
1761 status = -EPIPE;
1762 goto return_urb;
1764 /* FIXME make sure both ends agree on maxpacket */
1766 /* handle control requests */
1767 if (ep == &dum->ep[0] && ep->setup_stage) {
1768 struct usb_ctrlrequest setup;
1769 int value = 1;
1771 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
1772 /* paranoia, in case of stale queued data */
1773 list_for_each_entry(req, &ep->queue, queue) {
1774 list_del_init(&req->queue);
1775 req->req.status = -EOVERFLOW;
1776 dev_dbg(udc_dev(dum), "stale req = %p\n",
1777 req);
1779 spin_unlock(&dum->lock);
1780 req->req.complete(&ep->ep, &req->req);
1781 spin_lock(&dum->lock);
1782 ep->already_seen = 0;
1783 goto restart;
1786 /* gadget driver never sees set_address or operations
1787 * on standard feature flags. some hardware doesn't
1788 * even expose them.
1790 ep->last_io = jiffies;
1791 ep->setup_stage = 0;
1792 ep->halted = 0;
1794 value = handle_control_request(dum_hcd, urb, &setup,
1795 &status);
1797 /* gadget driver handles all other requests. block
1798 * until setup() returns; no reentrancy issues etc.
1800 if (value > 0) {
1801 spin_unlock(&dum->lock);
1802 value = dum->driver->setup(&dum->gadget,
1803 &setup);
1804 spin_lock(&dum->lock);
1806 if (value >= 0) {
1807 /* no delays (max 64KB data stage) */
1808 limit = 64*1024;
1809 goto treat_control_like_bulk;
1811 /* error, see below */
1814 if (value < 0) {
1815 if (value != -EOPNOTSUPP)
1816 dev_dbg(udc_dev(dum),
1817 "setup --> %d\n",
1818 value);
1819 status = -EPIPE;
1820 urb->actual_length = 0;
1823 goto return_urb;
1826 /* non-control requests */
1827 limit = total;
1828 switch (usb_pipetype(urb->pipe)) {
1829 case PIPE_ISOCHRONOUS:
1830 /* FIXME is it urb->interval since the last xfer?
1831 * use urb->iso_frame_desc[i].
1832 * complete whether or not ep has requests queued.
1833 * report random errors, to debug drivers.
1835 limit = max(limit, periodic_bytes(dum, ep));
1836 status = -ENOSYS;
1837 break;
1839 case PIPE_INTERRUPT:
1840 /* FIXME is it urb->interval since the last xfer?
1841 * this almost certainly polls too fast.
1843 limit = max(limit, periodic_bytes(dum, ep));
1844 /* FALLTHROUGH */
1846 default:
1847 treat_control_like_bulk:
1848 ep->last_io = jiffies;
1849 total = transfer(dum_hcd, urb, ep, limit, &status);
1850 break;
1853 /* incomplete transfer? */
1854 if (status == -EINPROGRESS)
1855 continue;
1857 return_urb:
1858 list_del(&urbp->urbp_list);
1859 kfree(urbp);
1860 if (ep)
1861 ep->already_seen = ep->setup_stage = 0;
1863 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1864 spin_unlock(&dum->lock);
1865 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1866 spin_lock(&dum->lock);
1868 goto restart;
1871 if (list_empty(&dum_hcd->urbp_list)) {
1872 usb_put_dev(dum_hcd->udev);
1873 dum_hcd->udev = NULL;
1874 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1875 /* want a 1 msec delay here */
1876 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1879 spin_unlock_irqrestore(&dum->lock, flags);
1882 /*-------------------------------------------------------------------------*/
1884 #define PORT_C_MASK \
1885 ((USB_PORT_STAT_C_CONNECTION \
1886 | USB_PORT_STAT_C_ENABLE \
1887 | USB_PORT_STAT_C_SUSPEND \
1888 | USB_PORT_STAT_C_OVERCURRENT \
1889 | USB_PORT_STAT_C_RESET) << 16)
1891 static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
1893 struct dummy_hcd *dum_hcd;
1894 unsigned long flags;
1895 int retval = 0;
1897 dum_hcd = hcd_to_dummy_hcd(hcd);
1899 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1900 if (!HCD_HW_ACCESSIBLE(hcd))
1901 goto done;
1903 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1904 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1905 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1906 set_link_state(dum_hcd);
1909 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1910 *buf = (1 << 1);
1911 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1912 dum_hcd->port_status);
1913 retval = 1;
1914 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1915 usb_hcd_resume_root_hub(hcd);
1917 done:
1918 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1919 return retval;
1922 static inline void
1923 ss_hub_descriptor(struct usb_hub_descriptor *desc)
1925 memset(desc, 0, sizeof *desc);
1926 desc->bDescriptorType = 0x2a;
1927 desc->bDescLength = 12;
1928 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1929 desc->bNbrPorts = 1;
1930 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1931 desc->u.ss.DeviceRemovable = 0xffff;
1934 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
1936 memset(desc, 0, sizeof *desc);
1937 desc->bDescriptorType = 0x29;
1938 desc->bDescLength = 9;
1939 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1940 desc->bNbrPorts = 1;
1941 desc->u.hs.DeviceRemovable[0] = 0xff;
1942 desc->u.hs.DeviceRemovable[1] = 0xff;
1945 static int dummy_hub_control(
1946 struct usb_hcd *hcd,
1947 u16 typeReq,
1948 u16 wValue,
1949 u16 wIndex,
1950 char *buf,
1951 u16 wLength
1953 struct dummy_hcd *dum_hcd;
1954 int retval = 0;
1955 unsigned long flags;
1957 if (!HCD_HW_ACCESSIBLE(hcd))
1958 return -ETIMEDOUT;
1960 dum_hcd = hcd_to_dummy_hcd(hcd);
1962 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1963 switch (typeReq) {
1964 case ClearHubFeature:
1965 break;
1966 case ClearPortFeature:
1967 switch (wValue) {
1968 case USB_PORT_FEAT_SUSPEND:
1969 if (hcd->speed == HCD_USB3) {
1970 dev_dbg(dummy_dev(dum_hcd),
1971 "USB_PORT_FEAT_SUSPEND req not "
1972 "supported for USB 3.0 roothub\n");
1973 goto error;
1975 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1976 /* 20msec resume signaling */
1977 dum_hcd->resuming = 1;
1978 dum_hcd->re_timeout = jiffies +
1979 msecs_to_jiffies(20);
1981 break;
1982 case USB_PORT_FEAT_POWER:
1983 if (hcd->speed == HCD_USB3) {
1984 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1985 dev_dbg(dummy_dev(dum_hcd),
1986 "power-off\n");
1987 } else
1988 if (dum_hcd->port_status &
1989 USB_SS_PORT_STAT_POWER)
1990 dev_dbg(dummy_dev(dum_hcd),
1991 "power-off\n");
1992 /* FALLS THROUGH */
1993 default:
1994 dum_hcd->port_status &= ~(1 << wValue);
1995 set_link_state(dum_hcd);
1997 break;
1998 case GetHubDescriptor:
1999 if (hcd->speed == HCD_USB3 &&
2000 (wLength < USB_DT_SS_HUB_SIZE ||
2001 wValue != (USB_DT_SS_HUB << 8))) {
2002 dev_dbg(dummy_dev(dum_hcd),
2003 "Wrong hub descriptor type for "
2004 "USB 3.0 roothub.\n");
2005 goto error;
2007 if (hcd->speed == HCD_USB3)
2008 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2009 else
2010 hub_descriptor((struct usb_hub_descriptor *) buf);
2011 break;
2012 case GetHubStatus:
2013 *(__le32 *) buf = cpu_to_le32(0);
2014 break;
2015 case GetPortStatus:
2016 if (wIndex != 1)
2017 retval = -EPIPE;
2019 /* whoever resets or resumes must GetPortStatus to
2020 * complete it!!
2022 if (dum_hcd->resuming &&
2023 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2024 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2025 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
2027 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2028 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2029 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2030 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2031 if (dum_hcd->dum->pullup) {
2032 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
2034 if (hcd->speed < HCD_USB3) {
2035 switch (dum_hcd->dum->gadget.speed) {
2036 case USB_SPEED_HIGH:
2037 dum_hcd->port_status |=
2038 USB_PORT_STAT_HIGH_SPEED;
2039 break;
2040 case USB_SPEED_LOW:
2041 dum_hcd->dum->gadget.ep0->
2042 maxpacket = 8;
2043 dum_hcd->port_status |=
2044 USB_PORT_STAT_LOW_SPEED;
2045 break;
2046 default:
2047 dum_hcd->dum->gadget.speed =
2048 USB_SPEED_FULL;
2049 break;
2054 set_link_state(dum_hcd);
2055 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2056 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
2057 break;
2058 case SetHubFeature:
2059 retval = -EPIPE;
2060 break;
2061 case SetPortFeature:
2062 switch (wValue) {
2063 case USB_PORT_FEAT_LINK_STATE:
2064 if (hcd->speed != HCD_USB3) {
2065 dev_dbg(dummy_dev(dum_hcd),
2066 "USB_PORT_FEAT_LINK_STATE req not "
2067 "supported for USB 2.0 roothub\n");
2068 goto error;
2071 * Since this is dummy we don't have an actual link so
2072 * there is nothing to do for the SET_LINK_STATE cmd
2074 break;
2075 case USB_PORT_FEAT_U1_TIMEOUT:
2076 case USB_PORT_FEAT_U2_TIMEOUT:
2077 /* TODO: add suspend/resume support! */
2078 if (hcd->speed != HCD_USB3) {
2079 dev_dbg(dummy_dev(dum_hcd),
2080 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2081 "supported for USB 2.0 roothub\n");
2082 goto error;
2084 break;
2085 case USB_PORT_FEAT_SUSPEND:
2086 /* Applicable only for USB2.0 hub */
2087 if (hcd->speed == HCD_USB3) {
2088 dev_dbg(dummy_dev(dum_hcd),
2089 "USB_PORT_FEAT_SUSPEND req not "
2090 "supported for USB 3.0 roothub\n");
2091 goto error;
2093 if (dum_hcd->active) {
2094 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
2096 /* HNP would happen here; for now we
2097 * assume b_bus_req is always true.
2099 set_link_state(dum_hcd);
2100 if (((1 << USB_DEVICE_B_HNP_ENABLE)
2101 & dum_hcd->dum->devstatus) != 0)
2102 dev_dbg(dummy_dev(dum_hcd),
2103 "no HNP yet!\n");
2105 break;
2106 case USB_PORT_FEAT_POWER:
2107 if (hcd->speed == HCD_USB3)
2108 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2109 else
2110 dum_hcd->port_status |= USB_PORT_STAT_POWER;
2111 set_link_state(dum_hcd);
2112 break;
2113 case USB_PORT_FEAT_BH_PORT_RESET:
2114 /* Applicable only for USB3.0 hub */
2115 if (hcd->speed != HCD_USB3) {
2116 dev_dbg(dummy_dev(dum_hcd),
2117 "USB_PORT_FEAT_BH_PORT_RESET req not "
2118 "supported for USB 2.0 roothub\n");
2119 goto error;
2121 /* FALLS THROUGH */
2122 case USB_PORT_FEAT_RESET:
2123 /* if it's already enabled, disable */
2124 if (hcd->speed == HCD_USB3) {
2125 dum_hcd->port_status = 0;
2126 dum_hcd->port_status =
2127 (USB_SS_PORT_STAT_POWER |
2128 USB_PORT_STAT_CONNECTION |
2129 USB_PORT_STAT_RESET);
2130 } else
2131 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
2132 | USB_PORT_STAT_LOW_SPEED
2133 | USB_PORT_STAT_HIGH_SPEED);
2135 * We want to reset device status. All but the
2136 * Self powered feature
2138 dum_hcd->dum->devstatus &=
2139 (1 << USB_DEVICE_SELF_POWERED);
2141 * FIXME USB3.0: what is the correct reset signaling
2142 * interval? Is it still 50msec as for HS?
2144 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2145 /* FALLS THROUGH */
2146 default:
2147 if (hcd->speed == HCD_USB3) {
2148 if ((dum_hcd->port_status &
2149 USB_SS_PORT_STAT_POWER) != 0) {
2150 dum_hcd->port_status |= (1 << wValue);
2151 set_link_state(dum_hcd);
2153 } else
2154 if ((dum_hcd->port_status &
2155 USB_PORT_STAT_POWER) != 0) {
2156 dum_hcd->port_status |= (1 << wValue);
2157 set_link_state(dum_hcd);
2160 break;
2161 case GetPortErrorCount:
2162 if (hcd->speed != HCD_USB3) {
2163 dev_dbg(dummy_dev(dum_hcd),
2164 "GetPortErrorCount req not "
2165 "supported for USB 2.0 roothub\n");
2166 goto error;
2168 /* We'll always return 0 since this is a dummy hub */
2169 *(__le32 *) buf = cpu_to_le32(0);
2170 break;
2171 case SetHubDepth:
2172 if (hcd->speed != HCD_USB3) {
2173 dev_dbg(dummy_dev(dum_hcd),
2174 "SetHubDepth req not supported for "
2175 "USB 2.0 roothub\n");
2176 goto error;
2178 break;
2179 default:
2180 dev_dbg(dummy_dev(dum_hcd),
2181 "hub control req%04x v%04x i%04x l%d\n",
2182 typeReq, wValue, wIndex, wLength);
2183 error:
2184 /* "protocol stall" on error */
2185 retval = -EPIPE;
2187 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2189 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2190 usb_hcd_poll_rh_status(hcd);
2191 return retval;
2194 static int dummy_bus_suspend(struct usb_hcd *hcd)
2196 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2198 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2200 spin_lock_irq(&dum_hcd->dum->lock);
2201 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2202 set_link_state(dum_hcd);
2203 hcd->state = HC_STATE_SUSPENDED;
2204 spin_unlock_irq(&dum_hcd->dum->lock);
2205 return 0;
2208 static int dummy_bus_resume(struct usb_hcd *hcd)
2210 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2211 int rc = 0;
2213 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2215 spin_lock_irq(&dum_hcd->dum->lock);
2216 if (!HCD_HW_ACCESSIBLE(hcd)) {
2217 rc = -ESHUTDOWN;
2218 } else {
2219 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2220 set_link_state(dum_hcd);
2221 if (!list_empty(&dum_hcd->urbp_list))
2222 mod_timer(&dum_hcd->timer, jiffies);
2223 hcd->state = HC_STATE_RUNNING;
2225 spin_unlock_irq(&dum_hcd->dum->lock);
2226 return rc;
2229 /*-------------------------------------------------------------------------*/
2231 static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
2233 int ep = usb_pipeendpoint(urb->pipe);
2235 return snprintf(buf, size,
2236 "urb/%p %s ep%d%s%s len %d/%d\n",
2237 urb,
2238 ({ char *s;
2239 switch (urb->dev->speed) {
2240 case USB_SPEED_LOW:
2241 s = "ls";
2242 break;
2243 case USB_SPEED_FULL:
2244 s = "fs";
2245 break;
2246 case USB_SPEED_HIGH:
2247 s = "hs";
2248 break;
2249 case USB_SPEED_SUPER:
2250 s = "ss";
2251 break;
2252 default:
2253 s = "?";
2254 break;
2255 }; s; }),
2256 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
2257 ({ char *s; \
2258 switch (usb_pipetype(urb->pipe)) { \
2259 case PIPE_CONTROL: \
2260 s = ""; \
2261 break; \
2262 case PIPE_BULK: \
2263 s = "-bulk"; \
2264 break; \
2265 case PIPE_INTERRUPT: \
2266 s = "-int"; \
2267 break; \
2268 default: \
2269 s = "-iso"; \
2270 break; \
2271 }; s; }),
2272 urb->actual_length, urb->transfer_buffer_length);
2275 static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2276 char *buf)
2278 struct usb_hcd *hcd = dev_get_drvdata(dev);
2279 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2280 struct urbp *urbp;
2281 size_t size = 0;
2282 unsigned long flags;
2284 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2285 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2286 size_t temp;
2288 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
2289 buf += temp;
2290 size += temp;
2292 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2294 return size;
2296 static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
2298 static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2300 init_timer(&dum_hcd->timer);
2301 dum_hcd->timer.function = dummy_timer;
2302 dum_hcd->timer.data = (unsigned long)dum_hcd;
2303 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2304 dum_hcd->stream_en_ep = 0;
2305 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2306 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2307 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2308 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2309 #ifdef CONFIG_USB_OTG
2310 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2311 #endif
2312 return 0;
2314 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2315 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2318 static int dummy_start(struct usb_hcd *hcd)
2320 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2323 * MASTER side init ... we emulate a root hub that'll only ever
2324 * talk to one device (the slave side). Also appears in sysfs,
2325 * just like more familiar pci-based HCDs.
2327 if (!usb_hcd_is_primary_hcd(hcd))
2328 return dummy_start_ss(dum_hcd);
2330 spin_lock_init(&dum_hcd->dum->lock);
2331 init_timer(&dum_hcd->timer);
2332 dum_hcd->timer.function = dummy_timer;
2333 dum_hcd->timer.data = (unsigned long)dum_hcd;
2334 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2336 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2338 hcd->power_budget = POWER_BUDGET;
2339 hcd->state = HC_STATE_RUNNING;
2340 hcd->uses_new_polling = 1;
2342 #ifdef CONFIG_USB_OTG
2343 hcd->self.otg_port = 1;
2344 #endif
2346 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2347 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2350 static void dummy_stop(struct usb_hcd *hcd)
2352 struct dummy *dum;
2354 dum = hcd_to_dummy_hcd(hcd)->dum;
2355 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2356 usb_gadget_unregister_driver(dum->driver);
2357 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2360 /*-------------------------------------------------------------------------*/
2362 static int dummy_h_get_frame(struct usb_hcd *hcd)
2364 return dummy_g_get_frame(NULL);
2367 static int dummy_setup(struct usb_hcd *hcd)
2369 hcd->self.sg_tablesize = ~0;
2370 if (usb_hcd_is_primary_hcd(hcd)) {
2371 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2372 the_controller.hs_hcd->dum = &the_controller;
2374 * Mark the first roothub as being USB 2.0.
2375 * The USB 3.0 roothub will be registered later by
2376 * dummy_hcd_probe()
2378 hcd->speed = HCD_USB2;
2379 hcd->self.root_hub->speed = USB_SPEED_HIGH;
2380 } else {
2381 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2382 the_controller.ss_hcd->dum = &the_controller;
2383 hcd->speed = HCD_USB3;
2384 hcd->self.root_hub->speed = USB_SPEED_SUPER;
2386 return 0;
2389 /* Change a group of bulk endpoints to support multiple stream IDs */
2390 static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2391 struct usb_host_endpoint **eps, unsigned int num_eps,
2392 unsigned int num_streams, gfp_t mem_flags)
2394 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2395 unsigned long flags;
2396 int max_stream;
2397 int ret_streams = num_streams;
2398 unsigned int index;
2399 unsigned int i;
2401 if (!num_eps)
2402 return -EINVAL;
2404 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2405 for (i = 0; i < num_eps; i++) {
2406 index = dummy_get_ep_idx(&eps[i]->desc);
2407 if ((1 << index) & dum_hcd->stream_en_ep) {
2408 ret_streams = -EINVAL;
2409 goto out;
2411 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2412 if (!max_stream) {
2413 ret_streams = -EINVAL;
2414 goto out;
2416 if (max_stream < ret_streams) {
2417 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2418 "stream IDs.\n",
2419 eps[i]->desc.bEndpointAddress,
2420 max_stream);
2421 ret_streams = max_stream;
2425 for (i = 0; i < num_eps; i++) {
2426 index = dummy_get_ep_idx(&eps[i]->desc);
2427 dum_hcd->stream_en_ep |= 1 << index;
2428 set_max_streams_for_pipe(dum_hcd,
2429 usb_endpoint_num(&eps[i]->desc), ret_streams);
2431 out:
2432 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2433 return ret_streams;
2436 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2437 static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2438 struct usb_host_endpoint **eps, unsigned int num_eps,
2439 gfp_t mem_flags)
2441 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2442 unsigned long flags;
2443 int ret;
2444 unsigned int index;
2445 unsigned int i;
2447 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2448 for (i = 0; i < num_eps; i++) {
2449 index = dummy_get_ep_idx(&eps[i]->desc);
2450 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2451 ret = -EINVAL;
2452 goto out;
2456 for (i = 0; i < num_eps; i++) {
2457 index = dummy_get_ep_idx(&eps[i]->desc);
2458 dum_hcd->stream_en_ep &= ~(1 << index);
2459 set_max_streams_for_pipe(dum_hcd,
2460 usb_endpoint_num(&eps[i]->desc), 0);
2462 ret = 0;
2463 out:
2464 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2465 return ret;
2468 static struct hc_driver dummy_hcd = {
2469 .description = (char *) driver_name,
2470 .product_desc = "Dummy host controller",
2471 .hcd_priv_size = sizeof(struct dummy_hcd),
2473 .flags = HCD_USB3 | HCD_SHARED,
2475 .reset = dummy_setup,
2476 .start = dummy_start,
2477 .stop = dummy_stop,
2479 .urb_enqueue = dummy_urb_enqueue,
2480 .urb_dequeue = dummy_urb_dequeue,
2482 .get_frame_number = dummy_h_get_frame,
2484 .hub_status_data = dummy_hub_status,
2485 .hub_control = dummy_hub_control,
2486 .bus_suspend = dummy_bus_suspend,
2487 .bus_resume = dummy_bus_resume,
2489 .alloc_streams = dummy_alloc_streams,
2490 .free_streams = dummy_free_streams,
2493 static int dummy_hcd_probe(struct platform_device *pdev)
2495 struct usb_hcd *hs_hcd;
2496 struct usb_hcd *ss_hcd;
2497 int retval;
2499 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2501 if (!mod_data.is_super_speed)
2502 dummy_hcd.flags = HCD_USB2;
2503 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2504 if (!hs_hcd)
2505 return -ENOMEM;
2506 hs_hcd->has_tt = 1;
2508 retval = usb_add_hcd(hs_hcd, 0, 0);
2509 if (retval != 0) {
2510 usb_put_hcd(hs_hcd);
2511 return retval;
2514 if (mod_data.is_super_speed) {
2515 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2516 dev_name(&pdev->dev), hs_hcd);
2517 if (!ss_hcd) {
2518 retval = -ENOMEM;
2519 goto dealloc_usb2_hcd;
2522 retval = usb_add_hcd(ss_hcd, 0, 0);
2523 if (retval)
2524 goto put_usb3_hcd;
2526 return 0;
2528 put_usb3_hcd:
2529 usb_put_hcd(ss_hcd);
2530 dealloc_usb2_hcd:
2531 usb_put_hcd(hs_hcd);
2532 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
2533 return retval;
2536 static int dummy_hcd_remove(struct platform_device *pdev)
2538 struct dummy *dum;
2540 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
2542 if (dum->ss_hcd) {
2543 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2544 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2547 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2548 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2550 the_controller.hs_hcd = NULL;
2551 the_controller.ss_hcd = NULL;
2553 return 0;
2556 static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
2558 struct usb_hcd *hcd;
2559 struct dummy_hcd *dum_hcd;
2560 int rc = 0;
2562 dev_dbg(&pdev->dev, "%s\n", __func__);
2564 hcd = platform_get_drvdata(pdev);
2565 dum_hcd = hcd_to_dummy_hcd(hcd);
2566 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2567 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2568 rc = -EBUSY;
2569 } else
2570 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2571 return rc;
2574 static int dummy_hcd_resume(struct platform_device *pdev)
2576 struct usb_hcd *hcd;
2578 dev_dbg(&pdev->dev, "%s\n", __func__);
2580 hcd = platform_get_drvdata(pdev);
2581 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2582 usb_hcd_poll_rh_status(hcd);
2583 return 0;
2586 static struct platform_driver dummy_hcd_driver = {
2587 .probe = dummy_hcd_probe,
2588 .remove = dummy_hcd_remove,
2589 .suspend = dummy_hcd_suspend,
2590 .resume = dummy_hcd_resume,
2591 .driver = {
2592 .name = (char *) driver_name,
2593 .owner = THIS_MODULE,
2597 /*-------------------------------------------------------------------------*/
2599 static struct platform_device *the_udc_pdev;
2600 static struct platform_device *the_hcd_pdev;
2602 static int __init init(void)
2604 int retval = -ENOMEM;
2606 if (usb_disabled())
2607 return -ENODEV;
2609 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2610 return -EINVAL;
2612 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2613 if (!the_hcd_pdev)
2614 return retval;
2615 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2616 if (!the_udc_pdev)
2617 goto err_alloc_udc;
2619 retval = platform_driver_register(&dummy_hcd_driver);
2620 if (retval < 0)
2621 goto err_register_hcd_driver;
2622 retval = platform_driver_register(&dummy_udc_driver);
2623 if (retval < 0)
2624 goto err_register_udc_driver;
2626 retval = platform_device_add(the_hcd_pdev);
2627 if (retval < 0)
2628 goto err_add_hcd;
2629 if (!the_controller.hs_hcd ||
2630 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
2632 * The hcd was added successfully but its probe function failed
2633 * for some reason.
2635 retval = -EINVAL;
2636 goto err_add_udc;
2638 retval = platform_device_add(the_udc_pdev);
2639 if (retval < 0)
2640 goto err_add_udc;
2641 if (!platform_get_drvdata(the_udc_pdev)) {
2643 * The udc was added successfully but its probe function failed
2644 * for some reason.
2646 retval = -EINVAL;
2647 goto err_probe_udc;
2649 return retval;
2651 err_probe_udc:
2652 platform_device_del(the_udc_pdev);
2653 err_add_udc:
2654 platform_device_del(the_hcd_pdev);
2655 err_add_hcd:
2656 platform_driver_unregister(&dummy_udc_driver);
2657 err_register_udc_driver:
2658 platform_driver_unregister(&dummy_hcd_driver);
2659 err_register_hcd_driver:
2660 platform_device_put(the_udc_pdev);
2661 err_alloc_udc:
2662 platform_device_put(the_hcd_pdev);
2663 return retval;
2665 module_init(init);
2667 static void __exit cleanup(void)
2669 platform_device_unregister(the_udc_pdev);
2670 platform_device_unregister(the_hcd_pdev);
2671 platform_driver_unregister(&dummy_udc_driver);
2672 platform_driver_unregister(&dummy_hcd_driver);
2674 module_exit(cleanup);