2 * (C) Copyright Linus Torvalds 1999
3 * (C) Copyright Johannes Erdfelt 1999-2001
4 * (C) Copyright Andreas Gal 1999
5 * (C) Copyright Gregory P. Smith 1999
6 * (C) Copyright Deti Fliegl 1999
7 * (C) Copyright Randy Dunlap 2000
8 * (C) Copyright David Brownell 2000-2002
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/version.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/completion.h>
30 #include <linux/utsname.h>
33 #include <asm/scatterlist.h>
34 #include <linux/device.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/mutex.h>
38 #include <asm/byteorder.h>
39 #include <asm/unaligned.h>
40 #include <linux/platform_device.h>
41 #include <linux/workqueue.h>
43 #include <linux/usb.h>
50 /*-------------------------------------------------------------------------*/
53 * USB Host Controller Driver framework
55 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
56 * HCD-specific behaviors/bugs.
58 * This does error checks, tracks devices and urbs, and delegates to a
59 * "hc_driver" only for code (and data) that really needs to know about
60 * hardware differences. That includes root hub registers, i/o queues,
61 * and so on ... but as little else as possible.
63 * Shared code includes most of the "root hub" code (these are emulated,
64 * though each HC's hardware works differently) and PCI glue, plus request
65 * tracking overhead. The HCD code should only block on spinlocks or on
66 * hardware handshaking; blocking on software events (such as other kernel
67 * threads releasing resources, or completing actions) is all generic.
69 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
70 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
71 * only by the hub driver ... and that neither should be seen or used by
72 * usb client device drivers.
74 * Contributors of ideas or unattributed patches include: David Brownell,
75 * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
78 * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
79 * associated cleanup. "usb_hcd" still != "usb_bus".
80 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
83 /*-------------------------------------------------------------------------*/
85 /* Keep track of which host controller drivers are loaded */
86 unsigned long usb_hcds_loaded
;
87 EXPORT_SYMBOL_GPL(usb_hcds_loaded
);
89 /* host controllers we manage */
90 LIST_HEAD (usb_bus_list
);
91 EXPORT_SYMBOL_GPL (usb_bus_list
);
93 /* used when allocating bus numbers */
96 unsigned long busmap
[USB_MAXBUS
/ (8*sizeof (unsigned long))];
98 static struct usb_busmap busmap
;
100 /* used when updating list of hcds */
101 DEFINE_MUTEX(usb_bus_list_lock
); /* exported only for usbfs */
102 EXPORT_SYMBOL_GPL (usb_bus_list_lock
);
104 /* used for controlling access to virtual root hubs */
105 static DEFINE_SPINLOCK(hcd_root_hub_lock
);
107 /* used when updating an endpoint's URB list */
108 static DEFINE_SPINLOCK(hcd_urb_list_lock
);
110 /* used to protect against unlinking URBs after the device is gone */
111 static DEFINE_SPINLOCK(hcd_urb_unlink_lock
);
113 /* wait queue for synchronous unlinks */
114 DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue
);
116 static inline int is_root_hub(struct usb_device
*udev
)
118 return (udev
->parent
== NULL
);
121 /*-------------------------------------------------------------------------*/
124 * Sharable chunks of root hub code.
127 /*-------------------------------------------------------------------------*/
129 #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
130 #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
132 /* usb 2.0 root hub device descriptor */
133 static const u8 usb2_rh_dev_descriptor
[18] = {
134 0x12, /* __u8 bLength; */
135 0x01, /* __u8 bDescriptorType; Device */
136 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
138 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
139 0x00, /* __u8 bDeviceSubClass; */
140 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
141 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
143 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
144 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
145 KERNEL_VER
, KERNEL_REL
, /* __le16 bcdDevice */
147 0x03, /* __u8 iManufacturer; */
148 0x02, /* __u8 iProduct; */
149 0x01, /* __u8 iSerialNumber; */
150 0x01 /* __u8 bNumConfigurations; */
153 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
155 /* usb 1.1 root hub device descriptor */
156 static const u8 usb11_rh_dev_descriptor
[18] = {
157 0x12, /* __u8 bLength; */
158 0x01, /* __u8 bDescriptorType; Device */
159 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
161 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
162 0x00, /* __u8 bDeviceSubClass; */
163 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
164 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
166 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
167 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
168 KERNEL_VER
, KERNEL_REL
, /* __le16 bcdDevice */
170 0x03, /* __u8 iManufacturer; */
171 0x02, /* __u8 iProduct; */
172 0x01, /* __u8 iSerialNumber; */
173 0x01 /* __u8 bNumConfigurations; */
177 /*-------------------------------------------------------------------------*/
179 /* Configuration descriptors for our root hubs */
181 static const u8 fs_rh_config_descriptor
[] = {
183 /* one configuration */
184 0x09, /* __u8 bLength; */
185 0x02, /* __u8 bDescriptorType; Configuration */
186 0x19, 0x00, /* __le16 wTotalLength; */
187 0x01, /* __u8 bNumInterfaces; (1) */
188 0x01, /* __u8 bConfigurationValue; */
189 0x00, /* __u8 iConfiguration; */
190 0xc0, /* __u8 bmAttributes;
195 0x00, /* __u8 MaxPower; */
198 * USB 2.0, single TT organization (mandatory):
199 * one interface, protocol 0
201 * USB 2.0, multiple TT organization (optional):
202 * two interfaces, protocols 1 (like single TT)
203 * and 2 (multiple TT mode) ... config is
209 0x09, /* __u8 if_bLength; */
210 0x04, /* __u8 if_bDescriptorType; Interface */
211 0x00, /* __u8 if_bInterfaceNumber; */
212 0x00, /* __u8 if_bAlternateSetting; */
213 0x01, /* __u8 if_bNumEndpoints; */
214 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
215 0x00, /* __u8 if_bInterfaceSubClass; */
216 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
217 0x00, /* __u8 if_iInterface; */
219 /* one endpoint (status change endpoint) */
220 0x07, /* __u8 ep_bLength; */
221 0x05, /* __u8 ep_bDescriptorType; Endpoint */
222 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
223 0x03, /* __u8 ep_bmAttributes; Interrupt */
224 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
225 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
228 static const u8 hs_rh_config_descriptor
[] = {
230 /* one configuration */
231 0x09, /* __u8 bLength; */
232 0x02, /* __u8 bDescriptorType; Configuration */
233 0x19, 0x00, /* __le16 wTotalLength; */
234 0x01, /* __u8 bNumInterfaces; (1) */
235 0x01, /* __u8 bConfigurationValue; */
236 0x00, /* __u8 iConfiguration; */
237 0xc0, /* __u8 bmAttributes;
242 0x00, /* __u8 MaxPower; */
245 * USB 2.0, single TT organization (mandatory):
246 * one interface, protocol 0
248 * USB 2.0, multiple TT organization (optional):
249 * two interfaces, protocols 1 (like single TT)
250 * and 2 (multiple TT mode) ... config is
256 0x09, /* __u8 if_bLength; */
257 0x04, /* __u8 if_bDescriptorType; Interface */
258 0x00, /* __u8 if_bInterfaceNumber; */
259 0x00, /* __u8 if_bAlternateSetting; */
260 0x01, /* __u8 if_bNumEndpoints; */
261 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
262 0x00, /* __u8 if_bInterfaceSubClass; */
263 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
264 0x00, /* __u8 if_iInterface; */
266 /* one endpoint (status change endpoint) */
267 0x07, /* __u8 ep_bLength; */
268 0x05, /* __u8 ep_bDescriptorType; Endpoint */
269 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
270 0x03, /* __u8 ep_bmAttributes; Interrupt */
271 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
272 * see hub.c:hub_configure() for details. */
273 (USB_MAXCHILDREN
+ 1 + 7) / 8, 0x00,
274 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
277 /*-------------------------------------------------------------------------*/
280 * helper routine for returning string descriptors in UTF-16LE
281 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
283 static unsigned ascii2utf(char *s
, u8
*utf
, int utfmax
)
287 for (retval
= 0; *s
&& utfmax
> 1; utfmax
-= 2, retval
+= 2) {
299 * rh_string - provides manufacturer, product and serial strings for root hub
300 * @id: the string ID number (1: serial number, 2: product, 3: vendor)
301 * @hcd: the host controller for this root hub
302 * @data: return packet in UTF-16 LE
303 * @len: length of the return packet
305 * Produces either a manufacturer, product or serial number string for the
306 * virtual root hub device.
308 static unsigned rh_string(int id
, struct usb_hcd
*hcd
, u8
*data
, unsigned len
)
314 buf
[0] = 4; buf
[1] = 3; /* 4 bytes string data */
315 buf
[2] = 0x09; buf
[3] = 0x04; /* MSFT-speak for "en-us" */
316 len
= min_t(unsigned, len
, 4);
317 memcpy (data
, buf
, len
);
321 } else if (id
== 1) {
322 strlcpy (buf
, hcd
->self
.bus_name
, sizeof buf
);
324 // product description
325 } else if (id
== 2) {
326 strlcpy (buf
, hcd
->product_desc
, sizeof buf
);
328 // id 3 == vendor description
329 } else if (id
== 3) {
330 snprintf (buf
, sizeof buf
, "%s %s %s", init_utsname()->sysname
,
331 init_utsname()->release
, hcd
->driver
->description
);
334 switch (len
) { /* All cases fall through */
336 len
= 2 + ascii2utf (buf
, data
+ 2, len
- 2);
338 data
[1] = 3; /* type == string */
340 data
[0] = 2 * (strlen (buf
) + 1);
342 ; /* Compiler wants a statement here */
348 /* Root hub control transfers execute synchronously */
349 static int rh_call_control (struct usb_hcd
*hcd
, struct urb
*urb
)
351 struct usb_ctrlrequest
*cmd
;
352 u16 typeReq
, wValue
, wIndex
, wLength
;
353 u8
*ubuf
= urb
->transfer_buffer
;
354 u8 tbuf
[sizeof (struct usb_hub_descriptor
)]
355 __attribute__((aligned(4)));
356 const u8
*bufp
= tbuf
;
360 u8 patch_protocol
= 0;
364 spin_lock_irq(&hcd_root_hub_lock
);
365 status
= usb_hcd_link_urb_to_ep(hcd
, urb
);
366 spin_unlock_irq(&hcd_root_hub_lock
);
369 urb
->hcpriv
= hcd
; /* Indicate it's queued */
371 cmd
= (struct usb_ctrlrequest
*) urb
->setup_packet
;
372 typeReq
= (cmd
->bRequestType
<< 8) | cmd
->bRequest
;
373 wValue
= le16_to_cpu (cmd
->wValue
);
374 wIndex
= le16_to_cpu (cmd
->wIndex
);
375 wLength
= le16_to_cpu (cmd
->wLength
);
377 if (wLength
> urb
->transfer_buffer_length
)
380 urb
->actual_length
= 0;
383 /* DEVICE REQUESTS */
385 /* The root hub's remote wakeup enable bit is implemented using
386 * driver model wakeup flags. If this system supports wakeup
387 * through USB, userspace may change the default "allow wakeup"
388 * policy through sysfs or these calls.
390 * Most root hubs support wakeup from downstream devices, for
391 * runtime power management (disabling USB clocks and reducing
392 * VBUS power usage). However, not all of them do so; silicon,
393 * board, and BIOS bugs here are not uncommon, so these can't
394 * be treated quite like external hubs.
396 * Likewise, not all root hubs will pass wakeup events upstream,
397 * to wake up the whole system. So don't assume root hub and
398 * controller capabilities are identical.
401 case DeviceRequest
| USB_REQ_GET_STATUS
:
402 tbuf
[0] = (device_may_wakeup(&hcd
->self
.root_hub
->dev
)
403 << USB_DEVICE_REMOTE_WAKEUP
)
404 | (1 << USB_DEVICE_SELF_POWERED
);
408 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
409 if (wValue
== USB_DEVICE_REMOTE_WAKEUP
)
410 device_set_wakeup_enable(&hcd
->self
.root_hub
->dev
, 0);
414 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
415 if (device_can_wakeup(&hcd
->self
.root_hub
->dev
)
416 && wValue
== USB_DEVICE_REMOTE_WAKEUP
)
417 device_set_wakeup_enable(&hcd
->self
.root_hub
->dev
, 1);
421 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
425 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
427 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
428 switch (wValue
& 0xff00) {
429 case USB_DT_DEVICE
<< 8:
430 if (hcd
->driver
->flags
& HCD_USB2
)
431 bufp
= usb2_rh_dev_descriptor
;
432 else if (hcd
->driver
->flags
& HCD_USB11
)
433 bufp
= usb11_rh_dev_descriptor
;
440 case USB_DT_CONFIG
<< 8:
441 if (hcd
->driver
->flags
& HCD_USB2
) {
442 bufp
= hs_rh_config_descriptor
;
443 len
= sizeof hs_rh_config_descriptor
;
445 bufp
= fs_rh_config_descriptor
;
446 len
= sizeof fs_rh_config_descriptor
;
448 if (device_can_wakeup(&hcd
->self
.root_hub
->dev
))
451 case USB_DT_STRING
<< 8:
452 if ((wValue
& 0xff) < 4)
453 urb
->actual_length
= rh_string(wValue
& 0xff,
455 else /* unsupported IDs --> "protocol stall" */
462 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
466 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
468 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
469 // wValue == urb->dev->devaddr
470 dev_dbg (hcd
->self
.controller
, "root hub device address %d\n",
474 /* INTERFACE REQUESTS (no defined feature/status flags) */
476 /* ENDPOINT REQUESTS */
478 case EndpointRequest
| USB_REQ_GET_STATUS
:
479 // ENDPOINT_HALT flag
484 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
485 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
486 dev_dbg (hcd
->self
.controller
, "no endpoint features yet\n");
489 /* CLASS REQUESTS (and errors) */
492 /* non-generic request */
498 case GetHubDescriptor
:
499 len
= sizeof (struct usb_hub_descriptor
);
502 status
= hcd
->driver
->hub_control (hcd
,
503 typeReq
, wValue
, wIndex
,
507 /* "protocol stall" on error */
513 if (status
!= -EPIPE
) {
514 dev_dbg (hcd
->self
.controller
,
515 "CTRL: TypeReq=0x%x val=0x%x "
516 "idx=0x%x len=%d ==> %d\n",
517 typeReq
, wValue
, wIndex
,
522 if (urb
->transfer_buffer_length
< len
)
523 len
= urb
->transfer_buffer_length
;
524 urb
->actual_length
= len
;
525 // always USB_DIR_IN, toward host
526 memcpy (ubuf
, bufp
, len
);
528 /* report whether RH hardware supports remote wakeup */
530 len
> offsetof (struct usb_config_descriptor
,
532 ((struct usb_config_descriptor
*)ubuf
)->bmAttributes
533 |= USB_CONFIG_ATT_WAKEUP
;
535 /* report whether RH hardware has an integrated TT */
536 if (patch_protocol
&&
537 len
> offsetof(struct usb_device_descriptor
,
539 ((struct usb_device_descriptor
*) ubuf
)->
543 /* any errors get returned through the urb completion */
544 spin_lock_irq(&hcd_root_hub_lock
);
545 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
547 /* This peculiar use of spinlocks echoes what real HC drivers do.
548 * Avoiding calls to local_irq_disable/enable makes the code
551 spin_unlock(&hcd_root_hub_lock
);
552 usb_hcd_giveback_urb(hcd
, urb
, status
);
553 spin_lock(&hcd_root_hub_lock
);
555 spin_unlock_irq(&hcd_root_hub_lock
);
559 /*-------------------------------------------------------------------------*/
562 * Root Hub interrupt transfers are polled using a timer if the
563 * driver requests it; otherwise the driver is responsible for
564 * calling usb_hcd_poll_rh_status() when an event occurs.
566 * Completions are called in_interrupt(), but they may or may not
569 void usb_hcd_poll_rh_status(struct usb_hcd
*hcd
)
574 char buffer
[4]; /* Any root hubs with > 31 ports? */
576 if (unlikely(!hcd
->rh_registered
))
578 if (!hcd
->uses_new_polling
&& !hcd
->status_urb
)
581 length
= hcd
->driver
->hub_status_data(hcd
, buffer
);
584 /* try to complete the status urb */
585 spin_lock_irqsave(&hcd_root_hub_lock
, flags
);
586 urb
= hcd
->status_urb
;
588 hcd
->poll_pending
= 0;
589 hcd
->status_urb
= NULL
;
590 urb
->actual_length
= length
;
591 memcpy(urb
->transfer_buffer
, buffer
, length
);
593 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
594 spin_unlock(&hcd_root_hub_lock
);
595 usb_hcd_giveback_urb(hcd
, urb
, 0);
596 spin_lock(&hcd_root_hub_lock
);
599 hcd
->poll_pending
= 1;
601 spin_unlock_irqrestore(&hcd_root_hub_lock
, flags
);
604 /* The USB 2.0 spec says 256 ms. This is close enough and won't
605 * exceed that limit if HZ is 100. The math is more clunky than
606 * maybe expected, this is to make sure that all timers for USB devices
607 * fire at the same time to give the CPU a break inbetween */
608 if (hcd
->uses_new_polling
? hcd
->poll_rh
:
609 (length
== 0 && hcd
->status_urb
!= NULL
))
610 mod_timer (&hcd
->rh_timer
, (jiffies
/(HZ
/4) + 1) * (HZ
/4));
612 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status
);
615 static void rh_timer_func (unsigned long _hcd
)
617 usb_hcd_poll_rh_status((struct usb_hcd
*) _hcd
);
620 /*-------------------------------------------------------------------------*/
622 static int rh_queue_status (struct usb_hcd
*hcd
, struct urb
*urb
)
626 unsigned len
= 1 + (urb
->dev
->maxchild
/ 8);
628 spin_lock_irqsave (&hcd_root_hub_lock
, flags
);
629 if (hcd
->status_urb
|| urb
->transfer_buffer_length
< len
) {
630 dev_dbg (hcd
->self
.controller
, "not queuing rh status urb\n");
635 retval
= usb_hcd_link_urb_to_ep(hcd
, urb
);
639 hcd
->status_urb
= urb
;
640 urb
->hcpriv
= hcd
; /* indicate it's queued */
641 if (!hcd
->uses_new_polling
)
642 mod_timer(&hcd
->rh_timer
, (jiffies
/(HZ
/4) + 1) * (HZ
/4));
644 /* If a status change has already occurred, report it ASAP */
645 else if (hcd
->poll_pending
)
646 mod_timer(&hcd
->rh_timer
, jiffies
);
649 spin_unlock_irqrestore (&hcd_root_hub_lock
, flags
);
653 static int rh_urb_enqueue (struct usb_hcd
*hcd
, struct urb
*urb
)
655 if (usb_endpoint_xfer_int(&urb
->ep
->desc
))
656 return rh_queue_status (hcd
, urb
);
657 if (usb_endpoint_xfer_control(&urb
->ep
->desc
))
658 return rh_call_control (hcd
, urb
);
662 /*-------------------------------------------------------------------------*/
664 /* Unlinks of root-hub control URBs are legal, but they don't do anything
665 * since these URBs always execute synchronously.
667 static int usb_rh_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
672 spin_lock_irqsave(&hcd_root_hub_lock
, flags
);
673 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
677 if (usb_endpoint_num(&urb
->ep
->desc
) == 0) { /* Control URB */
680 } else { /* Status URB */
681 if (!hcd
->uses_new_polling
)
682 del_timer (&hcd
->rh_timer
);
683 if (urb
== hcd
->status_urb
) {
684 hcd
->status_urb
= NULL
;
685 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
687 spin_unlock(&hcd_root_hub_lock
);
688 usb_hcd_giveback_urb(hcd
, urb
, status
);
689 spin_lock(&hcd_root_hub_lock
);
693 spin_unlock_irqrestore(&hcd_root_hub_lock
, flags
);
697 /*-------------------------------------------------------------------------*/
699 static struct class *usb_host_class
;
701 int usb_host_init(void)
705 usb_host_class
= class_create(THIS_MODULE
, "usb_host");
706 if (IS_ERR(usb_host_class
))
707 retval
= PTR_ERR(usb_host_class
);
711 void usb_host_cleanup(void)
713 class_destroy(usb_host_class
);
717 * usb_bus_init - shared initialization code
718 * @bus: the bus structure being initialized
720 * This code is used to initialize a usb_bus structure, memory for which is
721 * separately managed.
723 static void usb_bus_init (struct usb_bus
*bus
)
725 memset (&bus
->devmap
, 0, sizeof(struct usb_devmap
));
727 bus
->devnum_next
= 1;
729 bus
->root_hub
= NULL
;
731 bus
->bandwidth_allocated
= 0;
732 bus
->bandwidth_int_reqs
= 0;
733 bus
->bandwidth_isoc_reqs
= 0;
735 INIT_LIST_HEAD (&bus
->bus_list
);
738 /*-------------------------------------------------------------------------*/
741 * usb_register_bus - registers the USB host controller with the usb core
742 * @bus: pointer to the bus to register
743 * Context: !in_interrupt()
745 * Assigns a bus number, and links the controller into usbcore data
746 * structures so that it can be seen by scanning the bus list.
748 static int usb_register_bus(struct usb_bus
*bus
)
753 mutex_lock(&usb_bus_list_lock
);
754 busnum
= find_next_zero_bit (busmap
.busmap
, USB_MAXBUS
, 1);
755 if (busnum
>= USB_MAXBUS
) {
756 printk (KERN_ERR
"%s: too many buses\n", usbcore_name
);
757 goto error_find_busnum
;
759 set_bit (busnum
, busmap
.busmap
);
760 bus
->busnum
= busnum
;
762 bus
->dev
= device_create(usb_host_class
, bus
->controller
, MKDEV(0, 0),
763 "usb_host%d", busnum
);
764 result
= PTR_ERR(bus
->dev
);
765 if (IS_ERR(bus
->dev
))
766 goto error_create_class_dev
;
767 dev_set_drvdata(bus
->dev
, bus
);
769 /* Add it to the local list of buses */
770 list_add (&bus
->bus_list
, &usb_bus_list
);
771 mutex_unlock(&usb_bus_list_lock
);
773 usb_notify_add_bus(bus
);
775 dev_info (bus
->controller
, "new USB bus registered, assigned bus "
776 "number %d\n", bus
->busnum
);
779 error_create_class_dev
:
780 clear_bit(busnum
, busmap
.busmap
);
782 mutex_unlock(&usb_bus_list_lock
);
787 * usb_deregister_bus - deregisters the USB host controller
788 * @bus: pointer to the bus to deregister
789 * Context: !in_interrupt()
791 * Recycles the bus number, and unlinks the controller from usbcore data
792 * structures so that it won't be seen by scanning the bus list.
794 static void usb_deregister_bus (struct usb_bus
*bus
)
796 dev_info (bus
->controller
, "USB bus %d deregistered\n", bus
->busnum
);
799 * NOTE: make sure that all the devices are removed by the
800 * controller code, as well as having it call this when cleaning
803 mutex_lock(&usb_bus_list_lock
);
804 list_del (&bus
->bus_list
);
805 mutex_unlock(&usb_bus_list_lock
);
807 usb_notify_remove_bus(bus
);
809 clear_bit (bus
->busnum
, busmap
.busmap
);
811 device_unregister(bus
->dev
);
815 * register_root_hub - called by usb_add_hcd() to register a root hub
816 * @hcd: host controller for this root hub
818 * This function registers the root hub with the USB subsystem. It sets up
819 * the device properly in the device tree and then calls usb_new_device()
820 * to register the usb device. It also assigns the root hub's USB address
823 static int register_root_hub(struct usb_hcd
*hcd
)
825 struct device
*parent_dev
= hcd
->self
.controller
;
826 struct usb_device
*usb_dev
= hcd
->self
.root_hub
;
827 const int devnum
= 1;
830 usb_dev
->devnum
= devnum
;
831 usb_dev
->bus
->devnum_next
= devnum
+ 1;
832 memset (&usb_dev
->bus
->devmap
.devicemap
, 0,
833 sizeof usb_dev
->bus
->devmap
.devicemap
);
834 set_bit (devnum
, usb_dev
->bus
->devmap
.devicemap
);
835 usb_set_device_state(usb_dev
, USB_STATE_ADDRESS
);
837 mutex_lock(&usb_bus_list_lock
);
839 usb_dev
->ep0
.desc
.wMaxPacketSize
= __constant_cpu_to_le16(64);
840 retval
= usb_get_device_descriptor(usb_dev
, USB_DT_DEVICE_SIZE
);
841 if (retval
!= sizeof usb_dev
->descriptor
) {
842 mutex_unlock(&usb_bus_list_lock
);
843 dev_dbg (parent_dev
, "can't read %s device descriptor %d\n",
844 usb_dev
->dev
.bus_id
, retval
);
845 return (retval
< 0) ? retval
: -EMSGSIZE
;
848 retval
= usb_new_device (usb_dev
);
850 dev_err (parent_dev
, "can't register root hub for %s, %d\n",
851 usb_dev
->dev
.bus_id
, retval
);
853 mutex_unlock(&usb_bus_list_lock
);
856 spin_lock_irq (&hcd_root_hub_lock
);
857 hcd
->rh_registered
= 1;
858 spin_unlock_irq (&hcd_root_hub_lock
);
860 /* Did the HC die before the root hub was registered? */
861 if (hcd
->state
== HC_STATE_HALT
)
862 usb_hc_died (hcd
); /* This time clean up */
868 void usb_enable_root_hub_irq (struct usb_bus
*bus
)
872 hcd
= container_of (bus
, struct usb_hcd
, self
);
873 if (hcd
->driver
->hub_irq_enable
&& hcd
->state
!= HC_STATE_HALT
)
874 hcd
->driver
->hub_irq_enable (hcd
);
878 /*-------------------------------------------------------------------------*/
881 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
882 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
883 * @is_input: true iff the transaction sends data to the host
884 * @isoc: true for isochronous transactions, false for interrupt ones
885 * @bytecount: how many bytes in the transaction.
887 * Returns approximate bus time in nanoseconds for a periodic transaction.
888 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
889 * scheduled in software, this function is only used for such scheduling.
891 long usb_calc_bus_time (int speed
, int is_input
, int isoc
, int bytecount
)
896 case USB_SPEED_LOW
: /* INTR only */
898 tmp
= (67667L * (31L + 10L * BitTime (bytecount
))) / 1000L;
899 return (64060L + (2 * BW_HUB_LS_SETUP
) + BW_HOST_DELAY
+ tmp
);
901 tmp
= (66700L * (31L + 10L * BitTime (bytecount
))) / 1000L;
902 return (64107L + (2 * BW_HUB_LS_SETUP
) + BW_HOST_DELAY
+ tmp
);
904 case USB_SPEED_FULL
: /* ISOC or INTR */
906 tmp
= (8354L * (31L + 10L * BitTime (bytecount
))) / 1000L;
907 return (((is_input
) ? 7268L : 6265L) + BW_HOST_DELAY
+ tmp
);
909 tmp
= (8354L * (31L + 10L * BitTime (bytecount
))) / 1000L;
910 return (9107L + BW_HOST_DELAY
+ tmp
);
912 case USB_SPEED_HIGH
: /* ISOC or INTR */
913 // FIXME adjust for input vs output
915 tmp
= HS_NSECS_ISO (bytecount
);
917 tmp
= HS_NSECS (bytecount
);
920 pr_debug ("%s: bogus device speed!\n", usbcore_name
);
924 EXPORT_SYMBOL_GPL(usb_calc_bus_time
);
927 /*-------------------------------------------------------------------------*/
930 * Generic HC operations.
933 /*-------------------------------------------------------------------------*/
936 * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
937 * @hcd: host controller to which @urb was submitted
938 * @urb: URB being submitted
940 * Host controller drivers should call this routine in their enqueue()
941 * method. The HCD's private spinlock must be held and interrupts must
942 * be disabled. The actions carried out here are required for URB
943 * submission, as well as for endpoint shutdown and for usb_kill_urb.
945 * Returns 0 for no error, otherwise a negative error code (in which case
946 * the enqueue() method must fail). If no error occurs but enqueue() fails
947 * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
948 * the private spinlock and returning.
950 int usb_hcd_link_urb_to_ep(struct usb_hcd
*hcd
, struct urb
*urb
)
954 spin_lock(&hcd_urb_list_lock
);
956 /* Check that the URB isn't being killed */
957 if (unlikely(atomic_read(&urb
->reject
))) {
962 if (unlikely(!urb
->ep
->enabled
)) {
967 if (unlikely(!urb
->dev
->can_submit
)) {
973 * Check the host controller's state and add the URB to the
976 switch (hcd
->state
) {
977 case HC_STATE_RUNNING
:
978 case HC_STATE_RESUMING
:
980 list_add_tail(&urb
->urb_list
, &urb
->ep
->urb_list
);
987 spin_unlock(&hcd_urb_list_lock
);
990 EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep
);
993 * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
994 * @hcd: host controller to which @urb was submitted
995 * @urb: URB being checked for unlinkability
996 * @status: error code to store in @urb if the unlink succeeds
998 * Host controller drivers should call this routine in their dequeue()
999 * method. The HCD's private spinlock must be held and interrupts must
1000 * be disabled. The actions carried out here are required for making
1001 * sure than an unlink is valid.
1003 * Returns 0 for no error, otherwise a negative error code (in which case
1004 * the dequeue() method must fail). The possible error codes are:
1006 * -EIDRM: @urb was not submitted or has already completed.
1007 * The completion function may not have been called yet.
1009 * -EBUSY: @urb has already been unlinked.
1011 int usb_hcd_check_unlink_urb(struct usb_hcd
*hcd
, struct urb
*urb
,
1014 struct list_head
*tmp
;
1016 /* insist the urb is still queued */
1017 list_for_each(tmp
, &urb
->ep
->urb_list
) {
1018 if (tmp
== &urb
->urb_list
)
1021 if (tmp
!= &urb
->urb_list
)
1024 /* Any status except -EINPROGRESS means something already started to
1025 * unlink this URB from the hardware. So there's no more work to do.
1029 urb
->unlinked
= status
;
1031 /* IRQ setup can easily be broken so that USB controllers
1032 * never get completion IRQs ... maybe even the ones we need to
1033 * finish unlinking the initial failed usb_set_address()
1034 * or device descriptor fetch.
1036 if (!test_bit(HCD_FLAG_SAW_IRQ
, &hcd
->flags
) &&
1037 !is_root_hub(urb
->dev
)) {
1038 dev_warn(hcd
->self
.controller
, "Unlink after no-IRQ? "
1039 "Controller is probably using the wrong IRQ.\n");
1040 set_bit(HCD_FLAG_SAW_IRQ
, &hcd
->flags
);
1045 EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb
);
1048 * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
1049 * @hcd: host controller to which @urb was submitted
1050 * @urb: URB being unlinked
1052 * Host controller drivers should call this routine before calling
1053 * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
1054 * interrupts must be disabled. The actions carried out here are required
1055 * for URB completion.
1057 void usb_hcd_unlink_urb_from_ep(struct usb_hcd
*hcd
, struct urb
*urb
)
1059 /* clear all state linking urb to this dev (and hcd) */
1060 spin_lock(&hcd_urb_list_lock
);
1061 list_del_init(&urb
->urb_list
);
1062 spin_unlock(&hcd_urb_list_lock
);
1064 EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep
);
1067 * Some usb host controllers can only perform dma using a small SRAM area.
1068 * The usb core itself is however optimized for host controllers that can dma
1069 * using regular system memory - like pci devices doing bus mastering.
1071 * To support host controllers with limited dma capabilites we provide dma
1072 * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
1073 * For this to work properly the host controller code must first use the
1074 * function dma_declare_coherent_memory() to point out which memory area
1075 * that should be used for dma allocations.
1077 * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
1078 * dma using dma_alloc_coherent() which in turn allocates from the memory
1079 * area pointed out with dma_declare_coherent_memory().
1081 * So, to summarize...
1083 * - We need "local" memory, canonical example being
1084 * a small SRAM on a discrete controller being the
1085 * only memory that the controller can read ...
1086 * (a) "normal" kernel memory is no good, and
1087 * (b) there's not enough to share
1089 * - The only *portable* hook for such stuff in the
1090 * DMA framework is dma_declare_coherent_memory()
1092 * - So we use that, even though the primary requirement
1093 * is that the memory be "local" (hence addressible
1094 * by that device), not "coherent".
1098 static int hcd_alloc_coherent(struct usb_bus
*bus
,
1099 gfp_t mem_flags
, dma_addr_t
*dma_handle
,
1100 void **vaddr_handle
, size_t size
,
1101 enum dma_data_direction dir
)
1103 unsigned char *vaddr
;
1105 vaddr
= hcd_buffer_alloc(bus
, size
+ sizeof(vaddr
),
1106 mem_flags
, dma_handle
);
1111 * Store the virtual address of the buffer at the end
1112 * of the allocated dma buffer. The size of the buffer
1113 * may be uneven so use unaligned functions instead
1114 * of just rounding up. It makes sense to optimize for
1115 * memory footprint over access speed since the amount
1116 * of memory available for dma may be limited.
1118 put_unaligned((unsigned long)*vaddr_handle
,
1119 (unsigned long *)(vaddr
+ size
));
1121 if (dir
== DMA_TO_DEVICE
)
1122 memcpy(vaddr
, *vaddr_handle
, size
);
1124 *vaddr_handle
= vaddr
;
1128 static void hcd_free_coherent(struct usb_bus
*bus
, dma_addr_t
*dma_handle
,
1129 void **vaddr_handle
, size_t size
,
1130 enum dma_data_direction dir
)
1132 unsigned char *vaddr
= *vaddr_handle
;
1134 vaddr
= (void *)get_unaligned((unsigned long *)(vaddr
+ size
));
1136 if (dir
== DMA_FROM_DEVICE
)
1137 memcpy(vaddr
, *vaddr_handle
, size
);
1139 hcd_buffer_free(bus
, size
+ sizeof(vaddr
), *vaddr_handle
, *dma_handle
);
1141 *vaddr_handle
= vaddr
;
1145 static int map_urb_for_dma(struct usb_hcd
*hcd
, struct urb
*urb
,
1148 enum dma_data_direction dir
;
1151 /* Map the URB's buffers for DMA access.
1152 * Lower level HCD code should use *_dma exclusively,
1153 * unless it uses pio or talks to another transport.
1155 if (is_root_hub(urb
->dev
))
1158 if (usb_endpoint_xfer_control(&urb
->ep
->desc
)
1159 && !(urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
)) {
1160 if (hcd
->self
.uses_dma
)
1161 urb
->setup_dma
= dma_map_single(
1162 hcd
->self
.controller
,
1164 sizeof(struct usb_ctrlrequest
),
1166 else if (hcd
->driver
->flags
& HCD_LOCAL_MEM
)
1167 ret
= hcd_alloc_coherent(
1168 urb
->dev
->bus
, mem_flags
,
1170 (void **)&urb
->setup_packet
,
1171 sizeof(struct usb_ctrlrequest
),
1175 dir
= usb_urb_dir_in(urb
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
1176 if (ret
== 0 && urb
->transfer_buffer_length
!= 0
1177 && !(urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
)) {
1178 if (hcd
->self
.uses_dma
)
1179 urb
->transfer_dma
= dma_map_single (
1180 hcd
->self
.controller
,
1181 urb
->transfer_buffer
,
1182 urb
->transfer_buffer_length
,
1184 else if (hcd
->driver
->flags
& HCD_LOCAL_MEM
) {
1185 ret
= hcd_alloc_coherent(
1186 urb
->dev
->bus
, mem_flags
,
1188 &urb
->transfer_buffer
,
1189 urb
->transfer_buffer_length
,
1192 if (ret
&& usb_endpoint_xfer_control(&urb
->ep
->desc
)
1193 && !(urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
))
1194 hcd_free_coherent(urb
->dev
->bus
,
1196 (void **)&urb
->setup_packet
,
1197 sizeof(struct usb_ctrlrequest
),
1204 static void unmap_urb_for_dma(struct usb_hcd
*hcd
, struct urb
*urb
)
1206 enum dma_data_direction dir
;
1208 if (is_root_hub(urb
->dev
))
1211 if (usb_endpoint_xfer_control(&urb
->ep
->desc
)
1212 && !(urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
)) {
1213 if (hcd
->self
.uses_dma
)
1214 dma_unmap_single(hcd
->self
.controller
, urb
->setup_dma
,
1215 sizeof(struct usb_ctrlrequest
),
1217 else if (hcd
->driver
->flags
& HCD_LOCAL_MEM
)
1218 hcd_free_coherent(urb
->dev
->bus
, &urb
->setup_dma
,
1219 (void **)&urb
->setup_packet
,
1220 sizeof(struct usb_ctrlrequest
),
1224 dir
= usb_urb_dir_in(urb
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
1225 if (urb
->transfer_buffer_length
!= 0
1226 && !(urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
)) {
1227 if (hcd
->self
.uses_dma
)
1228 dma_unmap_single(hcd
->self
.controller
,
1230 urb
->transfer_buffer_length
,
1232 else if (hcd
->driver
->flags
& HCD_LOCAL_MEM
)
1233 hcd_free_coherent(urb
->dev
->bus
, &urb
->transfer_dma
,
1234 &urb
->transfer_buffer
,
1235 urb
->transfer_buffer_length
,
1240 /*-------------------------------------------------------------------------*/
1242 /* may be called in any context with a valid urb->dev usecount
1243 * caller surrenders "ownership" of urb
1244 * expects usb_submit_urb() to have sanity checked and conditioned all
1247 int usb_hcd_submit_urb (struct urb
*urb
, gfp_t mem_flags
)
1250 struct usb_hcd
*hcd
= bus_to_hcd(urb
->dev
->bus
);
1252 /* increment urb's reference count as part of giving it to the HCD
1253 * (which will control it). HCD guarantees that it either returns
1254 * an error or calls giveback(), but not both.
1257 atomic_inc(&urb
->use_count
);
1258 atomic_inc(&urb
->dev
->urbnum
);
1259 usbmon_urb_submit(&hcd
->self
, urb
);
1261 /* NOTE requirements on root-hub callers (usbfs and the hub
1262 * driver, for now): URBs' urb->transfer_buffer must be
1263 * valid and usb_buffer_{sync,unmap}() not be needed, since
1264 * they could clobber root hub response data. Also, control
1265 * URBs must be submitted in process context with interrupts
1268 status
= map_urb_for_dma(hcd
, urb
, mem_flags
);
1269 if (unlikely(status
)) {
1270 usbmon_urb_submit_error(&hcd
->self
, urb
, status
);
1274 if (is_root_hub(urb
->dev
))
1275 status
= rh_urb_enqueue(hcd
, urb
);
1277 status
= hcd
->driver
->urb_enqueue(hcd
, urb
, mem_flags
);
1279 if (unlikely(status
)) {
1280 usbmon_urb_submit_error(&hcd
->self
, urb
, status
);
1281 unmap_urb_for_dma(hcd
, urb
);
1284 INIT_LIST_HEAD(&urb
->urb_list
);
1285 atomic_dec(&urb
->use_count
);
1286 atomic_dec(&urb
->dev
->urbnum
);
1287 if (atomic_read(&urb
->reject
))
1288 wake_up(&usb_kill_urb_queue
);
1294 /*-------------------------------------------------------------------------*/
1296 /* this makes the hcd giveback() the urb more quickly, by kicking it
1297 * off hardware queues (which may take a while) and returning it as
1298 * soon as practical. we've already set up the urb's return status,
1299 * but we can't know if the callback completed already.
1301 static int unlink1(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1305 if (is_root_hub(urb
->dev
))
1306 value
= usb_rh_urb_dequeue(hcd
, urb
, status
);
1309 /* The only reason an HCD might fail this call is if
1310 * it has not yet fully queued the urb to begin with.
1311 * Such failures should be harmless. */
1312 value
= hcd
->driver
->urb_dequeue(hcd
, urb
, status
);
1318 * called in any context
1320 * caller guarantees urb won't be recycled till both unlink()
1321 * and the urb's completion function return
1323 int usb_hcd_unlink_urb (struct urb
*urb
, int status
)
1325 struct usb_hcd
*hcd
;
1326 int retval
= -EIDRM
;
1327 unsigned long flags
;
1329 /* Prevent the device and bus from going away while
1330 * the unlink is carried out. If they are already gone
1331 * then urb->use_count must be 0, since disconnected
1332 * devices can't have any active URBs.
1334 spin_lock_irqsave(&hcd_urb_unlink_lock
, flags
);
1335 if (atomic_read(&urb
->use_count
) > 0) {
1337 usb_get_dev(urb
->dev
);
1339 spin_unlock_irqrestore(&hcd_urb_unlink_lock
, flags
);
1341 hcd
= bus_to_hcd(urb
->dev
->bus
);
1342 retval
= unlink1(hcd
, urb
, status
);
1343 usb_put_dev(urb
->dev
);
1347 retval
= -EINPROGRESS
;
1348 else if (retval
!= -EIDRM
&& retval
!= -EBUSY
)
1349 dev_dbg(&urb
->dev
->dev
, "hcd_unlink_urb %p fail %d\n",
1354 /*-------------------------------------------------------------------------*/
1357 * usb_hcd_giveback_urb - return URB from HCD to device driver
1358 * @hcd: host controller returning the URB
1359 * @urb: urb being returned to the USB device driver.
1360 * @status: completion status code for the URB.
1361 * Context: in_interrupt()
1363 * This hands the URB from HCD to its USB device driver, using its
1364 * completion function. The HCD has freed all per-urb resources
1365 * (and is done using urb->hcpriv). It also released all HCD locks;
1366 * the device driver won't cause problems if it frees, modifies,
1367 * or resubmits this URB.
1369 * If @urb was unlinked, the value of @status will be overridden by
1370 * @urb->unlinked. Erroneous short transfers are detected in case
1371 * the HCD hasn't checked for them.
1373 void usb_hcd_giveback_urb(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1376 if (unlikely(urb
->unlinked
))
1377 status
= urb
->unlinked
;
1378 else if (unlikely((urb
->transfer_flags
& URB_SHORT_NOT_OK
) &&
1379 urb
->actual_length
< urb
->transfer_buffer_length
&&
1381 status
= -EREMOTEIO
;
1383 unmap_urb_for_dma(hcd
, urb
);
1384 usbmon_urb_complete(&hcd
->self
, urb
, status
);
1385 usb_unanchor_urb(urb
);
1387 /* pass ownership to the completion handler */
1388 urb
->status
= status
;
1389 urb
->complete (urb
);
1390 atomic_dec (&urb
->use_count
);
1391 if (unlikely(atomic_read(&urb
->reject
)))
1392 wake_up (&usb_kill_urb_queue
);
1395 EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb
);
1397 /*-------------------------------------------------------------------------*/
1399 /* Cancel all URBs pending on this endpoint and wait for the endpoint's
1400 * queue to drain completely. The caller must first insure that no more
1401 * URBs can be submitted for this endpoint.
1403 void usb_hcd_flush_endpoint(struct usb_device
*udev
,
1404 struct usb_host_endpoint
*ep
)
1406 struct usb_hcd
*hcd
;
1412 hcd
= bus_to_hcd(udev
->bus
);
1414 /* No more submits can occur */
1415 spin_lock_irq(&hcd_urb_list_lock
);
1417 list_for_each_entry (urb
, &ep
->urb_list
, urb_list
) {
1423 is_in
= usb_urb_dir_in(urb
);
1424 spin_unlock(&hcd_urb_list_lock
);
1427 unlink1(hcd
, urb
, -ESHUTDOWN
);
1428 dev_dbg (hcd
->self
.controller
,
1429 "shutdown urb %p ep%d%s%s\n",
1430 urb
, usb_endpoint_num(&ep
->desc
),
1431 is_in
? "in" : "out",
1434 switch (usb_endpoint_type(&ep
->desc
)) {
1435 case USB_ENDPOINT_XFER_CONTROL
:
1437 case USB_ENDPOINT_XFER_BULK
:
1439 case USB_ENDPOINT_XFER_INT
:
1448 /* list contents may have changed */
1449 spin_lock(&hcd_urb_list_lock
);
1452 spin_unlock_irq(&hcd_urb_list_lock
);
1454 /* Wait until the endpoint queue is completely empty */
1455 while (!list_empty (&ep
->urb_list
)) {
1456 spin_lock_irq(&hcd_urb_list_lock
);
1458 /* The list may have changed while we acquired the spinlock */
1460 if (!list_empty (&ep
->urb_list
)) {
1461 urb
= list_entry (ep
->urb_list
.prev
, struct urb
,
1465 spin_unlock_irq(&hcd_urb_list_lock
);
1474 /* Disables the endpoint: synchronizes with the hcd to make sure all
1475 * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
1476 * have been called previously. Use for set_configuration, set_interface,
1477 * driver removal, physical disconnect.
1479 * example: a qh stored in ep->hcpriv, holding state related to endpoint
1480 * type, maxpacket size, toggle, halt status, and scheduling.
1482 void usb_hcd_disable_endpoint(struct usb_device
*udev
,
1483 struct usb_host_endpoint
*ep
)
1485 struct usb_hcd
*hcd
;
1488 hcd
= bus_to_hcd(udev
->bus
);
1489 if (hcd
->driver
->endpoint_disable
)
1490 hcd
->driver
->endpoint_disable(hcd
, ep
);
1493 /* Protect against drivers that try to unlink URBs after the device
1494 * is gone, by waiting until all unlinks for @udev are finished.
1495 * Since we don't currently track URBs by device, simply wait until
1496 * nothing is running in the locked region of usb_hcd_unlink_urb().
1498 void usb_hcd_synchronize_unlinks(struct usb_device
*udev
)
1500 spin_lock_irq(&hcd_urb_unlink_lock
);
1501 spin_unlock_irq(&hcd_urb_unlink_lock
);
1504 /*-------------------------------------------------------------------------*/
1506 /* called in any context */
1507 int usb_hcd_get_frame_number (struct usb_device
*udev
)
1509 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1511 if (!HC_IS_RUNNING (hcd
->state
))
1513 return hcd
->driver
->get_frame_number (hcd
);
1516 /*-------------------------------------------------------------------------*/
1520 int hcd_bus_suspend(struct usb_device
*rhdev
)
1522 struct usb_hcd
*hcd
= container_of(rhdev
->bus
, struct usb_hcd
, self
);
1524 int old_state
= hcd
->state
;
1526 dev_dbg(&rhdev
->dev
, "bus %s%s\n",
1527 rhdev
->auto_pm
? "auto-" : "", "suspend");
1528 if (!hcd
->driver
->bus_suspend
) {
1531 hcd
->state
= HC_STATE_QUIESCING
;
1532 status
= hcd
->driver
->bus_suspend(hcd
);
1535 usb_set_device_state(rhdev
, USB_STATE_SUSPENDED
);
1536 hcd
->state
= HC_STATE_SUSPENDED
;
1538 hcd
->state
= old_state
;
1539 dev_dbg(&rhdev
->dev
, "bus %s fail, err %d\n",
1545 int hcd_bus_resume(struct usb_device
*rhdev
)
1547 struct usb_hcd
*hcd
= container_of(rhdev
->bus
, struct usb_hcd
, self
);
1549 int old_state
= hcd
->state
;
1551 dev_dbg(&rhdev
->dev
, "usb %s%s\n",
1552 rhdev
->auto_pm
? "auto-" : "", "resume");
1553 if (!hcd
->driver
->bus_resume
)
1555 if (hcd
->state
== HC_STATE_RUNNING
)
1558 hcd
->state
= HC_STATE_RESUMING
;
1559 status
= hcd
->driver
->bus_resume(hcd
);
1561 /* TRSMRCY = 10 msec */
1563 usb_set_device_state(rhdev
, rhdev
->actconfig
1564 ? USB_STATE_CONFIGURED
1565 : USB_STATE_ADDRESS
);
1566 hcd
->state
= HC_STATE_RUNNING
;
1568 hcd
->state
= old_state
;
1569 dev_dbg(&rhdev
->dev
, "bus %s fail, err %d\n",
1571 if (status
!= -ESHUTDOWN
)
1577 /* Workqueue routine for root-hub remote wakeup */
1578 static void hcd_resume_work(struct work_struct
*work
)
1580 struct usb_hcd
*hcd
= container_of(work
, struct usb_hcd
, wakeup_work
);
1581 struct usb_device
*udev
= hcd
->self
.root_hub
;
1583 usb_lock_device(udev
);
1584 usb_mark_last_busy(udev
);
1585 usb_external_resume_device(udev
);
1586 usb_unlock_device(udev
);
1590 * usb_hcd_resume_root_hub - called by HCD to resume its root hub
1591 * @hcd: host controller for this root hub
1593 * The USB host controller calls this function when its root hub is
1594 * suspended (with the remote wakeup feature enabled) and a remote
1595 * wakeup request is received. The routine submits a workqueue request
1596 * to resume the root hub (that is, manage its downstream ports again).
1598 void usb_hcd_resume_root_hub (struct usb_hcd
*hcd
)
1600 unsigned long flags
;
1602 spin_lock_irqsave (&hcd_root_hub_lock
, flags
);
1603 if (hcd
->rh_registered
)
1604 queue_work(ksuspend_usb_wq
, &hcd
->wakeup_work
);
1605 spin_unlock_irqrestore (&hcd_root_hub_lock
, flags
);
1607 EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub
);
1611 /*-------------------------------------------------------------------------*/
1613 #ifdef CONFIG_USB_OTG
1616 * usb_bus_start_enum - start immediate enumeration (for OTG)
1617 * @bus: the bus (must use hcd framework)
1618 * @port_num: 1-based number of port; usually bus->otg_port
1619 * Context: in_interrupt()
1621 * Starts enumeration, with an immediate reset followed later by
1622 * khubd identifying and possibly configuring the device.
1623 * This is needed by OTG controller drivers, where it helps meet
1624 * HNP protocol timing requirements for starting a port reset.
1626 int usb_bus_start_enum(struct usb_bus
*bus
, unsigned port_num
)
1628 struct usb_hcd
*hcd
;
1629 int status
= -EOPNOTSUPP
;
1631 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
1632 * boards with root hubs hooked up to internal devices (instead of
1633 * just the OTG port) may need more attention to resetting...
1635 hcd
= container_of (bus
, struct usb_hcd
, self
);
1636 if (port_num
&& hcd
->driver
->start_port_reset
)
1637 status
= hcd
->driver
->start_port_reset(hcd
, port_num
);
1639 /* run khubd shortly after (first) root port reset finishes;
1640 * it may issue others, until at least 50 msecs have passed.
1643 mod_timer(&hcd
->rh_timer
, jiffies
+ msecs_to_jiffies(10));
1646 EXPORT_SYMBOL_GPL(usb_bus_start_enum
);
1650 /*-------------------------------------------------------------------------*/
1653 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
1654 * @irq: the IRQ being raised
1655 * @__hcd: pointer to the HCD whose IRQ is being signaled
1657 * If the controller isn't HALTed, calls the driver's irq handler.
1658 * Checks whether the controller is now dead.
1660 irqreturn_t
usb_hcd_irq (int irq
, void *__hcd
)
1662 struct usb_hcd
*hcd
= __hcd
;
1663 unsigned long flags
;
1666 /* IRQF_DISABLED doesn't work correctly with shared IRQs
1667 * when the first handler doesn't use it. So let's just
1668 * assume it's never used.
1670 local_irq_save(flags
);
1672 if (unlikely(hcd
->state
== HC_STATE_HALT
||
1673 !test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
))) {
1675 } else if (hcd
->driver
->irq(hcd
) == IRQ_NONE
) {
1678 set_bit(HCD_FLAG_SAW_IRQ
, &hcd
->flags
);
1680 if (unlikely(hcd
->state
== HC_STATE_HALT
))
1685 local_irq_restore(flags
);
1689 /*-------------------------------------------------------------------------*/
1692 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
1693 * @hcd: pointer to the HCD representing the controller
1695 * This is called by bus glue to report a USB host controller that died
1696 * while operations may still have been pending. It's called automatically
1697 * by the PCI glue, so only glue for non-PCI busses should need to call it.
1699 void usb_hc_died (struct usb_hcd
*hcd
)
1701 unsigned long flags
;
1703 dev_err (hcd
->self
.controller
, "HC died; cleaning up\n");
1705 spin_lock_irqsave (&hcd_root_hub_lock
, flags
);
1706 if (hcd
->rh_registered
) {
1709 /* make khubd clean up old urbs and devices */
1710 usb_set_device_state (hcd
->self
.root_hub
,
1711 USB_STATE_NOTATTACHED
);
1712 usb_kick_khubd (hcd
->self
.root_hub
);
1714 spin_unlock_irqrestore (&hcd_root_hub_lock
, flags
);
1716 EXPORT_SYMBOL_GPL (usb_hc_died
);
1718 /*-------------------------------------------------------------------------*/
1721 * usb_create_hcd - create and initialize an HCD structure
1722 * @driver: HC driver that will use this hcd
1723 * @dev: device for this HC, stored in hcd->self.controller
1724 * @bus_name: value to store in hcd->self.bus_name
1725 * Context: !in_interrupt()
1727 * Allocate a struct usb_hcd, with extra space at the end for the
1728 * HC driver's private data. Initialize the generic members of the
1731 * If memory is unavailable, returns NULL.
1733 struct usb_hcd
*usb_create_hcd (const struct hc_driver
*driver
,
1734 struct device
*dev
, char *bus_name
)
1736 struct usb_hcd
*hcd
;
1738 hcd
= kzalloc(sizeof(*hcd
) + driver
->hcd_priv_size
, GFP_KERNEL
);
1740 dev_dbg (dev
, "hcd alloc failed\n");
1743 dev_set_drvdata(dev
, hcd
);
1744 kref_init(&hcd
->kref
);
1746 usb_bus_init(&hcd
->self
);
1747 hcd
->self
.controller
= dev
;
1748 hcd
->self
.bus_name
= bus_name
;
1749 hcd
->self
.uses_dma
= (dev
->dma_mask
!= NULL
);
1751 init_timer(&hcd
->rh_timer
);
1752 hcd
->rh_timer
.function
= rh_timer_func
;
1753 hcd
->rh_timer
.data
= (unsigned long) hcd
;
1755 INIT_WORK(&hcd
->wakeup_work
, hcd_resume_work
);
1758 hcd
->driver
= driver
;
1759 hcd
->product_desc
= (driver
->product_desc
) ? driver
->product_desc
:
1760 "USB Host Controller";
1764 EXPORT_SYMBOL_GPL(usb_create_hcd
);
1766 static void hcd_release (struct kref
*kref
)
1768 struct usb_hcd
*hcd
= container_of (kref
, struct usb_hcd
, kref
);
1773 struct usb_hcd
*usb_get_hcd (struct usb_hcd
*hcd
)
1776 kref_get (&hcd
->kref
);
1779 EXPORT_SYMBOL_GPL(usb_get_hcd
);
1781 void usb_put_hcd (struct usb_hcd
*hcd
)
1784 kref_put (&hcd
->kref
, hcd_release
);
1786 EXPORT_SYMBOL_GPL(usb_put_hcd
);
1789 * usb_add_hcd - finish generic HCD structure initialization and register
1790 * @hcd: the usb_hcd structure to initialize
1791 * @irqnum: Interrupt line to allocate
1792 * @irqflags: Interrupt type flags
1794 * Finish the remaining parts of generic HCD initialization: allocate the
1795 * buffers of consistent memory, register the bus, request the IRQ line,
1796 * and call the driver's reset() and start() routines.
1798 int usb_add_hcd(struct usb_hcd
*hcd
,
1799 unsigned int irqnum
, unsigned long irqflags
)
1802 struct usb_device
*rhdev
;
1804 dev_info(hcd
->self
.controller
, "%s\n", hcd
->product_desc
);
1806 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
1808 /* HC is in reset state, but accessible. Now do the one-time init,
1809 * bottom up so that hcds can customize the root hubs before khubd
1810 * starts talking to them. (Note, bus id is assigned early too.)
1812 if ((retval
= hcd_buffer_create(hcd
)) != 0) {
1813 dev_dbg(hcd
->self
.controller
, "pool alloc failed\n");
1817 if ((retval
= usb_register_bus(&hcd
->self
)) < 0)
1818 goto err_register_bus
;
1820 if ((rhdev
= usb_alloc_dev(NULL
, &hcd
->self
, 0)) == NULL
) {
1821 dev_err(hcd
->self
.controller
, "unable to allocate root hub\n");
1823 goto err_allocate_root_hub
;
1825 rhdev
->speed
= (hcd
->driver
->flags
& HCD_USB2
) ? USB_SPEED_HIGH
:
1827 hcd
->self
.root_hub
= rhdev
;
1829 /* wakeup flag init defaults to "everything works" for root hubs,
1830 * but drivers can override it in reset() if needed, along with
1831 * recording the overall controller's system wakeup capability.
1833 device_init_wakeup(&rhdev
->dev
, 1);
1835 /* "reset" is misnamed; its role is now one-time init. the controller
1836 * should already have been reset (and boot firmware kicked off etc).
1838 if (hcd
->driver
->reset
&& (retval
= hcd
->driver
->reset(hcd
)) < 0) {
1839 dev_err(hcd
->self
.controller
, "can't setup\n");
1840 goto err_hcd_driver_setup
;
1843 /* NOTE: root hub and controller capabilities may not be the same */
1844 if (device_can_wakeup(hcd
->self
.controller
)
1845 && device_can_wakeup(&hcd
->self
.root_hub
->dev
))
1846 dev_dbg(hcd
->self
.controller
, "supports USB remote wakeup\n");
1848 /* enable irqs just before we start the controller */
1849 if (hcd
->driver
->irq
) {
1851 /* IRQF_DISABLED doesn't work as advertised when used together
1852 * with IRQF_SHARED. As usb_hcd_irq() will always disable
1853 * interrupts we can remove it here.
1855 if (irqflags
& IRQF_SHARED
)
1856 irqflags
&= ~IRQF_DISABLED
;
1858 snprintf(hcd
->irq_descr
, sizeof(hcd
->irq_descr
), "%s:usb%d",
1859 hcd
->driver
->description
, hcd
->self
.busnum
);
1860 if ((retval
= request_irq(irqnum
, &usb_hcd_irq
, irqflags
,
1861 hcd
->irq_descr
, hcd
)) != 0) {
1862 dev_err(hcd
->self
.controller
,
1863 "request interrupt %d failed\n", irqnum
);
1864 goto err_request_irq
;
1867 dev_info(hcd
->self
.controller
, "irq %d, %s 0x%08llx\n", irqnum
,
1868 (hcd
->driver
->flags
& HCD_MEMORY
) ?
1869 "io mem" : "io base",
1870 (unsigned long long)hcd
->rsrc_start
);
1873 if (hcd
->rsrc_start
)
1874 dev_info(hcd
->self
.controller
, "%s 0x%08llx\n",
1875 (hcd
->driver
->flags
& HCD_MEMORY
) ?
1876 "io mem" : "io base",
1877 (unsigned long long)hcd
->rsrc_start
);
1880 if ((retval
= hcd
->driver
->start(hcd
)) < 0) {
1881 dev_err(hcd
->self
.controller
, "startup error %d\n", retval
);
1882 goto err_hcd_driver_start
;
1885 /* starting here, usbcore will pay attention to this root hub */
1886 rhdev
->bus_mA
= min(500u, hcd
->power_budget
);
1887 if ((retval
= register_root_hub(hcd
)) != 0)
1888 goto err_register_root_hub
;
1890 if (hcd
->uses_new_polling
&& hcd
->poll_rh
)
1891 usb_hcd_poll_rh_status(hcd
);
1894 err_register_root_hub
:
1895 hcd
->driver
->stop(hcd
);
1896 err_hcd_driver_start
:
1898 free_irq(irqnum
, hcd
);
1900 err_hcd_driver_setup
:
1901 hcd
->self
.root_hub
= NULL
;
1903 err_allocate_root_hub
:
1904 usb_deregister_bus(&hcd
->self
);
1906 hcd_buffer_destroy(hcd
);
1909 EXPORT_SYMBOL_GPL(usb_add_hcd
);
1912 * usb_remove_hcd - shutdown processing for generic HCDs
1913 * @hcd: the usb_hcd structure to remove
1914 * Context: !in_interrupt()
1916 * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
1917 * invoking the HCD's stop() method.
1919 void usb_remove_hcd(struct usb_hcd
*hcd
)
1921 dev_info(hcd
->self
.controller
, "remove, state %x\n", hcd
->state
);
1923 if (HC_IS_RUNNING (hcd
->state
))
1924 hcd
->state
= HC_STATE_QUIESCING
;
1926 dev_dbg(hcd
->self
.controller
, "roothub graceful disconnect\n");
1927 spin_lock_irq (&hcd_root_hub_lock
);
1928 hcd
->rh_registered
= 0;
1929 spin_unlock_irq (&hcd_root_hub_lock
);
1932 cancel_work_sync(&hcd
->wakeup_work
);
1935 mutex_lock(&usb_bus_list_lock
);
1936 usb_disconnect(&hcd
->self
.root_hub
);
1937 mutex_unlock(&usb_bus_list_lock
);
1939 hcd
->driver
->stop(hcd
);
1940 hcd
->state
= HC_STATE_HALT
;
1943 del_timer_sync(&hcd
->rh_timer
);
1946 free_irq(hcd
->irq
, hcd
);
1947 usb_deregister_bus(&hcd
->self
);
1948 hcd_buffer_destroy(hcd
);
1950 EXPORT_SYMBOL_GPL(usb_remove_hcd
);
1953 usb_hcd_platform_shutdown(struct platform_device
* dev
)
1955 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
1957 if (hcd
->driver
->shutdown
)
1958 hcd
->driver
->shutdown(hcd
);
1960 EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown
);
1962 /*-------------------------------------------------------------------------*/
1964 #if defined(CONFIG_USB_MON)
1966 struct usb_mon_operations
*mon_ops
;
1969 * The registration is unlocked.
1970 * We do it this way because we do not want to lock in hot paths.
1972 * Notice that the code is minimally error-proof. Because usbmon needs
1973 * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
1976 int usb_mon_register (struct usb_mon_operations
*ops
)
1986 EXPORT_SYMBOL_GPL (usb_mon_register
);
1988 void usb_mon_deregister (void)
1991 if (mon_ops
== NULL
) {
1992 printk(KERN_ERR
"USB: monitor was not registered\n");
1998 EXPORT_SYMBOL_GPL (usb_mon_deregister
);
2000 #endif /* CONFIG_USB_MON */