2 * (C) Copyright Linus Torvalds 1999
3 * (C) Copyright Johannes Erdfelt 1999-2001
4 * (C) Copyright Andreas Gal 1999
5 * (C) Copyright Gregory P. Smith 1999
6 * (C) Copyright Deti Fliegl 1999
7 * (C) Copyright Randy Dunlap 2000
8 * (C) Copyright David Brownell 2000-2002
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/config.h>
27 #ifdef CONFIG_USB_DEBUG
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/completion.h>
36 #include <linux/uts.h> /* for UTS_SYSNAME */
39 #include <asm/scatterlist.h>
40 #include <linux/device.h>
41 #include <linux/dma-mapping.h>
42 #include <asm/byteorder.h>
44 #include <linux/usb.h>
50 // #define USB_BANDWIDTH_MESSAGES
52 /*-------------------------------------------------------------------------*/
55 * USB Host Controller Driver framework
57 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
58 * HCD-specific behaviors/bugs.
60 * This does error checks, tracks devices and urbs, and delegates to a
61 * "hc_driver" only for code (and data) that really needs to know about
62 * hardware differences. That includes root hub registers, i/o queues,
63 * and so on ... but as little else as possible.
65 * Shared code includes most of the "root hub" code (these are emulated,
66 * though each HC's hardware works differently) and PCI glue, plus request
67 * tracking overhead. The HCD code should only block on spinlocks or on
68 * hardware handshaking; blocking on software events (such as other kernel
69 * threads releasing resources, or completing actions) is all generic.
71 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
72 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
73 * only by the hub driver ... and that neither should be seen or used by
74 * usb client device drivers.
76 * Contributors of ideas or unattributed patches include: David Brownell,
77 * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
80 * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
81 * associated cleanup. "usb_hcd" still != "usb_bus".
82 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
85 /*-------------------------------------------------------------------------*/
87 /* host controllers we manage */
88 LIST_HEAD (usb_bus_list);
89 EXPORT_SYMBOL_GPL (usb_bus_list);
91 /* used when allocating bus numbers */
94 unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
96 static struct usb_busmap busmap;
98 /* used when updating list of hcds */
99 DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */
100 EXPORT_SYMBOL_GPL (usb_bus_list_lock);
102 /* used when updating hcd data */
103 static spinlock_t hcd_data_lock = SPIN_LOCK_UNLOCKED;
105 /* wait queue for synchronous unlinks */
106 DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
108 /*-------------------------------------------------------------------------*/
111 * Sharable chunks of root hub code.
114 /*-------------------------------------------------------------------------*/
116 #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
117 #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
119 /* usb 2.0 root hub device descriptor */
120 static const u8 usb2_rh_dev_descriptor [18] = {
121 0x12, /* __u8 bLength; */
122 0x01, /* __u8 bDescriptorType; Device */
123 0x00, 0x02, /* __u16 bcdUSB; v2.0 */
125 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
126 0x00, /* __u8 bDeviceSubClass; */
127 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/
128 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
130 0x00, 0x00, /* __u16 idVendor; */
131 0x00, 0x00, /* __u16 idProduct; */
132 KERNEL_VER, KERNEL_REL, /* __u16 bcdDevice */
134 0x03, /* __u8 iManufacturer; */
135 0x02, /* __u8 iProduct; */
136 0x01, /* __u8 iSerialNumber; */
137 0x01 /* __u8 bNumConfigurations; */
140 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
142 /* usb 1.1 root hub device descriptor */
143 static const u8 usb11_rh_dev_descriptor [18] = {
144 0x12, /* __u8 bLength; */
145 0x01, /* __u8 bDescriptorType; Device */
146 0x10, 0x01, /* __u16 bcdUSB; v1.1 */
148 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
149 0x00, /* __u8 bDeviceSubClass; */
150 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
151 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
153 0x00, 0x00, /* __u16 idVendor; */
154 0x00, 0x00, /* __u16 idProduct; */
155 KERNEL_VER, KERNEL_REL, /* __u16 bcdDevice */
157 0x03, /* __u8 iManufacturer; */
158 0x02, /* __u8 iProduct; */
159 0x01, /* __u8 iSerialNumber; */
160 0x01 /* __u8 bNumConfigurations; */
164 /*-------------------------------------------------------------------------*/
166 /* Configuration descriptors for our root hubs */
168 static const u8 fs_rh_config_descriptor [] = {
170 /* one configuration */
171 0x09, /* __u8 bLength; */
172 0x02, /* __u8 bDescriptorType; Configuration */
173 0x19, 0x00, /* __u16 wTotalLength; */
174 0x01, /* __u8 bNumInterfaces; (1) */
175 0x01, /* __u8 bConfigurationValue; */
176 0x00, /* __u8 iConfiguration; */
177 0xc0, /* __u8 bmAttributes;
182 0x00, /* __u8 MaxPower; */
185 * USB 2.0, single TT organization (mandatory):
186 * one interface, protocol 0
188 * USB 2.0, multiple TT organization (optional):
189 * two interfaces, protocols 1 (like single TT)
190 * and 2 (multiple TT mode) ... config is
196 0x09, /* __u8 if_bLength; */
197 0x04, /* __u8 if_bDescriptorType; Interface */
198 0x00, /* __u8 if_bInterfaceNumber; */
199 0x00, /* __u8 if_bAlternateSetting; */
200 0x01, /* __u8 if_bNumEndpoints; */
201 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
202 0x00, /* __u8 if_bInterfaceSubClass; */
203 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
204 0x00, /* __u8 if_iInterface; */
206 /* one endpoint (status change endpoint) */
207 0x07, /* __u8 ep_bLength; */
208 0x05, /* __u8 ep_bDescriptorType; Endpoint */
209 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
210 0x03, /* __u8 ep_bmAttributes; Interrupt */
211 0x02, 0x00, /* __u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
212 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
215 static const u8 hs_rh_config_descriptor [] = {
217 /* one configuration */
218 0x09, /* __u8 bLength; */
219 0x02, /* __u8 bDescriptorType; Configuration */
220 0x19, 0x00, /* __u16 wTotalLength; */
221 0x01, /* __u8 bNumInterfaces; (1) */
222 0x01, /* __u8 bConfigurationValue; */
223 0x00, /* __u8 iConfiguration; */
224 0xc0, /* __u8 bmAttributes;
229 0x00, /* __u8 MaxPower; */
232 * USB 2.0, single TT organization (mandatory):
233 * one interface, protocol 0
235 * USB 2.0, multiple TT organization (optional):
236 * two interfaces, protocols 1 (like single TT)
237 * and 2 (multiple TT mode) ... config is
243 0x09, /* __u8 if_bLength; */
244 0x04, /* __u8 if_bDescriptorType; Interface */
245 0x00, /* __u8 if_bInterfaceNumber; */
246 0x00, /* __u8 if_bAlternateSetting; */
247 0x01, /* __u8 if_bNumEndpoints; */
248 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
249 0x00, /* __u8 if_bInterfaceSubClass; */
250 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
251 0x00, /* __u8 if_iInterface; */
253 /* one endpoint (status change endpoint) */
254 0x07, /* __u8 ep_bLength; */
255 0x05, /* __u8 ep_bDescriptorType; Endpoint */
256 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
257 0x03, /* __u8 ep_bmAttributes; Interrupt */
258 0x02, 0x00, /* __u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
259 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
262 /*-------------------------------------------------------------------------*/
265 * helper routine for returning string descriptors in UTF-16LE
266 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
268 static int ascii2utf (char *s, u8 *utf, int utfmax)
272 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
280 * rh_string - provides manufacturer, product and serial strings for root hub
281 * @id: the string ID number (1: serial number, 2: product, 3: vendor)
282 * @hcd: the host controller for this root hub
283 * @type: string describing our driver
284 * @data: return packet in UTF-16 LE
285 * @len: length of the return packet
287 * Produces either a manufacturer, product or serial number string for the
288 * virtual root hub device.
290 static int rh_string (
300 *data++ = 4; *data++ = 3; /* 4 bytes string data */
301 *data++ = 0x09; *data++ = 0x04; /* MSFT-speak for "en-us" */
305 } else if (id == 1) {
306 strcpy (buf, hcd->self.bus_name);
308 // product description
309 } else if (id == 2) {
310 strcpy (buf, hcd->product_desc);
312 // id 3 == vendor description
313 } else if (id == 3) {
314 sprintf (buf, "%s %s %s", UTS_SYSNAME, UTS_RELEASE,
317 // unsupported IDs --> "protocol stall"
321 data [0] = 2 * (strlen (buf) + 1);
322 data [1] = 3; /* type == string */
323 return 2 + ascii2utf (buf, data + 2, len - 2);
327 /* Root hub control transfers execute synchronously */
328 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
330 struct usb_ctrlrequest *cmd;
331 u16 typeReq, wValue, wIndex, wLength;
332 const u8 *bufp = NULL;
333 u8 *ubuf = urb->transfer_buffer;
335 int patch_wakeup = 0;
338 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
339 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
340 wValue = le16_to_cpu (cmd->wValue);
341 wIndex = le16_to_cpu (cmd->wIndex);
342 wLength = le16_to_cpu (cmd->wLength);
344 if (wLength > urb->transfer_buffer_length)
347 /* set up for success */
349 urb->actual_length = wLength;
352 /* DEVICE REQUESTS */
354 case DeviceRequest | USB_REQ_GET_STATUS:
355 ubuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP)
356 | (1 << USB_DEVICE_SELF_POWERED);
359 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
360 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
361 hcd->remote_wakeup = 0;
365 case DeviceOutRequest | USB_REQ_SET_FEATURE:
366 if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP)
367 hcd->remote_wakeup = 1;
371 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
374 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
376 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
377 switch (wValue & 0xff00) {
378 case USB_DT_DEVICE << 8:
379 if (hcd->driver->flags & HCD_USB2)
380 bufp = usb2_rh_dev_descriptor;
381 else if (hcd->driver->flags & HCD_USB11)
382 bufp = usb11_rh_dev_descriptor;
387 case USB_DT_CONFIG << 8:
388 if (hcd->driver->flags & HCD_USB2) {
389 bufp = hs_rh_config_descriptor;
390 len = sizeof hs_rh_config_descriptor;
392 bufp = fs_rh_config_descriptor;
393 len = sizeof fs_rh_config_descriptor;
398 case USB_DT_STRING << 8:
399 urb->actual_length = rh_string (
407 case DeviceRequest | USB_REQ_GET_INTERFACE:
410 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
412 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
413 // wValue == urb->dev->devaddr
414 dev_dbg (hcd->self.controller, "root hub device address %d\n",
418 /* INTERFACE REQUESTS (no defined feature/status flags) */
420 /* ENDPOINT REQUESTS */
422 case EndpointRequest | USB_REQ_GET_STATUS:
423 // ENDPOINT_HALT flag
427 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
428 case EndpointOutRequest | USB_REQ_SET_FEATURE:
429 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
432 /* CLASS REQUESTS (and errors) */
435 /* non-generic request */
436 if (HCD_IS_SUSPENDED (hcd->state))
437 urb->status = -EAGAIN;
438 else if (!HCD_IS_RUNNING (hcd->state))
439 urb->status = -ENODEV;
441 urb->status = hcd->driver->hub_control (hcd,
442 typeReq, wValue, wIndex,
446 /* "protocol stall" on error */
447 urb->status = -EPIPE;
448 dev_dbg (hcd->self.controller, "unsupported hub control message (maxchild %d)\n",
452 urb->actual_length = 0;
453 dev_dbg (hcd->self.controller, "CTRL: TypeReq=0x%x val=0x%x idx=0x%x len=%d ==> %d\n",
454 typeReq, wValue, wIndex, wLength, urb->status);
457 if (urb->transfer_buffer_length < len)
458 len = urb->transfer_buffer_length;
459 urb->actual_length = len;
460 // always USB_DIR_IN, toward host
461 memcpy (ubuf, bufp, len);
463 /* report whether RH hardware supports remote wakeup */
465 ((struct usb_config_descriptor *)ubuf)->bmAttributes
466 |= USB_CONFIG_ATT_WAKEUP;
469 /* any errors get returned through the urb completion */
470 local_irq_save (flags);
471 usb_hcd_giveback_urb (hcd, urb, NULL);
472 local_irq_restore (flags);
476 /*-------------------------------------------------------------------------*/
479 * Root Hub interrupt transfers are synthesized with a timer.
480 * Completions are called in_interrupt() but not in_irq().
483 static void rh_report_status (unsigned long ptr);
485 static int rh_status_urb (struct usb_hcd *hcd, struct urb *urb)
487 int len = 1 + (urb->dev->maxchild / 8);
489 /* rh_timer protected by hcd_data_lock */
490 if (hcd->rh_timer.data
491 || urb->status != -EINPROGRESS
492 || urb->transfer_buffer_length < len
493 || !HCD_IS_RUNNING (hcd->state)) {
494 dev_dbg (hcd->self.controller,
495 "not queuing rh status urb, stat %d\n",
500 init_timer (&hcd->rh_timer);
501 hcd->rh_timer.function = rh_report_status;
502 hcd->rh_timer.data = (unsigned long) urb;
503 /* USB 2.0 spec says 256msec; this is close enough */
504 hcd->rh_timer.expires = jiffies + HZ/4;
505 add_timer (&hcd->rh_timer);
506 urb->hcpriv = hcd; /* nonzero to indicate it's queued */
512 static void rh_report_status (unsigned long ptr)
519 urb = (struct urb *) ptr;
520 local_irq_save (flags);
521 spin_lock (&urb->lock);
523 /* do nothing if the urb's been unlinked */
525 || urb->status != -EINPROGRESS
526 || (hcd = urb->dev->bus->hcpriv) == 0) {
527 spin_unlock (&urb->lock);
528 local_irq_restore (flags);
532 if (!HCD_IS_SUSPENDED (hcd->state))
533 length = hcd->driver->hub_status_data (
534 hcd, urb->transfer_buffer);
536 /* complete the status urb, or retrigger the timer */
537 spin_lock (&hcd_data_lock);
539 hcd->rh_timer.data = 0;
540 urb->actual_length = length;
544 mod_timer (&hcd->rh_timer, jiffies + HZ/4);
545 spin_unlock (&hcd_data_lock);
546 spin_unlock (&urb->lock);
548 /* local irqs are always blocked in completions */
550 usb_hcd_giveback_urb (hcd, urb, NULL);
551 local_irq_restore (flags);
554 /*-------------------------------------------------------------------------*/
556 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
558 if (usb_pipeint (urb->pipe)) {
562 spin_lock_irqsave (&hcd_data_lock, flags);
563 retval = rh_status_urb (hcd, urb);
564 spin_unlock_irqrestore (&hcd_data_lock, flags);
567 if (usb_pipecontrol (urb->pipe))
568 return rh_call_control (hcd, urb);
573 /*-------------------------------------------------------------------------*/
575 int usb_rh_status_dequeue (struct usb_hcd *hcd, struct urb *urb)
579 /* note: always a synchronous unlink */
580 del_timer_sync (&hcd->rh_timer);
581 hcd->rh_timer.data = 0;
583 local_irq_save (flags);
585 usb_hcd_giveback_urb (hcd, urb, NULL);
586 local_irq_restore (flags);
590 /*-------------------------------------------------------------------------*/
592 /* exported only within usbcore */
593 struct usb_bus *usb_bus_get (struct usb_bus *bus)
595 struct class_device *tmp;
600 tmp = class_device_get(&bus->class_dev);
602 return to_usb_bus(tmp);
607 /* exported only within usbcore */
608 void usb_bus_put (struct usb_bus *bus)
611 class_device_put(&bus->class_dev);
614 /*-------------------------------------------------------------------------*/
616 static void usb_host_release(struct class_device *class_dev)
618 struct usb_bus *bus = to_usb_bus(class_dev);
624 static struct class usb_host_class = {
626 .release = &usb_host_release,
629 int usb_host_init(void)
631 return class_register(&usb_host_class);
634 void usb_host_cleanup(void)
636 class_unregister(&usb_host_class);
640 * usb_bus_init - shared initialization code
641 * @bus: the bus structure being initialized
643 * This code is used to initialize a usb_bus structure, memory for which is
644 * separately managed.
646 void usb_bus_init (struct usb_bus *bus)
648 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
650 bus->devnum_next = 1;
652 bus->root_hub = NULL;
655 bus->bandwidth_allocated = 0;
656 bus->bandwidth_int_reqs = 0;
657 bus->bandwidth_isoc_reqs = 0;
659 INIT_LIST_HEAD (&bus->bus_list);
661 EXPORT_SYMBOL (usb_bus_init);
664 * usb_alloc_bus - creates a new USB host controller structure
665 * @op: pointer to a struct usb_operations that this bus structure should use
666 * Context: !in_interrupt()
668 * Creates a USB host controller bus structure with the specified
669 * usb_operations and initializes all the necessary internal objects.
671 * If no memory is available, NULL is returned.
673 * The caller should call usb_put_bus() when it is finished with the structure.
675 struct usb_bus *usb_alloc_bus (struct usb_operations *op)
679 bus = kmalloc (sizeof *bus, GFP_KERNEL);
682 memset(bus, 0, sizeof(struct usb_bus));
687 EXPORT_SYMBOL (usb_alloc_bus);
689 /*-------------------------------------------------------------------------*/
692 * usb_register_bus - registers the USB host controller with the usb core
693 * @bus: pointer to the bus to register
694 * Context: !in_interrupt()
696 * Assigns a bus number, and links the controller into usbcore data
697 * structures so that it can be seen by scanning the bus list.
699 int usb_register_bus(struct usb_bus *bus)
704 down (&usb_bus_list_lock);
705 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
706 if (busnum < USB_MAXBUS) {
707 set_bit (busnum, busmap.busmap);
708 bus->busnum = busnum;
710 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
711 up(&usb_bus_list_lock);
715 snprintf(bus->class_dev.class_id, BUS_ID_SIZE, "usb%d", busnum);
716 bus->class_dev.class = &usb_host_class;
717 bus->class_dev.dev = bus->controller;
718 retval = class_device_register(&bus->class_dev);
720 clear_bit(busnum, busmap.busmap);
721 up(&usb_bus_list_lock);
725 /* Add it to the local list of buses */
726 list_add (&bus->bus_list, &usb_bus_list);
727 up (&usb_bus_list_lock);
731 dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
734 EXPORT_SYMBOL (usb_register_bus);
737 * usb_deregister_bus - deregisters the USB host controller
738 * @bus: pointer to the bus to deregister
739 * Context: !in_interrupt()
741 * Recycles the bus number, and unlinks the controller from usbcore data
742 * structures so that it won't be seen by scanning the bus list.
744 void usb_deregister_bus (struct usb_bus *bus)
746 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
749 * NOTE: make sure that all the devices are removed by the
750 * controller code, as well as having it call this when cleaning
753 down (&usb_bus_list_lock);
754 list_del (&bus->bus_list);
755 up (&usb_bus_list_lock);
757 usbfs_remove_bus (bus);
759 clear_bit (bus->busnum, busmap.busmap);
761 class_device_unregister(&bus->class_dev);
763 EXPORT_SYMBOL (usb_deregister_bus);
766 * usb_register_root_hub - called by HCD to register its root hub
767 * @usb_dev: the usb root hub device to be registered.
768 * @parent_dev: the parent device of this root hub.
770 * The USB host controller calls this function to register the root hub
771 * properly with the USB subsystem. It sets up the device properly in
772 * the device tree and stores the root_hub pointer in the bus structure,
773 * then calls usb_new_device() to register the usb device. It also
774 * assigns the root hub's USB address (always 1).
776 int usb_register_root_hub (struct usb_device *usb_dev, struct device *parent_dev)
778 const int devnum = 1;
781 usb_dev->devnum = devnum;
782 usb_dev->bus->devnum_next = devnum + 1;
783 memset (&usb_dev->bus->devmap.devicemap, 0,
784 sizeof usb_dev->bus->devmap.devicemap);
785 set_bit (devnum, usb_dev->bus->devmap.devicemap);
786 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
788 down (&usb_bus_list_lock);
789 usb_dev->bus->root_hub = usb_dev;
791 usb_dev->epmaxpacketin[0] = usb_dev->epmaxpacketout[0] = 64;
792 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
793 if (retval != sizeof usb_dev->descriptor) {
794 usb_dev->bus->root_hub = NULL;
795 up (&usb_bus_list_lock);
796 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
797 usb_dev->dev.bus_id, retval);
798 return (retval < 0) ? retval : -EMSGSIZE;
801 down (&usb_dev->serialize);
802 retval = usb_new_device (usb_dev);
803 up (&usb_dev->serialize);
805 usb_dev->bus->root_hub = NULL;
806 dev_err (parent_dev, "can't register root hub for %s, %d\n",
807 usb_dev->dev.bus_id, retval);
809 up (&usb_bus_list_lock);
812 EXPORT_SYMBOL (usb_register_root_hub);
815 /*-------------------------------------------------------------------------*/
818 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
819 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
820 * @is_input: true iff the transaction sends data to the host
821 * @isoc: true for isochronous transactions, false for interrupt ones
822 * @bytecount: how many bytes in the transaction.
824 * Returns approximate bus time in nanoseconds for a periodic transaction.
825 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
826 * scheduled in software, this function is only used for such scheduling.
828 long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
833 case USB_SPEED_LOW: /* INTR only */
835 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
836 return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
838 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
839 return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
841 case USB_SPEED_FULL: /* ISOC or INTR */
843 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
844 return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
846 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
847 return (9107L + BW_HOST_DELAY + tmp);
849 case USB_SPEED_HIGH: /* ISOC or INTR */
850 // FIXME adjust for input vs output
852 tmp = HS_USECS (bytecount);
854 tmp = HS_USECS_ISO (bytecount);
857 pr_debug ("%s: bogus device speed!\n", usbcore_name);
861 EXPORT_SYMBOL (usb_calc_bus_time);
864 * usb_check_bandwidth():
866 * old_alloc is from host_controller->bandwidth_allocated in microseconds;
867 * bustime is from calc_bus_time(), but converted to microseconds.
869 * returns <bustime in us> if successful,
870 * or -ENOSPC if bandwidth request fails.
873 * This initial implementation does not use Endpoint.bInterval
874 * in managing bandwidth allocation.
875 * It probably needs to be expanded to use Endpoint.bInterval.
876 * This can be done as a later enhancement (correction).
878 * This will also probably require some kind of
879 * frame allocation tracking...meaning, for example,
880 * that if multiple drivers request interrupts every 10 USB frames,
881 * they don't all have to be allocated at
882 * frame numbers N, N+10, N+20, etc. Some of them could be at
883 * N+11, N+21, N+31, etc., and others at
884 * N+12, N+22, N+32, etc.
886 * Similarly for isochronous transfers...
888 * Individual HCDs can schedule more directly ... this logic
889 * is not correct for high speed transfers.
891 int usb_check_bandwidth (struct usb_device *dev, struct urb *urb)
893 unsigned int pipe = urb->pipe;
895 int is_in = usb_pipein (pipe);
896 int is_iso = usb_pipeisoc (pipe);
897 int old_alloc = dev->bus->bandwidth_allocated;
901 bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso,
902 usb_maxpacket (dev, pipe, !is_in)));
904 bustime /= urb->number_of_packets;
906 new_alloc = old_alloc + (int) bustime;
907 if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) {
910 #ifdef CONFIG_USB_BANDWIDTH
915 dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n",
916 mode, old_alloc, bustime, new_alloc);
918 #ifdef CONFIG_USB_BANDWIDTH
919 bustime = -ENOSPC; /* report error */
925 EXPORT_SYMBOL (usb_check_bandwidth);
929 * usb_claim_bandwidth - records bandwidth for a periodic transfer
930 * @dev: source/target of request
931 * @urb: request (urb->dev == dev)
932 * @bustime: bandwidth consumed, in (average) microseconds per frame
933 * @isoc: true iff the request is isochronous
935 * Bus bandwidth reservations are recorded purely for diagnostic purposes.
936 * HCDs are expected not to overcommit periodic bandwidth, and to record such
937 * reservations whenever endpoints are added to the periodic schedule.
939 * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's
940 * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable
941 * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how
942 * large its periodic schedule is.
944 void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc)
946 dev->bus->bandwidth_allocated += bustime;
948 dev->bus->bandwidth_isoc_reqs++;
950 dev->bus->bandwidth_int_reqs++;
951 urb->bandwidth = bustime;
953 #ifdef USB_BANDWIDTH_MESSAGES
954 dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n",
956 isoc ? "ISOC" : "INTR",
957 dev->bus->bandwidth_allocated,
958 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
961 EXPORT_SYMBOL (usb_claim_bandwidth);
965 * usb_release_bandwidth - reverses effect of usb_claim_bandwidth()
966 * @dev: source/target of request
967 * @urb: request (urb->dev == dev)
968 * @isoc: true iff the request is isochronous
970 * This records that previously allocated bandwidth has been released.
971 * Bandwidth is released when endpoints are removed from the host controller's
974 void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc)
976 dev->bus->bandwidth_allocated -= urb->bandwidth;
978 dev->bus->bandwidth_isoc_reqs--;
980 dev->bus->bandwidth_int_reqs--;
982 #ifdef USB_BANDWIDTH_MESSAGES
983 dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n",
985 isoc ? "ISOC" : "INTR",
986 dev->bus->bandwidth_allocated,
987 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
991 EXPORT_SYMBOL (usb_release_bandwidth);
994 /*-------------------------------------------------------------------------*/
997 * Generic HC operations.
1000 /*-------------------------------------------------------------------------*/
1002 /* called from khubd, or root hub init threads for hcd-private init */
1003 static int hcd_alloc_dev (struct usb_device *udev)
1005 struct hcd_dev *dev;
1006 struct usb_hcd *hcd;
1007 unsigned long flags;
1009 if (!udev || udev->hcpriv)
1011 if (!udev->bus || !udev->bus->hcpriv)
1013 hcd = udev->bus->hcpriv;
1014 if (hcd->state == USB_STATE_QUIESCING)
1017 dev = (struct hcd_dev *) kmalloc (sizeof *dev, GFP_KERNEL);
1020 memset (dev, 0, sizeof *dev);
1022 INIT_LIST_HEAD (&dev->dev_list);
1023 INIT_LIST_HEAD (&dev->urb_list);
1025 spin_lock_irqsave (&hcd_data_lock, flags);
1026 list_add (&dev->dev_list, &hcd->dev_list);
1027 // refcount is implicit
1029 spin_unlock_irqrestore (&hcd_data_lock, flags);
1034 /*-------------------------------------------------------------------------*/
1036 static void urb_unlink (struct urb *urb)
1038 unsigned long flags;
1040 /* Release any periodic transfer bandwidth */
1042 usb_release_bandwidth (urb->dev, urb,
1043 usb_pipeisoc (urb->pipe));
1045 /* clear all state linking urb to this dev (and hcd) */
1047 spin_lock_irqsave (&hcd_data_lock, flags);
1048 list_del_init (&urb->urb_list);
1049 spin_unlock_irqrestore (&hcd_data_lock, flags);
1050 usb_put_dev (urb->dev);
1054 /* may be called in any context with a valid urb->dev usecount
1055 * caller surrenders "ownership" of urb
1056 * expects usb_submit_urb() to have sanity checked and conditioned all
1059 static int hcd_submit_urb (struct urb *urb, int mem_flags)
1062 struct usb_hcd *hcd = urb->dev->bus->hcpriv;
1063 struct hcd_dev *dev = urb->dev->hcpriv;
1064 unsigned long flags;
1070 * FIXME: make urb timeouts be generic, keeping the HCD cores
1071 * as simple as possible.
1074 // NOTE: a generic device/urb monitoring hook would go here.
1075 // hcd_monitor_hook(MONITOR_URB_SUBMIT, urb)
1076 // It would catch submission paths for all urbs.
1079 * Atomically queue the urb, first to our records, then to the HCD.
1080 * Access to urb->status is controlled by urb->lock ... changes on
1081 * i/o completion (normal or fault) or unlinking.
1084 // FIXME: verify that quiescing hc works right (RH cleans up)
1086 spin_lock_irqsave (&hcd_data_lock, flags);
1087 if (unlikely (urb->reject))
1089 else if (HCD_IS_RUNNING (hcd->state) &&
1090 hcd->state != USB_STATE_QUIESCING) {
1091 usb_get_dev (urb->dev);
1092 list_add_tail (&urb->urb_list, &dev->urb_list);
1095 status = -ESHUTDOWN;
1096 spin_unlock_irqrestore (&hcd_data_lock, flags);
1098 INIT_LIST_HEAD (&urb->urb_list);
1102 /* increment urb's reference count as part of giving it to the HCD
1103 * (which now controls it). HCD guarantees that it either returns
1104 * an error or calls giveback(), but not both.
1106 urb = usb_get_urb (urb);
1107 atomic_inc (&urb->use_count);
1109 if (urb->dev == hcd->self.root_hub) {
1110 /* NOTE: requirement on hub callers (usbfs and the hub
1111 * driver, for now) that URBs' urb->transfer_buffer be
1112 * valid and usb_buffer_{sync,unmap}() not be needed, since
1113 * they could clobber root hub response data.
1115 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1116 | URB_NO_SETUP_DMA_MAP);
1117 status = rh_urb_enqueue (hcd, urb);
1121 /* lower level hcd code should use *_dma exclusively,
1122 * unless it uses pio or talks to another transport.
1124 if (hcd->self.controller->dma_mask) {
1125 if (usb_pipecontrol (urb->pipe)
1126 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1127 urb->setup_dma = dma_map_single (
1128 hcd->self.controller,
1130 sizeof (struct usb_ctrlrequest),
1132 if (urb->transfer_buffer_length != 0
1133 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
1134 urb->transfer_dma = dma_map_single (
1135 hcd->self.controller,
1136 urb->transfer_buffer,
1137 urb->transfer_buffer_length,
1138 usb_pipein (urb->pipe)
1143 status = hcd->driver->urb_enqueue (hcd, urb, mem_flags);
1145 if (unlikely (status)) {
1147 atomic_dec (&urb->use_count);
1149 wake_up (&usb_kill_urb_queue);
1155 /*-------------------------------------------------------------------------*/
1157 /* called in any context */
1158 static int hcd_get_frame_number (struct usb_device *udev)
1160 struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
1161 if (!HCD_IS_RUNNING (hcd->state))
1163 return hcd->driver->get_frame_number (hcd);
1166 /*-------------------------------------------------------------------------*/
1168 /* this makes the hcd giveback() the urb more quickly, by kicking it
1169 * off hardware queues (which may take a while) and returning it as
1170 * soon as practical. we've already set up the urb's return status,
1171 * but we can't know if the callback completed already.
1174 unlink1 (struct usb_hcd *hcd, struct urb *urb)
1178 if (urb == (struct urb *) hcd->rh_timer.data)
1179 value = usb_rh_status_dequeue (hcd, urb);
1182 /* The only reason an HCD might fail this call is if
1183 * it has not yet fully queued the urb to begin with.
1184 * Such failures should be harmless. */
1185 value = hcd->driver->urb_dequeue (hcd, urb);
1189 dev_dbg (hcd->self.controller, "dequeue %p --> %d\n",
1195 * called in any context
1197 * caller guarantees urb won't be recycled till both unlink()
1198 * and the urb's completion function return
1200 static int hcd_unlink_urb (struct urb *urb, int status)
1202 struct hcd_dev *dev;
1203 struct usb_hcd *hcd = NULL;
1204 struct device *sys = NULL;
1205 unsigned long flags;
1206 struct list_head *tmp;
1213 * we contend for urb->status with the hcd core,
1214 * which changes it while returning the urb.
1216 * Caller guaranteed that the urb pointer hasn't been freed, and
1217 * that it was submitted. But as a rule it can't know whether or
1218 * not it's already been unlinked ... so we respect the reversed
1219 * lock sequence needed for the usb_hcd_giveback_urb() code paths
1220 * (urb lock, then hcd_data_lock) in case some other CPU is now
1223 spin_lock_irqsave (&urb->lock, flags);
1224 spin_lock (&hcd_data_lock);
1226 if (!urb->dev || !urb->dev->bus) {
1231 dev = urb->dev->hcpriv;
1232 sys = &urb->dev->dev;
1233 hcd = urb->dev->bus->hcpriv;
1239 /* running ~= hc unlink handshake works (irq, timer, etc)
1240 * halted ~= no unlink handshake is needed
1241 * suspended, resuming == should never happen
1243 WARN_ON (!HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_HALT);
1245 /* insist the urb is still queued */
1246 list_for_each(tmp, &dev->urb_list) {
1247 if (tmp == &urb->urb_list)
1250 if (tmp != &urb->urb_list) {
1255 /* Any status except -EINPROGRESS means something already started to
1256 * unlink this URB from the hardware. So there's no more work to do.
1258 if (urb->status != -EINPROGRESS) {
1263 /* PCI IRQ setup can easily be broken so that USB controllers
1264 * never get completion IRQs ... maybe even the ones we need to
1265 * finish unlinking the initial failed usb_set_address().
1267 if (!hcd->saw_irq) {
1268 dev_warn (hcd->self.controller, "Unlink after no-IRQ? "
1269 "Different ACPI or APIC settings may help."
1274 urb->status = status;
1276 spin_unlock (&hcd_data_lock);
1277 spin_unlock_irqrestore (&urb->lock, flags);
1279 retval = unlink1 (hcd, urb);
1281 retval = -EINPROGRESS;
1285 spin_unlock (&hcd_data_lock);
1286 spin_unlock_irqrestore (&urb->lock, flags);
1287 if (retval != -EIDRM && sys && sys->driver)
1288 dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval);
1292 /*-------------------------------------------------------------------------*/
1294 /* disables the endpoint: cancels any pending urbs, then synchronizes with
1295 * the hcd to make sure all endpoint state is gone from hardware. use for
1296 * set_configuration, set_interface, driver removal, physical disconnect.
1298 * example: a qh stored in hcd_dev.ep[], holding state related to endpoint
1299 * type, maxpacket size, toggle, halt status, and scheduling.
1301 static void hcd_endpoint_disable (struct usb_device *udev, int endpoint)
1303 struct hcd_dev *dev;
1304 struct usb_hcd *hcd;
1306 unsigned epnum = endpoint & USB_ENDPOINT_NUMBER_MASK;
1309 hcd = udev->bus->hcpriv;
1311 WARN_ON (!HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_HALT);
1313 local_irq_disable ();
1316 /* (re)block new requests, as best we can */
1317 if (endpoint & USB_DIR_IN)
1318 udev->epmaxpacketin [epnum] = 0;
1320 udev->epmaxpacketout [epnum] = 0;
1322 /* then kill any current requests */
1323 spin_lock (&hcd_data_lock);
1324 list_for_each_entry (urb, &dev->urb_list, urb_list) {
1325 int tmp = urb->pipe;
1327 /* ignore urbs for other endpoints */
1328 if (usb_pipeendpoint (tmp) != epnum)
1330 /* NOTE assumption that only ep0 is a control endpoint */
1331 if (epnum != 0 && ((tmp ^ endpoint) & USB_DIR_IN))
1334 /* another cpu may be in hcd, spinning on hcd_data_lock
1335 * to giveback() this urb. the races here should be
1336 * small, but a full fix needs a new "can't submit"
1339 if (urb->status != -EINPROGRESS)
1342 spin_unlock (&hcd_data_lock);
1344 spin_lock (&urb->lock);
1346 if (tmp == -EINPROGRESS)
1347 urb->status = -ESHUTDOWN;
1348 spin_unlock (&urb->lock);
1350 /* kick hcd unless it's already returning this */
1351 if (tmp == -EINPROGRESS) {
1354 dev_dbg (hcd->self.controller,
1355 "shutdown urb %p pipe %08x ep%d%s%s\n",
1356 urb, tmp, usb_pipeendpoint (tmp),
1357 (tmp & USB_DIR_IN) ? "in" : "out",
1359 switch (usb_pipetype (tmp)) { \
1360 case PIPE_CONTROL: s = ""; break; \
1361 case PIPE_BULK: s = "-bulk"; break; \
1362 case PIPE_INTERRUPT: s = "-intr"; break; \
1363 default: s = "-iso"; break; \
1368 /* list contents may have changed */
1371 spin_unlock (&hcd_data_lock);
1372 local_irq_enable ();
1374 /* synchronize with the hardware, so old configuration state
1375 * clears out immediately (and will be freed).
1378 if (hcd->driver->endpoint_disable)
1379 hcd->driver->endpoint_disable (hcd, dev, endpoint);
1382 /*-------------------------------------------------------------------------*/
1384 #ifdef CONFIG_USB_SUSPEND
1386 static int hcd_hub_suspend (struct usb_bus *bus)
1388 struct usb_hcd *hcd;
1390 hcd = container_of (bus, struct usb_hcd, self);
1391 if (hcd->driver->hub_suspend)
1392 return hcd->driver->hub_suspend (hcd);
1396 static int hcd_hub_resume (struct usb_bus *bus)
1398 struct usb_hcd *hcd;
1400 hcd = container_of (bus, struct usb_hcd, self);
1401 if (hcd->driver->hub_resume)
1402 return hcd->driver->hub_resume (hcd);
1408 /*-------------------------------------------------------------------------*/
1410 #ifdef CONFIG_USB_OTG
1413 * usb_bus_start_enum - start immediate enumeration (for OTG)
1414 * @bus: the bus (must use hcd framework)
1415 * @port: 1-based number of port; usually bus->otg_port
1416 * Context: in_interrupt()
1418 * Starts enumeration, with an immediate reset followed later by
1419 * khubd identifying and possibly configuring the device.
1420 * This is needed by OTG controller drivers, where it helps meet
1421 * HNP protocol timing requirements for starting a port reset.
1423 int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
1425 struct usb_hcd *hcd;
1426 int status = -EOPNOTSUPP;
1428 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
1429 * boards with root hubs hooked up to internal devices (instead of
1430 * just the OTG port) may need more attention to resetting...
1432 hcd = container_of (bus, struct usb_hcd, self);
1433 if (port_num && hcd->driver->start_port_reset)
1434 status = hcd->driver->start_port_reset(hcd, port_num);
1436 /* run khubd shortly after (first) root port reset finishes;
1437 * it may issue others, until at least 50 msecs have passed.
1440 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
1443 EXPORT_SYMBOL (usb_bus_start_enum);
1447 /*-------------------------------------------------------------------------*/
1449 /* called by khubd, rmmod, apmd, or other thread for hcd-private cleanup.
1450 * we're guaranteed that the device is fully quiesced. also, that each
1451 * endpoint has been hcd_endpoint_disabled.
1454 static int hcd_free_dev (struct usb_device *udev)
1456 struct hcd_dev *dev;
1457 struct usb_hcd *hcd;
1458 unsigned long flags;
1460 if (!udev || !udev->hcpriv)
1463 if (!udev->bus || !udev->bus->hcpriv)
1466 // should udev->devnum == -1 ??
1469 hcd = udev->bus->hcpriv;
1471 /* device driver problem with refcounts? */
1472 if (!list_empty (&dev->urb_list)) {
1473 dev_dbg (hcd->self.controller, "free busy dev, %s devnum %d (bug!)\n",
1474 hcd->self.bus_name, udev->devnum);
1478 spin_lock_irqsave (&hcd_data_lock, flags);
1479 list_del (&dev->dev_list);
1480 udev->hcpriv = NULL;
1481 spin_unlock_irqrestore (&hcd_data_lock, flags);
1488 * usb_hcd_operations - adapts usb_bus framework to HCD framework (bus glue)
1490 * When registering a USB bus through the HCD framework code, use this
1491 * usb_operations vector. The PCI glue layer does so automatically; only
1492 * bus glue for non-PCI system busses will need to use this.
1494 struct usb_operations usb_hcd_operations = {
1495 .allocate = hcd_alloc_dev,
1496 .get_frame_number = hcd_get_frame_number,
1497 .submit_urb = hcd_submit_urb,
1498 .unlink_urb = hcd_unlink_urb,
1499 .deallocate = hcd_free_dev,
1500 .buffer_alloc = hcd_buffer_alloc,
1501 .buffer_free = hcd_buffer_free,
1502 .disable = hcd_endpoint_disable,
1503 #ifdef CONFIG_USB_SUSPEND
1504 .hub_suspend = hcd_hub_suspend,
1505 .hub_resume = hcd_hub_resume,
1508 EXPORT_SYMBOL (usb_hcd_operations);
1510 /*-------------------------------------------------------------------------*/
1513 * usb_hcd_giveback_urb - return URB from HCD to device driver
1514 * @hcd: host controller returning the URB
1515 * @urb: urb being returned to the USB device driver.
1516 * @regs: pt_regs, passed down to the URB completion handler
1517 * Context: in_interrupt()
1519 * This hands the URB from HCD to its USB device driver, using its
1520 * completion function. The HCD has freed all per-urb resources
1521 * (and is done using urb->hcpriv). It also released all HCD locks;
1522 * the device driver won't cause problems if it frees, modifies,
1523 * or resubmits this URB.
1525 void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
1529 // NOTE: a generic device/urb monitoring hook would go here.
1530 // hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev)
1531 // It would catch exit/unlink paths for all urbs.
1533 /* lower level hcd code should use *_dma exclusively */
1534 if (hcd->self.controller->dma_mask) {
1535 if (usb_pipecontrol (urb->pipe)
1536 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1537 dma_unmap_single (hcd->self.controller, urb->setup_dma,
1538 sizeof (struct usb_ctrlrequest),
1540 if (urb->transfer_buffer_length != 0
1541 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
1542 dma_unmap_single (hcd->self.controller,
1544 urb->transfer_buffer_length,
1545 usb_pipein (urb->pipe)
1550 /* pass ownership to the completion handler */
1551 urb->complete (urb, regs);
1552 atomic_dec (&urb->use_count);
1553 if (unlikely (urb->reject))
1554 wake_up (&usb_kill_urb_queue);
1557 EXPORT_SYMBOL (usb_hcd_giveback_urb);
1559 /*-------------------------------------------------------------------------*/
1562 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
1563 * @irq: the IRQ being raised
1564 * @__hcd: pointer to the HCD whose IRQ is beinng signaled
1565 * @r: saved hardware registers
1567 * When registering a USB bus through the HCD framework code, use this
1568 * to handle interrupts. The PCI glue layer does so automatically; only
1569 * bus glue for non-PCI system busses will need to use this.
1571 irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r)
1573 struct usb_hcd *hcd = __hcd;
1574 int start = hcd->state;
1576 if (unlikely (hcd->state == USB_STATE_HALT)) /* irq sharing? */
1580 if (hcd->driver->irq (hcd, r) == IRQ_NONE)
1583 if (hcd->state != start && hcd->state == USB_STATE_HALT)
1587 EXPORT_SYMBOL (usb_hcd_irq);
1589 /*-------------------------------------------------------------------------*/
1591 static void hcd_panic (void *_hcd)
1593 struct usb_hcd *hcd = _hcd;
1594 struct usb_device *hub = hcd->self.root_hub;
1597 /* hc's root hub is removed later removed in hcd->stop() */
1598 down (&hub->serialize);
1599 usb_set_device_state(hub, USB_STATE_NOTATTACHED);
1600 for (i = 0; i < hub->maxchild; i++) {
1601 if (hub->children [i])
1602 usb_disconnect (&hub->children [i]);
1604 up (&hub->serialize);
1608 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
1609 * @hcd: pointer to the HCD representing the controller
1611 * This is called by bus glue to report a USB host controller that died
1612 * while operations may still have been pending. It's called automatically
1613 * by the PCI glue, so only glue for non-PCI busses should need to call it.
1615 void usb_hc_died (struct usb_hcd *hcd)
1617 dev_err (hcd->self.controller, "HC died; cleaning up\n");
1619 /* clean up old urbs and devices; needs a task context */
1620 INIT_WORK (&hcd->work, hcd_panic, hcd);
1621 (void) schedule_work (&hcd->work);
1623 EXPORT_SYMBOL (usb_hc_died);