debuggers.hg

view linux-2.4.29-xen-sparse/drivers/usb/hcd.c @ 3608:9bef38a51377

bitkeeper revision 1.1159.212.47 (41f97f66xOoJsNNqdPQ2dvZmBkFoHw)

Import hcd.c from vanilla Linux. Required a couple of ifdefs to
make 2.4 xenU domains compile with USB support.

Signed-off-by: mark.williamson@cl.cam.ac.uk
author mwilli2@equilibrium.research
date Thu Jan 27 23:55:18 2005 +0000 (2005-01-27)
parents
children e8d6036117fd
line source
1 /*
2 * Copyright (c) 2001-2002 by David Brownell
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/ioport.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/smp_lock.h>
28 #include <linux/errno.h>
29 #include <linux/kmod.h>
30 #include <linux/init.h>
31 #include <linux/timer.h>
32 #include <linux/list.h>
33 #include <linux/interrupt.h>
34 #include <linux/completion.h>
35 #include <linux/uts.h> /* for UTS_SYSNAME */
38 #ifdef CONFIG_USB_DEBUG
39 #define DEBUG
40 #else
41 #undef DEBUG
42 #endif
44 #include <linux/usb.h>
45 #include "hcd.h"
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/system.h>
50 #include <asm/unaligned.h>
53 /*-------------------------------------------------------------------------*/
55 /*
56 * USB Host Controller Driver framework
57 *
58 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
59 * HCD-specific behaviors/bugs. Think of it as the "upper level" of
60 * some drivers, where the "lower level" is hardware-specific.
61 *
62 * This does error checks, tracks devices and urbs, and delegates to a
63 * "hc_driver" only for code (and data) that really needs to know about
64 * hardware differences. That includes root hub registers, i/o queues,
65 * and so on ... but as little else as possible.
66 *
67 * Shared code includes most of the "root hub" code (these are emulated,
68 * though each HC's hardware works differently) and PCI glue, plus request
69 * tracking overhead. The HCD code should only block on spinlocks or on
70 * hardware handshaking; blocking on software events (such as other kernel
71 * threads releasing resources, or completing actions) is all generic.
72 *
73 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
74 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
75 * only by the hub driver ... and that neither should be seen or used by
76 * usb client device drivers.
77 *
78 * Contributors of ideas or unattributed patches include: David Brownell,
79 * Roman Weissgaerber, Rory Bolt, ...
80 *
81 * HISTORY:
82 * 2002-sept Merge some 2.5 updates so we can share hardware level HCD
83 * code between the 2.4.20+ and 2.5 trees.
84 * 2002-feb merge to 2.4.19
85 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
86 */
88 /*-------------------------------------------------------------------------*/
90 /* host controllers we manage */
91 static LIST_HEAD (hcd_list);
93 /* used when updating list of hcds */
94 static DECLARE_MUTEX (hcd_list_lock);
96 /* used when updating hcd data */
97 static spinlock_t hcd_data_lock = SPIN_LOCK_UNLOCKED;
99 static struct usb_operations hcd_operations;
101 /*-------------------------------------------------------------------------*/
103 /*
104 * Sharable chunks of root hub code.
105 */
107 /*-------------------------------------------------------------------------*/
109 #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
110 #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
112 /* usb 2.0 root hub device descriptor */
113 static const u8 usb2_rh_dev_descriptor [18] = {
114 0x12, /* __u8 bLength; */
115 0x01, /* __u8 bDescriptorType; Device */
116 0x00, 0x02, /* __u16 bcdUSB; v2.0 */
118 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
119 0x00, /* __u8 bDeviceSubClass; */
120 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/
121 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
123 0x00, 0x00, /* __u16 idVendor; */
124 0x00, 0x00, /* __u16 idProduct; */
125 KERNEL_VER, KERNEL_REL, /* __u16 bcdDevice */
127 0x03, /* __u8 iManufacturer; */
128 0x02, /* __u8 iProduct; */
129 0x01, /* __u8 iSerialNumber; */
130 0x01 /* __u8 bNumConfigurations; */
131 };
133 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
135 /* usb 1.1 root hub device descriptor */
136 static const u8 usb11_rh_dev_descriptor [18] = {
137 0x12, /* __u8 bLength; */
138 0x01, /* __u8 bDescriptorType; Device */
139 0x10, 0x01, /* __u16 bcdUSB; v1.1 */
141 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
142 0x00, /* __u8 bDeviceSubClass; */
143 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
144 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
146 0x00, 0x00, /* __u16 idVendor; */
147 0x00, 0x00, /* __u16 idProduct; */
148 KERNEL_VER, KERNEL_REL, /* __u16 bcdDevice */
150 0x03, /* __u8 iManufacturer; */
151 0x02, /* __u8 iProduct; */
152 0x01, /* __u8 iSerialNumber; */
153 0x01 /* __u8 bNumConfigurations; */
154 };
157 /*-------------------------------------------------------------------------*/
159 /* Configuration descriptors for our root hubs */
161 static const u8 fs_rh_config_descriptor [] = {
163 /* one configuration */
164 0x09, /* __u8 bLength; */
165 0x02, /* __u8 bDescriptorType; Configuration */
166 0x19, 0x00, /* __u16 wTotalLength; */
167 0x01, /* __u8 bNumInterfaces; (1) */
168 0x01, /* __u8 bConfigurationValue; */
169 0x00, /* __u8 iConfiguration; */
170 0x40, /* __u8 bmAttributes;
171 Bit 7: Bus-powered,
172 6: Self-powered,
173 5 Remote-wakwup,
174 4..0: resvd */
175 0x00, /* __u8 MaxPower; */
177 /* USB 1.1:
178 * USB 2.0, single TT organization (mandatory):
179 * one interface, protocol 0
180 *
181 * USB 2.0, multiple TT organization (optional):
182 * two interfaces, protocols 1 (like single TT)
183 * and 2 (multiple TT mode) ... config is
184 * sometimes settable
185 * NOT IMPLEMENTED
186 */
188 /* one interface */
189 0x09, /* __u8 if_bLength; */
190 0x04, /* __u8 if_bDescriptorType; Interface */
191 0x00, /* __u8 if_bInterfaceNumber; */
192 0x00, /* __u8 if_bAlternateSetting; */
193 0x01, /* __u8 if_bNumEndpoints; */
194 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
195 0x00, /* __u8 if_bInterfaceSubClass; */
196 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
197 0x00, /* __u8 if_iInterface; */
199 /* one endpoint (status change endpoint) */
200 0x07, /* __u8 ep_bLength; */
201 0x05, /* __u8 ep_bDescriptorType; Endpoint */
202 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
203 0x03, /* __u8 ep_bmAttributes; Interrupt */
204 0x02, 0x00, /* __u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
205 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
206 };
208 static const u8 hs_rh_config_descriptor [] = {
210 /* one configuration */
211 0x09, /* __u8 bLength; */
212 0x02, /* __u8 bDescriptorType; Configuration */
213 0x19, 0x00, /* __u16 wTotalLength; */
214 0x01, /* __u8 bNumInterfaces; (1) */
215 0x01, /* __u8 bConfigurationValue; */
216 0x00, /* __u8 iConfiguration; */
217 0x40, /* __u8 bmAttributes;
218 Bit 7: Bus-powered,
219 6: Self-powered,
220 5 Remote-wakwup,
221 4..0: resvd */
222 0x00, /* __u8 MaxPower; */
224 /* USB 1.1:
225 * USB 2.0, single TT organization (mandatory):
226 * one interface, protocol 0
227 *
228 * USB 2.0, multiple TT organization (optional):
229 * two interfaces, protocols 1 (like single TT)
230 * and 2 (multiple TT mode) ... config is
231 * sometimes settable
232 * NOT IMPLEMENTED
233 */
235 /* one interface */
236 0x09, /* __u8 if_bLength; */
237 0x04, /* __u8 if_bDescriptorType; Interface */
238 0x00, /* __u8 if_bInterfaceNumber; */
239 0x00, /* __u8 if_bAlternateSetting; */
240 0x01, /* __u8 if_bNumEndpoints; */
241 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
242 0x00, /* __u8 if_bInterfaceSubClass; */
243 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
244 0x00, /* __u8 if_iInterface; */
246 /* one endpoint (status change endpoint) */
247 0x07, /* __u8 ep_bLength; */
248 0x05, /* __u8 ep_bDescriptorType; Endpoint */
249 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
250 0x03, /* __u8 ep_bmAttributes; Interrupt */
251 0x02, 0x00, /* __u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
252 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
253 };
255 /*-------------------------------------------------------------------------*/
257 /*
258 * helper routine for returning string descriptors in UTF-16LE
259 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
260 */
261 static int ascii2utf (char *s, u8 *utf, int utfmax)
262 {
263 int retval;
265 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
266 *utf++ = *s++;
267 *utf++ = 0;
268 }
269 return retval;
270 }
272 /*
273 * rh_string - provides manufacturer, product and serial strings for root hub
274 * @id: the string ID number (1: serial number, 2: product, 3: vendor)
275 * @pci_desc: PCI device descriptor for the relevant HC
276 * @type: string describing our driver
277 * @data: return packet in UTF-16 LE
278 * @len: length of the return packet
279 *
280 * Produces either a manufacturer, product or serial number string for the
281 * virtual root hub device.
282 */
283 static int rh_string (
284 int id,
285 struct usb_hcd *hcd,
286 u8 *data,
287 int len
288 ) {
289 char buf [100];
291 // language ids
292 if (id == 0) {
293 *data++ = 4; *data++ = 3; /* 4 bytes string data */
294 *data++ = 0; *data++ = 0; /* some language id */
295 return 4;
297 // serial number
298 } else if (id == 1) {
299 strcpy (buf, hcd->bus->bus_name);
301 // product description
302 } else if (id == 2) {
303 strcpy (buf, hcd->product_desc);
305 // id 3 == vendor description
306 } else if (id == 3) {
307 sprintf (buf, "%s %s %s", UTS_SYSNAME, UTS_RELEASE,
308 hcd->description);
310 // unsupported IDs --> "protocol stall"
311 } else
312 return 0;
314 data [0] = 2 * (strlen (buf) + 1);
315 data [1] = 3; /* type == string */
316 return 2 + ascii2utf (buf, data + 2, len - 2);
317 }
320 /* Root hub control transfers execute synchronously */
321 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
322 {
323 struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *) urb->setup_packet;
324 u16 typeReq, wValue, wIndex, wLength;
325 const u8 *bufp = 0;
326 u8 *ubuf = urb->transfer_buffer;
327 int len = 0;
329 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
330 wValue = le16_to_cpu (cmd->wValue);
331 wIndex = le16_to_cpu (cmd->wIndex);
332 wLength = le16_to_cpu (cmd->wLength);
334 if (wLength > urb->transfer_buffer_length)
335 goto error;
337 /* set up for success */
338 urb->status = 0;
339 urb->actual_length = wLength;
340 switch (typeReq) {
342 /* DEVICE REQUESTS */
344 case DeviceRequest | USB_REQ_GET_STATUS:
345 // DEVICE_REMOTE_WAKEUP
346 ubuf [0] = 1; // selfpowered
347 ubuf [1] = 0;
348 /* FALLTHROUGH */
349 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
350 case DeviceOutRequest | USB_REQ_SET_FEATURE:
351 dbg ("no device features yet yet");
352 break;
353 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
354 ubuf [0] = 1;
355 /* FALLTHROUGH */
356 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
357 break;
358 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
359 switch (wValue & 0xff00) {
360 case USB_DT_DEVICE << 8:
361 if (hcd->driver->flags & HCD_USB2)
362 bufp = usb2_rh_dev_descriptor;
363 else if (hcd->driver->flags & HCD_USB11)
364 bufp = usb11_rh_dev_descriptor;
365 else
366 goto error;
367 len = 18;
368 break;
369 case USB_DT_CONFIG << 8:
370 if (hcd->driver->flags & HCD_USB2) {
371 bufp = hs_rh_config_descriptor;
372 len = sizeof hs_rh_config_descriptor;
373 } else {
374 bufp = fs_rh_config_descriptor;
375 len = sizeof fs_rh_config_descriptor;
376 }
377 break;
378 case USB_DT_STRING << 8:
379 urb->actual_length = rh_string (
380 wValue & 0xff, hcd,
381 ubuf, wLength);
382 break;
383 default:
384 goto error;
385 }
386 break;
387 case DeviceRequest | USB_REQ_GET_INTERFACE:
388 ubuf [0] = 0;
389 /* FALLTHROUGH */
390 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
391 break;
392 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
393 // wValue == urb->dev->devaddr
394 dbg ("%s root hub device address %d",
395 hcd->bus->bus_name, wValue);
396 break;
398 /* INTERFACE REQUESTS (no defined feature/status flags) */
400 /* ENDPOINT REQUESTS */
402 case EndpointRequest | USB_REQ_GET_STATUS:
403 // ENDPOINT_HALT flag
404 ubuf [0] = 0;
405 ubuf [1] = 0;
406 /* FALLTHROUGH */
407 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
408 case EndpointOutRequest | USB_REQ_SET_FEATURE:
409 dbg ("no endpoint features yet");
410 break;
412 /* CLASS REQUESTS (and errors) */
414 default:
415 /* non-generic request */
416 urb->status = hcd->driver->hub_control (hcd,
417 typeReq, wValue, wIndex,
418 ubuf, wLength);
419 break;
420 error:
421 /* "protocol stall" on error */
422 urb->status = -EPIPE;
423 dbg ("unsupported hub control message (maxchild %d)",
424 urb->dev->maxchild);
425 }
426 if (urb->status) {
427 urb->actual_length = 0;
428 dbg ("CTRL: TypeReq=0x%x val=0x%x idx=0x%x len=%d ==> %d",
429 typeReq, wValue, wIndex, wLength, urb->status);
430 }
431 if (bufp) {
432 if (urb->transfer_buffer_length < len)
433 len = urb->transfer_buffer_length;
434 urb->actual_length = len;
435 // always USB_DIR_IN, toward host
436 memcpy (ubuf, bufp, len);
437 }
439 /* any errors get returned through the urb completion */
440 usb_hcd_giveback_urb (hcd, urb, 0);
441 return 0;
442 }
444 /*-------------------------------------------------------------------------*/
446 /*
447 * Root Hub interrupt transfers are synthesized with a timer.
448 * Completions are called in_interrupt() but not in_irq().
449 */
451 static void rh_report_status (unsigned long ptr);
453 static int rh_status_urb (struct usb_hcd *hcd, struct urb *urb)
454 {
455 int len = 1 + (urb->dev->maxchild / 8);
457 /* rh_timer protected by hcd_data_lock */
458 if (timer_pending (&hcd->rh_timer)
459 || urb->status != -EINPROGRESS
460 || !HCD_IS_RUNNING (hcd->state)
461 || urb->transfer_buffer_length < len) {
462 dbg ("not queuing status urb, stat %d", urb->status);
463 return -EINVAL;
464 }
466 urb->hcpriv = hcd; /* nonzero to indicate it's queued */
467 init_timer (&hcd->rh_timer);
468 hcd->rh_timer.function = rh_report_status;
469 hcd->rh_timer.data = (unsigned long) urb;
470 /* USB 2.0 spec says 256msec; this is close enough */
471 hcd->rh_timer.expires = jiffies + HZ/4;
472 add_timer (&hcd->rh_timer);
473 return 0;
474 }
476 /* timer callback */
478 static void rh_report_status (unsigned long ptr)
479 {
480 struct urb *urb;
481 struct usb_hcd *hcd;
482 int length;
483 unsigned long flags;
485 urb = (struct urb *) ptr;
486 spin_lock_irqsave (&urb->lock, flags);
487 if (!urb->dev) {
488 spin_unlock_irqrestore (&urb->lock, flags);
489 return;
490 }
492 hcd = urb->dev->bus->hcpriv;
493 if (urb->status == -EINPROGRESS) {
494 if (HCD_IS_RUNNING (hcd->state)) {
495 length = hcd->driver->hub_status_data (hcd,
496 urb->transfer_buffer);
497 spin_unlock_irqrestore (&urb->lock, flags);
498 if (length > 0) {
499 urb->actual_length = length;
500 urb->status = 0;
501 urb->complete (urb);
502 }
503 spin_lock_irqsave (&hcd_data_lock, flags);
504 urb->status = -EINPROGRESS;
505 if (HCD_IS_RUNNING (hcd->state)
506 && rh_status_urb (hcd, urb) != 0) {
507 /* another driver snuck in? */
508 dbg ("%s, can't resubmit roothub status urb?",
509 hcd->bus->bus_name);
510 spin_unlock_irqrestore (&hcd_data_lock, flags);
511 BUG ();
512 }
513 spin_unlock_irqrestore (&hcd_data_lock, flags);
514 } else
515 spin_unlock_irqrestore (&urb->lock, flags);
516 } else {
517 /* this urb's been unlinked */
518 urb->hcpriv = 0;
519 spin_unlock_irqrestore (&urb->lock, flags);
521 usb_hcd_giveback_urb (hcd, urb, 0);
522 }
523 }
525 /*-------------------------------------------------------------------------*/
527 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
528 {
529 if (usb_pipeint (urb->pipe)) {
530 int retval;
531 unsigned long flags;
533 spin_lock_irqsave (&hcd_data_lock, flags);
534 retval = rh_status_urb (hcd, urb);
535 spin_unlock_irqrestore (&hcd_data_lock, flags);
536 return retval;
537 }
538 if (usb_pipecontrol (urb->pipe))
539 return rh_call_control (hcd, urb);
540 else
541 return -EINVAL;
542 }
544 /*-------------------------------------------------------------------------*/
546 static void rh_status_dequeue (struct usb_hcd *hcd, struct urb *urb)
547 {
548 unsigned long flags;
550 spin_lock_irqsave (&hcd_data_lock, flags);
551 del_timer_sync (&hcd->rh_timer);
552 hcd->rh_timer.data = 0;
553 spin_unlock_irqrestore (&hcd_data_lock, flags);
555 /* we rely on RH callback code not unlinking its URB! */
556 usb_hcd_giveback_urb (hcd, urb, 0);
557 }
559 /*-------------------------------------------------------------------------*/
561 #ifdef CONFIG_PCI
563 /* PCI-based HCs are normal, but custom bus glue should be ok */
565 static void hcd_irq (int irq, void *__hcd, struct pt_regs *r);
566 static void hc_died (struct usb_hcd *hcd);
568 /*-------------------------------------------------------------------------*/
570 /* configure so an HC device and id are always provided */
571 /* always called with process context; sleeping is OK */
573 /**
574 * usb_hcd_pci_probe - initialize PCI-based HCDs
575 * @dev: USB Host Controller being probed
576 * @id: pci hotplug id connecting controller to HCD framework
577 * Context: !in_interrupt()
578 *
579 * Allocates basic PCI resources for this USB host controller, and
580 * then invokes the start() method for the HCD associated with it
581 * through the hotplug entry's driver_data.
582 *
583 * Store this function in the HCD's struct pci_driver as probe().
584 */
585 int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
586 {
587 struct hc_driver *driver;
588 unsigned long resource, len;
589 void *base;
590 struct usb_bus *bus;
591 struct usb_hcd *hcd;
592 int retval, region;
593 char buf [8], *bufp = buf;
595 if (!id || !(driver = (struct hc_driver *) id->driver_data))
596 return -EINVAL;
598 if (pci_enable_device (dev) < 0)
599 return -ENODEV;
601 if (!dev->irq) {
602 err ("Found HC with no IRQ. Check BIOS/PCI %s setup!",
603 dev->slot_name);
604 return -ENODEV;
605 }
607 if (driver->flags & HCD_MEMORY) { // EHCI, OHCI
608 region = 0;
609 resource = pci_resource_start (dev, 0);
610 len = pci_resource_len (dev, 0);
611 if (!request_mem_region (resource, len, driver->description)) {
612 dbg ("controller already in use");
613 return -EBUSY;
614 }
615 base = ioremap_nocache (resource, len);
616 if (base == NULL) {
617 dbg ("error mapping memory");
618 retval = -EFAULT;
619 clean_1:
620 release_mem_region (resource, len);
621 err ("init %s fail, %d", dev->slot_name, retval);
622 return retval;
623 }
625 } else { // UHCI
626 resource = len = 0;
627 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
628 if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
629 continue;
631 resource = pci_resource_start (dev, region);
632 len = pci_resource_len (dev, region);
633 if (request_region (resource, len,
634 driver->description))
635 break;
636 }
637 if (region == PCI_ROM_RESOURCE) {
638 dbg ("no i/o regions available");
639 return -EBUSY;
640 }
641 base = (void *) resource;
642 }
644 // driver->start(), later on, will transfer device from
645 // control by SMM/BIOS to control by Linux (if needed)
647 pci_set_master (dev);
648 hcd = driver->hcd_alloc ();
649 if (hcd == NULL){
650 dbg ("hcd alloc fail");
651 retval = -ENOMEM;
652 clean_2:
653 if (driver->flags & HCD_MEMORY) {
654 iounmap (base);
655 goto clean_1;
656 } else {
657 release_region (resource, len);
658 err ("init %s fail, %d", dev->slot_name, retval);
659 return retval;
660 }
661 }
662 pci_set_drvdata(dev, hcd);
663 hcd->driver = driver;
664 hcd->description = driver->description;
665 hcd->pdev = dev;
666 printk (KERN_INFO "%s %s: %s\n",
667 hcd->description, dev->slot_name, dev->name);
669 #ifndef __sparc__
670 sprintf (buf, "%d", dev->irq);
671 #else
672 bufp = __irq_itoa(dev->irq);
673 #endif
674 if (request_irq (dev->irq, hcd_irq, SA_SHIRQ, hcd->description, hcd)
675 != 0) {
676 err ("request interrupt %s failed", bufp);
677 retval = -EBUSY;
678 clean_3:
679 driver->hcd_free (hcd);
680 goto clean_2;
681 }
682 hcd->irq = dev->irq;
684 hcd->regs = base;
685 hcd->region = region;
686 printk (KERN_INFO "%s %s: irq %s, %s %p\n",
687 hcd->description, dev->slot_name, bufp,
688 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
689 base);
691 // FIXME simpler: make "bus" be that data, not pointer to it.
692 // (fixed in 2.5)
693 bus = usb_alloc_bus (&hcd_operations);
694 if (bus == NULL) {
695 dbg ("usb_alloc_bus fail");
696 retval = -ENOMEM;
697 free_irq (dev->irq, hcd);
698 goto clean_3;
699 }
700 hcd->bus = bus;
701 bus->bus_name = dev->slot_name;
702 hcd->product_desc = dev->name;
703 bus->hcpriv = (void *) hcd;
705 INIT_LIST_HEAD (&hcd->dev_list);
706 INIT_LIST_HEAD (&hcd->hcd_list);
708 down (&hcd_list_lock);
709 list_add (&hcd->hcd_list, &hcd_list);
710 up (&hcd_list_lock);
712 usb_register_bus (bus);
714 if ((retval = driver->start (hcd)) < 0)
715 usb_hcd_pci_remove (dev);
717 return retval;
718 }
719 EXPORT_SYMBOL (usb_hcd_pci_probe);
722 /* may be called without controller electrically present */
723 /* may be called with controller, bus, and devices active */
725 /**
726 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
727 * @dev: USB Host Controller being removed
728 * Context: !in_interrupt()
729 *
730 * Reverses the effect of usb_hcd_pci_probe(), first invoking
731 * the HCD's stop() method. It is always called from a thread
732 * context, normally "rmmod", "apmd", or something similar.
733 *
734 * Store this function in the HCD's struct pci_driver as remove().
735 */
736 void usb_hcd_pci_remove (struct pci_dev *dev)
737 {
738 struct usb_hcd *hcd;
739 struct usb_device *hub;
741 hcd = pci_get_drvdata(dev);
742 if (!hcd)
743 return;
744 printk (KERN_INFO "%s %s: remove state %x\n",
745 hcd->description, dev->slot_name, hcd->state);
747 if (in_interrupt ()) BUG ();
749 hub = hcd->bus->root_hub;
750 hcd->state = USB_STATE_QUIESCING;
752 dbg ("%s: roothub graceful disconnect", hcd->bus->bus_name);
753 usb_disconnect (&hub);
754 // usb_disconnect (&hcd->bus->root_hub);
756 hcd->driver->stop (hcd);
757 hcd->state = USB_STATE_HALT;
759 free_irq (hcd->irq, hcd);
760 if (hcd->driver->flags & HCD_MEMORY) {
761 iounmap (hcd->regs);
762 release_mem_region (pci_resource_start (dev, 0),
763 pci_resource_len (dev, 0));
764 } else {
765 release_region (pci_resource_start (dev, hcd->region),
766 pci_resource_len (dev, hcd->region));
767 }
769 down (&hcd_list_lock);
770 list_del (&hcd->hcd_list);
771 up (&hcd_list_lock);
773 usb_deregister_bus (hcd->bus);
774 usb_free_bus (hcd->bus);
775 hcd->bus = NULL;
777 hcd->driver->hcd_free (hcd);
778 }
779 EXPORT_SYMBOL (usb_hcd_pci_remove);
782 #ifdef CONFIG_PM
784 /*
785 * Some "sleep" power levels imply updating struct usb_driver
786 * to include a callback asking hcds to do their bit by checking
787 * if all the drivers can suspend. Gets involved with remote wakeup.
788 *
789 * If there are pending urbs, then HCs will need to access memory,
790 * causing extra power drain. New sleep()/wakeup() PM calls might
791 * be needed, beyond PCI suspend()/resume(). The root hub timer
792 * still be accessing memory though ...
793 *
794 * FIXME: USB should have some power budgeting support working with
795 * all kinds of hubs.
796 *
797 * FIXME: This assumes only D0->D3 suspend and D3->D0 resume.
798 * D1 and D2 states should do something, yes?
799 *
800 * FIXME: Should provide generic enable_wake(), calling pci_enable_wake()
801 * for all supported states, so that USB remote wakeup can work for any
802 * devices that support it (and are connected via powered hubs).
803 *
804 * FIXME: resume doesn't seem to work right any more...
805 */
808 // 2.4 kernels have issued concurrent resumes (w/APM)
809 // we defend against that error; PCI doesn't yet.
811 /**
812 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
813 * @dev: USB Host Controller being suspended
814 *
815 * Store this function in the HCD's struct pci_driver as suspend().
816 */
817 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
818 {
819 struct usb_hcd *hcd;
820 int retval;
822 hcd = pci_get_drvdata(dev);
823 printk (KERN_INFO "%s %s: suspend to state %d\n",
824 hcd->description, dev->slot_name, state);
826 pci_save_state (dev, hcd->pci_state);
828 // FIXME for all connected devices, leaf-to-root:
829 // driver->suspend()
830 // proposed "new 2.5 driver model" will automate that
832 /* driver may want to disable DMA etc */
833 retval = hcd->driver->suspend (hcd, state);
834 hcd->state = USB_STATE_SUSPENDED;
836 pci_set_power_state (dev, state);
837 return retval;
838 }
839 EXPORT_SYMBOL (usb_hcd_pci_suspend);
841 /**
842 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
843 * @dev: USB Host Controller being resumed
844 *
845 * Store this function in the HCD's struct pci_driver as resume().
846 */
847 int usb_hcd_pci_resume (struct pci_dev *dev)
848 {
849 struct usb_hcd *hcd;
850 int retval;
852 hcd = pci_get_drvdata(dev);
853 printk (KERN_INFO "%s %s: resume\n",
854 hcd->description, dev->slot_name);
856 /* guard against multiple resumes (APM bug?) */
857 atomic_inc (&hcd->resume_count);
858 if (atomic_read (&hcd->resume_count) != 1) {
859 err ("concurrent PCI resumes for %s", hcd->bus->bus_name);
860 retval = 0;
861 goto done;
862 }
864 retval = -EBUSY;
865 if (hcd->state != USB_STATE_SUSPENDED) {
866 dbg ("can't resume, not suspended!");
867 goto done;
868 }
869 hcd->state = USB_STATE_RESUMING;
871 pci_set_power_state (dev, 0);
872 pci_restore_state (dev, hcd->pci_state);
874 retval = hcd->driver->resume (hcd);
875 if (!HCD_IS_RUNNING (hcd->state)) {
876 dbg ("resume %s failure, retval %d",
877 hcd->bus->bus_name, retval);
878 hc_died (hcd);
879 // FIXME: recover, reset etc.
880 } else {
881 // FIXME for all connected devices, root-to-leaf:
882 // driver->resume ();
883 // proposed "new 2.5 driver model" will automate that
884 }
886 done:
887 atomic_dec (&hcd->resume_count);
888 return retval;
889 }
890 EXPORT_SYMBOL (usb_hcd_pci_resume);
892 #endif /* CONFIG_PM */
894 #endif
896 /*-------------------------------------------------------------------------*/
898 /*
899 * Generic HC operations.
900 */
902 /*-------------------------------------------------------------------------*/
904 /* called from khubd, or root hub init threads for hcd-private init */
905 static int hcd_alloc_dev (struct usb_device *udev)
906 {
907 struct hcd_dev *dev;
908 struct usb_hcd *hcd;
909 unsigned long flags;
911 if (!udev || udev->hcpriv)
912 return -EINVAL;
913 if (!udev->bus || !udev->bus->hcpriv)
914 return -ENODEV;
915 hcd = udev->bus->hcpriv;
916 if (hcd->state == USB_STATE_QUIESCING)
917 return -ENOLINK;
919 dev = (struct hcd_dev *) kmalloc (sizeof *dev, GFP_KERNEL);
920 if (dev == NULL)
921 return -ENOMEM;
922 memset (dev, 0, sizeof *dev);
924 INIT_LIST_HEAD (&dev->dev_list);
925 INIT_LIST_HEAD (&dev->urb_list);
927 spin_lock_irqsave (&hcd_data_lock, flags);
928 list_add (&dev->dev_list, &hcd->dev_list);
929 // refcount is implicit
930 udev->hcpriv = dev;
931 spin_unlock_irqrestore (&hcd_data_lock, flags);
933 return 0;
934 }
936 /*-------------------------------------------------------------------------*/
938 static void hcd_panic (void *_hcd)
939 {
940 struct usb_hcd *hcd = _hcd;
941 hcd->driver->stop (hcd);
942 }
944 static void hc_died (struct usb_hcd *hcd)
945 {
946 struct list_head *devlist, *urblist;
947 struct hcd_dev *dev;
948 struct urb *urb;
949 unsigned long flags;
951 /* flag every pending urb as done */
952 spin_lock_irqsave (&hcd_data_lock, flags);
953 list_for_each (devlist, &hcd->dev_list) {
954 dev = list_entry (devlist, struct hcd_dev, dev_list);
955 list_for_each (urblist, &dev->urb_list) {
956 urb = list_entry (urblist, struct urb, urb_list);
957 dbg ("shutdown %s urb %p pipe %x, current status %d",
958 hcd->bus->bus_name,
959 urb, urb->pipe, urb->status);
960 if (urb->status == -EINPROGRESS)
961 urb->status = -ESHUTDOWN;
962 }
963 }
964 urb = (struct urb *) hcd->rh_timer.data;
965 if (urb)
966 urb->status = -ESHUTDOWN;
967 spin_unlock_irqrestore (&hcd_data_lock, flags);
969 if (urb)
970 rh_status_dequeue (hcd, urb);
972 /* hcd->stop() needs a task context */
973 INIT_TQUEUE (&hcd->work, hcd_panic, hcd);
974 (void) schedule_task (&hcd->work);
975 }
977 /*-------------------------------------------------------------------------*/
979 static void urb_unlink (struct urb *urb)
980 {
981 unsigned long flags;
982 struct usb_device *dev;
984 /* Release any periodic transfer bandwidth */
985 if (urb->bandwidth)
986 usb_release_bandwidth (urb->dev, urb,
987 usb_pipeisoc (urb->pipe));
989 /* clear all state linking urb to this dev (and hcd) */
991 spin_lock_irqsave (&hcd_data_lock, flags);
992 list_del_init (&urb->urb_list);
993 dev = urb->dev;
994 urb->dev = NULL;
995 usb_dec_dev_use (dev);
996 spin_unlock_irqrestore (&hcd_data_lock, flags);
997 }
1000 /* may be called in any context with a valid urb->dev usecount */
1001 /* caller surrenders "ownership" of urb */
1003 static int hcd_submit_urb (struct urb *urb)
1005 int status;
1006 struct usb_hcd *hcd;
1007 struct hcd_dev *dev;
1008 unsigned long flags;
1009 int pipe, temp, max;
1010 int mem_flags;
1012 if (!urb || urb->hcpriv || !urb->complete)
1013 return -EINVAL;
1015 urb->status = -EINPROGRESS;
1016 urb->actual_length = 0;
1017 urb->bandwidth = 0;
1018 INIT_LIST_HEAD (&urb->urb_list);
1020 if (!urb->dev || !urb->dev->bus || urb->dev->devnum <= 0)
1021 return -ENODEV;
1022 hcd = urb->dev->bus->hcpriv;
1023 dev = urb->dev->hcpriv;
1024 if (!hcd || !dev)
1025 return -ENODEV;
1027 /* can't submit new urbs when quiescing, halted, ... */
1028 if (hcd->state == USB_STATE_QUIESCING || !HCD_IS_RUNNING (hcd->state))
1029 return -ESHUTDOWN;
1030 pipe = urb->pipe;
1031 temp = usb_pipetype (urb->pipe);
1032 if (usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe),
1033 usb_pipeout (pipe)))
1034 return -EPIPE;
1036 /* NOTE: 2.5 passes this value explicitly in submit() */
1037 mem_flags = GFP_ATOMIC;
1039 /* FIXME there should be a sharable lock protecting us against
1040 * config/altsetting changes and disconnects, kicking in here.
1041 */
1043 /* Sanity check, so HCDs can rely on clean data */
1044 max = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
1045 if (max <= 0) {
1046 err ("bogus endpoint (bad maxpacket)");
1047 return -EINVAL;
1050 /* "high bandwidth" mode, 1-3 packets/uframe? */
1051 if (urb->dev->speed == USB_SPEED_HIGH) {
1052 int mult;
1053 switch (temp) {
1054 case PIPE_ISOCHRONOUS:
1055 case PIPE_INTERRUPT:
1056 mult = 1 + ((max >> 11) & 0x03);
1057 max &= 0x03ff;
1058 max *= mult;
1062 /* periodic transfers limit size per frame/uframe */
1063 switch (temp) {
1064 case PIPE_ISOCHRONOUS: {
1065 int n, len;
1067 if (urb->number_of_packets <= 0)
1068 return -EINVAL;
1069 for (n = 0; n < urb->number_of_packets; n++) {
1070 len = urb->iso_frame_desc [n].length;
1071 if (len < 0 || len > max)
1072 return -EINVAL;
1076 break;
1077 case PIPE_INTERRUPT:
1078 if (urb->transfer_buffer_length > max)
1079 return -EINVAL;
1082 /* the I/O buffer must usually be mapped/unmapped */
1083 if (urb->transfer_buffer_length < 0)
1084 return -EINVAL;
1086 if (urb->next) {
1087 warn ("use explicit queuing not urb->next");
1088 return -EINVAL;
1091 #ifdef DEBUG
1092 /* stuff that drivers shouldn't do, but which shouldn't
1093 * cause problems in HCDs if they get it wrong.
1094 */
1096 unsigned int orig_flags = urb->transfer_flags;
1097 unsigned int allowed;
1099 /* enforce simple/standard policy */
1100 allowed = USB_ASYNC_UNLINK; // affects later unlinks
1101 allowed |= USB_NO_FSBR; // only affects UHCI
1102 switch (temp) {
1103 case PIPE_CONTROL:
1104 allowed |= USB_DISABLE_SPD;
1105 break;
1106 case PIPE_BULK:
1107 allowed |= USB_DISABLE_SPD | USB_QUEUE_BULK
1108 | USB_ZERO_PACKET | URB_NO_INTERRUPT;
1109 break;
1110 case PIPE_INTERRUPT:
1111 allowed |= USB_DISABLE_SPD;
1112 break;
1113 case PIPE_ISOCHRONOUS:
1114 allowed |= USB_ISO_ASAP;
1115 break;
1117 urb->transfer_flags &= allowed;
1119 /* fail if submitter gave bogus flags */
1120 if (urb->transfer_flags != orig_flags) {
1121 err ("BOGUS urb flags, %x --> %x",
1122 orig_flags, urb->transfer_flags);
1123 return -EINVAL;
1126 #endif
1127 /*
1128 * Force periodic transfer intervals to be legal values that are
1129 * a power of two (so HCDs don't need to).
1131 * FIXME want bus->{intr,iso}_sched_horizon values here. Each HC
1132 * supports different values... this uses EHCI/UHCI defaults (and
1133 * EHCI can use smaller non-default values).
1134 */
1135 switch (temp) {
1136 case PIPE_ISOCHRONOUS:
1137 case PIPE_INTERRUPT:
1138 /* too small? */
1139 if (urb->interval <= 0)
1140 return -EINVAL;
1141 /* too big? */
1142 switch (urb->dev->speed) {
1143 case USB_SPEED_HIGH: /* units are microframes */
1144 // NOTE usb handles 2^15
1145 if (urb->interval > (1024 * 8))
1146 urb->interval = 1024 * 8;
1147 temp = 1024 * 8;
1148 break;
1149 case USB_SPEED_FULL: /* units are frames/msec */
1150 case USB_SPEED_LOW:
1151 if (temp == PIPE_INTERRUPT) {
1152 if (urb->interval > 255)
1153 return -EINVAL;
1154 // NOTE ohci only handles up to 32
1155 temp = 128;
1156 } else {
1157 if (urb->interval > 1024)
1158 urb->interval = 1024;
1159 // NOTE usb and ohci handle up to 2^15
1160 temp = 1024;
1162 break;
1163 default:
1164 return -EINVAL;
1166 /* power of two? */
1167 while (temp > urb->interval)
1168 temp >>= 1;
1169 urb->interval = temp;
1173 /*
1174 * FIXME: make urb timeouts be generic, keeping the HCD cores
1175 * as simple as possible.
1176 */
1178 // NOTE: a generic device/urb monitoring hook would go here.
1179 // hcd_monitor_hook(MONITOR_URB_SUBMIT, urb)
1180 // It would catch submission paths for all urbs.
1182 /*
1183 * Atomically queue the urb, first to our records, then to the HCD.
1184 * Access to urb->status is controlled by urb->lock ... changes on
1185 * i/o completion (normal or fault) or unlinking.
1186 */
1188 // FIXME: verify that quiescing hc works right (RH cleans up)
1190 spin_lock_irqsave (&hcd_data_lock, flags);
1191 if (HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_QUIESCING) {
1192 usb_inc_dev_use (urb->dev);
1193 list_add (&urb->urb_list, &dev->urb_list);
1194 status = 0;
1195 } else {
1196 INIT_LIST_HEAD (&urb->urb_list);
1197 status = -ESHUTDOWN;
1199 spin_unlock_irqrestore (&hcd_data_lock, flags);
1200 if (status)
1201 return status;
1203 // NOTE: 2.5 does this if !URB_NO_DMA_MAP transfer flag
1205 /* For 2.4, don't map bounce buffer if it's a root hub operation. */
1206 if (urb->dev == hcd->bus->root_hub) {
1207 status = rh_urb_enqueue (hcd, urb);
1208 } else {
1209 #ifdef CONFIG_PCI
1210 if (usb_pipecontrol (urb->pipe))
1211 urb->setup_dma = pci_map_single (
1212 hcd->pdev,
1213 urb->setup_packet,
1214 sizeof (struct usb_ctrlrequest),
1215 PCI_DMA_TODEVICE);
1216 if (urb->transfer_buffer_length != 0)
1217 urb->transfer_dma = pci_map_single (
1218 hcd->pdev,
1219 urb->transfer_buffer,
1220 urb->transfer_buffer_length,
1221 usb_pipein (urb->pipe)
1222 ? PCI_DMA_FROMDEVICE
1223 : PCI_DMA_TODEVICE);
1224 #endif
1225 status = hcd->driver->urb_enqueue (hcd, urb, mem_flags);
1227 return status;
1230 /*-------------------------------------------------------------------------*/
1232 /* called in any context */
1233 static int hcd_get_frame_number (struct usb_device *udev)
1235 struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
1236 return hcd->driver->get_frame_number (hcd);
1239 /*-------------------------------------------------------------------------*/
1241 struct completion_splice { // modified urb context:
1242 /* did we complete? */
1243 struct completion done;
1245 /* original urb data */
1246 void (*complete)(struct urb *);
1247 void *context;
1248 };
1250 static void unlink_complete (struct urb *urb)
1252 struct completion_splice *splice;
1254 splice = (struct completion_splice *) urb->context;
1256 /* issue original completion call */
1257 urb->complete = splice->complete;
1258 urb->context = splice->context;
1259 urb->complete (urb);
1261 /* then let the synchronous unlink call complete */
1262 complete (&splice->done);
1265 /*
1266 * called in any context; note ASYNC_UNLINK restrictions
1268 * caller guarantees urb won't be recycled till both unlink()
1269 * and the urb's completion function return
1270 */
1271 static int hcd_unlink_urb (struct urb *urb)
1273 struct hcd_dev *dev;
1274 struct usb_hcd *hcd = 0;
1275 unsigned long flags;
1276 struct completion_splice splice;
1277 int retval;
1279 if (!urb)
1280 return -EINVAL;
1282 /*
1283 * we contend for urb->status with the hcd core,
1284 * which changes it while returning the urb.
1286 * Caller guaranteed that the urb pointer hasn't been freed, and
1287 * that it was submitted. But as a rule it can't know whether or
1288 * not it's already been unlinked ... so we respect the reversed
1289 * lock sequence needed for the usb_hcd_giveback_urb() code paths
1290 * (urb lock, then hcd_data_lock) in case some other CPU is now
1291 * unlinking it.
1292 */
1293 spin_lock_irqsave (&urb->lock, flags);
1294 spin_lock (&hcd_data_lock);
1295 if (!urb->hcpriv || urb->transfer_flags & USB_TIMEOUT_KILLED) {
1296 retval = -EINVAL;
1297 goto done;
1300 if (!urb->dev || !urb->dev->bus) {
1301 retval = -ENODEV;
1302 goto done;
1305 /* giveback clears dev; non-null means it's linked at this level */
1306 dev = urb->dev->hcpriv;
1307 hcd = urb->dev->bus->hcpriv;
1308 if (!dev || !hcd) {
1309 retval = -ENODEV;
1310 goto done;
1313 /* Any status except -EINPROGRESS means the HCD has already started
1314 * to return this URB to the driver. In that case, there's no
1315 * more work for us to do.
1317 * There's much magic because of "automagic resubmit" of interrupt
1318 * transfers, stopped only by explicit unlinking. We won't issue
1319 * an "it's unlinked" callback more than once, but device drivers
1320 * can need to retry (SMP, -EAGAIN) an unlink request as well as
1321 * fake out the "not yet completed" state (set -EINPROGRESS) if
1322 * unlinking from complete(). Automagic eventually vanishes.
1324 * FIXME use an URB_UNLINKED flag to match URB_TIMEOUT_KILLED
1325 */
1326 if (urb->status != -EINPROGRESS) {
1327 if (usb_pipetype (urb->pipe) == PIPE_INTERRUPT)
1328 retval = -EAGAIN;
1329 else
1330 retval = -EBUSY;
1331 goto done;
1334 /* maybe set up to block on completion notification */
1335 if ((urb->transfer_flags & USB_TIMEOUT_KILLED))
1336 urb->status = -ETIMEDOUT;
1337 else if (!(urb->transfer_flags & USB_ASYNC_UNLINK)) {
1338 if (in_interrupt ()) {
1339 dbg ("non-async unlink in_interrupt");
1340 retval = -EWOULDBLOCK;
1341 goto done;
1343 /* synchronous unlink: block till we see the completion */
1344 init_completion (&splice.done);
1345 splice.complete = urb->complete;
1346 splice.context = urb->context;
1347 urb->complete = unlink_complete;
1348 urb->context = &splice;
1349 urb->status = -ENOENT;
1350 } else {
1351 /* asynchronous unlink */
1352 urb->status = -ECONNRESET;
1354 spin_unlock (&hcd_data_lock);
1355 spin_unlock_irqrestore (&urb->lock, flags);
1357 if (urb == (struct urb *) hcd->rh_timer.data) {
1358 rh_status_dequeue (hcd, urb);
1359 retval = 0;
1360 } else {
1361 retval = hcd->driver->urb_dequeue (hcd, urb);
1362 // FIXME: if retval and we tried to splice, whoa!!
1363 if (retval && urb->status == -ENOENT) err ("whoa! retval %d", retval);
1366 /* block till giveback, if needed */
1367 if (!(urb->transfer_flags & (USB_ASYNC_UNLINK|USB_TIMEOUT_KILLED))
1368 && HCD_IS_RUNNING (hcd->state)
1369 && !retval) {
1370 wait_for_completion (&splice.done);
1371 } else if ((urb->transfer_flags & USB_ASYNC_UNLINK) && retval == 0) {
1372 return -EINPROGRESS;
1374 goto bye;
1375 done:
1376 spin_unlock (&hcd_data_lock);
1377 spin_unlock_irqrestore (&urb->lock, flags);
1378 bye:
1379 if (retval)
1380 dbg ("%s: hcd_unlink_urb fail %d",
1381 hcd ? hcd->bus->bus_name : "(no bus?)",
1382 retval);
1383 return retval;
1386 /*-------------------------------------------------------------------------*/
1388 /* called by khubd, rmmod, apmd, or other thread for hcd-private cleanup */
1390 // FIXME: likely best to have explicit per-setting (config+alt)
1391 // setup primitives in the usbcore-to-hcd driver API, so nothing
1392 // is implicit. kernel 2.5 needs a bunch of config cleanup...
1394 static int hcd_free_dev (struct usb_device *udev)
1396 struct hcd_dev *dev;
1397 struct usb_hcd *hcd;
1398 unsigned long flags;
1400 if (!udev || !udev->hcpriv)
1401 return -EINVAL;
1403 if (!udev->bus || !udev->bus->hcpriv)
1404 return -ENODEV;
1406 // should udev->devnum == -1 ??
1408 dev = udev->hcpriv;
1409 hcd = udev->bus->hcpriv;
1411 /* device driver problem with refcounts? */
1412 if (!list_empty (&dev->urb_list)) {
1413 dbg ("free busy dev, %s devnum %d (bug!)",
1414 hcd->bus->bus_name, udev->devnum);
1415 return -EINVAL;
1418 hcd->driver->free_config (hcd, udev);
1420 spin_lock_irqsave (&hcd_data_lock, flags);
1421 list_del (&dev->dev_list);
1422 udev->hcpriv = NULL;
1423 spin_unlock_irqrestore (&hcd_data_lock, flags);
1425 kfree (dev);
1426 return 0;
1429 static struct usb_operations hcd_operations = {
1430 allocate: hcd_alloc_dev,
1431 get_frame_number: hcd_get_frame_number,
1432 submit_urb: hcd_submit_urb,
1433 unlink_urb: hcd_unlink_urb,
1434 deallocate: hcd_free_dev,
1435 };
1437 /*-------------------------------------------------------------------------*/
1439 static void hcd_irq (int irq, void *__hcd, struct pt_regs * r)
1441 struct usb_hcd *hcd = __hcd;
1442 int start = hcd->state;
1444 if (unlikely (hcd->state == USB_STATE_HALT)) /* irq sharing? */
1445 return;
1447 hcd->driver->irq (hcd, r);
1448 if (hcd->state != start && hcd->state == USB_STATE_HALT)
1449 hc_died (hcd);
1452 /*-------------------------------------------------------------------------*/
1454 /**
1455 * usb_hcd_giveback_urb - return URB from HCD to device driver
1456 * @hcd: host controller returning the URB
1457 * @urb: urb being returned to the USB device driver.
1458 * @regs: saved hardware registers (ignored on 2.4 kernels)
1459 * Context: in_interrupt()
1461 * This hands the URB from HCD to its USB device driver, using its
1462 * completion function. The HCD has freed all per-urb resources
1463 * (and is done using urb->hcpriv). It also released all HCD locks;
1464 * the device driver won't cause deadlocks if it resubmits this URB,
1465 * and won't confuse things by modifying and resubmitting this one.
1466 * Bandwidth and other resources will be deallocated.
1468 * HCDs must not use this for periodic URBs that are still scheduled
1469 * and will be reissued. They should just call their completion handlers
1470 * until the urb is returned to the device driver by unlinking.
1472 * NOTE that no urb->next processing is done, even for isochronous URBs.
1473 * ISO streaming functionality can be achieved by having completion handlers
1474 * re-queue URBs. Such explicit queuing doesn't discard error reports.
1475 */
1476 void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
1478 int is_root_hub_operation;
1480 /* Work this out here as urb_unlink clears urb->dev */
1481 is_root_hub_operation = (urb->dev == hcd->bus->root_hub);
1483 urb_unlink (urb);
1485 // NOTE: a generic device/urb monitoring hook would go here.
1486 // hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev)
1487 // It would catch exit/unlink paths for all urbs, but non-exit
1488 // completions for periodic urbs need hooks inside the HCD.
1489 // hcd_monitor_hook(MONITOR_URB_UPDATE, urb, dev)
1491 // NOTE: 2.5 does this if !URB_NO_DMA_MAP transfer flag
1493 #ifdef CONFIG_PCI
1494 /* For 2.4, don't unmap bounce buffer if it's a root hub operation. */
1495 if (usb_pipecontrol (urb->pipe) && !is_root_hub_operation)
1496 pci_unmap_single (hcd->pdev, urb->setup_dma,
1497 sizeof (struct usb_ctrlrequest),
1498 PCI_DMA_TODEVICE);
1500 if ((urb->transfer_buffer_length != 0) && !is_root_hub_operation)
1501 pci_unmap_single (hcd->pdev, urb->transfer_dma,
1502 urb->transfer_buffer_length,
1503 usb_pipein (urb->pipe)
1504 ? PCI_DMA_FROMDEVICE
1505 : PCI_DMA_TODEVICE);
1506 #endif
1508 /* pass ownership to the completion handler */
1509 urb->complete (urb);
1511 EXPORT_SYMBOL (usb_hcd_giveback_urb);