dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / usb / core / hcd.c
blob9c4f9b6e57e2923195d67471be30c22fcd6ec917
1 /*
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
18 * for more details.
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/bcd.h>
26 #include <linux/module.h>
27 #include <linux/version.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/completion.h>
31 #include <linux/utsname.h>
32 #include <linux/mm.h>
33 #include <asm/io.h>
34 #include <linux/device.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/mutex.h>
37 #include <asm/irq.h>
38 #include <asm/byteorder.h>
39 #include <asm/unaligned.h>
40 #include <linux/platform_device.h>
41 #include <linux/workqueue.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/types.h>
45 #include <linux/phy/phy.h>
46 #include <linux/usb.h>
47 #include <linux/usb/hcd.h>
48 #include <linux/usb/phy.h>
50 #include "usb.h"
53 /*-------------------------------------------------------------------------*/
56 * USB Host Controller Driver framework
58 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
59 * HCD-specific behaviors/bugs.
61 * This does error checks, tracks devices and urbs, and delegates to a
62 * "hc_driver" only for code (and data) that really needs to know about
63 * hardware differences. That includes root hub registers, i/o queues,
64 * and so on ... but as little else as possible.
66 * Shared code includes most of the "root hub" code (these are emulated,
67 * though each HC's hardware works differently) and PCI glue, plus request
68 * tracking overhead. The HCD code should only block on spinlocks or on
69 * hardware handshaking; blocking on software events (such as other kernel
70 * threads releasing resources, or completing actions) is all generic.
72 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
73 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
74 * only by the hub driver ... and that neither should be seen or used by
75 * usb client device drivers.
77 * Contributors of ideas or unattributed patches include: David Brownell,
78 * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
80 * HISTORY:
81 * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
82 * associated cleanup. "usb_hcd" still != "usb_bus".
83 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
86 /*-------------------------------------------------------------------------*/
88 /* Keep track of which host controller drivers are loaded */
89 unsigned long usb_hcds_loaded;
90 EXPORT_SYMBOL_GPL(usb_hcds_loaded);
92 /* host controllers we manage */
93 LIST_HEAD (usb_bus_list);
94 EXPORT_SYMBOL_GPL (usb_bus_list);
96 /* used when allocating bus numbers */
97 #define USB_MAXBUS 64
98 static DECLARE_BITMAP(busmap, USB_MAXBUS);
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 /*-------------------------------------------------------------------------*/
128 #define KERNEL_REL bin2bcd(((LINUX_VERSION_CODE >> 16) & 0x0ff))
129 #define KERNEL_VER bin2bcd(((LINUX_VERSION_CODE >> 8) & 0x0ff))
131 /* usb 3.0 root hub device descriptor */
132 static const u8 usb3_rh_dev_descriptor[18] = {
133 0x12, /* __u8 bLength; */
134 USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
135 0x00, 0x03, /* __le16 bcdUSB; v3.0 */
137 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
138 0x00, /* __u8 bDeviceSubClass; */
139 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */
140 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
142 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
143 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
144 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
146 0x03, /* __u8 iManufacturer; */
147 0x02, /* __u8 iProduct; */
148 0x01, /* __u8 iSerialNumber; */
149 0x01 /* __u8 bNumConfigurations; */
152 /* usb 2.5 (wireless USB 1.0) root hub device descriptor */
153 static const u8 usb25_rh_dev_descriptor[18] = {
154 0x12, /* __u8 bLength; */
155 USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
156 0x50, 0x02, /* __le16 bcdUSB; v2.5 */
158 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
159 0x00, /* __u8 bDeviceSubClass; */
160 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
161 0xFF, /* __u8 bMaxPacketSize0; always 0xFF (WUSB Spec 7.4.1). */
163 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
164 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
165 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
167 0x03, /* __u8 iManufacturer; */
168 0x02, /* __u8 iProduct; */
169 0x01, /* __u8 iSerialNumber; */
170 0x01 /* __u8 bNumConfigurations; */
173 /* usb 2.0 root hub device descriptor */
174 static const u8 usb2_rh_dev_descriptor[18] = {
175 0x12, /* __u8 bLength; */
176 USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
177 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
179 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
180 0x00, /* __u8 bDeviceSubClass; */
181 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
182 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
184 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
185 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
186 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
188 0x03, /* __u8 iManufacturer; */
189 0x02, /* __u8 iProduct; */
190 0x01, /* __u8 iSerialNumber; */
191 0x01 /* __u8 bNumConfigurations; */
194 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
196 /* usb 1.1 root hub device descriptor */
197 static const u8 usb11_rh_dev_descriptor[18] = {
198 0x12, /* __u8 bLength; */
199 USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
200 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
202 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
203 0x00, /* __u8 bDeviceSubClass; */
204 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
205 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
207 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
208 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
209 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
211 0x03, /* __u8 iManufacturer; */
212 0x02, /* __u8 iProduct; */
213 0x01, /* __u8 iSerialNumber; */
214 0x01 /* __u8 bNumConfigurations; */
218 /*-------------------------------------------------------------------------*/
220 /* Configuration descriptors for our root hubs */
222 static const u8 fs_rh_config_descriptor[] = {
224 /* one configuration */
225 0x09, /* __u8 bLength; */
226 USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
227 0x19, 0x00, /* __le16 wTotalLength; */
228 0x01, /* __u8 bNumInterfaces; (1) */
229 0x01, /* __u8 bConfigurationValue; */
230 0x00, /* __u8 iConfiguration; */
231 0xc0, /* __u8 bmAttributes;
232 Bit 7: must be set,
233 6: Self-powered,
234 5: Remote wakeup,
235 4..0: resvd */
236 0x00, /* __u8 MaxPower; */
238 /* USB 1.1:
239 * USB 2.0, single TT organization (mandatory):
240 * one interface, protocol 0
242 * USB 2.0, multiple TT organization (optional):
243 * two interfaces, protocols 1 (like single TT)
244 * and 2 (multiple TT mode) ... config is
245 * sometimes settable
246 * NOT IMPLEMENTED
249 /* one interface */
250 0x09, /* __u8 if_bLength; */
251 USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
252 0x00, /* __u8 if_bInterfaceNumber; */
253 0x00, /* __u8 if_bAlternateSetting; */
254 0x01, /* __u8 if_bNumEndpoints; */
255 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
256 0x00, /* __u8 if_bInterfaceSubClass; */
257 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
258 0x00, /* __u8 if_iInterface; */
260 /* one endpoint (status change endpoint) */
261 0x07, /* __u8 ep_bLength; */
262 USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
263 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
264 0x03, /* __u8 ep_bmAttributes; Interrupt */
265 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
266 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
269 static const u8 hs_rh_config_descriptor[] = {
271 /* one configuration */
272 0x09, /* __u8 bLength; */
273 USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
274 0x19, 0x00, /* __le16 wTotalLength; */
275 0x01, /* __u8 bNumInterfaces; (1) */
276 0x01, /* __u8 bConfigurationValue; */
277 0x00, /* __u8 iConfiguration; */
278 0xc0, /* __u8 bmAttributes;
279 Bit 7: must be set,
280 6: Self-powered,
281 5: Remote wakeup,
282 4..0: resvd */
283 0x00, /* __u8 MaxPower; */
285 /* USB 1.1:
286 * USB 2.0, single TT organization (mandatory):
287 * one interface, protocol 0
289 * USB 2.0, multiple TT organization (optional):
290 * two interfaces, protocols 1 (like single TT)
291 * and 2 (multiple TT mode) ... config is
292 * sometimes settable
293 * NOT IMPLEMENTED
296 /* one interface */
297 0x09, /* __u8 if_bLength; */
298 USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
299 0x00, /* __u8 if_bInterfaceNumber; */
300 0x00, /* __u8 if_bAlternateSetting; */
301 0x01, /* __u8 if_bNumEndpoints; */
302 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
303 0x00, /* __u8 if_bInterfaceSubClass; */
304 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
305 0x00, /* __u8 if_iInterface; */
307 /* one endpoint (status change endpoint) */
308 0x07, /* __u8 ep_bLength; */
309 USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
310 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
311 0x03, /* __u8 ep_bmAttributes; Interrupt */
312 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
313 * see hub.c:hub_configure() for details. */
314 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
315 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
318 static const u8 ss_rh_config_descriptor[] = {
319 /* one configuration */
320 0x09, /* __u8 bLength; */
321 USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
322 0x1f, 0x00, /* __le16 wTotalLength; */
323 0x01, /* __u8 bNumInterfaces; (1) */
324 0x01, /* __u8 bConfigurationValue; */
325 0x00, /* __u8 iConfiguration; */
326 0xc0, /* __u8 bmAttributes;
327 Bit 7: must be set,
328 6: Self-powered,
329 5: Remote wakeup,
330 4..0: resvd */
331 0x00, /* __u8 MaxPower; */
333 /* one interface */
334 0x09, /* __u8 if_bLength; */
335 USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
336 0x00, /* __u8 if_bInterfaceNumber; */
337 0x00, /* __u8 if_bAlternateSetting; */
338 0x01, /* __u8 if_bNumEndpoints; */
339 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
340 0x00, /* __u8 if_bInterfaceSubClass; */
341 0x00, /* __u8 if_bInterfaceProtocol; */
342 0x00, /* __u8 if_iInterface; */
344 /* one endpoint (status change endpoint) */
345 0x07, /* __u8 ep_bLength; */
346 USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
347 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
348 0x03, /* __u8 ep_bmAttributes; Interrupt */
349 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
350 * see hub.c:hub_configure() for details. */
351 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
352 0x0c, /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
354 /* one SuperSpeed endpoint companion descriptor */
355 0x06, /* __u8 ss_bLength */
356 USB_DT_SS_ENDPOINT_COMP, /* __u8 ss_bDescriptorType; SuperSpeed EP */
357 /* Companion */
358 0x00, /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */
359 0x00, /* __u8 ss_bmAttributes; 1 packet per service interval */
360 0x02, 0x00 /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */
363 /* authorized_default behaviour:
364 * -1 is authorized for all devices except wireless (old behaviour)
365 * 0 is unauthorized for all devices
366 * 1 is authorized for all devices
368 static int authorized_default = -1;
369 module_param(authorized_default, int, S_IRUGO|S_IWUSR);
370 MODULE_PARM_DESC(authorized_default,
371 "Default USB device authorization: 0 is not authorized, 1 is "
372 "authorized, -1 is authorized except for wireless USB (default, "
373 "old behaviour");
374 /*-------------------------------------------------------------------------*/
377 * ascii2desc() - Helper routine for producing UTF-16LE string descriptors
378 * @s: Null-terminated ASCII (actually ISO-8859-1) string
379 * @buf: Buffer for USB string descriptor (header + UTF-16LE)
380 * @len: Length (in bytes; may be odd) of descriptor buffer.
382 * Return: The number of bytes filled in: 2 + 2*strlen(s) or @len,
383 * whichever is less.
385 * Note:
386 * USB String descriptors can contain at most 126 characters; input
387 * strings longer than that are truncated.
389 static unsigned
390 ascii2desc(char const *s, u8 *buf, unsigned len)
392 unsigned n, t = 2 + 2*strlen(s);
394 if (t > 254)
395 t = 254; /* Longest possible UTF string descriptor */
396 if (len > t)
397 len = t;
399 t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */
401 n = len;
402 while (n--) {
403 *buf++ = t;
404 if (!n--)
405 break;
406 *buf++ = t >> 8;
407 t = (unsigned char)*s++;
409 return len;
413 * rh_string() - provides string descriptors for root hub
414 * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
415 * @hcd: the host controller for this root hub
416 * @data: buffer for output packet
417 * @len: length of the provided buffer
419 * Produces either a manufacturer, product or serial number string for the
420 * virtual root hub device.
422 * Return: The number of bytes filled in: the length of the descriptor or
423 * of the provided buffer, whichever is less.
425 static unsigned
426 rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
428 char buf[100];
429 char const *s;
430 static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
432 /* language ids */
433 switch (id) {
434 case 0:
435 /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
436 /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
437 if (len > 4)
438 len = 4;
439 memcpy(data, langids, len);
440 return len;
441 case 1:
442 /* Serial number */
443 s = hcd->self.bus_name;
444 break;
445 case 2:
446 /* Product name */
447 s = hcd->product_desc;
448 break;
449 case 3:
450 /* Manufacturer */
451 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
452 init_utsname()->release, hcd->driver->description);
453 s = buf;
454 break;
455 default:
456 /* Can't happen; caller guarantees it */
457 return 0;
460 return ascii2desc(s, data, len);
464 /* Root hub control transfers execute synchronously */
465 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
467 struct usb_ctrlrequest *cmd;
468 u16 typeReq, wValue, wIndex, wLength;
469 u8 *ubuf = urb->transfer_buffer;
470 unsigned len = 0;
471 int status;
472 u8 patch_wakeup = 0;
473 u8 patch_protocol = 0;
474 u16 tbuf_size;
475 u8 *tbuf = NULL;
476 const u8 *bufp;
478 might_sleep();
480 spin_lock_irq(&hcd_root_hub_lock);
481 status = usb_hcd_link_urb_to_ep(hcd, urb);
482 spin_unlock_irq(&hcd_root_hub_lock);
483 if (status)
484 return status;
485 urb->hcpriv = hcd; /* Indicate it's queued */
487 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
488 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
489 wValue = le16_to_cpu (cmd->wValue);
490 wIndex = le16_to_cpu (cmd->wIndex);
491 wLength = le16_to_cpu (cmd->wLength);
493 if (wLength > urb->transfer_buffer_length)
494 goto error;
497 * tbuf should be at least as big as the
498 * USB hub descriptor.
500 tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength);
501 tbuf = kzalloc(tbuf_size, GFP_KERNEL);
502 if (!tbuf) {
503 status = -ENOMEM;
504 goto err_alloc;
507 bufp = tbuf;
510 urb->actual_length = 0;
511 switch (typeReq) {
513 /* DEVICE REQUESTS */
515 /* The root hub's remote wakeup enable bit is implemented using
516 * driver model wakeup flags. If this system supports wakeup
517 * through USB, userspace may change the default "allow wakeup"
518 * policy through sysfs or these calls.
520 * Most root hubs support wakeup from downstream devices, for
521 * runtime power management (disabling USB clocks and reducing
522 * VBUS power usage). However, not all of them do so; silicon,
523 * board, and BIOS bugs here are not uncommon, so these can't
524 * be treated quite like external hubs.
526 * Likewise, not all root hubs will pass wakeup events upstream,
527 * to wake up the whole system. So don't assume root hub and
528 * controller capabilities are identical.
531 case DeviceRequest | USB_REQ_GET_STATUS:
532 tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev)
533 << USB_DEVICE_REMOTE_WAKEUP)
534 | (1 << USB_DEVICE_SELF_POWERED);
535 tbuf[1] = 0;
536 len = 2;
537 break;
538 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
539 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
540 device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
541 else
542 goto error;
543 break;
544 case DeviceOutRequest | USB_REQ_SET_FEATURE:
545 if (device_can_wakeup(&hcd->self.root_hub->dev)
546 && wValue == USB_DEVICE_REMOTE_WAKEUP)
547 device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
548 else
549 goto error;
550 break;
551 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
552 tbuf[0] = 1;
553 len = 1;
554 /* FALLTHROUGH */
555 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
556 break;
557 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
558 switch (wValue & 0xff00) {
559 case USB_DT_DEVICE << 8:
560 switch (hcd->speed) {
561 case HCD_USB31:
562 case HCD_USB3:
563 bufp = usb3_rh_dev_descriptor;
564 break;
565 case HCD_USB25:
566 bufp = usb25_rh_dev_descriptor;
567 break;
568 case HCD_USB2:
569 bufp = usb2_rh_dev_descriptor;
570 break;
571 case HCD_USB11:
572 bufp = usb11_rh_dev_descriptor;
573 break;
574 default:
575 goto error;
577 len = 18;
578 if (hcd->has_tt)
579 patch_protocol = 1;
580 break;
581 case USB_DT_CONFIG << 8:
582 switch (hcd->speed) {
583 case HCD_USB31:
584 case HCD_USB3:
585 bufp = ss_rh_config_descriptor;
586 len = sizeof ss_rh_config_descriptor;
587 break;
588 case HCD_USB25:
589 case HCD_USB2:
590 bufp = hs_rh_config_descriptor;
591 len = sizeof hs_rh_config_descriptor;
592 break;
593 case HCD_USB11:
594 bufp = fs_rh_config_descriptor;
595 len = sizeof fs_rh_config_descriptor;
596 break;
597 default:
598 goto error;
600 if (device_can_wakeup(&hcd->self.root_hub->dev))
601 patch_wakeup = 1;
602 break;
603 case USB_DT_STRING << 8:
604 if ((wValue & 0xff) < 4)
605 urb->actual_length = rh_string(wValue & 0xff,
606 hcd, ubuf, wLength);
607 else /* unsupported IDs --> "protocol stall" */
608 goto error;
609 break;
610 case USB_DT_BOS << 8:
611 goto nongeneric;
612 default:
613 goto error;
615 break;
616 case DeviceRequest | USB_REQ_GET_INTERFACE:
617 tbuf[0] = 0;
618 len = 1;
619 /* FALLTHROUGH */
620 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
621 break;
622 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
623 /* wValue == urb->dev->devaddr */
624 dev_dbg (hcd->self.controller, "root hub device address %d\n",
625 wValue);
626 break;
628 /* INTERFACE REQUESTS (no defined feature/status flags) */
630 /* ENDPOINT REQUESTS */
632 case EndpointRequest | USB_REQ_GET_STATUS:
633 /* ENDPOINT_HALT flag */
634 tbuf[0] = 0;
635 tbuf[1] = 0;
636 len = 2;
637 /* FALLTHROUGH */
638 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
639 case EndpointOutRequest | USB_REQ_SET_FEATURE:
640 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
641 break;
643 /* CLASS REQUESTS (and errors) */
645 default:
646 nongeneric:
647 /* non-generic request */
648 switch (typeReq) {
649 case GetHubStatus:
650 case GetPortStatus:
651 len = 4;
652 break;
653 case GetHubDescriptor:
654 len = sizeof (struct usb_hub_descriptor);
655 break;
656 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
657 /* len is returned by hub_control */
658 break;
660 status = hcd->driver->hub_control (hcd,
661 typeReq, wValue, wIndex,
662 tbuf, wLength);
664 if (typeReq == GetHubDescriptor)
665 usb_hub_adjust_deviceremovable(hcd->self.root_hub,
666 (struct usb_hub_descriptor *)tbuf);
667 break;
668 error:
669 /* "protocol stall" on error */
670 status = -EPIPE;
673 if (status < 0) {
674 len = 0;
675 if (status != -EPIPE) {
676 dev_dbg (hcd->self.controller,
677 "CTRL: TypeReq=0x%x val=0x%x "
678 "idx=0x%x len=%d ==> %d\n",
679 typeReq, wValue, wIndex,
680 wLength, status);
682 } else if (status > 0) {
683 /* hub_control may return the length of data copied. */
684 len = status;
685 status = 0;
687 if (len) {
688 if (urb->transfer_buffer_length < len)
689 len = urb->transfer_buffer_length;
690 urb->actual_length = len;
691 /* always USB_DIR_IN, toward host */
692 memcpy (ubuf, bufp, len);
694 /* report whether RH hardware supports remote wakeup */
695 if (patch_wakeup &&
696 len > offsetof (struct usb_config_descriptor,
697 bmAttributes))
698 ((struct usb_config_descriptor *)ubuf)->bmAttributes
699 |= USB_CONFIG_ATT_WAKEUP;
701 /* report whether RH hardware has an integrated TT */
702 if (patch_protocol &&
703 len > offsetof(struct usb_device_descriptor,
704 bDeviceProtocol))
705 ((struct usb_device_descriptor *) ubuf)->
706 bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT;
709 kfree(tbuf);
710 err_alloc:
712 /* any errors get returned through the urb completion */
713 spin_lock_irq(&hcd_root_hub_lock);
714 usb_hcd_unlink_urb_from_ep(hcd, urb);
715 usb_hcd_giveback_urb(hcd, urb, status);
716 spin_unlock_irq(&hcd_root_hub_lock);
717 return 0;
720 /*-------------------------------------------------------------------------*/
723 * Root Hub interrupt transfers are polled using a timer if the
724 * driver requests it; otherwise the driver is responsible for
725 * calling usb_hcd_poll_rh_status() when an event occurs.
727 * Completions are called in_interrupt(), but they may or may not
728 * be in_irq().
730 void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
732 struct urb *urb;
733 int length;
734 unsigned long flags;
735 char buffer[6]; /* Any root hubs with > 31 ports? */
737 if (unlikely(!hcd->rh_pollable))
738 return;
739 if (!hcd->uses_new_polling && !hcd->status_urb)
740 return;
742 length = hcd->driver->hub_status_data(hcd, buffer);
743 if (length > 0) {
745 /* try to complete the status urb */
746 spin_lock_irqsave(&hcd_root_hub_lock, flags);
747 urb = hcd->status_urb;
748 if (urb) {
749 clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
750 hcd->status_urb = NULL;
751 urb->actual_length = length;
752 memcpy(urb->transfer_buffer, buffer, length);
754 usb_hcd_unlink_urb_from_ep(hcd, urb);
755 usb_hcd_giveback_urb(hcd, urb, 0);
756 } else {
757 length = 0;
758 set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
760 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
763 /* The USB 2.0 spec says 256 ms. This is close enough and won't
764 * exceed that limit if HZ is 100. The math is more clunky than
765 * maybe expected, this is to make sure that all timers for USB devices
766 * fire at the same time to give the CPU a break in between */
767 if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
768 (length == 0 && hcd->status_urb != NULL))
769 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
771 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
773 /* timer callback */
774 static void rh_timer_func (unsigned long _hcd)
776 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
779 /*-------------------------------------------------------------------------*/
781 static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
783 int retval;
784 unsigned long flags;
785 unsigned len = 1 + (urb->dev->maxchild / 8);
787 spin_lock_irqsave (&hcd_root_hub_lock, flags);
788 if (hcd->status_urb || urb->transfer_buffer_length < len) {
789 dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
790 retval = -EINVAL;
791 goto done;
794 retval = usb_hcd_link_urb_to_ep(hcd, urb);
795 if (retval)
796 goto done;
798 hcd->status_urb = urb;
799 urb->hcpriv = hcd; /* indicate it's queued */
800 if (!hcd->uses_new_polling)
801 mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
803 /* If a status change has already occurred, report it ASAP */
804 else if (HCD_POLL_PENDING(hcd))
805 mod_timer(&hcd->rh_timer, jiffies);
806 retval = 0;
807 done:
808 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
809 return retval;
812 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
814 if (usb_endpoint_xfer_int(&urb->ep->desc))
815 return rh_queue_status (hcd, urb);
816 if (usb_endpoint_xfer_control(&urb->ep->desc))
817 return rh_call_control (hcd, urb);
818 return -EINVAL;
821 /*-------------------------------------------------------------------------*/
823 /* Unlinks of root-hub control URBs are legal, but they don't do anything
824 * since these URBs always execute synchronously.
826 static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
828 unsigned long flags;
829 int rc;
831 spin_lock_irqsave(&hcd_root_hub_lock, flags);
832 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
833 if (rc)
834 goto done;
836 if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
837 ; /* Do nothing */
839 } else { /* Status URB */
840 if (!hcd->uses_new_polling)
841 del_timer (&hcd->rh_timer);
842 if (urb == hcd->status_urb) {
843 hcd->status_urb = NULL;
844 usb_hcd_unlink_urb_from_ep(hcd, urb);
845 usb_hcd_giveback_urb(hcd, urb, status);
848 done:
849 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
850 return rc;
856 * Show & store the current value of authorized_default
858 static ssize_t authorized_default_show(struct device *dev,
859 struct device_attribute *attr, char *buf)
861 struct usb_device *rh_usb_dev = to_usb_device(dev);
862 struct usb_bus *usb_bus = rh_usb_dev->bus;
863 struct usb_hcd *hcd;
865 hcd = bus_to_hcd(usb_bus);
866 return snprintf(buf, PAGE_SIZE, "%u\n", !!HCD_DEV_AUTHORIZED(hcd));
869 static ssize_t authorized_default_store(struct device *dev,
870 struct device_attribute *attr,
871 const char *buf, size_t size)
873 ssize_t result;
874 unsigned val;
875 struct usb_device *rh_usb_dev = to_usb_device(dev);
876 struct usb_bus *usb_bus = rh_usb_dev->bus;
877 struct usb_hcd *hcd;
879 hcd = bus_to_hcd(usb_bus);
880 result = sscanf(buf, "%u\n", &val);
881 if (result == 1) {
882 if (val)
883 set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
884 else
885 clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
887 result = size;
888 } else {
889 result = -EINVAL;
891 return result;
893 static DEVICE_ATTR_RW(authorized_default);
896 * interface_authorized_default_show - show default authorization status
897 * for USB interfaces
899 * note: interface_authorized_default is the default value
900 * for initializing the authorized attribute of interfaces
902 static ssize_t interface_authorized_default_show(struct device *dev,
903 struct device_attribute *attr, char *buf)
905 struct usb_device *usb_dev = to_usb_device(dev);
906 struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
908 return sprintf(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd));
912 * interface_authorized_default_store - store default authorization status
913 * for USB interfaces
915 * note: interface_authorized_default is the default value
916 * for initializing the authorized attribute of interfaces
918 static ssize_t interface_authorized_default_store(struct device *dev,
919 struct device_attribute *attr, const char *buf, size_t count)
921 struct usb_device *usb_dev = to_usb_device(dev);
922 struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
923 int rc = count;
924 bool val;
926 if (strtobool(buf, &val) != 0)
927 return -EINVAL;
929 if (val)
930 set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
931 else
932 clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
934 return rc;
936 static DEVICE_ATTR_RW(interface_authorized_default);
938 /* Group all the USB bus attributes */
939 static struct attribute *usb_bus_attrs[] = {
940 &dev_attr_authorized_default.attr,
941 &dev_attr_interface_authorized_default.attr,
942 NULL,
945 static struct attribute_group usb_bus_attr_group = {
946 .name = NULL, /* we want them in the same directory */
947 .attrs = usb_bus_attrs,
952 /*-------------------------------------------------------------------------*/
955 * usb_bus_init - shared initialization code
956 * @bus: the bus structure being initialized
958 * This code is used to initialize a usb_bus structure, memory for which is
959 * separately managed.
961 static void usb_bus_init (struct usb_bus *bus)
963 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
965 bus->devnum_next = 1;
967 bus->root_hub = NULL;
968 bus->busnum = -1;
969 bus->bandwidth_allocated = 0;
970 bus->bandwidth_int_reqs = 0;
971 bus->bandwidth_isoc_reqs = 0;
972 mutex_init(&bus->devnum_next_mutex);
974 INIT_LIST_HEAD (&bus->bus_list);
977 /*-------------------------------------------------------------------------*/
980 * usb_register_bus - registers the USB host controller with the usb core
981 * @bus: pointer to the bus to register
982 * Context: !in_interrupt()
984 * Assigns a bus number, and links the controller into usbcore data
985 * structures so that it can be seen by scanning the bus list.
987 * Return: 0 if successful. A negative error code otherwise.
989 static int usb_register_bus(struct usb_bus *bus)
991 int result = -E2BIG;
992 int busnum;
994 mutex_lock(&usb_bus_list_lock);
995 busnum = find_next_zero_bit(busmap, USB_MAXBUS, 1);
996 if (busnum >= USB_MAXBUS) {
997 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
998 goto error_find_busnum;
1000 set_bit(busnum, busmap);
1001 bus->busnum = busnum;
1003 /* Add it to the local list of buses */
1004 list_add (&bus->bus_list, &usb_bus_list);
1005 mutex_unlock(&usb_bus_list_lock);
1007 usb_notify_add_bus(bus);
1009 dev_info (bus->controller, "new USB bus registered, assigned bus "
1010 "number %d\n", bus->busnum);
1011 return 0;
1013 error_find_busnum:
1014 mutex_unlock(&usb_bus_list_lock);
1015 return result;
1019 * usb_deregister_bus - deregisters the USB host controller
1020 * @bus: pointer to the bus to deregister
1021 * Context: !in_interrupt()
1023 * Recycles the bus number, and unlinks the controller from usbcore data
1024 * structures so that it won't be seen by scanning the bus list.
1026 static void usb_deregister_bus (struct usb_bus *bus)
1028 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
1031 * NOTE: make sure that all the devices are removed by the
1032 * controller code, as well as having it call this when cleaning
1033 * itself up
1035 mutex_lock(&usb_bus_list_lock);
1036 list_del (&bus->bus_list);
1037 mutex_unlock(&usb_bus_list_lock);
1039 usb_notify_remove_bus(bus);
1041 clear_bit(bus->busnum, busmap);
1045 * register_root_hub - called by usb_add_hcd() to register a root hub
1046 * @hcd: host controller for this root hub
1048 * This function registers the root hub with the USB subsystem. It sets up
1049 * the device properly in the device tree and then calls usb_new_device()
1050 * to register the usb device. It also assigns the root hub's USB address
1051 * (always 1).
1053 * Return: 0 if successful. A negative error code otherwise.
1055 static int register_root_hub(struct usb_hcd *hcd)
1057 struct device *parent_dev = hcd->self.controller;
1058 struct usb_device *usb_dev = hcd->self.root_hub;
1059 const int devnum = 1;
1060 int retval;
1062 usb_dev->devnum = devnum;
1063 usb_dev->bus->devnum_next = devnum + 1;
1064 memset (&usb_dev->bus->devmap.devicemap, 0,
1065 sizeof usb_dev->bus->devmap.devicemap);
1066 set_bit (devnum, usb_dev->bus->devmap.devicemap);
1067 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
1069 mutex_lock(&usb_bus_list_lock);
1071 usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1072 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
1073 if (retval != sizeof usb_dev->descriptor) {
1074 mutex_unlock(&usb_bus_list_lock);
1075 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
1076 dev_name(&usb_dev->dev), retval);
1077 return (retval < 0) ? retval : -EMSGSIZE;
1080 if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) {
1081 retval = usb_get_bos_descriptor(usb_dev);
1082 if (!retval) {
1083 usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev);
1084 } else if (usb_dev->speed >= USB_SPEED_SUPER) {
1085 mutex_unlock(&usb_bus_list_lock);
1086 dev_dbg(parent_dev, "can't read %s bos descriptor %d\n",
1087 dev_name(&usb_dev->dev), retval);
1088 return retval;
1092 retval = usb_new_device (usb_dev);
1093 if (retval) {
1094 dev_err (parent_dev, "can't register root hub for %s, %d\n",
1095 dev_name(&usb_dev->dev), retval);
1096 } else {
1097 spin_lock_irq (&hcd_root_hub_lock);
1098 hcd->rh_registered = 1;
1099 spin_unlock_irq (&hcd_root_hub_lock);
1101 /* Did the HC die before the root hub was registered? */
1102 if (HCD_DEAD(hcd))
1103 usb_hc_died (hcd); /* This time clean up */
1105 mutex_unlock(&usb_bus_list_lock);
1107 return retval;
1111 * usb_hcd_start_port_resume - a root-hub port is sending a resume signal
1112 * @bus: the bus which the root hub belongs to
1113 * @portnum: the port which is being resumed
1115 * HCDs should call this function when they know that a resume signal is
1116 * being sent to a root-hub port. The root hub will be prevented from
1117 * going into autosuspend until usb_hcd_end_port_resume() is called.
1119 * The bus's private lock must be held by the caller.
1121 void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum)
1123 unsigned bit = 1 << portnum;
1125 if (!(bus->resuming_ports & bit)) {
1126 bus->resuming_ports |= bit;
1127 pm_runtime_get_noresume(&bus->root_hub->dev);
1130 EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume);
1133 * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal
1134 * @bus: the bus which the root hub belongs to
1135 * @portnum: the port which is being resumed
1137 * HCDs should call this function when they know that a resume signal has
1138 * stopped being sent to a root-hub port. The root hub will be allowed to
1139 * autosuspend again.
1141 * The bus's private lock must be held by the caller.
1143 void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum)
1145 unsigned bit = 1 << portnum;
1147 if (bus->resuming_ports & bit) {
1148 bus->resuming_ports &= ~bit;
1149 pm_runtime_put_noidle(&bus->root_hub->dev);
1152 EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume);
1154 /*-------------------------------------------------------------------------*/
1157 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
1158 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
1159 * @is_input: true iff the transaction sends data to the host
1160 * @isoc: true for isochronous transactions, false for interrupt ones
1161 * @bytecount: how many bytes in the transaction.
1163 * Return: Approximate bus time in nanoseconds for a periodic transaction.
1165 * Note:
1166 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
1167 * scheduled in software, this function is only used for such scheduling.
1169 long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
1171 unsigned long tmp;
1173 switch (speed) {
1174 case USB_SPEED_LOW: /* INTR only */
1175 if (is_input) {
1176 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
1177 return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
1178 } else {
1179 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
1180 return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
1182 case USB_SPEED_FULL: /* ISOC or INTR */
1183 if (isoc) {
1184 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1185 return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
1186 } else {
1187 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1188 return 9107L + BW_HOST_DELAY + tmp;
1190 case USB_SPEED_HIGH: /* ISOC or INTR */
1191 /* FIXME adjust for input vs output */
1192 if (isoc)
1193 tmp = HS_NSECS_ISO (bytecount);
1194 else
1195 tmp = HS_NSECS (bytecount);
1196 return tmp;
1197 default:
1198 pr_debug ("%s: bogus device speed!\n", usbcore_name);
1199 return -1;
1202 EXPORT_SYMBOL_GPL(usb_calc_bus_time);
1205 /*-------------------------------------------------------------------------*/
1208 * Generic HC operations.
1211 /*-------------------------------------------------------------------------*/
1214 * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
1215 * @hcd: host controller to which @urb was submitted
1216 * @urb: URB being submitted
1218 * Host controller drivers should call this routine in their enqueue()
1219 * method. The HCD's private spinlock must be held and interrupts must
1220 * be disabled. The actions carried out here are required for URB
1221 * submission, as well as for endpoint shutdown and for usb_kill_urb.
1223 * Return: 0 for no error, otherwise a negative error code (in which case
1224 * the enqueue() method must fail). If no error occurs but enqueue() fails
1225 * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
1226 * the private spinlock and returning.
1228 int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
1230 int rc = 0;
1232 spin_lock(&hcd_urb_list_lock);
1234 /* Check that the URB isn't being killed */
1235 if (unlikely(atomic_read(&urb->reject))) {
1236 rc = -EPERM;
1237 goto done;
1240 if (unlikely(!urb->ep->enabled)) {
1241 rc = -ENOENT;
1242 goto done;
1245 if (unlikely(!urb->dev->can_submit)) {
1246 rc = -EHOSTUNREACH;
1247 goto done;
1251 * Check the host controller's state and add the URB to the
1252 * endpoint's queue.
1254 if (HCD_RH_RUNNING(hcd)) {
1255 urb->unlinked = 0;
1256 list_add_tail(&urb->urb_list, &urb->ep->urb_list);
1257 } else {
1258 rc = -ESHUTDOWN;
1259 goto done;
1261 done:
1262 spin_unlock(&hcd_urb_list_lock);
1263 return rc;
1265 EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
1268 * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
1269 * @hcd: host controller to which @urb was submitted
1270 * @urb: URB being checked for unlinkability
1271 * @status: error code to store in @urb if the unlink succeeds
1273 * Host controller drivers should call this routine in their dequeue()
1274 * method. The HCD's private spinlock must be held and interrupts must
1275 * be disabled. The actions carried out here are required for making
1276 * sure than an unlink is valid.
1278 * Return: 0 for no error, otherwise a negative error code (in which case
1279 * the dequeue() method must fail). The possible error codes are:
1281 * -EIDRM: @urb was not submitted or has already completed.
1282 * The completion function may not have been called yet.
1284 * -EBUSY: @urb has already been unlinked.
1286 int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
1287 int status)
1289 struct list_head *tmp;
1291 /* insist the urb is still queued */
1292 list_for_each(tmp, &urb->ep->urb_list) {
1293 if (tmp == &urb->urb_list)
1294 break;
1296 if (tmp != &urb->urb_list)
1297 return -EIDRM;
1299 /* Any status except -EINPROGRESS means something already started to
1300 * unlink this URB from the hardware. So there's no more work to do.
1302 if (urb->unlinked)
1303 return -EBUSY;
1304 urb->unlinked = status;
1305 return 0;
1307 EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
1310 * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
1311 * @hcd: host controller to which @urb was submitted
1312 * @urb: URB being unlinked
1314 * Host controller drivers should call this routine before calling
1315 * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
1316 * interrupts must be disabled. The actions carried out here are required
1317 * for URB completion.
1319 void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
1321 /* clear all state linking urb to this dev (and hcd) */
1322 spin_lock(&hcd_urb_list_lock);
1323 list_del_init(&urb->urb_list);
1324 spin_unlock(&hcd_urb_list_lock);
1326 EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
1329 * Some usb host controllers can only perform dma using a small SRAM area.
1330 * The usb core itself is however optimized for host controllers that can dma
1331 * using regular system memory - like pci devices doing bus mastering.
1333 * To support host controllers with limited dma capabilities we provide dma
1334 * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
1335 * For this to work properly the host controller code must first use the
1336 * function dma_declare_coherent_memory() to point out which memory area
1337 * that should be used for dma allocations.
1339 * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
1340 * dma using dma_alloc_coherent() which in turn allocates from the memory
1341 * area pointed out with dma_declare_coherent_memory().
1343 * So, to summarize...
1345 * - We need "local" memory, canonical example being
1346 * a small SRAM on a discrete controller being the
1347 * only memory that the controller can read ...
1348 * (a) "normal" kernel memory is no good, and
1349 * (b) there's not enough to share
1351 * - The only *portable* hook for such stuff in the
1352 * DMA framework is dma_declare_coherent_memory()
1354 * - So we use that, even though the primary requirement
1355 * is that the memory be "local" (hence addressable
1356 * by that device), not "coherent".
1360 static int hcd_alloc_coherent(struct usb_bus *bus,
1361 gfp_t mem_flags, dma_addr_t *dma_handle,
1362 void **vaddr_handle, size_t size,
1363 enum dma_data_direction dir)
1365 unsigned char *vaddr;
1367 if (*vaddr_handle == NULL) {
1368 WARN_ON_ONCE(1);
1369 return -EFAULT;
1372 vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
1373 mem_flags, dma_handle);
1374 if (!vaddr)
1375 return -ENOMEM;
1378 * Store the virtual address of the buffer at the end
1379 * of the allocated dma buffer. The size of the buffer
1380 * may be uneven so use unaligned functions instead
1381 * of just rounding up. It makes sense to optimize for
1382 * memory footprint over access speed since the amount
1383 * of memory available for dma may be limited.
1385 put_unaligned((unsigned long)*vaddr_handle,
1386 (unsigned long *)(vaddr + size));
1388 if (dir == DMA_TO_DEVICE)
1389 memcpy(vaddr, *vaddr_handle, size);
1391 *vaddr_handle = vaddr;
1392 return 0;
1395 static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
1396 void **vaddr_handle, size_t size,
1397 enum dma_data_direction dir)
1399 unsigned char *vaddr = *vaddr_handle;
1401 vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
1403 if (dir == DMA_FROM_DEVICE)
1404 memcpy(vaddr, *vaddr_handle, size);
1406 hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
1408 *vaddr_handle = vaddr;
1409 *dma_handle = 0;
1412 void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb)
1414 if (urb->transfer_flags & URB_SETUP_MAP_SINGLE)
1415 dma_unmap_single(hcd->self.controller,
1416 urb->setup_dma,
1417 sizeof(struct usb_ctrlrequest),
1418 DMA_TO_DEVICE);
1419 else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL)
1420 hcd_free_coherent(urb->dev->bus,
1421 &urb->setup_dma,
1422 (void **) &urb->setup_packet,
1423 sizeof(struct usb_ctrlrequest),
1424 DMA_TO_DEVICE);
1426 /* Make it safe to call this routine more than once */
1427 urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL);
1429 EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma);
1431 static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1433 if (hcd->driver->unmap_urb_for_dma)
1434 hcd->driver->unmap_urb_for_dma(hcd, urb);
1435 else
1436 usb_hcd_unmap_urb_for_dma(hcd, urb);
1439 void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1441 enum dma_data_direction dir;
1443 usb_hcd_unmap_urb_setup_for_dma(hcd, urb);
1445 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1446 if (urb->transfer_flags & URB_DMA_MAP_SG)
1447 dma_unmap_sg(hcd->self.controller,
1448 urb->sg,
1449 urb->num_sgs,
1450 dir);
1451 else if (urb->transfer_flags & URB_DMA_MAP_PAGE)
1452 dma_unmap_page(hcd->self.controller,
1453 urb->transfer_dma,
1454 urb->transfer_buffer_length,
1455 dir);
1456 else if (urb->transfer_flags & URB_DMA_MAP_SINGLE)
1457 dma_unmap_single(hcd->self.controller,
1458 urb->transfer_dma,
1459 urb->transfer_buffer_length,
1460 dir);
1461 else if (urb->transfer_flags & URB_MAP_LOCAL)
1462 hcd_free_coherent(urb->dev->bus,
1463 &urb->transfer_dma,
1464 &urb->transfer_buffer,
1465 urb->transfer_buffer_length,
1466 dir);
1468 /* Make it safe to call this routine more than once */
1469 urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
1470 URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
1472 EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma);
1474 static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1475 gfp_t mem_flags)
1477 if (hcd->driver->map_urb_for_dma)
1478 return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags);
1479 else
1480 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
1483 int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1484 gfp_t mem_flags)
1486 enum dma_data_direction dir;
1487 int ret = 0;
1489 /* Map the URB's buffers for DMA access.
1490 * Lower level HCD code should use *_dma exclusively,
1491 * unless it uses pio or talks to another transport,
1492 * or uses the provided scatter gather list for bulk.
1495 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1496 if (hcd->self.uses_pio_for_control)
1497 return ret;
1498 if (hcd->self.uses_dma) {
1499 urb->setup_dma = dma_map_single(
1500 hcd->self.controller,
1501 urb->setup_packet,
1502 sizeof(struct usb_ctrlrequest),
1503 DMA_TO_DEVICE);
1504 if (dma_mapping_error(hcd->self.controller,
1505 urb->setup_dma))
1506 return -EAGAIN;
1507 urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
1508 } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
1509 ret = hcd_alloc_coherent(
1510 urb->dev->bus, mem_flags,
1511 &urb->setup_dma,
1512 (void **)&urb->setup_packet,
1513 sizeof(struct usb_ctrlrequest),
1514 DMA_TO_DEVICE);
1515 if (ret)
1516 return ret;
1517 urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
1521 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1522 if (urb->transfer_buffer_length != 0
1523 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1524 if (hcd->self.uses_dma) {
1525 if (urb->num_sgs) {
1526 int n;
1528 /* We don't support sg for isoc transfers ! */
1529 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1530 WARN_ON(1);
1531 return -EINVAL;
1534 n = dma_map_sg(
1535 hcd->self.controller,
1536 urb->sg,
1537 urb->num_sgs,
1538 dir);
1539 if (n <= 0)
1540 ret = -EAGAIN;
1541 else
1542 urb->transfer_flags |= URB_DMA_MAP_SG;
1543 urb->num_mapped_sgs = n;
1544 if (n != urb->num_sgs)
1545 urb->transfer_flags |=
1546 URB_DMA_SG_COMBINED;
1547 } else if (urb->sg) {
1548 struct scatterlist *sg = urb->sg;
1549 urb->transfer_dma = dma_map_page(
1550 hcd->self.controller,
1551 sg_page(sg),
1552 sg->offset,
1553 urb->transfer_buffer_length,
1554 dir);
1555 if (dma_mapping_error(hcd->self.controller,
1556 urb->transfer_dma))
1557 ret = -EAGAIN;
1558 else
1559 urb->transfer_flags |= URB_DMA_MAP_PAGE;
1560 } else if (is_vmalloc_addr(urb->transfer_buffer)) {
1561 WARN_ONCE(1, "transfer buffer not dma capable\n");
1562 ret = -EAGAIN;
1563 } else {
1564 urb->transfer_dma = dma_map_single(
1565 hcd->self.controller,
1566 urb->transfer_buffer,
1567 urb->transfer_buffer_length,
1568 dir);
1569 if (dma_mapping_error(hcd->self.controller,
1570 urb->transfer_dma))
1571 ret = -EAGAIN;
1572 else
1573 urb->transfer_flags |= URB_DMA_MAP_SINGLE;
1575 } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
1576 ret = hcd_alloc_coherent(
1577 urb->dev->bus, mem_flags,
1578 &urb->transfer_dma,
1579 &urb->transfer_buffer,
1580 urb->transfer_buffer_length,
1581 dir);
1582 if (ret == 0)
1583 urb->transfer_flags |= URB_MAP_LOCAL;
1585 if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
1586 URB_SETUP_MAP_LOCAL)))
1587 usb_hcd_unmap_urb_for_dma(hcd, urb);
1589 return ret;
1591 EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
1593 /*-------------------------------------------------------------------------*/
1595 /* may be called in any context with a valid urb->dev usecount
1596 * caller surrenders "ownership" of urb
1597 * expects usb_submit_urb() to have sanity checked and conditioned all
1598 * inputs in the urb
1600 int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1602 int status;
1603 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1605 /* increment urb's reference count as part of giving it to the HCD
1606 * (which will control it). HCD guarantees that it either returns
1607 * an error or calls giveback(), but not both.
1609 usb_get_urb(urb);
1610 atomic_inc(&urb->use_count);
1611 atomic_inc(&urb->dev->urbnum);
1612 usbmon_urb_submit(&hcd->self, urb);
1614 /* NOTE requirements on root-hub callers (usbfs and the hub
1615 * driver, for now): URBs' urb->transfer_buffer must be
1616 * valid and usb_buffer_{sync,unmap}() not be needed, since
1617 * they could clobber root hub response data. Also, control
1618 * URBs must be submitted in process context with interrupts
1619 * enabled.
1622 if (is_root_hub(urb->dev)) {
1623 status = rh_urb_enqueue(hcd, urb);
1624 } else {
1625 status = map_urb_for_dma(hcd, urb, mem_flags);
1626 if (likely(status == 0)) {
1627 status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
1628 if (unlikely(status))
1629 unmap_urb_for_dma(hcd, urb);
1633 if (unlikely(status)) {
1634 usbmon_urb_submit_error(&hcd->self, urb, status);
1635 urb->hcpriv = NULL;
1636 INIT_LIST_HEAD(&urb->urb_list);
1637 atomic_dec(&urb->use_count);
1638 atomic_dec(&urb->dev->urbnum);
1639 if (atomic_read(&urb->reject))
1640 wake_up(&usb_kill_urb_queue);
1641 usb_put_urb(urb);
1643 return status;
1646 /*-------------------------------------------------------------------------*/
1648 /* this makes the hcd giveback() the urb more quickly, by kicking it
1649 * off hardware queues (which may take a while) and returning it as
1650 * soon as practical. we've already set up the urb's return status,
1651 * but we can't know if the callback completed already.
1653 static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
1655 int value;
1657 if (is_root_hub(urb->dev))
1658 value = usb_rh_urb_dequeue(hcd, urb, status);
1659 else {
1661 /* The only reason an HCD might fail this call is if
1662 * it has not yet fully queued the urb to begin with.
1663 * Such failures should be harmless. */
1664 value = hcd->driver->urb_dequeue(hcd, urb, status);
1666 return value;
1670 * called in any context
1672 * caller guarantees urb won't be recycled till both unlink()
1673 * and the urb's completion function return
1675 int usb_hcd_unlink_urb (struct urb *urb, int status)
1677 struct usb_hcd *hcd;
1678 struct usb_device *udev = urb->dev;
1679 int retval = -EIDRM;
1680 unsigned long flags;
1682 /* Prevent the device and bus from going away while
1683 * the unlink is carried out. If they are already gone
1684 * then urb->use_count must be 0, since disconnected
1685 * devices can't have any active URBs.
1687 spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
1688 if (atomic_read(&urb->use_count) > 0) {
1689 retval = 0;
1690 usb_get_dev(udev);
1692 spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
1693 if (retval == 0) {
1694 hcd = bus_to_hcd(urb->dev->bus);
1695 retval = unlink1(hcd, urb, status);
1696 if (retval == 0)
1697 retval = -EINPROGRESS;
1698 else if (retval != -EIDRM && retval != -EBUSY)
1699 dev_dbg(&udev->dev, "hcd_unlink_urb %pK fail %d\n",
1700 urb, retval);
1701 usb_put_dev(udev);
1703 return retval;
1706 /*-------------------------------------------------------------------------*/
1708 static void __usb_hcd_giveback_urb(struct urb *urb)
1710 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1711 struct usb_anchor *anchor = urb->anchor;
1712 int status = urb->unlinked;
1713 unsigned long flags;
1715 urb->hcpriv = NULL;
1716 if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
1717 urb->actual_length < urb->transfer_buffer_length &&
1718 !status))
1719 status = -EREMOTEIO;
1721 unmap_urb_for_dma(hcd, urb);
1722 usbmon_urb_complete(&hcd->self, urb, status);
1723 usb_anchor_suspend_wakeups(anchor);
1724 usb_unanchor_urb(urb);
1725 if (likely(status == 0))
1726 usb_led_activity(USB_LED_EVENT_HOST);
1728 /* pass ownership to the completion handler */
1729 urb->status = status;
1732 * We disable local IRQs here avoid possible deadlock because
1733 * drivers may call spin_lock() to hold lock which might be
1734 * acquired in one hard interrupt handler.
1736 * The local_irq_save()/local_irq_restore() around complete()
1737 * will be removed if current USB drivers have been cleaned up
1738 * and no one may trigger the above deadlock situation when
1739 * running complete() in tasklet.
1741 local_irq_save(flags);
1742 urb->complete(urb);
1743 local_irq_restore(flags);
1745 usb_anchor_resume_wakeups(anchor);
1746 atomic_dec(&urb->use_count);
1747 if (unlikely(atomic_read(&urb->reject)))
1748 wake_up(&usb_kill_urb_queue);
1749 usb_put_urb(urb);
1752 static void usb_giveback_urb_bh(unsigned long param)
1754 struct giveback_urb_bh *bh = (struct giveback_urb_bh *)param;
1755 struct list_head local_list;
1757 spin_lock_irq(&bh->lock);
1758 bh->running = true;
1759 restart:
1760 list_replace_init(&bh->head, &local_list);
1761 spin_unlock_irq(&bh->lock);
1763 while (!list_empty(&local_list)) {
1764 struct urb *urb;
1766 urb = list_entry(local_list.next, struct urb, urb_list);
1767 list_del_init(&urb->urb_list);
1768 bh->completing_ep = urb->ep;
1769 __usb_hcd_giveback_urb(urb);
1770 bh->completing_ep = NULL;
1773 /* check if there are new URBs to giveback */
1774 spin_lock_irq(&bh->lock);
1775 if (!list_empty(&bh->head))
1776 goto restart;
1777 bh->running = false;
1778 spin_unlock_irq(&bh->lock);
1782 * usb_hcd_giveback_urb - return URB from HCD to device driver
1783 * @hcd: host controller returning the URB
1784 * @urb: urb being returned to the USB device driver.
1785 * @status: completion status code for the URB.
1786 * Context: in_interrupt()
1788 * This hands the URB from HCD to its USB device driver, using its
1789 * completion function. The HCD has freed all per-urb resources
1790 * (and is done using urb->hcpriv). It also released all HCD locks;
1791 * the device driver won't cause problems if it frees, modifies,
1792 * or resubmits this URB.
1794 * If @urb was unlinked, the value of @status will be overridden by
1795 * @urb->unlinked. Erroneous short transfers are detected in case
1796 * the HCD hasn't checked for them.
1798 void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
1800 struct giveback_urb_bh *bh;
1801 bool running, high_prio_bh;
1803 /* pass status to tasklet via unlinked */
1804 if (likely(!urb->unlinked))
1805 urb->unlinked = status;
1807 if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
1808 __usb_hcd_giveback_urb(urb);
1809 return;
1812 if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) {
1813 bh = &hcd->high_prio_bh;
1814 high_prio_bh = true;
1815 } else {
1816 bh = &hcd->low_prio_bh;
1817 high_prio_bh = false;
1820 spin_lock(&bh->lock);
1821 list_add_tail(&urb->urb_list, &bh->head);
1822 running = bh->running;
1823 spin_unlock(&bh->lock);
1825 if (running)
1827 else if (high_prio_bh)
1828 tasklet_hi_schedule(&bh->bh);
1829 else
1830 tasklet_schedule(&bh->bh);
1832 EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
1834 /*-------------------------------------------------------------------------*/
1836 /* Cancel all URBs pending on this endpoint and wait for the endpoint's
1837 * queue to drain completely. The caller must first insure that no more
1838 * URBs can be submitted for this endpoint.
1840 void usb_hcd_flush_endpoint(struct usb_device *udev,
1841 struct usb_host_endpoint *ep)
1843 struct usb_hcd *hcd;
1844 struct urb *urb;
1846 if (!ep)
1847 return;
1848 might_sleep();
1849 hcd = bus_to_hcd(udev->bus);
1851 /* No more submits can occur */
1852 spin_lock_irq(&hcd_urb_list_lock);
1853 rescan:
1854 list_for_each_entry_reverse(urb, &ep->urb_list, urb_list) {
1855 int is_in;
1857 if (urb->unlinked)
1858 continue;
1859 usb_get_urb (urb);
1860 is_in = usb_urb_dir_in(urb);
1861 spin_unlock(&hcd_urb_list_lock);
1863 /* kick hcd */
1864 unlink1(hcd, urb, -ESHUTDOWN);
1865 dev_dbg (hcd->self.controller,
1866 "shutdown urb %pK ep%d%s%s\n",
1867 urb, usb_endpoint_num(&ep->desc),
1868 is_in ? "in" : "out",
1869 ({ char *s;
1871 switch (usb_endpoint_type(&ep->desc)) {
1872 case USB_ENDPOINT_XFER_CONTROL:
1873 s = ""; break;
1874 case USB_ENDPOINT_XFER_BULK:
1875 s = "-bulk"; break;
1876 case USB_ENDPOINT_XFER_INT:
1877 s = "-intr"; break;
1878 default:
1879 s = "-iso"; break;
1882 }));
1883 usb_put_urb (urb);
1885 /* list contents may have changed */
1886 spin_lock(&hcd_urb_list_lock);
1887 goto rescan;
1889 spin_unlock_irq(&hcd_urb_list_lock);
1891 /* Wait until the endpoint queue is completely empty */
1892 while (!list_empty (&ep->urb_list)) {
1893 spin_lock_irq(&hcd_urb_list_lock);
1895 /* The list may have changed while we acquired the spinlock */
1896 urb = NULL;
1897 if (!list_empty (&ep->urb_list)) {
1898 urb = list_entry (ep->urb_list.prev, struct urb,
1899 urb_list);
1900 usb_get_urb (urb);
1902 spin_unlock_irq(&hcd_urb_list_lock);
1904 if (urb) {
1905 usb_kill_urb (urb);
1906 usb_put_urb (urb);
1912 * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
1913 * the bus bandwidth
1914 * @udev: target &usb_device
1915 * @new_config: new configuration to install
1916 * @cur_alt: the current alternate interface setting
1917 * @new_alt: alternate interface setting that is being installed
1919 * To change configurations, pass in the new configuration in new_config,
1920 * and pass NULL for cur_alt and new_alt.
1922 * To reset a device's configuration (put the device in the ADDRESSED state),
1923 * pass in NULL for new_config, cur_alt, and new_alt.
1925 * To change alternate interface settings, pass in NULL for new_config,
1926 * pass in the current alternate interface setting in cur_alt,
1927 * and pass in the new alternate interface setting in new_alt.
1929 * Return: An error if the requested bandwidth change exceeds the
1930 * bus bandwidth or host controller internal resources.
1932 int usb_hcd_alloc_bandwidth(struct usb_device *udev,
1933 struct usb_host_config *new_config,
1934 struct usb_host_interface *cur_alt,
1935 struct usb_host_interface *new_alt)
1937 int num_intfs, i, j;
1938 struct usb_host_interface *alt = NULL;
1939 int ret = 0;
1940 struct usb_hcd *hcd;
1941 struct usb_host_endpoint *ep;
1943 hcd = bus_to_hcd(udev->bus);
1944 if (!hcd->driver->check_bandwidth)
1945 return 0;
1947 /* Configuration is being removed - set configuration 0 */
1948 if (!new_config && !cur_alt) {
1949 for (i = 1; i < 16; ++i) {
1950 ep = udev->ep_out[i];
1951 if (ep)
1952 hcd->driver->drop_endpoint(hcd, udev, ep);
1953 ep = udev->ep_in[i];
1954 if (ep)
1955 hcd->driver->drop_endpoint(hcd, udev, ep);
1957 hcd->driver->check_bandwidth(hcd, udev);
1958 return 0;
1960 /* Check if the HCD says there's enough bandwidth. Enable all endpoints
1961 * each interface's alt setting 0 and ask the HCD to check the bandwidth
1962 * of the bus. There will always be bandwidth for endpoint 0, so it's
1963 * ok to exclude it.
1965 if (new_config) {
1966 num_intfs = new_config->desc.bNumInterfaces;
1967 /* Remove endpoints (except endpoint 0, which is always on the
1968 * schedule) from the old config from the schedule
1970 for (i = 1; i < 16; ++i) {
1971 ep = udev->ep_out[i];
1972 if (ep) {
1973 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1974 if (ret < 0)
1975 goto reset;
1977 ep = udev->ep_in[i];
1978 if (ep) {
1979 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1980 if (ret < 0)
1981 goto reset;
1984 for (i = 0; i < num_intfs; ++i) {
1985 struct usb_host_interface *first_alt;
1986 int iface_num;
1988 first_alt = &new_config->intf_cache[i]->altsetting[0];
1989 iface_num = first_alt->desc.bInterfaceNumber;
1990 /* Set up endpoints for alternate interface setting 0 */
1991 alt = usb_find_alt_setting(new_config, iface_num, 0);
1992 if (!alt)
1993 /* No alt setting 0? Pick the first setting. */
1994 alt = first_alt;
1996 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1997 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
1998 if (ret < 0)
1999 goto reset;
2003 if (cur_alt && new_alt) {
2004 struct usb_interface *iface = usb_ifnum_to_if(udev,
2005 cur_alt->desc.bInterfaceNumber);
2007 if (!iface)
2008 return -EINVAL;
2009 if (iface->resetting_device) {
2011 * The USB core just reset the device, so the xHCI host
2012 * and the device will think alt setting 0 is installed.
2013 * However, the USB core will pass in the alternate
2014 * setting installed before the reset as cur_alt. Dig
2015 * out the alternate setting 0 structure, or the first
2016 * alternate setting if a broken device doesn't have alt
2017 * setting 0.
2019 cur_alt = usb_altnum_to_altsetting(iface, 0);
2020 if (!cur_alt)
2021 cur_alt = &iface->altsetting[0];
2024 /* Drop all the endpoints in the current alt setting */
2025 for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
2026 ret = hcd->driver->drop_endpoint(hcd, udev,
2027 &cur_alt->endpoint[i]);
2028 if (ret < 0)
2029 goto reset;
2031 /* Add all the endpoints in the new alt setting */
2032 for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
2033 ret = hcd->driver->add_endpoint(hcd, udev,
2034 &new_alt->endpoint[i]);
2035 if (ret < 0)
2036 goto reset;
2039 ret = hcd->driver->check_bandwidth(hcd, udev);
2040 reset:
2041 if (ret < 0)
2042 hcd->driver->reset_bandwidth(hcd, udev);
2043 return ret;
2046 /* Disables the endpoint: synchronizes with the hcd to make sure all
2047 * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
2048 * have been called previously. Use for set_configuration, set_interface,
2049 * driver removal, physical disconnect.
2051 * example: a qh stored in ep->hcpriv, holding state related to endpoint
2052 * type, maxpacket size, toggle, halt status, and scheduling.
2054 void usb_hcd_disable_endpoint(struct usb_device *udev,
2055 struct usb_host_endpoint *ep)
2057 struct usb_hcd *hcd;
2059 might_sleep();
2060 hcd = bus_to_hcd(udev->bus);
2061 if (hcd->driver->endpoint_disable)
2062 hcd->driver->endpoint_disable(hcd, ep);
2066 * usb_hcd_reset_endpoint - reset host endpoint state
2067 * @udev: USB device.
2068 * @ep: the endpoint to reset.
2070 * Resets any host endpoint state such as the toggle bit, sequence
2071 * number and current window.
2073 void usb_hcd_reset_endpoint(struct usb_device *udev,
2074 struct usb_host_endpoint *ep)
2076 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2078 if (hcd->driver->endpoint_reset)
2079 hcd->driver->endpoint_reset(hcd, ep);
2080 else {
2081 int epnum = usb_endpoint_num(&ep->desc);
2082 int is_out = usb_endpoint_dir_out(&ep->desc);
2083 int is_control = usb_endpoint_xfer_control(&ep->desc);
2085 usb_settoggle(udev, epnum, is_out, 0);
2086 if (is_control)
2087 usb_settoggle(udev, epnum, !is_out, 0);
2092 * usb_alloc_streams - allocate bulk endpoint stream IDs.
2093 * @interface: alternate setting that includes all endpoints.
2094 * @eps: array of endpoints that need streams.
2095 * @num_eps: number of endpoints in the array.
2096 * @num_streams: number of streams to allocate.
2097 * @mem_flags: flags hcd should use to allocate memory.
2099 * Sets up a group of bulk endpoints to have @num_streams stream IDs available.
2100 * Drivers may queue multiple transfers to different stream IDs, which may
2101 * complete in a different order than they were queued.
2103 * Return: On success, the number of allocated streams. On failure, a negative
2104 * error code.
2106 int usb_alloc_streams(struct usb_interface *interface,
2107 struct usb_host_endpoint **eps, unsigned int num_eps,
2108 unsigned int num_streams, gfp_t mem_flags)
2110 struct usb_hcd *hcd;
2111 struct usb_device *dev;
2112 int i, ret;
2114 dev = interface_to_usbdev(interface);
2115 hcd = bus_to_hcd(dev->bus);
2116 if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
2117 return -EINVAL;
2118 if (dev->speed < USB_SPEED_SUPER)
2119 return -EINVAL;
2120 if (dev->state < USB_STATE_CONFIGURED)
2121 return -ENODEV;
2123 for (i = 0; i < num_eps; i++) {
2124 /* Streams only apply to bulk endpoints. */
2125 if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
2126 return -EINVAL;
2127 /* Re-alloc is not allowed */
2128 if (eps[i]->streams)
2129 return -EINVAL;
2132 ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
2133 num_streams, mem_flags);
2134 if (ret < 0)
2135 return ret;
2137 for (i = 0; i < num_eps; i++)
2138 eps[i]->streams = ret;
2140 return ret;
2142 EXPORT_SYMBOL_GPL(usb_alloc_streams);
2145 * usb_free_streams - free bulk endpoint stream IDs.
2146 * @interface: alternate setting that includes all endpoints.
2147 * @eps: array of endpoints to remove streams from.
2148 * @num_eps: number of endpoints in the array.
2149 * @mem_flags: flags hcd should use to allocate memory.
2151 * Reverts a group of bulk endpoints back to not using stream IDs.
2152 * Can fail if we are given bad arguments, or HCD is broken.
2154 * Return: 0 on success. On failure, a negative error code.
2156 int usb_free_streams(struct usb_interface *interface,
2157 struct usb_host_endpoint **eps, unsigned int num_eps,
2158 gfp_t mem_flags)
2160 struct usb_hcd *hcd;
2161 struct usb_device *dev;
2162 int i, ret;
2164 dev = interface_to_usbdev(interface);
2165 hcd = bus_to_hcd(dev->bus);
2166 if (dev->speed < USB_SPEED_SUPER)
2167 return -EINVAL;
2169 /* Double-free is not allowed */
2170 for (i = 0; i < num_eps; i++)
2171 if (!eps[i] || !eps[i]->streams)
2172 return -EINVAL;
2174 ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
2175 if (ret < 0)
2176 return ret;
2178 for (i = 0; i < num_eps; i++)
2179 eps[i]->streams = 0;
2181 return ret;
2183 EXPORT_SYMBOL_GPL(usb_free_streams);
2185 /* Protect against drivers that try to unlink URBs after the device
2186 * is gone, by waiting until all unlinks for @udev are finished.
2187 * Since we don't currently track URBs by device, simply wait until
2188 * nothing is running in the locked region of usb_hcd_unlink_urb().
2190 void usb_hcd_synchronize_unlinks(struct usb_device *udev)
2192 spin_lock_irq(&hcd_urb_unlink_lock);
2193 spin_unlock_irq(&hcd_urb_unlink_lock);
2196 /*-------------------------------------------------------------------------*/
2198 /* called in any context */
2199 int usb_hcd_get_frame_number (struct usb_device *udev)
2201 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2203 if (!HCD_RH_RUNNING(hcd))
2204 return -ESHUTDOWN;
2205 return hcd->driver->get_frame_number (hcd);
2208 /*-------------------------------------------------------------------------*/
2210 #ifdef CONFIG_PM
2212 int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
2214 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
2215 int status;
2216 int old_state = hcd->state;
2218 dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
2219 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2220 rhdev->do_remote_wakeup);
2221 if (HCD_DEAD(hcd)) {
2222 dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
2223 return 0;
2226 if (!hcd->driver->bus_suspend) {
2227 status = -ENOENT;
2228 } else {
2229 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2230 hcd->state = HC_STATE_QUIESCING;
2231 status = hcd->driver->bus_suspend(hcd);
2233 if (status == 0) {
2234 usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
2235 hcd->state = HC_STATE_SUSPENDED;
2237 /* Did we race with a root-hub wakeup event? */
2238 if (rhdev->do_remote_wakeup) {
2239 char buffer[6];
2241 status = hcd->driver->hub_status_data(hcd, buffer);
2242 if (status != 0) {
2243 dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n");
2244 hcd_bus_resume(rhdev, PMSG_AUTO_RESUME);
2245 status = -EBUSY;
2248 } else {
2249 spin_lock_irq(&hcd_root_hub_lock);
2250 if (!HCD_DEAD(hcd)) {
2251 set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2252 hcd->state = old_state;
2254 spin_unlock_irq(&hcd_root_hub_lock);
2255 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
2256 "suspend", status);
2258 return status;
2261 int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
2263 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
2264 int status;
2265 int old_state = hcd->state;
2267 dev_dbg(&rhdev->dev, "usb %sresume\n",
2268 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
2269 if (HCD_DEAD(hcd)) {
2270 dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
2271 return 0;
2273 if (!hcd->driver->bus_resume)
2274 return -ENOENT;
2275 if (HCD_RH_RUNNING(hcd))
2276 return 0;
2278 hcd->state = HC_STATE_RESUMING;
2279 status = hcd->driver->bus_resume(hcd);
2280 clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
2281 if (status == 0) {
2282 struct usb_device *udev;
2283 int port1;
2285 spin_lock_irq(&hcd_root_hub_lock);
2286 if (!HCD_DEAD(hcd)) {
2287 usb_set_device_state(rhdev, rhdev->actconfig
2288 ? USB_STATE_CONFIGURED
2289 : USB_STATE_ADDRESS);
2290 set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2291 hcd->state = HC_STATE_RUNNING;
2293 spin_unlock_irq(&hcd_root_hub_lock);
2296 * Check whether any of the enabled ports on the root hub are
2297 * unsuspended. If they are then a TRSMRCY delay is needed
2298 * (this is what the USB-2 spec calls a "global resume").
2299 * Otherwise we can skip the delay.
2301 usb_hub_for_each_child(rhdev, port1, udev) {
2302 if (udev->state != USB_STATE_NOTATTACHED &&
2303 !udev->port_is_suspended) {
2304 usleep_range(10000, 11000); /* TRSMRCY */
2305 break;
2308 } else {
2309 hcd->state = old_state;
2310 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
2311 "resume", status);
2312 if (status != -ESHUTDOWN)
2313 usb_hc_died(hcd);
2315 return status;
2318 /* Workqueue routine for root-hub remote wakeup */
2319 static void hcd_resume_work(struct work_struct *work)
2321 struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
2322 struct usb_device *udev = hcd->self.root_hub;
2324 usb_remote_wakeup(udev);
2328 * usb_hcd_resume_root_hub - called by HCD to resume its root hub
2329 * @hcd: host controller for this root hub
2331 * The USB host controller calls this function when its root hub is
2332 * suspended (with the remote wakeup feature enabled) and a remote
2333 * wakeup request is received. The routine submits a workqueue request
2334 * to resume the root hub (that is, manage its downstream ports again).
2336 void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
2338 unsigned long flags;
2340 spin_lock_irqsave (&hcd_root_hub_lock, flags);
2341 if (hcd->rh_registered) {
2342 pm_wakeup_event(&hcd->self.root_hub->dev, 0);
2343 set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
2344 queue_work(pm_wq, &hcd->wakeup_work);
2346 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2348 EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
2350 #endif /* CONFIG_PM */
2352 /*-------------------------------------------------------------------------*/
2354 #ifdef CONFIG_USB_OTG
2357 * usb_bus_start_enum - start immediate enumeration (for OTG)
2358 * @bus: the bus (must use hcd framework)
2359 * @port_num: 1-based number of port; usually bus->otg_port
2360 * Context: in_interrupt()
2362 * Starts enumeration, with an immediate reset followed later by
2363 * hub_wq identifying and possibly configuring the device.
2364 * This is needed by OTG controller drivers, where it helps meet
2365 * HNP protocol timing requirements for starting a port reset.
2367 * Return: 0 if successful.
2369 int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
2371 struct usb_hcd *hcd;
2372 int status = -EOPNOTSUPP;
2374 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
2375 * boards with root hubs hooked up to internal devices (instead of
2376 * just the OTG port) may need more attention to resetting...
2378 hcd = container_of (bus, struct usb_hcd, self);
2379 if (port_num && hcd->driver->start_port_reset)
2380 status = hcd->driver->start_port_reset(hcd, port_num);
2382 /* allocate hub_wq shortly after (first) root port reset finishes;
2383 * it may issue others, until at least 50 msecs have passed.
2385 if (status == 0)
2386 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
2387 return status;
2389 EXPORT_SYMBOL_GPL(usb_bus_start_enum);
2391 #endif
2393 /*-------------------------------------------------------------------------*/
2396 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
2397 * @irq: the IRQ being raised
2398 * @__hcd: pointer to the HCD whose IRQ is being signaled
2400 * If the controller isn't HALTed, calls the driver's irq handler.
2401 * Checks whether the controller is now dead.
2403 * Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise.
2405 irqreturn_t usb_hcd_irq (int irq, void *__hcd)
2407 struct usb_hcd *hcd = __hcd;
2408 irqreturn_t rc;
2410 if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd)))
2411 rc = IRQ_NONE;
2412 else if (hcd->driver->irq(hcd) == IRQ_NONE)
2413 rc = IRQ_NONE;
2414 else
2415 rc = IRQ_HANDLED;
2417 return rc;
2419 EXPORT_SYMBOL_GPL(usb_hcd_irq);
2421 /*-------------------------------------------------------------------------*/
2424 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
2425 * @hcd: pointer to the HCD representing the controller
2427 * This is called by bus glue to report a USB host controller that died
2428 * while operations may still have been pending. It's called automatically
2429 * by the PCI glue, so only glue for non-PCI busses should need to call it.
2431 * Only call this function with the primary HCD.
2433 void usb_hc_died (struct usb_hcd *hcd)
2435 unsigned long flags;
2437 dev_err (hcd->self.controller, "HC died; cleaning up\n");
2439 spin_lock_irqsave (&hcd_root_hub_lock, flags);
2440 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2441 set_bit(HCD_FLAG_DEAD, &hcd->flags);
2442 if (hcd->rh_registered) {
2443 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2445 /* make hub_wq clean up old urbs and devices */
2446 usb_set_device_state (hcd->self.root_hub,
2447 USB_STATE_NOTATTACHED);
2448 usb_kick_hub_wq(hcd->self.root_hub);
2450 if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
2451 hcd = hcd->shared_hcd;
2452 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2453 set_bit(HCD_FLAG_DEAD, &hcd->flags);
2454 if (hcd->rh_registered) {
2455 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2457 /* make hub_wq clean up old urbs and devices */
2458 usb_set_device_state(hcd->self.root_hub,
2459 USB_STATE_NOTATTACHED);
2460 usb_kick_hub_wq(hcd->self.root_hub);
2463 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2464 /* Make sure that the other roothub is also deallocated. */
2466 EXPORT_SYMBOL_GPL (usb_hc_died);
2468 /*-------------------------------------------------------------------------*/
2470 static void init_giveback_urb_bh(struct giveback_urb_bh *bh)
2473 spin_lock_init(&bh->lock);
2474 INIT_LIST_HEAD(&bh->head);
2475 tasklet_init(&bh->bh, usb_giveback_urb_bh, (unsigned long)bh);
2479 * usb_create_shared_hcd - create and initialize an HCD structure
2480 * @driver: HC driver that will use this hcd
2481 * @dev: device for this HC, stored in hcd->self.controller
2482 * @bus_name: value to store in hcd->self.bus_name
2483 * @primary_hcd: a pointer to the usb_hcd structure that is sharing the
2484 * PCI device. Only allocate certain resources for the primary HCD
2485 * Context: !in_interrupt()
2487 * Allocate a struct usb_hcd, with extra space at the end for the
2488 * HC driver's private data. Initialize the generic members of the
2489 * hcd structure.
2491 * Return: On success, a pointer to the created and initialized HCD structure.
2492 * On failure (e.g. if memory is unavailable), %NULL.
2494 struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
2495 struct device *dev, const char *bus_name,
2496 struct usb_hcd *primary_hcd)
2498 struct usb_hcd *hcd;
2500 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
2501 if (!hcd) {
2502 dev_dbg (dev, "hcd alloc failed\n");
2503 return NULL;
2505 if (primary_hcd == NULL) {
2506 hcd->address0_mutex = kmalloc(sizeof(*hcd->address0_mutex),
2507 GFP_KERNEL);
2508 if (!hcd->address0_mutex) {
2509 kfree(hcd);
2510 dev_dbg(dev, "hcd address0 mutex alloc failed\n");
2511 return NULL;
2513 mutex_init(hcd->address0_mutex);
2514 hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
2515 GFP_KERNEL);
2516 if (!hcd->bandwidth_mutex) {
2517 kfree(hcd->address0_mutex);
2518 kfree(hcd);
2519 dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
2520 return NULL;
2522 mutex_init(hcd->bandwidth_mutex);
2523 dev_set_drvdata(dev, hcd);
2524 } else {
2525 mutex_lock(&usb_port_peer_mutex);
2526 hcd->address0_mutex = primary_hcd->address0_mutex;
2527 hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
2528 hcd->primary_hcd = primary_hcd;
2529 primary_hcd->primary_hcd = primary_hcd;
2530 hcd->shared_hcd = primary_hcd;
2531 primary_hcd->shared_hcd = hcd;
2532 mutex_unlock(&usb_port_peer_mutex);
2535 kref_init(&hcd->kref);
2537 usb_bus_init(&hcd->self);
2538 hcd->self.controller = dev;
2539 hcd->self.bus_name = bus_name;
2540 hcd->self.uses_dma = (dev->dma_mask != NULL);
2542 init_timer(&hcd->rh_timer);
2543 hcd->rh_timer.function = rh_timer_func;
2544 hcd->rh_timer.data = (unsigned long) hcd;
2545 #ifdef CONFIG_PM
2546 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
2547 #endif
2549 hcd->driver = driver;
2550 hcd->speed = driver->flags & HCD_MASK;
2551 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
2552 "USB Host Controller";
2553 return hcd;
2555 EXPORT_SYMBOL_GPL(usb_create_shared_hcd);
2558 * usb_create_hcd - create and initialize an HCD structure
2559 * @driver: HC driver that will use this hcd
2560 * @dev: device for this HC, stored in hcd->self.controller
2561 * @bus_name: value to store in hcd->self.bus_name
2562 * Context: !in_interrupt()
2564 * Allocate a struct usb_hcd, with extra space at the end for the
2565 * HC driver's private data. Initialize the generic members of the
2566 * hcd structure.
2568 * Return: On success, a pointer to the created and initialized HCD
2569 * structure. On failure (e.g. if memory is unavailable), %NULL.
2571 struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
2572 struct device *dev, const char *bus_name)
2574 return usb_create_shared_hcd(driver, dev, bus_name, NULL);
2576 EXPORT_SYMBOL_GPL(usb_create_hcd);
2579 * Roothubs that share one PCI device must also share the bandwidth mutex.
2580 * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is
2581 * deallocated.
2583 * Make sure to deallocate the bandwidth_mutex only when the last HCD is
2584 * freed. When hcd_release() is called for either hcd in a peer set,
2585 * invalidate the peer's ->shared_hcd and ->primary_hcd pointers.
2587 static void hcd_release(struct kref *kref)
2589 struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
2591 mutex_lock(&usb_port_peer_mutex);
2592 if (hcd->shared_hcd) {
2593 struct usb_hcd *peer = hcd->shared_hcd;
2595 peer->shared_hcd = NULL;
2596 peer->primary_hcd = NULL;
2597 } else {
2598 kfree(hcd->address0_mutex);
2599 kfree(hcd->bandwidth_mutex);
2601 mutex_unlock(&usb_port_peer_mutex);
2602 kfree(hcd);
2605 struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
2607 if (hcd)
2608 kref_get (&hcd->kref);
2609 return hcd;
2611 EXPORT_SYMBOL_GPL(usb_get_hcd);
2613 void usb_put_hcd (struct usb_hcd *hcd)
2615 if (hcd)
2616 kref_put (&hcd->kref, hcd_release);
2618 EXPORT_SYMBOL_GPL(usb_put_hcd);
2620 int usb_hcd_is_primary_hcd(struct usb_hcd *hcd)
2622 if (!hcd->primary_hcd)
2623 return 1;
2624 return hcd == hcd->primary_hcd;
2626 EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd);
2628 int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1)
2630 if (!hcd->driver->find_raw_port_number)
2631 return port1;
2633 return hcd->driver->find_raw_port_number(hcd, port1);
2636 static int usb_hcd_request_irqs(struct usb_hcd *hcd,
2637 unsigned int irqnum, unsigned long irqflags)
2639 int retval;
2641 if (hcd->driver->irq) {
2643 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
2644 hcd->driver->description, hcd->self.busnum);
2645 retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
2646 hcd->irq_descr, hcd);
2647 if (retval != 0) {
2648 dev_err(hcd->self.controller,
2649 "request interrupt %d failed\n",
2650 irqnum);
2651 return retval;
2653 hcd->irq = irqnum;
2654 dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
2655 (hcd->driver->flags & HCD_MEMORY) ?
2656 "io mem" : "io base",
2657 (unsigned long long)hcd->rsrc_start);
2658 } else {
2659 hcd->irq = 0;
2660 if (hcd->rsrc_start)
2661 dev_info(hcd->self.controller, "%s 0x%08llx\n",
2662 (hcd->driver->flags & HCD_MEMORY) ?
2663 "io mem" : "io base",
2664 (unsigned long long)hcd->rsrc_start);
2666 return 0;
2670 * Before we free this root hub, flush in-flight peering attempts
2671 * and disable peer lookups
2673 static void usb_put_invalidate_rhdev(struct usb_hcd *hcd)
2675 struct usb_device *rhdev;
2677 mutex_lock(&usb_port_peer_mutex);
2678 rhdev = hcd->self.root_hub;
2679 hcd->self.root_hub = NULL;
2680 mutex_unlock(&usb_port_peer_mutex);
2681 usb_put_dev(rhdev);
2685 * usb_add_hcd - finish generic HCD structure initialization and register
2686 * @hcd: the usb_hcd structure to initialize
2687 * @irqnum: Interrupt line to allocate
2688 * @irqflags: Interrupt type flags
2690 * Finish the remaining parts of generic HCD initialization: allocate the
2691 * buffers of consistent memory, register the bus, request the IRQ line,
2692 * and call the driver's reset() and start() routines.
2694 int usb_add_hcd(struct usb_hcd *hcd,
2695 unsigned int irqnum, unsigned long irqflags)
2697 int retval;
2698 struct usb_device *rhdev;
2700 if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) {
2701 struct usb_phy *phy = usb_get_phy_dev(hcd->self.controller, 0);
2703 if (IS_ERR(phy)) {
2704 retval = PTR_ERR(phy);
2705 if (retval == -EPROBE_DEFER)
2706 return retval;
2707 } else {
2708 retval = usb_phy_init(phy);
2709 if (retval) {
2710 usb_put_phy(phy);
2711 return retval;
2713 hcd->usb_phy = phy;
2714 hcd->remove_phy = 1;
2718 if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) {
2719 struct phy *phy = phy_get(hcd->self.controller, "usb");
2721 if (IS_ERR(phy)) {
2722 retval = PTR_ERR(phy);
2723 if (retval == -EPROBE_DEFER)
2724 goto err_phy;
2725 } else {
2726 retval = phy_init(phy);
2727 if (retval) {
2728 phy_put(phy);
2729 goto err_phy;
2731 retval = phy_power_on(phy);
2732 if (retval) {
2733 phy_exit(phy);
2734 phy_put(phy);
2735 goto err_phy;
2737 hcd->phy = phy;
2738 hcd->remove_phy = 1;
2742 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
2744 /* Keep old behaviour if authorized_default is not in [0, 1]. */
2745 if (authorized_default < 0 || authorized_default > 1) {
2746 if (hcd->wireless)
2747 clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
2748 else
2749 set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
2750 } else {
2751 if (authorized_default)
2752 set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
2753 else
2754 clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
2756 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2758 /* per default all interfaces are authorized */
2759 set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
2761 /* HC is in reset state, but accessible. Now do the one-time init,
2762 * bottom up so that hcds can customize the root hubs before hub_wq
2763 * starts talking to them. (Note, bus id is assigned early too.)
2765 retval = hcd_buffer_create(hcd);
2766 if (retval != 0) {
2767 dev_dbg(hcd->self.controller, "pool alloc failed\n");
2768 goto err_create_buf;
2771 retval = usb_register_bus(&hcd->self);
2772 if (retval < 0)
2773 goto err_register_bus;
2775 rhdev = usb_alloc_dev(NULL, &hcd->self, 0);
2776 if (rhdev == NULL) {
2777 dev_err(hcd->self.controller, "unable to allocate root hub\n");
2778 retval = -ENOMEM;
2779 goto err_allocate_root_hub;
2781 mutex_lock(&usb_port_peer_mutex);
2782 hcd->self.root_hub = rhdev;
2783 mutex_unlock(&usb_port_peer_mutex);
2785 switch (hcd->speed) {
2786 case HCD_USB11:
2787 rhdev->speed = USB_SPEED_FULL;
2788 break;
2789 case HCD_USB2:
2790 rhdev->speed = USB_SPEED_HIGH;
2791 break;
2792 case HCD_USB25:
2793 rhdev->speed = USB_SPEED_WIRELESS;
2794 break;
2795 case HCD_USB3:
2796 case HCD_USB31:
2797 rhdev->speed = USB_SPEED_SUPER;
2798 break;
2799 default:
2800 retval = -EINVAL;
2801 goto err_set_rh_speed;
2804 /* wakeup flag init defaults to "everything works" for root hubs,
2805 * but drivers can override it in reset() if needed, along with
2806 * recording the overall controller's system wakeup capability.
2808 device_set_wakeup_capable(&rhdev->dev, 1);
2810 /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
2811 * registered. But since the controller can die at any time,
2812 * let's initialize the flag before touching the hardware.
2814 set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2816 /* "reset" is misnamed; its role is now one-time init. the controller
2817 * should already have been reset (and boot firmware kicked off etc).
2819 if (hcd->driver->reset) {
2820 retval = hcd->driver->reset(hcd);
2821 if (retval < 0) {
2822 dev_err(hcd->self.controller, "can't setup: %d\n",
2823 retval);
2824 goto err_hcd_driver_setup;
2827 hcd->rh_pollable = 1;
2829 /* NOTE: root hub and controller capabilities may not be the same */
2830 if (device_can_wakeup(hcd->self.controller)
2831 && device_can_wakeup(&hcd->self.root_hub->dev))
2832 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
2834 /* initialize tasklets */
2835 init_giveback_urb_bh(&hcd->high_prio_bh);
2836 init_giveback_urb_bh(&hcd->low_prio_bh);
2838 /* enable irqs just before we start the controller,
2839 * if the BIOS provides legacy PCI irqs.
2841 if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
2842 retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
2843 if (retval)
2844 goto err_request_irq;
2847 hcd->state = HC_STATE_RUNNING;
2848 retval = hcd->driver->start(hcd);
2849 if (retval < 0) {
2850 dev_err(hcd->self.controller, "startup error %d\n", retval);
2851 goto err_hcd_driver_start;
2854 /* starting here, usbcore will pay attention to this root hub */
2855 retval = register_root_hub(hcd);
2856 if (retval != 0)
2857 goto err_register_root_hub;
2859 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2860 if (retval < 0) {
2861 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
2862 retval);
2863 goto error_create_attr_group;
2865 if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
2866 usb_hcd_poll_rh_status(hcd);
2868 return retval;
2870 error_create_attr_group:
2871 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2872 if (HC_IS_RUNNING(hcd->state))
2873 hcd->state = HC_STATE_QUIESCING;
2874 spin_lock_irq(&hcd_root_hub_lock);
2875 hcd->rh_registered = 0;
2876 spin_unlock_irq(&hcd_root_hub_lock);
2878 #ifdef CONFIG_PM
2879 cancel_work_sync(&hcd->wakeup_work);
2880 #endif
2881 mutex_lock(&usb_bus_list_lock);
2882 usb_disconnect(&rhdev); /* Sets rhdev to NULL */
2883 mutex_unlock(&usb_bus_list_lock);
2884 err_register_root_hub:
2885 hcd->rh_pollable = 0;
2886 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2887 del_timer_sync(&hcd->rh_timer);
2888 hcd->driver->stop(hcd);
2889 hcd->state = HC_STATE_HALT;
2890 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2891 del_timer_sync(&hcd->rh_timer);
2892 err_hcd_driver_start:
2893 if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
2894 free_irq(irqnum, hcd);
2895 err_request_irq:
2896 err_hcd_driver_setup:
2897 err_set_rh_speed:
2898 usb_put_invalidate_rhdev(hcd);
2899 err_allocate_root_hub:
2900 usb_deregister_bus(&hcd->self);
2901 err_register_bus:
2902 hcd_buffer_destroy(hcd);
2903 err_create_buf:
2904 if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
2905 phy_power_off(hcd->phy);
2906 phy_exit(hcd->phy);
2907 phy_put(hcd->phy);
2908 hcd->phy = NULL;
2910 err_phy:
2911 if (hcd->remove_phy && hcd->usb_phy) {
2912 usb_phy_shutdown(hcd->usb_phy);
2913 usb_put_phy(hcd->usb_phy);
2914 hcd->usb_phy = NULL;
2916 return retval;
2918 EXPORT_SYMBOL_GPL(usb_add_hcd);
2921 * usb_remove_hcd - shutdown processing for generic HCDs
2922 * @hcd: the usb_hcd structure to remove
2923 * Context: !in_interrupt()
2925 * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
2926 * invoking the HCD's stop() method.
2928 void usb_remove_hcd(struct usb_hcd *hcd)
2930 struct usb_device *rhdev = hcd->self.root_hub;
2932 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
2934 usb_get_dev(rhdev);
2935 sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2937 clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2938 if (HC_IS_RUNNING (hcd->state))
2939 hcd->state = HC_STATE_QUIESCING;
2941 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
2942 spin_lock_irq (&hcd_root_hub_lock);
2943 hcd->rh_registered = 0;
2944 spin_unlock_irq (&hcd_root_hub_lock);
2946 #ifdef CONFIG_PM
2947 cancel_work_sync(&hcd->wakeup_work);
2948 #endif
2950 mutex_lock(&usb_bus_list_lock);
2951 usb_disconnect(&rhdev); /* Sets rhdev to NULL */
2952 mutex_unlock(&usb_bus_list_lock);
2955 * tasklet_kill() isn't needed here because:
2956 * - driver's disconnect() called from usb_disconnect() should
2957 * make sure its URBs are completed during the disconnect()
2958 * callback
2960 * - it is too late to run complete() here since driver may have
2961 * been removed already now
2964 /* Prevent any more root-hub status calls from the timer.
2965 * The HCD might still restart the timer (if a port status change
2966 * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke
2967 * the hub_status_data() callback.
2969 hcd->rh_pollable = 0;
2970 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2971 del_timer_sync(&hcd->rh_timer);
2973 hcd->driver->stop(hcd);
2974 hcd->state = HC_STATE_HALT;
2976 /* In case the HCD restarted the timer, stop it again. */
2977 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2978 del_timer_sync(&hcd->rh_timer);
2980 if (usb_hcd_is_primary_hcd(hcd)) {
2981 if (hcd->irq > 0)
2982 free_irq(hcd->irq, hcd);
2985 usb_deregister_bus(&hcd->self);
2986 hcd_buffer_destroy(hcd);
2988 if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) {
2989 phy_power_off(hcd->phy);
2990 phy_exit(hcd->phy);
2991 phy_put(hcd->phy);
2992 hcd->phy = NULL;
2994 if (hcd->remove_phy && hcd->usb_phy) {
2995 usb_phy_shutdown(hcd->usb_phy);
2996 usb_put_phy(hcd->usb_phy);
2997 hcd->usb_phy = NULL;
3000 usb_put_invalidate_rhdev(hcd);
3001 hcd->flags = 0;
3003 EXPORT_SYMBOL_GPL(usb_remove_hcd);
3005 void
3006 usb_hcd_platform_shutdown(struct platform_device *dev)
3008 struct usb_hcd *hcd = platform_get_drvdata(dev);
3010 if (hcd->driver->shutdown)
3011 hcd->driver->shutdown(hcd);
3013 EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
3015 /*-------------------------------------------------------------------------*/
3017 #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
3019 struct usb_mon_operations *mon_ops;
3022 * The registration is unlocked.
3023 * We do it this way because we do not want to lock in hot paths.
3025 * Notice that the code is minimally error-proof. Because usbmon needs
3026 * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
3029 int usb_mon_register (struct usb_mon_operations *ops)
3032 if (mon_ops)
3033 return -EBUSY;
3035 mon_ops = ops;
3036 mb();
3037 return 0;
3039 EXPORT_SYMBOL_GPL (usb_mon_register);
3041 void usb_mon_deregister (void)
3044 if (mon_ops == NULL) {
3045 printk(KERN_ERR "USB: monitor was not registered\n");
3046 return;
3048 mon_ops = NULL;
3049 mb();
3051 EXPORT_SYMBOL_GPL (usb_mon_deregister);
3053 #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */