OMAP3: PM: decouple PER and CORE context save and restore
[linux-ginger.git] / drivers / usb / core / hcd.c
blob34de475f016e790efbba02ada9067131870c294d
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
9 *
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/module.h>
26 #include <linux/version.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/completion.h>
30 #include <linux/utsname.h>
31 #include <linux/mm.h>
32 #include <asm/io.h>
33 #include <linux/device.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/mutex.h>
36 #include <asm/irq.h>
37 #include <asm/byteorder.h>
38 #include <asm/unaligned.h>
39 #include <linux/platform_device.h>
40 #include <linux/workqueue.h>
42 #include <linux/usb.h>
44 #include "usb.h"
45 #include "hcd.h"
46 #include "hub.h"
49 /*-------------------------------------------------------------------------*/
52 * USB Host Controller Driver framework
54 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
55 * HCD-specific behaviors/bugs.
57 * This does error checks, tracks devices and urbs, and delegates to a
58 * "hc_driver" only for code (and data) that really needs to know about
59 * hardware differences. That includes root hub registers, i/o queues,
60 * and so on ... but as little else as possible.
62 * Shared code includes most of the "root hub" code (these are emulated,
63 * though each HC's hardware works differently) and PCI glue, plus request
64 * tracking overhead. The HCD code should only block on spinlocks or on
65 * hardware handshaking; blocking on software events (such as other kernel
66 * threads releasing resources, or completing actions) is all generic.
68 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
69 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
70 * only by the hub driver ... and that neither should be seen or used by
71 * usb client device drivers.
73 * Contributors of ideas or unattributed patches include: David Brownell,
74 * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
76 * HISTORY:
77 * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
78 * associated cleanup. "usb_hcd" still != "usb_bus".
79 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
82 /*-------------------------------------------------------------------------*/
84 /* Keep track of which host controller drivers are loaded */
85 unsigned long usb_hcds_loaded;
86 EXPORT_SYMBOL_GPL(usb_hcds_loaded);
88 /* host controllers we manage */
89 LIST_HEAD (usb_bus_list);
90 EXPORT_SYMBOL_GPL (usb_bus_list);
92 /* used when allocating bus numbers */
93 #define USB_MAXBUS 64
94 struct usb_busmap {
95 unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
97 static struct usb_busmap busmap;
99 /* used when updating list of hcds */
100 DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
101 EXPORT_SYMBOL_GPL (usb_bus_list_lock);
103 /* used for controlling access to virtual root hubs */
104 static DEFINE_SPINLOCK(hcd_root_hub_lock);
106 /* used when updating an endpoint's URB list */
107 static DEFINE_SPINLOCK(hcd_urb_list_lock);
109 /* used to protect against unlinking URBs after the device is gone */
110 static DEFINE_SPINLOCK(hcd_urb_unlink_lock);
112 /* wait queue for synchronous unlinks */
113 DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
115 static inline int is_root_hub(struct usb_device *udev)
117 return (udev->parent == NULL);
120 /*-------------------------------------------------------------------------*/
123 * Sharable chunks of root hub code.
126 /*-------------------------------------------------------------------------*/
128 #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
129 #define KERNEL_VER ((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 0x01, /* __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 */
143 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
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.0 root hub device descriptor */
153 static const u8 usb2_rh_dev_descriptor [18] = {
154 0x12, /* __u8 bLength; */
155 0x01, /* __u8 bDescriptorType; Device */
156 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
158 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
159 0x00, /* __u8 bDeviceSubClass; */
160 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
161 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
163 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
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 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
175 /* usb 1.1 root hub device descriptor */
176 static const u8 usb11_rh_dev_descriptor [18] = {
177 0x12, /* __u8 bLength; */
178 0x01, /* __u8 bDescriptorType; Device */
179 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
181 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
182 0x00, /* __u8 bDeviceSubClass; */
183 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
184 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
186 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
187 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
188 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
190 0x03, /* __u8 iManufacturer; */
191 0x02, /* __u8 iProduct; */
192 0x01, /* __u8 iSerialNumber; */
193 0x01 /* __u8 bNumConfigurations; */
197 /*-------------------------------------------------------------------------*/
199 /* Configuration descriptors for our root hubs */
201 static const u8 fs_rh_config_descriptor [] = {
203 /* one configuration */
204 0x09, /* __u8 bLength; */
205 0x02, /* __u8 bDescriptorType; Configuration */
206 0x19, 0x00, /* __le16 wTotalLength; */
207 0x01, /* __u8 bNumInterfaces; (1) */
208 0x01, /* __u8 bConfigurationValue; */
209 0x00, /* __u8 iConfiguration; */
210 0xc0, /* __u8 bmAttributes;
211 Bit 7: must be set,
212 6: Self-powered,
213 5: Remote wakeup,
214 4..0: resvd */
215 0x00, /* __u8 MaxPower; */
217 /* USB 1.1:
218 * USB 2.0, single TT organization (mandatory):
219 * one interface, protocol 0
221 * USB 2.0, multiple TT organization (optional):
222 * two interfaces, protocols 1 (like single TT)
223 * and 2 (multiple TT mode) ... config is
224 * sometimes settable
225 * NOT IMPLEMENTED
228 /* one interface */
229 0x09, /* __u8 if_bLength; */
230 0x04, /* __u8 if_bDescriptorType; Interface */
231 0x00, /* __u8 if_bInterfaceNumber; */
232 0x00, /* __u8 if_bAlternateSetting; */
233 0x01, /* __u8 if_bNumEndpoints; */
234 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
235 0x00, /* __u8 if_bInterfaceSubClass; */
236 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
237 0x00, /* __u8 if_iInterface; */
239 /* one endpoint (status change endpoint) */
240 0x07, /* __u8 ep_bLength; */
241 0x05, /* __u8 ep_bDescriptorType; Endpoint */
242 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
243 0x03, /* __u8 ep_bmAttributes; Interrupt */
244 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
245 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
248 static const u8 hs_rh_config_descriptor [] = {
250 /* one configuration */
251 0x09, /* __u8 bLength; */
252 0x02, /* __u8 bDescriptorType; Configuration */
253 0x19, 0x00, /* __le16 wTotalLength; */
254 0x01, /* __u8 bNumInterfaces; (1) */
255 0x01, /* __u8 bConfigurationValue; */
256 0x00, /* __u8 iConfiguration; */
257 0xc0, /* __u8 bmAttributes;
258 Bit 7: must be set,
259 6: Self-powered,
260 5: Remote wakeup,
261 4..0: resvd */
262 0x00, /* __u8 MaxPower; */
264 /* USB 1.1:
265 * USB 2.0, single TT organization (mandatory):
266 * one interface, protocol 0
268 * USB 2.0, multiple TT organization (optional):
269 * two interfaces, protocols 1 (like single TT)
270 * and 2 (multiple TT mode) ... config is
271 * sometimes settable
272 * NOT IMPLEMENTED
275 /* one interface */
276 0x09, /* __u8 if_bLength; */
277 0x04, /* __u8 if_bDescriptorType; Interface */
278 0x00, /* __u8 if_bInterfaceNumber; */
279 0x00, /* __u8 if_bAlternateSetting; */
280 0x01, /* __u8 if_bNumEndpoints; */
281 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
282 0x00, /* __u8 if_bInterfaceSubClass; */
283 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
284 0x00, /* __u8 if_iInterface; */
286 /* one endpoint (status change endpoint) */
287 0x07, /* __u8 ep_bLength; */
288 0x05, /* __u8 ep_bDescriptorType; Endpoint */
289 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
290 0x03, /* __u8 ep_bmAttributes; Interrupt */
291 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
292 * see hub.c:hub_configure() for details. */
293 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
294 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
297 static const u8 ss_rh_config_descriptor[] = {
298 /* one configuration */
299 0x09, /* __u8 bLength; */
300 0x02, /* __u8 bDescriptorType; Configuration */
301 0x19, 0x00, /* __le16 wTotalLength; FIXME */
302 0x01, /* __u8 bNumInterfaces; (1) */
303 0x01, /* __u8 bConfigurationValue; */
304 0x00, /* __u8 iConfiguration; */
305 0xc0, /* __u8 bmAttributes;
306 Bit 7: must be set,
307 6: Self-powered,
308 5: Remote wakeup,
309 4..0: resvd */
310 0x00, /* __u8 MaxPower; */
312 /* one interface */
313 0x09, /* __u8 if_bLength; */
314 0x04, /* __u8 if_bDescriptorType; Interface */
315 0x00, /* __u8 if_bInterfaceNumber; */
316 0x00, /* __u8 if_bAlternateSetting; */
317 0x01, /* __u8 if_bNumEndpoints; */
318 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
319 0x00, /* __u8 if_bInterfaceSubClass; */
320 0x00, /* __u8 if_bInterfaceProtocol; */
321 0x00, /* __u8 if_iInterface; */
323 /* one endpoint (status change endpoint) */
324 0x07, /* __u8 ep_bLength; */
325 0x05, /* __u8 ep_bDescriptorType; Endpoint */
326 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
327 0x03, /* __u8 ep_bmAttributes; Interrupt */
328 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
329 * see hub.c:hub_configure() for details. */
330 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
331 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
333 * All 3.0 hubs should have an endpoint companion descriptor,
334 * but we're ignoring that for now. FIXME?
338 /*-------------------------------------------------------------------------*/
341 * ascii2desc() - Helper routine for producing UTF-16LE string descriptors
342 * @s: Null-terminated ASCII (actually ISO-8859-1) string
343 * @buf: Buffer for USB string descriptor (header + UTF-16LE)
344 * @len: Length (in bytes; may be odd) of descriptor buffer.
346 * The return value is the number of bytes filled in: 2 + 2*strlen(s) or
347 * buflen, whichever is less.
349 * USB String descriptors can contain at most 126 characters; input
350 * strings longer than that are truncated.
352 static unsigned
353 ascii2desc(char const *s, u8 *buf, unsigned len)
355 unsigned n, t = 2 + 2*strlen(s);
357 if (t > 254)
358 t = 254; /* Longest possible UTF string descriptor */
359 if (len > t)
360 len = t;
362 t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */
364 n = len;
365 while (n--) {
366 *buf++ = t;
367 if (!n--)
368 break;
369 *buf++ = t >> 8;
370 t = (unsigned char)*s++;
372 return len;
376 * rh_string() - provides string descriptors for root hub
377 * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
378 * @hcd: the host controller for this root hub
379 * @data: buffer for output packet
380 * @len: length of the provided buffer
382 * Produces either a manufacturer, product or serial number string for the
383 * virtual root hub device.
384 * Returns the number of bytes filled in: the length of the descriptor or
385 * of the provided buffer, whichever is less.
387 static unsigned
388 rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
390 char buf[100];
391 char const *s;
392 static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
394 // language ids
395 switch (id) {
396 case 0:
397 /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
398 /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
399 if (len > 4)
400 len = 4;
401 memcpy(data, langids, len);
402 return len;
403 case 1:
404 /* Serial number */
405 s = hcd->self.bus_name;
406 break;
407 case 2:
408 /* Product name */
409 s = hcd->product_desc;
410 break;
411 case 3:
412 /* Manufacturer */
413 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
414 init_utsname()->release, hcd->driver->description);
415 s = buf;
416 break;
417 default:
418 /* Can't happen; caller guarantees it */
419 return 0;
422 return ascii2desc(s, data, len);
426 /* Root hub control transfers execute synchronously */
427 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
429 struct usb_ctrlrequest *cmd;
430 u16 typeReq, wValue, wIndex, wLength;
431 u8 *ubuf = urb->transfer_buffer;
432 u8 tbuf [sizeof (struct usb_hub_descriptor)]
433 __attribute__((aligned(4)));
434 const u8 *bufp = tbuf;
435 unsigned len = 0;
436 int status;
437 u8 patch_wakeup = 0;
438 u8 patch_protocol = 0;
440 might_sleep();
442 spin_lock_irq(&hcd_root_hub_lock);
443 status = usb_hcd_link_urb_to_ep(hcd, urb);
444 spin_unlock_irq(&hcd_root_hub_lock);
445 if (status)
446 return status;
447 urb->hcpriv = hcd; /* Indicate it's queued */
449 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
450 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
451 wValue = le16_to_cpu (cmd->wValue);
452 wIndex = le16_to_cpu (cmd->wIndex);
453 wLength = le16_to_cpu (cmd->wLength);
455 if (wLength > urb->transfer_buffer_length)
456 goto error;
458 urb->actual_length = 0;
459 switch (typeReq) {
461 /* DEVICE REQUESTS */
463 /* The root hub's remote wakeup enable bit is implemented using
464 * driver model wakeup flags. If this system supports wakeup
465 * through USB, userspace may change the default "allow wakeup"
466 * policy through sysfs or these calls.
468 * Most root hubs support wakeup from downstream devices, for
469 * runtime power management (disabling USB clocks and reducing
470 * VBUS power usage). However, not all of them do so; silicon,
471 * board, and BIOS bugs here are not uncommon, so these can't
472 * be treated quite like external hubs.
474 * Likewise, not all root hubs will pass wakeup events upstream,
475 * to wake up the whole system. So don't assume root hub and
476 * controller capabilities are identical.
479 case DeviceRequest | USB_REQ_GET_STATUS:
480 tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
481 << USB_DEVICE_REMOTE_WAKEUP)
482 | (1 << USB_DEVICE_SELF_POWERED);
483 tbuf [1] = 0;
484 len = 2;
485 break;
486 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
487 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
488 device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
489 else
490 goto error;
491 break;
492 case DeviceOutRequest | USB_REQ_SET_FEATURE:
493 if (device_can_wakeup(&hcd->self.root_hub->dev)
494 && wValue == USB_DEVICE_REMOTE_WAKEUP)
495 device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
496 else
497 goto error;
498 break;
499 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
500 tbuf [0] = 1;
501 len = 1;
502 /* FALLTHROUGH */
503 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
504 break;
505 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
506 switch (wValue & 0xff00) {
507 case USB_DT_DEVICE << 8:
508 switch (hcd->driver->flags & HCD_MASK) {
509 case HCD_USB3:
510 bufp = usb3_rh_dev_descriptor;
511 break;
512 case HCD_USB2:
513 bufp = usb2_rh_dev_descriptor;
514 break;
515 case HCD_USB11:
516 bufp = usb11_rh_dev_descriptor;
517 break;
518 default:
519 goto error;
521 len = 18;
522 if (hcd->has_tt)
523 patch_protocol = 1;
524 break;
525 case USB_DT_CONFIG << 8:
526 switch (hcd->driver->flags & HCD_MASK) {
527 case HCD_USB3:
528 bufp = ss_rh_config_descriptor;
529 len = sizeof ss_rh_config_descriptor;
530 break;
531 case HCD_USB2:
532 bufp = hs_rh_config_descriptor;
533 len = sizeof hs_rh_config_descriptor;
534 break;
535 case HCD_USB11:
536 bufp = fs_rh_config_descriptor;
537 len = sizeof fs_rh_config_descriptor;
538 break;
539 default:
540 goto error;
542 if (device_can_wakeup(&hcd->self.root_hub->dev))
543 patch_wakeup = 1;
544 break;
545 case USB_DT_STRING << 8:
546 if ((wValue & 0xff) < 4)
547 urb->actual_length = rh_string(wValue & 0xff,
548 hcd, ubuf, wLength);
549 else /* unsupported IDs --> "protocol stall" */
550 goto error;
551 break;
552 default:
553 goto error;
555 break;
556 case DeviceRequest | USB_REQ_GET_INTERFACE:
557 tbuf [0] = 0;
558 len = 1;
559 /* FALLTHROUGH */
560 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
561 break;
562 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
563 // wValue == urb->dev->devaddr
564 dev_dbg (hcd->self.controller, "root hub device address %d\n",
565 wValue);
566 break;
568 /* INTERFACE REQUESTS (no defined feature/status flags) */
570 /* ENDPOINT REQUESTS */
572 case EndpointRequest | USB_REQ_GET_STATUS:
573 // ENDPOINT_HALT flag
574 tbuf [0] = 0;
575 tbuf [1] = 0;
576 len = 2;
577 /* FALLTHROUGH */
578 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
579 case EndpointOutRequest | USB_REQ_SET_FEATURE:
580 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
581 break;
583 /* CLASS REQUESTS (and errors) */
585 default:
586 /* non-generic request */
587 switch (typeReq) {
588 case GetHubStatus:
589 case GetPortStatus:
590 len = 4;
591 break;
592 case GetHubDescriptor:
593 len = sizeof (struct usb_hub_descriptor);
594 break;
596 status = hcd->driver->hub_control (hcd,
597 typeReq, wValue, wIndex,
598 tbuf, wLength);
599 break;
600 error:
601 /* "protocol stall" on error */
602 status = -EPIPE;
605 if (status) {
606 len = 0;
607 if (status != -EPIPE) {
608 dev_dbg (hcd->self.controller,
609 "CTRL: TypeReq=0x%x val=0x%x "
610 "idx=0x%x len=%d ==> %d\n",
611 typeReq, wValue, wIndex,
612 wLength, status);
615 if (len) {
616 if (urb->transfer_buffer_length < len)
617 len = urb->transfer_buffer_length;
618 urb->actual_length = len;
619 // always USB_DIR_IN, toward host
620 memcpy (ubuf, bufp, len);
622 /* report whether RH hardware supports remote wakeup */
623 if (patch_wakeup &&
624 len > offsetof (struct usb_config_descriptor,
625 bmAttributes))
626 ((struct usb_config_descriptor *)ubuf)->bmAttributes
627 |= USB_CONFIG_ATT_WAKEUP;
629 /* report whether RH hardware has an integrated TT */
630 if (patch_protocol &&
631 len > offsetof(struct usb_device_descriptor,
632 bDeviceProtocol))
633 ((struct usb_device_descriptor *) ubuf)->
634 bDeviceProtocol = 1;
637 /* any errors get returned through the urb completion */
638 spin_lock_irq(&hcd_root_hub_lock);
639 usb_hcd_unlink_urb_from_ep(hcd, urb);
641 /* This peculiar use of spinlocks echoes what real HC drivers do.
642 * Avoiding calls to local_irq_disable/enable makes the code
643 * RT-friendly.
645 spin_unlock(&hcd_root_hub_lock);
646 usb_hcd_giveback_urb(hcd, urb, status);
647 spin_lock(&hcd_root_hub_lock);
649 spin_unlock_irq(&hcd_root_hub_lock);
650 return 0;
653 /*-------------------------------------------------------------------------*/
656 * Root Hub interrupt transfers are polled using a timer if the
657 * driver requests it; otherwise the driver is responsible for
658 * calling usb_hcd_poll_rh_status() when an event occurs.
660 * Completions are called in_interrupt(), but they may or may not
661 * be in_irq().
663 void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
665 struct urb *urb;
666 int length;
667 unsigned long flags;
668 char buffer[6]; /* Any root hubs with > 31 ports? */
670 if (unlikely(!hcd->rh_registered))
671 return;
672 if (!hcd->uses_new_polling && !hcd->status_urb)
673 return;
675 length = hcd->driver->hub_status_data(hcd, buffer);
676 if (length > 0) {
678 /* try to complete the status urb */
679 spin_lock_irqsave(&hcd_root_hub_lock, flags);
680 urb = hcd->status_urb;
681 if (urb) {
682 hcd->poll_pending = 0;
683 hcd->status_urb = NULL;
684 urb->actual_length = length;
685 memcpy(urb->transfer_buffer, buffer, length);
687 usb_hcd_unlink_urb_from_ep(hcd, urb);
688 spin_unlock(&hcd_root_hub_lock);
689 usb_hcd_giveback_urb(hcd, urb, 0);
690 spin_lock(&hcd_root_hub_lock);
691 } else {
692 length = 0;
693 hcd->poll_pending = 1;
695 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
698 /* The USB 2.0 spec says 256 ms. This is close enough and won't
699 * exceed that limit if HZ is 100. The math is more clunky than
700 * maybe expected, this is to make sure that all timers for USB devices
701 * fire at the same time to give the CPU a break inbetween */
702 if (hcd->uses_new_polling ? hcd->poll_rh :
703 (length == 0 && hcd->status_urb != NULL))
704 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
706 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
708 /* timer callback */
709 static void rh_timer_func (unsigned long _hcd)
711 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
714 /*-------------------------------------------------------------------------*/
716 static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
718 int retval;
719 unsigned long flags;
720 unsigned len = 1 + (urb->dev->maxchild / 8);
722 spin_lock_irqsave (&hcd_root_hub_lock, flags);
723 if (hcd->status_urb || urb->transfer_buffer_length < len) {
724 dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
725 retval = -EINVAL;
726 goto done;
729 retval = usb_hcd_link_urb_to_ep(hcd, urb);
730 if (retval)
731 goto done;
733 hcd->status_urb = urb;
734 urb->hcpriv = hcd; /* indicate it's queued */
735 if (!hcd->uses_new_polling)
736 mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
738 /* If a status change has already occurred, report it ASAP */
739 else if (hcd->poll_pending)
740 mod_timer(&hcd->rh_timer, jiffies);
741 retval = 0;
742 done:
743 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
744 return retval;
747 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
749 if (usb_endpoint_xfer_int(&urb->ep->desc))
750 return rh_queue_status (hcd, urb);
751 if (usb_endpoint_xfer_control(&urb->ep->desc))
752 return rh_call_control (hcd, urb);
753 return -EINVAL;
756 /*-------------------------------------------------------------------------*/
758 /* Unlinks of root-hub control URBs are legal, but they don't do anything
759 * since these URBs always execute synchronously.
761 static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
763 unsigned long flags;
764 int rc;
766 spin_lock_irqsave(&hcd_root_hub_lock, flags);
767 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
768 if (rc)
769 goto done;
771 if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
772 ; /* Do nothing */
774 } else { /* Status URB */
775 if (!hcd->uses_new_polling)
776 del_timer (&hcd->rh_timer);
777 if (urb == hcd->status_urb) {
778 hcd->status_urb = NULL;
779 usb_hcd_unlink_urb_from_ep(hcd, urb);
781 spin_unlock(&hcd_root_hub_lock);
782 usb_hcd_giveback_urb(hcd, urb, status);
783 spin_lock(&hcd_root_hub_lock);
786 done:
787 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
788 return rc;
794 * Show & store the current value of authorized_default
796 static ssize_t usb_host_authorized_default_show(struct device *dev,
797 struct device_attribute *attr,
798 char *buf)
800 struct usb_device *rh_usb_dev = to_usb_device(dev);
801 struct usb_bus *usb_bus = rh_usb_dev->bus;
802 struct usb_hcd *usb_hcd;
804 if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
805 return -ENODEV;
806 usb_hcd = bus_to_hcd(usb_bus);
807 return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default);
810 static ssize_t usb_host_authorized_default_store(struct device *dev,
811 struct device_attribute *attr,
812 const char *buf, size_t size)
814 ssize_t result;
815 unsigned val;
816 struct usb_device *rh_usb_dev = to_usb_device(dev);
817 struct usb_bus *usb_bus = rh_usb_dev->bus;
818 struct usb_hcd *usb_hcd;
820 if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
821 return -ENODEV;
822 usb_hcd = bus_to_hcd(usb_bus);
823 result = sscanf(buf, "%u\n", &val);
824 if (result == 1) {
825 usb_hcd->authorized_default = val? 1 : 0;
826 result = size;
828 else
829 result = -EINVAL;
830 return result;
833 static DEVICE_ATTR(authorized_default, 0644,
834 usb_host_authorized_default_show,
835 usb_host_authorized_default_store);
838 /* Group all the USB bus attributes */
839 static struct attribute *usb_bus_attrs[] = {
840 &dev_attr_authorized_default.attr,
841 NULL,
844 static struct attribute_group usb_bus_attr_group = {
845 .name = NULL, /* we want them in the same directory */
846 .attrs = usb_bus_attrs,
851 /*-------------------------------------------------------------------------*/
854 * usb_bus_init - shared initialization code
855 * @bus: the bus structure being initialized
857 * This code is used to initialize a usb_bus structure, memory for which is
858 * separately managed.
860 static void usb_bus_init (struct usb_bus *bus)
862 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
864 bus->devnum_next = 1;
866 bus->root_hub = NULL;
867 bus->busnum = -1;
868 bus->bandwidth_allocated = 0;
869 bus->bandwidth_int_reqs = 0;
870 bus->bandwidth_isoc_reqs = 0;
872 INIT_LIST_HEAD (&bus->bus_list);
875 /*-------------------------------------------------------------------------*/
878 * usb_register_bus - registers the USB host controller with the usb core
879 * @bus: pointer to the bus to register
880 * Context: !in_interrupt()
882 * Assigns a bus number, and links the controller into usbcore data
883 * structures so that it can be seen by scanning the bus list.
885 static int usb_register_bus(struct usb_bus *bus)
887 int result = -E2BIG;
888 int busnum;
890 mutex_lock(&usb_bus_list_lock);
891 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
892 if (busnum >= USB_MAXBUS) {
893 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
894 goto error_find_busnum;
896 set_bit (busnum, busmap.busmap);
897 bus->busnum = busnum;
899 /* Add it to the local list of buses */
900 list_add (&bus->bus_list, &usb_bus_list);
901 mutex_unlock(&usb_bus_list_lock);
903 usb_notify_add_bus(bus);
905 dev_info (bus->controller, "new USB bus registered, assigned bus "
906 "number %d\n", bus->busnum);
907 return 0;
909 error_find_busnum:
910 mutex_unlock(&usb_bus_list_lock);
911 return result;
915 * usb_deregister_bus - deregisters the USB host controller
916 * @bus: pointer to the bus to deregister
917 * Context: !in_interrupt()
919 * Recycles the bus number, and unlinks the controller from usbcore data
920 * structures so that it won't be seen by scanning the bus list.
922 static void usb_deregister_bus (struct usb_bus *bus)
924 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
927 * NOTE: make sure that all the devices are removed by the
928 * controller code, as well as having it call this when cleaning
929 * itself up
931 mutex_lock(&usb_bus_list_lock);
932 list_del (&bus->bus_list);
933 mutex_unlock(&usb_bus_list_lock);
935 usb_notify_remove_bus(bus);
937 clear_bit (bus->busnum, busmap.busmap);
941 * register_root_hub - called by usb_add_hcd() to register a root hub
942 * @hcd: host controller for this root hub
944 * This function registers the root hub with the USB subsystem. It sets up
945 * the device properly in the device tree and then calls usb_new_device()
946 * to register the usb device. It also assigns the root hub's USB address
947 * (always 1).
949 static int register_root_hub(struct usb_hcd *hcd)
951 struct device *parent_dev = hcd->self.controller;
952 struct usb_device *usb_dev = hcd->self.root_hub;
953 const int devnum = 1;
954 int retval;
956 usb_dev->devnum = devnum;
957 usb_dev->bus->devnum_next = devnum + 1;
958 memset (&usb_dev->bus->devmap.devicemap, 0,
959 sizeof usb_dev->bus->devmap.devicemap);
960 set_bit (devnum, usb_dev->bus->devmap.devicemap);
961 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
963 mutex_lock(&usb_bus_list_lock);
965 usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
966 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
967 if (retval != sizeof usb_dev->descriptor) {
968 mutex_unlock(&usb_bus_list_lock);
969 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
970 dev_name(&usb_dev->dev), retval);
971 return (retval < 0) ? retval : -EMSGSIZE;
974 retval = usb_new_device (usb_dev);
975 if (retval) {
976 dev_err (parent_dev, "can't register root hub for %s, %d\n",
977 dev_name(&usb_dev->dev), retval);
979 mutex_unlock(&usb_bus_list_lock);
981 if (retval == 0) {
982 spin_lock_irq (&hcd_root_hub_lock);
983 hcd->rh_registered = 1;
984 spin_unlock_irq (&hcd_root_hub_lock);
986 /* Did the HC die before the root hub was registered? */
987 if (hcd->state == HC_STATE_HALT)
988 usb_hc_died (hcd); /* This time clean up */
991 return retval;
995 /*-------------------------------------------------------------------------*/
998 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
999 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
1000 * @is_input: true iff the transaction sends data to the host
1001 * @isoc: true for isochronous transactions, false for interrupt ones
1002 * @bytecount: how many bytes in the transaction.
1004 * Returns approximate bus time in nanoseconds for a periodic transaction.
1005 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
1006 * scheduled in software, this function is only used for such scheduling.
1008 long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
1010 unsigned long tmp;
1012 switch (speed) {
1013 case USB_SPEED_LOW: /* INTR only */
1014 if (is_input) {
1015 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
1016 return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
1017 } else {
1018 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
1019 return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
1021 case USB_SPEED_FULL: /* ISOC or INTR */
1022 if (isoc) {
1023 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1024 return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
1025 } else {
1026 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1027 return (9107L + BW_HOST_DELAY + tmp);
1029 case USB_SPEED_HIGH: /* ISOC or INTR */
1030 // FIXME adjust for input vs output
1031 if (isoc)
1032 tmp = HS_NSECS_ISO (bytecount);
1033 else
1034 tmp = HS_NSECS (bytecount);
1035 return tmp;
1036 default:
1037 pr_debug ("%s: bogus device speed!\n", usbcore_name);
1038 return -1;
1041 EXPORT_SYMBOL_GPL(usb_calc_bus_time);
1044 /*-------------------------------------------------------------------------*/
1047 * Generic HC operations.
1050 /*-------------------------------------------------------------------------*/
1053 * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
1054 * @hcd: host controller to which @urb was submitted
1055 * @urb: URB being submitted
1057 * Host controller drivers should call this routine in their enqueue()
1058 * method. The HCD's private spinlock must be held and interrupts must
1059 * be disabled. The actions carried out here are required for URB
1060 * submission, as well as for endpoint shutdown and for usb_kill_urb.
1062 * Returns 0 for no error, otherwise a negative error code (in which case
1063 * the enqueue() method must fail). If no error occurs but enqueue() fails
1064 * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
1065 * the private spinlock and returning.
1067 int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
1069 int rc = 0;
1071 spin_lock(&hcd_urb_list_lock);
1073 /* Check that the URB isn't being killed */
1074 if (unlikely(atomic_read(&urb->reject))) {
1075 rc = -EPERM;
1076 goto done;
1079 if (unlikely(!urb->ep->enabled)) {
1080 rc = -ENOENT;
1081 goto done;
1084 if (unlikely(!urb->dev->can_submit)) {
1085 rc = -EHOSTUNREACH;
1086 goto done;
1090 * Check the host controller's state and add the URB to the
1091 * endpoint's queue.
1093 switch (hcd->state) {
1094 case HC_STATE_RUNNING:
1095 case HC_STATE_RESUMING:
1096 urb->unlinked = 0;
1097 list_add_tail(&urb->urb_list, &urb->ep->urb_list);
1098 break;
1099 default:
1100 rc = -ESHUTDOWN;
1101 goto done;
1103 done:
1104 spin_unlock(&hcd_urb_list_lock);
1105 return rc;
1107 EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
1110 * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
1111 * @hcd: host controller to which @urb was submitted
1112 * @urb: URB being checked for unlinkability
1113 * @status: error code to store in @urb if the unlink succeeds
1115 * Host controller drivers should call this routine in their dequeue()
1116 * method. The HCD's private spinlock must be held and interrupts must
1117 * be disabled. The actions carried out here are required for making
1118 * sure than an unlink is valid.
1120 * Returns 0 for no error, otherwise a negative error code (in which case
1121 * the dequeue() method must fail). The possible error codes are:
1123 * -EIDRM: @urb was not submitted or has already completed.
1124 * The completion function may not have been called yet.
1126 * -EBUSY: @urb has already been unlinked.
1128 int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
1129 int status)
1131 struct list_head *tmp;
1133 /* insist the urb is still queued */
1134 list_for_each(tmp, &urb->ep->urb_list) {
1135 if (tmp == &urb->urb_list)
1136 break;
1138 if (tmp != &urb->urb_list)
1139 return -EIDRM;
1141 /* Any status except -EINPROGRESS means something already started to
1142 * unlink this URB from the hardware. So there's no more work to do.
1144 if (urb->unlinked)
1145 return -EBUSY;
1146 urb->unlinked = status;
1148 /* IRQ setup can easily be broken so that USB controllers
1149 * never get completion IRQs ... maybe even the ones we need to
1150 * finish unlinking the initial failed usb_set_address()
1151 * or device descriptor fetch.
1153 if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) &&
1154 !is_root_hub(urb->dev)) {
1155 dev_warn(hcd->self.controller, "Unlink after no-IRQ? "
1156 "Controller is probably using the wrong IRQ.\n");
1157 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
1160 return 0;
1162 EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
1165 * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
1166 * @hcd: host controller to which @urb was submitted
1167 * @urb: URB being unlinked
1169 * Host controller drivers should call this routine before calling
1170 * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
1171 * interrupts must be disabled. The actions carried out here are required
1172 * for URB completion.
1174 void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
1176 /* clear all state linking urb to this dev (and hcd) */
1177 spin_lock(&hcd_urb_list_lock);
1178 list_del_init(&urb->urb_list);
1179 spin_unlock(&hcd_urb_list_lock);
1181 EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
1184 * Some usb host controllers can only perform dma using a small SRAM area.
1185 * The usb core itself is however optimized for host controllers that can dma
1186 * using regular system memory - like pci devices doing bus mastering.
1188 * To support host controllers with limited dma capabilites we provide dma
1189 * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
1190 * For this to work properly the host controller code must first use the
1191 * function dma_declare_coherent_memory() to point out which memory area
1192 * that should be used for dma allocations.
1194 * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
1195 * dma using dma_alloc_coherent() which in turn allocates from the memory
1196 * area pointed out with dma_declare_coherent_memory().
1198 * So, to summarize...
1200 * - We need "local" memory, canonical example being
1201 * a small SRAM on a discrete controller being the
1202 * only memory that the controller can read ...
1203 * (a) "normal" kernel memory is no good, and
1204 * (b) there's not enough to share
1206 * - The only *portable* hook for such stuff in the
1207 * DMA framework is dma_declare_coherent_memory()
1209 * - So we use that, even though the primary requirement
1210 * is that the memory be "local" (hence addressible
1211 * by that device), not "coherent".
1215 static int hcd_alloc_coherent(struct usb_bus *bus,
1216 gfp_t mem_flags, dma_addr_t *dma_handle,
1217 void **vaddr_handle, size_t size,
1218 enum dma_data_direction dir)
1220 unsigned char *vaddr;
1222 vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
1223 mem_flags, dma_handle);
1224 if (!vaddr)
1225 return -ENOMEM;
1228 * Store the virtual address of the buffer at the end
1229 * of the allocated dma buffer. The size of the buffer
1230 * may be uneven so use unaligned functions instead
1231 * of just rounding up. It makes sense to optimize for
1232 * memory footprint over access speed since the amount
1233 * of memory available for dma may be limited.
1235 put_unaligned((unsigned long)*vaddr_handle,
1236 (unsigned long *)(vaddr + size));
1238 if (dir == DMA_TO_DEVICE)
1239 memcpy(vaddr, *vaddr_handle, size);
1241 *vaddr_handle = vaddr;
1242 return 0;
1245 static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
1246 void **vaddr_handle, size_t size,
1247 enum dma_data_direction dir)
1249 unsigned char *vaddr = *vaddr_handle;
1251 vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
1253 if (dir == DMA_FROM_DEVICE)
1254 memcpy(vaddr, *vaddr_handle, size);
1256 hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
1258 *vaddr_handle = vaddr;
1259 *dma_handle = 0;
1262 static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1263 gfp_t mem_flags)
1265 enum dma_data_direction dir;
1266 int ret = 0;
1268 /* Map the URB's buffers for DMA access.
1269 * Lower level HCD code should use *_dma exclusively,
1270 * unless it uses pio or talks to another transport,
1271 * or uses the provided scatter gather list for bulk.
1273 if (is_root_hub(urb->dev))
1274 return 0;
1276 if (usb_endpoint_xfer_control(&urb->ep->desc)
1277 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
1278 if (hcd->self.uses_dma)
1279 urb->setup_dma = dma_map_single(
1280 hcd->self.controller,
1281 urb->setup_packet,
1282 sizeof(struct usb_ctrlrequest),
1283 DMA_TO_DEVICE);
1284 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1285 ret = hcd_alloc_coherent(
1286 urb->dev->bus, mem_flags,
1287 &urb->setup_dma,
1288 (void **)&urb->setup_packet,
1289 sizeof(struct usb_ctrlrequest),
1290 DMA_TO_DEVICE);
1293 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1294 if (ret == 0 && urb->transfer_buffer_length != 0
1295 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1296 if (hcd->self.uses_dma)
1297 urb->transfer_dma = dma_map_single (
1298 hcd->self.controller,
1299 urb->transfer_buffer,
1300 urb->transfer_buffer_length,
1301 dir);
1302 else if (hcd->driver->flags & HCD_LOCAL_MEM) {
1303 ret = hcd_alloc_coherent(
1304 urb->dev->bus, mem_flags,
1305 &urb->transfer_dma,
1306 &urb->transfer_buffer,
1307 urb->transfer_buffer_length,
1308 dir);
1310 if (ret && usb_endpoint_xfer_control(&urb->ep->desc)
1311 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1312 hcd_free_coherent(urb->dev->bus,
1313 &urb->setup_dma,
1314 (void **)&urb->setup_packet,
1315 sizeof(struct usb_ctrlrequest),
1316 DMA_TO_DEVICE);
1319 return ret;
1322 static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1324 enum dma_data_direction dir;
1326 if (is_root_hub(urb->dev))
1327 return;
1329 if (usb_endpoint_xfer_control(&urb->ep->desc)
1330 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
1331 if (hcd->self.uses_dma)
1332 dma_unmap_single(hcd->self.controller, urb->setup_dma,
1333 sizeof(struct usb_ctrlrequest),
1334 DMA_TO_DEVICE);
1335 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1336 hcd_free_coherent(urb->dev->bus, &urb->setup_dma,
1337 (void **)&urb->setup_packet,
1338 sizeof(struct usb_ctrlrequest),
1339 DMA_TO_DEVICE);
1342 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1343 if (urb->transfer_buffer_length != 0
1344 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1345 if (hcd->self.uses_dma)
1346 dma_unmap_single(hcd->self.controller,
1347 urb->transfer_dma,
1348 urb->transfer_buffer_length,
1349 dir);
1350 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1351 hcd_free_coherent(urb->dev->bus, &urb->transfer_dma,
1352 &urb->transfer_buffer,
1353 urb->transfer_buffer_length,
1354 dir);
1358 /*-------------------------------------------------------------------------*/
1360 /* may be called in any context with a valid urb->dev usecount
1361 * caller surrenders "ownership" of urb
1362 * expects usb_submit_urb() to have sanity checked and conditioned all
1363 * inputs in the urb
1365 int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1367 int status;
1368 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1370 /* increment urb's reference count as part of giving it to the HCD
1371 * (which will control it). HCD guarantees that it either returns
1372 * an error or calls giveback(), but not both.
1374 usb_get_urb(urb);
1375 atomic_inc(&urb->use_count);
1376 atomic_inc(&urb->dev->urbnum);
1377 usbmon_urb_submit(&hcd->self, urb);
1379 /* NOTE requirements on root-hub callers (usbfs and the hub
1380 * driver, for now): URBs' urb->transfer_buffer must be
1381 * valid and usb_buffer_{sync,unmap}() not be needed, since
1382 * they could clobber root hub response data. Also, control
1383 * URBs must be submitted in process context with interrupts
1384 * enabled.
1386 status = map_urb_for_dma(hcd, urb, mem_flags);
1387 if (unlikely(status)) {
1388 usbmon_urb_submit_error(&hcd->self, urb, status);
1389 goto error;
1392 if (is_root_hub(urb->dev))
1393 status = rh_urb_enqueue(hcd, urb);
1394 else
1395 status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
1397 if (unlikely(status)) {
1398 usbmon_urb_submit_error(&hcd->self, urb, status);
1399 unmap_urb_for_dma(hcd, urb);
1400 error:
1401 urb->hcpriv = NULL;
1402 INIT_LIST_HEAD(&urb->urb_list);
1403 atomic_dec(&urb->use_count);
1404 atomic_dec(&urb->dev->urbnum);
1405 if (atomic_read(&urb->reject))
1406 wake_up(&usb_kill_urb_queue);
1407 usb_put_urb(urb);
1409 return status;
1412 /*-------------------------------------------------------------------------*/
1414 /* this makes the hcd giveback() the urb more quickly, by kicking it
1415 * off hardware queues (which may take a while) and returning it as
1416 * soon as practical. we've already set up the urb's return status,
1417 * but we can't know if the callback completed already.
1419 static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
1421 int value;
1423 if (is_root_hub(urb->dev))
1424 value = usb_rh_urb_dequeue(hcd, urb, status);
1425 else {
1427 /* The only reason an HCD might fail this call is if
1428 * it has not yet fully queued the urb to begin with.
1429 * Such failures should be harmless. */
1430 value = hcd->driver->urb_dequeue(hcd, urb, status);
1432 return value;
1436 * called in any context
1438 * caller guarantees urb won't be recycled till both unlink()
1439 * and the urb's completion function return
1441 int usb_hcd_unlink_urb (struct urb *urb, int status)
1443 struct usb_hcd *hcd;
1444 int retval = -EIDRM;
1445 unsigned long flags;
1447 /* Prevent the device and bus from going away while
1448 * the unlink is carried out. If they are already gone
1449 * then urb->use_count must be 0, since disconnected
1450 * devices can't have any active URBs.
1452 spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
1453 if (atomic_read(&urb->use_count) > 0) {
1454 retval = 0;
1455 usb_get_dev(urb->dev);
1457 spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
1458 if (retval == 0) {
1459 hcd = bus_to_hcd(urb->dev->bus);
1460 retval = unlink1(hcd, urb, status);
1461 usb_put_dev(urb->dev);
1464 if (retval == 0)
1465 retval = -EINPROGRESS;
1466 else if (retval != -EIDRM && retval != -EBUSY)
1467 dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
1468 urb, retval);
1469 return retval;
1472 /*-------------------------------------------------------------------------*/
1475 * usb_hcd_giveback_urb - return URB from HCD to device driver
1476 * @hcd: host controller returning the URB
1477 * @urb: urb being returned to the USB device driver.
1478 * @status: completion status code for the URB.
1479 * Context: in_interrupt()
1481 * This hands the URB from HCD to its USB device driver, using its
1482 * completion function. The HCD has freed all per-urb resources
1483 * (and is done using urb->hcpriv). It also released all HCD locks;
1484 * the device driver won't cause problems if it frees, modifies,
1485 * or resubmits this URB.
1487 * If @urb was unlinked, the value of @status will be overridden by
1488 * @urb->unlinked. Erroneous short transfers are detected in case
1489 * the HCD hasn't checked for them.
1491 void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
1493 urb->hcpriv = NULL;
1494 if (unlikely(urb->unlinked))
1495 status = urb->unlinked;
1496 else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
1497 urb->actual_length < urb->transfer_buffer_length &&
1498 !status))
1499 status = -EREMOTEIO;
1501 unmap_urb_for_dma(hcd, urb);
1502 usbmon_urb_complete(&hcd->self, urb, status);
1503 usb_unanchor_urb(urb);
1505 /* pass ownership to the completion handler */
1506 urb->status = status;
1507 urb->complete (urb);
1508 atomic_dec (&urb->use_count);
1509 if (unlikely(atomic_read(&urb->reject)))
1510 wake_up (&usb_kill_urb_queue);
1511 usb_put_urb (urb);
1513 EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
1515 /*-------------------------------------------------------------------------*/
1517 /* Cancel all URBs pending on this endpoint and wait for the endpoint's
1518 * queue to drain completely. The caller must first insure that no more
1519 * URBs can be submitted for this endpoint.
1521 void usb_hcd_flush_endpoint(struct usb_device *udev,
1522 struct usb_host_endpoint *ep)
1524 struct usb_hcd *hcd;
1525 struct urb *urb;
1527 if (!ep)
1528 return;
1529 might_sleep();
1530 hcd = bus_to_hcd(udev->bus);
1532 /* No more submits can occur */
1533 spin_lock_irq(&hcd_urb_list_lock);
1534 rescan:
1535 list_for_each_entry (urb, &ep->urb_list, urb_list) {
1536 int is_in;
1538 if (urb->unlinked)
1539 continue;
1540 usb_get_urb (urb);
1541 is_in = usb_urb_dir_in(urb);
1542 spin_unlock(&hcd_urb_list_lock);
1544 /* kick hcd */
1545 unlink1(hcd, urb, -ESHUTDOWN);
1546 dev_dbg (hcd->self.controller,
1547 "shutdown urb %p ep%d%s%s\n",
1548 urb, usb_endpoint_num(&ep->desc),
1549 is_in ? "in" : "out",
1550 ({ char *s;
1552 switch (usb_endpoint_type(&ep->desc)) {
1553 case USB_ENDPOINT_XFER_CONTROL:
1554 s = ""; break;
1555 case USB_ENDPOINT_XFER_BULK:
1556 s = "-bulk"; break;
1557 case USB_ENDPOINT_XFER_INT:
1558 s = "-intr"; break;
1559 default:
1560 s = "-iso"; break;
1563 }));
1564 usb_put_urb (urb);
1566 /* list contents may have changed */
1567 spin_lock(&hcd_urb_list_lock);
1568 goto rescan;
1570 spin_unlock_irq(&hcd_urb_list_lock);
1572 /* Wait until the endpoint queue is completely empty */
1573 while (!list_empty (&ep->urb_list)) {
1574 spin_lock_irq(&hcd_urb_list_lock);
1576 /* The list may have changed while we acquired the spinlock */
1577 urb = NULL;
1578 if (!list_empty (&ep->urb_list)) {
1579 urb = list_entry (ep->urb_list.prev, struct urb,
1580 urb_list);
1581 usb_get_urb (urb);
1583 spin_unlock_irq(&hcd_urb_list_lock);
1585 if (urb) {
1586 usb_kill_urb (urb);
1587 usb_put_urb (urb);
1592 /* Check whether a new configuration or alt setting for an interface
1593 * will exceed the bandwidth for the bus (or the host controller resources).
1594 * Only pass in a non-NULL config or interface, not both!
1595 * Passing NULL for both new_config and new_intf means the device will be
1596 * de-configured by issuing a set configuration 0 command.
1598 int usb_hcd_check_bandwidth(struct usb_device *udev,
1599 struct usb_host_config *new_config,
1600 struct usb_interface *new_intf)
1602 int num_intfs, i, j;
1603 struct usb_interface_cache *intf_cache;
1604 struct usb_host_interface *alt = 0;
1605 int ret = 0;
1606 struct usb_hcd *hcd;
1607 struct usb_host_endpoint *ep;
1609 hcd = bus_to_hcd(udev->bus);
1610 if (!hcd->driver->check_bandwidth)
1611 return 0;
1613 /* Configuration is being removed - set configuration 0 */
1614 if (!new_config && !new_intf) {
1615 for (i = 1; i < 16; ++i) {
1616 ep = udev->ep_out[i];
1617 if (ep)
1618 hcd->driver->drop_endpoint(hcd, udev, ep);
1619 ep = udev->ep_in[i];
1620 if (ep)
1621 hcd->driver->drop_endpoint(hcd, udev, ep);
1623 hcd->driver->check_bandwidth(hcd, udev);
1624 return 0;
1626 /* Check if the HCD says there's enough bandwidth. Enable all endpoints
1627 * each interface's alt setting 0 and ask the HCD to check the bandwidth
1628 * of the bus. There will always be bandwidth for endpoint 0, so it's
1629 * ok to exclude it.
1631 if (new_config) {
1632 num_intfs = new_config->desc.bNumInterfaces;
1633 /* Remove endpoints (except endpoint 0, which is always on the
1634 * schedule) from the old config from the schedule
1636 for (i = 1; i < 16; ++i) {
1637 ep = udev->ep_out[i];
1638 if (ep) {
1639 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1640 if (ret < 0)
1641 goto reset;
1643 ep = udev->ep_in[i];
1644 if (ep) {
1645 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1646 if (ret < 0)
1647 goto reset;
1650 for (i = 0; i < num_intfs; ++i) {
1652 /* Dig the endpoints for alt setting 0 out of the
1653 * interface cache for this interface
1655 intf_cache = new_config->intf_cache[i];
1656 for (j = 0; j < intf_cache->num_altsetting; j++) {
1657 if (intf_cache->altsetting[j].desc.bAlternateSetting == 0)
1658 alt = &intf_cache->altsetting[j];
1660 if (!alt) {
1661 printk(KERN_DEBUG "Did not find alt setting 0 for intf %d\n", i);
1662 continue;
1664 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1665 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
1666 if (ret < 0)
1667 goto reset;
1671 ret = hcd->driver->check_bandwidth(hcd, udev);
1672 reset:
1673 if (ret < 0)
1674 hcd->driver->reset_bandwidth(hcd, udev);
1675 return ret;
1678 /* Disables the endpoint: synchronizes with the hcd to make sure all
1679 * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
1680 * have been called previously. Use for set_configuration, set_interface,
1681 * driver removal, physical disconnect.
1683 * example: a qh stored in ep->hcpriv, holding state related to endpoint
1684 * type, maxpacket size, toggle, halt status, and scheduling.
1686 void usb_hcd_disable_endpoint(struct usb_device *udev,
1687 struct usb_host_endpoint *ep)
1689 struct usb_hcd *hcd;
1691 might_sleep();
1692 hcd = bus_to_hcd(udev->bus);
1693 if (hcd->driver->endpoint_disable)
1694 hcd->driver->endpoint_disable(hcd, ep);
1698 * usb_hcd_reset_endpoint - reset host endpoint state
1699 * @udev: USB device.
1700 * @ep: the endpoint to reset.
1702 * Resets any host endpoint state such as the toggle bit, sequence
1703 * number and current window.
1705 void usb_hcd_reset_endpoint(struct usb_device *udev,
1706 struct usb_host_endpoint *ep)
1708 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1710 if (hcd->driver->endpoint_reset)
1711 hcd->driver->endpoint_reset(hcd, ep);
1712 else {
1713 int epnum = usb_endpoint_num(&ep->desc);
1714 int is_out = usb_endpoint_dir_out(&ep->desc);
1715 int is_control = usb_endpoint_xfer_control(&ep->desc);
1717 usb_settoggle(udev, epnum, is_out, 0);
1718 if (is_control)
1719 usb_settoggle(udev, epnum, !is_out, 0);
1723 /* Protect against drivers that try to unlink URBs after the device
1724 * is gone, by waiting until all unlinks for @udev are finished.
1725 * Since we don't currently track URBs by device, simply wait until
1726 * nothing is running in the locked region of usb_hcd_unlink_urb().
1728 void usb_hcd_synchronize_unlinks(struct usb_device *udev)
1730 spin_lock_irq(&hcd_urb_unlink_lock);
1731 spin_unlock_irq(&hcd_urb_unlink_lock);
1734 /*-------------------------------------------------------------------------*/
1736 /* called in any context */
1737 int usb_hcd_get_frame_number (struct usb_device *udev)
1739 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1741 if (!HC_IS_RUNNING (hcd->state))
1742 return -ESHUTDOWN;
1743 return hcd->driver->get_frame_number (hcd);
1746 /*-------------------------------------------------------------------------*/
1748 #ifdef CONFIG_PM
1750 int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
1752 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
1753 int status;
1754 int old_state = hcd->state;
1756 dev_dbg(&rhdev->dev, "bus %s%s\n",
1757 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "suspend");
1758 if (!hcd->driver->bus_suspend) {
1759 status = -ENOENT;
1760 } else {
1761 hcd->state = HC_STATE_QUIESCING;
1762 status = hcd->driver->bus_suspend(hcd);
1764 if (status == 0) {
1765 usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
1766 hcd->state = HC_STATE_SUSPENDED;
1767 } else {
1768 hcd->state = old_state;
1769 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
1770 "suspend", status);
1772 return status;
1775 int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
1777 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
1778 int status;
1779 int old_state = hcd->state;
1781 dev_dbg(&rhdev->dev, "usb %s%s\n",
1782 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume");
1783 if (!hcd->driver->bus_resume)
1784 return -ENOENT;
1785 if (hcd->state == HC_STATE_RUNNING)
1786 return 0;
1788 hcd->state = HC_STATE_RESUMING;
1789 status = hcd->driver->bus_resume(hcd);
1790 if (status == 0) {
1791 /* TRSMRCY = 10 msec */
1792 msleep(10);
1793 usb_set_device_state(rhdev, rhdev->actconfig
1794 ? USB_STATE_CONFIGURED
1795 : USB_STATE_ADDRESS);
1796 hcd->state = HC_STATE_RUNNING;
1797 } else {
1798 hcd->state = old_state;
1799 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
1800 "resume", status);
1801 if (status != -ESHUTDOWN)
1802 usb_hc_died(hcd);
1804 return status;
1807 /* Workqueue routine for root-hub remote wakeup */
1808 static void hcd_resume_work(struct work_struct *work)
1810 struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
1811 struct usb_device *udev = hcd->self.root_hub;
1813 usb_lock_device(udev);
1814 usb_mark_last_busy(udev);
1815 usb_external_resume_device(udev, PMSG_REMOTE_RESUME);
1816 usb_unlock_device(udev);
1820 * usb_hcd_resume_root_hub - called by HCD to resume its root hub
1821 * @hcd: host controller for this root hub
1823 * The USB host controller calls this function when its root hub is
1824 * suspended (with the remote wakeup feature enabled) and a remote
1825 * wakeup request is received. The routine submits a workqueue request
1826 * to resume the root hub (that is, manage its downstream ports again).
1828 void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1830 unsigned long flags;
1832 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1833 if (hcd->rh_registered)
1834 queue_work(ksuspend_usb_wq, &hcd->wakeup_work);
1835 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1837 EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
1839 #endif
1841 /*-------------------------------------------------------------------------*/
1843 #ifdef CONFIG_USB_OTG
1846 * usb_bus_start_enum - start immediate enumeration (for OTG)
1847 * @bus: the bus (must use hcd framework)
1848 * @port_num: 1-based number of port; usually bus->otg_port
1849 * Context: in_interrupt()
1851 * Starts enumeration, with an immediate reset followed later by
1852 * khubd identifying and possibly configuring the device.
1853 * This is needed by OTG controller drivers, where it helps meet
1854 * HNP protocol timing requirements for starting a port reset.
1856 int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
1858 struct usb_hcd *hcd;
1859 int status = -EOPNOTSUPP;
1861 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
1862 * boards with root hubs hooked up to internal devices (instead of
1863 * just the OTG port) may need more attention to resetting...
1865 hcd = container_of (bus, struct usb_hcd, self);
1866 if (port_num && hcd->driver->start_port_reset)
1867 status = hcd->driver->start_port_reset(hcd, port_num);
1869 /* run khubd shortly after (first) root port reset finishes;
1870 * it may issue others, until at least 50 msecs have passed.
1872 if (status == 0)
1873 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
1874 return status;
1876 EXPORT_SYMBOL_GPL(usb_bus_start_enum);
1878 #endif
1880 /*-------------------------------------------------------------------------*/
1883 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
1884 * @irq: the IRQ being raised
1885 * @__hcd: pointer to the HCD whose IRQ is being signaled
1887 * If the controller isn't HALTed, calls the driver's irq handler.
1888 * Checks whether the controller is now dead.
1890 irqreturn_t usb_hcd_irq (int irq, void *__hcd)
1892 struct usb_hcd *hcd = __hcd;
1893 unsigned long flags;
1894 irqreturn_t rc;
1896 /* IRQF_DISABLED doesn't work correctly with shared IRQs
1897 * when the first handler doesn't use it. So let's just
1898 * assume it's never used.
1900 local_irq_save(flags);
1902 if (unlikely(hcd->state == HC_STATE_HALT ||
1903 !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
1904 rc = IRQ_NONE;
1905 } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
1906 rc = IRQ_NONE;
1907 } else {
1908 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
1910 if (unlikely(hcd->state == HC_STATE_HALT))
1911 usb_hc_died(hcd);
1912 rc = IRQ_HANDLED;
1915 local_irq_restore(flags);
1916 return rc;
1919 /*-------------------------------------------------------------------------*/
1922 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
1923 * @hcd: pointer to the HCD representing the controller
1925 * This is called by bus glue to report a USB host controller that died
1926 * while operations may still have been pending. It's called automatically
1927 * by the PCI glue, so only glue for non-PCI busses should need to call it.
1929 void usb_hc_died (struct usb_hcd *hcd)
1931 unsigned long flags;
1933 dev_err (hcd->self.controller, "HC died; cleaning up\n");
1935 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1936 if (hcd->rh_registered) {
1937 hcd->poll_rh = 0;
1939 /* make khubd clean up old urbs and devices */
1940 usb_set_device_state (hcd->self.root_hub,
1941 USB_STATE_NOTATTACHED);
1942 usb_kick_khubd (hcd->self.root_hub);
1944 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1946 EXPORT_SYMBOL_GPL (usb_hc_died);
1948 /*-------------------------------------------------------------------------*/
1951 * usb_create_hcd - create and initialize an HCD structure
1952 * @driver: HC driver that will use this hcd
1953 * @dev: device for this HC, stored in hcd->self.controller
1954 * @bus_name: value to store in hcd->self.bus_name
1955 * Context: !in_interrupt()
1957 * Allocate a struct usb_hcd, with extra space at the end for the
1958 * HC driver's private data. Initialize the generic members of the
1959 * hcd structure.
1961 * If memory is unavailable, returns NULL.
1963 struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
1964 struct device *dev, const char *bus_name)
1966 struct usb_hcd *hcd;
1968 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
1969 if (!hcd) {
1970 dev_dbg (dev, "hcd alloc failed\n");
1971 return NULL;
1973 dev_set_drvdata(dev, hcd);
1974 kref_init(&hcd->kref);
1976 usb_bus_init(&hcd->self);
1977 hcd->self.controller = dev;
1978 hcd->self.bus_name = bus_name;
1979 hcd->self.uses_dma = (dev->dma_mask != NULL);
1981 init_timer(&hcd->rh_timer);
1982 hcd->rh_timer.function = rh_timer_func;
1983 hcd->rh_timer.data = (unsigned long) hcd;
1984 #ifdef CONFIG_PM
1985 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
1986 #endif
1988 hcd->driver = driver;
1989 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
1990 "USB Host Controller";
1991 return hcd;
1993 EXPORT_SYMBOL_GPL(usb_create_hcd);
1995 static void hcd_release (struct kref *kref)
1997 struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
1999 kfree(hcd);
2002 struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
2004 if (hcd)
2005 kref_get (&hcd->kref);
2006 return hcd;
2008 EXPORT_SYMBOL_GPL(usb_get_hcd);
2010 void usb_put_hcd (struct usb_hcd *hcd)
2012 if (hcd)
2013 kref_put (&hcd->kref, hcd_release);
2015 EXPORT_SYMBOL_GPL(usb_put_hcd);
2018 * usb_add_hcd - finish generic HCD structure initialization and register
2019 * @hcd: the usb_hcd structure to initialize
2020 * @irqnum: Interrupt line to allocate
2021 * @irqflags: Interrupt type flags
2023 * Finish the remaining parts of generic HCD initialization: allocate the
2024 * buffers of consistent memory, register the bus, request the IRQ line,
2025 * and call the driver's reset() and start() routines.
2027 int usb_add_hcd(struct usb_hcd *hcd,
2028 unsigned int irqnum, unsigned long irqflags)
2030 int retval;
2031 struct usb_device *rhdev;
2033 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
2035 hcd->authorized_default = hcd->wireless? 0 : 1;
2036 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2038 /* HC is in reset state, but accessible. Now do the one-time init,
2039 * bottom up so that hcds can customize the root hubs before khubd
2040 * starts talking to them. (Note, bus id is assigned early too.)
2042 if ((retval = hcd_buffer_create(hcd)) != 0) {
2043 dev_dbg(hcd->self.controller, "pool alloc failed\n");
2044 return retval;
2047 if ((retval = usb_register_bus(&hcd->self)) < 0)
2048 goto err_register_bus;
2050 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
2051 dev_err(hcd->self.controller, "unable to allocate root hub\n");
2052 retval = -ENOMEM;
2053 goto err_allocate_root_hub;
2056 switch (hcd->driver->flags & HCD_MASK) {
2057 case HCD_USB11:
2058 rhdev->speed = USB_SPEED_FULL;
2059 break;
2060 case HCD_USB2:
2061 rhdev->speed = USB_SPEED_HIGH;
2062 break;
2063 case HCD_USB3:
2064 rhdev->speed = USB_SPEED_SUPER;
2065 break;
2066 default:
2067 goto err_allocate_root_hub;
2069 hcd->self.root_hub = rhdev;
2071 /* wakeup flag init defaults to "everything works" for root hubs,
2072 * but drivers can override it in reset() if needed, along with
2073 * recording the overall controller's system wakeup capability.
2075 device_init_wakeup(&rhdev->dev, 1);
2077 /* "reset" is misnamed; its role is now one-time init. the controller
2078 * should already have been reset (and boot firmware kicked off etc).
2080 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
2081 dev_err(hcd->self.controller, "can't setup\n");
2082 goto err_hcd_driver_setup;
2085 /* NOTE: root hub and controller capabilities may not be the same */
2086 if (device_can_wakeup(hcd->self.controller)
2087 && device_can_wakeup(&hcd->self.root_hub->dev))
2088 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
2090 /* enable irqs just before we start the controller */
2091 if (hcd->driver->irq) {
2093 /* IRQF_DISABLED doesn't work as advertised when used together
2094 * with IRQF_SHARED. As usb_hcd_irq() will always disable
2095 * interrupts we can remove it here.
2097 if (irqflags & IRQF_SHARED)
2098 irqflags &= ~IRQF_DISABLED;
2100 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
2101 hcd->driver->description, hcd->self.busnum);
2102 if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
2103 hcd->irq_descr, hcd)) != 0) {
2104 dev_err(hcd->self.controller,
2105 "request interrupt %d failed\n", irqnum);
2106 goto err_request_irq;
2108 hcd->irq = irqnum;
2109 dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
2110 (hcd->driver->flags & HCD_MEMORY) ?
2111 "io mem" : "io base",
2112 (unsigned long long)hcd->rsrc_start);
2113 } else {
2114 hcd->irq = -1;
2115 if (hcd->rsrc_start)
2116 dev_info(hcd->self.controller, "%s 0x%08llx\n",
2117 (hcd->driver->flags & HCD_MEMORY) ?
2118 "io mem" : "io base",
2119 (unsigned long long)hcd->rsrc_start);
2122 if ((retval = hcd->driver->start(hcd)) < 0) {
2123 dev_err(hcd->self.controller, "startup error %d\n", retval);
2124 goto err_hcd_driver_start;
2127 /* starting here, usbcore will pay attention to this root hub */
2128 rhdev->bus_mA = min(500u, hcd->power_budget);
2129 if ((retval = register_root_hub(hcd)) != 0)
2130 goto err_register_root_hub;
2132 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2133 if (retval < 0) {
2134 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
2135 retval);
2136 goto error_create_attr_group;
2138 if (hcd->uses_new_polling && hcd->poll_rh)
2139 usb_hcd_poll_rh_status(hcd);
2140 return retval;
2142 error_create_attr_group:
2143 mutex_lock(&usb_bus_list_lock);
2144 usb_disconnect(&hcd->self.root_hub);
2145 mutex_unlock(&usb_bus_list_lock);
2146 err_register_root_hub:
2147 hcd->driver->stop(hcd);
2148 err_hcd_driver_start:
2149 if (hcd->irq >= 0)
2150 free_irq(irqnum, hcd);
2151 err_request_irq:
2152 err_hcd_driver_setup:
2153 hcd->self.root_hub = NULL;
2154 usb_put_dev(rhdev);
2155 err_allocate_root_hub:
2156 usb_deregister_bus(&hcd->self);
2157 err_register_bus:
2158 hcd_buffer_destroy(hcd);
2159 return retval;
2161 EXPORT_SYMBOL_GPL(usb_add_hcd);
2164 * usb_remove_hcd - shutdown processing for generic HCDs
2165 * @hcd: the usb_hcd structure to remove
2166 * Context: !in_interrupt()
2168 * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
2169 * invoking the HCD's stop() method.
2171 void usb_remove_hcd(struct usb_hcd *hcd)
2173 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
2175 if (HC_IS_RUNNING (hcd->state))
2176 hcd->state = HC_STATE_QUIESCING;
2178 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
2179 spin_lock_irq (&hcd_root_hub_lock);
2180 hcd->rh_registered = 0;
2181 spin_unlock_irq (&hcd_root_hub_lock);
2183 #ifdef CONFIG_PM
2184 cancel_work_sync(&hcd->wakeup_work);
2185 #endif
2187 sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
2188 mutex_lock(&usb_bus_list_lock);
2189 usb_disconnect(&hcd->self.root_hub);
2190 mutex_unlock(&usb_bus_list_lock);
2192 hcd->driver->stop(hcd);
2193 hcd->state = HC_STATE_HALT;
2195 hcd->poll_rh = 0;
2196 del_timer_sync(&hcd->rh_timer);
2198 if (hcd->irq >= 0)
2199 free_irq(hcd->irq, hcd);
2200 usb_deregister_bus(&hcd->self);
2201 hcd_buffer_destroy(hcd);
2203 EXPORT_SYMBOL_GPL(usb_remove_hcd);
2205 void
2206 usb_hcd_platform_shutdown(struct platform_device* dev)
2208 struct usb_hcd *hcd = platform_get_drvdata(dev);
2210 if (hcd->driver->shutdown)
2211 hcd->driver->shutdown(hcd);
2213 EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
2215 /*-------------------------------------------------------------------------*/
2217 #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
2219 struct usb_mon_operations *mon_ops;
2222 * The registration is unlocked.
2223 * We do it this way because we do not want to lock in hot paths.
2225 * Notice that the code is minimally error-proof. Because usbmon needs
2226 * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
2229 int usb_mon_register (struct usb_mon_operations *ops)
2232 if (mon_ops)
2233 return -EBUSY;
2235 mon_ops = ops;
2236 mb();
2237 return 0;
2239 EXPORT_SYMBOL_GPL (usb_mon_register);
2241 void usb_mon_deregister (void)
2244 if (mon_ops == NULL) {
2245 printk(KERN_ERR "USB: monitor was not registered\n");
2246 return;
2248 mon_ops = NULL;
2249 mb();
2251 EXPORT_SYMBOL_GPL (usb_mon_deregister);
2253 #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */