Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / usb / core / usb.c
blob2885522a971e9ec34573b190501fc141598bfaa1
1 /*
2 * drivers/usb/usb.c
4 * (C) Copyright Linus Torvalds 1999
5 * (C) Copyright Johannes Erdfelt 1999-2001
6 * (C) Copyright Andreas Gal 1999
7 * (C) Copyright Gregory P. Smith 1999
8 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
9 * (C) Copyright Randy Dunlap 2000
10 * (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id,
11 more docs, etc)
12 * (C) Copyright Yggdrasil Computing, Inc. 2000
13 * (usb_device_id matching changes by Adam J. Richter)
14 * (C) Copyright Greg Kroah-Hartman 2002-2003
16 * NOTE! This is not actually a driver at all, rather this is
17 * just a collection of helper routines that implement the
18 * generic USB things that the real drivers can use..
20 * Think of this as a "USB library" rather than anything else.
21 * It should be considered a slave, with no callbacks. Callbacks
22 * are evil.
25 #include <linux/config.h>
27 #ifdef CONFIG_USB_DEBUG
28 #define DEBUG
29 #else
30 #undef DEBUG
31 #endif
33 #include <linux/module.h>
34 #include <linux/string.h>
35 #include <linux/bitops.h>
36 #include <linux/slab.h>
37 #include <linux/interrupt.h> /* for in_interrupt() */
38 #include <linux/kmod.h>
39 #include <linux/init.h>
40 #include <linux/spinlock.h>
41 #include <linux/errno.h>
42 #include <linux/smp_lock.h>
43 #include <linux/usb.h>
45 #include <asm/io.h>
46 #include <asm/scatterlist.h>
47 #include <linux/mm.h>
48 #include <linux/dma-mapping.h>
50 #include "hcd.h"
51 #include "usb.h"
53 extern int usb_hub_init(void);
54 extern void usb_hub_cleanup(void);
55 extern int usb_major_init(void);
56 extern void usb_major_cleanup(void);
57 extern int usb_host_init(void);
58 extern void usb_host_cleanup(void);
61 int nousb; /* Disable USB when built into kernel image */
62 /* Not honored on modular build */
65 static int generic_probe (struct device *dev)
67 return 0;
69 static int generic_remove (struct device *dev)
71 return 0;
74 static struct device_driver usb_generic_driver = {
75 .name = "usb",
76 .bus = &usb_bus_type,
77 .probe = generic_probe,
78 .remove = generic_remove,
81 static int usb_generic_driver_data;
83 /* needs to be called with BKL held */
84 int usb_device_probe(struct device *dev)
86 struct usb_interface * intf = to_usb_interface(dev);
87 struct usb_driver * driver = to_usb_driver(dev->driver);
88 const struct usb_device_id *id;
89 int error = -ENODEV;
91 dev_dbg(dev, "%s\n", __FUNCTION__);
93 if (!driver->probe)
94 return error;
96 id = usb_match_id (intf, driver->id_table);
97 if (id) {
98 dev_dbg (dev, "%s - got id\n", __FUNCTION__);
99 down (&driver->serialize);
100 error = driver->probe (intf, id);
101 up (&driver->serialize);
103 if (!error)
104 intf->driver = driver;
106 return error;
109 int usb_device_remove(struct device *dev)
111 struct usb_interface *intf;
112 struct usb_driver *driver;
114 intf = list_entry(dev,struct usb_interface,dev);
115 driver = to_usb_driver(dev->driver);
117 down(&driver->serialize);
119 if (intf->driver && intf->driver->disconnect)
120 intf->driver->disconnect(intf);
122 /* if driver->disconnect didn't release the interface */
123 if (intf->driver)
124 usb_driver_release_interface(driver, intf);
126 up(&driver->serialize);
128 return 0;
132 * usb_register - register a USB driver
133 * @new_driver: USB operations for the driver
135 * Registers a USB driver with the USB core. The list of unattached
136 * interfaces will be rescanned whenever a new driver is added, allowing
137 * the new driver to attach to any recognized devices.
138 * Returns a negative error code on failure and 0 on success.
140 * NOTE: if you want your driver to use the USB major number, you must call
141 * usb_register_dev() to enable that functionality. This function no longer
142 * takes care of that.
144 int usb_register(struct usb_driver *new_driver)
146 int retval = 0;
148 if (nousb)
149 return -ENODEV;
151 new_driver->driver.name = (char *)new_driver->name;
152 new_driver->driver.bus = &usb_bus_type;
153 new_driver->driver.probe = usb_device_probe;
154 new_driver->driver.remove = usb_device_remove;
156 init_MUTEX(&new_driver->serialize);
158 retval = driver_register(&new_driver->driver);
160 if (!retval) {
161 info("registered new driver %s", new_driver->name);
162 usbfs_update_special();
163 } else {
164 err("problem %d when registering driver %s",
165 retval, new_driver->name);
168 return retval;
172 * usb_deregister - unregister a USB driver
173 * @driver: USB operations of the driver to unregister
174 * Context: !in_interrupt (), must be called with BKL held
176 * Unlinks the specified driver from the internal USB driver list.
178 * NOTE: If you called usb_register_dev(), you still need to call
179 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
180 * this * call will no longer do it for you.
182 void usb_deregister(struct usb_driver *driver)
184 info("deregistering driver %s", driver->name);
186 driver_unregister (&driver->driver);
188 usbfs_update_special();
192 * usb_ifnum_to_if - get the interface object with a given interface number (usbcore-internal)
193 * @dev: the device whose current configuration is considered
194 * @ifnum: the desired interface
196 * This walks the device descriptor for the currently active configuration
197 * and returns a pointer to the interface with that particular interface
198 * number, or null.
200 * Note that configuration descriptors are not required to assign interface
201 * numbers sequentially, so that it would be incorrect to assume that
202 * the first interface in that descriptor corresponds to interface zero.
203 * This routine helps device drivers avoid such mistakes.
204 * However, you should make sure that you do the right thing with any
205 * alternate settings available for this interfaces.
207 struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
209 int i;
211 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
212 if (dev->actconfig->interface[i].altsetting[0]
213 .desc.bInterfaceNumber == ifnum)
214 return &dev->actconfig->interface[i];
216 return NULL;
220 * usb_epnum_to_ep_desc - get the endpoint object with a given endpoint number
221 * @dev: the device whose current configuration is considered
222 * @epnum: the desired endpoint
224 * This walks the device descriptor for the currently active configuration,
225 * and returns a pointer to the endpoint with that particular endpoint
226 * number, or null.
228 * Note that interface descriptors are not required to assign endpont
229 * numbers sequentially, so that it would be incorrect to assume that
230 * the first endpoint in that descriptor corresponds to interface zero.
231 * This routine helps device drivers avoid such mistakes.
233 struct usb_endpoint_descriptor *
234 usb_epnum_to_ep_desc(struct usb_device *dev, unsigned epnum)
236 int i, j, k;
238 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
239 for (j = 0; j < dev->actconfig->interface[i].num_altsetting; j++)
240 for (k = 0; k < dev->actconfig->interface[i]
241 .altsetting[j].desc.bNumEndpoints; k++)
242 if (epnum == dev->actconfig->interface[i]
243 .altsetting[j].endpoint[k]
244 .desc.bEndpointAddress)
245 return &dev->actconfig->interface[i]
246 .altsetting[j].endpoint[k]
247 .desc;
249 return NULL;
253 * usb_driver_claim_interface - bind a driver to an interface
254 * @driver: the driver to be bound
255 * @iface: the interface to which it will be bound
256 * @priv: driver data associated with that interface
258 * This is used by usb device drivers that need to claim more than one
259 * interface on a device when probing (audio and acm are current examples).
260 * No device driver should directly modify internal usb_interface or
261 * usb_device structure members.
263 * Few drivers should need to use this routine, since the most natural
264 * way to bind to an interface is to return the private data from
265 * the driver's probe() method. Any driver that does use this must
266 * first be sure that no other driver has claimed the interface, by
267 * checking with usb_interface_claimed().
269 void usb_driver_claim_interface(struct usb_driver *driver, struct usb_interface *iface, void* priv)
271 if (!iface || !driver)
272 return;
274 // FIXME change API to report an error in this case
275 if (iface->driver)
276 err ("%s driver booted %s off interface %p",
277 driver->name, iface->driver->name, iface);
278 else
279 dbg("%s driver claimed interface %p", driver->name, iface);
281 iface->driver = driver;
282 usb_set_intfdata(iface, priv);
286 * usb_interface_claimed - returns true iff an interface is claimed
287 * @iface: the interface being checked
289 * This should be used by drivers to check other interfaces to see if
290 * they are available or not. If another driver has claimed the interface,
291 * they may not claim it. Otherwise it's OK to claim it using
292 * usb_driver_claim_interface().
294 * Returns true (nonzero) iff the interface is claimed, else false (zero).
296 int usb_interface_claimed(struct usb_interface *iface)
298 if (!iface)
299 return 0;
301 return (iface->driver != NULL);
302 } /* usb_interface_claimed() */
305 * usb_driver_release_interface - unbind a driver from an interface
306 * @driver: the driver to be unbound
307 * @iface: the interface from which it will be unbound
309 * This should be used by drivers to release their claimed interfaces.
310 * It is normally called in their disconnect() methods, and only for
311 * drivers that bound to more than one interface in their probe().
313 * When the USB subsystem disconnect()s a driver from some interface,
314 * it automatically invokes this method for that interface. That
315 * means that even drivers that used usb_driver_claim_interface()
316 * usually won't need to call this.
318 void usb_driver_release_interface(struct usb_driver *driver, struct usb_interface *iface)
320 /* this should never happen, don't release something that's not ours */
321 if (!iface || iface->driver != driver)
322 return;
324 iface->driver = NULL;
325 usb_set_intfdata(iface, NULL);
329 * usb_match_id - find first usb_device_id matching device or interface
330 * @interface: the interface of interest
331 * @id: array of usb_device_id structures, terminated by zero entry
333 * usb_match_id searches an array of usb_device_id's and returns
334 * the first one matching the device or interface, or null.
335 * This is used when binding (or rebinding) a driver to an interface.
336 * Most USB device drivers will use this indirectly, through the usb core,
337 * but some layered driver frameworks use it directly.
338 * These device tables are exported with MODULE_DEVICE_TABLE, through
339 * modutils and "modules.usbmap", to support the driver loading
340 * functionality of USB hotplugging.
342 * What Matches:
344 * The "match_flags" element in a usb_device_id controls which
345 * members are used. If the corresponding bit is set, the
346 * value in the device_id must match its corresponding member
347 * in the device or interface descriptor, or else the device_id
348 * does not match.
350 * "driver_info" is normally used only by device drivers,
351 * but you can create a wildcard "matches anything" usb_device_id
352 * as a driver's "modules.usbmap" entry if you provide an id with
353 * only a nonzero "driver_info" field. If you do this, the USB device
354 * driver's probe() routine should use additional intelligence to
355 * decide whether to bind to the specified interface.
357 * What Makes Good usb_device_id Tables:
359 * The match algorithm is very simple, so that intelligence in
360 * driver selection must come from smart driver id records.
361 * Unless you have good reasons to use another selection policy,
362 * provide match elements only in related groups, and order match
363 * specifiers from specific to general. Use the macros provided
364 * for that purpose if you can.
366 * The most specific match specifiers use device descriptor
367 * data. These are commonly used with product-specific matches;
368 * the USB_DEVICE macro lets you provide vendor and product IDs,
369 * and you can also match against ranges of product revisions.
370 * These are widely used for devices with application or vendor
371 * specific bDeviceClass values.
373 * Matches based on device class/subclass/protocol specifications
374 * are slightly more general; use the USB_DEVICE_INFO macro, or
375 * its siblings. These are used with single-function devices
376 * where bDeviceClass doesn't specify that each interface has
377 * its own class.
379 * Matches based on interface class/subclass/protocol are the
380 * most general; they let drivers bind to any interface on a
381 * multiple-function device. Use the USB_INTERFACE_INFO
382 * macro, or its siblings, to match class-per-interface style
383 * devices (as recorded in bDeviceClass).
385 * Within those groups, remember that not all combinations are
386 * meaningful. For example, don't give a product version range
387 * without vendor and product IDs; or specify a protocol without
388 * its associated class and subclass.
390 const struct usb_device_id *
391 usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
393 struct usb_host_interface *intf;
394 struct usb_device *dev;
396 /* proc_connectinfo in devio.c may call us with id == NULL. */
397 if (id == NULL)
398 return NULL;
400 intf = &interface->altsetting [interface->act_altsetting];
401 dev = interface_to_usbdev(interface);
403 /* It is important to check that id->driver_info is nonzero,
404 since an entry that is all zeroes except for a nonzero
405 id->driver_info is the way to create an entry that
406 indicates that the driver want to examine every
407 device and interface. */
408 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
409 id->driver_info; id++) {
411 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
412 id->idVendor != dev->descriptor.idVendor)
413 continue;
415 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
416 id->idProduct != dev->descriptor.idProduct)
417 continue;
419 /* No need to test id->bcdDevice_lo != 0, since 0 is never
420 greater than any unsigned number. */
421 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
422 (id->bcdDevice_lo > dev->descriptor.bcdDevice))
423 continue;
425 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
426 (id->bcdDevice_hi < dev->descriptor.bcdDevice))
427 continue;
429 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
430 (id->bDeviceClass != dev->descriptor.bDeviceClass))
431 continue;
433 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
434 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
435 continue;
437 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
438 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
439 continue;
441 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
442 (id->bInterfaceClass != intf->desc.bInterfaceClass))
443 continue;
445 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
446 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
447 continue;
449 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
450 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
451 continue;
453 return id;
456 return NULL;
460 * usb_find_interface - find usb_interface pointer for driver and device
461 * @drv: the driver whose current configuration is considered
462 * @minor: the minor number of the desired device
464 * This walks the driver device list and returns a pointer to the interface
465 * with the matching minor. Note, this only works for devices that share the
466 * USB major number.
468 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
470 struct list_head *entry;
471 struct device *dev;
472 struct usb_interface *intf;
474 list_for_each(entry, &drv->driver.devices) {
475 dev = container_of(entry, struct device, driver_list);
477 /* can't look at usb devices, only interfaces */
478 if (dev->driver == &usb_generic_driver)
479 continue;
481 intf = to_usb_interface(dev);
482 if (intf->minor == -1)
483 continue;
484 if (intf->minor == minor)
485 return intf;
488 /* no device found that matches */
489 return NULL;
492 static int usb_device_match (struct device *dev, struct device_driver *drv)
494 struct usb_interface *intf;
495 struct usb_driver *usb_drv;
496 const struct usb_device_id *id;
498 /* check for generic driver, which we don't match any device with */
499 if (drv == &usb_generic_driver)
500 return 0;
502 intf = to_usb_interface(dev);
504 usb_drv = to_usb_driver(drv);
505 id = usb_drv->id_table;
507 id = usb_match_id (intf, usb_drv->id_table);
508 if (id)
509 return 1;
511 return 0;
515 #ifdef CONFIG_HOTPLUG
518 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
519 * (normally /sbin/hotplug) when USB devices get added or removed.
521 * This invokes a user mode policy agent, typically helping to load driver
522 * or other modules, configure the device, and more. Drivers can provide
523 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
525 * We're called either from khubd (the typical case) or from root hub
526 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
527 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
528 * device (and this configuration!) are still present.
530 static int usb_hotplug (struct device *dev, char **envp, int num_envp,
531 char *buffer, int buffer_size)
533 struct usb_interface *intf;
534 struct usb_device *usb_dev;
535 char *scratch;
536 int i = 0;
537 int length = 0;
539 dbg ("%s", __FUNCTION__);
541 if (!dev)
542 return -ENODEV;
544 /* Must check driver_data here, as on remove driver is always NULL */
545 if ((dev->driver == &usb_generic_driver) ||
546 (dev->driver_data == &usb_generic_driver_data))
547 return 0;
549 intf = to_usb_interface(dev);
550 usb_dev = interface_to_usbdev (intf);
552 if (usb_dev->devnum < 0) {
553 dbg ("device already deleted ??");
554 return -ENODEV;
556 if (!usb_dev->bus) {
557 dbg ("bus already removed?");
558 return -ENODEV;
561 scratch = buffer;
563 #ifdef CONFIG_USB_DEVICEFS
564 /* If this is available, userspace programs can directly read
565 * all the device descriptors we don't tell them about. Or
566 * even act as usermode drivers.
568 * FIXME reduce hardwired intelligence here
570 envp [i++] = scratch;
571 length += snprintf (scratch, buffer_size - length,
572 "DEVICE=/proc/bus/usb/%03d/%03d",
573 usb_dev->bus->busnum, usb_dev->devnum);
574 if ((buffer_size - length <= 0) || (i >= num_envp))
575 return -ENOMEM;
576 ++length;
577 scratch += length;
578 #endif
580 /* per-device configurations are common */
581 envp [i++] = scratch;
582 length += snprintf (scratch, buffer_size - length, "PRODUCT=%x/%x/%x",
583 usb_dev->descriptor.idVendor,
584 usb_dev->descriptor.idProduct,
585 usb_dev->descriptor.bcdDevice);
586 if ((buffer_size - length <= 0) || (i >= num_envp))
587 return -ENOMEM;
588 ++length;
589 scratch += length;
591 /* class-based driver binding models */
592 envp [i++] = scratch;
593 length += snprintf (scratch, buffer_size - length, "TYPE=%d/%d/%d",
594 usb_dev->descriptor.bDeviceClass,
595 usb_dev->descriptor.bDeviceSubClass,
596 usb_dev->descriptor.bDeviceProtocol);
597 if ((buffer_size - length <= 0) || (i >= num_envp))
598 return -ENOMEM;
599 ++length;
600 scratch += length;
602 if (usb_dev->descriptor.bDeviceClass == 0) {
603 int alt = intf->act_altsetting;
605 /* 2.4 only exposed interface zero. in 2.5, hotplug
606 * agents are called for all interfaces, and can use
607 * $DEVPATH/bInterfaceNumber if necessary.
609 envp [i++] = scratch;
610 length += snprintf (scratch, buffer_size - length,
611 "INTERFACE=%d/%d/%d",
612 intf->altsetting[alt].desc.bInterfaceClass,
613 intf->altsetting[alt].desc.bInterfaceSubClass,
614 intf->altsetting[alt].desc.bInterfaceProtocol);
615 if ((buffer_size - length <= 0) || (i >= num_envp))
616 return -ENOMEM;
617 ++length;
618 scratch += length;
621 envp [i++] = 0;
623 return 0;
626 #else
628 static int usb_hotplug (struct device *dev, char **envp,
629 int num_envp, char *buffer, int buffer_size)
631 return -ENODEV;
634 #endif /* CONFIG_HOTPLUG */
637 * usb_alloc_dev - allocate a usb device structure (usbcore-internal)
638 * @parent: hub to which device is connected
639 * @bus: bus used to access the device
640 * Context: !in_interrupt ()
642 * Only hub drivers (including virtual root hub drivers for host
643 * controllers) should ever call this.
645 * This call is synchronous, and may not be used in an interrupt context.
647 struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus)
649 struct usb_device *dev;
651 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
652 if (!dev)
653 return NULL;
655 memset(dev, 0, sizeof(*dev));
657 bus = usb_bus_get(bus);
658 if (!bus) {
659 kfree(dev);
660 return NULL;
663 device_initialize(&dev->dev);
664 dev->state = USB_STATE_ATTACHED;
666 if (!parent)
667 dev->devpath [0] = '0';
668 dev->bus = bus;
669 dev->parent = parent;
670 INIT_LIST_HEAD(&dev->filelist);
672 init_MUTEX(&dev->serialize);
674 if (dev->bus->op->allocate)
675 dev->bus->op->allocate(dev);
677 return dev;
681 * usb_get_dev - increments the reference count of the usb device structure
682 * @dev: the device being referenced
684 * Each live reference to a device should be refcounted.
686 * Drivers for USB interfaces should normally record such references in
687 * their probe() methods, when they bind to an interface, and release
688 * them by calling usb_put_dev(), in their disconnect() methods.
690 * A pointer to the device with the incremented reference counter is returned.
692 struct usb_device *usb_get_dev (struct usb_device *dev)
694 struct device *tmp;
696 if (!dev)
697 return NULL;
699 tmp = get_device(&dev->dev);
700 if (tmp)
701 return to_usb_device(tmp);
702 else
703 return NULL;
707 * usb_put_dev - release a use of the usb device structure
708 * @dev: device that's been disconnected
710 * Must be called when a user of a device is finished with it. When the last
711 * user of the device calls this function, the memory of the device is freed.
713 void usb_put_dev(struct usb_device *dev)
715 if (dev)
716 put_device(&dev->dev);
720 * usb_release_dev - free a usb device structure when all users of it are finished.
721 * @dev: device that's been disconnected
723 * Will be called only by the device core when all users of this usb device are
724 * done.
726 static void usb_release_dev(struct device *dev)
728 struct usb_device *udev;
730 udev = to_usb_device(dev);
732 if (udev->bus && udev->bus->op && udev->bus->op->deallocate)
733 udev->bus->op->deallocate(udev);
734 usb_destroy_configuration (udev);
735 usb_bus_put (udev->bus);
736 kfree (udev);
740 static struct usb_device *match_device(struct usb_device *dev,
741 u16 vendor_id, u16 product_id)
743 struct usb_device *ret_dev = NULL;
744 int child;
746 dbg("looking at vendor %d, product %d",
747 dev->descriptor.idVendor,
748 dev->descriptor.idProduct);
750 /* see if this device matches */
751 if ((dev->descriptor.idVendor == vendor_id) &&
752 (dev->descriptor.idProduct == product_id)) {
753 dbg ("found the device!");
754 ret_dev = usb_get_dev(dev);
755 goto exit;
758 /* look through all of the children of this device */
759 for (child = 0; child < dev->maxchild; ++child) {
760 if (dev->children[child]) {
761 ret_dev = match_device(dev->children[child],
762 vendor_id, product_id);
763 if (ret_dev)
764 goto exit;
767 exit:
768 return ret_dev;
772 * usb_find_device - find a specific usb device in the system
773 * @vendor_id: the vendor id of the device to find
774 * @product_id: the product id of the device to find
776 * Returns a pointer to a struct usb_device if such a specified usb
777 * device is present in the system currently. The usage count of the
778 * device will be incremented if a device is found. Make sure to call
779 * usb_put_dev() when the caller is finished with the device.
781 * If a device with the specified vendor and product id is not found,
782 * NULL is returned.
784 struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
786 struct list_head *buslist;
787 struct usb_bus *bus;
788 struct usb_device *dev = NULL;
790 down(&usb_bus_list_lock);
791 for (buslist = usb_bus_list.next;
792 buslist != &usb_bus_list;
793 buslist = buslist->next) {
794 bus = container_of(buslist, struct usb_bus, bus_list);
795 dev = match_device(bus->root_hub, vendor_id, product_id);
796 if (dev)
797 goto exit;
799 exit:
800 up(&usb_bus_list_lock);
801 return dev;
805 * usb_get_current_frame_number - return current bus frame number
806 * @dev: the device whose bus is being queried
808 * Returns the current frame number for the USB host controller
809 * used with the given USB device. This can be used when scheduling
810 * isochronous requests.
812 * Note that different kinds of host controller have different
813 * "scheduling horizons". While one type might support scheduling only
814 * 32 frames into the future, others could support scheduling up to
815 * 1024 frames into the future.
817 int usb_get_current_frame_number(struct usb_device *dev)
819 return dev->bus->op->get_frame_number (dev);
822 /*-------------------------------------------------------------------*/
824 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
825 * extra field of the interface and endpoint descriptor structs.
828 int __usb_get_extra_descriptor(char *buffer, unsigned size, unsigned char type, void **ptr)
830 struct usb_descriptor_header *header;
832 while (size >= sizeof(struct usb_descriptor_header)) {
833 header = (struct usb_descriptor_header *)buffer;
835 if (header->bLength < 2) {
836 err("invalid descriptor length of %d", header->bLength);
837 return -1;
840 if (header->bDescriptorType == type) {
841 *ptr = header;
842 return 0;
845 buffer += header->bLength;
846 size -= header->bLength;
848 return -1;
852 * usb_disconnect - disconnect a device (usbcore-internal)
853 * @pdev: pointer to device being disconnected
854 * Context: !in_interrupt ()
856 * Something got disconnected. Get rid of it, and all of its children.
858 * Only hub drivers (including virtual root hub drivers for host
859 * controllers) should ever call this.
861 * This call is synchronous, and may not be used in an interrupt context.
863 void usb_disconnect(struct usb_device **pdev)
865 struct usb_device *dev = *pdev;
866 struct usb_bus *bus;
867 struct usb_operations *ops;
868 int i;
870 might_sleep ();
872 if (!dev) {
873 pr_debug ("%s nodev\n", __FUNCTION__);
874 return;
876 bus = dev->bus;
877 if (!bus) {
878 pr_debug ("%s nobus\n", __FUNCTION__);
879 return;
881 ops = bus->op;
883 *pdev = NULL;
885 /* mark the device as inactive, so any further urb submissions for
886 * this device will fail.
888 dev->state = USB_STATE_NOTATTACHED;
890 dev_info (&dev->dev, "USB disconnect, address %d\n", dev->devnum);
892 /* Free up all the children before we remove this device */
893 for (i = 0; i < USB_MAXCHILDREN; i++) {
894 struct usb_device **child = dev->children + i;
895 if (*child)
896 usb_disconnect(child);
899 /* disconnect() drivers from interfaces (a key side effect) */
900 dev_dbg (&dev->dev, "unregistering interfaces\n");
901 if (dev->actconfig) {
902 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
903 struct usb_interface *interface;
905 /* remove this interface */
906 interface = &dev->actconfig->interface[i];
907 device_unregister(&interface->dev);
911 /* deallocate hcd/hardware state */
912 if (ops->disable) {
913 void (*disable)(struct usb_device *, int) = ops->disable;
915 for (i = 0; i < 15; i++) {
916 disable (dev, i);
917 disable (dev, USB_DIR_IN | i);
921 dev_dbg (&dev->dev, "unregistering device\n");
922 /* Free the device number and remove the /proc/bus/usb entry */
923 if (dev->devnum > 0) {
924 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
925 usbfs_remove_device(dev);
927 device_unregister(&dev->dev);
929 /* Decrement the reference count, it'll auto free everything when */
930 /* it hits 0 which could very well be now */
931 usb_put_dev(dev);
935 * usb_connect - pick device address (usbcore-internal)
936 * @dev: newly detected device (in DEFAULT state)
938 * Picks a device address. It's up to the hub (or root hub) driver
939 * to handle and manage enumeration, starting from the DEFAULT state.
940 * Only hub drivers (including virtual root hub drivers for host
941 * controllers) should ever call this.
943 void usb_connect(struct usb_device *dev)
945 int devnum;
946 // FIXME needs locking for SMP!!
947 /* why? this is called only from the hub thread,
948 * which hopefully doesn't run on multiple CPU's simultaneously 8-)
949 * ... it's also called from modprobe/rmmod/apmd threads as part
950 * of virtual root hub init/reinit. In the init case, the hub code
951 * won't have seen this, but not so for reinit ...
953 dev->descriptor.bMaxPacketSize0 = 8; /* Start off at 8 bytes */
955 /* Try to allocate the next devnum beginning at bus->devnum_next. */
956 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, dev->bus->devnum_next);
957 if (devnum >= 128)
958 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, 1);
960 dev->bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
962 if (devnum < 128) {
963 set_bit(devnum, dev->bus->devmap.devicemap);
964 dev->devnum = devnum;
969 // hub-only!! ... and only exported for reset/reinit path.
970 // otherwise used internally, for usb_new_device()
971 int usb_set_address(struct usb_device *dev)
973 int retval;
975 if (dev->devnum == 0)
976 return -EINVAL;
977 if (dev->state != USB_STATE_DEFAULT && dev->state != USB_STATE_ADDRESS)
978 return -EINVAL;
979 retval = usb_control_msg(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS,
980 0, dev->devnum, 0, NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
981 if (retval == 0)
982 dev->state = USB_STATE_ADDRESS;
983 return retval;
987 /* improve on the default device description, if we can ... and
988 * while we're at it, maybe show the vendor and product strings.
990 static void set_device_description (struct usb_device *dev)
992 void *buf;
993 int mfgr = dev->descriptor.iManufacturer;
994 int prod = dev->descriptor.iProduct;
995 int vendor_id = dev->descriptor.idVendor;
996 int product_id = dev->descriptor.idProduct;
997 char *mfgr_str, *prod_str;
999 /* set default; keep it if there are no strings, or kmalloc fails */
1000 sprintf (dev->dev.name, "USB device %04x:%04x",
1001 vendor_id, product_id);
1003 if (!(buf = kmalloc(256 * 2, GFP_KERNEL)))
1004 return;
1006 prod_str = (char *) buf;
1007 mfgr_str = (char *) buf + 256;
1009 if (prod && usb_string (dev, prod, prod_str, 256) > 0) {
1010 #ifdef DEBUG
1011 dev_printk (KERN_INFO, &dev->dev, "Product: %s\n", prod_str);
1012 #endif
1013 } else {
1014 prod_str = 0;
1017 if (mfgr && usb_string (dev, mfgr, mfgr_str, 256) > 0) {
1018 #ifdef DEBUG
1019 dev_printk (KERN_INFO, &dev->dev, "Manufacturer: %s\n", mfgr_str);
1020 #endif
1021 } else {
1022 mfgr_str = 0;
1025 /* much like pci ... describe as either:
1026 * - both strings: 'product descr (vendor descr)'
1027 * - product only: 'product descr (USB device vvvv:pppp)'
1028 * - vendor only: 'USB device vvvv:pppp (vendor descr)'
1029 * - neither string: 'USB device vvvv:pppp'
1032 if (prod_str && mfgr_str) {
1033 snprintf(dev->dev.name, sizeof dev->dev.name,
1034 "%s (%s)", prod_str, mfgr_str);
1035 } else if (prod_str) {
1036 snprintf(dev->dev.name, sizeof dev->dev.name,
1037 "%s (USB device %04x:%04x)",
1038 prod_str, vendor_id, product_id);
1039 } else if (mfgr_str) {
1040 snprintf(dev->dev.name, sizeof dev->dev.name,
1041 "USB device %04x:%04x (%s)",
1042 vendor_id, product_id, mfgr_str);
1045 kfree(buf);
1049 * By the time we get here, we chose a new device address
1050 * and is in the default state. We need to identify the thing and
1051 * get the ball rolling..
1053 * Returns 0 for success, != 0 for error.
1055 * This call is synchronous, and may not be used in an interrupt context.
1057 * Only hub drivers (including virtual root hub drivers for host
1058 * controllers) should ever call this.
1060 #define NEW_DEVICE_RETRYS 2
1061 #define SET_ADDRESS_RETRYS 2
1062 int usb_new_device(struct usb_device *dev, struct device *parent)
1064 int err = 0;
1065 int i;
1066 int j;
1069 * Set the driver for the usb device to point to the "generic" driver.
1070 * This prevents the main usb device from being sent to the usb bus
1071 * probe function. Yes, it's a hack, but a nice one :)
1073 * Do it asap, so more driver model stuff (like the device.h message
1074 * utilities) can be used in hcd submit/unlink code paths.
1076 usb_generic_driver.bus = &usb_bus_type;
1077 dev->dev.parent = parent;
1078 dev->dev.driver = &usb_generic_driver;
1079 dev->dev.bus = &usb_bus_type;
1080 dev->dev.release = usb_release_dev;
1081 dev->dev.driver_data = &usb_generic_driver_data;
1082 usb_get_dev(dev);
1083 if (dev->dev.bus_id[0] == 0)
1084 sprintf (&dev->dev.bus_id[0], "%d-%s",
1085 dev->bus->busnum, dev->devpath);
1087 /* dma masks come from the controller; readonly, except to hcd */
1088 dev->dev.dma_mask = parent->dma_mask;
1090 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
1091 * it's fixed size except for full speed devices.
1093 switch (dev->speed) {
1094 case USB_SPEED_HIGH: /* fixed at 64 */
1095 i = 64;
1096 break;
1097 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
1098 /* to determine the ep0 maxpacket size, read the first 8
1099 * bytes from the device descriptor to get bMaxPacketSize0;
1100 * then correct our initial (small) guess.
1102 // FALLTHROUGH
1103 case USB_SPEED_LOW: /* fixed at 8 */
1104 i = 8;
1105 break;
1106 default:
1107 return -EINVAL;
1109 dev->epmaxpacketin [0] = i;
1110 dev->epmaxpacketout[0] = i;
1112 for (i = 0; i < NEW_DEVICE_RETRYS; ++i) {
1114 for (j = 0; j < SET_ADDRESS_RETRYS; ++j) {
1115 err = usb_set_address(dev);
1116 if (err >= 0)
1117 break;
1118 wait_ms(200);
1120 if (err < 0) {
1121 dev_err(&dev->dev, "USB device not accepting new address=%d (error=%d)\n",
1122 dev->devnum, err);
1123 dev->state = USB_STATE_DEFAULT;
1124 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1125 dev->devnum = -1;
1126 return 1;
1129 wait_ms(10); /* Let the SET_ADDRESS settle */
1131 /* high and low speed devices don't need this... */
1132 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8);
1133 if (err >= 8)
1134 break;
1135 wait_ms(100);
1138 if (err < 8) {
1139 if (err < 0)
1140 dev_err(&dev->dev, "USB device not responding, giving up (error=%d)\n", err);
1141 else
1142 dev_err(&dev->dev, "USB device descriptor short read (expected %i, got %i)\n", 8, err);
1143 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1144 dev->devnum = -1;
1145 return 1;
1147 if (dev->speed == USB_SPEED_FULL) {
1148 dev->epmaxpacketin [0] = dev->descriptor.bMaxPacketSize0;
1149 dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
1152 /* USB device state == addressed ... still not usable */
1154 err = usb_get_device_descriptor(dev);
1155 if (err < (signed)sizeof(dev->descriptor)) {
1156 if (err < 0)
1157 dev_err(&dev->dev, "unable to get device descriptor (error=%d)\n", err);
1158 else
1159 dev_err(&dev->dev, "USB device descriptor short read (expected %Zi, got %i)\n",
1160 sizeof(dev->descriptor), err);
1162 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1163 dev->devnum = -1;
1164 return 1;
1167 err = usb_get_configuration(dev);
1168 if (err < 0) {
1169 dev_err(&dev->dev, "unable to get device %d configuration (error=%d)\n",
1170 dev->devnum, err);
1171 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1172 dev->devnum = -1;
1173 return 1;
1176 /* we set the default configuration here */
1177 err = usb_set_configuration(dev, dev->config[0].desc.bConfigurationValue);
1178 if (err) {
1179 dev_err(&dev->dev, "failed to set device %d default configuration (error=%d)\n",
1180 dev->devnum, err);
1181 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1182 dev->devnum = -1;
1183 return 1;
1186 /* USB device state == configured ... tell the world! */
1188 dev_dbg(&dev->dev, "new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1189 dev->descriptor.iManufacturer, dev->descriptor.iProduct, dev->descriptor.iSerialNumber);
1190 set_device_description (dev);
1192 #ifdef DEBUG
1193 if (dev->descriptor.iSerialNumber)
1194 usb_show_string(dev, "SerialNumber", dev->descriptor.iSerialNumber);
1195 #endif
1197 /* put into sysfs, with device and config specific files */
1198 err = device_add (&dev->dev);
1199 if (err)
1200 return err;
1201 usb_create_driverfs_dev_files (dev);
1203 /* Register all of the interfaces for this device with the driver core.
1204 * Remember, interfaces get bound to drivers, not devices. */
1205 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1206 struct usb_interface *interface = &dev->actconfig->interface[i];
1207 struct usb_interface_descriptor *desc;
1209 desc = &interface->altsetting [interface->act_altsetting].desc;
1210 interface->dev.parent = &dev->dev;
1211 interface->dev.driver = NULL;
1212 interface->dev.bus = &usb_bus_type;
1213 interface->dev.dma_mask = parent->dma_mask;
1214 sprintf (&interface->dev.bus_id[0], "%d-%s:%d",
1215 dev->bus->busnum, dev->devpath,
1216 desc->bInterfaceNumber);
1217 if (!desc->iInterface
1218 || usb_string (dev, desc->iInterface,
1219 interface->dev.name,
1220 sizeof interface->dev.name) <= 0) {
1221 /* typically devices won't bother with interface
1222 * descriptions; this is the normal case. an
1223 * interface's driver might describe it better.
1224 * (also: iInterface is per-altsetting ...)
1226 sprintf (&interface->dev.name[0],
1227 "usb-%s-%s interface %d",
1228 dev->bus->bus_name, dev->devpath,
1229 desc->bInterfaceNumber);
1231 dev_dbg (&dev->dev, "%s - registering interface %s\n", __FUNCTION__, interface->dev.bus_id);
1232 device_add (&interface->dev);
1233 usb_create_driverfs_intf_files (interface);
1236 /* add a /proc/bus/usb entry */
1237 usbfs_add_device(dev);
1239 return 0;
1243 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
1244 * @dev: device the buffer will be used with
1245 * @size: requested buffer size
1246 * @mem_flags: affect whether allocation may block
1247 * @dma: used to return DMA address of buffer
1249 * Return value is either null (indicating no buffer could be allocated), or
1250 * the cpu-space pointer to a buffer that may be used to perform DMA to the
1251 * specified device. Such cpu-space buffers are returned along with the DMA
1252 * address (through the pointer provided).
1254 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
1255 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
1256 * mapping hardware for long idle periods. The implementation varies between
1257 * platforms, depending on details of how DMA will work to this device.
1258 * Using these buffers also helps prevent cacheline sharing problems on
1259 * architectures where CPU caches are not DMA-coherent.
1261 * When the buffer is no longer used, free it with usb_buffer_free().
1263 void *usb_buffer_alloc (
1264 struct usb_device *dev,
1265 size_t size,
1266 int mem_flags,
1267 dma_addr_t *dma
1270 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
1271 return 0;
1272 return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
1276 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
1277 * @dev: device the buffer was used with
1278 * @size: requested buffer size
1279 * @addr: CPU address of buffer
1280 * @dma: DMA address of buffer
1282 * This reclaims an I/O buffer, letting it be reused. The memory must have
1283 * been allocated using usb_buffer_alloc(), and the parameters must match
1284 * those provided in that allocation request.
1286 void usb_buffer_free (
1287 struct usb_device *dev,
1288 size_t size,
1289 void *addr,
1290 dma_addr_t dma
1293 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
1294 return;
1295 dev->bus->op->buffer_free (dev->bus, size, addr, dma);
1299 * usb_buffer_map - create DMA mapping(s) for an urb
1300 * @urb: urb whose transfer_buffer/setup_packet will be mapped
1302 * Return value is either null (indicating no buffer could be mapped), or
1303 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
1304 * added to urb->transfer_flags if the operation succeeds. If the device
1305 * is connected to this system through a non-DMA controller, this operation
1306 * always succeeds.
1308 * This call would normally be used for an urb which is reused, perhaps
1309 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1310 * calls to synchronize memory and dma state.
1312 * Reverse the effect of this call with usb_buffer_unmap().
1314 struct urb *usb_buffer_map (struct urb *urb)
1316 struct usb_bus *bus;
1317 struct device *controller;
1319 if (!urb
1320 || !urb->dev
1321 || !(bus = urb->dev->bus)
1322 || !(controller = bus->controller))
1323 return 0;
1325 if (controller->dma_mask) {
1326 urb->transfer_dma = dma_map_single (controller,
1327 urb->transfer_buffer, urb->transfer_buffer_length,
1328 usb_pipein (urb->pipe)
1329 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1330 if (usb_pipecontrol (urb->pipe))
1331 urb->setup_dma = dma_map_single (controller,
1332 urb->setup_packet,
1333 sizeof (struct usb_ctrlrequest),
1334 DMA_TO_DEVICE);
1335 // FIXME generic api broken like pci, can't report errors
1336 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1337 } else
1338 urb->transfer_dma = ~0;
1339 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1340 | URB_NO_SETUP_DMA_MAP);
1341 return urb;
1345 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1346 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1348 void usb_buffer_dmasync (struct urb *urb)
1350 struct usb_bus *bus;
1351 struct device *controller;
1353 if (!urb
1354 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1355 || !urb->dev
1356 || !(bus = urb->dev->bus)
1357 || !(controller = bus->controller))
1358 return;
1360 if (controller->dma_mask) {
1361 dma_sync_single (controller,
1362 urb->transfer_dma, urb->transfer_buffer_length,
1363 usb_pipein (urb->pipe)
1364 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1365 if (usb_pipecontrol (urb->pipe))
1366 dma_sync_single (controller,
1367 urb->setup_dma,
1368 sizeof (struct usb_ctrlrequest),
1369 DMA_TO_DEVICE);
1374 * usb_buffer_unmap - free DMA mapping(s) for an urb
1375 * @urb: urb whose transfer_buffer will be unmapped
1377 * Reverses the effect of usb_buffer_map().
1379 void usb_buffer_unmap (struct urb *urb)
1381 struct usb_bus *bus;
1382 struct device *controller;
1384 if (!urb
1385 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1386 || !urb->dev
1387 || !(bus = urb->dev->bus)
1388 || !(controller = bus->controller))
1389 return;
1391 if (controller->dma_mask) {
1392 dma_unmap_single (controller,
1393 urb->transfer_dma, urb->transfer_buffer_length,
1394 usb_pipein (urb->pipe)
1395 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1396 if (usb_pipecontrol (urb->pipe))
1397 dma_unmap_single (controller,
1398 urb->setup_dma,
1399 sizeof (struct usb_ctrlrequest),
1400 DMA_TO_DEVICE);
1402 urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
1403 | URB_NO_SETUP_DMA_MAP);
1407 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1408 * @dev: device to which the scatterlist will be mapped
1409 * @pipe: endpoint defining the mapping direction
1410 * @sg: the scatterlist to map
1411 * @nents: the number of entries in the scatterlist
1413 * Return value is either < 0 (indicating no buffers could be mapped), or
1414 * the number of DMA mapping array entries in the scatterlist.
1416 * The caller is responsible for placing the resulting DMA addresses from
1417 * the scatterlist into URB transfer buffer pointers, and for setting the
1418 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1420 * Top I/O rates come from queuing URBs, instead of waiting for each one
1421 * to complete before starting the next I/O. This is particularly easy
1422 * to do with scatterlists. Just allocate and submit one URB for each DMA
1423 * mapping entry returned, stopping on the first error or when all succeed.
1424 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1426 * This call would normally be used when translating scatterlist requests,
1427 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1428 * may be able to coalesce mappings for improved I/O efficiency.
1430 * Reverse the effect of this call with usb_buffer_unmap_sg().
1432 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
1433 struct scatterlist *sg, int nents)
1435 struct usb_bus *bus;
1436 struct device *controller;
1438 if (!dev
1439 || usb_pipecontrol (pipe)
1440 || !(bus = dev->bus)
1441 || !(controller = bus->controller)
1442 || !controller->dma_mask)
1443 return -1;
1445 // FIXME generic api broken like pci, can't report errors
1446 return dma_map_sg (controller, sg, nents,
1447 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1451 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1452 * @dev: device to which the scatterlist will be mapped
1453 * @pipe: endpoint defining the mapping direction
1454 * @sg: the scatterlist to synchronize
1455 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1457 * Use this when you are re-using a scatterlist's data buffers for
1458 * another USB request.
1460 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
1461 struct scatterlist *sg, int n_hw_ents)
1463 struct usb_bus *bus;
1464 struct device *controller;
1466 if (!dev
1467 || !(bus = dev->bus)
1468 || !(controller = bus->controller)
1469 || !controller->dma_mask)
1470 return;
1472 dma_sync_sg (controller, sg, n_hw_ents,
1473 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1477 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1478 * @dev: device to which the scatterlist will be mapped
1479 * @pipe: endpoint defining the mapping direction
1480 * @sg: the scatterlist to unmap
1481 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1483 * Reverses the effect of usb_buffer_map_sg().
1485 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1486 struct scatterlist *sg, int n_hw_ents)
1488 struct usb_bus *bus;
1489 struct device *controller;
1491 if (!dev
1492 || !(bus = dev->bus)
1493 || !(controller = bus->controller)
1494 || !controller->dma_mask)
1495 return;
1497 dma_unmap_sg (controller, sg, n_hw_ents,
1498 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1502 struct bus_type usb_bus_type = {
1503 .name = "usb",
1504 .match = usb_device_match,
1505 .hotplug = usb_hotplug,
1508 #ifndef MODULE
1510 static int __init usb_setup_disable(char *str)
1512 nousb = 1;
1513 return 1;
1516 /* format to disable USB on kernel command line is: nousb */
1517 __setup("nousb", usb_setup_disable);
1519 #endif
1522 * for external read access to <nousb>
1524 int usb_disabled(void)
1526 return nousb;
1530 * Init
1532 static int __init usb_init(void)
1534 if (nousb) {
1535 info("USB support disabled\n");
1536 return 0;
1539 bus_register(&usb_bus_type);
1540 usb_host_init();
1541 usb_major_init();
1542 usbfs_init();
1543 usb_hub_init();
1545 driver_register(&usb_generic_driver);
1547 return 0;
1551 * Cleanup
1553 static void __exit usb_exit(void)
1555 /* This will matter if shutdown/reboot does exitcalls. */
1556 if (nousb)
1557 return;
1559 driver_unregister(&usb_generic_driver);
1560 usb_major_cleanup();
1561 usbfs_cleanup();
1562 usb_hub_cleanup();
1563 usb_host_cleanup();
1564 bus_unregister(&usb_bus_type);
1567 subsys_initcall(usb_init);
1568 module_exit(usb_exit);
1571 * USB may be built into the kernel or be built as modules.
1572 * These symbols are exported for device (or host controller)
1573 * driver modules to use.
1575 EXPORT_SYMBOL(usb_epnum_to_ep_desc);
1577 EXPORT_SYMBOL(usb_register);
1578 EXPORT_SYMBOL(usb_deregister);
1579 EXPORT_SYMBOL(usb_disabled);
1581 EXPORT_SYMBOL(usb_device_probe);
1582 EXPORT_SYMBOL(usb_device_remove);
1584 EXPORT_SYMBOL(usb_alloc_dev);
1585 EXPORT_SYMBOL(usb_put_dev);
1586 EXPORT_SYMBOL(usb_get_dev);
1587 EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
1589 EXPORT_SYMBOL(usb_driver_claim_interface);
1590 EXPORT_SYMBOL(usb_interface_claimed);
1591 EXPORT_SYMBOL(usb_driver_release_interface);
1592 EXPORT_SYMBOL(usb_match_id);
1593 EXPORT_SYMBOL(usb_find_interface);
1594 EXPORT_SYMBOL(usb_ifnum_to_if);
1596 EXPORT_SYMBOL(usb_new_device);
1597 EXPORT_SYMBOL(usb_reset_device);
1598 EXPORT_SYMBOL(usb_connect);
1599 EXPORT_SYMBOL(usb_disconnect);
1601 EXPORT_SYMBOL(__usb_get_extra_descriptor);
1603 EXPORT_SYMBOL(usb_find_device);
1604 EXPORT_SYMBOL(usb_get_current_frame_number);
1606 EXPORT_SYMBOL (usb_buffer_alloc);
1607 EXPORT_SYMBOL (usb_buffer_free);
1609 EXPORT_SYMBOL (usb_buffer_map);
1610 EXPORT_SYMBOL (usb_buffer_dmasync);
1611 EXPORT_SYMBOL (usb_buffer_unmap);
1613 EXPORT_SYMBOL (usb_buffer_map_sg);
1614 EXPORT_SYMBOL (usb_buffer_dmasync_sg);
1615 EXPORT_SYMBOL (usb_buffer_unmap_sg);
1617 MODULE_LICENSE("GPL");