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/system.h>
48 #include <asm/unaligned.h>
50 #define DRIVER_DESC "USB Host+Gadget Emulator"
51 #define DRIVER_VERSION "02 May 2005"
53 #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
55 static const char driver_name
[] = "dummy_hcd";
56 static const char driver_desc
[] = "USB Host+Gadget Emulator";
58 static const char gadget_name
[] = "dummy_udc";
60 MODULE_DESCRIPTION(DRIVER_DESC
);
61 MODULE_AUTHOR("David Brownell");
62 MODULE_LICENSE("GPL");
64 struct dummy_hcd_module_parameters
{
69 static struct dummy_hcd_module_parameters mod_data
= {
70 .is_super_speed
= false,
71 .is_high_speed
= true,
73 module_param_named(is_super_speed
, mod_data
.is_super_speed
, bool, S_IRUGO
);
74 MODULE_PARM_DESC(is_super_speed
, "true to simulate SuperSpeed connection");
75 module_param_named(is_high_speed
, mod_data
.is_high_speed
, bool, S_IRUGO
);
76 MODULE_PARM_DESC(is_high_speed
, "true to simulate HighSpeed connection");
77 /*-------------------------------------------------------------------------*/
79 /* gadget side driver data structres */
81 struct list_head queue
;
82 unsigned long last_io
; /* jiffies timestamp */
83 struct usb_gadget
*gadget
;
84 const struct usb_endpoint_descriptor
*desc
;
88 unsigned already_seen
:1;
89 unsigned setup_stage
:1;
93 struct dummy_request
{
94 struct list_head queue
; /* ep's requests */
95 struct usb_request req
;
98 static inline struct dummy_ep
*usb_ep_to_dummy_ep(struct usb_ep
*_ep
)
100 return container_of(_ep
, struct dummy_ep
, ep
);
103 static inline struct dummy_request
*usb_request_to_dummy_request
104 (struct usb_request
*_req
)
106 return container_of(_req
, struct dummy_request
, req
);
109 /*-------------------------------------------------------------------------*/
112 * Every device has ep0 for control requests, plus up to 30 more endpoints,
113 * in one of two types:
115 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
116 * number can be changed. Names like "ep-a" are used for this type.
118 * - Fixed Function: in other cases. some characteristics may be mutable;
119 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
121 * Gadget drivers are responsible for not setting up conflicting endpoint
122 * configurations, illegal or unsupported packet lengths, and so on.
125 static const char ep0name
[] = "ep0";
127 static const char *const ep_name
[] = {
128 ep0name
, /* everyone has ep0 */
130 /* act like a net2280: high speed, six configurable endpoints */
131 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
133 /* or like 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 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
144 /*-------------------------------------------------------------------------*/
150 struct list_head urbp_list
;
151 struct sg_mapping_iter miter
;
156 enum dummy_rh_state
{
164 enum dummy_rh_state rh_state
;
165 struct timer_list timer
;
168 unsigned long re_timeout
;
170 struct usb_device
*udev
;
171 struct list_head urbp_list
;
173 u8 num_stream
[30 / 2];
176 unsigned old_active
:1;
184 * SLAVE/GADGET side support
186 struct dummy_ep ep
[DUMMY_ENDPOINTS
];
188 struct usb_gadget gadget
;
189 struct usb_gadget_driver
*driver
;
190 struct dummy_request fifo_req
;
191 u8 fifo_buf
[FIFO_SIZE
];
193 unsigned udc_suspended
:1;
197 * MASTER/HOST side support
199 struct dummy_hcd
*hs_hcd
;
200 struct dummy_hcd
*ss_hcd
;
203 static inline struct dummy_hcd
*hcd_to_dummy_hcd(struct usb_hcd
*hcd
)
205 return (struct dummy_hcd
*) (hcd
->hcd_priv
);
208 static inline struct usb_hcd
*dummy_hcd_to_hcd(struct dummy_hcd
*dum
)
210 return container_of((void *) dum
, struct usb_hcd
, hcd_priv
);
213 static inline struct device
*dummy_dev(struct dummy_hcd
*dum
)
215 return dummy_hcd_to_hcd(dum
)->self
.controller
;
218 static inline struct device
*udc_dev(struct dummy
*dum
)
220 return dum
->gadget
.dev
.parent
;
223 static inline struct dummy
*ep_to_dummy(struct dummy_ep
*ep
)
225 return container_of(ep
->gadget
, struct dummy
, gadget
);
228 static inline struct dummy_hcd
*gadget_to_dummy_hcd(struct usb_gadget
*gadget
)
230 struct dummy
*dum
= container_of(gadget
, struct dummy
, gadget
);
231 if (dum
->gadget
.speed
== USB_SPEED_SUPER
)
237 static inline struct dummy
*gadget_dev_to_dummy(struct device
*dev
)
239 return container_of(dev
, struct dummy
, gadget
.dev
);
242 static struct dummy the_controller
;
244 /*-------------------------------------------------------------------------*/
246 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
248 /* called with spinlock held */
249 static void nuke(struct dummy
*dum
, struct dummy_ep
*ep
)
251 while (!list_empty(&ep
->queue
)) {
252 struct dummy_request
*req
;
254 req
= list_entry(ep
->queue
.next
, struct dummy_request
, queue
);
255 list_del_init(&req
->queue
);
256 req
->req
.status
= -ESHUTDOWN
;
258 spin_unlock(&dum
->lock
);
259 req
->req
.complete(&ep
->ep
, &req
->req
);
260 spin_lock(&dum
->lock
);
264 /* caller must hold lock */
265 static void stop_activity(struct dummy
*dum
)
269 /* prevent any more requests */
272 /* The timer is left running so that outstanding URBs can fail */
274 /* nuke any pending requests first, so driver i/o is quiesced */
275 list_for_each_entry(ep
, &dum
->gadget
.ep_list
, ep
.ep_list
)
278 /* driver now does any non-usb quiescing necessary */
282 * set_link_state_by_speed() - Sets the current state of the link according to
284 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
286 * This function updates the port_status according to the link state and the
289 static void set_link_state_by_speed(struct dummy_hcd
*dum_hcd
)
291 struct dummy
*dum
= dum_hcd
->dum
;
293 if (dummy_hcd_to_hcd(dum_hcd
)->speed
== HCD_USB3
) {
294 if ((dum_hcd
->port_status
& USB_SS_PORT_STAT_POWER
) == 0) {
295 dum_hcd
->port_status
= 0;
296 } else if (!dum
->pullup
|| dum
->udc_suspended
) {
297 /* UDC suspend must cause a disconnect */
298 dum_hcd
->port_status
&= ~(USB_PORT_STAT_CONNECTION
|
299 USB_PORT_STAT_ENABLE
);
300 if ((dum_hcd
->old_status
&
301 USB_PORT_STAT_CONNECTION
) != 0)
302 dum_hcd
->port_status
|=
303 (USB_PORT_STAT_C_CONNECTION
<< 16);
305 /* device is connected and not suspended */
306 dum_hcd
->port_status
|= (USB_PORT_STAT_CONNECTION
|
307 USB_PORT_STAT_SPEED_5GBPS
) ;
308 if ((dum_hcd
->old_status
&
309 USB_PORT_STAT_CONNECTION
) == 0)
310 dum_hcd
->port_status
|=
311 (USB_PORT_STAT_C_CONNECTION
<< 16);
312 if ((dum_hcd
->port_status
&
313 USB_PORT_STAT_ENABLE
) == 1 &&
314 (dum_hcd
->port_status
&
315 USB_SS_PORT_LS_U0
) == 1 &&
316 dum_hcd
->rh_state
!= DUMMY_RH_SUSPENDED
)
320 if ((dum_hcd
->port_status
& USB_PORT_STAT_POWER
) == 0) {
321 dum_hcd
->port_status
= 0;
322 } else if (!dum
->pullup
|| dum
->udc_suspended
) {
323 /* UDC suspend must cause a disconnect */
324 dum_hcd
->port_status
&= ~(USB_PORT_STAT_CONNECTION
|
325 USB_PORT_STAT_ENABLE
|
326 USB_PORT_STAT_LOW_SPEED
|
327 USB_PORT_STAT_HIGH_SPEED
|
328 USB_PORT_STAT_SUSPEND
);
329 if ((dum_hcd
->old_status
&
330 USB_PORT_STAT_CONNECTION
) != 0)
331 dum_hcd
->port_status
|=
332 (USB_PORT_STAT_C_CONNECTION
<< 16);
334 dum_hcd
->port_status
|= USB_PORT_STAT_CONNECTION
;
335 if ((dum_hcd
->old_status
&
336 USB_PORT_STAT_CONNECTION
) == 0)
337 dum_hcd
->port_status
|=
338 (USB_PORT_STAT_C_CONNECTION
<< 16);
339 if ((dum_hcd
->port_status
& USB_PORT_STAT_ENABLE
) == 0)
340 dum_hcd
->port_status
&= ~USB_PORT_STAT_SUSPEND
;
341 else if ((dum_hcd
->port_status
&
342 USB_PORT_STAT_SUSPEND
) == 0 &&
343 dum_hcd
->rh_state
!= DUMMY_RH_SUSPENDED
)
349 /* caller must hold lock */
350 static void set_link_state(struct dummy_hcd
*dum_hcd
)
352 struct dummy
*dum
= dum_hcd
->dum
;
356 if ((dummy_hcd_to_hcd(dum_hcd
)->speed
== HCD_USB3
&&
357 dum
->gadget
.speed
!= USB_SPEED_SUPER
) ||
358 (dummy_hcd_to_hcd(dum_hcd
)->speed
!= HCD_USB3
&&
359 dum
->gadget
.speed
== USB_SPEED_SUPER
))
362 set_link_state_by_speed(dum_hcd
);
364 if ((dum_hcd
->port_status
& USB_PORT_STAT_ENABLE
) == 0 ||
366 dum_hcd
->resuming
= 0;
368 /* if !connected or reset */
369 if ((dum_hcd
->port_status
& USB_PORT_STAT_CONNECTION
) == 0 ||
370 (dum_hcd
->port_status
& USB_PORT_STAT_RESET
) != 0) {
372 * We're connected and not reset (reset occurred now),
373 * and driver attached - disconnect!
375 if ((dum_hcd
->old_status
& USB_PORT_STAT_CONNECTION
) != 0 &&
376 (dum_hcd
->old_status
& USB_PORT_STAT_RESET
) == 0 &&
379 spin_unlock(&dum
->lock
);
380 dum
->driver
->disconnect(&dum
->gadget
);
381 spin_lock(&dum
->lock
);
383 } else if (dum_hcd
->active
!= dum_hcd
->old_active
) {
384 if (dum_hcd
->old_active
&& dum
->driver
->suspend
) {
385 spin_unlock(&dum
->lock
);
386 dum
->driver
->suspend(&dum
->gadget
);
387 spin_lock(&dum
->lock
);
388 } else if (!dum_hcd
->old_active
&& dum
->driver
->resume
) {
389 spin_unlock(&dum
->lock
);
390 dum
->driver
->resume(&dum
->gadget
);
391 spin_lock(&dum
->lock
);
395 dum_hcd
->old_status
= dum_hcd
->port_status
;
396 dum_hcd
->old_active
= dum_hcd
->active
;
399 /*-------------------------------------------------------------------------*/
401 /* SLAVE/GADGET SIDE DRIVER
403 * This only tracks gadget state. All the work is done when the host
404 * side tries some (emulated) i/o operation. Real device controller
405 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
408 #define is_enabled(dum) \
409 (dum->port_status & USB_PORT_STAT_ENABLE)
411 static int dummy_enable(struct usb_ep
*_ep
,
412 const struct usb_endpoint_descriptor
*desc
)
415 struct dummy_hcd
*dum_hcd
;
420 ep
= usb_ep_to_dummy_ep(_ep
);
421 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
422 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
424 dum
= ep_to_dummy(ep
);
428 dum_hcd
= gadget_to_dummy_hcd(&dum
->gadget
);
429 if (!is_enabled(dum_hcd
))
433 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
434 * maximum packet size.
435 * For SS devices the wMaxPacketSize is limited by 1024.
437 max
= usb_endpoint_maxp(desc
) & 0x7ff;
439 /* drivers must not request bad settings, since lower levels
440 * (hardware or its drivers) may not check. some endpoints
441 * can't do iso, many have maxpacket limitations, etc.
443 * since this "hardware" driver is here to help debugging, we
444 * have some extra sanity checks. (there could be more though,
445 * especially for "ep9out" style fixed function ones.)
448 switch (usb_endpoint_type(desc
)) {
449 case USB_ENDPOINT_XFER_BULK
:
450 if (strstr(ep
->ep
.name
, "-iso")
451 || strstr(ep
->ep
.name
, "-int")) {
454 switch (dum
->gadget
.speed
) {
455 case USB_SPEED_SUPER
:
464 if (max
== 8 || max
== 16 || max
== 32 || max
== 64)
465 /* we'll fake any legal size */
467 /* save a return statement */
472 case USB_ENDPOINT_XFER_INT
:
473 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
475 /* real hardware might not handle all packet sizes */
476 switch (dum
->gadget
.speed
) {
477 case USB_SPEED_SUPER
:
481 /* save a return statement */
485 /* save a return statement */
492 case USB_ENDPOINT_XFER_ISOC
:
493 if (strstr(ep
->ep
.name
, "-bulk")
494 || strstr(ep
->ep
.name
, "-int"))
496 /* real hardware might not handle all packet sizes */
497 switch (dum
->gadget
.speed
) {
498 case USB_SPEED_SUPER
:
502 /* save a return statement */
506 /* save a return statement */
512 /* few chips support control except on ep0 */
516 _ep
->maxpacket
= max
;
518 if (usb_ss_max_streams(_ep
->comp_desc
)) {
519 if (!usb_endpoint_xfer_bulk(desc
)) {
520 dev_err(udc_dev(dum
), "Can't enable stream support on "
521 "non-bulk ep %s\n", _ep
->name
);
527 dev_dbg(udc_dev(dum
), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
529 desc
->bEndpointAddress
& 0x0f,
530 (desc
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out",
532 switch (usb_endpoint_type(desc
)) {
533 case USB_ENDPOINT_XFER_BULK
:
536 case USB_ENDPOINT_XFER_ISOC
:
539 case USB_ENDPOINT_XFER_INT
:
546 max
, ep
->stream_en
? "enabled" : "disabled");
548 /* at this point real hardware should be NAKing transfers
549 * to that endpoint, until a buffer is queued to it.
551 ep
->halted
= ep
->wedged
= 0;
557 static int dummy_disable(struct usb_ep
*_ep
)
564 ep
= usb_ep_to_dummy_ep(_ep
);
565 if (!_ep
|| !ep
->desc
|| _ep
->name
== ep0name
)
567 dum
= ep_to_dummy(ep
);
569 spin_lock_irqsave(&dum
->lock
, flags
);
574 spin_unlock_irqrestore(&dum
->lock
, flags
);
576 dev_dbg(udc_dev(dum
), "disabled %s\n", _ep
->name
);
580 static struct usb_request
*dummy_alloc_request(struct usb_ep
*_ep
,
584 struct dummy_request
*req
;
588 ep
= usb_ep_to_dummy_ep(_ep
);
590 req
= kzalloc(sizeof(*req
), mem_flags
);
593 INIT_LIST_HEAD(&req
->queue
);
597 static void dummy_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
600 struct dummy_request
*req
;
602 ep
= usb_ep_to_dummy_ep(_ep
);
603 if (!ep
|| !_req
|| (!ep
->desc
&& _ep
->name
!= ep0name
))
606 req
= usb_request_to_dummy_request(_req
);
607 WARN_ON(!list_empty(&req
->queue
));
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
,
619 struct dummy_request
*req
;
621 struct dummy_hcd
*dum_hcd
;
624 req
= usb_request_to_dummy_request(_req
);
625 if (!_req
|| !list_empty(&req
->queue
) || !_req
->complete
)
628 ep
= usb_ep_to_dummy_ep(_ep
);
629 if (!_ep
|| (!ep
->desc
&& _ep
->name
!= ep0name
))
632 dum
= ep_to_dummy(ep
);
633 dum_hcd
= gadget_to_dummy_hcd(&dum
->gadget
);
634 if (!dum
->driver
|| !is_enabled(dum_hcd
))
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
);
641 _req
->status
= -EINPROGRESS
;
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
;
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
;
661 _req
->complete(_ep
, _req
);
662 spin_lock(&dum
->lock
);
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.
673 static int dummy_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
677 int retval
= -EINVAL
;
679 struct dummy_request
*req
= NULL
;
683 ep
= usb_ep_to_dummy_ep(_ep
);
684 dum
= ep_to_dummy(ep
);
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
;
699 spin_unlock(&dum
->lock
);
702 dev_dbg(udc_dev(dum
),
703 "dequeued req %p from %s, len %d buf %p\n",
704 req
, _ep
->name
, _req
->length
, _req
->buf
);
705 _req
->complete(_ep
, _req
);
707 local_irq_restore(flags
);
712 dummy_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
719 ep
= usb_ep_to_dummy_ep(_ep
);
720 dum
= ep_to_dummy(ep
);
724 ep
->halted
= ep
->wedged
= 0;
725 else if (ep
->desc
&& (ep
->desc
->bEndpointAddress
& USB_DIR_IN
) &&
726 !list_empty(&ep
->queue
))
733 /* FIXME clear emulated data toggle too */
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
)
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
)
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
))))
783 if ((dum_hcd
->port_status
& USB_PORT_STAT_CONNECTION
) == 0)
785 if ((dum_hcd
->port_status
& USB_PORT_STAT_SUSPEND
) == 0 &&
786 dum_hcd
->rh_state
!= DUMMY_RH_SUSPENDED
)
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
);
798 static int dummy_set_selfpowered(struct usb_gadget
*_gadget
, int value
)
802 dum
= gadget_to_dummy_hcd(_gadget
)->dum
;
804 dum
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
806 dum
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
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;
815 dum
->ep
[0].ep
.maxpacket
= 64;
818 static int dummy_pullup(struct usb_gadget
*_gadget
, int value
)
820 struct dummy_hcd
*dum_hcd
;
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
);
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
));
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
show_function(struct device
*dev
, struct device_attribute
*attr
,
872 struct dummy
*dum
= gadget_dev_to_dummy(dev
);
874 if (!dum
->driver
|| !dum
->driver
->function
)
876 return scnprintf(buf
, PAGE_SIZE
, "%s\n", dum
->driver
->function
);
878 static DEVICE_ATTR(function
, S_IRUGO
, show_function
, NULL
);
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
)
906 * SLAVE side init ... the layer above hardware, which
907 * can't enumerate without help from the driver we're binding.
912 dum
->driver
= driver
;
913 dev_dbg(udc_dev(dum
), "binding gadget driver '%s'\n",
914 driver
->driver
.name
);
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 driver
->driver
.name
);
929 dummy_pullup(&dum
->gadget
, 0);
935 /* The gadget structure is stored inside the hcd structure and will be
936 * released along with it. */
937 static void dummy_gadget_release(struct device
*dev
)
942 static void init_dummy_udc_hw(struct dummy
*dum
)
946 INIT_LIST_HEAD(&dum
->gadget
.ep_list
);
947 for (i
= 0; i
< DUMMY_ENDPOINTS
; i
++) {
948 struct dummy_ep
*ep
= &dum
->ep
[i
];
952 ep
->ep
.name
= ep_name
[i
];
953 ep
->ep
.ops
= &dummy_ep_ops
;
954 list_add_tail(&ep
->ep
.ep_list
, &dum
->gadget
.ep_list
);
955 ep
->halted
= ep
->wedged
= ep
->already_seen
=
957 ep
->ep
.maxpacket
= ~0;
958 ep
->ep
.max_streams
= 16;
959 ep
->last_io
= jiffies
;
960 ep
->gadget
= &dum
->gadget
;
962 INIT_LIST_HEAD(&ep
->queue
);
965 dum
->gadget
.ep0
= &dum
->ep
[0].ep
;
966 list_del_init(&dum
->ep
[0].ep
.ep_list
);
967 INIT_LIST_HEAD(&dum
->fifo_req
.queue
);
969 #ifdef CONFIG_USB_OTG
970 dum
->gadget
.is_otg
= 1;
974 static int dummy_udc_probe(struct platform_device
*pdev
)
976 struct dummy
*dum
= &the_controller
;
979 dum
->gadget
.name
= gadget_name
;
980 dum
->gadget
.ops
= &dummy_ops
;
981 dum
->gadget
.max_speed
= USB_SPEED_SUPER
;
983 dev_set_name(&dum
->gadget
.dev
, "gadget");
984 dum
->gadget
.dev
.parent
= &pdev
->dev
;
985 dum
->gadget
.dev
.release
= dummy_gadget_release
;
986 rc
= device_register(&dum
->gadget
.dev
);
988 put_device(&dum
->gadget
.dev
);
992 init_dummy_udc_hw(dum
);
994 rc
= usb_add_gadget_udc(&pdev
->dev
, &dum
->gadget
);
998 rc
= device_create_file(&dum
->gadget
.dev
, &dev_attr_function
);
1001 platform_set_drvdata(pdev
, dum
);
1005 usb_del_gadget_udc(&dum
->gadget
);
1007 device_unregister(&dum
->gadget
.dev
);
1011 static int dummy_udc_remove(struct platform_device
*pdev
)
1013 struct dummy
*dum
= platform_get_drvdata(pdev
);
1015 usb_del_gadget_udc(&dum
->gadget
);
1016 platform_set_drvdata(pdev
, NULL
);
1017 device_remove_file(&dum
->gadget
.dev
, &dev_attr_function
);
1018 device_unregister(&dum
->gadget
.dev
);
1022 static void dummy_udc_pm(struct dummy
*dum
, struct dummy_hcd
*dum_hcd
,
1025 spin_lock_irq(&dum
->lock
);
1026 dum
->udc_suspended
= suspend
;
1027 set_link_state(dum_hcd
);
1028 spin_unlock_irq(&dum
->lock
);
1031 static int dummy_udc_suspend(struct platform_device
*pdev
, pm_message_t state
)
1033 struct dummy
*dum
= platform_get_drvdata(pdev
);
1034 struct dummy_hcd
*dum_hcd
= gadget_to_dummy_hcd(&dum
->gadget
);
1036 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
1037 dummy_udc_pm(dum
, dum_hcd
, 1);
1038 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd
));
1042 static int dummy_udc_resume(struct platform_device
*pdev
)
1044 struct dummy
*dum
= platform_get_drvdata(pdev
);
1045 struct dummy_hcd
*dum_hcd
= gadget_to_dummy_hcd(&dum
->gadget
);
1047 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
1048 dummy_udc_pm(dum
, dum_hcd
, 0);
1049 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd
));
1053 static struct platform_driver dummy_udc_driver
= {
1054 .probe
= dummy_udc_probe
,
1055 .remove
= dummy_udc_remove
,
1056 .suspend
= dummy_udc_suspend
,
1057 .resume
= dummy_udc_resume
,
1059 .name
= (char *) gadget_name
,
1060 .owner
= THIS_MODULE
,
1064 /*-------------------------------------------------------------------------*/
1066 static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor
*desc
)
1070 index
= usb_endpoint_num(desc
) << 1;
1071 if (usb_endpoint_dir_in(desc
))
1076 /* MASTER/HOST SIDE DRIVER
1078 * this uses the hcd framework to hook up to host side drivers.
1079 * its root hub will only have one device, otherwise it acts like
1080 * a normal host controller.
1082 * when urbs are queued, they're just stuck on a list that we
1083 * scan in a timer callback. that callback connects writes from
1084 * the host with reads from the device, and so on, based on the
1088 static int dummy_ep_stream_en(struct dummy_hcd
*dum_hcd
, struct urb
*urb
)
1090 const struct usb_endpoint_descriptor
*desc
= &urb
->ep
->desc
;
1093 if (!usb_endpoint_xfer_bulk(desc
))
1096 index
= dummy_get_ep_idx(desc
);
1097 return (1 << index
) & dum_hcd
->stream_en_ep
;
1101 * The max stream number is saved as a nibble so for the 30 possible endpoints
1102 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1103 * means we use only 1 stream). The maximum according to the spec is 16bit so
1104 * if the 16 stream limit is about to go, the array size should be incremented
1105 * to 30 elements of type u16.
1107 static int get_max_streams_for_pipe(struct dummy_hcd
*dum_hcd
,
1112 max_streams
= dum_hcd
->num_stream
[usb_pipeendpoint(pipe
)];
1113 if (usb_pipeout(pipe
))
1121 static void set_max_streams_for_pipe(struct dummy_hcd
*dum_hcd
,
1122 unsigned int pipe
, unsigned int streams
)
1127 max_streams
= dum_hcd
->num_stream
[usb_pipeendpoint(pipe
)];
1128 if (usb_pipeout(pipe
)) {
1132 max_streams
&= 0xf0;
1134 max_streams
|= streams
;
1135 dum_hcd
->num_stream
[usb_pipeendpoint(pipe
)] = max_streams
;
1138 static int dummy_validate_stream(struct dummy_hcd
*dum_hcd
, struct urb
*urb
)
1140 unsigned int max_streams
;
1143 enabled
= dummy_ep_stream_en(dum_hcd
, urb
);
1144 if (!urb
->stream_id
) {
1152 max_streams
= get_max_streams_for_pipe(dum_hcd
,
1153 usb_pipeendpoint(urb
->pipe
));
1154 if (urb
->stream_id
> max_streams
) {
1155 dev_err(dummy_dev(dum_hcd
), "Stream id %d is out of range.\n",
1163 static int dummy_urb_enqueue(
1164 struct usb_hcd
*hcd
,
1168 struct dummy_hcd
*dum_hcd
;
1170 unsigned long flags
;
1173 urbp
= kmalloc(sizeof *urbp
, mem_flags
);
1177 urbp
->miter_started
= 0;
1179 dum_hcd
= hcd_to_dummy_hcd(hcd
);
1180 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
1182 rc
= dummy_validate_stream(dum_hcd
, urb
);
1188 rc
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1194 if (!dum_hcd
->udev
) {
1195 dum_hcd
->udev
= urb
->dev
;
1196 usb_get_dev(dum_hcd
->udev
);
1197 } else if (unlikely(dum_hcd
->udev
!= urb
->dev
))
1198 dev_err(dummy_dev(dum_hcd
), "usb_device address has changed!\n");
1200 list_add_tail(&urbp
->urbp_list
, &dum_hcd
->urbp_list
);
1202 if (usb_pipetype(urb
->pipe
) == PIPE_CONTROL
)
1203 urb
->error_count
= 1; /* mark as a new urb */
1205 /* kick the scheduler, it'll do the rest */
1206 if (!timer_pending(&dum_hcd
->timer
))
1207 mod_timer(&dum_hcd
->timer
, jiffies
+ 1);
1210 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
1214 static int dummy_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1216 struct dummy_hcd
*dum_hcd
;
1217 unsigned long flags
;
1220 /* giveback happens automatically in timer callback,
1221 * so make sure the callback happens */
1222 dum_hcd
= hcd_to_dummy_hcd(hcd
);
1223 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
1225 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1226 if (!rc
&& dum_hcd
->rh_state
!= DUMMY_RH_RUNNING
&&
1227 !list_empty(&dum_hcd
->urbp_list
))
1228 mod_timer(&dum_hcd
->timer
, jiffies
);
1230 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
1234 static int dummy_perform_transfer(struct urb
*urb
, struct dummy_request
*req
,
1238 struct urbp
*urbp
= urb
->hcpriv
;
1240 struct sg_mapping_iter
*miter
= &urbp
->miter
;
1245 to_host
= usb_pipein(urb
->pipe
);
1246 rbuf
= req
->req
.buf
+ req
->req
.actual
;
1248 if (!urb
->num_sgs
) {
1249 ubuf
= urb
->transfer_buffer
+ urb
->actual_length
;
1251 memcpy(ubuf
, rbuf
, len
);
1253 memcpy(rbuf
, ubuf
, len
);
1257 if (!urbp
->miter_started
) {
1258 u32 flags
= SG_MITER_ATOMIC
;
1261 flags
|= SG_MITER_TO_SG
;
1263 flags
|= SG_MITER_FROM_SG
;
1265 sg_miter_start(miter
, urb
->sg
, urb
->num_sgs
, flags
);
1266 urbp
->miter_started
= 1;
1268 next_sg
= sg_miter_next(miter
);
1269 if (next_sg
== false) {
1275 this_sg
= min_t(u32
, len
, miter
->length
);
1276 miter
->consumed
= this_sg
;
1280 memcpy(ubuf
, rbuf
, this_sg
);
1282 memcpy(rbuf
, ubuf
, this_sg
);
1287 next_sg
= sg_miter_next(miter
);
1288 if (next_sg
== false) {
1296 sg_miter_stop(miter
);
1300 /* transfer up to a frame's worth; caller must own lock */
1301 static int transfer(struct dummy_hcd
*dum_hcd
, struct urb
*urb
,
1302 struct dummy_ep
*ep
, int limit
, int *status
)
1304 struct dummy
*dum
= dum_hcd
->dum
;
1305 struct dummy_request
*req
;
1308 /* if there's no request queued, the device is NAKing; return */
1309 list_for_each_entry(req
, &ep
->queue
, queue
) {
1310 unsigned host_len
, dev_len
, len
;
1311 int is_short
, to_host
;
1314 if (dummy_ep_stream_en(dum_hcd
, urb
)) {
1315 if ((urb
->stream_id
!= req
->req
.stream_id
))
1319 /* 1..N packets of ep->ep.maxpacket each ... the last one
1320 * may be short (including zero length).
1322 * writer can send a zlp explicitly (length 0) or implicitly
1323 * (length mod maxpacket zero, and 'zero' flag); they always
1326 host_len
= urb
->transfer_buffer_length
- urb
->actual_length
;
1327 dev_len
= req
->req
.length
- req
->req
.actual
;
1328 len
= min(host_len
, dev_len
);
1330 /* FIXME update emulated data toggle too */
1332 to_host
= usb_pipein(urb
->pipe
);
1333 if (unlikely(len
== 0))
1336 /* not enough bandwidth left? */
1337 if (limit
< ep
->ep
.maxpacket
&& limit
< len
)
1339 len
= min_t(unsigned, len
, limit
);
1343 /* use an extra pass for the final short packet */
1344 if (len
> ep
->ep
.maxpacket
) {
1346 len
-= (len
% ep
->ep
.maxpacket
);
1348 is_short
= (len
% ep
->ep
.maxpacket
) != 0;
1350 len
= dummy_perform_transfer(urb
, req
, len
);
1352 ep
->last_io
= jiffies
;
1354 req
->req
.status
= len
;
1357 urb
->actual_length
+= len
;
1358 req
->req
.actual
+= len
;
1362 /* short packets terminate, maybe with overflow/underflow.
1363 * it's only really an error to write too much.
1365 * partially filling a buffer optionally blocks queue advances
1366 * (so completion handlers can clean up the queue) but we don't
1367 * need to emulate such data-in-flight.
1370 if (host_len
== dev_len
) {
1371 req
->req
.status
= 0;
1373 } else if (to_host
) {
1374 req
->req
.status
= 0;
1375 if (dev_len
> host_len
)
1376 *status
= -EOVERFLOW
;
1379 } else if (!to_host
) {
1381 if (host_len
> dev_len
)
1382 req
->req
.status
= -EOVERFLOW
;
1384 req
->req
.status
= 0;
1387 /* many requests terminate without a short packet */
1389 if (req
->req
.length
== req
->req
.actual
1391 req
->req
.status
= 0;
1392 if (urb
->transfer_buffer_length
== urb
->actual_length
1393 && !(urb
->transfer_flags
1398 /* device side completion --> continuable */
1399 if (req
->req
.status
!= -EINPROGRESS
) {
1400 list_del_init(&req
->queue
);
1402 spin_unlock(&dum
->lock
);
1403 req
->req
.complete(&ep
->ep
, &req
->req
);
1404 spin_lock(&dum
->lock
);
1406 /* requests might have been unlinked... */
1410 /* host side completion --> terminate */
1411 if (*status
!= -EINPROGRESS
)
1414 /* rescan to continue with any other queued i/o */
1421 static int periodic_bytes(struct dummy
*dum
, struct dummy_ep
*ep
)
1423 int limit
= ep
->ep
.maxpacket
;
1425 if (dum
->gadget
.speed
== USB_SPEED_HIGH
) {
1428 /* high bandwidth mode */
1429 tmp
= usb_endpoint_maxp(ep
->desc
);
1430 tmp
= (tmp
>> 11) & 0x03;
1431 tmp
*= 8 /* applies to entire frame */;
1432 limit
+= limit
* tmp
;
1434 if (dum
->gadget
.speed
== USB_SPEED_SUPER
) {
1435 switch (usb_endpoint_type(ep
->desc
)) {
1436 case USB_ENDPOINT_XFER_ISOC
:
1437 /* Sec. 4.4.8.2 USB3.0 Spec */
1438 limit
= 3 * 16 * 1024 * 8;
1440 case USB_ENDPOINT_XFER_INT
:
1441 /* Sec. 4.4.7.2 USB3.0 Spec */
1442 limit
= 3 * 1024 * 8;
1444 case USB_ENDPOINT_XFER_BULK
:
1452 #define is_active(dum_hcd) ((dum_hcd->port_status & \
1453 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1454 USB_PORT_STAT_SUSPEND)) \
1455 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1457 static struct dummy_ep
*find_endpoint(struct dummy
*dum
, u8 address
)
1461 if (!is_active((dum
->gadget
.speed
== USB_SPEED_SUPER
?
1462 dum
->ss_hcd
: dum
->hs_hcd
)))
1464 if ((address
& ~USB_DIR_IN
) == 0)
1466 for (i
= 1; i
< DUMMY_ENDPOINTS
; i
++) {
1467 struct dummy_ep
*ep
= &dum
->ep
[i
];
1471 if (ep
->desc
->bEndpointAddress
== address
)
1479 #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1480 #define Dev_InRequest (Dev_Request | USB_DIR_IN)
1481 #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1482 #define Intf_InRequest (Intf_Request | USB_DIR_IN)
1483 #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1484 #define Ep_InRequest (Ep_Request | USB_DIR_IN)
1488 * handle_control_request() - handles all control transfers
1489 * @dum: pointer to dummy (the_controller)
1490 * @urb: the urb request to handle
1491 * @setup: pointer to the setup data for a USB device control
1493 * @status: pointer to request handling status
1495 * Return 0 - if the request was handled
1496 * 1 - if the request wasn't handles
1497 * error code on error
1499 static int handle_control_request(struct dummy_hcd
*dum_hcd
, struct urb
*urb
,
1500 struct usb_ctrlrequest
*setup
,
1503 struct dummy_ep
*ep2
;
1504 struct dummy
*dum
= dum_hcd
->dum
;
1509 w_index
= le16_to_cpu(setup
->wIndex
);
1510 w_value
= le16_to_cpu(setup
->wValue
);
1511 switch (setup
->bRequest
) {
1512 case USB_REQ_SET_ADDRESS
:
1513 if (setup
->bRequestType
!= Dev_Request
)
1515 dum
->address
= w_value
;
1517 dev_dbg(udc_dev(dum
), "set_address = %d\n",
1521 case USB_REQ_SET_FEATURE
:
1522 if (setup
->bRequestType
== Dev_Request
) {
1525 case USB_DEVICE_REMOTE_WAKEUP
:
1527 case USB_DEVICE_B_HNP_ENABLE
:
1528 dum
->gadget
.b_hnp_enable
= 1;
1530 case USB_DEVICE_A_HNP_SUPPORT
:
1531 dum
->gadget
.a_hnp_support
= 1;
1533 case USB_DEVICE_A_ALT_HNP_SUPPORT
:
1534 dum
->gadget
.a_alt_hnp_support
= 1;
1536 case USB_DEVICE_U1_ENABLE
:
1537 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1539 w_value
= USB_DEV_STAT_U1_ENABLED
;
1541 ret_val
= -EOPNOTSUPP
;
1543 case USB_DEVICE_U2_ENABLE
:
1544 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1546 w_value
= USB_DEV_STAT_U2_ENABLED
;
1548 ret_val
= -EOPNOTSUPP
;
1550 case USB_DEVICE_LTM_ENABLE
:
1551 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1553 w_value
= USB_DEV_STAT_LTM_ENABLED
;
1555 ret_val
= -EOPNOTSUPP
;
1558 ret_val
= -EOPNOTSUPP
;
1561 dum
->devstatus
|= (1 << w_value
);
1564 } else if (setup
->bRequestType
== Ep_Request
) {
1566 ep2
= find_endpoint(dum
, w_index
);
1567 if (!ep2
|| ep2
->ep
.name
== ep0name
) {
1568 ret_val
= -EOPNOTSUPP
;
1576 case USB_REQ_CLEAR_FEATURE
:
1577 if (setup
->bRequestType
== Dev_Request
) {
1580 case USB_DEVICE_REMOTE_WAKEUP
:
1581 w_value
= USB_DEVICE_REMOTE_WAKEUP
;
1583 case USB_DEVICE_U1_ENABLE
:
1584 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1586 w_value
= USB_DEV_STAT_U1_ENABLED
;
1588 ret_val
= -EOPNOTSUPP
;
1590 case USB_DEVICE_U2_ENABLE
:
1591 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1593 w_value
= USB_DEV_STAT_U2_ENABLED
;
1595 ret_val
= -EOPNOTSUPP
;
1597 case USB_DEVICE_LTM_ENABLE
:
1598 if (dummy_hcd_to_hcd(dum_hcd
)->speed
==
1600 w_value
= USB_DEV_STAT_LTM_ENABLED
;
1602 ret_val
= -EOPNOTSUPP
;
1605 ret_val
= -EOPNOTSUPP
;
1609 dum
->devstatus
&= ~(1 << w_value
);
1612 } else if (setup
->bRequestType
== Ep_Request
) {
1614 ep2
= find_endpoint(dum
, w_index
);
1616 ret_val
= -EOPNOTSUPP
;
1625 case USB_REQ_GET_STATUS
:
1626 if (setup
->bRequestType
== Dev_InRequest
1627 || setup
->bRequestType
== Intf_InRequest
1628 || setup
->bRequestType
== Ep_InRequest
) {
1631 * device: remote wakeup, selfpowered
1632 * interface: nothing
1635 buf
= (char *)urb
->transfer_buffer
;
1636 if (urb
->transfer_buffer_length
> 0) {
1637 if (setup
->bRequestType
== Ep_InRequest
) {
1638 ep2
= find_endpoint(dum
, w_index
);
1640 ret_val
= -EOPNOTSUPP
;
1643 buf
[0] = ep2
->halted
;
1644 } else if (setup
->bRequestType
==
1646 buf
[0] = (u8
)dum
->devstatus
;
1650 if (urb
->transfer_buffer_length
> 1)
1652 urb
->actual_length
= min_t(u32
, 2,
1653 urb
->transfer_buffer_length
);
1662 /* drive both sides of the transfers; looks like irq handlers to
1663 * both drivers except the callbacks aren't in_irq().
1665 static void dummy_timer(unsigned long _dum_hcd
)
1667 struct dummy_hcd
*dum_hcd
= (struct dummy_hcd
*) _dum_hcd
;
1668 struct dummy
*dum
= dum_hcd
->dum
;
1669 struct urbp
*urbp
, *tmp
;
1670 unsigned long flags
;
1674 /* simplistic model for one frame's bandwidth */
1675 switch (dum
->gadget
.speed
) {
1677 total
= 8/*bytes*/ * 12/*packets*/;
1679 case USB_SPEED_FULL
:
1680 total
= 64/*bytes*/ * 19/*packets*/;
1682 case USB_SPEED_HIGH
:
1683 total
= 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1685 case USB_SPEED_SUPER
:
1686 /* Bus speed is 500000 bytes/ms, so use a little less */
1690 dev_err(dummy_dev(dum_hcd
), "bogus device speed\n");
1694 /* FIXME if HZ != 1000 this will probably misbehave ... */
1696 /* look at each urb queued by the host side driver */
1697 spin_lock_irqsave(&dum
->lock
, flags
);
1699 if (!dum_hcd
->udev
) {
1700 dev_err(dummy_dev(dum_hcd
),
1701 "timer fired with no URBs pending?\n");
1702 spin_unlock_irqrestore(&dum
->lock
, flags
);
1706 for (i
= 0; i
< DUMMY_ENDPOINTS
; i
++) {
1709 dum
->ep
[i
].already_seen
= 0;
1713 list_for_each_entry_safe(urbp
, tmp
, &dum_hcd
->urbp_list
, urbp_list
) {
1715 struct dummy_request
*req
;
1717 struct dummy_ep
*ep
= NULL
;
1719 int status
= -EINPROGRESS
;
1724 else if (dum_hcd
->rh_state
!= DUMMY_RH_RUNNING
)
1726 type
= usb_pipetype(urb
->pipe
);
1728 /* used up this frame's non-periodic bandwidth?
1729 * FIXME there's infinite bandwidth for control and
1730 * periodic transfers ... unrealistic.
1732 if (total
<= 0 && type
== PIPE_BULK
)
1735 /* find the gadget's ep for this request (if configured) */
1736 address
= usb_pipeendpoint (urb
->pipe
);
1737 if (usb_pipein(urb
->pipe
))
1738 address
|= USB_DIR_IN
;
1739 ep
= find_endpoint(dum
, address
);
1741 /* set_configuration() disagreement */
1742 dev_dbg(dummy_dev(dum_hcd
),
1743 "no ep configured for urb %p\n",
1749 if (ep
->already_seen
)
1751 ep
->already_seen
= 1;
1752 if (ep
== &dum
->ep
[0] && urb
->error_count
) {
1753 ep
->setup_stage
= 1; /* a new urb */
1754 urb
->error_count
= 0;
1756 if (ep
->halted
&& !ep
->setup_stage
) {
1757 /* NOTE: must not be iso! */
1758 dev_dbg(dummy_dev(dum_hcd
), "ep %s halted, urb %p\n",
1763 /* FIXME make sure both ends agree on maxpacket */
1765 /* handle control requests */
1766 if (ep
== &dum
->ep
[0] && ep
->setup_stage
) {
1767 struct usb_ctrlrequest setup
;
1770 setup
= *(struct usb_ctrlrequest
*) urb
->setup_packet
;
1771 /* paranoia, in case of stale queued data */
1772 list_for_each_entry(req
, &ep
->queue
, queue
) {
1773 list_del_init(&req
->queue
);
1774 req
->req
.status
= -EOVERFLOW
;
1775 dev_dbg(udc_dev(dum
), "stale req = %p\n",
1778 spin_unlock(&dum
->lock
);
1779 req
->req
.complete(&ep
->ep
, &req
->req
);
1780 spin_lock(&dum
->lock
);
1781 ep
->already_seen
= 0;
1785 /* gadget driver never sees set_address or operations
1786 * on standard feature flags. some hardware doesn't
1789 ep
->last_io
= jiffies
;
1790 ep
->setup_stage
= 0;
1793 value
= handle_control_request(dum_hcd
, urb
, &setup
,
1796 /* gadget driver handles all other requests. block
1797 * until setup() returns; no reentrancy issues etc.
1800 spin_unlock(&dum
->lock
);
1801 value
= dum
->driver
->setup(&dum
->gadget
,
1803 spin_lock(&dum
->lock
);
1806 /* no delays (max 64KB data stage) */
1808 goto treat_control_like_bulk
;
1810 /* error, see below */
1814 if (value
!= -EOPNOTSUPP
)
1815 dev_dbg(udc_dev(dum
),
1819 urb
->actual_length
= 0;
1825 /* non-control requests */
1827 switch (usb_pipetype(urb
->pipe
)) {
1828 case PIPE_ISOCHRONOUS
:
1829 /* FIXME is it urb->interval since the last xfer?
1830 * use urb->iso_frame_desc[i].
1831 * complete whether or not ep has requests queued.
1832 * report random errors, to debug drivers.
1834 limit
= max(limit
, periodic_bytes(dum
, ep
));
1838 case PIPE_INTERRUPT
:
1839 /* FIXME is it urb->interval since the last xfer?
1840 * this almost certainly polls too fast.
1842 limit
= max(limit
, periodic_bytes(dum
, ep
));
1846 treat_control_like_bulk
:
1847 ep
->last_io
= jiffies
;
1848 total
= transfer(dum_hcd
, urb
, ep
, limit
, &status
);
1852 /* incomplete transfer? */
1853 if (status
== -EINPROGRESS
)
1857 list_del(&urbp
->urbp_list
);
1860 ep
->already_seen
= ep
->setup_stage
= 0;
1862 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd
), urb
);
1863 spin_unlock(&dum
->lock
);
1864 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd
), urb
, status
);
1865 spin_lock(&dum
->lock
);
1870 if (list_empty(&dum_hcd
->urbp_list
)) {
1871 usb_put_dev(dum_hcd
->udev
);
1872 dum_hcd
->udev
= NULL
;
1873 } else if (dum_hcd
->rh_state
== DUMMY_RH_RUNNING
) {
1874 /* want a 1 msec delay here */
1875 mod_timer(&dum_hcd
->timer
, jiffies
+ msecs_to_jiffies(1));
1878 spin_unlock_irqrestore(&dum
->lock
, flags
);
1881 /*-------------------------------------------------------------------------*/
1883 #define PORT_C_MASK \
1884 ((USB_PORT_STAT_C_CONNECTION \
1885 | USB_PORT_STAT_C_ENABLE \
1886 | USB_PORT_STAT_C_SUSPEND \
1887 | USB_PORT_STAT_C_OVERCURRENT \
1888 | USB_PORT_STAT_C_RESET) << 16)
1890 static int dummy_hub_status(struct usb_hcd
*hcd
, char *buf
)
1892 struct dummy_hcd
*dum_hcd
;
1893 unsigned long flags
;
1896 dum_hcd
= hcd_to_dummy_hcd(hcd
);
1898 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
1899 if (!HCD_HW_ACCESSIBLE(hcd
))
1902 if (dum_hcd
->resuming
&& time_after_eq(jiffies
, dum_hcd
->re_timeout
)) {
1903 dum_hcd
->port_status
|= (USB_PORT_STAT_C_SUSPEND
<< 16);
1904 dum_hcd
->port_status
&= ~USB_PORT_STAT_SUSPEND
;
1905 set_link_state(dum_hcd
);
1908 if ((dum_hcd
->port_status
& PORT_C_MASK
) != 0) {
1910 dev_dbg(dummy_dev(dum_hcd
), "port status 0x%08x has changes\n",
1911 dum_hcd
->port_status
);
1913 if (dum_hcd
->rh_state
== DUMMY_RH_SUSPENDED
)
1914 usb_hcd_resume_root_hub(hcd
);
1917 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
1922 ss_hub_descriptor(struct usb_hub_descriptor
*desc
)
1924 memset(desc
, 0, sizeof *desc
);
1925 desc
->bDescriptorType
= 0x2a;
1926 desc
->bDescLength
= 12;
1927 desc
->wHubCharacteristics
= cpu_to_le16(0x0001);
1928 desc
->bNbrPorts
= 1;
1929 desc
->u
.ss
.bHubHdrDecLat
= 0x04; /* Worst case: 0.4 micro sec*/
1930 desc
->u
.ss
.DeviceRemovable
= 0xffff;
1933 static inline void hub_descriptor(struct usb_hub_descriptor
*desc
)
1935 memset(desc
, 0, sizeof *desc
);
1936 desc
->bDescriptorType
= 0x29;
1937 desc
->bDescLength
= 9;
1938 desc
->wHubCharacteristics
= cpu_to_le16(0x0001);
1939 desc
->bNbrPorts
= 1;
1940 desc
->u
.hs
.DeviceRemovable
[0] = 0xff;
1941 desc
->u
.hs
.DeviceRemovable
[1] = 0xff;
1944 static int dummy_hub_control(
1945 struct usb_hcd
*hcd
,
1952 struct dummy_hcd
*dum_hcd
;
1954 unsigned long flags
;
1956 if (!HCD_HW_ACCESSIBLE(hcd
))
1959 dum_hcd
= hcd_to_dummy_hcd(hcd
);
1961 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
1963 case ClearHubFeature
:
1965 case ClearPortFeature
:
1967 case USB_PORT_FEAT_SUSPEND
:
1968 if (hcd
->speed
== HCD_USB3
) {
1969 dev_dbg(dummy_dev(dum_hcd
),
1970 "USB_PORT_FEAT_SUSPEND req not "
1971 "supported for USB 3.0 roothub\n");
1974 if (dum_hcd
->port_status
& USB_PORT_STAT_SUSPEND
) {
1975 /* 20msec resume signaling */
1976 dum_hcd
->resuming
= 1;
1977 dum_hcd
->re_timeout
= jiffies
+
1978 msecs_to_jiffies(20);
1981 case USB_PORT_FEAT_POWER
:
1982 if (hcd
->speed
== HCD_USB3
) {
1983 if (dum_hcd
->port_status
& USB_PORT_STAT_POWER
)
1984 dev_dbg(dummy_dev(dum_hcd
),
1987 if (dum_hcd
->port_status
&
1988 USB_SS_PORT_STAT_POWER
)
1989 dev_dbg(dummy_dev(dum_hcd
),
1993 dum_hcd
->port_status
&= ~(1 << wValue
);
1994 set_link_state(dum_hcd
);
1997 case GetHubDescriptor
:
1998 if (hcd
->speed
== HCD_USB3
&&
1999 (wLength
< USB_DT_SS_HUB_SIZE
||
2000 wValue
!= (USB_DT_SS_HUB
<< 8))) {
2001 dev_dbg(dummy_dev(dum_hcd
),
2002 "Wrong hub descriptor type for "
2003 "USB 3.0 roothub.\n");
2006 if (hcd
->speed
== HCD_USB3
)
2007 ss_hub_descriptor((struct usb_hub_descriptor
*) buf
);
2009 hub_descriptor((struct usb_hub_descriptor
*) buf
);
2012 *(__le32
*) buf
= cpu_to_le32(0);
2018 /* whoever resets or resumes must GetPortStatus to
2021 if (dum_hcd
->resuming
&&
2022 time_after_eq(jiffies
, dum_hcd
->re_timeout
)) {
2023 dum_hcd
->port_status
|= (USB_PORT_STAT_C_SUSPEND
<< 16);
2024 dum_hcd
->port_status
&= ~USB_PORT_STAT_SUSPEND
;
2026 if ((dum_hcd
->port_status
& USB_PORT_STAT_RESET
) != 0 &&
2027 time_after_eq(jiffies
, dum_hcd
->re_timeout
)) {
2028 dum_hcd
->port_status
|= (USB_PORT_STAT_C_RESET
<< 16);
2029 dum_hcd
->port_status
&= ~USB_PORT_STAT_RESET
;
2030 if (dum_hcd
->dum
->pullup
) {
2031 dum_hcd
->port_status
|= USB_PORT_STAT_ENABLE
;
2033 if (hcd
->speed
< HCD_USB3
) {
2034 switch (dum_hcd
->dum
->gadget
.speed
) {
2035 case USB_SPEED_HIGH
:
2036 dum_hcd
->port_status
|=
2037 USB_PORT_STAT_HIGH_SPEED
;
2040 dum_hcd
->dum
->gadget
.ep0
->
2042 dum_hcd
->port_status
|=
2043 USB_PORT_STAT_LOW_SPEED
;
2046 dum_hcd
->dum
->gadget
.speed
=
2053 set_link_state(dum_hcd
);
2054 ((__le16
*) buf
)[0] = cpu_to_le16(dum_hcd
->port_status
);
2055 ((__le16
*) buf
)[1] = cpu_to_le16(dum_hcd
->port_status
>> 16);
2060 case SetPortFeature
:
2062 case USB_PORT_FEAT_LINK_STATE
:
2063 if (hcd
->speed
!= HCD_USB3
) {
2064 dev_dbg(dummy_dev(dum_hcd
),
2065 "USB_PORT_FEAT_LINK_STATE req not "
2066 "supported for USB 2.0 roothub\n");
2070 * Since this is dummy we don't have an actual link so
2071 * there is nothing to do for the SET_LINK_STATE cmd
2074 case USB_PORT_FEAT_U1_TIMEOUT
:
2075 case USB_PORT_FEAT_U2_TIMEOUT
:
2076 /* TODO: add suspend/resume support! */
2077 if (hcd
->speed
!= HCD_USB3
) {
2078 dev_dbg(dummy_dev(dum_hcd
),
2079 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2080 "supported for USB 2.0 roothub\n");
2084 case USB_PORT_FEAT_SUSPEND
:
2085 /* Applicable only for USB2.0 hub */
2086 if (hcd
->speed
== HCD_USB3
) {
2087 dev_dbg(dummy_dev(dum_hcd
),
2088 "USB_PORT_FEAT_SUSPEND req not "
2089 "supported for USB 3.0 roothub\n");
2092 if (dum_hcd
->active
) {
2093 dum_hcd
->port_status
|= USB_PORT_STAT_SUSPEND
;
2095 /* HNP would happen here; for now we
2096 * assume b_bus_req is always true.
2098 set_link_state(dum_hcd
);
2099 if (((1 << USB_DEVICE_B_HNP_ENABLE
)
2100 & dum_hcd
->dum
->devstatus
) != 0)
2101 dev_dbg(dummy_dev(dum_hcd
),
2105 case USB_PORT_FEAT_POWER
:
2106 if (hcd
->speed
== HCD_USB3
)
2107 dum_hcd
->port_status
|= USB_SS_PORT_STAT_POWER
;
2109 dum_hcd
->port_status
|= USB_PORT_STAT_POWER
;
2110 set_link_state(dum_hcd
);
2112 case USB_PORT_FEAT_BH_PORT_RESET
:
2113 /* Applicable only for USB3.0 hub */
2114 if (hcd
->speed
!= HCD_USB3
) {
2115 dev_dbg(dummy_dev(dum_hcd
),
2116 "USB_PORT_FEAT_BH_PORT_RESET req not "
2117 "supported for USB 2.0 roothub\n");
2121 case USB_PORT_FEAT_RESET
:
2122 /* if it's already enabled, disable */
2123 if (hcd
->speed
== HCD_USB3
) {
2124 dum_hcd
->port_status
= 0;
2125 dum_hcd
->port_status
=
2126 (USB_SS_PORT_STAT_POWER
|
2127 USB_PORT_STAT_CONNECTION
|
2128 USB_PORT_STAT_RESET
);
2130 dum_hcd
->port_status
&= ~(USB_PORT_STAT_ENABLE
2131 | USB_PORT_STAT_LOW_SPEED
2132 | USB_PORT_STAT_HIGH_SPEED
);
2134 * We want to reset device status. All but the
2135 * Self powered feature
2137 dum_hcd
->dum
->devstatus
&=
2138 (1 << USB_DEVICE_SELF_POWERED
);
2140 * FIXME USB3.0: what is the correct reset signaling
2141 * interval? Is it still 50msec as for HS?
2143 dum_hcd
->re_timeout
= jiffies
+ msecs_to_jiffies(50);
2146 if (hcd
->speed
== HCD_USB3
) {
2147 if ((dum_hcd
->port_status
&
2148 USB_SS_PORT_STAT_POWER
) != 0) {
2149 dum_hcd
->port_status
|= (1 << wValue
);
2150 set_link_state(dum_hcd
);
2153 if ((dum_hcd
->port_status
&
2154 USB_PORT_STAT_POWER
) != 0) {
2155 dum_hcd
->port_status
|= (1 << wValue
);
2156 set_link_state(dum_hcd
);
2160 case GetPortErrorCount
:
2161 if (hcd
->speed
!= HCD_USB3
) {
2162 dev_dbg(dummy_dev(dum_hcd
),
2163 "GetPortErrorCount req not "
2164 "supported for USB 2.0 roothub\n");
2167 /* We'll always return 0 since this is a dummy hub */
2168 *(__le32
*) buf
= cpu_to_le32(0);
2171 if (hcd
->speed
!= HCD_USB3
) {
2172 dev_dbg(dummy_dev(dum_hcd
),
2173 "SetHubDepth req not supported for "
2174 "USB 2.0 roothub\n");
2179 dev_dbg(dummy_dev(dum_hcd
),
2180 "hub control req%04x v%04x i%04x l%d\n",
2181 typeReq
, wValue
, wIndex
, wLength
);
2183 /* "protocol stall" on error */
2186 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
2188 if ((dum_hcd
->port_status
& PORT_C_MASK
) != 0)
2189 usb_hcd_poll_rh_status(hcd
);
2193 static int dummy_bus_suspend(struct usb_hcd
*hcd
)
2195 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2197 dev_dbg(&hcd
->self
.root_hub
->dev
, "%s\n", __func__
);
2199 spin_lock_irq(&dum_hcd
->dum
->lock
);
2200 dum_hcd
->rh_state
= DUMMY_RH_SUSPENDED
;
2201 set_link_state(dum_hcd
);
2202 hcd
->state
= HC_STATE_SUSPENDED
;
2203 spin_unlock_irq(&dum_hcd
->dum
->lock
);
2207 static int dummy_bus_resume(struct usb_hcd
*hcd
)
2209 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2212 dev_dbg(&hcd
->self
.root_hub
->dev
, "%s\n", __func__
);
2214 spin_lock_irq(&dum_hcd
->dum
->lock
);
2215 if (!HCD_HW_ACCESSIBLE(hcd
)) {
2218 dum_hcd
->rh_state
= DUMMY_RH_RUNNING
;
2219 set_link_state(dum_hcd
);
2220 if (!list_empty(&dum_hcd
->urbp_list
))
2221 mod_timer(&dum_hcd
->timer
, jiffies
);
2222 hcd
->state
= HC_STATE_RUNNING
;
2224 spin_unlock_irq(&dum_hcd
->dum
->lock
);
2228 /*-------------------------------------------------------------------------*/
2230 static inline ssize_t
show_urb(char *buf
, size_t size
, struct urb
*urb
)
2232 int ep
= usb_pipeendpoint(urb
->pipe
);
2234 return snprintf(buf
, size
,
2235 "urb/%p %s ep%d%s%s len %d/%d\n",
2238 switch (urb
->dev
->speed
) {
2242 case USB_SPEED_FULL
:
2245 case USB_SPEED_HIGH
:
2248 case USB_SPEED_SUPER
:
2255 ep
, ep
? (usb_pipein(urb
->pipe
) ? "in" : "out") : "",
2257 switch (usb_pipetype(urb
->pipe
)) { \
2258 case PIPE_CONTROL
: \
2264 case PIPE_INTERRUPT
: \
2271 urb
->actual_length
, urb
->transfer_buffer_length
);
2274 static ssize_t
show_urbs(struct device
*dev
, struct device_attribute
*attr
,
2277 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
2278 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2281 unsigned long flags
;
2283 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
2284 list_for_each_entry(urbp
, &dum_hcd
->urbp_list
, urbp_list
) {
2287 temp
= show_urb(buf
, PAGE_SIZE
- size
, urbp
->urb
);
2291 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
2295 static DEVICE_ATTR(urbs
, S_IRUGO
, show_urbs
, NULL
);
2297 static int dummy_start_ss(struct dummy_hcd
*dum_hcd
)
2299 init_timer(&dum_hcd
->timer
);
2300 dum_hcd
->timer
.function
= dummy_timer
;
2301 dum_hcd
->timer
.data
= (unsigned long)dum_hcd
;
2302 dum_hcd
->rh_state
= DUMMY_RH_RUNNING
;
2303 dum_hcd
->stream_en_ep
= 0;
2304 INIT_LIST_HEAD(&dum_hcd
->urbp_list
);
2305 dummy_hcd_to_hcd(dum_hcd
)->power_budget
= POWER_BUDGET
;
2306 dummy_hcd_to_hcd(dum_hcd
)->state
= HC_STATE_RUNNING
;
2307 dummy_hcd_to_hcd(dum_hcd
)->uses_new_polling
= 1;
2308 #ifdef CONFIG_USB_OTG
2309 dummy_hcd_to_hcd(dum_hcd
)->self
.otg_port
= 1;
2313 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2314 return device_create_file(dummy_dev(dum_hcd
), &dev_attr_urbs
);
2317 static int dummy_start(struct usb_hcd
*hcd
)
2319 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2322 * MASTER side init ... we emulate a root hub that'll only ever
2323 * talk to one device (the slave side). Also appears in sysfs,
2324 * just like more familiar pci-based HCDs.
2326 if (!usb_hcd_is_primary_hcd(hcd
))
2327 return dummy_start_ss(dum_hcd
);
2329 spin_lock_init(&dum_hcd
->dum
->lock
);
2330 init_timer(&dum_hcd
->timer
);
2331 dum_hcd
->timer
.function
= dummy_timer
;
2332 dum_hcd
->timer
.data
= (unsigned long)dum_hcd
;
2333 dum_hcd
->rh_state
= DUMMY_RH_RUNNING
;
2335 INIT_LIST_HEAD(&dum_hcd
->urbp_list
);
2337 hcd
->power_budget
= POWER_BUDGET
;
2338 hcd
->state
= HC_STATE_RUNNING
;
2339 hcd
->uses_new_polling
= 1;
2341 #ifdef CONFIG_USB_OTG
2342 hcd
->self
.otg_port
= 1;
2345 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2346 return device_create_file(dummy_dev(dum_hcd
), &dev_attr_urbs
);
2349 static void dummy_stop(struct usb_hcd
*hcd
)
2353 dum
= hcd_to_dummy_hcd(hcd
)->dum
;
2354 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd
)), &dev_attr_urbs
);
2355 usb_gadget_unregister_driver(dum
->driver
);
2356 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd
)), "stopped\n");
2359 /*-------------------------------------------------------------------------*/
2361 static int dummy_h_get_frame(struct usb_hcd
*hcd
)
2363 return dummy_g_get_frame(NULL
);
2366 static int dummy_setup(struct usb_hcd
*hcd
)
2368 hcd
->self
.sg_tablesize
= ~0;
2369 if (usb_hcd_is_primary_hcd(hcd
)) {
2370 the_controller
.hs_hcd
= hcd_to_dummy_hcd(hcd
);
2371 the_controller
.hs_hcd
->dum
= &the_controller
;
2373 * Mark the first roothub as being USB 2.0.
2374 * The USB 3.0 roothub will be registered later by
2377 hcd
->speed
= HCD_USB2
;
2378 hcd
->self
.root_hub
->speed
= USB_SPEED_HIGH
;
2380 the_controller
.ss_hcd
= hcd_to_dummy_hcd(hcd
);
2381 the_controller
.ss_hcd
->dum
= &the_controller
;
2382 hcd
->speed
= HCD_USB3
;
2383 hcd
->self
.root_hub
->speed
= USB_SPEED_SUPER
;
2388 /* Change a group of bulk endpoints to support multiple stream IDs */
2389 static int dummy_alloc_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
2390 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
2391 unsigned int num_streams
, gfp_t mem_flags
)
2393 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2394 unsigned long flags
;
2396 int ret_streams
= num_streams
;
2403 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
2404 for (i
= 0; i
< num_eps
; i
++) {
2405 index
= dummy_get_ep_idx(&eps
[i
]->desc
);
2406 if ((1 << index
) & dum_hcd
->stream_en_ep
) {
2407 ret_streams
= -EINVAL
;
2410 max_stream
= usb_ss_max_streams(&eps
[i
]->ss_ep_comp
);
2412 ret_streams
= -EINVAL
;
2415 if (max_stream
< ret_streams
) {
2416 dev_dbg(dummy_dev(dum_hcd
), "Ep 0x%x only supports %u "
2418 eps
[i
]->desc
.bEndpointAddress
,
2420 ret_streams
= max_stream
;
2424 for (i
= 0; i
< num_eps
; i
++) {
2425 index
= dummy_get_ep_idx(&eps
[i
]->desc
);
2426 dum_hcd
->stream_en_ep
|= 1 << index
;
2427 set_max_streams_for_pipe(dum_hcd
,
2428 usb_endpoint_num(&eps
[i
]->desc
), ret_streams
);
2431 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
2435 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2436 static int dummy_free_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
2437 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
2440 struct dummy_hcd
*dum_hcd
= hcd_to_dummy_hcd(hcd
);
2441 unsigned long flags
;
2446 spin_lock_irqsave(&dum_hcd
->dum
->lock
, flags
);
2447 for (i
= 0; i
< num_eps
; i
++) {
2448 index
= dummy_get_ep_idx(&eps
[i
]->desc
);
2449 if (!((1 << index
) & dum_hcd
->stream_en_ep
)) {
2455 for (i
= 0; i
< num_eps
; i
++) {
2456 index
= dummy_get_ep_idx(&eps
[i
]->desc
);
2457 dum_hcd
->stream_en_ep
&= ~(1 << index
);
2458 set_max_streams_for_pipe(dum_hcd
,
2459 usb_endpoint_num(&eps
[i
]->desc
), 0);
2463 spin_unlock_irqrestore(&dum_hcd
->dum
->lock
, flags
);
2467 static struct hc_driver dummy_hcd
= {
2468 .description
= (char *) driver_name
,
2469 .product_desc
= "Dummy host controller",
2470 .hcd_priv_size
= sizeof(struct dummy_hcd
),
2472 .flags
= HCD_USB3
| HCD_SHARED
,
2474 .reset
= dummy_setup
,
2475 .start
= dummy_start
,
2478 .urb_enqueue
= dummy_urb_enqueue
,
2479 .urb_dequeue
= dummy_urb_dequeue
,
2481 .get_frame_number
= dummy_h_get_frame
,
2483 .hub_status_data
= dummy_hub_status
,
2484 .hub_control
= dummy_hub_control
,
2485 .bus_suspend
= dummy_bus_suspend
,
2486 .bus_resume
= dummy_bus_resume
,
2488 .alloc_streams
= dummy_alloc_streams
,
2489 .free_streams
= dummy_free_streams
,
2492 static int dummy_hcd_probe(struct platform_device
*pdev
)
2494 struct usb_hcd
*hs_hcd
;
2495 struct usb_hcd
*ss_hcd
;
2498 dev_info(&pdev
->dev
, "%s, driver " DRIVER_VERSION
"\n", driver_desc
);
2500 if (!mod_data
.is_super_speed
)
2501 dummy_hcd
.flags
= HCD_USB2
;
2502 hs_hcd
= usb_create_hcd(&dummy_hcd
, &pdev
->dev
, dev_name(&pdev
->dev
));
2507 retval
= usb_add_hcd(hs_hcd
, 0, 0);
2509 usb_put_hcd(hs_hcd
);
2513 if (mod_data
.is_super_speed
) {
2514 ss_hcd
= usb_create_shared_hcd(&dummy_hcd
, &pdev
->dev
,
2515 dev_name(&pdev
->dev
), hs_hcd
);
2518 goto dealloc_usb2_hcd
;
2521 retval
= usb_add_hcd(ss_hcd
, 0, 0);
2528 usb_put_hcd(ss_hcd
);
2530 usb_put_hcd(hs_hcd
);
2531 the_controller
.hs_hcd
= the_controller
.ss_hcd
= NULL
;
2535 static int dummy_hcd_remove(struct platform_device
*pdev
)
2539 dum
= hcd_to_dummy_hcd(platform_get_drvdata(pdev
))->dum
;
2542 usb_remove_hcd(dummy_hcd_to_hcd(dum
->ss_hcd
));
2543 usb_put_hcd(dummy_hcd_to_hcd(dum
->ss_hcd
));
2546 usb_remove_hcd(dummy_hcd_to_hcd(dum
->hs_hcd
));
2547 usb_put_hcd(dummy_hcd_to_hcd(dum
->hs_hcd
));
2549 the_controller
.hs_hcd
= NULL
;
2550 the_controller
.ss_hcd
= NULL
;
2555 static int dummy_hcd_suspend(struct platform_device
*pdev
, pm_message_t state
)
2557 struct usb_hcd
*hcd
;
2558 struct dummy_hcd
*dum_hcd
;
2561 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
2563 hcd
= platform_get_drvdata(pdev
);
2564 dum_hcd
= hcd_to_dummy_hcd(hcd
);
2565 if (dum_hcd
->rh_state
== DUMMY_RH_RUNNING
) {
2566 dev_warn(&pdev
->dev
, "Root hub isn't suspended!\n");
2569 clear_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
2573 static int dummy_hcd_resume(struct platform_device
*pdev
)
2575 struct usb_hcd
*hcd
;
2577 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
2579 hcd
= platform_get_drvdata(pdev
);
2580 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
2581 usb_hcd_poll_rh_status(hcd
);
2585 static struct platform_driver dummy_hcd_driver
= {
2586 .probe
= dummy_hcd_probe
,
2587 .remove
= dummy_hcd_remove
,
2588 .suspend
= dummy_hcd_suspend
,
2589 .resume
= dummy_hcd_resume
,
2591 .name
= (char *) driver_name
,
2592 .owner
= THIS_MODULE
,
2596 /*-------------------------------------------------------------------------*/
2598 static struct platform_device
*the_udc_pdev
;
2599 static struct platform_device
*the_hcd_pdev
;
2601 static int __init
init(void)
2603 int retval
= -ENOMEM
;
2608 if (!mod_data
.is_high_speed
&& mod_data
.is_super_speed
)
2611 the_hcd_pdev
= platform_device_alloc(driver_name
, -1);
2614 the_udc_pdev
= platform_device_alloc(gadget_name
, -1);
2618 retval
= platform_driver_register(&dummy_hcd_driver
);
2620 goto err_register_hcd_driver
;
2621 retval
= platform_driver_register(&dummy_udc_driver
);
2623 goto err_register_udc_driver
;
2625 retval
= platform_device_add(the_hcd_pdev
);
2628 if (!the_controller
.hs_hcd
||
2629 (!the_controller
.ss_hcd
&& mod_data
.is_super_speed
)) {
2631 * The hcd was added successfully but its probe function failed
2637 retval
= platform_device_add(the_udc_pdev
);
2640 if (!platform_get_drvdata(the_udc_pdev
)) {
2642 * The udc was added successfully but its probe function failed
2651 platform_device_del(the_udc_pdev
);
2653 platform_device_del(the_hcd_pdev
);
2655 platform_driver_unregister(&dummy_udc_driver
);
2656 err_register_udc_driver
:
2657 platform_driver_unregister(&dummy_hcd_driver
);
2658 err_register_hcd_driver
:
2659 platform_device_put(the_udc_pdev
);
2661 platform_device_put(the_hcd_pdev
);
2666 static void __exit
cleanup(void)
2668 platform_device_unregister(the_udc_pdev
);
2669 platform_device_unregister(the_hcd_pdev
);
2670 platform_driver_unregister(&dummy_udc_driver
);
2671 platform_driver_unregister(&dummy_hcd_driver
);
2673 module_exit(cleanup
);