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 <linux/device.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/mutex.h>
37 #include <asm/byteorder.h>
38 #include <asm/unaligned.h>
39 #include <linux/platform_device.h>
40 #include <linux/workqueue.h>
41 #include <linux/mutex.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 3.0 root hub device descriptor */
133 static const u8 usb3_rh_dev_descriptor
[18] = {
134 0x12, /* __u8 bLength; */
135 0x01, /* __u8 bDescriptorType; Device */
136 0x00, 0x03, /* __le16 bcdUSB; v3.0 */
138 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
139 0x00, /* __u8 bDeviceSubClass; */
140 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */
141 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 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 /* usb 2.0 root hub device descriptor */
154 static const u8 usb2_rh_dev_descriptor
[18] = {
155 0x12, /* __u8 bLength; */
156 0x01, /* __u8 bDescriptorType; Device */
157 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
159 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
160 0x00, /* __u8 bDeviceSubClass; */
161 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
162 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
164 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
165 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
166 KERNEL_VER
, KERNEL_REL
, /* __le16 bcdDevice */
168 0x03, /* __u8 iManufacturer; */
169 0x02, /* __u8 iProduct; */
170 0x01, /* __u8 iSerialNumber; */
171 0x01 /* __u8 bNumConfigurations; */
174 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
176 /* usb 1.1 root hub device descriptor */
177 static const u8 usb11_rh_dev_descriptor
[18] = {
178 0x12, /* __u8 bLength; */
179 0x01, /* __u8 bDescriptorType; Device */
180 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
182 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
183 0x00, /* __u8 bDeviceSubClass; */
184 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
185 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
187 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
188 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
189 KERNEL_VER
, KERNEL_REL
, /* __le16 bcdDevice */
191 0x03, /* __u8 iManufacturer; */
192 0x02, /* __u8 iProduct; */
193 0x01, /* __u8 iSerialNumber; */
194 0x01 /* __u8 bNumConfigurations; */
198 /*-------------------------------------------------------------------------*/
200 /* Configuration descriptors for our root hubs */
202 static const u8 fs_rh_config_descriptor
[] = {
204 /* one configuration */
205 0x09, /* __u8 bLength; */
206 0x02, /* __u8 bDescriptorType; Configuration */
207 0x19, 0x00, /* __le16 wTotalLength; */
208 0x01, /* __u8 bNumInterfaces; (1) */
209 0x01, /* __u8 bConfigurationValue; */
210 0x00, /* __u8 iConfiguration; */
211 0xc0, /* __u8 bmAttributes;
216 0x00, /* __u8 MaxPower; */
219 * USB 2.0, single TT organization (mandatory):
220 * one interface, protocol 0
222 * USB 2.0, multiple TT organization (optional):
223 * two interfaces, protocols 1 (like single TT)
224 * and 2 (multiple TT mode) ... config is
230 0x09, /* __u8 if_bLength; */
231 0x04, /* __u8 if_bDescriptorType; Interface */
232 0x00, /* __u8 if_bInterfaceNumber; */
233 0x00, /* __u8 if_bAlternateSetting; */
234 0x01, /* __u8 if_bNumEndpoints; */
235 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
236 0x00, /* __u8 if_bInterfaceSubClass; */
237 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
238 0x00, /* __u8 if_iInterface; */
240 /* one endpoint (status change endpoint) */
241 0x07, /* __u8 ep_bLength; */
242 0x05, /* __u8 ep_bDescriptorType; Endpoint */
243 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
244 0x03, /* __u8 ep_bmAttributes; Interrupt */
245 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
246 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
249 static const u8 hs_rh_config_descriptor
[] = {
251 /* one configuration */
252 0x09, /* __u8 bLength; */
253 0x02, /* __u8 bDescriptorType; Configuration */
254 0x19, 0x00, /* __le16 wTotalLength; */
255 0x01, /* __u8 bNumInterfaces; (1) */
256 0x01, /* __u8 bConfigurationValue; */
257 0x00, /* __u8 iConfiguration; */
258 0xc0, /* __u8 bmAttributes;
263 0x00, /* __u8 MaxPower; */
266 * USB 2.0, single TT organization (mandatory):
267 * one interface, protocol 0
269 * USB 2.0, multiple TT organization (optional):
270 * two interfaces, protocols 1 (like single TT)
271 * and 2 (multiple TT mode) ... config is
277 0x09, /* __u8 if_bLength; */
278 0x04, /* __u8 if_bDescriptorType; Interface */
279 0x00, /* __u8 if_bInterfaceNumber; */
280 0x00, /* __u8 if_bAlternateSetting; */
281 0x01, /* __u8 if_bNumEndpoints; */
282 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
283 0x00, /* __u8 if_bInterfaceSubClass; */
284 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
285 0x00, /* __u8 if_iInterface; */
287 /* one endpoint (status change endpoint) */
288 0x07, /* __u8 ep_bLength; */
289 0x05, /* __u8 ep_bDescriptorType; Endpoint */
290 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
291 0x03, /* __u8 ep_bmAttributes; Interrupt */
292 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
293 * see hub.c:hub_configure() for details. */
294 (USB_MAXCHILDREN
+ 1 + 7) / 8, 0x00,
295 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
298 static const u8 ss_rh_config_descriptor
[] = {
299 /* one configuration */
300 0x09, /* __u8 bLength; */
301 0x02, /* __u8 bDescriptorType; Configuration */
302 0x19, 0x00, /* __le16 wTotalLength; FIXME */
303 0x01, /* __u8 bNumInterfaces; (1) */
304 0x01, /* __u8 bConfigurationValue; */
305 0x00, /* __u8 iConfiguration; */
306 0xc0, /* __u8 bmAttributes;
311 0x00, /* __u8 MaxPower; */
314 0x09, /* __u8 if_bLength; */
315 0x04, /* __u8 if_bDescriptorType; Interface */
316 0x00, /* __u8 if_bInterfaceNumber; */
317 0x00, /* __u8 if_bAlternateSetting; */
318 0x01, /* __u8 if_bNumEndpoints; */
319 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
320 0x00, /* __u8 if_bInterfaceSubClass; */
321 0x00, /* __u8 if_bInterfaceProtocol; */
322 0x00, /* __u8 if_iInterface; */
324 /* one endpoint (status change endpoint) */
325 0x07, /* __u8 ep_bLength; */
326 0x05, /* __u8 ep_bDescriptorType; Endpoint */
327 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
328 0x03, /* __u8 ep_bmAttributes; Interrupt */
329 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
330 * see hub.c:hub_configure() for details. */
331 (USB_MAXCHILDREN
+ 1 + 7) / 8, 0x00,
332 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
334 * All 3.0 hubs should have an endpoint companion descriptor,
335 * but we're ignoring that for now. FIXME?
339 /*-------------------------------------------------------------------------*/
342 * ascii2desc() - Helper routine for producing UTF-16LE string descriptors
343 * @s: Null-terminated ASCII (actually ISO-8859-1) string
344 * @buf: Buffer for USB string descriptor (header + UTF-16LE)
345 * @len: Length (in bytes; may be odd) of descriptor buffer.
347 * The return value is the number of bytes filled in: 2 + 2*strlen(s) or
348 * buflen, whichever is less.
350 * USB String descriptors can contain at most 126 characters; input
351 * strings longer than that are truncated.
354 ascii2desc(char const *s
, u8
*buf
, unsigned len
)
356 unsigned n
, t
= 2 + 2*strlen(s
);
359 t
= 254; /* Longest possible UTF string descriptor */
363 t
+= USB_DT_STRING
<< 8; /* Now t is first 16 bits to store */
371 t
= (unsigned char)*s
++;
377 * rh_string() - provides string descriptors for root hub
378 * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
379 * @hcd: the host controller for this root hub
380 * @data: buffer for output packet
381 * @len: length of the provided buffer
383 * Produces either a manufacturer, product or serial number string for the
384 * virtual root hub device.
385 * Returns the number of bytes filled in: the length of the descriptor or
386 * of the provided buffer, whichever is less.
389 rh_string(int id
, struct usb_hcd
const *hcd
, u8
*data
, unsigned len
)
393 static char const langids
[4] = {4, USB_DT_STRING
, 0x09, 0x04};
398 /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
399 /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
402 memcpy(data
, langids
, len
);
406 s
= hcd
->self
.bus_name
;
410 s
= hcd
->product_desc
;
414 snprintf (buf
, sizeof buf
, "%s %s %s", init_utsname()->sysname
,
415 init_utsname()->release
, hcd
->driver
->description
);
419 /* Can't happen; caller guarantees it */
423 return ascii2desc(s
, data
, len
);
427 /* Root hub control transfers execute synchronously */
428 static int rh_call_control (struct usb_hcd
*hcd
, struct urb
*urb
)
430 struct usb_ctrlrequest
*cmd
;
431 u16 typeReq
, wValue
, wIndex
, wLength
;
432 u8
*ubuf
= urb
->transfer_buffer
;
433 u8 tbuf
[sizeof (struct usb_hub_descriptor
)]
434 __attribute__((aligned(4)));
435 const u8
*bufp
= tbuf
;
439 u8 patch_protocol
= 0;
443 spin_lock_irq(&hcd_root_hub_lock
);
444 status
= usb_hcd_link_urb_to_ep(hcd
, urb
);
445 spin_unlock_irq(&hcd_root_hub_lock
);
448 urb
->hcpriv
= hcd
; /* Indicate it's queued */
450 cmd
= (struct usb_ctrlrequest
*) urb
->setup_packet
;
451 typeReq
= (cmd
->bRequestType
<< 8) | cmd
->bRequest
;
452 wValue
= le16_to_cpu (cmd
->wValue
);
453 wIndex
= le16_to_cpu (cmd
->wIndex
);
454 wLength
= le16_to_cpu (cmd
->wLength
);
456 if (wLength
> urb
->transfer_buffer_length
)
459 urb
->actual_length
= 0;
462 /* DEVICE REQUESTS */
464 /* The root hub's remote wakeup enable bit is implemented using
465 * driver model wakeup flags. If this system supports wakeup
466 * through USB, userspace may change the default "allow wakeup"
467 * policy through sysfs or these calls.
469 * Most root hubs support wakeup from downstream devices, for
470 * runtime power management (disabling USB clocks and reducing
471 * VBUS power usage). However, not all of them do so; silicon,
472 * board, and BIOS bugs here are not uncommon, so these can't
473 * be treated quite like external hubs.
475 * Likewise, not all root hubs will pass wakeup events upstream,
476 * to wake up the whole system. So don't assume root hub and
477 * controller capabilities are identical.
480 case DeviceRequest
| USB_REQ_GET_STATUS
:
481 tbuf
[0] = (device_may_wakeup(&hcd
->self
.root_hub
->dev
)
482 << USB_DEVICE_REMOTE_WAKEUP
)
483 | (1 << USB_DEVICE_SELF_POWERED
);
487 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
488 if (wValue
== USB_DEVICE_REMOTE_WAKEUP
)
489 device_set_wakeup_enable(&hcd
->self
.root_hub
->dev
, 0);
493 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
494 if (device_can_wakeup(&hcd
->self
.root_hub
->dev
)
495 && wValue
== USB_DEVICE_REMOTE_WAKEUP
)
496 device_set_wakeup_enable(&hcd
->self
.root_hub
->dev
, 1);
500 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
504 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
506 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
507 switch (wValue
& 0xff00) {
508 case USB_DT_DEVICE
<< 8:
509 switch (hcd
->driver
->flags
& HCD_MASK
) {
511 bufp
= usb3_rh_dev_descriptor
;
514 bufp
= usb2_rh_dev_descriptor
;
517 bufp
= usb11_rh_dev_descriptor
;
526 case USB_DT_CONFIG
<< 8:
527 switch (hcd
->driver
->flags
& HCD_MASK
) {
529 bufp
= ss_rh_config_descriptor
;
530 len
= sizeof ss_rh_config_descriptor
;
533 bufp
= hs_rh_config_descriptor
;
534 len
= sizeof hs_rh_config_descriptor
;
537 bufp
= fs_rh_config_descriptor
;
538 len
= sizeof fs_rh_config_descriptor
;
543 if (device_can_wakeup(&hcd
->self
.root_hub
->dev
))
546 case USB_DT_STRING
<< 8:
547 if ((wValue
& 0xff) < 4)
548 urb
->actual_length
= rh_string(wValue
& 0xff,
550 else /* unsupported IDs --> "protocol stall" */
557 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
561 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
563 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
564 // wValue == urb->dev->devaddr
565 dev_dbg (hcd
->self
.controller
, "root hub device address %d\n",
569 /* INTERFACE REQUESTS (no defined feature/status flags) */
571 /* ENDPOINT REQUESTS */
573 case EndpointRequest
| USB_REQ_GET_STATUS
:
574 // ENDPOINT_HALT flag
579 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
580 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
581 dev_dbg (hcd
->self
.controller
, "no endpoint features yet\n");
584 /* CLASS REQUESTS (and errors) */
587 /* non-generic request */
593 case GetHubDescriptor
:
594 len
= sizeof (struct usb_hub_descriptor
);
597 status
= hcd
->driver
->hub_control (hcd
,
598 typeReq
, wValue
, wIndex
,
602 /* "protocol stall" on error */
608 if (status
!= -EPIPE
) {
609 dev_dbg (hcd
->self
.controller
,
610 "CTRL: TypeReq=0x%x val=0x%x "
611 "idx=0x%x len=%d ==> %d\n",
612 typeReq
, wValue
, wIndex
,
617 if (urb
->transfer_buffer_length
< len
)
618 len
= urb
->transfer_buffer_length
;
619 urb
->actual_length
= len
;
620 // always USB_DIR_IN, toward host
621 memcpy (ubuf
, bufp
, len
);
623 /* report whether RH hardware supports remote wakeup */
625 len
> offsetof (struct usb_config_descriptor
,
627 ((struct usb_config_descriptor
*)ubuf
)->bmAttributes
628 |= USB_CONFIG_ATT_WAKEUP
;
630 /* report whether RH hardware has an integrated TT */
631 if (patch_protocol
&&
632 len
> offsetof(struct usb_device_descriptor
,
634 ((struct usb_device_descriptor
*) ubuf
)->
638 /* any errors get returned through the urb completion */
639 spin_lock_irq(&hcd_root_hub_lock
);
640 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
642 /* This peculiar use of spinlocks echoes what real HC drivers do.
643 * Avoiding calls to local_irq_disable/enable makes the code
646 spin_unlock(&hcd_root_hub_lock
);
647 usb_hcd_giveback_urb(hcd
, urb
, status
);
648 spin_lock(&hcd_root_hub_lock
);
650 spin_unlock_irq(&hcd_root_hub_lock
);
654 /*-------------------------------------------------------------------------*/
657 * Root Hub interrupt transfers are polled using a timer if the
658 * driver requests it; otherwise the driver is responsible for
659 * calling usb_hcd_poll_rh_status() when an event occurs.
661 * Completions are called in_interrupt(), but they may or may not
664 void usb_hcd_poll_rh_status(struct usb_hcd
*hcd
)
669 char buffer
[6]; /* Any root hubs with > 31 ports? */
671 if (unlikely(!hcd
->rh_registered
))
673 if (!hcd
->uses_new_polling
&& !hcd
->status_urb
)
676 length
= hcd
->driver
->hub_status_data(hcd
, buffer
);
679 /* try to complete the status urb */
680 spin_lock_irqsave(&hcd_root_hub_lock
, flags
);
681 urb
= hcd
->status_urb
;
683 hcd
->poll_pending
= 0;
684 hcd
->status_urb
= NULL
;
685 urb
->actual_length
= length
;
686 memcpy(urb
->transfer_buffer
, buffer
, length
);
688 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
689 spin_unlock(&hcd_root_hub_lock
);
690 usb_hcd_giveback_urb(hcd
, urb
, 0);
691 spin_lock(&hcd_root_hub_lock
);
694 hcd
->poll_pending
= 1;
696 spin_unlock_irqrestore(&hcd_root_hub_lock
, flags
);
699 /* The USB 2.0 spec says 256 ms. This is close enough and won't
700 * exceed that limit if HZ is 100. The math is more clunky than
701 * maybe expected, this is to make sure that all timers for USB devices
702 * fire at the same time to give the CPU a break inbetween */
703 if (hcd
->uses_new_polling
? hcd
->poll_rh
:
704 (length
== 0 && hcd
->status_urb
!= NULL
))
705 mod_timer (&hcd
->rh_timer
, (jiffies
/(HZ
/4) + 1) * (HZ
/4));
707 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status
);
710 static void rh_timer_func (unsigned long _hcd
)
712 usb_hcd_poll_rh_status((struct usb_hcd
*) _hcd
);
715 /*-------------------------------------------------------------------------*/
717 static int rh_queue_status (struct usb_hcd
*hcd
, struct urb
*urb
)
721 unsigned len
= 1 + (urb
->dev
->maxchild
/ 8);
723 spin_lock_irqsave (&hcd_root_hub_lock
, flags
);
724 if (hcd
->status_urb
|| urb
->transfer_buffer_length
< len
) {
725 dev_dbg (hcd
->self
.controller
, "not queuing rh status urb\n");
730 retval
= usb_hcd_link_urb_to_ep(hcd
, urb
);
734 hcd
->status_urb
= urb
;
735 urb
->hcpriv
= hcd
; /* indicate it's queued */
736 if (!hcd
->uses_new_polling
)
737 mod_timer(&hcd
->rh_timer
, (jiffies
/(HZ
/4) + 1) * (HZ
/4));
739 /* If a status change has already occurred, report it ASAP */
740 else if (hcd
->poll_pending
)
741 mod_timer(&hcd
->rh_timer
, jiffies
);
744 spin_unlock_irqrestore (&hcd_root_hub_lock
, flags
);
748 static int rh_urb_enqueue (struct usb_hcd
*hcd
, struct urb
*urb
)
750 if (usb_endpoint_xfer_int(&urb
->ep
->desc
))
751 return rh_queue_status (hcd
, urb
);
752 if (usb_endpoint_xfer_control(&urb
->ep
->desc
))
753 return rh_call_control (hcd
, urb
);
757 /*-------------------------------------------------------------------------*/
759 /* Unlinks of root-hub control URBs are legal, but they don't do anything
760 * since these URBs always execute synchronously.
762 static int usb_rh_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
767 spin_lock_irqsave(&hcd_root_hub_lock
, flags
);
768 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
772 if (usb_endpoint_num(&urb
->ep
->desc
) == 0) { /* Control URB */
775 } else { /* Status URB */
776 if (!hcd
->uses_new_polling
)
777 del_timer (&hcd
->rh_timer
);
778 if (urb
== hcd
->status_urb
) {
779 hcd
->status_urb
= NULL
;
780 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
782 spin_unlock(&hcd_root_hub_lock
);
783 usb_hcd_giveback_urb(hcd
, urb
, status
);
784 spin_lock(&hcd_root_hub_lock
);
788 spin_unlock_irqrestore(&hcd_root_hub_lock
, flags
);
795 * Show & store the current value of authorized_default
797 static ssize_t
usb_host_authorized_default_show(struct device
*dev
,
798 struct device_attribute
*attr
,
801 struct usb_device
*rh_usb_dev
= to_usb_device(dev
);
802 struct usb_bus
*usb_bus
= rh_usb_dev
->bus
;
803 struct usb_hcd
*usb_hcd
;
805 if (usb_bus
== NULL
) /* FIXME: not sure if this case is possible */
807 usb_hcd
= bus_to_hcd(usb_bus
);
808 return snprintf(buf
, PAGE_SIZE
, "%u\n", usb_hcd
->authorized_default
);
811 static ssize_t
usb_host_authorized_default_store(struct device
*dev
,
812 struct device_attribute
*attr
,
813 const char *buf
, size_t size
)
817 struct usb_device
*rh_usb_dev
= to_usb_device(dev
);
818 struct usb_bus
*usb_bus
= rh_usb_dev
->bus
;
819 struct usb_hcd
*usb_hcd
;
821 if (usb_bus
== NULL
) /* FIXME: not sure if this case is possible */
823 usb_hcd
= bus_to_hcd(usb_bus
);
824 result
= sscanf(buf
, "%u\n", &val
);
826 usb_hcd
->authorized_default
= val
? 1 : 0;
834 static DEVICE_ATTR(authorized_default
, 0644,
835 usb_host_authorized_default_show
,
836 usb_host_authorized_default_store
);
839 /* Group all the USB bus attributes */
840 static struct attribute
*usb_bus_attrs
[] = {
841 &dev_attr_authorized_default
.attr
,
845 static struct attribute_group usb_bus_attr_group
= {
846 .name
= NULL
, /* we want them in the same directory */
847 .attrs
= usb_bus_attrs
,
852 /*-------------------------------------------------------------------------*/
855 * usb_bus_init - shared initialization code
856 * @bus: the bus structure being initialized
858 * This code is used to initialize a usb_bus structure, memory for which is
859 * separately managed.
861 static void usb_bus_init (struct usb_bus
*bus
)
863 memset (&bus
->devmap
, 0, sizeof(struct usb_devmap
));
865 bus
->devnum_next
= 1;
867 bus
->root_hub
= NULL
;
869 bus
->bandwidth_allocated
= 0;
870 bus
->bandwidth_int_reqs
= 0;
871 bus
->bandwidth_isoc_reqs
= 0;
873 INIT_LIST_HEAD (&bus
->bus_list
);
876 /*-------------------------------------------------------------------------*/
879 * usb_register_bus - registers the USB host controller with the usb core
880 * @bus: pointer to the bus to register
881 * Context: !in_interrupt()
883 * Assigns a bus number, and links the controller into usbcore data
884 * structures so that it can be seen by scanning the bus list.
886 static int usb_register_bus(struct usb_bus
*bus
)
891 mutex_lock(&usb_bus_list_lock
);
892 busnum
= find_next_zero_bit (busmap
.busmap
, USB_MAXBUS
, 1);
893 if (busnum
>= USB_MAXBUS
) {
894 printk (KERN_ERR
"%s: too many buses\n", usbcore_name
);
895 goto error_find_busnum
;
897 set_bit (busnum
, busmap
.busmap
);
898 bus
->busnum
= busnum
;
900 /* Add it to the local list of buses */
901 list_add (&bus
->bus_list
, &usb_bus_list
);
902 mutex_unlock(&usb_bus_list_lock
);
904 usb_notify_add_bus(bus
);
906 dev_info (bus
->controller
, "new USB bus registered, assigned bus "
907 "number %d\n", bus
->busnum
);
911 mutex_unlock(&usb_bus_list_lock
);
916 * usb_deregister_bus - deregisters the USB host controller
917 * @bus: pointer to the bus to deregister
918 * Context: !in_interrupt()
920 * Recycles the bus number, and unlinks the controller from usbcore data
921 * structures so that it won't be seen by scanning the bus list.
923 static void usb_deregister_bus (struct usb_bus
*bus
)
925 dev_info (bus
->controller
, "USB bus %d deregistered\n", bus
->busnum
);
928 * NOTE: make sure that all the devices are removed by the
929 * controller code, as well as having it call this when cleaning
932 mutex_lock(&usb_bus_list_lock
);
933 list_del (&bus
->bus_list
);
934 mutex_unlock(&usb_bus_list_lock
);
936 usb_notify_remove_bus(bus
);
938 clear_bit (bus
->busnum
, busmap
.busmap
);
942 * register_root_hub - called by usb_add_hcd() to register a root hub
943 * @hcd: host controller for this root hub
945 * This function registers the root hub with the USB subsystem. It sets up
946 * the device properly in the device tree and then calls usb_new_device()
947 * to register the usb device. It also assigns the root hub's USB address
950 static int register_root_hub(struct usb_hcd
*hcd
)
952 struct device
*parent_dev
= hcd
->self
.controller
;
953 struct usb_device
*usb_dev
= hcd
->self
.root_hub
;
954 const int devnum
= 1;
957 usb_dev
->devnum
= devnum
;
958 usb_dev
->bus
->devnum_next
= devnum
+ 1;
959 memset (&usb_dev
->bus
->devmap
.devicemap
, 0,
960 sizeof usb_dev
->bus
->devmap
.devicemap
);
961 set_bit (devnum
, usb_dev
->bus
->devmap
.devicemap
);
962 usb_set_device_state(usb_dev
, USB_STATE_ADDRESS
);
964 mutex_lock(&usb_bus_list_lock
);
966 usb_dev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(64);
967 retval
= usb_get_device_descriptor(usb_dev
, USB_DT_DEVICE_SIZE
);
968 if (retval
!= sizeof usb_dev
->descriptor
) {
969 mutex_unlock(&usb_bus_list_lock
);
970 dev_dbg (parent_dev
, "can't read %s device descriptor %d\n",
971 dev_name(&usb_dev
->dev
), retval
);
972 return (retval
< 0) ? retval
: -EMSGSIZE
;
975 retval
= usb_new_device (usb_dev
);
977 dev_err (parent_dev
, "can't register root hub for %s, %d\n",
978 dev_name(&usb_dev
->dev
), retval
);
980 mutex_unlock(&usb_bus_list_lock
);
983 spin_lock_irq (&hcd_root_hub_lock
);
984 hcd
->rh_registered
= 1;
985 spin_unlock_irq (&hcd_root_hub_lock
);
987 /* Did the HC die before the root hub was registered? */
988 if (hcd
->state
== HC_STATE_HALT
)
989 usb_hc_died (hcd
); /* This time clean up */
996 /*-------------------------------------------------------------------------*/
999 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
1000 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
1001 * @is_input: true iff the transaction sends data to the host
1002 * @isoc: true for isochronous transactions, false for interrupt ones
1003 * @bytecount: how many bytes in the transaction.
1005 * Returns approximate bus time in nanoseconds for a periodic transaction.
1006 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
1007 * scheduled in software, this function is only used for such scheduling.
1009 long usb_calc_bus_time (int speed
, int is_input
, int isoc
, int bytecount
)
1014 case USB_SPEED_LOW
: /* INTR only */
1016 tmp
= (67667L * (31L + 10L * BitTime (bytecount
))) / 1000L;
1017 return (64060L + (2 * BW_HUB_LS_SETUP
) + BW_HOST_DELAY
+ tmp
);
1019 tmp
= (66700L * (31L + 10L * BitTime (bytecount
))) / 1000L;
1020 return (64107L + (2 * BW_HUB_LS_SETUP
) + BW_HOST_DELAY
+ tmp
);
1022 case USB_SPEED_FULL
: /* ISOC or INTR */
1024 tmp
= (8354L * (31L + 10L * BitTime (bytecount
))) / 1000L;
1025 return (((is_input
) ? 7268L : 6265L) + BW_HOST_DELAY
+ tmp
);
1027 tmp
= (8354L * (31L + 10L * BitTime (bytecount
))) / 1000L;
1028 return (9107L + BW_HOST_DELAY
+ tmp
);
1030 case USB_SPEED_HIGH
: /* ISOC or INTR */
1031 // FIXME adjust for input vs output
1033 tmp
= HS_NSECS_ISO (bytecount
);
1035 tmp
= HS_NSECS (bytecount
);
1038 pr_debug ("%s: bogus device speed!\n", usbcore_name
);
1042 EXPORT_SYMBOL_GPL(usb_calc_bus_time
);
1045 /*-------------------------------------------------------------------------*/
1048 * Generic HC operations.
1051 /*-------------------------------------------------------------------------*/
1054 * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
1055 * @hcd: host controller to which @urb was submitted
1056 * @urb: URB being submitted
1058 * Host controller drivers should call this routine in their enqueue()
1059 * method. The HCD's private spinlock must be held and interrupts must
1060 * be disabled. The actions carried out here are required for URB
1061 * submission, as well as for endpoint shutdown and for usb_kill_urb.
1063 * Returns 0 for no error, otherwise a negative error code (in which case
1064 * the enqueue() method must fail). If no error occurs but enqueue() fails
1065 * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
1066 * the private spinlock and returning.
1068 int usb_hcd_link_urb_to_ep(struct usb_hcd
*hcd
, struct urb
*urb
)
1072 spin_lock(&hcd_urb_list_lock
);
1074 /* Check that the URB isn't being killed */
1075 if (unlikely(atomic_read(&urb
->reject
))) {
1080 if (unlikely(!urb
->ep
->enabled
)) {
1085 if (unlikely(!urb
->dev
->can_submit
)) {
1091 * Check the host controller's state and add the URB to the
1094 switch (hcd
->state
) {
1095 case HC_STATE_RUNNING
:
1096 case HC_STATE_RESUMING
:
1098 list_add_tail(&urb
->urb_list
, &urb
->ep
->urb_list
);
1105 spin_unlock(&hcd_urb_list_lock
);
1108 EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep
);
1111 * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
1112 * @hcd: host controller to which @urb was submitted
1113 * @urb: URB being checked for unlinkability
1114 * @status: error code to store in @urb if the unlink succeeds
1116 * Host controller drivers should call this routine in their dequeue()
1117 * method. The HCD's private spinlock must be held and interrupts must
1118 * be disabled. The actions carried out here are required for making
1119 * sure than an unlink is valid.
1121 * Returns 0 for no error, otherwise a negative error code (in which case
1122 * the dequeue() method must fail). The possible error codes are:
1124 * -EIDRM: @urb was not submitted or has already completed.
1125 * The completion function may not have been called yet.
1127 * -EBUSY: @urb has already been unlinked.
1129 int usb_hcd_check_unlink_urb(struct usb_hcd
*hcd
, struct urb
*urb
,
1132 struct list_head
*tmp
;
1134 /* insist the urb is still queued */
1135 list_for_each(tmp
, &urb
->ep
->urb_list
) {
1136 if (tmp
== &urb
->urb_list
)
1139 if (tmp
!= &urb
->urb_list
)
1142 /* Any status except -EINPROGRESS means something already started to
1143 * unlink this URB from the hardware. So there's no more work to do.
1147 urb
->unlinked
= status
;
1149 /* IRQ setup can easily be broken so that USB controllers
1150 * never get completion IRQs ... maybe even the ones we need to
1151 * finish unlinking the initial failed usb_set_address()
1152 * or device descriptor fetch.
1154 if (!test_bit(HCD_FLAG_SAW_IRQ
, &hcd
->flags
) &&
1155 !is_root_hub(urb
->dev
)) {
1156 dev_warn(hcd
->self
.controller
, "Unlink after no-IRQ? "
1157 "Controller is probably using the wrong IRQ.\n");
1158 set_bit(HCD_FLAG_SAW_IRQ
, &hcd
->flags
);
1163 EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb
);
1166 * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
1167 * @hcd: host controller to which @urb was submitted
1168 * @urb: URB being unlinked
1170 * Host controller drivers should call this routine before calling
1171 * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
1172 * interrupts must be disabled. The actions carried out here are required
1173 * for URB completion.
1175 void usb_hcd_unlink_urb_from_ep(struct usb_hcd
*hcd
, struct urb
*urb
)
1177 /* clear all state linking urb to this dev (and hcd) */
1178 spin_lock(&hcd_urb_list_lock
);
1179 list_del_init(&urb
->urb_list
);
1180 spin_unlock(&hcd_urb_list_lock
);
1182 EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep
);
1185 * Some usb host controllers can only perform dma using a small SRAM area.
1186 * The usb core itself is however optimized for host controllers that can dma
1187 * using regular system memory - like pci devices doing bus mastering.
1189 * To support host controllers with limited dma capabilites we provide dma
1190 * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
1191 * For this to work properly the host controller code must first use the
1192 * function dma_declare_coherent_memory() to point out which memory area
1193 * that should be used for dma allocations.
1195 * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
1196 * dma using dma_alloc_coherent() which in turn allocates from the memory
1197 * area pointed out with dma_declare_coherent_memory().
1199 * So, to summarize...
1201 * - We need "local" memory, canonical example being
1202 * a small SRAM on a discrete controller being the
1203 * only memory that the controller can read ...
1204 * (a) "normal" kernel memory is no good, and
1205 * (b) there's not enough to share
1207 * - The only *portable* hook for such stuff in the
1208 * DMA framework is dma_declare_coherent_memory()
1210 * - So we use that, even though the primary requirement
1211 * is that the memory be "local" (hence addressible
1212 * by that device), not "coherent".
1216 static int hcd_alloc_coherent(struct usb_bus
*bus
,
1217 gfp_t mem_flags
, dma_addr_t
*dma_handle
,
1218 void **vaddr_handle
, size_t size
,
1219 enum dma_data_direction dir
)
1221 unsigned char *vaddr
;
1223 vaddr
= hcd_buffer_alloc(bus
, size
+ sizeof(vaddr
),
1224 mem_flags
, dma_handle
);
1229 * Store the virtual address of the buffer at the end
1230 * of the allocated dma buffer. The size of the buffer
1231 * may be uneven so use unaligned functions instead
1232 * of just rounding up. It makes sense to optimize for
1233 * memory footprint over access speed since the amount
1234 * of memory available for dma may be limited.
1236 put_unaligned((unsigned long)*vaddr_handle
,
1237 (unsigned long *)(vaddr
+ size
));
1239 if (dir
== DMA_TO_DEVICE
)
1240 memcpy(vaddr
, *vaddr_handle
, size
);
1242 *vaddr_handle
= vaddr
;
1246 static void hcd_free_coherent(struct usb_bus
*bus
, dma_addr_t
*dma_handle
,
1247 void **vaddr_handle
, size_t size
,
1248 enum dma_data_direction dir
)
1250 unsigned char *vaddr
= *vaddr_handle
;
1252 vaddr
= (void *)get_unaligned((unsigned long *)(vaddr
+ size
));
1254 if (dir
== DMA_FROM_DEVICE
)
1255 memcpy(vaddr
, *vaddr_handle
, size
);
1257 hcd_buffer_free(bus
, size
+ sizeof(vaddr
), *vaddr_handle
, *dma_handle
);
1259 *vaddr_handle
= vaddr
;
1263 static int map_urb_for_dma(struct usb_hcd
*hcd
, struct urb
*urb
,
1266 enum dma_data_direction dir
;
1269 /* Map the URB's buffers for DMA access.
1270 * Lower level HCD code should use *_dma exclusively,
1271 * unless it uses pio or talks to another transport,
1272 * or uses the provided scatter gather list for bulk.
1274 if (is_root_hub(urb
->dev
))
1277 if (usb_endpoint_xfer_control(&urb
->ep
->desc
)
1278 && !(urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
)) {
1279 if (hcd
->self
.uses_dma
) {
1280 urb
->setup_dma
= dma_map_single(
1281 hcd
->self
.controller
,
1283 sizeof(struct usb_ctrlrequest
),
1285 if (dma_mapping_error(hcd
->self
.controller
,
1288 } else if (hcd
->driver
->flags
& HCD_LOCAL_MEM
)
1289 ret
= hcd_alloc_coherent(
1290 urb
->dev
->bus
, mem_flags
,
1292 (void **)&urb
->setup_packet
,
1293 sizeof(struct usb_ctrlrequest
),
1297 dir
= usb_urb_dir_in(urb
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
1298 if (ret
== 0 && urb
->transfer_buffer_length
!= 0
1299 && !(urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
)) {
1300 if (hcd
->self
.uses_dma
) {
1301 urb
->transfer_dma
= dma_map_single (
1302 hcd
->self
.controller
,
1303 urb
->transfer_buffer
,
1304 urb
->transfer_buffer_length
,
1306 if (dma_mapping_error(hcd
->self
.controller
,
1309 } else if (hcd
->driver
->flags
& HCD_LOCAL_MEM
) {
1310 ret
= hcd_alloc_coherent(
1311 urb
->dev
->bus
, mem_flags
,
1313 &urb
->transfer_buffer
,
1314 urb
->transfer_buffer_length
,
1317 if (ret
&& usb_endpoint_xfer_control(&urb
->ep
->desc
)
1318 && !(urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
))
1319 hcd_free_coherent(urb
->dev
->bus
,
1321 (void **)&urb
->setup_packet
,
1322 sizeof(struct usb_ctrlrequest
),
1329 static void unmap_urb_for_dma(struct usb_hcd
*hcd
, struct urb
*urb
)
1331 enum dma_data_direction dir
;
1333 if (is_root_hub(urb
->dev
))
1336 if (usb_endpoint_xfer_control(&urb
->ep
->desc
)
1337 && !(urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
)) {
1338 if (hcd
->self
.uses_dma
)
1339 dma_unmap_single(hcd
->self
.controller
, urb
->setup_dma
,
1340 sizeof(struct usb_ctrlrequest
),
1342 else if (hcd
->driver
->flags
& HCD_LOCAL_MEM
)
1343 hcd_free_coherent(urb
->dev
->bus
, &urb
->setup_dma
,
1344 (void **)&urb
->setup_packet
,
1345 sizeof(struct usb_ctrlrequest
),
1349 dir
= usb_urb_dir_in(urb
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
1350 if (urb
->transfer_buffer_length
!= 0
1351 && !(urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
)) {
1352 if (hcd
->self
.uses_dma
)
1353 dma_unmap_single(hcd
->self
.controller
,
1355 urb
->transfer_buffer_length
,
1357 else if (hcd
->driver
->flags
& HCD_LOCAL_MEM
)
1358 hcd_free_coherent(urb
->dev
->bus
, &urb
->transfer_dma
,
1359 &urb
->transfer_buffer
,
1360 urb
->transfer_buffer_length
,
1365 /*-------------------------------------------------------------------------*/
1367 /* may be called in any context with a valid urb->dev usecount
1368 * caller surrenders "ownership" of urb
1369 * expects usb_submit_urb() to have sanity checked and conditioned all
1372 int usb_hcd_submit_urb (struct urb
*urb
, gfp_t mem_flags
)
1375 struct usb_hcd
*hcd
= bus_to_hcd(urb
->dev
->bus
);
1377 /* increment urb's reference count as part of giving it to the HCD
1378 * (which will control it). HCD guarantees that it either returns
1379 * an error or calls giveback(), but not both.
1382 atomic_inc(&urb
->use_count
);
1383 atomic_inc(&urb
->dev
->urbnum
);
1384 usbmon_urb_submit(&hcd
->self
, urb
);
1386 /* NOTE requirements on root-hub callers (usbfs and the hub
1387 * driver, for now): URBs' urb->transfer_buffer must be
1388 * valid and usb_buffer_{sync,unmap}() not be needed, since
1389 * they could clobber root hub response data. Also, control
1390 * URBs must be submitted in process context with interrupts
1393 status
= map_urb_for_dma(hcd
, urb
, mem_flags
);
1394 if (unlikely(status
)) {
1395 usbmon_urb_submit_error(&hcd
->self
, urb
, status
);
1399 if (is_root_hub(urb
->dev
))
1400 status
= rh_urb_enqueue(hcd
, urb
);
1402 status
= hcd
->driver
->urb_enqueue(hcd
, urb
, mem_flags
);
1404 if (unlikely(status
)) {
1405 usbmon_urb_submit_error(&hcd
->self
, urb
, status
);
1406 unmap_urb_for_dma(hcd
, urb
);
1409 INIT_LIST_HEAD(&urb
->urb_list
);
1410 atomic_dec(&urb
->use_count
);
1411 atomic_dec(&urb
->dev
->urbnum
);
1412 if (atomic_read(&urb
->reject
))
1413 wake_up(&usb_kill_urb_queue
);
1419 /*-------------------------------------------------------------------------*/
1421 /* this makes the hcd giveback() the urb more quickly, by kicking it
1422 * off hardware queues (which may take a while) and returning it as
1423 * soon as practical. we've already set up the urb's return status,
1424 * but we can't know if the callback completed already.
1426 static int unlink1(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1430 if (is_root_hub(urb
->dev
))
1431 value
= usb_rh_urb_dequeue(hcd
, urb
, status
);
1434 /* The only reason an HCD might fail this call is if
1435 * it has not yet fully queued the urb to begin with.
1436 * Such failures should be harmless. */
1437 value
= hcd
->driver
->urb_dequeue(hcd
, urb
, status
);
1443 * called in any context
1445 * caller guarantees urb won't be recycled till both unlink()
1446 * and the urb's completion function return
1448 int usb_hcd_unlink_urb (struct urb
*urb
, int status
)
1450 struct usb_hcd
*hcd
;
1451 int retval
= -EIDRM
;
1452 unsigned long flags
;
1454 /* Prevent the device and bus from going away while
1455 * the unlink is carried out. If they are already gone
1456 * then urb->use_count must be 0, since disconnected
1457 * devices can't have any active URBs.
1459 spin_lock_irqsave(&hcd_urb_unlink_lock
, flags
);
1460 if (atomic_read(&urb
->use_count
) > 0) {
1462 usb_get_dev(urb
->dev
);
1464 spin_unlock_irqrestore(&hcd_urb_unlink_lock
, flags
);
1466 hcd
= bus_to_hcd(urb
->dev
->bus
);
1467 retval
= unlink1(hcd
, urb
, status
);
1468 usb_put_dev(urb
->dev
);
1472 retval
= -EINPROGRESS
;
1473 else if (retval
!= -EIDRM
&& retval
!= -EBUSY
)
1474 dev_dbg(&urb
->dev
->dev
, "hcd_unlink_urb %p fail %d\n",
1479 /*-------------------------------------------------------------------------*/
1482 * usb_hcd_giveback_urb - return URB from HCD to device driver
1483 * @hcd: host controller returning the URB
1484 * @urb: urb being returned to the USB device driver.
1485 * @status: completion status code for the URB.
1486 * Context: in_interrupt()
1488 * This hands the URB from HCD to its USB device driver, using its
1489 * completion function. The HCD has freed all per-urb resources
1490 * (and is done using urb->hcpriv). It also released all HCD locks;
1491 * the device driver won't cause problems if it frees, modifies,
1492 * or resubmits this URB.
1494 * If @urb was unlinked, the value of @status will be overridden by
1495 * @urb->unlinked. Erroneous short transfers are detected in case
1496 * the HCD hasn't checked for them.
1498 void usb_hcd_giveback_urb(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1501 if (unlikely(urb
->unlinked
))
1502 status
= urb
->unlinked
;
1503 else if (unlikely((urb
->transfer_flags
& URB_SHORT_NOT_OK
) &&
1504 urb
->actual_length
< urb
->transfer_buffer_length
&&
1506 status
= -EREMOTEIO
;
1508 unmap_urb_for_dma(hcd
, urb
);
1509 usbmon_urb_complete(&hcd
->self
, urb
, status
);
1510 usb_unanchor_urb(urb
);
1512 /* pass ownership to the completion handler */
1513 urb
->status
= status
;
1514 urb
->complete (urb
);
1515 atomic_dec (&urb
->use_count
);
1516 if (unlikely(atomic_read(&urb
->reject
)))
1517 wake_up (&usb_kill_urb_queue
);
1520 EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb
);
1522 /*-------------------------------------------------------------------------*/
1524 /* Cancel all URBs pending on this endpoint and wait for the endpoint's
1525 * queue to drain completely. The caller must first insure that no more
1526 * URBs can be submitted for this endpoint.
1528 void usb_hcd_flush_endpoint(struct usb_device
*udev
,
1529 struct usb_host_endpoint
*ep
)
1531 struct usb_hcd
*hcd
;
1537 hcd
= bus_to_hcd(udev
->bus
);
1539 /* No more submits can occur */
1540 spin_lock_irq(&hcd_urb_list_lock
);
1542 list_for_each_entry (urb
, &ep
->urb_list
, urb_list
) {
1548 is_in
= usb_urb_dir_in(urb
);
1549 spin_unlock(&hcd_urb_list_lock
);
1552 unlink1(hcd
, urb
, -ESHUTDOWN
);
1553 dev_dbg (hcd
->self
.controller
,
1554 "shutdown urb %p ep%d%s%s\n",
1555 urb
, usb_endpoint_num(&ep
->desc
),
1556 is_in
? "in" : "out",
1559 switch (usb_endpoint_type(&ep
->desc
)) {
1560 case USB_ENDPOINT_XFER_CONTROL
:
1562 case USB_ENDPOINT_XFER_BULK
:
1564 case USB_ENDPOINT_XFER_INT
:
1573 /* list contents may have changed */
1574 spin_lock(&hcd_urb_list_lock
);
1577 spin_unlock_irq(&hcd_urb_list_lock
);
1579 /* Wait until the endpoint queue is completely empty */
1580 while (!list_empty (&ep
->urb_list
)) {
1581 spin_lock_irq(&hcd_urb_list_lock
);
1583 /* The list may have changed while we acquired the spinlock */
1585 if (!list_empty (&ep
->urb_list
)) {
1586 urb
= list_entry (ep
->urb_list
.prev
, struct urb
,
1590 spin_unlock_irq(&hcd_urb_list_lock
);
1600 * Check whether a new bandwidth setting exceeds the bus bandwidth.
1601 * @new_config: new configuration to install
1602 * @cur_alt: the current alternate interface setting
1603 * @new_alt: alternate interface setting that is being installed
1605 * To change configurations, pass in the new configuration in new_config,
1606 * and pass NULL for cur_alt and new_alt.
1608 * To reset a device's configuration (put the device in the ADDRESSED state),
1609 * pass in NULL for new_config, cur_alt, and new_alt.
1611 * To change alternate interface settings, pass in NULL for new_config,
1612 * pass in the current alternate interface setting in cur_alt,
1613 * and pass in the new alternate interface setting in new_alt.
1615 * Returns an error if the requested bandwidth change exceeds the
1616 * bus bandwidth or host controller internal resources.
1618 int usb_hcd_alloc_bandwidth(struct usb_device
*udev
,
1619 struct usb_host_config
*new_config
,
1620 struct usb_host_interface
*cur_alt
,
1621 struct usb_host_interface
*new_alt
)
1623 int num_intfs
, i
, j
;
1624 struct usb_host_interface
*alt
= NULL
;
1626 struct usb_hcd
*hcd
;
1627 struct usb_host_endpoint
*ep
;
1629 hcd
= bus_to_hcd(udev
->bus
);
1630 if (!hcd
->driver
->check_bandwidth
)
1633 /* Configuration is being removed - set configuration 0 */
1634 if (!new_config
&& !cur_alt
) {
1635 for (i
= 1; i
< 16; ++i
) {
1636 ep
= udev
->ep_out
[i
];
1638 hcd
->driver
->drop_endpoint(hcd
, udev
, ep
);
1639 ep
= udev
->ep_in
[i
];
1641 hcd
->driver
->drop_endpoint(hcd
, udev
, ep
);
1643 hcd
->driver
->check_bandwidth(hcd
, udev
);
1646 /* Check if the HCD says there's enough bandwidth. Enable all endpoints
1647 * each interface's alt setting 0 and ask the HCD to check the bandwidth
1648 * of the bus. There will always be bandwidth for endpoint 0, so it's
1652 num_intfs
= new_config
->desc
.bNumInterfaces
;
1653 /* Remove endpoints (except endpoint 0, which is always on the
1654 * schedule) from the old config from the schedule
1656 for (i
= 1; i
< 16; ++i
) {
1657 ep
= udev
->ep_out
[i
];
1659 ret
= hcd
->driver
->drop_endpoint(hcd
, udev
, ep
);
1663 ep
= udev
->ep_in
[i
];
1665 ret
= hcd
->driver
->drop_endpoint(hcd
, udev
, ep
);
1670 for (i
= 0; i
< num_intfs
; ++i
) {
1671 /* Set up endpoints for alternate interface setting 0 */
1672 alt
= usb_find_alt_setting(new_config
, i
, 0);
1674 /* No alt setting 0? Pick the first setting. */
1675 alt
= &new_config
->intf_cache
[i
]->altsetting
[0];
1677 for (j
= 0; j
< alt
->desc
.bNumEndpoints
; j
++) {
1678 ret
= hcd
->driver
->add_endpoint(hcd
, udev
, &alt
->endpoint
[j
]);
1684 if (cur_alt
&& new_alt
) {
1685 /* Drop all the endpoints in the current alt setting */
1686 for (i
= 0; i
< cur_alt
->desc
.bNumEndpoints
; i
++) {
1687 ret
= hcd
->driver
->drop_endpoint(hcd
, udev
,
1688 &cur_alt
->endpoint
[i
]);
1692 /* Add all the endpoints in the new alt setting */
1693 for (i
= 0; i
< new_alt
->desc
.bNumEndpoints
; i
++) {
1694 ret
= hcd
->driver
->add_endpoint(hcd
, udev
,
1695 &new_alt
->endpoint
[i
]);
1700 ret
= hcd
->driver
->check_bandwidth(hcd
, udev
);
1703 hcd
->driver
->reset_bandwidth(hcd
, udev
);
1707 /* Disables the endpoint: synchronizes with the hcd to make sure all
1708 * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
1709 * have been called previously. Use for set_configuration, set_interface,
1710 * driver removal, physical disconnect.
1712 * example: a qh stored in ep->hcpriv, holding state related to endpoint
1713 * type, maxpacket size, toggle, halt status, and scheduling.
1715 void usb_hcd_disable_endpoint(struct usb_device
*udev
,
1716 struct usb_host_endpoint
*ep
)
1718 struct usb_hcd
*hcd
;
1721 hcd
= bus_to_hcd(udev
->bus
);
1722 if (hcd
->driver
->endpoint_disable
)
1723 hcd
->driver
->endpoint_disable(hcd
, ep
);
1727 * usb_hcd_reset_endpoint - reset host endpoint state
1728 * @udev: USB device.
1729 * @ep: the endpoint to reset.
1731 * Resets any host endpoint state such as the toggle bit, sequence
1732 * number and current window.
1734 void usb_hcd_reset_endpoint(struct usb_device
*udev
,
1735 struct usb_host_endpoint
*ep
)
1737 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1739 if (hcd
->driver
->endpoint_reset
)
1740 hcd
->driver
->endpoint_reset(hcd
, ep
);
1742 int epnum
= usb_endpoint_num(&ep
->desc
);
1743 int is_out
= usb_endpoint_dir_out(&ep
->desc
);
1744 int is_control
= usb_endpoint_xfer_control(&ep
->desc
);
1746 usb_settoggle(udev
, epnum
, is_out
, 0);
1748 usb_settoggle(udev
, epnum
, !is_out
, 0);
1752 /* Protect against drivers that try to unlink URBs after the device
1753 * is gone, by waiting until all unlinks for @udev are finished.
1754 * Since we don't currently track URBs by device, simply wait until
1755 * nothing is running in the locked region of usb_hcd_unlink_urb().
1757 void usb_hcd_synchronize_unlinks(struct usb_device
*udev
)
1759 spin_lock_irq(&hcd_urb_unlink_lock
);
1760 spin_unlock_irq(&hcd_urb_unlink_lock
);
1763 /*-------------------------------------------------------------------------*/
1765 /* called in any context */
1766 int usb_hcd_get_frame_number (struct usb_device
*udev
)
1768 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1770 if (!HC_IS_RUNNING (hcd
->state
))
1772 return hcd
->driver
->get_frame_number (hcd
);
1775 /*-------------------------------------------------------------------------*/
1779 int hcd_bus_suspend(struct usb_device
*rhdev
, pm_message_t msg
)
1781 struct usb_hcd
*hcd
= container_of(rhdev
->bus
, struct usb_hcd
, self
);
1783 int old_state
= hcd
->state
;
1785 dev_dbg(&rhdev
->dev
, "bus %s%s\n",
1786 (msg
.event
& PM_EVENT_AUTO
? "auto-" : ""), "suspend");
1787 if (!hcd
->driver
->bus_suspend
) {
1790 hcd
->state
= HC_STATE_QUIESCING
;
1791 status
= hcd
->driver
->bus_suspend(hcd
);
1794 usb_set_device_state(rhdev
, USB_STATE_SUSPENDED
);
1795 hcd
->state
= HC_STATE_SUSPENDED
;
1797 hcd
->state
= old_state
;
1798 dev_dbg(&rhdev
->dev
, "bus %s fail, err %d\n",
1804 int hcd_bus_resume(struct usb_device
*rhdev
, pm_message_t msg
)
1806 struct usb_hcd
*hcd
= container_of(rhdev
->bus
, struct usb_hcd
, self
);
1808 int old_state
= hcd
->state
;
1810 dev_dbg(&rhdev
->dev
, "usb %s%s\n",
1811 (msg
.event
& PM_EVENT_AUTO
? "auto-" : ""), "resume");
1812 if (!hcd
->driver
->bus_resume
)
1814 if (hcd
->state
== HC_STATE_RUNNING
)
1817 hcd
->state
= HC_STATE_RESUMING
;
1818 status
= hcd
->driver
->bus_resume(hcd
);
1820 /* TRSMRCY = 10 msec */
1822 usb_set_device_state(rhdev
, rhdev
->actconfig
1823 ? USB_STATE_CONFIGURED
1824 : USB_STATE_ADDRESS
);
1825 hcd
->state
= HC_STATE_RUNNING
;
1827 hcd
->state
= old_state
;
1828 dev_dbg(&rhdev
->dev
, "bus %s fail, err %d\n",
1830 if (status
!= -ESHUTDOWN
)
1836 /* Workqueue routine for root-hub remote wakeup */
1837 static void hcd_resume_work(struct work_struct
*work
)
1839 struct usb_hcd
*hcd
= container_of(work
, struct usb_hcd
, wakeup_work
);
1840 struct usb_device
*udev
= hcd
->self
.root_hub
;
1842 usb_lock_device(udev
);
1843 usb_mark_last_busy(udev
);
1844 usb_external_resume_device(udev
, PMSG_REMOTE_RESUME
);
1845 usb_unlock_device(udev
);
1849 * usb_hcd_resume_root_hub - called by HCD to resume its root hub
1850 * @hcd: host controller for this root hub
1852 * The USB host controller calls this function when its root hub is
1853 * suspended (with the remote wakeup feature enabled) and a remote
1854 * wakeup request is received. The routine submits a workqueue request
1855 * to resume the root hub (that is, manage its downstream ports again).
1857 void usb_hcd_resume_root_hub (struct usb_hcd
*hcd
)
1859 unsigned long flags
;
1861 spin_lock_irqsave (&hcd_root_hub_lock
, flags
);
1862 if (hcd
->rh_registered
)
1863 queue_work(ksuspend_usb_wq
, &hcd
->wakeup_work
);
1864 spin_unlock_irqrestore (&hcd_root_hub_lock
, flags
);
1866 EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub
);
1870 /*-------------------------------------------------------------------------*/
1872 #ifdef CONFIG_USB_OTG
1875 * usb_bus_start_enum - start immediate enumeration (for OTG)
1876 * @bus: the bus (must use hcd framework)
1877 * @port_num: 1-based number of port; usually bus->otg_port
1878 * Context: in_interrupt()
1880 * Starts enumeration, with an immediate reset followed later by
1881 * khubd identifying and possibly configuring the device.
1882 * This is needed by OTG controller drivers, where it helps meet
1883 * HNP protocol timing requirements for starting a port reset.
1885 int usb_bus_start_enum(struct usb_bus
*bus
, unsigned port_num
)
1887 struct usb_hcd
*hcd
;
1888 int status
= -EOPNOTSUPP
;
1890 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
1891 * boards with root hubs hooked up to internal devices (instead of
1892 * just the OTG port) may need more attention to resetting...
1894 hcd
= container_of (bus
, struct usb_hcd
, self
);
1895 if (port_num
&& hcd
->driver
->start_port_reset
)
1896 status
= hcd
->driver
->start_port_reset(hcd
, port_num
);
1898 /* run khubd shortly after (first) root port reset finishes;
1899 * it may issue others, until at least 50 msecs have passed.
1902 mod_timer(&hcd
->rh_timer
, jiffies
+ msecs_to_jiffies(10));
1905 EXPORT_SYMBOL_GPL(usb_bus_start_enum
);
1909 /*-------------------------------------------------------------------------*/
1912 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
1913 * @irq: the IRQ being raised
1914 * @__hcd: pointer to the HCD whose IRQ is being signaled
1916 * If the controller isn't HALTed, calls the driver's irq handler.
1917 * Checks whether the controller is now dead.
1919 irqreturn_t
usb_hcd_irq (int irq
, void *__hcd
)
1921 struct usb_hcd
*hcd
= __hcd
;
1922 unsigned long flags
;
1925 /* IRQF_DISABLED doesn't work correctly with shared IRQs
1926 * when the first handler doesn't use it. So let's just
1927 * assume it's never used.
1929 local_irq_save(flags
);
1931 if (unlikely(hcd
->state
== HC_STATE_HALT
||
1932 !test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
))) {
1934 } else if (hcd
->driver
->irq(hcd
) == IRQ_NONE
) {
1937 set_bit(HCD_FLAG_SAW_IRQ
, &hcd
->flags
);
1939 if (unlikely(hcd
->state
== HC_STATE_HALT
))
1944 local_irq_restore(flags
);
1948 /*-------------------------------------------------------------------------*/
1951 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
1952 * @hcd: pointer to the HCD representing the controller
1954 * This is called by bus glue to report a USB host controller that died
1955 * while operations may still have been pending. It's called automatically
1956 * by the PCI glue, so only glue for non-PCI busses should need to call it.
1958 void usb_hc_died (struct usb_hcd
*hcd
)
1960 unsigned long flags
;
1962 dev_err (hcd
->self
.controller
, "HC died; cleaning up\n");
1964 spin_lock_irqsave (&hcd_root_hub_lock
, flags
);
1965 if (hcd
->rh_registered
) {
1968 /* make khubd clean up old urbs and devices */
1969 usb_set_device_state (hcd
->self
.root_hub
,
1970 USB_STATE_NOTATTACHED
);
1971 usb_kick_khubd (hcd
->self
.root_hub
);
1973 spin_unlock_irqrestore (&hcd_root_hub_lock
, flags
);
1975 EXPORT_SYMBOL_GPL (usb_hc_died
);
1977 /*-------------------------------------------------------------------------*/
1980 * usb_create_hcd - create and initialize an HCD structure
1981 * @driver: HC driver that will use this hcd
1982 * @dev: device for this HC, stored in hcd->self.controller
1983 * @bus_name: value to store in hcd->self.bus_name
1984 * Context: !in_interrupt()
1986 * Allocate a struct usb_hcd, with extra space at the end for the
1987 * HC driver's private data. Initialize the generic members of the
1990 * If memory is unavailable, returns NULL.
1992 struct usb_hcd
*usb_create_hcd (const struct hc_driver
*driver
,
1993 struct device
*dev
, const char *bus_name
)
1995 struct usb_hcd
*hcd
;
1997 hcd
= kzalloc(sizeof(*hcd
) + driver
->hcd_priv_size
, GFP_KERNEL
);
1999 dev_dbg (dev
, "hcd alloc failed\n");
2002 dev_set_drvdata(dev
, hcd
);
2003 kref_init(&hcd
->kref
);
2005 usb_bus_init(&hcd
->self
);
2006 hcd
->self
.controller
= dev
;
2007 hcd
->self
.bus_name
= bus_name
;
2008 hcd
->self
.uses_dma
= (dev
->dma_mask
!= NULL
);
2010 init_timer(&hcd
->rh_timer
);
2011 hcd
->rh_timer
.function
= rh_timer_func
;
2012 hcd
->rh_timer
.data
= (unsigned long) hcd
;
2014 INIT_WORK(&hcd
->wakeup_work
, hcd_resume_work
);
2016 mutex_init(&hcd
->bandwidth_mutex
);
2018 hcd
->driver
= driver
;
2019 hcd
->product_desc
= (driver
->product_desc
) ? driver
->product_desc
:
2020 "USB Host Controller";
2023 EXPORT_SYMBOL_GPL(usb_create_hcd
);
2025 static void hcd_release (struct kref
*kref
)
2027 struct usb_hcd
*hcd
= container_of (kref
, struct usb_hcd
, kref
);
2032 struct usb_hcd
*usb_get_hcd (struct usb_hcd
*hcd
)
2035 kref_get (&hcd
->kref
);
2038 EXPORT_SYMBOL_GPL(usb_get_hcd
);
2040 void usb_put_hcd (struct usb_hcd
*hcd
)
2043 kref_put (&hcd
->kref
, hcd_release
);
2045 EXPORT_SYMBOL_GPL(usb_put_hcd
);
2048 * usb_add_hcd - finish generic HCD structure initialization and register
2049 * @hcd: the usb_hcd structure to initialize
2050 * @irqnum: Interrupt line to allocate
2051 * @irqflags: Interrupt type flags
2053 * Finish the remaining parts of generic HCD initialization: allocate the
2054 * buffers of consistent memory, register the bus, request the IRQ line,
2055 * and call the driver's reset() and start() routines.
2057 int usb_add_hcd(struct usb_hcd
*hcd
,
2058 unsigned int irqnum
, unsigned long irqflags
)
2061 struct usb_device
*rhdev
;
2063 dev_info(hcd
->self
.controller
, "%s\n", hcd
->product_desc
);
2065 hcd
->authorized_default
= hcd
->wireless
? 0 : 1;
2066 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
2068 /* HC is in reset state, but accessible. Now do the one-time init,
2069 * bottom up so that hcds can customize the root hubs before khubd
2070 * starts talking to them. (Note, bus id is assigned early too.)
2072 if ((retval
= hcd_buffer_create(hcd
)) != 0) {
2073 dev_dbg(hcd
->self
.controller
, "pool alloc failed\n");
2077 if ((retval
= usb_register_bus(&hcd
->self
)) < 0)
2078 goto err_register_bus
;
2080 if ((rhdev
= usb_alloc_dev(NULL
, &hcd
->self
, 0)) == NULL
) {
2081 dev_err(hcd
->self
.controller
, "unable to allocate root hub\n");
2083 goto err_allocate_root_hub
;
2086 switch (hcd
->driver
->flags
& HCD_MASK
) {
2088 rhdev
->speed
= USB_SPEED_FULL
;
2091 rhdev
->speed
= USB_SPEED_HIGH
;
2094 rhdev
->speed
= USB_SPEED_SUPER
;
2097 goto err_allocate_root_hub
;
2099 hcd
->self
.root_hub
= rhdev
;
2101 /* wakeup flag init defaults to "everything works" for root hubs,
2102 * but drivers can override it in reset() if needed, along with
2103 * recording the overall controller's system wakeup capability.
2105 device_init_wakeup(&rhdev
->dev
, 1);
2107 /* "reset" is misnamed; its role is now one-time init. the controller
2108 * should already have been reset (and boot firmware kicked off etc).
2110 if (hcd
->driver
->reset
&& (retval
= hcd
->driver
->reset(hcd
)) < 0) {
2111 dev_err(hcd
->self
.controller
, "can't setup\n");
2112 goto err_hcd_driver_setup
;
2115 /* NOTE: root hub and controller capabilities may not be the same */
2116 if (device_can_wakeup(hcd
->self
.controller
)
2117 && device_can_wakeup(&hcd
->self
.root_hub
->dev
))
2118 dev_dbg(hcd
->self
.controller
, "supports USB remote wakeup\n");
2120 /* enable irqs just before we start the controller */
2121 if (hcd
->driver
->irq
) {
2123 /* IRQF_DISABLED doesn't work as advertised when used together
2124 * with IRQF_SHARED. As usb_hcd_irq() will always disable
2125 * interrupts we can remove it here.
2127 if (irqflags
& IRQF_SHARED
)
2128 irqflags
&= ~IRQF_DISABLED
;
2130 snprintf(hcd
->irq_descr
, sizeof(hcd
->irq_descr
), "%s:usb%d",
2131 hcd
->driver
->description
, hcd
->self
.busnum
);
2132 if ((retval
= request_irq(irqnum
, &usb_hcd_irq
, irqflags
,
2133 hcd
->irq_descr
, hcd
)) != 0) {
2134 dev_err(hcd
->self
.controller
,
2135 "request interrupt %d failed\n", irqnum
);
2136 goto err_request_irq
;
2139 dev_info(hcd
->self
.controller
, "irq %d, %s 0x%08llx\n", irqnum
,
2140 (hcd
->driver
->flags
& HCD_MEMORY
) ?
2141 "io mem" : "io base",
2142 (unsigned long long)hcd
->rsrc_start
);
2145 if (hcd
->rsrc_start
)
2146 dev_info(hcd
->self
.controller
, "%s 0x%08llx\n",
2147 (hcd
->driver
->flags
& HCD_MEMORY
) ?
2148 "io mem" : "io base",
2149 (unsigned long long)hcd
->rsrc_start
);
2152 if ((retval
= hcd
->driver
->start(hcd
)) < 0) {
2153 dev_err(hcd
->self
.controller
, "startup error %d\n", retval
);
2154 goto err_hcd_driver_start
;
2157 /* starting here, usbcore will pay attention to this root hub */
2158 rhdev
->bus_mA
= min(500u, hcd
->power_budget
);
2159 if ((retval
= register_root_hub(hcd
)) != 0)
2160 goto err_register_root_hub
;
2162 retval
= sysfs_create_group(&rhdev
->dev
.kobj
, &usb_bus_attr_group
);
2164 printk(KERN_ERR
"Cannot register USB bus sysfs attributes: %d\n",
2166 goto error_create_attr_group
;
2168 if (hcd
->uses_new_polling
&& hcd
->poll_rh
)
2169 usb_hcd_poll_rh_status(hcd
);
2172 error_create_attr_group
:
2173 mutex_lock(&usb_bus_list_lock
);
2174 usb_disconnect(&hcd
->self
.root_hub
);
2175 mutex_unlock(&usb_bus_list_lock
);
2176 err_register_root_hub
:
2177 hcd
->driver
->stop(hcd
);
2178 err_hcd_driver_start
:
2180 free_irq(irqnum
, hcd
);
2182 err_hcd_driver_setup
:
2183 hcd
->self
.root_hub
= NULL
;
2185 err_allocate_root_hub
:
2186 usb_deregister_bus(&hcd
->self
);
2188 hcd_buffer_destroy(hcd
);
2191 EXPORT_SYMBOL_GPL(usb_add_hcd
);
2194 * usb_remove_hcd - shutdown processing for generic HCDs
2195 * @hcd: the usb_hcd structure to remove
2196 * Context: !in_interrupt()
2198 * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
2199 * invoking the HCD's stop() method.
2201 void usb_remove_hcd(struct usb_hcd
*hcd
)
2203 dev_info(hcd
->self
.controller
, "remove, state %x\n", hcd
->state
);
2205 if (HC_IS_RUNNING (hcd
->state
))
2206 hcd
->state
= HC_STATE_QUIESCING
;
2208 dev_dbg(hcd
->self
.controller
, "roothub graceful disconnect\n");
2209 spin_lock_irq (&hcd_root_hub_lock
);
2210 hcd
->rh_registered
= 0;
2211 spin_unlock_irq (&hcd_root_hub_lock
);
2214 cancel_work_sync(&hcd
->wakeup_work
);
2217 sysfs_remove_group(&hcd
->self
.root_hub
->dev
.kobj
, &usb_bus_attr_group
);
2218 mutex_lock(&usb_bus_list_lock
);
2219 usb_disconnect(&hcd
->self
.root_hub
);
2220 mutex_unlock(&usb_bus_list_lock
);
2222 hcd
->driver
->stop(hcd
);
2223 hcd
->state
= HC_STATE_HALT
;
2226 del_timer_sync(&hcd
->rh_timer
);
2229 free_irq(hcd
->irq
, hcd
);
2230 usb_deregister_bus(&hcd
->self
);
2231 hcd_buffer_destroy(hcd
);
2233 EXPORT_SYMBOL_GPL(usb_remove_hcd
);
2236 usb_hcd_platform_shutdown(struct platform_device
* dev
)
2238 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
2240 if (hcd
->driver
->shutdown
)
2241 hcd
->driver
->shutdown(hcd
);
2243 EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown
);
2245 /*-------------------------------------------------------------------------*/
2247 #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
2249 struct usb_mon_operations
*mon_ops
;
2252 * The registration is unlocked.
2253 * We do it this way because we do not want to lock in hot paths.
2255 * Notice that the code is minimally error-proof. Because usbmon needs
2256 * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
2259 int usb_mon_register (struct usb_mon_operations
*ops
)
2269 EXPORT_SYMBOL_GPL (usb_mon_register
);
2271 void usb_mon_deregister (void)
2274 if (mon_ops
== NULL
) {
2275 printk(KERN_ERR
"USB: monitor was not registered\n");
2281 EXPORT_SYMBOL_GPL (usb_mon_deregister
);
2283 #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */