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>
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
{
69 static struct dummy_hcd_module_parameters mod_data
= {
70 .is_super_speed
= false,
71 .is_high_speed
= true,
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 */
84 struct list_head queue
;
85 unsigned long last_io
; /* jiffies timestamp */
86 struct usb_gadget
*gadget
;
87 const struct usb_endpoint_descriptor
*desc
;
91 unsigned already_seen
:1;
92 unsigned setup_stage
: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",
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 /*-------------------------------------------------------------------------*/
154 struct list_head urbp_list
;
155 struct sg_mapping_iter miter
;
160 enum dummy_rh_state
{
168 enum dummy_rh_state rh_state
;
169 struct timer_list timer
;
172 unsigned long re_timeout
;
174 struct usb_device
*udev
;
175 struct list_head urbp_list
;
177 u8 num_stream
[30 / 2];
180 unsigned old_active
:1;
188 * SLAVE/GADGET side support
190 struct dummy_ep ep
[DUMMY_ENDPOINTS
];
192 struct usb_gadget gadget
;
193 struct usb_gadget_driver
*driver
;
194 struct dummy_request fifo_req
;
195 u8 fifo_buf
[FIFO_SIZE
];
197 unsigned udc_suspended
: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
)
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 req
->req
.complete(&ep
->ep
, &req
->req
);
262 spin_lock(&dum
->lock
);
266 /* caller must hold lock */
267 static void stop_activity(struct dummy
*dum
)
271 /* prevent any more requests */
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
)
280 /* driver now does any non-usb quiescing necessary */
284 * set_link_state_by_speed() - Sets the current state of the link according to
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
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);
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
)
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);
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
)
351 /* caller must hold lock */
352 static void set_link_state(struct dummy_hcd
*dum_hcd
)
354 struct dummy
*dum
= dum_hcd
->dum
;
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
))
364 set_link_state_by_speed(dum_hcd
);
366 if ((dum_hcd
->port_status
& USB_PORT_STAT_ENABLE
) == 0 ||
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 &&
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
)
417 struct dummy_hcd
*dum_hcd
;
422 ep
= usb_ep_to_dummy_ep(_ep
);
423 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
424 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
426 dum
= ep_to_dummy(ep
);
430 dum_hcd
= gadget_to_dummy_hcd(&dum
->gadget
);
431 if (!is_enabled(dum_hcd
))
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.)
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")) {
456 switch (dum
->gadget
.speed
) {
457 case USB_SPEED_SUPER
:
466 if (max
== 8 || max
== 16 || max
== 32 || max
== 64)
467 /* we'll fake any legal size */
469 /* save a return statement */
474 case USB_ENDPOINT_XFER_INT
:
475 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
477 /* real hardware might not handle all packet sizes */
478 switch (dum
->gadget
.speed
) {
479 case USB_SPEED_SUPER
:
483 /* save a return statement */
487 /* save a return statement */
494 case USB_ENDPOINT_XFER_ISOC
:
495 if (strstr(ep
->ep
.name
, "-bulk")
496 || strstr(ep
->ep
.name
, "-int"))
498 /* real hardware might not handle all packet sizes */
499 switch (dum
->gadget
.speed
) {
500 case USB_SPEED_SUPER
:
504 /* save a return statement */
508 /* save a return statement */
514 /* few chips support control except on ep0 */
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
);
529 dev_dbg(udc_dev(dum
), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
531 desc
->bEndpointAddress
& 0x0f,
532 (desc
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out",
534 switch (usb_endpoint_type(desc
)) {
535 case USB_ENDPOINT_XFER_BULK
:
538 case USB_ENDPOINT_XFER_ISOC
:
541 case USB_ENDPOINT_XFER_INT
:
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;
559 static int dummy_disable(struct usb_ep
*_ep
)
566 ep
= usb_ep_to_dummy_ep(_ep
);
567 if (!_ep
|| !ep
->desc
|| _ep
->name
== ep0name
)
569 dum
= ep_to_dummy(ep
);
571 spin_lock_irqsave(&dum
->lock
, flags
);
576 spin_unlock_irqrestore(&dum
->lock
, flags
);
578 dev_dbg(udc_dev(dum
), "disabled %s\n", _ep
->name
);
582 static struct usb_request
*dummy_alloc_request(struct usb_ep
*_ep
,
586 struct dummy_request
*req
;
590 ep
= usb_ep_to_dummy_ep(_ep
);
592 req
= kzalloc(sizeof(*req
), mem_flags
);
595 INIT_LIST_HEAD(&req
->queue
);
599 static void dummy_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
601 struct dummy_request
*req
;
608 req
= usb_request_to_dummy_request(_req
);
609 WARN_ON(!list_empty(&req
->queue
));
613 static void fifo_complete(struct usb_ep
*ep
, struct usb_request
*req
)
617 static int dummy_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
621 struct dummy_request
*req
;
623 struct dummy_hcd
*dum_hcd
;
626 req
= usb_request_to_dummy_request(_req
);
627 if (!_req
|| !list_empty(&req
->queue
) || !_req
->complete
)
630 ep
= usb_ep_to_dummy_ep(_ep
);
631 if (!_ep
|| (!ep
->desc
&& _ep
->name
!= ep0name
))
634 dum
= ep_to_dummy(ep
);
635 dum_hcd
= gadget_to_dummy_hcd(&dum
->gadget
);
636 if (!dum
->driver
|| !is_enabled(dum_hcd
))
640 dev_dbg(udc_dev(dum
), "ep %p queue req %p to %s, len %d buf %p\n",
641 ep
, _req
, _ep
->name
, _req
->length
, _req
->buf
);
643 _req
->status
= -EINPROGRESS
;
645 spin_lock_irqsave(&dum
->lock
, flags
);
647 /* implement an emulated single-request FIFO */
648 if (ep
->desc
&& (ep
->desc
->bEndpointAddress
& USB_DIR_IN
) &&
649 list_empty(&dum
->fifo_req
.queue
) &&
650 list_empty(&ep
->queue
) &&
651 _req
->length
<= FIFO_SIZE
) {
652 req
= &dum
->fifo_req
;
654 req
->req
.buf
= dum
->fifo_buf
;
655 memcpy(dum
->fifo_buf
, _req
->buf
, _req
->length
);
656 req
->req
.context
= dum
;
657 req
->req
.complete
= fifo_complete
;
659 list_add_tail(&req
->queue
, &ep
->queue
);
660 spin_unlock(&dum
->lock
);
661 _req
->actual
= _req
->length
;
663 _req
->complete(_ep
, _req
);
664 spin_lock(&dum
->lock
);
666 list_add_tail(&req
->queue
, &ep
->queue
);
667 spin_unlock_irqrestore(&dum
->lock
, flags
);
669 /* real hardware would likely enable transfers here, in case
670 * it'd been left NAKing.
675 static int dummy_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
679 int retval
= -EINVAL
;
681 struct dummy_request
*req
= NULL
;
685 ep
= usb_ep_to_dummy_ep(_ep
);
686 dum
= ep_to_dummy(ep
);
691 local_irq_save(flags
);
692 spin_lock(&dum
->lock
);
693 list_for_each_entry(req
, &ep
->queue
, queue
) {
694 if (&req
->req
== _req
) {
695 list_del_init(&req
->queue
);
696 _req
->status
= -ECONNRESET
;
701 spin_unlock(&dum
->lock
);
704 dev_dbg(udc_dev(dum
),
705 "dequeued req %p from %s, len %d buf %p\n",
706 req
, _ep
->name
, _req
->length
, _req
->buf
);
707 _req
->complete(_ep
, _req
);
709 local_irq_restore(flags
);
714 dummy_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
721 ep
= usb_ep_to_dummy_ep(_ep
);
722 dum
= ep_to_dummy(ep
);
726 ep
->halted
= ep
->wedged
= 0;
727 else if (ep
->desc
&& (ep
->desc
->bEndpointAddress
& USB_DIR_IN
) &&
728 !list_empty(&ep
->queue
))
735 /* FIXME clear emulated data toggle too */
740 dummy_set_halt(struct usb_ep
*_ep
, int value
)
742 return dummy_set_halt_and_wedge(_ep
, value
, 0);
745 static int dummy_set_wedge(struct usb_ep
*_ep
)
747 if (!_ep
|| _ep
->name
== ep0name
)
749 return dummy_set_halt_and_wedge(_ep
, 1, 1);
752 static const struct usb_ep_ops dummy_ep_ops
= {
753 .enable
= dummy_enable
,
754 .disable
= dummy_disable
,
756 .alloc_request
= dummy_alloc_request
,
757 .free_request
= dummy_free_request
,
759 .queue
= dummy_queue
,
760 .dequeue
= dummy_dequeue
,
762 .set_halt
= dummy_set_halt
,
763 .set_wedge
= dummy_set_wedge
,
766 /*-------------------------------------------------------------------------*/
768 /* there are both host and device side versions of this call ... */
769 static int dummy_g_get_frame(struct usb_gadget
*_gadget
)
773 do_gettimeofday(&tv
);
774 return tv
.tv_usec
/ 1000;
777 static int dummy_wakeup(struct usb_gadget
*_gadget
)
779 struct dummy_hcd
*dum_hcd
;
781 dum_hcd
= gadget_to_dummy_hcd(_gadget
);
782 if (!(dum_hcd
->dum
->devstatus
& ((1 << USB_DEVICE_B_HNP_ENABLE
)
783 | (1 << USB_DEVICE_REMOTE_WAKEUP
))))
785 if ((dum_hcd
->port_status
& USB_PORT_STAT_CONNECTION
) == 0)
787 if ((dum_hcd
->port_status
& USB_PORT_STAT_SUSPEND
) == 0 &&
788 dum_hcd
->rh_state
!= DUMMY_RH_SUSPENDED
)
791 /* FIXME: What if the root hub is suspended but the port isn't? */
793 /* hub notices our request, issues downstream resume, etc */
794 dum_hcd
->resuming
= 1;
795 dum_hcd
->re_timeout
= jiffies
+ msecs_to_jiffies(20);
796 mod_timer(&dummy_hcd_to_hcd(dum_hcd
)->rh_timer
, dum_hcd
->re_timeout
);
800 static int dummy_set_selfpowered(struct usb_gadget
*_gadget
, int value
)
804 dum
= gadget_to_dummy_hcd(_gadget
)->dum
;
806 dum
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
808 dum
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
812 static void dummy_udc_update_ep0(struct dummy
*dum
)
814 if (dum
->gadget
.speed
== USB_SPEED_SUPER
)
815 dum
->ep
[0].ep
.maxpacket
= 9;
817 dum
->ep
[0].ep
.maxpacket
= 64;
820 static int dummy_pullup(struct usb_gadget
*_gadget
, int value
)
822 struct dummy_hcd
*dum_hcd
;
826 dum
= gadget_dev_to_dummy(&_gadget
->dev
);
828 if (value
&& dum
->driver
) {
829 if (mod_data
.is_super_speed
)
830 dum
->gadget
.speed
= dum
->driver
->max_speed
;
831 else if (mod_data
.is_high_speed
)
832 dum
->gadget
.speed
= min_t(u8
, USB_SPEED_HIGH
,
833 dum
->driver
->max_speed
);
835 dum
->gadget
.speed
= USB_SPEED_FULL
;
836 dummy_udc_update_ep0(dum
);
838 if (dum
->gadget
.speed
< dum
->driver
->max_speed
)
839 dev_dbg(udc_dev(dum
), "This device can perform faster"
840 " if you connect it to a %s port...\n",
841 usb_speed_string(dum
->driver
->max_speed
));
843 dum_hcd
= gadget_to_dummy_hcd(_gadget
);
845 spin_lock_irqsave(&dum
->lock
, flags
);
846 dum
->pullup
= (value
!= 0);
847 set_link_state(dum_hcd
);
848 spin_unlock_irqrestore(&dum
->lock
, flags
);
850 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd
));
854 static int dummy_udc_start(struct usb_gadget
*g
,
855 struct usb_gadget_driver
*driver
);
856 static int dummy_udc_stop(struct usb_gadget
*g
,
857 struct usb_gadget_driver
*driver
);
859 static const struct usb_gadget_ops dummy_ops
= {
860 .get_frame
= dummy_g_get_frame
,
861 .wakeup
= dummy_wakeup
,
862 .set_selfpowered
= dummy_set_selfpowered
,
863 .pullup
= dummy_pullup
,
864 .udc_start
= dummy_udc_start
,
865 .udc_stop
= dummy_udc_stop
,
868 /*-------------------------------------------------------------------------*/
870 /* "function" sysfs attribute */
871 static ssize_t
show_function(struct device
*dev
, struct device_attribute
*attr
,
874 struct dummy
*dum
= gadget_dev_to_dummy(dev
);
876 if (!dum
->driver
|| !dum
->driver
->function
)
878 return scnprintf(buf
, PAGE_SIZE
, "%s\n", dum
->driver
->function
);
880 static DEVICE_ATTR(function
, S_IRUGO
, show_function
, NULL
);
882 /*-------------------------------------------------------------------------*/
885 * Driver registration/unregistration.
887 * This is basically hardware-specific; there's usually only one real USB
888 * device (not host) controller since that's how USB devices are intended
889 * to work. So most implementations of these api calls will rely on the
890 * fact that only one driver will ever bind to the hardware. But curious
891 * hardware can be built with discrete components, so the gadget API doesn't
892 * require that assumption.
894 * For this emulator, it might be convenient to create a usb slave device
895 * for each driver that registers: just add to a big root hub.
898 static int dummy_udc_start(struct usb_gadget
*g
,
899 struct usb_gadget_driver
*driver
)
901 struct dummy_hcd
*dum_hcd
= gadget_to_dummy_hcd(g
);
902 struct dummy
*dum
= dum_hcd
->dum
;
904 if (driver
->max_speed
== USB_SPEED_UNKNOWN
)
908 * SLAVE side init ... the layer above hardware, which
909 * can't enumerate without help from the driver we're binding.
914 dum
->driver
= driver
;
915 dum
->gadget
.dev
.driver
= &driver
->driver
;
916 dev_dbg(udc_dev(dum
), "binding gadget driver '%s'\n",
917 driver
->driver
.name
);
921 static int dummy_udc_stop(struct usb_gadget
*g
,
922 struct usb_gadget_driver
*driver
)
924 struct dummy_hcd
*dum_hcd
= gadget_to_dummy_hcd(g
);
925 struct dummy
*dum
= dum_hcd
->dum
;
927 dev_dbg(udc_dev(dum
), "unregister gadget driver '%s'\n",
928 driver
->driver
.name
);
930 dum
->gadget
.dev
.driver
= NULL
;
938 /* The gadget structure is stored inside the hcd structure and will be
939 * released along with it. */
940 static void dummy_gadget_release(struct device
*dev
)
945 static void init_dummy_udc_hw(struct dummy
*dum
)
949 INIT_LIST_HEAD(&dum
->gadget
.ep_list
);
950 for (i
= 0; i
< DUMMY_ENDPOINTS
; i
++) {
951 struct dummy_ep
*ep
= &dum
->ep
[i
];
955 ep
->ep
.name
= ep_name
[i
];
956 ep
->ep
.ops
= &dummy_ep_ops
;
957 list_add_tail(&ep
->ep
.ep_list
, &dum
->gadget
.ep_list
);
958 ep
->halted
= ep
->wedged
= ep
->already_seen
=
960 ep
->ep
.maxpacket
= ~0;
961 ep
->ep
.max_streams
= 16;
962 ep
->last_io
= jiffies
;
963 ep
->gadget
= &dum
->gadget
;
965 INIT_LIST_HEAD(&ep
->queue
);
968 dum
->gadget
.ep0
= &dum
->ep
[0].ep
;
969 list_del_init(&dum
->ep
[0].ep
.ep_list
);
970 INIT_LIST_HEAD(&dum
->fifo_req
.queue
);
972 #ifdef CONFIG_USB_OTG
973 dum
->gadget
.is_otg
= 1;
977 static int dummy_udc_probe(struct platform_device
*pdev
)
982 dum
= *((void **)dev_get_platdata(&pdev
->dev
));
983 dum
->gadget
.name
= gadget_name
;
984 dum
->gadget
.ops
= &dummy_ops
;
985 dum
->gadget
.max_speed
= USB_SPEED_SUPER
;
987 dev_set_name(&dum
->gadget
.dev
, "gadget");
988 dum
->gadget
.dev
.parent
= &pdev
->dev
;
989 dum
->gadget
.dev
.release
= dummy_gadget_release
;
990 rc
= device_register(&dum
->gadget
.dev
);
992 put_device(&dum
->gadget
.dev
);
996 init_dummy_udc_hw(dum
);
998 rc
= usb_add_gadget_udc(&pdev
->dev
, &dum
->gadget
);
1002 rc
= device_create_file(&dum
->gadget
.dev
, &dev_attr_function
);
1005 platform_set_drvdata(pdev
, dum
);
1009 usb_del_gadget_udc(&dum
->gadget
);
1011 device_unregister(&dum
->gadget
.dev
);
1015 static int dummy_udc_remove(struct platform_device
*pdev
)
1017 struct dummy
*dum
= platform_get_drvdata(pdev
);
1019 usb_del_gadget_udc(&dum
->gadget
);
1020 platform_set_drvdata(pdev
, NULL
);
1021 device_remove_file(&dum
->gadget
.dev
, &dev_attr_function
);
1022 device_unregister(&dum
->gadget
.dev
);
1026 static void dummy_udc_pm(struct dummy
*dum
, struct dummy_hcd
*dum_hcd
,
1029 spin_lock_irq(&dum
->lock
);
1030 dum
->udc_suspended
= suspend
;
1031 set_link_state(dum_hcd
);
1032 spin_unlock_irq(&dum
->lock
);
1035 static int dummy_udc_suspend(struct platform_device
*pdev
, pm_message_t state
)
1037 struct dummy
*dum
= platform_get_drvdata(pdev
);
1038 struct dummy_hcd
*dum_hcd
= gadget_to_dummy_hcd(&dum
->gadget
);
1040 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
1041 dummy_udc_pm(dum
, dum_hcd
, 1);
1042 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd
));
1046 static int dummy_udc_resume(struct platform_device
*pdev
)
1048 struct dummy
*dum
= platform_get_drvdata(pdev
);
1049 struct dummy_hcd
*dum_hcd
= gadget_to_dummy_hcd(&dum
->gadget
);
1051 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
1052 dummy_udc_pm(dum
, dum_hcd
, 0);
1053 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd
));
1057 static struct platform_driver dummy_udc_driver
= {
1058 .probe
= dummy_udc_probe
,
1059 .remove
= dummy_udc_remove
,
1060 .suspend
= dummy_udc_suspend
,
1061 .resume
= dummy_udc_resume
,
1063 .name
= (char *) gadget_name
,
1064 .owner
= THIS_MODULE
,
1068 /*-------------------------------------------------------------------------*/
1070 static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor
*desc
)
1074 index
= usb_endpoint_num(desc
) << 1;
1075 if (usb_endpoint_dir_in(desc
))
1080 /* MASTER/HOST SIDE DRIVER
1082 * this uses the hcd framework to hook up to host side drivers.
1083 * its root hub will only have one device, otherwise it acts like
1084 * a normal host controller.
1086 * when urbs are queued, they're just stuck on a list that we
1087 * scan in a timer callback. that callback connects writes from
1088 * the host with reads from the device, and so on, based on the
1092 static int dummy_ep_stream_en(struct dummy_hcd
*dum_hcd
, struct urb
*urb
)
1094 const struct usb_endpoint_descriptor
*desc
= &urb
->ep
->desc
;
1097 if (!usb_endpoint_xfer_bulk(desc
))
1100 index
= dummy_get_ep_idx(desc
);
1101 return (1 << index
) & dum_hcd
->stream_en_ep
;
1105 * The max stream number is saved as a nibble so for the 30 possible endpoints
1106 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1107 * means we use only 1 stream). The maximum according to the spec is 16bit so
1108 * if the 16 stream limit is about to go, the array size should be incremented
1109 * to 30 elements of type u16.
1111 static int get_max_streams_for_pipe(struct dummy_hcd
*dum_hcd
,
1116 max_streams
= dum_hcd
->num_stream
[usb_pipeendpoint(pipe
)];
1117 if (usb_pipeout(pipe
))
1125 static void set_max_streams_for_pipe(struct dummy_hcd
*dum_hcd
,
1126 unsigned int pipe
, unsigned int streams
)
1131 max_streams
= dum_hcd
->num_stream
[usb_pipeendpoint(pipe
)];
1132 if (usb_pipeout(pipe
)) {
1136 max_streams
&= 0xf0;
1138 max_streams
|= streams
;
1139 dum_hcd
->num_stream
[usb_pipeendpoint(pipe
)] = max_streams
;
1142 static int dummy_validate_stream(struct dummy_hcd
*dum_hcd
, struct urb
*urb
)
1144 unsigned int max_streams
;
1147 enabled
= dummy_ep_stream_en(dum_hcd
, urb
);
1148 if (!urb
->stream_id
) {
1156 max_streams
= get_max_streams_for_pipe(dum_hcd
,
1157 usb_pipeendpoint(urb
->pipe
));
1158 if (urb
->stream_id
> max_streams
) {
1159 dev_err(dummy_dev(dum_hcd
), "Stream id %d is out of range.\n",
1167 static int dummy_urb_enqueue(
1168 struct usb_hcd
*hcd
,
1172 struct dummy_hcd
*dum_hcd
;
1174 unsigned long flags
;
1177 urbp
= kmalloc(sizeof *urbp
, mem_flags
);
1181 urbp
->miter_started
= 0;
1183 dum_hcd
= hcd_to_dummy_hcd(hcd
);
1184 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
1186 rc
= dummy_validate_stream(dum_hcd
, urb
);
1192 rc
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1198 if (!dum_hcd
->udev
) {
1199 dum_hcd
->udev
= urb
->dev
;
1200 usb_get_dev(dum_hcd
->udev
);
1201 } else if (unlikely(dum_hcd
->udev
!= urb
->dev
))
1202 dev_err(dummy_dev(dum_hcd
), "usb_device address has changed!\n");
1204 list_add_tail(&urbp
->urbp_list
, &dum_hcd
->urbp_list
);
1206 if (usb_pipetype(urb
->pipe
) == PIPE_CONTROL
)
1207 urb
->error_count
= 1; /* mark as a new urb */
1209 /* kick the scheduler, it'll do the rest */
1210 if (!timer_pending(&dum_hcd
->timer
))
1211 mod_timer(&dum_hcd
->timer
, jiffies
+ 1);
1214 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
1218 static int dummy_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1220 struct dummy_hcd
*dum_hcd
;
1221 unsigned long flags
;
1224 /* giveback happens automatically in timer callback,
1225 * so make sure the callback happens */
1226 dum_hcd
= hcd_to_dummy_hcd(hcd
);
1227 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
1229 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1230 if (!rc
&& dum_hcd
->rh_state
!= DUMMY_RH_RUNNING
&&
1231 !list_empty(&dum_hcd
->urbp_list
))
1232 mod_timer(&dum_hcd
->timer
, jiffies
);
1234 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
1238 static int dummy_perform_transfer(struct urb
*urb
, struct dummy_request
*req
,
1242 struct urbp
*urbp
= urb
->hcpriv
;
1244 struct sg_mapping_iter
*miter
= &urbp
->miter
;
1249 to_host
= usb_pipein(urb
->pipe
);
1250 rbuf
= req
->req
.buf
+ req
->req
.actual
;
1252 if (!urb
->num_sgs
) {
1253 ubuf
= urb
->transfer_buffer
+ urb
->actual_length
;
1255 memcpy(ubuf
, rbuf
, len
);
1257 memcpy(rbuf
, ubuf
, len
);
1261 if (!urbp
->miter_started
) {
1262 u32 flags
= SG_MITER_ATOMIC
;
1265 flags
|= SG_MITER_TO_SG
;
1267 flags
|= SG_MITER_FROM_SG
;
1269 sg_miter_start(miter
, urb
->sg
, urb
->num_sgs
, flags
);
1270 urbp
->miter_started
= 1;
1272 next_sg
= sg_miter_next(miter
);
1273 if (next_sg
== false) {
1279 this_sg
= min_t(u32
, len
, miter
->length
);
1280 miter
->consumed
= this_sg
;
1284 memcpy(ubuf
, rbuf
, this_sg
);
1286 memcpy(rbuf
, ubuf
, this_sg
);
1291 next_sg
= sg_miter_next(miter
);
1292 if (next_sg
== false) {
1300 sg_miter_stop(miter
);
1304 /* transfer up to a frame's worth; caller must own lock */
1305 static int transfer(struct dummy_hcd
*dum_hcd
, struct urb
*urb
,
1306 struct dummy_ep
*ep
, int limit
, int *status
)
1308 struct dummy
*dum
= dum_hcd
->dum
;
1309 struct dummy_request
*req
;
1312 /* if there's no request queued, the device is NAKing; return */
1313 list_for_each_entry(req
, &ep
->queue
, queue
) {
1314 unsigned host_len
, dev_len
, len
;
1315 int is_short
, to_host
;
1318 if (dummy_ep_stream_en(dum_hcd
, urb
)) {
1319 if ((urb
->stream_id
!= req
->req
.stream_id
))
1323 /* 1..N packets of ep->ep.maxpacket each ... the last one
1324 * may be short (including zero length).
1326 * writer can send a zlp explicitly (length 0) or implicitly
1327 * (length mod maxpacket zero, and 'zero' flag); they always
1330 host_len
= urb
->transfer_buffer_length
- urb
->actual_length
;
1331 dev_len
= req
->req
.length
- req
->req
.actual
;
1332 len
= min(host_len
, dev_len
);
1334 /* FIXME update emulated data toggle too */
1336 to_host
= usb_pipein(urb
->pipe
);
1337 if (unlikely(len
== 0))
1340 /* not enough bandwidth left? */
1341 if (limit
< ep
->ep
.maxpacket
&& limit
< len
)
1343 len
= min_t(unsigned, len
, limit
);
1347 /* use an extra pass for the final short packet */
1348 if (len
> ep
->ep
.maxpacket
) {
1350 len
-= (len
% ep
->ep
.maxpacket
);
1352 is_short
= (len
% ep
->ep
.maxpacket
) != 0;
1354 len
= dummy_perform_transfer(urb
, req
, len
);
1356 ep
->last_io
= jiffies
;
1358 req
->req
.status
= len
;
1361 urb
->actual_length
+= len
;
1362 req
->req
.actual
+= len
;
1366 /* short packets terminate, maybe with overflow/underflow.
1367 * it's only really an error to write too much.
1369 * partially filling a buffer optionally blocks queue advances
1370 * (so completion handlers can clean up the queue) but we don't
1371 * need to emulate such data-in-flight.
1374 if (host_len
== dev_len
) {
1375 req
->req
.status
= 0;
1377 } else if (to_host
) {
1378 req
->req
.status
= 0;
1379 if (dev_len
> host_len
)
1380 *status
= -EOVERFLOW
;
1383 } else if (!to_host
) {
1385 if (host_len
> dev_len
)
1386 req
->req
.status
= -EOVERFLOW
;
1388 req
->req
.status
= 0;
1391 /* many requests terminate without a short packet */
1393 if (req
->req
.length
== req
->req
.actual
1395 req
->req
.status
= 0;
1396 if (urb
->transfer_buffer_length
== urb
->actual_length
1397 && !(urb
->transfer_flags
1402 /* device side completion --> continuable */
1403 if (req
->req
.status
!= -EINPROGRESS
) {
1404 list_del_init(&req
->queue
);
1406 spin_unlock(&dum
->lock
);
1407 req
->req
.complete(&ep
->ep
, &req
->req
);
1408 spin_lock(&dum
->lock
);
1410 /* requests might have been unlinked... */
1414 /* host side completion --> terminate */
1415 if (*status
!= -EINPROGRESS
)
1418 /* rescan to continue with any other queued i/o */
1425 static int periodic_bytes(struct dummy
*dum
, struct dummy_ep
*ep
)
1427 int limit
= ep
->ep
.maxpacket
;
1429 if (dum
->gadget
.speed
== USB_SPEED_HIGH
) {
1432 /* high bandwidth mode */
1433 tmp
= usb_endpoint_maxp(ep
->desc
);
1434 tmp
= (tmp
>> 11) & 0x03;
1435 tmp
*= 8 /* applies to entire frame */;
1436 limit
+= limit
* tmp
;
1438 if (dum
->gadget
.speed
== USB_SPEED_SUPER
) {
1439 switch (usb_endpoint_type(ep
->desc
)) {
1440 case USB_ENDPOINT_XFER_ISOC
:
1441 /* Sec. 4.4.8.2 USB3.0 Spec */
1442 limit
= 3 * 16 * 1024 * 8;
1444 case USB_ENDPOINT_XFER_INT
:
1445 /* Sec. 4.4.7.2 USB3.0 Spec */
1446 limit
= 3 * 1024 * 8;
1448 case USB_ENDPOINT_XFER_BULK
:
1456 #define is_active(dum_hcd) ((dum_hcd->port_status & \
1457 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1458 USB_PORT_STAT_SUSPEND)) \
1459 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1461 static struct dummy_ep
*find_endpoint(struct dummy
*dum
, u8 address
)
1465 if (!is_active((dum
->gadget
.speed
== USB_SPEED_SUPER
?
1466 dum
->ss_hcd
: dum
->hs_hcd
)))
1468 if ((address
& ~USB_DIR_IN
) == 0)
1470 for (i
= 1; i
< DUMMY_ENDPOINTS
; i
++) {
1471 struct dummy_ep
*ep
= &dum
->ep
[i
];
1475 if (ep
->desc
->bEndpointAddress
== address
)
1483 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1484 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1485 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1486 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1487 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1488 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1492 * handle_control_request() - handles all control transfers
1493 * @dum: pointer to dummy (the_controller)
1494 * @urb: the urb request to handle
1495 * @setup: pointer to the setup data for a USB device control
1497 * @status: pointer to request handling status
1499 * Return 0 - if the request was handled
1500 * 1 - if the request wasn't handles
1501 * error code on error
1503 static int handle_control_request(struct dummy_hcd
*dum_hcd
, struct urb
*urb
,
1504 struct usb_ctrlrequest
*setup
,
1507 struct dummy_ep
*ep2
;
1508 struct dummy
*dum
= dum_hcd
->dum
;
1513 w_index
= le16_to_cpu(setup
->wIndex
);
1514 w_value
= le16_to_cpu(setup
->wValue
);
1515 switch (setup
->bRequest
) {
1516 case USB_REQ_SET_ADDRESS
:
1517 if (setup
->bRequestType
!= Dev_Request
)
1519 dum
->address
= w_value
;
1521 dev_dbg(udc_dev(dum
), "set_address = %d\n",
1525 case USB_REQ_SET_FEATURE
:
1526 if (setup
->bRequestType
== Dev_Request
) {
1529 case USB_DEVICE_REMOTE_WAKEUP
:
1531 case USB_DEVICE_B_HNP_ENABLE
:
1532 dum
->gadget
.b_hnp_enable
= 1;
1534 case USB_DEVICE_A_HNP_SUPPORT
:
1535 dum
->gadget
.a_hnp_support
= 1;
1537 case USB_DEVICE_A_ALT_HNP_SUPPORT
:
1538 dum
->gadget
.a_alt_hnp_support
= 1;
1540 case USB_DEVICE_U1_ENABLE
:
1541 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1543 w_value
= USB_DEV_STAT_U1_ENABLED
;
1545 ret_val
= -EOPNOTSUPP
;
1547 case USB_DEVICE_U2_ENABLE
:
1548 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1550 w_value
= USB_DEV_STAT_U2_ENABLED
;
1552 ret_val
= -EOPNOTSUPP
;
1554 case USB_DEVICE_LTM_ENABLE
:
1555 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1557 w_value
= USB_DEV_STAT_LTM_ENABLED
;
1559 ret_val
= -EOPNOTSUPP
;
1562 ret_val
= -EOPNOTSUPP
;
1565 dum
->devstatus
|= (1 << w_value
);
1568 } else if (setup
->bRequestType
== Ep_Request
) {
1570 ep2
= find_endpoint(dum
, w_index
);
1571 if (!ep2
|| ep2
->ep
.name
== ep0name
) {
1572 ret_val
= -EOPNOTSUPP
;
1580 case USB_REQ_CLEAR_FEATURE
:
1581 if (setup
->bRequestType
== Dev_Request
) {
1584 case USB_DEVICE_REMOTE_WAKEUP
:
1585 w_value
= USB_DEVICE_REMOTE_WAKEUP
;
1587 case USB_DEVICE_U1_ENABLE
:
1588 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1590 w_value
= USB_DEV_STAT_U1_ENABLED
;
1592 ret_val
= -EOPNOTSUPP
;
1594 case USB_DEVICE_U2_ENABLE
:
1595 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1597 w_value
= USB_DEV_STAT_U2_ENABLED
;
1599 ret_val
= -EOPNOTSUPP
;
1601 case USB_DEVICE_LTM_ENABLE
:
1602 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1604 w_value
= USB_DEV_STAT_LTM_ENABLED
;
1606 ret_val
= -EOPNOTSUPP
;
1609 ret_val
= -EOPNOTSUPP
;
1613 dum
->devstatus
&= ~(1 << w_value
);
1616 } else if (setup
->bRequestType
== Ep_Request
) {
1618 ep2
= find_endpoint(dum
, w_index
);
1620 ret_val
= -EOPNOTSUPP
;
1629 case USB_REQ_GET_STATUS
:
1630 if (setup
->bRequestType
== Dev_InRequest
1631 || setup
->bRequestType
== Intf_InRequest
1632 || setup
->bRequestType
== Ep_InRequest
) {
1635 * device: remote wakeup, selfpowered
1636 * interface: nothing
1639 buf
= (char *)urb
->transfer_buffer
;
1640 if (urb
->transfer_buffer_length
> 0) {
1641 if (setup
->bRequestType
== Ep_InRequest
) {
1642 ep2
= find_endpoint(dum
, w_index
);
1644 ret_val
= -EOPNOTSUPP
;
1647 buf
[0] = ep2
->halted
;
1648 } else if (setup
->bRequestType
==
1650 buf
[0] = (u8
)dum
->devstatus
;
1654 if (urb
->transfer_buffer_length
> 1)
1656 urb
->actual_length
= min_t(u32
, 2,
1657 urb
->transfer_buffer_length
);
1666 /* drive both sides of the transfers; looks like irq handlers to
1667 * both drivers except the callbacks aren't in_irq().
1669 static void dummy_timer(unsigned long _dum_hcd
)
1671 struct dummy_hcd
*dum_hcd
= (struct dummy_hcd
*) _dum_hcd
;
1672 struct dummy
*dum
= dum_hcd
->dum
;
1673 struct urbp
*urbp
, *tmp
;
1674 unsigned long flags
;
1678 /* simplistic model for one frame's bandwidth */
1679 switch (dum
->gadget
.speed
) {
1681 total
= 8/*bytes*/ * 12/*packets*/;
1683 case USB_SPEED_FULL
:
1684 total
= 64/*bytes*/ * 19/*packets*/;
1686 case USB_SPEED_HIGH
:
1687 total
= 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1689 case USB_SPEED_SUPER
:
1690 /* Bus speed is 500000 bytes/ms, so use a little less */
1694 dev_err(dummy_dev(dum_hcd
), "bogus device speed\n");
1698 /* FIXME if HZ != 1000 this will probably misbehave ... */
1700 /* look at each urb queued by the host side driver */
1701 spin_lock_irqsave(&dum
->lock
, flags
);
1703 if (!dum_hcd
->udev
) {
1704 dev_err(dummy_dev(dum_hcd
),
1705 "timer fired with no URBs pending?\n");
1706 spin_unlock_irqrestore(&dum
->lock
, flags
);
1710 for (i
= 0; i
< DUMMY_ENDPOINTS
; i
++) {
1713 dum
->ep
[i
].already_seen
= 0;
1717 list_for_each_entry_safe(urbp
, tmp
, &dum_hcd
->urbp_list
, urbp_list
) {
1719 struct dummy_request
*req
;
1721 struct dummy_ep
*ep
= NULL
;
1723 int status
= -EINPROGRESS
;
1728 else if (dum_hcd
->rh_state
!= DUMMY_RH_RUNNING
)
1730 type
= usb_pipetype(urb
->pipe
);
1732 /* used up this frame's non-periodic bandwidth?
1733 * FIXME there's infinite bandwidth for control and
1734 * periodic transfers ... unrealistic.
1736 if (total
<= 0 && type
== PIPE_BULK
)
1739 /* find the gadget's ep for this request (if configured) */
1740 address
= usb_pipeendpoint (urb
->pipe
);
1741 if (usb_pipein(urb
->pipe
))
1742 address
|= USB_DIR_IN
;
1743 ep
= find_endpoint(dum
, address
);
1745 /* set_configuration() disagreement */
1746 dev_dbg(dummy_dev(dum_hcd
),
1747 "no ep configured for urb %p\n",
1753 if (ep
->already_seen
)
1755 ep
->already_seen
= 1;
1756 if (ep
== &dum
->ep
[0] && urb
->error_count
) {
1757 ep
->setup_stage
= 1; /* a new urb */
1758 urb
->error_count
= 0;
1760 if (ep
->halted
&& !ep
->setup_stage
) {
1761 /* NOTE: must not be iso! */
1762 dev_dbg(dummy_dev(dum_hcd
), "ep %s halted, urb %p\n",
1767 /* FIXME make sure both ends agree on maxpacket */
1769 /* handle control requests */
1770 if (ep
== &dum
->ep
[0] && ep
->setup_stage
) {
1771 struct usb_ctrlrequest setup
;
1774 setup
= *(struct usb_ctrlrequest
*) urb
->setup_packet
;
1775 /* paranoia, in case of stale queued data */
1776 list_for_each_entry(req
, &ep
->queue
, queue
) {
1777 list_del_init(&req
->queue
);
1778 req
->req
.status
= -EOVERFLOW
;
1779 dev_dbg(udc_dev(dum
), "stale req = %p\n",
1782 spin_unlock(&dum
->lock
);
1783 req
->req
.complete(&ep
->ep
, &req
->req
);
1784 spin_lock(&dum
->lock
);
1785 ep
->already_seen
= 0;
1789 /* gadget driver never sees set_address or operations
1790 * on standard feature flags. some hardware doesn't
1793 ep
->last_io
= jiffies
;
1794 ep
->setup_stage
= 0;
1797 value
= handle_control_request(dum_hcd
, urb
, &setup
,
1800 /* gadget driver handles all other requests. block
1801 * until setup() returns; no reentrancy issues etc.
1804 spin_unlock(&dum
->lock
);
1805 value
= dum
->driver
->setup(&dum
->gadget
,
1807 spin_lock(&dum
->lock
);
1810 /* no delays (max 64KB data stage) */
1812 goto treat_control_like_bulk
;
1814 /* error, see below */
1818 if (value
!= -EOPNOTSUPP
)
1819 dev_dbg(udc_dev(dum
),
1823 urb
->actual_length
= 0;
1829 /* non-control requests */
1831 switch (usb_pipetype(urb
->pipe
)) {
1832 case PIPE_ISOCHRONOUS
:
1833 /* FIXME is it urb->interval since the last xfer?
1834 * use urb->iso_frame_desc[i].
1835 * complete whether or not ep has requests queued.
1836 * report random errors, to debug drivers.
1838 limit
= max(limit
, periodic_bytes(dum
, ep
));
1842 case PIPE_INTERRUPT
:
1843 /* FIXME is it urb->interval since the last xfer?
1844 * this almost certainly polls too fast.
1846 limit
= max(limit
, periodic_bytes(dum
, ep
));
1850 treat_control_like_bulk
:
1851 ep
->last_io
= jiffies
;
1852 total
= transfer(dum_hcd
, urb
, ep
, limit
, &status
);
1856 /* incomplete transfer? */
1857 if (status
== -EINPROGRESS
)
1861 list_del(&urbp
->urbp_list
);
1864 ep
->already_seen
= ep
->setup_stage
= 0;
1866 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd
), urb
);
1867 spin_unlock(&dum
->lock
);
1868 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd
), urb
, status
);
1869 spin_lock(&dum
->lock
);
1874 if (list_empty(&dum_hcd
->urbp_list
)) {
1875 usb_put_dev(dum_hcd
->udev
);
1876 dum_hcd
->udev
= NULL
;
1877 } else if (dum_hcd
->rh_state
== DUMMY_RH_RUNNING
) {
1878 /* want a 1 msec delay here */
1879 mod_timer(&dum_hcd
->timer
, jiffies
+ msecs_to_jiffies(1));
1882 spin_unlock_irqrestore(&dum
->lock
, flags
);
1885 /*-------------------------------------------------------------------------*/
1887 #define PORT_C_MASK \
1888 ((USB_PORT_STAT_C_CONNECTION \
1889 | USB_PORT_STAT_C_ENABLE \
1890 | USB_PORT_STAT_C_SUSPEND \
1891 | USB_PORT_STAT_C_OVERCURRENT \
1892 | USB_PORT_STAT_C_RESET) << 16)
1894 static int dummy_hub_status(struct usb_hcd
*hcd
, char *buf
)
1896 struct dummy_hcd
*dum_hcd
;
1897 unsigned long flags
;
1900 dum_hcd
= hcd_to_dummy_hcd(hcd
);
1902 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
1903 if (!HCD_HW_ACCESSIBLE(hcd
))
1906 if (dum_hcd
->resuming
&& time_after_eq(jiffies
, dum_hcd
->re_timeout
)) {
1907 dum_hcd
->port_status
|= (USB_PORT_STAT_C_SUSPEND
<< 16);
1908 dum_hcd
->port_status
&= ~USB_PORT_STAT_SUSPEND
;
1909 set_link_state(dum_hcd
);
1912 if ((dum_hcd
->port_status
& PORT_C_MASK
) != 0) {
1914 dev_dbg(dummy_dev(dum_hcd
), "port status 0x%08x has changes\n",
1915 dum_hcd
->port_status
);
1917 if (dum_hcd
->rh_state
== DUMMY_RH_SUSPENDED
)
1918 usb_hcd_resume_root_hub(hcd
);
1921 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
1925 /* usb 3.0 root hub device descriptor */
1927 struct usb_bos_descriptor bos
;
1928 struct usb_ss_cap_descriptor ss_cap
;
1929 } __packed usb3_bos_desc
= {
1932 .bLength
= USB_DT_BOS_SIZE
,
1933 .bDescriptorType
= USB_DT_BOS
,
1934 .wTotalLength
= cpu_to_le16(sizeof(usb3_bos_desc
)),
1935 .bNumDeviceCaps
= 1,
1938 .bLength
= USB_DT_USB_SS_CAP_SIZE
,
1939 .bDescriptorType
= USB_DT_DEVICE_CAPABILITY
,
1940 .bDevCapabilityType
= USB_SS_CAP_TYPE
,
1941 .wSpeedSupported
= cpu_to_le16(USB_5GBPS_OPERATION
),
1942 .bFunctionalitySupport
= ilog2(USB_5GBPS_OPERATION
),
1947 ss_hub_descriptor(struct usb_hub_descriptor
*desc
)
1949 memset(desc
, 0, sizeof *desc
);
1950 desc
->bDescriptorType
= 0x2a;
1951 desc
->bDescLength
= 12;
1952 desc
->wHubCharacteristics
= cpu_to_le16(0x0001);
1953 desc
->bNbrPorts
= 1;
1954 desc
->u
.ss
.bHubHdrDecLat
= 0x04; /* Worst case: 0.4 micro sec*/
1955 desc
->u
.ss
.DeviceRemovable
= 0xffff;
1958 static inline void hub_descriptor(struct usb_hub_descriptor
*desc
)
1960 memset(desc
, 0, sizeof *desc
);
1961 desc
->bDescriptorType
= 0x29;
1962 desc
->bDescLength
= 9;
1963 desc
->wHubCharacteristics
= cpu_to_le16(0x0001);
1964 desc
->bNbrPorts
= 1;
1965 desc
->u
.hs
.DeviceRemovable
[0] = 0xff;
1966 desc
->u
.hs
.DeviceRemovable
[1] = 0xff;
1969 static int dummy_hub_control(
1970 struct usb_hcd
*hcd
,
1977 struct dummy_hcd
*dum_hcd
;
1979 unsigned long flags
;
1981 if (!HCD_HW_ACCESSIBLE(hcd
))
1984 dum_hcd
= hcd_to_dummy_hcd(hcd
);
1986 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
1988 case ClearHubFeature
:
1990 case ClearPortFeature
:
1992 case USB_PORT_FEAT_SUSPEND
:
1993 if (hcd
->speed
== HCD_USB3
) {
1994 dev_dbg(dummy_dev(dum_hcd
),
1995 "USB_PORT_FEAT_SUSPEND req not "
1996 "supported for USB 3.0 roothub\n");
1999 if (dum_hcd
->port_status
& USB_PORT_STAT_SUSPEND
) {
2000 /* 20msec resume signaling */
2001 dum_hcd
->resuming
= 1;
2002 dum_hcd
->re_timeout
= jiffies
+
2003 msecs_to_jiffies(20);
2006 case USB_PORT_FEAT_POWER
:
2007 if (hcd
->speed
== HCD_USB3
) {
2008 if (dum_hcd
->port_status
& USB_PORT_STAT_POWER
)
2009 dev_dbg(dummy_dev(dum_hcd
),
2012 if (dum_hcd
->port_status
&
2013 USB_SS_PORT_STAT_POWER
)
2014 dev_dbg(dummy_dev(dum_hcd
),
2018 dum_hcd
->port_status
&= ~(1 << wValue
);
2019 set_link_state(dum_hcd
);
2022 case GetHubDescriptor
:
2023 if (hcd
->speed
== HCD_USB3
&&
2024 (wLength
< USB_DT_SS_HUB_SIZE
||
2025 wValue
!= (USB_DT_SS_HUB
<< 8))) {
2026 dev_dbg(dummy_dev(dum_hcd
),
2027 "Wrong hub descriptor type for "
2028 "USB 3.0 roothub.\n");
2031 if (hcd
->speed
== HCD_USB3
)
2032 ss_hub_descriptor((struct usb_hub_descriptor
*) buf
);
2034 hub_descriptor((struct usb_hub_descriptor
*) buf
);
2037 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
2038 if (hcd
->speed
!= HCD_USB3
)
2041 if ((wValue
>> 8) != USB_DT_BOS
)
2044 memcpy(buf
, &usb3_bos_desc
, sizeof(usb3_bos_desc
));
2045 retval
= sizeof(usb3_bos_desc
);
2049 *(__le32
*) buf
= cpu_to_le32(0);
2055 /* whoever resets or resumes must GetPortStatus to
2058 if (dum_hcd
->resuming
&&
2059 time_after_eq(jiffies
, dum_hcd
->re_timeout
)) {
2060 dum_hcd
->port_status
|= (USB_PORT_STAT_C_SUSPEND
<< 16);
2061 dum_hcd
->port_status
&= ~USB_PORT_STAT_SUSPEND
;
2063 if ((dum_hcd
->port_status
& USB_PORT_STAT_RESET
) != 0 &&
2064 time_after_eq(jiffies
, dum_hcd
->re_timeout
)) {
2065 dum_hcd
->port_status
|= (USB_PORT_STAT_C_RESET
<< 16);
2066 dum_hcd
->port_status
&= ~USB_PORT_STAT_RESET
;
2067 if (dum_hcd
->dum
->pullup
) {
2068 dum_hcd
->port_status
|= USB_PORT_STAT_ENABLE
;
2070 if (hcd
->speed
< HCD_USB3
) {
2071 switch (dum_hcd
->dum
->gadget
.speed
) {
2072 case USB_SPEED_HIGH
:
2073 dum_hcd
->port_status
|=
2074 USB_PORT_STAT_HIGH_SPEED
;
2077 dum_hcd
->dum
->gadget
.ep0
->
2079 dum_hcd
->port_status
|=
2080 USB_PORT_STAT_LOW_SPEED
;
2083 dum_hcd
->dum
->gadget
.speed
=
2090 set_link_state(dum_hcd
);
2091 ((__le16
*) buf
)[0] = cpu_to_le16(dum_hcd
->port_status
);
2092 ((__le16
*) buf
)[1] = cpu_to_le16(dum_hcd
->port_status
>> 16);
2097 case SetPortFeature
:
2099 case USB_PORT_FEAT_LINK_STATE
:
2100 if (hcd
->speed
!= HCD_USB3
) {
2101 dev_dbg(dummy_dev(dum_hcd
),
2102 "USB_PORT_FEAT_LINK_STATE req not "
2103 "supported for USB 2.0 roothub\n");
2107 * Since this is dummy we don't have an actual link so
2108 * there is nothing to do for the SET_LINK_STATE cmd
2111 case USB_PORT_FEAT_U1_TIMEOUT
:
2112 case USB_PORT_FEAT_U2_TIMEOUT
:
2113 /* TODO: add suspend/resume support! */
2114 if (hcd
->speed
!= HCD_USB3
) {
2115 dev_dbg(dummy_dev(dum_hcd
),
2116 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2117 "supported for USB 2.0 roothub\n");
2121 case USB_PORT_FEAT_SUSPEND
:
2122 /* Applicable only for USB2.0 hub */
2123 if (hcd
->speed
== HCD_USB3
) {
2124 dev_dbg(dummy_dev(dum_hcd
),
2125 "USB_PORT_FEAT_SUSPEND req not "
2126 "supported for USB 3.0 roothub\n");
2129 if (dum_hcd
->active
) {
2130 dum_hcd
->port_status
|= USB_PORT_STAT_SUSPEND
;
2132 /* HNP would happen here; for now we
2133 * assume b_bus_req is always true.
2135 set_link_state(dum_hcd
);
2136 if (((1 << USB_DEVICE_B_HNP_ENABLE
)
2137 & dum_hcd
->dum
->devstatus
) != 0)
2138 dev_dbg(dummy_dev(dum_hcd
),
2142 case USB_PORT_FEAT_POWER
:
2143 if (hcd
->speed
== HCD_USB3
)
2144 dum_hcd
->port_status
|= USB_SS_PORT_STAT_POWER
;
2146 dum_hcd
->port_status
|= USB_PORT_STAT_POWER
;
2147 set_link_state(dum_hcd
);
2149 case USB_PORT_FEAT_BH_PORT_RESET
:
2150 /* Applicable only for USB3.0 hub */
2151 if (hcd
->speed
!= HCD_USB3
) {
2152 dev_dbg(dummy_dev(dum_hcd
),
2153 "USB_PORT_FEAT_BH_PORT_RESET req not "
2154 "supported for USB 2.0 roothub\n");
2158 case USB_PORT_FEAT_RESET
:
2159 /* if it's already enabled, disable */
2160 if (hcd
->speed
== HCD_USB3
) {
2161 dum_hcd
->port_status
= 0;
2162 dum_hcd
->port_status
=
2163 (USB_SS_PORT_STAT_POWER
|
2164 USB_PORT_STAT_CONNECTION
|
2165 USB_PORT_STAT_RESET
);
2167 dum_hcd
->port_status
&= ~(USB_PORT_STAT_ENABLE
2168 | USB_PORT_STAT_LOW_SPEED
2169 | USB_PORT_STAT_HIGH_SPEED
);
2171 * We want to reset device status. All but the
2172 * Self powered feature
2174 dum_hcd
->dum
->devstatus
&=
2175 (1 << USB_DEVICE_SELF_POWERED
);
2177 * FIXME USB3.0: what is the correct reset signaling
2178 * interval? Is it still 50msec as for HS?
2180 dum_hcd
->re_timeout
= jiffies
+ msecs_to_jiffies(50);
2183 if (hcd
->speed
== HCD_USB3
) {
2184 if ((dum_hcd
->port_status
&
2185 USB_SS_PORT_STAT_POWER
) != 0) {
2186 dum_hcd
->port_status
|= (1 << wValue
);
2187 set_link_state(dum_hcd
);
2190 if ((dum_hcd
->port_status
&
2191 USB_PORT_STAT_POWER
) != 0) {
2192 dum_hcd
->port_status
|= (1 << wValue
);
2193 set_link_state(dum_hcd
);
2197 case GetPortErrorCount
:
2198 if (hcd
->speed
!= HCD_USB3
) {
2199 dev_dbg(dummy_dev(dum_hcd
),
2200 "GetPortErrorCount req not "
2201 "supported for USB 2.0 roothub\n");
2204 /* We'll always return 0 since this is a dummy hub */
2205 *(__le32
*) buf
= cpu_to_le32(0);
2208 if (hcd
->speed
!= HCD_USB3
) {
2209 dev_dbg(dummy_dev(dum_hcd
),
2210 "SetHubDepth req not supported for "
2211 "USB 2.0 roothub\n");
2216 dev_dbg(dummy_dev(dum_hcd
),
2217 "hub control req%04x v%04x i%04x l%d\n",
2218 typeReq
, wValue
, wIndex
, wLength
);
2220 /* "protocol stall" on error */
2223 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
2225 if ((dum_hcd
->port_status
& PORT_C_MASK
) != 0)
2226 usb_hcd_poll_rh_status(hcd
);
2230 static int dummy_bus_suspend(struct usb_hcd
*hcd
)
2232 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2234 dev_dbg(&hcd
->self
.root_hub
->dev
, "%s\n", __func__
);
2236 spin_lock_irq(&dum_hcd
->dum
->lock
);
2237 dum_hcd
->rh_state
= DUMMY_RH_SUSPENDED
;
2238 set_link_state(dum_hcd
);
2239 hcd
->state
= HC_STATE_SUSPENDED
;
2240 spin_unlock_irq(&dum_hcd
->dum
->lock
);
2244 static int dummy_bus_resume(struct usb_hcd
*hcd
)
2246 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2249 dev_dbg(&hcd
->self
.root_hub
->dev
, "%s\n", __func__
);
2251 spin_lock_irq(&dum_hcd
->dum
->lock
);
2252 if (!HCD_HW_ACCESSIBLE(hcd
)) {
2255 dum_hcd
->rh_state
= DUMMY_RH_RUNNING
;
2256 set_link_state(dum_hcd
);
2257 if (!list_empty(&dum_hcd
->urbp_list
))
2258 mod_timer(&dum_hcd
->timer
, jiffies
);
2259 hcd
->state
= HC_STATE_RUNNING
;
2261 spin_unlock_irq(&dum_hcd
->dum
->lock
);
2265 /*-------------------------------------------------------------------------*/
2267 static inline ssize_t
show_urb(char *buf
, size_t size
, struct urb
*urb
)
2269 int ep
= usb_pipeendpoint(urb
->pipe
);
2271 return snprintf(buf
, size
,
2272 "urb/%p %s ep%d%s%s len %d/%d\n",
2275 switch (urb
->dev
->speed
) {
2279 case USB_SPEED_FULL
:
2282 case USB_SPEED_HIGH
:
2285 case USB_SPEED_SUPER
:
2292 ep
, ep
? (usb_pipein(urb
->pipe
) ? "in" : "out") : "",
2294 switch (usb_pipetype(urb
->pipe
)) { \
2295 case PIPE_CONTROL
: \
2301 case PIPE_INTERRUPT
: \
2308 urb
->actual_length
, urb
->transfer_buffer_length
);
2311 static ssize_t
show_urbs(struct device
*dev
, struct device_attribute
*attr
,
2314 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
2315 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2318 unsigned long flags
;
2320 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
2321 list_for_each_entry(urbp
, &dum_hcd
->urbp_list
, urbp_list
) {
2324 temp
= show_urb(buf
, PAGE_SIZE
- size
, urbp
->urb
);
2328 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
2332 static DEVICE_ATTR(urbs
, S_IRUGO
, show_urbs
, NULL
);
2334 static int dummy_start_ss(struct dummy_hcd
*dum_hcd
)
2336 init_timer(&dum_hcd
->timer
);
2337 dum_hcd
->timer
.function
= dummy_timer
;
2338 dum_hcd
->timer
.data
= (unsigned long)dum_hcd
;
2339 dum_hcd
->rh_state
= DUMMY_RH_RUNNING
;
2340 dum_hcd
->stream_en_ep
= 0;
2341 INIT_LIST_HEAD(&dum_hcd
->urbp_list
);
2342 dummy_hcd_to_hcd(dum_hcd
)->power_budget
= POWER_BUDGET
;
2343 dummy_hcd_to_hcd(dum_hcd
)->state
= HC_STATE_RUNNING
;
2344 dummy_hcd_to_hcd(dum_hcd
)->uses_new_polling
= 1;
2345 #ifdef CONFIG_USB_OTG
2346 dummy_hcd_to_hcd(dum_hcd
)->self
.otg_port
= 1;
2350 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2351 return device_create_file(dummy_dev(dum_hcd
), &dev_attr_urbs
);
2354 static int dummy_start(struct usb_hcd
*hcd
)
2356 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2359 * MASTER side init ... we emulate a root hub that'll only ever
2360 * talk to one device (the slave side). Also appears in sysfs,
2361 * just like more familiar pci-based HCDs.
2363 if (!usb_hcd_is_primary_hcd(hcd
))
2364 return dummy_start_ss(dum_hcd
);
2366 spin_lock_init(&dum_hcd
->dum
->lock
);
2367 init_timer(&dum_hcd
->timer
);
2368 dum_hcd
->timer
.function
= dummy_timer
;
2369 dum_hcd
->timer
.data
= (unsigned long)dum_hcd
;
2370 dum_hcd
->rh_state
= DUMMY_RH_RUNNING
;
2372 INIT_LIST_HEAD(&dum_hcd
->urbp_list
);
2374 hcd
->power_budget
= POWER_BUDGET
;
2375 hcd
->state
= HC_STATE_RUNNING
;
2376 hcd
->uses_new_polling
= 1;
2378 #ifdef CONFIG_USB_OTG
2379 hcd
->self
.otg_port
= 1;
2382 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2383 return device_create_file(dummy_dev(dum_hcd
), &dev_attr_urbs
);
2386 static void dummy_stop(struct usb_hcd
*hcd
)
2390 dum
= hcd_to_dummy_hcd(hcd
)->dum
;
2391 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd
)), &dev_attr_urbs
);
2392 usb_gadget_unregister_driver(dum
->driver
);
2393 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd
)), "stopped\n");
2396 /*-------------------------------------------------------------------------*/
2398 static int dummy_h_get_frame(struct usb_hcd
*hcd
)
2400 return dummy_g_get_frame(NULL
);
2403 static int dummy_setup(struct usb_hcd
*hcd
)
2407 dum
= *((void **)dev_get_platdata(hcd
->self
.controller
));
2408 hcd
->self
.sg_tablesize
= ~0;
2409 if (usb_hcd_is_primary_hcd(hcd
)) {
2410 dum
->hs_hcd
= hcd_to_dummy_hcd(hcd
);
2411 dum
->hs_hcd
->dum
= dum
;
2413 * Mark the first roothub as being USB 2.0.
2414 * The USB 3.0 roothub will be registered later by
2417 hcd
->speed
= HCD_USB2
;
2418 hcd
->self
.root_hub
->speed
= USB_SPEED_HIGH
;
2420 dum
->ss_hcd
= hcd_to_dummy_hcd(hcd
);
2421 dum
->ss_hcd
->dum
= dum
;
2422 hcd
->speed
= HCD_USB3
;
2423 hcd
->self
.root_hub
->speed
= USB_SPEED_SUPER
;
2428 /* Change a group of bulk endpoints to support multiple stream IDs */
2429 static int dummy_alloc_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
2430 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
2431 unsigned int num_streams
, gfp_t mem_flags
)
2433 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2434 unsigned long flags
;
2436 int ret_streams
= num_streams
;
2443 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
2444 for (i
= 0; i
< num_eps
; i
++) {
2445 index
= dummy_get_ep_idx(&eps
[i
]->desc
);
2446 if ((1 << index
) & dum_hcd
->stream_en_ep
) {
2447 ret_streams
= -EINVAL
;
2450 max_stream
= usb_ss_max_streams(&eps
[i
]->ss_ep_comp
);
2452 ret_streams
= -EINVAL
;
2455 if (max_stream
< ret_streams
) {
2456 dev_dbg(dummy_dev(dum_hcd
), "Ep 0x%x only supports %u "
2458 eps
[i
]->desc
.bEndpointAddress
,
2460 ret_streams
= max_stream
;
2464 for (i
= 0; i
< num_eps
; i
++) {
2465 index
= dummy_get_ep_idx(&eps
[i
]->desc
);
2466 dum_hcd
->stream_en_ep
|= 1 << index
;
2467 set_max_streams_for_pipe(dum_hcd
,
2468 usb_endpoint_num(&eps
[i
]->desc
), ret_streams
);
2471 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
2475 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2476 static int dummy_free_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
2477 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
2480 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2481 unsigned long flags
;
2486 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
2487 for (i
= 0; i
< num_eps
; i
++) {
2488 index
= dummy_get_ep_idx(&eps
[i
]->desc
);
2489 if (!((1 << index
) & dum_hcd
->stream_en_ep
)) {
2495 for (i
= 0; i
< num_eps
; i
++) {
2496 index
= dummy_get_ep_idx(&eps
[i
]->desc
);
2497 dum_hcd
->stream_en_ep
&= ~(1 << index
);
2498 set_max_streams_for_pipe(dum_hcd
,
2499 usb_endpoint_num(&eps
[i
]->desc
), 0);
2503 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
2507 static struct hc_driver dummy_hcd
= {
2508 .description
= (char *) driver_name
,
2509 .product_desc
= "Dummy host controller",
2510 .hcd_priv_size
= sizeof(struct dummy_hcd
),
2512 .flags
= HCD_USB3
| HCD_SHARED
,
2514 .reset
= dummy_setup
,
2515 .start
= dummy_start
,
2518 .urb_enqueue
= dummy_urb_enqueue
,
2519 .urb_dequeue
= dummy_urb_dequeue
,
2521 .get_frame_number
= dummy_h_get_frame
,
2523 .hub_status_data
= dummy_hub_status
,
2524 .hub_control
= dummy_hub_control
,
2525 .bus_suspend
= dummy_bus_suspend
,
2526 .bus_resume
= dummy_bus_resume
,
2528 .alloc_streams
= dummy_alloc_streams
,
2529 .free_streams
= dummy_free_streams
,
2532 static int dummy_hcd_probe(struct platform_device
*pdev
)
2535 struct usb_hcd
*hs_hcd
;
2536 struct usb_hcd
*ss_hcd
;
2539 dev_info(&pdev
->dev
, "%s, driver " DRIVER_VERSION
"\n", driver_desc
);
2540 dum
= *((void **)dev_get_platdata(&pdev
->dev
));
2542 if (!mod_data
.is_super_speed
)
2543 dummy_hcd
.flags
= HCD_USB2
;
2544 hs_hcd
= usb_create_hcd(&dummy_hcd
, &pdev
->dev
, dev_name(&pdev
->dev
));
2549 retval
= usb_add_hcd(hs_hcd
, 0, 0);
2553 if (mod_data
.is_super_speed
) {
2554 ss_hcd
= usb_create_shared_hcd(&dummy_hcd
, &pdev
->dev
,
2555 dev_name(&pdev
->dev
), hs_hcd
);
2558 goto dealloc_usb2_hcd
;
2561 retval
= usb_add_hcd(ss_hcd
, 0, 0);
2568 usb_put_hcd(ss_hcd
);
2570 usb_remove_hcd(hs_hcd
);
2572 usb_put_hcd(hs_hcd
);
2573 dum
->hs_hcd
= dum
->ss_hcd
= NULL
;
2577 static int dummy_hcd_remove(struct platform_device
*pdev
)
2581 dum
= hcd_to_dummy_hcd(platform_get_drvdata(pdev
))->dum
;
2584 usb_remove_hcd(dummy_hcd_to_hcd(dum
->ss_hcd
));
2585 usb_put_hcd(dummy_hcd_to_hcd(dum
->ss_hcd
));
2588 usb_remove_hcd(dummy_hcd_to_hcd(dum
->hs_hcd
));
2589 usb_put_hcd(dummy_hcd_to_hcd(dum
->hs_hcd
));
2597 static int dummy_hcd_suspend(struct platform_device
*pdev
, pm_message_t state
)
2599 struct usb_hcd
*hcd
;
2600 struct dummy_hcd
*dum_hcd
;
2603 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
2605 hcd
= platform_get_drvdata(pdev
);
2606 dum_hcd
= hcd_to_dummy_hcd(hcd
);
2607 if (dum_hcd
->rh_state
== DUMMY_RH_RUNNING
) {
2608 dev_warn(&pdev
->dev
, "Root hub isn't suspended!\n");
2611 clear_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
2615 static int dummy_hcd_resume(struct platform_device
*pdev
)
2617 struct usb_hcd
*hcd
;
2619 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
2621 hcd
= platform_get_drvdata(pdev
);
2622 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
2623 usb_hcd_poll_rh_status(hcd
);
2627 static struct platform_driver dummy_hcd_driver
= {
2628 .probe
= dummy_hcd_probe
,
2629 .remove
= dummy_hcd_remove
,
2630 .suspend
= dummy_hcd_suspend
,
2631 .resume
= dummy_hcd_resume
,
2633 .name
= (char *) driver_name
,
2634 .owner
= THIS_MODULE
,
2638 /*-------------------------------------------------------------------------*/
2639 #define MAX_NUM_UDC 2
2640 static struct platform_device
*the_udc_pdev
[MAX_NUM_UDC
];
2641 static struct platform_device
*the_hcd_pdev
[MAX_NUM_UDC
];
2643 static int __init
init(void)
2645 int retval
= -ENOMEM
;
2647 struct dummy
*dum
[MAX_NUM_UDC
];
2652 if (!mod_data
.is_high_speed
&& mod_data
.is_super_speed
)
2655 if (mod_data
.num
< 1 || mod_data
.num
> MAX_NUM_UDC
) {
2656 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2661 for (i
= 0; i
< mod_data
.num
; i
++) {
2662 the_hcd_pdev
[i
] = platform_device_alloc(driver_name
, i
);
2663 if (!the_hcd_pdev
[i
]) {
2666 platform_device_put(the_hcd_pdev
[i
--]);
2670 for (i
= 0; i
< mod_data
.num
; i
++) {
2671 the_udc_pdev
[i
] = platform_device_alloc(gadget_name
, i
);
2672 if (!the_udc_pdev
[i
]) {
2675 platform_device_put(the_udc_pdev
[i
--]);
2679 for (i
= 0; i
< mod_data
.num
; i
++) {
2680 dum
[i
] = kzalloc(sizeof(struct dummy
), GFP_KERNEL
);
2683 retval
= platform_device_add_data(the_hcd_pdev
[i
], &dum
[i
],
2687 retval
= platform_device_add_data(the_udc_pdev
[i
], &dum
[i
],
2693 retval
= platform_driver_register(&dummy_hcd_driver
);
2696 retval
= platform_driver_register(&dummy_udc_driver
);
2698 goto err_register_udc_driver
;
2700 for (i
= 0; i
< mod_data
.num
; i
++) {
2701 retval
= platform_device_add(the_hcd_pdev
[i
]);
2705 platform_device_del(the_hcd_pdev
[i
--]);
2709 for (i
= 0; i
< mod_data
.num
; i
++) {
2710 if (!dum
[i
]->hs_hcd
||
2711 (!dum
[i
]->ss_hcd
&& mod_data
.is_super_speed
)) {
2713 * The hcd was added successfully but its probe
2714 * function failed for some reason.
2721 for (i
= 0; i
< mod_data
.num
; i
++) {
2722 retval
= platform_device_add(the_udc_pdev
[i
]);
2726 platform_device_del(the_udc_pdev
[i
]);
2731 for (i
= 0; i
< mod_data
.num
; i
++) {
2732 if (!platform_get_drvdata(the_udc_pdev
[i
])) {
2734 * The udc was added successfully but its probe
2735 * function failed for some reason.
2744 for (i
= 0; i
< mod_data
.num
; i
++)
2745 platform_device_del(the_udc_pdev
[i
]);
2747 for (i
= 0; i
< mod_data
.num
; i
++)
2748 platform_device_del(the_hcd_pdev
[i
]);
2750 platform_driver_unregister(&dummy_udc_driver
);
2751 err_register_udc_driver
:
2752 platform_driver_unregister(&dummy_hcd_driver
);
2754 for (i
= 0; i
< mod_data
.num
; i
++)
2756 for (i
= 0; i
< mod_data
.num
; i
++)
2757 platform_device_put(the_udc_pdev
[i
]);
2759 for (i
= 0; i
< mod_data
.num
; i
++)
2760 platform_device_put(the_hcd_pdev
[i
]);
2765 static void __exit
cleanup(void)
2769 for (i
= 0; i
< mod_data
.num
; i
++) {
2772 dum
= *((void **)dev_get_platdata(&the_udc_pdev
[i
]->dev
));
2774 platform_device_unregister(the_udc_pdev
[i
]);
2775 platform_device_unregister(the_hcd_pdev
[i
]);
2778 platform_driver_unregister(&dummy_udc_driver
);
2779 platform_driver_unregister(&dummy_hcd_driver
);
2781 module_exit(cleanup
);