MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / core / hcd.c
blobe20711d27848aa2e3c2936ac33fc5e08d4a2e5b2
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/config.h>
27 #ifdef CONFIG_USB_DEBUG
28 #define DEBUG
29 #endif
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 */
37 #include <linux/mm.h>
38 #include <asm/io.h>
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>
46 #include "usb.h"
47 #include "hcd.h"
49 //#define VICTOR_DEBUG
50 #ifdef VICTOR_DEBUG
51 #define victor_printk(x...) printk(x)
52 #else // VICTOR_DEBUG
53 #define victor_printk(x...)
54 #endif // VICTOR_DEBUG
56 // #define USB_BANDWIDTH_MESSAGES
58 /*-------------------------------------------------------------------------*/
61 * USB Host Controller Driver framework
63 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
64 * HCD-specific behaviors/bugs.
66 * This does error checks, tracks devices and urbs, and delegates to a
67 * "hc_driver" only for code (and data) that really needs to know about
68 * hardware differences. That includes root hub registers, i/o queues,
69 * and so on ... but as little else as possible.
71 * Shared code includes most of the "root hub" code (these are emulated,
72 * though each HC's hardware works differently) and PCI glue, plus request
73 * tracking overhead. The HCD code should only block on spinlocks or on
74 * hardware handshaking; blocking on software events (such as other kernel
75 * threads releasing resources, or completing actions) is all generic.
77 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
78 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
79 * only by the hub driver ... and that neither should be seen or used by
80 * usb client device drivers.
82 * Contributors of ideas or unattributed patches include: David Brownell,
83 * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
85 * HISTORY:
86 * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
87 * associated cleanup. "usb_hcd" still != "usb_bus".
88 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
91 /*-------------------------------------------------------------------------*/
93 /* host controllers we manage */
94 LIST_HEAD (usb_bus_list);
95 EXPORT_SYMBOL_GPL (usb_bus_list);
97 /* used when allocating bus numbers */
98 #define USB_MAXBUS 64
99 struct usb_busmap {
100 unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
102 static struct usb_busmap busmap;
104 /* used when updating list of hcds */
105 DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */
106 EXPORT_SYMBOL_GPL (usb_bus_list_lock);
108 /* used when updating hcd data */
109 static spinlock_t hcd_data_lock = SPIN_LOCK_UNLOCKED;
111 /* wait queue for synchronous unlinks */
112 DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
114 /*-------------------------------------------------------------------------*/
117 * Sharable chunks of root hub code.
120 /*-------------------------------------------------------------------------*/
122 #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
123 #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
125 /* usb 2.0 root hub device descriptor */
126 static const u8 usb2_rh_dev_descriptor [18] = {
127 0x12, /* __u8 bLength; */
128 0x01, /* __u8 bDescriptorType; Device */
129 0x00, 0x02, /* __u16 bcdUSB; v2.0 */
131 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
132 0x00, /* __u8 bDeviceSubClass; */
133 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/
134 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
136 0x00, 0x00, /* __u16 idVendor; */
137 0x00, 0x00, /* __u16 idProduct; */
138 KERNEL_VER, KERNEL_REL, /* __u16 bcdDevice */
140 0x03, /* __u8 iManufacturer; */
141 0x02, /* __u8 iProduct; */
142 0x01, /* __u8 iSerialNumber; */
143 0x01 /* __u8 bNumConfigurations; */
146 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
148 /* usb 1.1 root hub device descriptor */
149 static const u8 usb11_rh_dev_descriptor [18] = {
150 0x12, /* __u8 bLength; */
151 0x01, /* __u8 bDescriptorType; Device */
152 0x10, 0x01, /* __u16 bcdUSB; v1.1 */
154 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
155 0x00, /* __u8 bDeviceSubClass; */
156 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
157 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
159 0x00, 0x00, /* __u16 idVendor; */
160 0x00, 0x00, /* __u16 idProduct; */
161 KERNEL_VER, KERNEL_REL, /* __u16 bcdDevice */
163 0x03, /* __u8 iManufacturer; */
164 0x02, /* __u8 iProduct; */
165 0x01, /* __u8 iSerialNumber; */
166 0x01 /* __u8 bNumConfigurations; */
170 /*-------------------------------------------------------------------------*/
172 /* Configuration descriptors for our root hubs */
174 static const u8 fs_rh_config_descriptor [] = {
176 /* one configuration */
177 0x09, /* __u8 bLength; */
178 0x02, /* __u8 bDescriptorType; Configuration */
179 0x19, 0x00, /* __u16 wTotalLength; */
180 0x01, /* __u8 bNumInterfaces; (1) */
181 0x01, /* __u8 bConfigurationValue; */
182 0x00, /* __u8 iConfiguration; */
183 0xc0, /* __u8 bmAttributes;
184 Bit 7: must be set,
185 6: Self-powered,
186 5: Remote wakeup,
187 4..0: resvd */
188 0x00, /* __u8 MaxPower; */
190 /* USB 1.1:
191 * USB 2.0, single TT organization (mandatory):
192 * one interface, protocol 0
194 * USB 2.0, multiple TT organization (optional):
195 * two interfaces, protocols 1 (like single TT)
196 * and 2 (multiple TT mode) ... config is
197 * sometimes settable
198 * NOT IMPLEMENTED
201 /* one interface */
202 0x09, /* __u8 if_bLength; */
203 0x04, /* __u8 if_bDescriptorType; Interface */
204 0x00, /* __u8 if_bInterfaceNumber; */
205 0x00, /* __u8 if_bAlternateSetting; */
206 0x01, /* __u8 if_bNumEndpoints; */
207 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
208 0x00, /* __u8 if_bInterfaceSubClass; */
209 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
210 0x00, /* __u8 if_iInterface; */
212 /* one endpoint (status change endpoint) */
213 0x07, /* __u8 ep_bLength; */
214 0x05, /* __u8 ep_bDescriptorType; Endpoint */
215 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
216 0x03, /* __u8 ep_bmAttributes; Interrupt */
217 0x02, 0x00, /* __u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
218 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
221 static const u8 hs_rh_config_descriptor [] = {
223 /* one configuration */
224 0x09, /* __u8 bLength; */
225 0x02, /* __u8 bDescriptorType; Configuration */
226 0x19, 0x00, /* __u16 wTotalLength; */
227 0x01, /* __u8 bNumInterfaces; (1) */
228 0x01, /* __u8 bConfigurationValue; */
229 0x00, /* __u8 iConfiguration; */
230 0xc0, /* __u8 bmAttributes;
231 Bit 7: must be set,
232 6: Self-powered,
233 5: Remote wakeup,
234 4..0: resvd */
235 0x00, /* __u8 MaxPower; */
237 /* USB 1.1:
238 * USB 2.0, single TT organization (mandatory):
239 * one interface, protocol 0
241 * USB 2.0, multiple TT organization (optional):
242 * two interfaces, protocols 1 (like single TT)
243 * and 2 (multiple TT mode) ... config is
244 * sometimes settable
245 * NOT IMPLEMENTED
248 /* one interface */
249 0x09, /* __u8 if_bLength; */
250 0x04, /* __u8 if_bDescriptorType; Interface */
251 0x00, /* __u8 if_bInterfaceNumber; */
252 0x00, /* __u8 if_bAlternateSetting; */
253 0x01, /* __u8 if_bNumEndpoints; */
254 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
255 0x00, /* __u8 if_bInterfaceSubClass; */
256 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
257 0x00, /* __u8 if_iInterface; */
259 /* one endpoint (status change endpoint) */
260 0x07, /* __u8 ep_bLength; */
261 0x05, /* __u8 ep_bDescriptorType; Endpoint */
262 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
263 0x03, /* __u8 ep_bmAttributes; Interrupt */
264 0x02, 0x00, /* __u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
265 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
268 /*-------------------------------------------------------------------------*/
271 * helper routine for returning string descriptors in UTF-16LE
272 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
274 static int ascii2utf (char *s, u8 *utf, int utfmax)
276 int retval;
278 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
279 *utf++ = *s++;
280 *utf++ = 0;
282 return retval;
286 * rh_string - provides manufacturer, product and serial strings for root hub
287 * @id: the string ID number (1: serial number, 2: product, 3: vendor)
288 * @hcd: the host controller for this root hub
289 * @type: string describing our driver
290 * @data: return packet in UTF-16 LE
291 * @len: length of the return packet
293 * Produces either a manufacturer, product or serial number string for the
294 * virtual root hub device.
296 static int rh_string (
297 int id,
298 struct usb_hcd *hcd,
299 u8 *data,
300 int len
302 char buf [100];
304 // language ids
305 if (id == 0) {
306 *data++ = 4; *data++ = 3; /* 4 bytes string data */
307 *data++ = 0x09; *data++ = 0x04; /* MSFT-speak for "en-us" */
308 return 4;
310 // serial number
311 } else if (id == 1) {
312 strcpy (buf, hcd->self.bus_name);
314 // product description
315 } else if (id == 2) {
316 strcpy (buf, hcd->product_desc);
318 // id 3 == vendor description
319 } else if (id == 3) {
320 sprintf (buf, "%s %s %s", UTS_SYSNAME, UTS_RELEASE,
321 hcd->description);
323 // unsupported IDs --> "protocol stall"
324 } else
325 return 0;
327 data [0] = 2 * (strlen (buf) + 1);
328 data [1] = 3; /* type == string */
329 return 2 + ascii2utf (buf, data + 2, len - 2);
333 /* Root hub control transfers execute synchronously */
334 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
336 struct usb_ctrlrequest *cmd;
337 u16 typeReq, wValue, wIndex, wLength;
338 const u8 *bufp = NULL;
339 u8 *ubuf = urb->transfer_buffer;
340 int len = 0;
341 int patch_wakeup = 0;
342 unsigned long flags;
344 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
345 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
346 wValue = le16_to_cpu (cmd->wValue);
347 wIndex = le16_to_cpu (cmd->wIndex);
348 wLength = le16_to_cpu (cmd->wLength);
350 if (wLength > urb->transfer_buffer_length)
351 goto error;
353 /* set up for success */
354 urb->status = 0;
355 urb->actual_length = wLength;
356 switch (typeReq) {
358 /* DEVICE REQUESTS */
360 case DeviceRequest | USB_REQ_GET_STATUS:
361 ubuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP)
362 | (1 << USB_DEVICE_SELF_POWERED);
363 ubuf [1] = 0;
364 break;
365 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
366 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
367 hcd->remote_wakeup = 0;
368 else
369 goto error;
370 break;
371 case DeviceOutRequest | USB_REQ_SET_FEATURE:
372 if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP)
373 hcd->remote_wakeup = 1;
374 else
375 goto error;
376 break;
377 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
378 ubuf [0] = 1;
379 /* FALLTHROUGH */
380 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
381 break;
382 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
383 switch (wValue & 0xff00) {
384 case USB_DT_DEVICE << 8:
385 if (hcd->driver->flags & HCD_USB2)
386 bufp = usb2_rh_dev_descriptor;
387 else if (hcd->driver->flags & HCD_USB11)
388 bufp = usb11_rh_dev_descriptor;
389 else
390 goto error;
391 len = 18;
392 break;
393 case USB_DT_CONFIG << 8:
394 if (hcd->driver->flags & HCD_USB2) {
395 bufp = hs_rh_config_descriptor;
396 len = sizeof hs_rh_config_descriptor;
397 } else {
398 bufp = fs_rh_config_descriptor;
399 len = sizeof fs_rh_config_descriptor;
401 if (hcd->can_wakeup)
402 patch_wakeup = 1;
403 break;
404 case USB_DT_STRING << 8:
405 urb->actual_length = rh_string (
406 wValue & 0xff, hcd,
407 ubuf, wLength);
408 break;
409 default:
410 goto error;
412 break;
413 case DeviceRequest | USB_REQ_GET_INTERFACE:
414 ubuf [0] = 0;
415 /* FALLTHROUGH */
416 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
417 break;
418 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
419 // wValue == urb->dev->devaddr
420 dev_dbg (hcd->self.controller, "root hub device address %d\n",
421 wValue);
422 break;
424 /* INTERFACE REQUESTS (no defined feature/status flags) */
426 /* ENDPOINT REQUESTS */
428 case EndpointRequest | USB_REQ_GET_STATUS:
429 // ENDPOINT_HALT flag
430 ubuf [0] = 0;
431 ubuf [1] = 0;
432 /* FALLTHROUGH */
433 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
434 case EndpointOutRequest | USB_REQ_SET_FEATURE:
435 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
436 break;
438 /* CLASS REQUESTS (and errors) */
440 default:
441 /* non-generic request */
442 if (HCD_IS_SUSPENDED (hcd->state))
443 urb->status = -EAGAIN;
444 else if (!HCD_IS_RUNNING (hcd->state))
445 urb->status = -ENODEV;
446 else
447 urb->status = hcd->driver->hub_control (hcd,
448 typeReq, wValue, wIndex,
449 ubuf, wLength);
450 break;
451 error:
452 /* "protocol stall" on error */
453 urb->status = -EPIPE;
454 dev_dbg (hcd->self.controller, "unsupported hub control message (maxchild %d)\n",
455 urb->dev->maxchild);
457 if (urb->status) {
458 urb->actual_length = 0;
459 dev_dbg (hcd->self.controller, "CTRL: TypeReq=0x%x val=0x%x idx=0x%x len=%d ==> %d\n",
460 typeReq, wValue, wIndex, wLength, urb->status);
462 if (bufp) {
463 if (urb->transfer_buffer_length < len)
464 len = urb->transfer_buffer_length;
465 urb->actual_length = len;
466 // always USB_DIR_IN, toward host
467 memcpy (ubuf, bufp, len);
469 /* report whether RH hardware supports remote wakeup */
470 if (patch_wakeup)
471 ((struct usb_config_descriptor *)ubuf)->bmAttributes
472 |= USB_CONFIG_ATT_WAKEUP;
475 /* any errors get returned through the urb completion */
476 local_irq_save (flags);
477 usb_hcd_giveback_urb (hcd, urb, NULL);
478 local_irq_restore (flags);
479 return 0;
482 /*-------------------------------------------------------------------------*/
485 * Root Hub interrupt transfers are synthesized with a timer.
486 * Completions are called in_interrupt() but not in_irq().
489 static void rh_report_status (unsigned long ptr);
491 static int rh_status_urb (struct usb_hcd *hcd, struct urb *urb)
493 int len = 1 + (urb->dev->maxchild / 8);
495 /* rh_timer protected by hcd_data_lock */
496 if (hcd->rh_timer.data
497 || urb->status != -EINPROGRESS
498 || urb->transfer_buffer_length < len
499 || !HCD_IS_RUNNING (hcd->state)) {
500 dev_dbg (hcd->self.controller,
501 "not queuing rh status urb, stat %d\n",
502 urb->status);
503 return -EINVAL;
506 init_timer (&hcd->rh_timer);
507 hcd->rh_timer.function = rh_report_status;
508 hcd->rh_timer.data = (unsigned long) urb;
509 /* USB 2.0 spec says 256msec; this is close enough */
510 hcd->rh_timer.expires = jiffies + HZ/4;
511 add_timer (&hcd->rh_timer);
512 urb->hcpriv = hcd; /* nonzero to indicate it's queued */
513 return 0;
516 /* timer callback */
518 static void rh_report_status (unsigned long ptr)
520 struct urb *urb;
521 struct usb_hcd *hcd;
522 int length = 0;
523 unsigned long flags;
525 urb = (struct urb *) ptr;
526 local_irq_save (flags);
527 spin_lock (&urb->lock);
529 /* do nothing if the urb's been unlinked */
530 if (!urb->dev
531 || urb->status != -EINPROGRESS
532 || (hcd = urb->dev->bus->hcpriv) == 0) {
533 spin_unlock (&urb->lock);
534 local_irq_restore (flags);
535 return;
538 if (!HCD_IS_SUSPENDED (hcd->state))
539 length = hcd->driver->hub_status_data (
540 hcd, urb->transfer_buffer);
542 /* complete the status urb, or retrigger the timer */
543 spin_lock (&hcd_data_lock);
544 if (length > 0) {
545 hcd->rh_timer.data = 0;
546 urb->actual_length = length;
547 urb->status = 0;
548 urb->hcpriv = NULL;
549 } else
550 mod_timer (&hcd->rh_timer, jiffies + HZ/4);
551 spin_unlock (&hcd_data_lock);
552 spin_unlock (&urb->lock);
554 /* local irqs are always blocked in completions */
555 if (length > 0)
556 usb_hcd_giveback_urb (hcd, urb, NULL);
557 local_irq_restore (flags);
560 /*-------------------------------------------------------------------------*/
562 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
564 if (usb_pipeint (urb->pipe)) {
565 int retval;
566 unsigned long flags;
568 spin_lock_irqsave (&hcd_data_lock, flags);
569 retval = rh_status_urb (hcd, urb);
570 spin_unlock_irqrestore (&hcd_data_lock, flags);
571 return retval;
573 if (usb_pipecontrol (urb->pipe))
574 return rh_call_control (hcd, urb);
575 else
576 return -EINVAL;
579 /*-------------------------------------------------------------------------*/
581 int usb_rh_status_dequeue (struct usb_hcd *hcd, struct urb *urb)
583 unsigned long flags;
585 /* note: always a synchronous unlink */
586 del_timer_sync (&hcd->rh_timer);
587 hcd->rh_timer.data = 0;
589 local_irq_save (flags);
590 urb->hcpriv = NULL;
591 usb_hcd_giveback_urb (hcd, urb, NULL);
592 local_irq_restore (flags);
593 return 0;
596 /*-------------------------------------------------------------------------*/
598 /* exported only within usbcore */
599 struct usb_bus *usb_bus_get (struct usb_bus *bus)
601 struct class_device *tmp;
603 if (!bus)
604 return NULL;
606 tmp = class_device_get(&bus->class_dev);
607 if (tmp)
608 return to_usb_bus(tmp);
609 else
610 return NULL;
613 /* exported only within usbcore */
614 void usb_bus_put (struct usb_bus *bus)
616 if (bus)
617 class_device_put(&bus->class_dev);
620 /*-------------------------------------------------------------------------*/
622 static void usb_host_release(struct class_device *class_dev)
624 struct usb_bus *bus = to_usb_bus(class_dev);
626 if (bus->release)
627 bus->release(bus);
630 static struct class usb_host_class = {
631 .name = "usb_host",
632 .release = &usb_host_release,
635 int usb_host_init(void)
637 return class_register(&usb_host_class);
640 void usb_host_cleanup(void)
642 class_unregister(&usb_host_class);
646 * usb_bus_init - shared initialization code
647 * @bus: the bus structure being initialized
649 * This code is used to initialize a usb_bus structure, memory for which is
650 * separately managed.
652 void usb_bus_init (struct usb_bus *bus)
654 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
656 bus->devnum_next = 1;
658 bus->root_hub = NULL;
659 bus->hcpriv = NULL;
660 bus->busnum = -1;
661 bus->bandwidth_allocated = 0;
662 bus->bandwidth_int_reqs = 0;
663 bus->bandwidth_isoc_reqs = 0;
665 INIT_LIST_HEAD (&bus->bus_list);
667 EXPORT_SYMBOL (usb_bus_init);
670 * usb_alloc_bus - creates a new USB host controller structure
671 * @op: pointer to a struct usb_operations that this bus structure should use
672 * Context: !in_interrupt()
674 * Creates a USB host controller bus structure with the specified
675 * usb_operations and initializes all the necessary internal objects.
677 * If no memory is available, NULL is returned.
679 * The caller should call usb_put_bus() when it is finished with the structure.
681 struct usb_bus *usb_alloc_bus (struct usb_operations *op)
683 struct usb_bus *bus;
685 bus = kmalloc (sizeof *bus, GFP_KERNEL);
686 if (!bus)
687 return NULL;
688 memset(bus, 0, sizeof(struct usb_bus));
689 usb_bus_init (bus);
690 bus->op = op;
691 return bus;
693 EXPORT_SYMBOL (usb_alloc_bus);
695 /*-------------------------------------------------------------------------*/
698 * usb_register_bus - registers the USB host controller with the usb core
699 * @bus: pointer to the bus to register
700 * Context: !in_interrupt()
702 * Assigns a bus number, and links the controller into usbcore data
703 * structures so that it can be seen by scanning the bus list.
705 int usb_register_bus(struct usb_bus *bus)
707 int busnum;
708 int retval;
710 down (&usb_bus_list_lock);
711 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
712 if (busnum < USB_MAXBUS) {
713 set_bit (busnum, busmap.busmap);
714 bus->busnum = busnum;
715 } else {
716 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
717 up(&usb_bus_list_lock);
718 return -E2BIG;
721 snprintf(bus->class_dev.class_id, BUS_ID_SIZE, "usb%d", busnum);
722 bus->class_dev.class = &usb_host_class;
723 bus->class_dev.dev = bus->controller;
724 retval = class_device_register(&bus->class_dev);
725 if (retval) {
726 clear_bit(busnum, busmap.busmap);
727 up(&usb_bus_list_lock);
728 return retval;
731 /* Add it to the local list of buses */
732 list_add (&bus->bus_list, &usb_bus_list);
733 up (&usb_bus_list_lock);
735 usbfs_add_bus (bus);
737 dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
738 return 0;
740 EXPORT_SYMBOL (usb_register_bus);
743 * usb_deregister_bus - deregisters the USB host controller
744 * @bus: pointer to the bus to deregister
745 * Context: !in_interrupt()
747 * Recycles the bus number, and unlinks the controller from usbcore data
748 * structures so that it won't be seen by scanning the bus list.
750 void usb_deregister_bus (struct usb_bus *bus)
752 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
755 * NOTE: make sure that all the devices are removed by the
756 * controller code, as well as having it call this when cleaning
757 * itself up
759 down (&usb_bus_list_lock);
760 list_del (&bus->bus_list);
761 up (&usb_bus_list_lock);
763 usbfs_remove_bus (bus);
765 clear_bit (bus->busnum, busmap.busmap);
767 class_device_unregister(&bus->class_dev);
769 EXPORT_SYMBOL (usb_deregister_bus);
772 * usb_register_root_hub - called by HCD to register its root hub
773 * @usb_dev: the usb root hub device to be registered.
774 * @parent_dev: the parent device of this root hub.
776 * The USB host controller calls this function to register the root hub
777 * properly with the USB subsystem. It sets up the device properly in
778 * the device tree and stores the root_hub pointer in the bus structure,
779 * then calls usb_new_device() to register the usb device. It also
780 * assigns the root hub's USB address (always 1).
782 int usb_register_root_hub (struct usb_device *usb_dev, struct device *parent_dev)
784 const int devnum = 1;
785 int retval;
787 usb_dev->devnum = devnum;
788 usb_dev->bus->devnum_next = devnum + 1;
789 memset (&usb_dev->bus->devmap.devicemap, 0,
790 sizeof usb_dev->bus->devmap.devicemap);
791 set_bit (devnum, usb_dev->bus->devmap.devicemap);
792 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
794 down (&usb_bus_list_lock);
795 usb_dev->bus->root_hub = usb_dev;
797 usb_dev->epmaxpacketin[0] = usb_dev->epmaxpacketout[0] = 64;
798 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
799 if (retval != sizeof usb_dev->descriptor) {
800 usb_dev->bus->root_hub = NULL;
801 up (&usb_bus_list_lock);
802 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
803 usb_dev->dev.bus_id, retval);
804 return (retval < 0) ? retval : -EMSGSIZE;
807 down (&usb_dev->serialize);
808 retval = usb_new_device (usb_dev);
809 up (&usb_dev->serialize);
810 if (retval) {
811 usb_dev->bus->root_hub = NULL;
812 dev_err (parent_dev, "can't register root hub for %s, %d\n",
813 usb_dev->dev.bus_id, retval);
815 up (&usb_bus_list_lock);
816 return retval;
818 EXPORT_SYMBOL (usb_register_root_hub);
821 /*-------------------------------------------------------------------------*/
824 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
825 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
826 * @is_input: true iff the transaction sends data to the host
827 * @isoc: true for isochronous transactions, false for interrupt ones
828 * @bytecount: how many bytes in the transaction.
830 * Returns approximate bus time in nanoseconds for a periodic transaction.
831 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
832 * scheduled in software, this function is only used for such scheduling.
834 long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
836 unsigned long tmp;
838 switch (speed) {
839 case USB_SPEED_LOW: /* INTR only */
840 if (is_input) {
841 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
842 return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
843 } else {
844 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
845 return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
847 case USB_SPEED_FULL: /* ISOC or INTR */
848 if (isoc) {
849 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
850 return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
851 } else {
852 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
853 return (9107L + BW_HOST_DELAY + tmp);
855 case USB_SPEED_HIGH: /* ISOC or INTR */
856 // FIXME adjust for input vs output
857 if (isoc)
858 tmp = HS_USECS (bytecount);
859 else
860 tmp = HS_USECS_ISO (bytecount);
861 return tmp;
862 default:
863 pr_debug ("%s: bogus device speed!\n", usbcore_name);
864 return -1;
867 EXPORT_SYMBOL (usb_calc_bus_time);
870 * usb_check_bandwidth():
872 * old_alloc is from host_controller->bandwidth_allocated in microseconds;
873 * bustime is from calc_bus_time(), but converted to microseconds.
875 * returns <bustime in us> if successful,
876 * or -ENOSPC if bandwidth request fails.
878 * FIXME:
879 * This initial implementation does not use Endpoint.bInterval
880 * in managing bandwidth allocation.
881 * It probably needs to be expanded to use Endpoint.bInterval.
882 * This can be done as a later enhancement (correction).
884 * This will also probably require some kind of
885 * frame allocation tracking...meaning, for example,
886 * that if multiple drivers request interrupts every 10 USB frames,
887 * they don't all have to be allocated at
888 * frame numbers N, N+10, N+20, etc. Some of them could be at
889 * N+11, N+21, N+31, etc., and others at
890 * N+12, N+22, N+32, etc.
892 * Similarly for isochronous transfers...
894 * Individual HCDs can schedule more directly ... this logic
895 * is not correct for high speed transfers.
897 int usb_check_bandwidth (struct usb_device *dev, struct urb *urb)
899 unsigned int pipe = urb->pipe;
900 long bustime;
901 int is_in = usb_pipein (pipe);
902 int is_iso = usb_pipeisoc (pipe);
903 int old_alloc = dev->bus->bandwidth_allocated;
904 int new_alloc;
907 bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso,
908 usb_maxpacket (dev, pipe, !is_in)));
909 if (is_iso)
910 bustime /= urb->number_of_packets;
912 new_alloc = old_alloc + (int) bustime;
913 if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) {
914 #ifdef DEBUG
915 char *mode =
916 #ifdef CONFIG_USB_BANDWIDTH
918 #else
919 "would have ";
920 #endif
921 dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n",
922 mode, old_alloc, bustime, new_alloc);
923 #endif
924 #ifdef CONFIG_USB_BANDWIDTH
925 bustime = -ENOSPC; /* report error */
926 #endif
929 return bustime;
931 EXPORT_SYMBOL (usb_check_bandwidth);
935 * usb_claim_bandwidth - records bandwidth for a periodic transfer
936 * @dev: source/target of request
937 * @urb: request (urb->dev == dev)
938 * @bustime: bandwidth consumed, in (average) microseconds per frame
939 * @isoc: true iff the request is isochronous
941 * Bus bandwidth reservations are recorded purely for diagnostic purposes.
942 * HCDs are expected not to overcommit periodic bandwidth, and to record such
943 * reservations whenever endpoints are added to the periodic schedule.
945 * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's
946 * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable
947 * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how
948 * large its periodic schedule is.
950 void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc)
952 dev->bus->bandwidth_allocated += bustime;
953 if (isoc)
954 dev->bus->bandwidth_isoc_reqs++;
955 else
956 dev->bus->bandwidth_int_reqs++;
957 urb->bandwidth = bustime;
959 #ifdef USB_BANDWIDTH_MESSAGES
960 dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n",
961 bustime,
962 isoc ? "ISOC" : "INTR",
963 dev->bus->bandwidth_allocated,
964 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
965 #endif
967 EXPORT_SYMBOL (usb_claim_bandwidth);
971 * usb_release_bandwidth - reverses effect of usb_claim_bandwidth()
972 * @dev: source/target of request
973 * @urb: request (urb->dev == dev)
974 * @isoc: true iff the request is isochronous
976 * This records that previously allocated bandwidth has been released.
977 * Bandwidth is released when endpoints are removed from the host controller's
978 * periodic schedule.
980 void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc)
982 dev->bus->bandwidth_allocated -= urb->bandwidth;
983 if (isoc)
984 dev->bus->bandwidth_isoc_reqs--;
985 else
986 dev->bus->bandwidth_int_reqs--;
988 #ifdef USB_BANDWIDTH_MESSAGES
989 dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n",
990 urb->bandwidth,
991 isoc ? "ISOC" : "INTR",
992 dev->bus->bandwidth_allocated,
993 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
994 #endif
995 urb->bandwidth = 0;
997 EXPORT_SYMBOL (usb_release_bandwidth);
1000 /*-------------------------------------------------------------------------*/
1003 * Generic HC operations.
1006 /*-------------------------------------------------------------------------*/
1008 /* called from khubd, or root hub init threads for hcd-private init */
1009 static int hcd_alloc_dev (struct usb_device *udev)
1011 struct hcd_dev *dev;
1012 struct usb_hcd *hcd;
1013 unsigned long flags;
1015 if (!udev || udev->hcpriv)
1016 return -EINVAL;
1017 if (!udev->bus || !udev->bus->hcpriv)
1018 return -ENODEV;
1019 hcd = udev->bus->hcpriv;
1020 if (hcd->state == USB_STATE_QUIESCING)
1021 return -ENOLINK;
1023 dev = (struct hcd_dev *) kmalloc (sizeof *dev, GFP_KERNEL);
1024 if (dev == NULL)
1025 return -ENOMEM;
1026 memset (dev, 0, sizeof *dev);
1028 INIT_LIST_HEAD (&dev->dev_list);
1029 INIT_LIST_HEAD (&dev->urb_list);
1031 spin_lock_irqsave (&hcd_data_lock, flags);
1032 list_add (&dev->dev_list, &hcd->dev_list);
1033 // refcount is implicit
1034 udev->hcpriv = dev;
1035 spin_unlock_irqrestore (&hcd_data_lock, flags);
1037 return 0;
1040 /*-------------------------------------------------------------------------*/
1042 static void urb_unlink (struct urb *urb)
1044 unsigned long flags;
1046 /* Release any periodic transfer bandwidth */
1047 if (urb->bandwidth)
1048 usb_release_bandwidth (urb->dev, urb,
1049 usb_pipeisoc (urb->pipe));
1051 /* clear all state linking urb to this dev (and hcd) */
1053 spin_lock_irqsave (&hcd_data_lock, flags);
1054 list_del_init (&urb->urb_list);
1055 spin_unlock_irqrestore (&hcd_data_lock, flags);
1056 usb_put_dev (urb->dev);
1060 /* may be called in any context with a valid urb->dev usecount
1061 * caller surrenders "ownership" of urb
1062 * expects usb_submit_urb() to have sanity checked and conditioned all
1063 * inputs in the urb
1065 static int hcd_submit_urb (struct urb *urb, int mem_flags)
1067 int status;
1068 struct usb_hcd *hcd = urb->dev->bus->hcpriv;
1069 struct hcd_dev *dev = urb->dev->hcpriv;
1070 unsigned long flags;
1072 if (!hcd || !dev)
1073 return -ENODEV;
1076 * FIXME: make urb timeouts be generic, keeping the HCD cores
1077 * as simple as possible.
1080 // NOTE: a generic device/urb monitoring hook would go here.
1081 // hcd_monitor_hook(MONITOR_URB_SUBMIT, urb)
1082 // It would catch submission paths for all urbs.
1085 * Atomically queue the urb, first to our records, then to the HCD.
1086 * Access to urb->status is controlled by urb->lock ... changes on
1087 * i/o completion (normal or fault) or unlinking.
1090 // FIXME: verify that quiescing hc works right (RH cleans up)
1092 spin_lock_irqsave (&hcd_data_lock, flags);
1093 if (unlikely (urb->reject))
1094 status = -EPERM;
1095 else if (HCD_IS_RUNNING (hcd->state) &&
1096 hcd->state != USB_STATE_QUIESCING) {
1097 usb_get_dev (urb->dev);
1098 list_add_tail (&urb->urb_list, &dev->urb_list);
1099 status = 0;
1100 } else
1101 status = -ESHUTDOWN;
1102 spin_unlock_irqrestore (&hcd_data_lock, flags);
1103 if (status) {
1104 INIT_LIST_HEAD (&urb->urb_list);
1105 return status;
1108 /* increment urb's reference count as part of giving it to the HCD
1109 * (which now controls it). HCD guarantees that it either returns
1110 * an error or calls giveback(), but not both.
1112 urb = usb_get_urb (urb);
1113 atomic_inc (&urb->use_count);
1115 if (urb->dev == hcd->self.root_hub) {
1116 /* NOTE: requirement on hub callers (usbfs and the hub
1117 * driver, for now) that URBs' urb->transfer_buffer be
1118 * valid and usb_buffer_{sync,unmap}() not be needed, since
1119 * they could clobber root hub response data.
1121 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1122 | URB_NO_SETUP_DMA_MAP);
1123 status = rh_urb_enqueue (hcd, urb);
1124 goto done;
1127 /* lower level hcd code should use *_dma exclusively,
1128 * unless it uses pio or talks to another transport.
1130 if (hcd->self.controller->dma_mask) {
1131 if (usb_pipecontrol (urb->pipe)
1132 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1133 urb->setup_dma = dma_map_single (
1134 hcd->self.controller,
1135 urb->setup_packet,
1136 sizeof (struct usb_ctrlrequest),
1137 DMA_TO_DEVICE);
1138 if (urb->transfer_buffer_length != 0
1139 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
1140 urb->transfer_dma = dma_map_single (
1141 hcd->self.controller,
1142 urb->transfer_buffer,
1143 urb->transfer_buffer_length,
1144 usb_pipein (urb->pipe)
1145 ? DMA_FROM_DEVICE
1146 : DMA_TO_DEVICE);
1149 status = hcd->driver->urb_enqueue (hcd, urb, mem_flags);
1150 done:
1151 if (unlikely (status)) {
1152 urb_unlink (urb);
1153 atomic_dec (&urb->use_count);
1154 if (urb->reject)
1155 wake_up (&usb_kill_urb_queue);
1156 usb_put_urb (urb);
1158 return status;
1161 /*-------------------------------------------------------------------------*/
1163 /* called in any context */
1164 static int hcd_get_frame_number (struct usb_device *udev)
1166 struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
1167 if (!HCD_IS_RUNNING (hcd->state))
1168 return -ESHUTDOWN;
1169 return hcd->driver->get_frame_number (hcd);
1172 /*-------------------------------------------------------------------------*/
1174 /* this makes the hcd giveback() the urb more quickly, by kicking it
1175 * off hardware queues (which may take a while) and returning it as
1176 * soon as practical. we've already set up the urb's return status,
1177 * but we can't know if the callback completed already.
1179 static int
1180 unlink1 (struct usb_hcd *hcd, struct urb *urb)
1182 int value;
1184 if (urb == (struct urb *) hcd->rh_timer.data)
1185 value = usb_rh_status_dequeue (hcd, urb);
1186 else {
1188 /* The only reason an HCD might fail this call is if
1189 * it has not yet fully queued the urb to begin with.
1190 * Such failures should be harmless. */
1191 value = hcd->driver->urb_dequeue (hcd, urb);
1194 if (value != 0)
1195 dev_dbg (hcd->self.controller, "dequeue %p --> %d\n",
1196 urb, value);
1197 return value;
1201 * called in any context
1203 * caller guarantees urb won't be recycled till both unlink()
1204 * and the urb's completion function return
1206 static int hcd_unlink_urb (struct urb *urb, int status)
1208 struct hcd_dev *dev;
1209 struct usb_hcd *hcd = NULL;
1210 struct device *sys = NULL;
1211 unsigned long flags;
1212 struct list_head *tmp;
1213 int retval;
1215 if (!urb)
1216 return -EINVAL;
1219 * we contend for urb->status with the hcd core,
1220 * which changes it while returning the urb.
1222 * Caller guaranteed that the urb pointer hasn't been freed, and
1223 * that it was submitted. But as a rule it can't know whether or
1224 * not it's already been unlinked ... so we respect the reversed
1225 * lock sequence needed for the usb_hcd_giveback_urb() code paths
1226 * (urb lock, then hcd_data_lock) in case some other CPU is now
1227 * unlinking it.
1229 spin_lock_irqsave (&urb->lock, flags);
1230 spin_lock (&hcd_data_lock);
1232 if (!urb->dev || !urb->dev->bus) {
1233 retval = -ENODEV;
1234 goto done;
1237 dev = urb->dev->hcpriv;
1238 sys = &urb->dev->dev;
1239 hcd = urb->dev->bus->hcpriv;
1240 if (!dev || !hcd) {
1241 retval = -ENODEV;
1242 goto done;
1245 /* running ~= hc unlink handshake works (irq, timer, etc)
1246 * halted ~= no unlink handshake is needed
1247 * suspended, resuming == should never happen
1249 WARN_ON (!HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_HALT);
1251 /* insist the urb is still queued */
1252 list_for_each(tmp, &dev->urb_list) {
1253 if (tmp == &urb->urb_list)
1254 break;
1256 if (tmp != &urb->urb_list) {
1257 retval = -EIDRM;
1258 goto done;
1261 /* Any status except -EINPROGRESS means something already started to
1262 * unlink this URB from the hardware. So there's no more work to do.
1264 if (urb->status != -EINPROGRESS) {
1265 retval = -EBUSY;
1266 goto done;
1269 /* PCI IRQ setup can easily be broken so that USB controllers
1270 * never get completion IRQs ... maybe even the ones we need to
1271 * finish unlinking the initial failed usb_set_address().
1273 if (!hcd->saw_irq) {
1274 dev_warn (hcd->self.controller, "Unlink after no-IRQ? "
1275 "Different ACPI or APIC settings may help."
1276 "\n");
1277 hcd->saw_irq = 1;
1280 urb->status = status;
1282 spin_unlock (&hcd_data_lock);
1283 spin_unlock_irqrestore (&urb->lock, flags);
1285 retval = unlink1 (hcd, urb);
1286 if (retval == 0)
1287 retval = -EINPROGRESS;
1288 return retval;
1290 done:
1291 spin_unlock (&hcd_data_lock);
1292 spin_unlock_irqrestore (&urb->lock, flags);
1293 if (retval != -EIDRM && sys && sys->driver)
1294 dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval);
1295 return retval;
1298 /*-------------------------------------------------------------------------*/
1300 /* disables the endpoint: cancels any pending urbs, then synchronizes with
1301 * the hcd to make sure all endpoint state is gone from hardware. use for
1302 * set_configuration, set_interface, driver removal, physical disconnect.
1304 * example: a qh stored in hcd_dev.ep[], holding state related to endpoint
1305 * type, maxpacket size, toggle, halt status, and scheduling.
1307 static void hcd_endpoint_disable (struct usb_device *udev, int endpoint)
1309 struct hcd_dev *dev;
1310 struct usb_hcd *hcd;
1311 struct urb *urb;
1312 unsigned epnum = endpoint & USB_ENDPOINT_NUMBER_MASK;
1314 dev = udev->hcpriv;
1315 hcd = udev->bus->hcpriv;
1317 WARN_ON (!HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_HALT);
1319 local_irq_disable ();
1321 rescan:
1322 /* (re)block new requests, as best we can */
1323 if (endpoint & USB_DIR_IN)
1324 udev->epmaxpacketin [epnum] = 0;
1325 else
1326 udev->epmaxpacketout [epnum] = 0;
1328 /* then kill any current requests */
1329 spin_lock (&hcd_data_lock);
1330 list_for_each_entry (urb, &dev->urb_list, urb_list) {
1331 int tmp = urb->pipe;
1333 /* ignore urbs for other endpoints */
1334 if (usb_pipeendpoint (tmp) != epnum)
1335 continue;
1336 /* NOTE assumption that only ep0 is a control endpoint */
1337 if (epnum != 0 && ((tmp ^ endpoint) & USB_DIR_IN))
1338 continue;
1340 /* another cpu may be in hcd, spinning on hcd_data_lock
1341 * to giveback() this urb. the races here should be
1342 * small, but a full fix needs a new "can't submit"
1343 * urb state.
1345 if (urb->status != -EINPROGRESS)
1346 continue;
1347 usb_get_urb (urb);
1348 spin_unlock (&hcd_data_lock);
1350 spin_lock (&urb->lock);
1351 tmp = urb->status;
1352 if (tmp == -EINPROGRESS)
1353 urb->status = -ESHUTDOWN;
1354 spin_unlock (&urb->lock);
1356 /* kick hcd unless it's already returning this */
1357 if (tmp == -EINPROGRESS) {
1358 tmp = urb->pipe;
1359 unlink1 (hcd, urb);
1360 dev_dbg (hcd->self.controller,
1361 "shutdown urb %p pipe %08x ep%d%s%s\n",
1362 urb, tmp, usb_pipeendpoint (tmp),
1363 (tmp & USB_DIR_IN) ? "in" : "out",
1364 ({ char *s; \
1365 switch (usb_pipetype (tmp)) { \
1366 case PIPE_CONTROL: s = ""; break; \
1367 case PIPE_BULK: s = "-bulk"; break; \
1368 case PIPE_INTERRUPT: s = "-intr"; break; \
1369 default: s = "-iso"; break; \
1370 }; s;}));
1372 usb_put_urb (urb);
1374 /* list contents may have changed */
1375 goto rescan;
1377 spin_unlock (&hcd_data_lock);
1378 local_irq_enable ();
1380 /* synchronize with the hardware, so old configuration state
1381 * clears out immediately (and will be freed).
1383 might_sleep ();
1384 if (hcd->driver->endpoint_disable)
1385 hcd->driver->endpoint_disable (hcd, dev, endpoint);
1388 /*-------------------------------------------------------------------------*/
1390 #ifdef CONFIG_USB_SUSPEND
1392 static int hcd_hub_suspend (struct usb_bus *bus)
1394 struct usb_hcd *hcd;
1396 hcd = container_of (bus, struct usb_hcd, self);
1397 if (hcd->driver->hub_suspend)
1398 return hcd->driver->hub_suspend (hcd);
1399 return 0;
1402 static int hcd_hub_resume (struct usb_bus *bus)
1404 struct usb_hcd *hcd;
1406 hcd = container_of (bus, struct usb_hcd, self);
1407 if (hcd->driver->hub_resume)
1408 return hcd->driver->hub_resume (hcd);
1409 return 0;
1412 #endif
1414 /*-------------------------------------------------------------------------*/
1416 #ifdef CONFIG_USB_OTG
1419 * usb_bus_start_enum - start immediate enumeration (for OTG)
1420 * @bus: the bus (must use hcd framework)
1421 * @port: 1-based number of port; usually bus->otg_port
1422 * Context: in_interrupt()
1424 * Starts enumeration, with an immediate reset followed later by
1425 * khubd identifying and possibly configuring the device.
1426 * This is needed by OTG controller drivers, where it helps meet
1427 * HNP protocol timing requirements for starting a port reset.
1429 int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
1431 struct usb_hcd *hcd;
1432 int status = -EOPNOTSUPP;
1434 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
1435 * boards with root hubs hooked up to internal devices (instead of
1436 * just the OTG port) may need more attention to resetting...
1438 hcd = container_of (bus, struct usb_hcd, self);
1439 if (port_num && hcd->driver->start_port_reset)
1440 status = hcd->driver->start_port_reset(hcd, port_num);
1442 /* run khubd shortly after (first) root port reset finishes;
1443 * it may issue others, until at least 50 msecs have passed.
1445 if (status == 0)
1446 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
1447 return status;
1449 EXPORT_SYMBOL (usb_bus_start_enum);
1451 #endif
1453 /*-------------------------------------------------------------------------*/
1455 /* called by khubd, rmmod, apmd, or other thread for hcd-private cleanup.
1456 * we're guaranteed that the device is fully quiesced. also, that each
1457 * endpoint has been hcd_endpoint_disabled.
1460 static int hcd_free_dev (struct usb_device *udev)
1462 struct hcd_dev *dev;
1463 struct usb_hcd *hcd;
1464 unsigned long flags;
1466 if (!udev || !udev->hcpriv)
1467 return -EINVAL;
1469 if (!udev->bus || !udev->bus->hcpriv)
1470 return -ENODEV;
1472 // should udev->devnum == -1 ??
1474 dev = udev->hcpriv;
1475 hcd = udev->bus->hcpriv;
1477 /* device driver problem with refcounts? */
1478 if (!list_empty (&dev->urb_list)) {
1479 dev_dbg (hcd->self.controller, "free busy dev, %s devnum %d (bug!)\n",
1480 hcd->self.bus_name, udev->devnum);
1481 return -EINVAL;
1484 spin_lock_irqsave (&hcd_data_lock, flags);
1485 list_del (&dev->dev_list);
1486 udev->hcpriv = NULL;
1487 spin_unlock_irqrestore (&hcd_data_lock, flags);
1489 kfree (dev);
1490 return 0;
1494 * usb_hcd_operations - adapts usb_bus framework to HCD framework (bus glue)
1496 * When registering a USB bus through the HCD framework code, use this
1497 * usb_operations vector. The PCI glue layer does so automatically; only
1498 * bus glue for non-PCI system busses will need to use this.
1500 struct usb_operations usb_hcd_operations = {
1501 .allocate = hcd_alloc_dev,
1502 .get_frame_number = hcd_get_frame_number,
1503 .submit_urb = hcd_submit_urb,
1504 .unlink_urb = hcd_unlink_urb,
1505 .deallocate = hcd_free_dev,
1506 .buffer_alloc = hcd_buffer_alloc,
1507 .buffer_free = hcd_buffer_free,
1508 .disable = hcd_endpoint_disable,
1509 #ifdef CONFIG_USB_SUSPEND
1510 .hub_suspend = hcd_hub_suspend,
1511 .hub_resume = hcd_hub_resume,
1512 #endif
1514 EXPORT_SYMBOL (usb_hcd_operations);
1516 /*-------------------------------------------------------------------------*/
1519 * usb_hcd_giveback_urb - return URB from HCD to device driver
1520 * @hcd: host controller returning the URB
1521 * @urb: urb being returned to the USB device driver.
1522 * @regs: pt_regs, passed down to the URB completion handler
1523 * Context: in_interrupt()
1525 * This hands the URB from HCD to its USB device driver, using its
1526 * completion function. The HCD has freed all per-urb resources
1527 * (and is done using urb->hcpriv). It also released all HCD locks;
1528 * the device driver won't cause problems if it frees, modifies,
1529 * or resubmits this URB.
1531 void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
1533 urb_unlink (urb);
1535 // NOTE: a generic device/urb monitoring hook would go here.
1536 // hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev)
1537 // It would catch exit/unlink paths for all urbs.
1539 /* lower level hcd code should use *_dma exclusively */
1540 if (hcd->self.controller->dma_mask) {
1541 if (usb_pipecontrol (urb->pipe)
1542 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1543 dma_unmap_single (hcd->self.controller, urb->setup_dma,
1544 sizeof (struct usb_ctrlrequest),
1545 DMA_TO_DEVICE);
1546 if (urb->transfer_buffer_length != 0
1547 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
1548 dma_unmap_single (hcd->self.controller,
1549 urb->transfer_dma,
1550 urb->transfer_buffer_length,
1551 usb_pipein (urb->pipe)
1552 ? DMA_FROM_DEVICE
1553 : DMA_TO_DEVICE);
1556 /* pass ownership to the completion handler */
1557 urb->complete (urb, regs);
1558 atomic_dec (&urb->use_count);
1559 if (unlikely (urb->reject))
1560 wake_up (&usb_kill_urb_queue);
1561 usb_put_urb (urb);
1563 EXPORT_SYMBOL (usb_hcd_giveback_urb);
1565 /*-------------------------------------------------------------------------*/
1568 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
1569 * @irq: the IRQ being raised
1570 * @__hcd: pointer to the HCD whose IRQ is beinng signaled
1571 * @r: saved hardware registers
1573 * When registering a USB bus through the HCD framework code, use this
1574 * to handle interrupts. The PCI glue layer does so automatically; only
1575 * bus glue for non-PCI system busses will need to use this.
1577 irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r)
1579 struct usb_hcd *hcd = __hcd;
1580 int start = hcd->state;
1582 if (unlikely (hcd->state == USB_STATE_HALT)) /* irq sharing? */
1583 return IRQ_NONE;
1585 hcd->saw_irq = 1;
1586 if (hcd->driver->irq (hcd, r) == IRQ_NONE)
1587 return IRQ_NONE;
1589 if (hcd->state != start && hcd->state == USB_STATE_HALT) {
1590 usb_hc_died (hcd);
1592 return IRQ_HANDLED;
1594 EXPORT_SYMBOL (usb_hcd_irq);
1596 /*-------------------------------------------------------------------------*/
1598 static void hcd_panic (void *_hcd)
1600 struct usb_hcd *hcd = _hcd;
1601 struct usb_device *hub = hcd->self.root_hub;
1602 unsigned i;
1604 /* hc's root hub is removed later removed in hcd->stop() */
1605 down (&hub->serialize);
1606 usb_set_device_state(hub, USB_STATE_NOTATTACHED);
1607 for (i = 0; i < hub->maxchild; i++) {
1608 if (hub->children [i])
1609 usb_disconnect (&hub->children [i]);
1611 up (&hub->serialize);
1615 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
1616 * @hcd: pointer to the HCD representing the controller
1618 * This is called by bus glue to report a USB host controller that died
1619 * while operations may still have been pending. It's called automatically
1620 * by the PCI glue, so only glue for non-PCI busses should need to call it.
1622 void usb_hc_died (struct usb_hcd *hcd)
1624 dev_err (hcd->self.controller, "HC died; cleaning up\n");
1626 /* clean up old urbs and devices; needs a task context */
1627 INIT_WORK (&hcd->work, hcd_panic, hcd);
1628 (void) schedule_work (&hcd->work);
1630 EXPORT_SYMBOL (usb_hc_died);