usb: gadget: udc: dummy: do not rely on 'driver' argument
[linux-2.6/btrfs-unstable.git] / drivers / usb / gadget / udc / dummy_hcd.c
blob32bdd788c05aee57affd05f1623809882e5433d7
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;
66 unsigned int num;
69 static struct dummy_hcd_module_parameters mod_data = {
70 .is_super_speed = false,
71 .is_high_speed = true,
72 .num = 1,
74 module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
75 MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
76 module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
77 MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
78 module_param_named(num, mod_data.num, uint, S_IRUGO);
79 MODULE_PARM_DESC(num, "number of emulated controllers");
80 /*-------------------------------------------------------------------------*/
82 /* gadget side driver data structres */
83 struct dummy_ep {
84 struct list_head queue;
85 unsigned long last_io; /* jiffies timestamp */
86 struct usb_gadget *gadget;
87 const struct usb_endpoint_descriptor *desc;
88 struct usb_ep ep;
89 unsigned halted:1;
90 unsigned wedged:1;
91 unsigned already_seen:1;
92 unsigned setup_stage:1;
93 unsigned stream_en:1;
96 struct dummy_request {
97 struct list_head queue; /* ep's requests */
98 struct usb_request req;
101 static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
103 return container_of(_ep, struct dummy_ep, ep);
106 static inline struct dummy_request *usb_request_to_dummy_request
107 (struct usb_request *_req)
109 return container_of(_req, struct dummy_request, req);
112 /*-------------------------------------------------------------------------*/
115 * Every device has ep0 for control requests, plus up to 30 more endpoints,
116 * in one of two types:
118 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
119 * number can be changed. Names like "ep-a" are used for this type.
121 * - Fixed Function: in other cases. some characteristics may be mutable;
122 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
124 * Gadget drivers are responsible for not setting up conflicting endpoint
125 * configurations, illegal or unsupported packet lengths, and so on.
128 static const char ep0name[] = "ep0";
130 static const char *const ep_name[] = {
131 ep0name, /* everyone has ep0 */
133 /* act like a pxa250: fifteen fixed function endpoints */
134 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
135 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
136 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
137 "ep15in-int",
139 /* or like sa1100: two fixed function endpoints */
140 "ep1out-bulk", "ep2in-bulk",
142 /* and now some generic EPs so we have enough in multi config */
143 "ep3out", "ep4in", "ep5out", "ep6out", "ep7in", "ep8out", "ep9in",
144 "ep10out", "ep11out", "ep12in", "ep13out", "ep14in", "ep15out",
146 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
148 /*-------------------------------------------------------------------------*/
150 #define FIFO_SIZE 64
152 struct urbp {
153 struct urb *urb;
154 struct list_head urbp_list;
155 struct sg_mapping_iter miter;
156 u32 miter_started;
160 enum dummy_rh_state {
161 DUMMY_RH_RESET,
162 DUMMY_RH_SUSPENDED,
163 DUMMY_RH_RUNNING
166 struct dummy_hcd {
167 struct dummy *dum;
168 enum dummy_rh_state rh_state;
169 struct timer_list timer;
170 u32 port_status;
171 u32 old_status;
172 unsigned long re_timeout;
174 struct usb_device *udev;
175 struct list_head urbp_list;
176 u32 stream_en_ep;
177 u8 num_stream[30 / 2];
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 /*-------------------------------------------------------------------------*/
248 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
250 /* called with spinlock held */
251 static void nuke(struct dummy *dum, struct dummy_ep *ep)
253 while (!list_empty(&ep->queue)) {
254 struct dummy_request *req;
256 req = list_entry(ep->queue.next, struct dummy_request, queue);
257 list_del_init(&req->queue);
258 req->req.status = -ESHUTDOWN;
260 spin_unlock(&dum->lock);
261 usb_gadget_giveback_request(&ep->ep, &req->req);
262 spin_lock(&dum->lock);
266 /* caller must hold lock */
267 static void stop_activity(struct dummy *dum)
269 struct dummy_ep *ep;
271 /* prevent any more requests */
272 dum->address = 0;
274 /* The timer is left running so that outstanding URBs can fail */
276 /* nuke any pending requests first, so driver i/o is quiesced */
277 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
278 nuke(dum, ep);
280 /* driver now does any non-usb quiescing necessary */
284 * set_link_state_by_speed() - Sets the current state of the link according to
285 * the hcd speed
286 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
288 * This function updates the port_status according to the link state and the
289 * speed of the hcd.
291 static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
293 struct dummy *dum = dum_hcd->dum;
295 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
296 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
297 dum_hcd->port_status = 0;
298 } else if (!dum->pullup || dum->udc_suspended) {
299 /* UDC suspend must cause a disconnect */
300 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
301 USB_PORT_STAT_ENABLE);
302 if ((dum_hcd->old_status &
303 USB_PORT_STAT_CONNECTION) != 0)
304 dum_hcd->port_status |=
305 (USB_PORT_STAT_C_CONNECTION << 16);
306 } else {
307 /* device is connected and not suspended */
308 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
309 USB_PORT_STAT_SPEED_5GBPS) ;
310 if ((dum_hcd->old_status &
311 USB_PORT_STAT_CONNECTION) == 0)
312 dum_hcd->port_status |=
313 (USB_PORT_STAT_C_CONNECTION << 16);
314 if ((dum_hcd->port_status &
315 USB_PORT_STAT_ENABLE) == 1 &&
316 (dum_hcd->port_status &
317 USB_SS_PORT_LS_U0) == 1 &&
318 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
319 dum_hcd->active = 1;
321 } else {
322 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
323 dum_hcd->port_status = 0;
324 } else if (!dum->pullup || dum->udc_suspended) {
325 /* UDC suspend must cause a disconnect */
326 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
327 USB_PORT_STAT_ENABLE |
328 USB_PORT_STAT_LOW_SPEED |
329 USB_PORT_STAT_HIGH_SPEED |
330 USB_PORT_STAT_SUSPEND);
331 if ((dum_hcd->old_status &
332 USB_PORT_STAT_CONNECTION) != 0)
333 dum_hcd->port_status |=
334 (USB_PORT_STAT_C_CONNECTION << 16);
335 } else {
336 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
337 if ((dum_hcd->old_status &
338 USB_PORT_STAT_CONNECTION) == 0)
339 dum_hcd->port_status |=
340 (USB_PORT_STAT_C_CONNECTION << 16);
341 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
342 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
343 else if ((dum_hcd->port_status &
344 USB_PORT_STAT_SUSPEND) == 0 &&
345 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
346 dum_hcd->active = 1;
351 /* caller must hold lock */
352 static void set_link_state(struct dummy_hcd *dum_hcd)
354 struct dummy *dum = dum_hcd->dum;
356 dum_hcd->active = 0;
357 if (dum->pullup)
358 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
359 dum->gadget.speed != USB_SPEED_SUPER) ||
360 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
361 dum->gadget.speed == USB_SPEED_SUPER))
362 return;
364 set_link_state_by_speed(dum_hcd);
366 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
367 dum_hcd->active)
368 dum_hcd->resuming = 0;
370 /* if !connected or reset */
371 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
372 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
374 * We're connected and not reset (reset occurred now),
375 * and driver attached - disconnect!
377 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
378 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
379 dum->driver) {
380 stop_activity(dum);
381 spin_unlock(&dum->lock);
382 dum->driver->disconnect(&dum->gadget);
383 spin_lock(&dum->lock);
385 } else if (dum_hcd->active != dum_hcd->old_active) {
386 if (dum_hcd->old_active && dum->driver->suspend) {
387 spin_unlock(&dum->lock);
388 dum->driver->suspend(&dum->gadget);
389 spin_lock(&dum->lock);
390 } else if (!dum_hcd->old_active && dum->driver->resume) {
391 spin_unlock(&dum->lock);
392 dum->driver->resume(&dum->gadget);
393 spin_lock(&dum->lock);
397 dum_hcd->old_status = dum_hcd->port_status;
398 dum_hcd->old_active = dum_hcd->active;
401 /*-------------------------------------------------------------------------*/
403 /* SLAVE/GADGET SIDE DRIVER
405 * This only tracks gadget state. All the work is done when the host
406 * side tries some (emulated) i/o operation. Real device controller
407 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
410 #define is_enabled(dum) \
411 (dum->port_status & USB_PORT_STAT_ENABLE)
413 static int dummy_enable(struct usb_ep *_ep,
414 const struct usb_endpoint_descriptor *desc)
416 struct dummy *dum;
417 struct dummy_hcd *dum_hcd;
418 struct dummy_ep *ep;
419 unsigned max;
420 int retval;
422 ep = usb_ep_to_dummy_ep(_ep);
423 if (!_ep || !desc || ep->desc || _ep->name == ep0name
424 || desc->bDescriptorType != USB_DT_ENDPOINT)
425 return -EINVAL;
426 dum = ep_to_dummy(ep);
427 if (!dum->driver)
428 return -ESHUTDOWN;
430 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
431 if (!is_enabled(dum_hcd))
432 return -ESHUTDOWN;
435 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
436 * maximum packet size.
437 * For SS devices the wMaxPacketSize is limited by 1024.
439 max = usb_endpoint_maxp(desc) & 0x7ff;
441 /* drivers must not request bad settings, since lower levels
442 * (hardware or its drivers) may not check. some endpoints
443 * can't do iso, many have maxpacket limitations, etc.
445 * since this "hardware" driver is here to help debugging, we
446 * have some extra sanity checks. (there could be more though,
447 * especially for "ep9out" style fixed function ones.)
449 retval = -EINVAL;
450 switch (usb_endpoint_type(desc)) {
451 case USB_ENDPOINT_XFER_BULK:
452 if (strstr(ep->ep.name, "-iso")
453 || strstr(ep->ep.name, "-int")) {
454 goto done;
456 switch (dum->gadget.speed) {
457 case USB_SPEED_SUPER:
458 if (max == 1024)
459 break;
460 goto done;
461 case USB_SPEED_HIGH:
462 if (max == 512)
463 break;
464 goto done;
465 case USB_SPEED_FULL:
466 if (max == 8 || max == 16 || max == 32 || max == 64)
467 /* we'll fake any legal size */
468 break;
469 /* save a return statement */
470 default:
471 goto done;
473 break;
474 case USB_ENDPOINT_XFER_INT:
475 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
476 goto done;
477 /* real hardware might not handle all packet sizes */
478 switch (dum->gadget.speed) {
479 case USB_SPEED_SUPER:
480 case USB_SPEED_HIGH:
481 if (max <= 1024)
482 break;
483 /* save a return statement */
484 case USB_SPEED_FULL:
485 if (max <= 64)
486 break;
487 /* save a return statement */
488 default:
489 if (max <= 8)
490 break;
491 goto done;
493 break;
494 case USB_ENDPOINT_XFER_ISOC:
495 if (strstr(ep->ep.name, "-bulk")
496 || strstr(ep->ep.name, "-int"))
497 goto done;
498 /* real hardware might not handle all packet sizes */
499 switch (dum->gadget.speed) {
500 case USB_SPEED_SUPER:
501 case USB_SPEED_HIGH:
502 if (max <= 1024)
503 break;
504 /* save a return statement */
505 case USB_SPEED_FULL:
506 if (max <= 1023)
507 break;
508 /* save a return statement */
509 default:
510 goto done;
512 break;
513 default:
514 /* few chips support control except on ep0 */
515 goto done;
518 _ep->maxpacket = max;
519 if (usb_ss_max_streams(_ep->comp_desc)) {
520 if (!usb_endpoint_xfer_bulk(desc)) {
521 dev_err(udc_dev(dum), "Can't enable stream support on "
522 "non-bulk ep %s\n", _ep->name);
523 return -EINVAL;
525 ep->stream_en = 1;
527 ep->desc = desc;
529 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
530 _ep->name,
531 desc->bEndpointAddress & 0x0f,
532 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
533 ({ char *val;
534 switch (usb_endpoint_type(desc)) {
535 case USB_ENDPOINT_XFER_BULK:
536 val = "bulk";
537 break;
538 case USB_ENDPOINT_XFER_ISOC:
539 val = "iso";
540 break;
541 case USB_ENDPOINT_XFER_INT:
542 val = "intr";
543 break;
544 default:
545 val = "ctrl";
546 break;
547 } val; }),
548 max, ep->stream_en ? "enabled" : "disabled");
550 /* at this point real hardware should be NAKing transfers
551 * to that endpoint, until a buffer is queued to it.
553 ep->halted = ep->wedged = 0;
554 retval = 0;
555 done:
556 return retval;
559 static int dummy_disable(struct usb_ep *_ep)
561 struct dummy_ep *ep;
562 struct dummy *dum;
563 unsigned long flags;
565 ep = usb_ep_to_dummy_ep(_ep);
566 if (!_ep || !ep->desc || _ep->name == ep0name)
567 return -EINVAL;
568 dum = ep_to_dummy(ep);
570 spin_lock_irqsave(&dum->lock, flags);
571 ep->desc = NULL;
572 ep->stream_en = 0;
573 nuke(dum, ep);
574 spin_unlock_irqrestore(&dum->lock, flags);
576 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
577 return 0;
580 static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
581 gfp_t mem_flags)
583 struct dummy_ep *ep;
584 struct dummy_request *req;
586 if (!_ep)
587 return NULL;
588 ep = usb_ep_to_dummy_ep(_ep);
590 req = kzalloc(sizeof(*req), mem_flags);
591 if (!req)
592 return NULL;
593 INIT_LIST_HEAD(&req->queue);
594 return &req->req;
597 static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
599 struct dummy_request *req;
601 if (!_ep || !_req) {
602 WARN_ON(1);
603 return;
606 req = usb_request_to_dummy_request(_req);
607 WARN_ON(!list_empty(&req->queue));
608 kfree(req);
611 static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
615 static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
616 gfp_t mem_flags)
618 struct dummy_ep *ep;
619 struct dummy_request *req;
620 struct dummy *dum;
621 struct dummy_hcd *dum_hcd;
622 unsigned long flags;
624 req = usb_request_to_dummy_request(_req);
625 if (!_req || !list_empty(&req->queue) || !_req->complete)
626 return -EINVAL;
628 ep = usb_ep_to_dummy_ep(_ep);
629 if (!_ep || (!ep->desc && _ep->name != ep0name))
630 return -EINVAL;
632 dum = ep_to_dummy(ep);
633 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
634 if (!dum->driver || !is_enabled(dum_hcd))
635 return -ESHUTDOWN;
637 #if 0
638 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
639 ep, _req, _ep->name, _req->length, _req->buf);
640 #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 usb_gadget_giveback_request(_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 usb_gadget_giveback_request(_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_update_ep0(struct dummy *dum)
812 if (dum->gadget.speed == USB_SPEED_SUPER)
813 dum->ep[0].ep.maxpacket = 9;
814 else
815 dum->ep[0].ep.maxpacket = 64;
818 static int dummy_pullup(struct usb_gadget *_gadget, int value)
820 struct dummy_hcd *dum_hcd;
821 struct dummy *dum;
822 unsigned long flags;
824 dum = gadget_dev_to_dummy(&_gadget->dev);
826 if (value && dum->driver) {
827 if (mod_data.is_super_speed)
828 dum->gadget.speed = dum->driver->max_speed;
829 else if (mod_data.is_high_speed)
830 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
831 dum->driver->max_speed);
832 else
833 dum->gadget.speed = USB_SPEED_FULL;
834 dummy_udc_update_ep0(dum);
836 if (dum->gadget.speed < dum->driver->max_speed)
837 dev_dbg(udc_dev(dum), "This device can perform faster"
838 " if you connect it to a %s port...\n",
839 usb_speed_string(dum->driver->max_speed));
841 dum_hcd = gadget_to_dummy_hcd(_gadget);
843 spin_lock_irqsave(&dum->lock, flags);
844 dum->pullup = (value != 0);
845 set_link_state(dum_hcd);
846 spin_unlock_irqrestore(&dum->lock, flags);
848 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
849 return 0;
852 static int dummy_udc_start(struct usb_gadget *g,
853 struct usb_gadget_driver *driver);
854 static int dummy_udc_stop(struct usb_gadget *g,
855 struct usb_gadget_driver *driver);
857 static const struct usb_gadget_ops dummy_ops = {
858 .get_frame = dummy_g_get_frame,
859 .wakeup = dummy_wakeup,
860 .set_selfpowered = dummy_set_selfpowered,
861 .pullup = dummy_pullup,
862 .udc_start = dummy_udc_start,
863 .udc_stop = dummy_udc_stop,
866 /*-------------------------------------------------------------------------*/
868 /* "function" sysfs attribute */
869 static ssize_t function_show(struct device *dev, struct device_attribute *attr,
870 char *buf)
872 struct dummy *dum = gadget_dev_to_dummy(dev);
874 if (!dum->driver || !dum->driver->function)
875 return 0;
876 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
878 static DEVICE_ATTR_RO(function);
880 /*-------------------------------------------------------------------------*/
883 * Driver registration/unregistration.
885 * This is basically hardware-specific; there's usually only one real USB
886 * device (not host) controller since that's how USB devices are intended
887 * to work. So most implementations of these api calls will rely on the
888 * fact that only one driver will ever bind to the hardware. But curious
889 * hardware can be built with discrete components, so the gadget API doesn't
890 * require that assumption.
892 * For this emulator, it might be convenient to create a usb slave device
893 * for each driver that registers: just add to a big root hub.
896 static int dummy_udc_start(struct usb_gadget *g,
897 struct usb_gadget_driver *driver)
899 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
900 struct dummy *dum = dum_hcd->dum;
902 if (driver->max_speed == USB_SPEED_UNKNOWN)
903 return -EINVAL;
906 * SLAVE side init ... the layer above hardware, which
907 * can't enumerate without help from the driver we're binding.
910 dum->devstatus = 0;
912 dum->driver = driver;
913 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
914 driver->driver.name);
915 return 0;
918 static int dummy_udc_stop(struct usb_gadget *g,
919 struct usb_gadget_driver *driver)
921 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
922 struct dummy *dum = dum_hcd->dum;
924 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
925 dum->driver->driver.name);
927 dum->driver = NULL;
929 return 0;
932 #undef is_enabled
934 /* The gadget structure is stored inside the hcd structure and will be
935 * released along with it. */
936 static void init_dummy_udc_hw(struct dummy *dum)
938 int i;
940 INIT_LIST_HEAD(&dum->gadget.ep_list);
941 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
942 struct dummy_ep *ep = &dum->ep[i];
944 if (!ep_name[i])
945 break;
946 ep->ep.name = ep_name[i];
947 ep->ep.ops = &dummy_ep_ops;
948 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
949 ep->halted = ep->wedged = ep->already_seen =
950 ep->setup_stage = 0;
951 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
952 ep->ep.max_streams = 16;
953 ep->last_io = jiffies;
954 ep->gadget = &dum->gadget;
955 ep->desc = NULL;
956 INIT_LIST_HEAD(&ep->queue);
959 dum->gadget.ep0 = &dum->ep[0].ep;
960 list_del_init(&dum->ep[0].ep.ep_list);
961 INIT_LIST_HEAD(&dum->fifo_req.queue);
963 #ifdef CONFIG_USB_OTG
964 dum->gadget.is_otg = 1;
965 #endif
968 static int dummy_udc_probe(struct platform_device *pdev)
970 struct dummy *dum;
971 int rc;
973 dum = *((void **)dev_get_platdata(&pdev->dev));
974 dum->gadget.name = gadget_name;
975 dum->gadget.ops = &dummy_ops;
976 dum->gadget.max_speed = USB_SPEED_SUPER;
978 dum->gadget.dev.parent = &pdev->dev;
979 init_dummy_udc_hw(dum);
981 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
982 if (rc < 0)
983 goto err_udc;
985 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
986 if (rc < 0)
987 goto err_dev;
988 platform_set_drvdata(pdev, dum);
989 return rc;
991 err_dev:
992 usb_del_gadget_udc(&dum->gadget);
993 err_udc:
994 return rc;
997 static int dummy_udc_remove(struct platform_device *pdev)
999 struct dummy *dum = platform_get_drvdata(pdev);
1001 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1002 usb_del_gadget_udc(&dum->gadget);
1003 return 0;
1006 static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1007 int suspend)
1009 spin_lock_irq(&dum->lock);
1010 dum->udc_suspended = suspend;
1011 set_link_state(dum_hcd);
1012 spin_unlock_irq(&dum->lock);
1015 static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1017 struct dummy *dum = platform_get_drvdata(pdev);
1018 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1020 dev_dbg(&pdev->dev, "%s\n", __func__);
1021 dummy_udc_pm(dum, dum_hcd, 1);
1022 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1023 return 0;
1026 static int dummy_udc_resume(struct platform_device *pdev)
1028 struct dummy *dum = platform_get_drvdata(pdev);
1029 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1031 dev_dbg(&pdev->dev, "%s\n", __func__);
1032 dummy_udc_pm(dum, dum_hcd, 0);
1033 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1034 return 0;
1037 static struct platform_driver dummy_udc_driver = {
1038 .probe = dummy_udc_probe,
1039 .remove = dummy_udc_remove,
1040 .suspend = dummy_udc_suspend,
1041 .resume = dummy_udc_resume,
1042 .driver = {
1043 .name = (char *) gadget_name,
1044 .owner = THIS_MODULE,
1048 /*-------------------------------------------------------------------------*/
1050 static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1052 unsigned int index;
1054 index = usb_endpoint_num(desc) << 1;
1055 if (usb_endpoint_dir_in(desc))
1056 index |= 1;
1057 return index;
1060 /* MASTER/HOST SIDE DRIVER
1062 * this uses the hcd framework to hook up to host side drivers.
1063 * its root hub will only have one device, otherwise it acts like
1064 * a normal host controller.
1066 * when urbs are queued, they're just stuck on a list that we
1067 * scan in a timer callback. that callback connects writes from
1068 * the host with reads from the device, and so on, based on the
1069 * usb 2.0 rules.
1072 static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1074 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1075 u32 index;
1077 if (!usb_endpoint_xfer_bulk(desc))
1078 return 0;
1080 index = dummy_get_ep_idx(desc);
1081 return (1 << index) & dum_hcd->stream_en_ep;
1085 * The max stream number is saved as a nibble so for the 30 possible endpoints
1086 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1087 * means we use only 1 stream). The maximum according to the spec is 16bit so
1088 * if the 16 stream limit is about to go, the array size should be incremented
1089 * to 30 elements of type u16.
1091 static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1092 unsigned int pipe)
1094 int max_streams;
1096 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1097 if (usb_pipeout(pipe))
1098 max_streams >>= 4;
1099 else
1100 max_streams &= 0xf;
1101 max_streams++;
1102 return max_streams;
1105 static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1106 unsigned int pipe, unsigned int streams)
1108 int max_streams;
1110 streams--;
1111 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1112 if (usb_pipeout(pipe)) {
1113 streams <<= 4;
1114 max_streams &= 0xf;
1115 } else {
1116 max_streams &= 0xf0;
1118 max_streams |= streams;
1119 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1122 static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1124 unsigned int max_streams;
1125 int enabled;
1127 enabled = dummy_ep_stream_en(dum_hcd, urb);
1128 if (!urb->stream_id) {
1129 if (enabled)
1130 return -EINVAL;
1131 return 0;
1133 if (!enabled)
1134 return -EINVAL;
1136 max_streams = get_max_streams_for_pipe(dum_hcd,
1137 usb_pipeendpoint(urb->pipe));
1138 if (urb->stream_id > max_streams) {
1139 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1140 urb->stream_id);
1141 BUG();
1142 return -EINVAL;
1144 return 0;
1147 static int dummy_urb_enqueue(
1148 struct usb_hcd *hcd,
1149 struct urb *urb,
1150 gfp_t mem_flags
1152 struct dummy_hcd *dum_hcd;
1153 struct urbp *urbp;
1154 unsigned long flags;
1155 int rc;
1157 urbp = kmalloc(sizeof *urbp, mem_flags);
1158 if (!urbp)
1159 return -ENOMEM;
1160 urbp->urb = urb;
1161 urbp->miter_started = 0;
1163 dum_hcd = hcd_to_dummy_hcd(hcd);
1164 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1166 rc = dummy_validate_stream(dum_hcd, urb);
1167 if (rc) {
1168 kfree(urbp);
1169 goto done;
1172 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1173 if (rc) {
1174 kfree(urbp);
1175 goto done;
1178 if (!dum_hcd->udev) {
1179 dum_hcd->udev = urb->dev;
1180 usb_get_dev(dum_hcd->udev);
1181 } else if (unlikely(dum_hcd->udev != urb->dev))
1182 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1184 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1185 urb->hcpriv = urbp;
1186 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
1187 urb->error_count = 1; /* mark as a new urb */
1189 /* kick the scheduler, it'll do the rest */
1190 if (!timer_pending(&dum_hcd->timer))
1191 mod_timer(&dum_hcd->timer, jiffies + 1);
1193 done:
1194 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1195 return rc;
1198 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1200 struct dummy_hcd *dum_hcd;
1201 unsigned long flags;
1202 int rc;
1204 /* giveback happens automatically in timer callback,
1205 * so make sure the callback happens */
1206 dum_hcd = hcd_to_dummy_hcd(hcd);
1207 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1209 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1210 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1211 !list_empty(&dum_hcd->urbp_list))
1212 mod_timer(&dum_hcd->timer, jiffies);
1214 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1215 return rc;
1218 static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1219 u32 len)
1221 void *ubuf, *rbuf;
1222 struct urbp *urbp = urb->hcpriv;
1223 int to_host;
1224 struct sg_mapping_iter *miter = &urbp->miter;
1225 u32 trans = 0;
1226 u32 this_sg;
1227 bool next_sg;
1229 to_host = usb_pipein(urb->pipe);
1230 rbuf = req->req.buf + req->req.actual;
1232 if (!urb->num_sgs) {
1233 ubuf = urb->transfer_buffer + urb->actual_length;
1234 if (to_host)
1235 memcpy(ubuf, rbuf, len);
1236 else
1237 memcpy(rbuf, ubuf, len);
1238 return len;
1241 if (!urbp->miter_started) {
1242 u32 flags = SG_MITER_ATOMIC;
1244 if (to_host)
1245 flags |= SG_MITER_TO_SG;
1246 else
1247 flags |= SG_MITER_FROM_SG;
1249 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1250 urbp->miter_started = 1;
1252 next_sg = sg_miter_next(miter);
1253 if (next_sg == false) {
1254 WARN_ON_ONCE(1);
1255 return -EINVAL;
1257 do {
1258 ubuf = miter->addr;
1259 this_sg = min_t(u32, len, miter->length);
1260 miter->consumed = this_sg;
1261 trans += this_sg;
1263 if (to_host)
1264 memcpy(ubuf, rbuf, this_sg);
1265 else
1266 memcpy(rbuf, ubuf, this_sg);
1267 len -= this_sg;
1269 if (!len)
1270 break;
1271 next_sg = sg_miter_next(miter);
1272 if (next_sg == false) {
1273 WARN_ON_ONCE(1);
1274 return -EINVAL;
1277 rbuf += this_sg;
1278 } while (1);
1280 sg_miter_stop(miter);
1281 return trans;
1284 /* transfer up to a frame's worth; caller must own lock */
1285 static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1286 struct dummy_ep *ep, int limit, int *status)
1288 struct dummy *dum = dum_hcd->dum;
1289 struct dummy_request *req;
1291 top:
1292 /* if there's no request queued, the device is NAKing; return */
1293 list_for_each_entry(req, &ep->queue, queue) {
1294 unsigned host_len, dev_len, len;
1295 int is_short, to_host;
1296 int rescan = 0;
1298 if (dummy_ep_stream_en(dum_hcd, urb)) {
1299 if ((urb->stream_id != req->req.stream_id))
1300 continue;
1303 /* 1..N packets of ep->ep.maxpacket each ... the last one
1304 * may be short (including zero length).
1306 * writer can send a zlp explicitly (length 0) or implicitly
1307 * (length mod maxpacket zero, and 'zero' flag); they always
1308 * terminate reads.
1310 host_len = urb->transfer_buffer_length - urb->actual_length;
1311 dev_len = req->req.length - req->req.actual;
1312 len = min(host_len, dev_len);
1314 /* FIXME update emulated data toggle too */
1316 to_host = usb_pipein(urb->pipe);
1317 if (unlikely(len == 0))
1318 is_short = 1;
1319 else {
1320 /* not enough bandwidth left? */
1321 if (limit < ep->ep.maxpacket && limit < len)
1322 break;
1323 len = min_t(unsigned, len, limit);
1324 if (len == 0)
1325 break;
1327 /* use an extra pass for the final short packet */
1328 if (len > ep->ep.maxpacket) {
1329 rescan = 1;
1330 len -= (len % ep->ep.maxpacket);
1332 is_short = (len % ep->ep.maxpacket) != 0;
1334 len = dummy_perform_transfer(urb, req, len);
1336 ep->last_io = jiffies;
1337 if ((int)len < 0) {
1338 req->req.status = len;
1339 } else {
1340 limit -= len;
1341 urb->actual_length += len;
1342 req->req.actual += len;
1346 /* short packets terminate, maybe with overflow/underflow.
1347 * it's only really an error to write too much.
1349 * partially filling a buffer optionally blocks queue advances
1350 * (so completion handlers can clean up the queue) but we don't
1351 * need to emulate such data-in-flight.
1353 if (is_short) {
1354 if (host_len == dev_len) {
1355 req->req.status = 0;
1356 *status = 0;
1357 } else if (to_host) {
1358 req->req.status = 0;
1359 if (dev_len > host_len)
1360 *status = -EOVERFLOW;
1361 else
1362 *status = 0;
1363 } else if (!to_host) {
1364 *status = 0;
1365 if (host_len > dev_len)
1366 req->req.status = -EOVERFLOW;
1367 else
1368 req->req.status = 0;
1371 /* many requests terminate without a short packet */
1372 } else {
1373 if (req->req.length == req->req.actual
1374 && !req->req.zero)
1375 req->req.status = 0;
1376 if (urb->transfer_buffer_length == urb->actual_length
1377 && !(urb->transfer_flags
1378 & URB_ZERO_PACKET))
1379 *status = 0;
1382 /* device side completion --> continuable */
1383 if (req->req.status != -EINPROGRESS) {
1384 list_del_init(&req->queue);
1386 spin_unlock(&dum->lock);
1387 usb_gadget_giveback_request(&ep->ep, &req->req);
1388 spin_lock(&dum->lock);
1390 /* requests might have been unlinked... */
1391 rescan = 1;
1394 /* host side completion --> terminate */
1395 if (*status != -EINPROGRESS)
1396 break;
1398 /* rescan to continue with any other queued i/o */
1399 if (rescan)
1400 goto top;
1402 return limit;
1405 static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
1407 int limit = ep->ep.maxpacket;
1409 if (dum->gadget.speed == USB_SPEED_HIGH) {
1410 int tmp;
1412 /* high bandwidth mode */
1413 tmp = usb_endpoint_maxp(ep->desc);
1414 tmp = (tmp >> 11) & 0x03;
1415 tmp *= 8 /* applies to entire frame */;
1416 limit += limit * tmp;
1418 if (dum->gadget.speed == USB_SPEED_SUPER) {
1419 switch (usb_endpoint_type(ep->desc)) {
1420 case USB_ENDPOINT_XFER_ISOC:
1421 /* Sec. 4.4.8.2 USB3.0 Spec */
1422 limit = 3 * 16 * 1024 * 8;
1423 break;
1424 case USB_ENDPOINT_XFER_INT:
1425 /* Sec. 4.4.7.2 USB3.0 Spec */
1426 limit = 3 * 1024 * 8;
1427 break;
1428 case USB_ENDPOINT_XFER_BULK:
1429 default:
1430 break;
1433 return limit;
1436 #define is_active(dum_hcd) ((dum_hcd->port_status & \
1437 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1438 USB_PORT_STAT_SUSPEND)) \
1439 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1441 static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
1443 int i;
1445 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1446 dum->ss_hcd : dum->hs_hcd)))
1447 return NULL;
1448 if ((address & ~USB_DIR_IN) == 0)
1449 return &dum->ep[0];
1450 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1451 struct dummy_ep *ep = &dum->ep[i];
1453 if (!ep->desc)
1454 continue;
1455 if (ep->desc->bEndpointAddress == address)
1456 return ep;
1458 return NULL;
1461 #undef is_active
1463 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1464 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1465 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1466 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1467 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1468 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1472 * handle_control_request() - handles all control transfers
1473 * @dum: pointer to dummy (the_controller)
1474 * @urb: the urb request to handle
1475 * @setup: pointer to the setup data for a USB device control
1476 * request
1477 * @status: pointer to request handling status
1479 * Return 0 - if the request was handled
1480 * 1 - if the request wasn't handles
1481 * error code on error
1483 static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1484 struct usb_ctrlrequest *setup,
1485 int *status)
1487 struct dummy_ep *ep2;
1488 struct dummy *dum = dum_hcd->dum;
1489 int ret_val = 1;
1490 unsigned w_index;
1491 unsigned w_value;
1493 w_index = le16_to_cpu(setup->wIndex);
1494 w_value = le16_to_cpu(setup->wValue);
1495 switch (setup->bRequest) {
1496 case USB_REQ_SET_ADDRESS:
1497 if (setup->bRequestType != Dev_Request)
1498 break;
1499 dum->address = w_value;
1500 *status = 0;
1501 dev_dbg(udc_dev(dum), "set_address = %d\n",
1502 w_value);
1503 ret_val = 0;
1504 break;
1505 case USB_REQ_SET_FEATURE:
1506 if (setup->bRequestType == Dev_Request) {
1507 ret_val = 0;
1508 switch (w_value) {
1509 case USB_DEVICE_REMOTE_WAKEUP:
1510 break;
1511 case USB_DEVICE_B_HNP_ENABLE:
1512 dum->gadget.b_hnp_enable = 1;
1513 break;
1514 case USB_DEVICE_A_HNP_SUPPORT:
1515 dum->gadget.a_hnp_support = 1;
1516 break;
1517 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1518 dum->gadget.a_alt_hnp_support = 1;
1519 break;
1520 case USB_DEVICE_U1_ENABLE:
1521 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1522 HCD_USB3)
1523 w_value = USB_DEV_STAT_U1_ENABLED;
1524 else
1525 ret_val = -EOPNOTSUPP;
1526 break;
1527 case USB_DEVICE_U2_ENABLE:
1528 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1529 HCD_USB3)
1530 w_value = USB_DEV_STAT_U2_ENABLED;
1531 else
1532 ret_val = -EOPNOTSUPP;
1533 break;
1534 case USB_DEVICE_LTM_ENABLE:
1535 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1536 HCD_USB3)
1537 w_value = USB_DEV_STAT_LTM_ENABLED;
1538 else
1539 ret_val = -EOPNOTSUPP;
1540 break;
1541 default:
1542 ret_val = -EOPNOTSUPP;
1544 if (ret_val == 0) {
1545 dum->devstatus |= (1 << w_value);
1546 *status = 0;
1548 } else if (setup->bRequestType == Ep_Request) {
1549 /* endpoint halt */
1550 ep2 = find_endpoint(dum, w_index);
1551 if (!ep2 || ep2->ep.name == ep0name) {
1552 ret_val = -EOPNOTSUPP;
1553 break;
1555 ep2->halted = 1;
1556 ret_val = 0;
1557 *status = 0;
1559 break;
1560 case USB_REQ_CLEAR_FEATURE:
1561 if (setup->bRequestType == Dev_Request) {
1562 ret_val = 0;
1563 switch (w_value) {
1564 case USB_DEVICE_REMOTE_WAKEUP:
1565 w_value = USB_DEVICE_REMOTE_WAKEUP;
1566 break;
1567 case USB_DEVICE_U1_ENABLE:
1568 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1569 HCD_USB3)
1570 w_value = USB_DEV_STAT_U1_ENABLED;
1571 else
1572 ret_val = -EOPNOTSUPP;
1573 break;
1574 case USB_DEVICE_U2_ENABLE:
1575 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1576 HCD_USB3)
1577 w_value = USB_DEV_STAT_U2_ENABLED;
1578 else
1579 ret_val = -EOPNOTSUPP;
1580 break;
1581 case USB_DEVICE_LTM_ENABLE:
1582 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1583 HCD_USB3)
1584 w_value = USB_DEV_STAT_LTM_ENABLED;
1585 else
1586 ret_val = -EOPNOTSUPP;
1587 break;
1588 default:
1589 ret_val = -EOPNOTSUPP;
1590 break;
1592 if (ret_val == 0) {
1593 dum->devstatus &= ~(1 << w_value);
1594 *status = 0;
1596 } else if (setup->bRequestType == Ep_Request) {
1597 /* endpoint halt */
1598 ep2 = find_endpoint(dum, w_index);
1599 if (!ep2) {
1600 ret_val = -EOPNOTSUPP;
1601 break;
1603 if (!ep2->wedged)
1604 ep2->halted = 0;
1605 ret_val = 0;
1606 *status = 0;
1608 break;
1609 case USB_REQ_GET_STATUS:
1610 if (setup->bRequestType == Dev_InRequest
1611 || setup->bRequestType == Intf_InRequest
1612 || setup->bRequestType == Ep_InRequest) {
1613 char *buf;
1615 * device: remote wakeup, selfpowered
1616 * interface: nothing
1617 * endpoint: halt
1619 buf = (char *)urb->transfer_buffer;
1620 if (urb->transfer_buffer_length > 0) {
1621 if (setup->bRequestType == Ep_InRequest) {
1622 ep2 = find_endpoint(dum, w_index);
1623 if (!ep2) {
1624 ret_val = -EOPNOTSUPP;
1625 break;
1627 buf[0] = ep2->halted;
1628 } else if (setup->bRequestType ==
1629 Dev_InRequest) {
1630 buf[0] = (u8)dum->devstatus;
1631 } else
1632 buf[0] = 0;
1634 if (urb->transfer_buffer_length > 1)
1635 buf[1] = 0;
1636 urb->actual_length = min_t(u32, 2,
1637 urb->transfer_buffer_length);
1638 ret_val = 0;
1639 *status = 0;
1641 break;
1643 return ret_val;
1646 /* drive both sides of the transfers; looks like irq handlers to
1647 * both drivers except the callbacks aren't in_irq().
1649 static void dummy_timer(unsigned long _dum_hcd)
1651 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1652 struct dummy *dum = dum_hcd->dum;
1653 struct urbp *urbp, *tmp;
1654 unsigned long flags;
1655 int limit, total;
1656 int i;
1658 /* simplistic model for one frame's bandwidth */
1659 switch (dum->gadget.speed) {
1660 case USB_SPEED_LOW:
1661 total = 8/*bytes*/ * 12/*packets*/;
1662 break;
1663 case USB_SPEED_FULL:
1664 total = 64/*bytes*/ * 19/*packets*/;
1665 break;
1666 case USB_SPEED_HIGH:
1667 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1668 break;
1669 case USB_SPEED_SUPER:
1670 /* Bus speed is 500000 bytes/ms, so use a little less */
1671 total = 490000;
1672 break;
1673 default:
1674 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1675 return;
1678 /* FIXME if HZ != 1000 this will probably misbehave ... */
1680 /* look at each urb queued by the host side driver */
1681 spin_lock_irqsave(&dum->lock, flags);
1683 if (!dum_hcd->udev) {
1684 dev_err(dummy_dev(dum_hcd),
1685 "timer fired with no URBs pending?\n");
1686 spin_unlock_irqrestore(&dum->lock, flags);
1687 return;
1690 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1691 if (!ep_name[i])
1692 break;
1693 dum->ep[i].already_seen = 0;
1696 restart:
1697 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1698 struct urb *urb;
1699 struct dummy_request *req;
1700 u8 address;
1701 struct dummy_ep *ep = NULL;
1702 int type;
1703 int status = -EINPROGRESS;
1705 urb = urbp->urb;
1706 if (urb->unlinked)
1707 goto return_urb;
1708 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1709 continue;
1710 type = usb_pipetype(urb->pipe);
1712 /* used up this frame's non-periodic bandwidth?
1713 * FIXME there's infinite bandwidth for control and
1714 * periodic transfers ... unrealistic.
1716 if (total <= 0 && type == PIPE_BULK)
1717 continue;
1719 /* find the gadget's ep for this request (if configured) */
1720 address = usb_pipeendpoint (urb->pipe);
1721 if (usb_pipein(urb->pipe))
1722 address |= USB_DIR_IN;
1723 ep = find_endpoint(dum, address);
1724 if (!ep) {
1725 /* set_configuration() disagreement */
1726 dev_dbg(dummy_dev(dum_hcd),
1727 "no ep configured for urb %p\n",
1728 urb);
1729 status = -EPROTO;
1730 goto return_urb;
1733 if (ep->already_seen)
1734 continue;
1735 ep->already_seen = 1;
1736 if (ep == &dum->ep[0] && urb->error_count) {
1737 ep->setup_stage = 1; /* a new urb */
1738 urb->error_count = 0;
1740 if (ep->halted && !ep->setup_stage) {
1741 /* NOTE: must not be iso! */
1742 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1743 ep->ep.name, urb);
1744 status = -EPIPE;
1745 goto return_urb;
1747 /* FIXME make sure both ends agree on maxpacket */
1749 /* handle control requests */
1750 if (ep == &dum->ep[0] && ep->setup_stage) {
1751 struct usb_ctrlrequest setup;
1752 int value = 1;
1754 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
1755 /* paranoia, in case of stale queued data */
1756 list_for_each_entry(req, &ep->queue, queue) {
1757 list_del_init(&req->queue);
1758 req->req.status = -EOVERFLOW;
1759 dev_dbg(udc_dev(dum), "stale req = %p\n",
1760 req);
1762 spin_unlock(&dum->lock);
1763 usb_gadget_giveback_request(&ep->ep, &req->req);
1764 spin_lock(&dum->lock);
1765 ep->already_seen = 0;
1766 goto restart;
1769 /* gadget driver never sees set_address or operations
1770 * on standard feature flags. some hardware doesn't
1771 * even expose them.
1773 ep->last_io = jiffies;
1774 ep->setup_stage = 0;
1775 ep->halted = 0;
1777 value = handle_control_request(dum_hcd, urb, &setup,
1778 &status);
1780 /* gadget driver handles all other requests. block
1781 * until setup() returns; no reentrancy issues etc.
1783 if (value > 0) {
1784 spin_unlock(&dum->lock);
1785 value = dum->driver->setup(&dum->gadget,
1786 &setup);
1787 spin_lock(&dum->lock);
1789 if (value >= 0) {
1790 /* no delays (max 64KB data stage) */
1791 limit = 64*1024;
1792 goto treat_control_like_bulk;
1794 /* error, see below */
1797 if (value < 0) {
1798 if (value != -EOPNOTSUPP)
1799 dev_dbg(udc_dev(dum),
1800 "setup --> %d\n",
1801 value);
1802 status = -EPIPE;
1803 urb->actual_length = 0;
1806 goto return_urb;
1809 /* non-control requests */
1810 limit = total;
1811 switch (usb_pipetype(urb->pipe)) {
1812 case PIPE_ISOCHRONOUS:
1813 /* FIXME is it urb->interval since the last xfer?
1814 * use urb->iso_frame_desc[i].
1815 * complete whether or not ep has requests queued.
1816 * report random errors, to debug drivers.
1818 limit = max(limit, periodic_bytes(dum, ep));
1819 status = -ENOSYS;
1820 break;
1822 case PIPE_INTERRUPT:
1823 /* FIXME is it urb->interval since the last xfer?
1824 * this almost certainly polls too fast.
1826 limit = max(limit, periodic_bytes(dum, ep));
1827 /* FALLTHROUGH */
1829 default:
1830 treat_control_like_bulk:
1831 ep->last_io = jiffies;
1832 total = transfer(dum_hcd, urb, ep, limit, &status);
1833 break;
1836 /* incomplete transfer? */
1837 if (status == -EINPROGRESS)
1838 continue;
1840 return_urb:
1841 list_del(&urbp->urbp_list);
1842 kfree(urbp);
1843 if (ep)
1844 ep->already_seen = ep->setup_stage = 0;
1846 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1847 spin_unlock(&dum->lock);
1848 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1849 spin_lock(&dum->lock);
1851 goto restart;
1854 if (list_empty(&dum_hcd->urbp_list)) {
1855 usb_put_dev(dum_hcd->udev);
1856 dum_hcd->udev = NULL;
1857 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1858 /* want a 1 msec delay here */
1859 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1862 spin_unlock_irqrestore(&dum->lock, flags);
1865 /*-------------------------------------------------------------------------*/
1867 #define PORT_C_MASK \
1868 ((USB_PORT_STAT_C_CONNECTION \
1869 | USB_PORT_STAT_C_ENABLE \
1870 | USB_PORT_STAT_C_SUSPEND \
1871 | USB_PORT_STAT_C_OVERCURRENT \
1872 | USB_PORT_STAT_C_RESET) << 16)
1874 static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
1876 struct dummy_hcd *dum_hcd;
1877 unsigned long flags;
1878 int retval = 0;
1880 dum_hcd = hcd_to_dummy_hcd(hcd);
1882 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1883 if (!HCD_HW_ACCESSIBLE(hcd))
1884 goto done;
1886 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1887 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1888 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1889 set_link_state(dum_hcd);
1892 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1893 *buf = (1 << 1);
1894 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1895 dum_hcd->port_status);
1896 retval = 1;
1897 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1898 usb_hcd_resume_root_hub(hcd);
1900 done:
1901 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1902 return retval;
1905 /* usb 3.0 root hub device descriptor */
1906 static struct {
1907 struct usb_bos_descriptor bos;
1908 struct usb_ss_cap_descriptor ss_cap;
1909 } __packed usb3_bos_desc = {
1911 .bos = {
1912 .bLength = USB_DT_BOS_SIZE,
1913 .bDescriptorType = USB_DT_BOS,
1914 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1915 .bNumDeviceCaps = 1,
1917 .ss_cap = {
1918 .bLength = USB_DT_USB_SS_CAP_SIZE,
1919 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1920 .bDevCapabilityType = USB_SS_CAP_TYPE,
1921 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1922 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1926 static inline void
1927 ss_hub_descriptor(struct usb_hub_descriptor *desc)
1929 memset(desc, 0, sizeof *desc);
1930 desc->bDescriptorType = 0x2a;
1931 desc->bDescLength = 12;
1932 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1933 desc->bNbrPorts = 1;
1934 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1935 desc->u.ss.DeviceRemovable = 0xffff;
1938 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
1940 memset(desc, 0, sizeof *desc);
1941 desc->bDescriptorType = 0x29;
1942 desc->bDescLength = 9;
1943 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1944 desc->bNbrPorts = 1;
1945 desc->u.hs.DeviceRemovable[0] = 0xff;
1946 desc->u.hs.DeviceRemovable[1] = 0xff;
1949 static int dummy_hub_control(
1950 struct usb_hcd *hcd,
1951 u16 typeReq,
1952 u16 wValue,
1953 u16 wIndex,
1954 char *buf,
1955 u16 wLength
1957 struct dummy_hcd *dum_hcd;
1958 int retval = 0;
1959 unsigned long flags;
1961 if (!HCD_HW_ACCESSIBLE(hcd))
1962 return -ETIMEDOUT;
1964 dum_hcd = hcd_to_dummy_hcd(hcd);
1966 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1967 switch (typeReq) {
1968 case ClearHubFeature:
1969 break;
1970 case ClearPortFeature:
1971 switch (wValue) {
1972 case USB_PORT_FEAT_SUSPEND:
1973 if (hcd->speed == HCD_USB3) {
1974 dev_dbg(dummy_dev(dum_hcd),
1975 "USB_PORT_FEAT_SUSPEND req not "
1976 "supported for USB 3.0 roothub\n");
1977 goto error;
1979 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1980 /* 20msec resume signaling */
1981 dum_hcd->resuming = 1;
1982 dum_hcd->re_timeout = jiffies +
1983 msecs_to_jiffies(20);
1985 break;
1986 case USB_PORT_FEAT_POWER:
1987 if (hcd->speed == HCD_USB3) {
1988 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1989 dev_dbg(dummy_dev(dum_hcd),
1990 "power-off\n");
1991 } else
1992 if (dum_hcd->port_status &
1993 USB_SS_PORT_STAT_POWER)
1994 dev_dbg(dummy_dev(dum_hcd),
1995 "power-off\n");
1996 /* FALLS THROUGH */
1997 default:
1998 dum_hcd->port_status &= ~(1 << wValue);
1999 set_link_state(dum_hcd);
2001 break;
2002 case GetHubDescriptor:
2003 if (hcd->speed == HCD_USB3 &&
2004 (wLength < USB_DT_SS_HUB_SIZE ||
2005 wValue != (USB_DT_SS_HUB << 8))) {
2006 dev_dbg(dummy_dev(dum_hcd),
2007 "Wrong hub descriptor type for "
2008 "USB 3.0 roothub.\n");
2009 goto error;
2011 if (hcd->speed == HCD_USB3)
2012 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2013 else
2014 hub_descriptor((struct usb_hub_descriptor *) buf);
2015 break;
2017 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2018 if (hcd->speed != HCD_USB3)
2019 goto error;
2021 if ((wValue >> 8) != USB_DT_BOS)
2022 goto error;
2024 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2025 retval = sizeof(usb3_bos_desc);
2026 break;
2028 case GetHubStatus:
2029 *(__le32 *) buf = cpu_to_le32(0);
2030 break;
2031 case GetPortStatus:
2032 if (wIndex != 1)
2033 retval = -EPIPE;
2035 /* whoever resets or resumes must GetPortStatus to
2036 * complete it!!
2038 if (dum_hcd->resuming &&
2039 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2040 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2041 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
2043 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2044 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2045 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2046 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2047 if (dum_hcd->dum->pullup) {
2048 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
2050 if (hcd->speed < HCD_USB3) {
2051 switch (dum_hcd->dum->gadget.speed) {
2052 case USB_SPEED_HIGH:
2053 dum_hcd->port_status |=
2054 USB_PORT_STAT_HIGH_SPEED;
2055 break;
2056 case USB_SPEED_LOW:
2057 dum_hcd->dum->gadget.ep0->
2058 maxpacket = 8;
2059 dum_hcd->port_status |=
2060 USB_PORT_STAT_LOW_SPEED;
2061 break;
2062 default:
2063 dum_hcd->dum->gadget.speed =
2064 USB_SPEED_FULL;
2065 break;
2070 set_link_state(dum_hcd);
2071 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2072 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
2073 break;
2074 case SetHubFeature:
2075 retval = -EPIPE;
2076 break;
2077 case SetPortFeature:
2078 switch (wValue) {
2079 case USB_PORT_FEAT_LINK_STATE:
2080 if (hcd->speed != HCD_USB3) {
2081 dev_dbg(dummy_dev(dum_hcd),
2082 "USB_PORT_FEAT_LINK_STATE req not "
2083 "supported for USB 2.0 roothub\n");
2084 goto error;
2087 * Since this is dummy we don't have an actual link so
2088 * there is nothing to do for the SET_LINK_STATE cmd
2090 break;
2091 case USB_PORT_FEAT_U1_TIMEOUT:
2092 case USB_PORT_FEAT_U2_TIMEOUT:
2093 /* TODO: add suspend/resume support! */
2094 if (hcd->speed != HCD_USB3) {
2095 dev_dbg(dummy_dev(dum_hcd),
2096 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2097 "supported for USB 2.0 roothub\n");
2098 goto error;
2100 break;
2101 case USB_PORT_FEAT_SUSPEND:
2102 /* Applicable only for USB2.0 hub */
2103 if (hcd->speed == HCD_USB3) {
2104 dev_dbg(dummy_dev(dum_hcd),
2105 "USB_PORT_FEAT_SUSPEND req not "
2106 "supported for USB 3.0 roothub\n");
2107 goto error;
2109 if (dum_hcd->active) {
2110 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
2112 /* HNP would happen here; for now we
2113 * assume b_bus_req is always true.
2115 set_link_state(dum_hcd);
2116 if (((1 << USB_DEVICE_B_HNP_ENABLE)
2117 & dum_hcd->dum->devstatus) != 0)
2118 dev_dbg(dummy_dev(dum_hcd),
2119 "no HNP yet!\n");
2121 break;
2122 case USB_PORT_FEAT_POWER:
2123 if (hcd->speed == HCD_USB3)
2124 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2125 else
2126 dum_hcd->port_status |= USB_PORT_STAT_POWER;
2127 set_link_state(dum_hcd);
2128 break;
2129 case USB_PORT_FEAT_BH_PORT_RESET:
2130 /* Applicable only for USB3.0 hub */
2131 if (hcd->speed != HCD_USB3) {
2132 dev_dbg(dummy_dev(dum_hcd),
2133 "USB_PORT_FEAT_BH_PORT_RESET req not "
2134 "supported for USB 2.0 roothub\n");
2135 goto error;
2137 /* FALLS THROUGH */
2138 case USB_PORT_FEAT_RESET:
2139 /* if it's already enabled, disable */
2140 if (hcd->speed == HCD_USB3) {
2141 dum_hcd->port_status = 0;
2142 dum_hcd->port_status =
2143 (USB_SS_PORT_STAT_POWER |
2144 USB_PORT_STAT_CONNECTION |
2145 USB_PORT_STAT_RESET);
2146 } else
2147 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
2148 | USB_PORT_STAT_LOW_SPEED
2149 | USB_PORT_STAT_HIGH_SPEED);
2151 * We want to reset device status. All but the
2152 * Self powered feature
2154 dum_hcd->dum->devstatus &=
2155 (1 << USB_DEVICE_SELF_POWERED);
2157 * FIXME USB3.0: what is the correct reset signaling
2158 * interval? Is it still 50msec as for HS?
2160 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2161 /* FALLS THROUGH */
2162 default:
2163 if (hcd->speed == HCD_USB3) {
2164 if ((dum_hcd->port_status &
2165 USB_SS_PORT_STAT_POWER) != 0) {
2166 dum_hcd->port_status |= (1 << wValue);
2167 set_link_state(dum_hcd);
2169 } else
2170 if ((dum_hcd->port_status &
2171 USB_PORT_STAT_POWER) != 0) {
2172 dum_hcd->port_status |= (1 << wValue);
2173 set_link_state(dum_hcd);
2176 break;
2177 case GetPortErrorCount:
2178 if (hcd->speed != HCD_USB3) {
2179 dev_dbg(dummy_dev(dum_hcd),
2180 "GetPortErrorCount req not "
2181 "supported for USB 2.0 roothub\n");
2182 goto error;
2184 /* We'll always return 0 since this is a dummy hub */
2185 *(__le32 *) buf = cpu_to_le32(0);
2186 break;
2187 case SetHubDepth:
2188 if (hcd->speed != HCD_USB3) {
2189 dev_dbg(dummy_dev(dum_hcd),
2190 "SetHubDepth req not supported for "
2191 "USB 2.0 roothub\n");
2192 goto error;
2194 break;
2195 default:
2196 dev_dbg(dummy_dev(dum_hcd),
2197 "hub control req%04x v%04x i%04x l%d\n",
2198 typeReq, wValue, wIndex, wLength);
2199 error:
2200 /* "protocol stall" on error */
2201 retval = -EPIPE;
2203 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2205 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2206 usb_hcd_poll_rh_status(hcd);
2207 return retval;
2210 static int dummy_bus_suspend(struct usb_hcd *hcd)
2212 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2214 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2216 spin_lock_irq(&dum_hcd->dum->lock);
2217 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2218 set_link_state(dum_hcd);
2219 hcd->state = HC_STATE_SUSPENDED;
2220 spin_unlock_irq(&dum_hcd->dum->lock);
2221 return 0;
2224 static int dummy_bus_resume(struct usb_hcd *hcd)
2226 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2227 int rc = 0;
2229 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2231 spin_lock_irq(&dum_hcd->dum->lock);
2232 if (!HCD_HW_ACCESSIBLE(hcd)) {
2233 rc = -ESHUTDOWN;
2234 } else {
2235 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2236 set_link_state(dum_hcd);
2237 if (!list_empty(&dum_hcd->urbp_list))
2238 mod_timer(&dum_hcd->timer, jiffies);
2239 hcd->state = HC_STATE_RUNNING;
2241 spin_unlock_irq(&dum_hcd->dum->lock);
2242 return rc;
2245 /*-------------------------------------------------------------------------*/
2247 static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
2249 int ep = usb_pipeendpoint(urb->pipe);
2251 return snprintf(buf, size,
2252 "urb/%p %s ep%d%s%s len %d/%d\n",
2253 urb,
2254 ({ char *s;
2255 switch (urb->dev->speed) {
2256 case USB_SPEED_LOW:
2257 s = "ls";
2258 break;
2259 case USB_SPEED_FULL:
2260 s = "fs";
2261 break;
2262 case USB_SPEED_HIGH:
2263 s = "hs";
2264 break;
2265 case USB_SPEED_SUPER:
2266 s = "ss";
2267 break;
2268 default:
2269 s = "?";
2270 break;
2271 } s; }),
2272 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
2273 ({ char *s; \
2274 switch (usb_pipetype(urb->pipe)) { \
2275 case PIPE_CONTROL: \
2276 s = ""; \
2277 break; \
2278 case PIPE_BULK: \
2279 s = "-bulk"; \
2280 break; \
2281 case PIPE_INTERRUPT: \
2282 s = "-int"; \
2283 break; \
2284 default: \
2285 s = "-iso"; \
2286 break; \
2287 } s; }),
2288 urb->actual_length, urb->transfer_buffer_length);
2291 static ssize_t urbs_show(struct device *dev, struct device_attribute *attr,
2292 char *buf)
2294 struct usb_hcd *hcd = dev_get_drvdata(dev);
2295 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2296 struct urbp *urbp;
2297 size_t size = 0;
2298 unsigned long flags;
2300 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2301 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2302 size_t temp;
2304 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
2305 buf += temp;
2306 size += temp;
2308 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2310 return size;
2312 static DEVICE_ATTR_RO(urbs);
2314 static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2316 init_timer(&dum_hcd->timer);
2317 dum_hcd->timer.function = dummy_timer;
2318 dum_hcd->timer.data = (unsigned long)dum_hcd;
2319 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2320 dum_hcd->stream_en_ep = 0;
2321 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2322 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2323 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2324 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2325 #ifdef CONFIG_USB_OTG
2326 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2327 #endif
2328 return 0;
2330 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2331 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2334 static int dummy_start(struct usb_hcd *hcd)
2336 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2339 * MASTER side init ... we emulate a root hub that'll only ever
2340 * talk to one device (the slave side). Also appears in sysfs,
2341 * just like more familiar pci-based HCDs.
2343 if (!usb_hcd_is_primary_hcd(hcd))
2344 return dummy_start_ss(dum_hcd);
2346 spin_lock_init(&dum_hcd->dum->lock);
2347 init_timer(&dum_hcd->timer);
2348 dum_hcd->timer.function = dummy_timer;
2349 dum_hcd->timer.data = (unsigned long)dum_hcd;
2350 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2352 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2354 hcd->power_budget = POWER_BUDGET;
2355 hcd->state = HC_STATE_RUNNING;
2356 hcd->uses_new_polling = 1;
2358 #ifdef CONFIG_USB_OTG
2359 hcd->self.otg_port = 1;
2360 #endif
2362 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2363 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2366 static void dummy_stop(struct usb_hcd *hcd)
2368 struct dummy *dum;
2370 dum = hcd_to_dummy_hcd(hcd)->dum;
2371 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2372 usb_gadget_unregister_driver(dum->driver);
2373 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2376 /*-------------------------------------------------------------------------*/
2378 static int dummy_h_get_frame(struct usb_hcd *hcd)
2380 return dummy_g_get_frame(NULL);
2383 static int dummy_setup(struct usb_hcd *hcd)
2385 struct dummy *dum;
2387 dum = *((void **)dev_get_platdata(hcd->self.controller));
2388 hcd->self.sg_tablesize = ~0;
2389 if (usb_hcd_is_primary_hcd(hcd)) {
2390 dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2391 dum->hs_hcd->dum = dum;
2393 * Mark the first roothub as being USB 2.0.
2394 * The USB 3.0 roothub will be registered later by
2395 * dummy_hcd_probe()
2397 hcd->speed = HCD_USB2;
2398 hcd->self.root_hub->speed = USB_SPEED_HIGH;
2399 } else {
2400 dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2401 dum->ss_hcd->dum = dum;
2402 hcd->speed = HCD_USB3;
2403 hcd->self.root_hub->speed = USB_SPEED_SUPER;
2405 return 0;
2408 /* Change a group of bulk endpoints to support multiple stream IDs */
2409 static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2410 struct usb_host_endpoint **eps, unsigned int num_eps,
2411 unsigned int num_streams, gfp_t mem_flags)
2413 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2414 unsigned long flags;
2415 int max_stream;
2416 int ret_streams = num_streams;
2417 unsigned int index;
2418 unsigned int i;
2420 if (!num_eps)
2421 return -EINVAL;
2423 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2424 for (i = 0; i < num_eps; i++) {
2425 index = dummy_get_ep_idx(&eps[i]->desc);
2426 if ((1 << index) & dum_hcd->stream_en_ep) {
2427 ret_streams = -EINVAL;
2428 goto out;
2430 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2431 if (!max_stream) {
2432 ret_streams = -EINVAL;
2433 goto out;
2435 if (max_stream < ret_streams) {
2436 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2437 "stream IDs.\n",
2438 eps[i]->desc.bEndpointAddress,
2439 max_stream);
2440 ret_streams = max_stream;
2444 for (i = 0; i < num_eps; i++) {
2445 index = dummy_get_ep_idx(&eps[i]->desc);
2446 dum_hcd->stream_en_ep |= 1 << index;
2447 set_max_streams_for_pipe(dum_hcd,
2448 usb_endpoint_num(&eps[i]->desc), ret_streams);
2450 out:
2451 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2452 return ret_streams;
2455 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2456 static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2457 struct usb_host_endpoint **eps, unsigned int num_eps,
2458 gfp_t mem_flags)
2460 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2461 unsigned long flags;
2462 int ret;
2463 unsigned int index;
2464 unsigned int i;
2466 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2467 for (i = 0; i < num_eps; i++) {
2468 index = dummy_get_ep_idx(&eps[i]->desc);
2469 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2470 ret = -EINVAL;
2471 goto out;
2475 for (i = 0; i < num_eps; i++) {
2476 index = dummy_get_ep_idx(&eps[i]->desc);
2477 dum_hcd->stream_en_ep &= ~(1 << index);
2478 set_max_streams_for_pipe(dum_hcd,
2479 usb_endpoint_num(&eps[i]->desc), 0);
2481 ret = 0;
2482 out:
2483 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2484 return ret;
2487 static struct hc_driver dummy_hcd = {
2488 .description = (char *) driver_name,
2489 .product_desc = "Dummy host controller",
2490 .hcd_priv_size = sizeof(struct dummy_hcd),
2492 .flags = HCD_USB3 | HCD_SHARED,
2494 .reset = dummy_setup,
2495 .start = dummy_start,
2496 .stop = dummy_stop,
2498 .urb_enqueue = dummy_urb_enqueue,
2499 .urb_dequeue = dummy_urb_dequeue,
2501 .get_frame_number = dummy_h_get_frame,
2503 .hub_status_data = dummy_hub_status,
2504 .hub_control = dummy_hub_control,
2505 .bus_suspend = dummy_bus_suspend,
2506 .bus_resume = dummy_bus_resume,
2508 .alloc_streams = dummy_alloc_streams,
2509 .free_streams = dummy_free_streams,
2512 static int dummy_hcd_probe(struct platform_device *pdev)
2514 struct dummy *dum;
2515 struct usb_hcd *hs_hcd;
2516 struct usb_hcd *ss_hcd;
2517 int retval;
2519 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2520 dum = *((void **)dev_get_platdata(&pdev->dev));
2522 if (!mod_data.is_super_speed)
2523 dummy_hcd.flags = HCD_USB2;
2524 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2525 if (!hs_hcd)
2526 return -ENOMEM;
2527 hs_hcd->has_tt = 1;
2529 retval = usb_add_hcd(hs_hcd, 0, 0);
2530 if (retval)
2531 goto put_usb2_hcd;
2533 if (mod_data.is_super_speed) {
2534 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2535 dev_name(&pdev->dev), hs_hcd);
2536 if (!ss_hcd) {
2537 retval = -ENOMEM;
2538 goto dealloc_usb2_hcd;
2541 retval = usb_add_hcd(ss_hcd, 0, 0);
2542 if (retval)
2543 goto put_usb3_hcd;
2545 return 0;
2547 put_usb3_hcd:
2548 usb_put_hcd(ss_hcd);
2549 dealloc_usb2_hcd:
2550 usb_remove_hcd(hs_hcd);
2551 put_usb2_hcd:
2552 usb_put_hcd(hs_hcd);
2553 dum->hs_hcd = dum->ss_hcd = NULL;
2554 return retval;
2557 static int dummy_hcd_remove(struct platform_device *pdev)
2559 struct dummy *dum;
2561 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
2563 if (dum->ss_hcd) {
2564 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2565 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2568 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2569 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2571 dum->hs_hcd = NULL;
2572 dum->ss_hcd = NULL;
2574 return 0;
2577 static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
2579 struct usb_hcd *hcd;
2580 struct dummy_hcd *dum_hcd;
2581 int rc = 0;
2583 dev_dbg(&pdev->dev, "%s\n", __func__);
2585 hcd = platform_get_drvdata(pdev);
2586 dum_hcd = hcd_to_dummy_hcd(hcd);
2587 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2588 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2589 rc = -EBUSY;
2590 } else
2591 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2592 return rc;
2595 static int dummy_hcd_resume(struct platform_device *pdev)
2597 struct usb_hcd *hcd;
2599 dev_dbg(&pdev->dev, "%s\n", __func__);
2601 hcd = platform_get_drvdata(pdev);
2602 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2603 usb_hcd_poll_rh_status(hcd);
2604 return 0;
2607 static struct platform_driver dummy_hcd_driver = {
2608 .probe = dummy_hcd_probe,
2609 .remove = dummy_hcd_remove,
2610 .suspend = dummy_hcd_suspend,
2611 .resume = dummy_hcd_resume,
2612 .driver = {
2613 .name = (char *) driver_name,
2614 .owner = THIS_MODULE,
2618 /*-------------------------------------------------------------------------*/
2619 #define MAX_NUM_UDC 2
2620 static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2621 static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
2623 static int __init init(void)
2625 int retval = -ENOMEM;
2626 int i;
2627 struct dummy *dum[MAX_NUM_UDC];
2629 if (usb_disabled())
2630 return -ENODEV;
2632 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2633 return -EINVAL;
2635 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2636 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2637 MAX_NUM_UDC);
2638 return -EINVAL;
2641 for (i = 0; i < mod_data.num; i++) {
2642 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2643 if (!the_hcd_pdev[i]) {
2644 i--;
2645 while (i >= 0)
2646 platform_device_put(the_hcd_pdev[i--]);
2647 return retval;
2650 for (i = 0; i < mod_data.num; i++) {
2651 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2652 if (!the_udc_pdev[i]) {
2653 i--;
2654 while (i >= 0)
2655 platform_device_put(the_udc_pdev[i--]);
2656 goto err_alloc_udc;
2659 for (i = 0; i < mod_data.num; i++) {
2660 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
2661 if (!dum[i]) {
2662 retval = -ENOMEM;
2663 goto err_add_pdata;
2665 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2666 sizeof(void *));
2667 if (retval)
2668 goto err_add_pdata;
2669 retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2670 sizeof(void *));
2671 if (retval)
2672 goto err_add_pdata;
2675 retval = platform_driver_register(&dummy_hcd_driver);
2676 if (retval < 0)
2677 goto err_add_pdata;
2678 retval = platform_driver_register(&dummy_udc_driver);
2679 if (retval < 0)
2680 goto err_register_udc_driver;
2682 for (i = 0; i < mod_data.num; i++) {
2683 retval = platform_device_add(the_hcd_pdev[i]);
2684 if (retval < 0) {
2685 i--;
2686 while (i >= 0)
2687 platform_device_del(the_hcd_pdev[i--]);
2688 goto err_add_hcd;
2691 for (i = 0; i < mod_data.num; i++) {
2692 if (!dum[i]->hs_hcd ||
2693 (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2695 * The hcd was added successfully but its probe
2696 * function failed for some reason.
2698 retval = -EINVAL;
2699 goto err_add_udc;
2703 for (i = 0; i < mod_data.num; i++) {
2704 retval = platform_device_add(the_udc_pdev[i]);
2705 if (retval < 0) {
2706 i--;
2707 while (i >= 0)
2708 platform_device_del(the_udc_pdev[i]);
2709 goto err_add_udc;
2713 for (i = 0; i < mod_data.num; i++) {
2714 if (!platform_get_drvdata(the_udc_pdev[i])) {
2716 * The udc was added successfully but its probe
2717 * function failed for some reason.
2719 retval = -EINVAL;
2720 goto err_probe_udc;
2723 return retval;
2725 err_probe_udc:
2726 for (i = 0; i < mod_data.num; i++)
2727 platform_device_del(the_udc_pdev[i]);
2728 err_add_udc:
2729 for (i = 0; i < mod_data.num; i++)
2730 platform_device_del(the_hcd_pdev[i]);
2731 err_add_hcd:
2732 platform_driver_unregister(&dummy_udc_driver);
2733 err_register_udc_driver:
2734 platform_driver_unregister(&dummy_hcd_driver);
2735 err_add_pdata:
2736 for (i = 0; i < mod_data.num; i++)
2737 kfree(dum[i]);
2738 for (i = 0; i < mod_data.num; i++)
2739 platform_device_put(the_udc_pdev[i]);
2740 err_alloc_udc:
2741 for (i = 0; i < mod_data.num; i++)
2742 platform_device_put(the_hcd_pdev[i]);
2743 return retval;
2745 module_init(init);
2747 static void __exit cleanup(void)
2749 int i;
2751 for (i = 0; i < mod_data.num; i++) {
2752 struct dummy *dum;
2754 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2756 platform_device_unregister(the_udc_pdev[i]);
2757 platform_device_unregister(the_hcd_pdev[i]);
2758 kfree(dum);
2760 platform_driver_unregister(&dummy_udc_driver);
2761 platform_driver_unregister(&dummy_hcd_driver);
2763 module_exit(cleanup);