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-2004
11 * (C) Copyright Yggdrasil Computing, Inc. 2000
12 * (usb_device_id matching changes by Adam J. Richter)
13 * (C) Copyright Greg Kroah-Hartman 2002-2003
15 * NOTE! This is not actually a driver at all, rather this is
16 * just a collection of helper routines that implement the
17 * generic USB things that the real drivers can use..
19 * Think of this as a "USB library" rather than anything else.
20 * It should be considered a slave, with no callbacks. Callbacks
24 #include <linux/config.h>
26 #ifdef CONFIG_USB_DEBUG
32 #include <linux/module.h>
33 #include <linux/string.h>
34 #include <linux/bitops.h>
35 #include <linux/slab.h>
36 #include <linux/interrupt.h> /* for in_interrupt() */
37 #include <linux/kmod.h>
38 #include <linux/init.h>
39 #include <linux/spinlock.h>
40 #include <linux/errno.h>
41 #include <linux/smp_lock.h>
42 #include <linux/usb.h>
45 #include <asm/scatterlist.h>
47 #include <linux/dma-mapping.h>
52 extern int usb_hub_init(void);
53 extern void usb_hub_cleanup(void);
54 extern int usb_major_init(void);
55 extern void usb_major_cleanup(void);
56 extern int usb_host_init(void);
57 extern void usb_host_cleanup(void);
60 const char *usbcore_name = "usbcore";
62 int nousb; /* Disable USB when built into kernel image */
63 /* Not honored on modular build */
66 static int generic_probe (struct device *dev)
70 static int generic_remove (struct device *dev)
75 static struct device_driver usb_generic_driver = {
78 .probe = generic_probe,
79 .remove = generic_remove,
82 static int usb_generic_driver_data;
84 /* called from driver core with usb_bus_type.subsys writelock */
85 int usb_probe_interface(struct device *dev)
87 struct usb_interface * intf = to_usb_interface(dev);
88 struct usb_driver * driver = to_usb_driver(dev->driver);
89 const struct usb_device_id *id;
92 dev_dbg(dev, "%s\n", __FUNCTION__);
97 id = usb_match_id (intf, driver->id_table);
99 dev_dbg (dev, "%s - got id\n", __FUNCTION__);
100 error = driver->probe (intf, id);
106 /* called from driver core with usb_bus_type.subsys writelock */
107 int usb_unbind_interface(struct device *dev)
109 struct usb_interface *intf = to_usb_interface(dev);
110 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
112 /* release all urbs for this interface */
113 usb_disable_interface(interface_to_usbdev(intf), intf);
115 if (driver && driver->disconnect)
116 driver->disconnect(intf);
118 /* reset other interface state */
119 usb_set_interface(interface_to_usbdev(intf),
120 intf->altsetting[0].desc.bInterfaceNumber,
122 usb_set_intfdata(intf, NULL);
128 * usb_register - register a USB driver
129 * @new_driver: USB operations for the driver
131 * Registers a USB driver with the USB core. The list of unattached
132 * interfaces will be rescanned whenever a new driver is added, allowing
133 * the new driver to attach to any recognized devices.
134 * Returns a negative error code on failure and 0 on success.
136 * NOTE: if you want your driver to use the USB major number, you must call
137 * usb_register_dev() to enable that functionality. This function no longer
138 * takes care of that.
140 int usb_register(struct usb_driver *new_driver)
147 new_driver->driver.name = (char *)new_driver->name;
148 new_driver->driver.bus = &usb_bus_type;
149 new_driver->driver.probe = usb_probe_interface;
150 new_driver->driver.remove = usb_unbind_interface;
152 retval = driver_register(&new_driver->driver);
155 pr_info("%s: registered new driver %s\n",
156 usbcore_name, new_driver->name);
157 usbfs_update_special();
159 printk(KERN_ERR "%s: error %d registering driver %s\n",
160 usbcore_name, retval, new_driver->name);
167 * usb_deregister - unregister a USB driver
168 * @driver: USB operations of the driver to unregister
169 * Context: must be able to sleep
171 * Unlinks the specified driver from the internal USB driver list.
173 * NOTE: If you called usb_register_dev(), you still need to call
174 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
175 * this * call will no longer do it for you.
177 void usb_deregister(struct usb_driver *driver)
179 pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
181 driver_unregister (&driver->driver);
183 usbfs_update_special();
187 * usb_ifnum_to_if - get the interface object with a given interface number
188 * @dev: the device whose current configuration is considered
189 * @ifnum: the desired interface
191 * This walks the device descriptor for the currently active configuration
192 * and returns a pointer to the interface with that particular interface
195 * Note that configuration descriptors are not required to assign interface
196 * numbers sequentially, so that it would be incorrect to assume that
197 * the first interface in that descriptor corresponds to interface zero.
198 * This routine helps device drivers avoid such mistakes.
199 * However, you should make sure that you do the right thing with any
200 * alternate settings available for this interfaces.
202 * Don't call this function unless you are bound to one of the interfaces
203 * on this device or you own the dev->serialize semaphore!
205 struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
207 struct usb_host_config *config = dev->actconfig;
212 for (i = 0; i < config->desc.bNumInterfaces; i++)
213 if (config->interface[i]->altsetting[0]
214 .desc.bInterfaceNumber == ifnum)
215 return config->interface[i];
221 * usb_altnum_to_altsetting - get the altsetting structure with a given
222 * alternate setting number.
223 * @intf: the interface containing the altsetting in question
224 * @altnum: the desired alternate setting number
226 * This searches the altsetting array of the specified interface for
227 * an entry with the correct bAlternateSetting value and returns a pointer
228 * to that entry, or null.
230 * Note that altsettings need not be stored sequentially by number, so
231 * it would be incorrect to assume that the first altsetting entry in
232 * the array corresponds to altsetting zero. This routine helps device
233 * drivers avoid such mistakes.
235 * Don't call this function unless you are bound to the intf interface
236 * or you own the device's ->serialize semaphore!
238 struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
243 for (i = 0; i < intf->num_altsetting; i++) {
244 if (intf->altsetting[i].desc.bAlternateSetting == altnum)
245 return &intf->altsetting[i];
251 * usb_epnum_to_ep_desc - get the endpoint object with a given endpoint number
252 * @dev: the device whose current configuration+altsettings is considered
253 * @epnum: the desired endpoint, masked with USB_DIR_IN as appropriate.
255 * This walks the device descriptor for the currently active configuration,
256 * and returns a pointer to the endpoint with that particular endpoint
259 * Note that interface descriptors are not required to list endpoint
260 * numbers in any standardized order, so that it would be wrong to
261 * assume that ep2in precedes either ep5in, ep2out, or even ep1out.
262 * This routine helps device drivers avoid such mistakes.
264 struct usb_endpoint_descriptor *
265 usb_epnum_to_ep_desc(struct usb_device *dev, unsigned epnum)
267 struct usb_host_config *config = dev->actconfig;
272 for (i = 0; i < config->desc.bNumInterfaces; i++) {
273 struct usb_interface *intf;
274 struct usb_host_interface *alt;
276 /* only endpoints in current altsetting are active */
277 intf = config->interface[i];
278 alt = intf->cur_altsetting;
280 for (k = 0; k < alt->desc.bNumEndpoints; k++)
281 if (epnum == alt->endpoint[k].desc.bEndpointAddress)
282 return &alt->endpoint[k].desc;
289 * usb_driver_claim_interface - bind a driver to an interface
290 * @driver: the driver to be bound
291 * @iface: the interface to which it will be bound; must be in the
292 * usb device's active configuration
293 * @priv: driver data associated with that interface
295 * This is used by usb device drivers that need to claim more than one
296 * interface on a device when probing (audio and acm are current examples).
297 * No device driver should directly modify internal usb_interface or
298 * usb_device structure members.
300 * Few drivers should need to use this routine, since the most natural
301 * way to bind to an interface is to return the private data from
302 * the driver's probe() method.
304 * Callers must own the driver model's usb bus writelock. So driver
305 * probe() entries don't need extra locking, but other call contexts
306 * may need to explicitly claim that lock.
308 int usb_driver_claim_interface(struct usb_driver *driver, struct usb_interface *iface, void* priv)
310 struct device *dev = &iface->dev;
315 dev->driver = &driver->driver;
316 usb_set_intfdata(iface, priv);
318 /* if interface was already added, bind now; else let
319 * the future device_add() bind it, bypassing probe()
321 if (!list_empty (&dev->bus_list))
322 device_bind_driver(dev);
328 * usb_driver_release_interface - unbind a driver from an interface
329 * @driver: the driver to be unbound
330 * @iface: the interface from which it will be unbound
332 * This can be used by drivers to release an interface without waiting
333 * for their disconnect() methods to be called. In typical cases this
334 * also causes the driver disconnect() method to be called.
336 * This call is synchronous, and may not be used in an interrupt context.
337 * Callers must own the usb_device serialize semaphore and the driver model's
338 * usb bus writelock. So driver disconnect() entries don't need extra locking,
339 * but other call contexts may need to explicitly claim those locks.
341 void usb_driver_release_interface(struct usb_driver *driver,
342 struct usb_interface *iface)
344 struct device *dev = &iface->dev;
346 /* this should never happen, don't release something that's not ours */
347 if (!dev->driver || dev->driver != &driver->driver)
350 /* don't disconnect from disconnect(), or before dev_add() */
351 if (!list_empty (&dev->driver_list) && !list_empty (&dev->bus_list))
352 device_release_driver(dev);
355 usb_set_intfdata(iface, NULL);
359 * usb_match_id - find first usb_device_id matching device or interface
360 * @interface: the interface of interest
361 * @id: array of usb_device_id structures, terminated by zero entry
363 * usb_match_id searches an array of usb_device_id's and returns
364 * the first one matching the device or interface, or null.
365 * This is used when binding (or rebinding) a driver to an interface.
366 * Most USB device drivers will use this indirectly, through the usb core,
367 * but some layered driver frameworks use it directly.
368 * These device tables are exported with MODULE_DEVICE_TABLE, through
369 * modutils and "modules.usbmap", to support the driver loading
370 * functionality of USB hotplugging.
374 * The "match_flags" element in a usb_device_id controls which
375 * members are used. If the corresponding bit is set, the
376 * value in the device_id must match its corresponding member
377 * in the device or interface descriptor, or else the device_id
380 * "driver_info" is normally used only by device drivers,
381 * but you can create a wildcard "matches anything" usb_device_id
382 * as a driver's "modules.usbmap" entry if you provide an id with
383 * only a nonzero "driver_info" field. If you do this, the USB device
384 * driver's probe() routine should use additional intelligence to
385 * decide whether to bind to the specified interface.
387 * What Makes Good usb_device_id Tables:
389 * The match algorithm is very simple, so that intelligence in
390 * driver selection must come from smart driver id records.
391 * Unless you have good reasons to use another selection policy,
392 * provide match elements only in related groups, and order match
393 * specifiers from specific to general. Use the macros provided
394 * for that purpose if you can.
396 * The most specific match specifiers use device descriptor
397 * data. These are commonly used with product-specific matches;
398 * the USB_DEVICE macro lets you provide vendor and product IDs,
399 * and you can also match against ranges of product revisions.
400 * These are widely used for devices with application or vendor
401 * specific bDeviceClass values.
403 * Matches based on device class/subclass/protocol specifications
404 * are slightly more general; use the USB_DEVICE_INFO macro, or
405 * its siblings. These are used with single-function devices
406 * where bDeviceClass doesn't specify that each interface has
409 * Matches based on interface class/subclass/protocol are the
410 * most general; they let drivers bind to any interface on a
411 * multiple-function device. Use the USB_INTERFACE_INFO
412 * macro, or its siblings, to match class-per-interface style
413 * devices (as recorded in bDeviceClass).
415 * Within those groups, remember that not all combinations are
416 * meaningful. For example, don't give a product version range
417 * without vendor and product IDs; or specify a protocol without
418 * its associated class and subclass.
420 const struct usb_device_id *
421 usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
423 struct usb_host_interface *intf;
424 struct usb_device *dev;
426 /* proc_connectinfo in devio.c may call us with id == NULL. */
430 intf = interface->cur_altsetting;
431 dev = interface_to_usbdev(interface);
433 /* It is important to check that id->driver_info is nonzero,
434 since an entry that is all zeroes except for a nonzero
435 id->driver_info is the way to create an entry that
436 indicates that the driver want to examine every
437 device and interface. */
438 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
439 id->driver_info; id++) {
441 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
442 id->idVendor != dev->descriptor.idVendor)
445 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
446 id->idProduct != dev->descriptor.idProduct)
449 /* No need to test id->bcdDevice_lo != 0, since 0 is never
450 greater than any unsigned number. */
451 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
452 (id->bcdDevice_lo > dev->descriptor.bcdDevice))
455 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
456 (id->bcdDevice_hi < dev->descriptor.bcdDevice))
459 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
460 (id->bDeviceClass != dev->descriptor.bDeviceClass))
463 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
464 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
467 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
468 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
471 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
472 (id->bInterfaceClass != intf->desc.bInterfaceClass))
475 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
476 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
479 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
480 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
490 * usb_find_interface - find usb_interface pointer for driver and device
491 * @drv: the driver whose current configuration is considered
492 * @minor: the minor number of the desired device
494 * This walks the driver device list and returns a pointer to the interface
495 * with the matching minor. Note, this only works for devices that share the
498 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
500 struct list_head *entry;
502 struct usb_interface *intf;
504 list_for_each(entry, &drv->driver.devices) {
505 dev = container_of(entry, struct device, driver_list);
507 /* can't look at usb devices, only interfaces */
508 if (dev->driver == &usb_generic_driver)
511 intf = to_usb_interface(dev);
512 if (intf->minor == -1)
514 if (intf->minor == minor)
518 /* no device found that matches */
522 static int usb_device_match (struct device *dev, struct device_driver *drv)
524 struct usb_interface *intf;
525 struct usb_driver *usb_drv;
526 const struct usb_device_id *id;
528 /* check for generic driver, which we don't match any device with */
529 if (drv == &usb_generic_driver)
532 intf = to_usb_interface(dev);
534 usb_drv = to_usb_driver(drv);
535 id = usb_drv->id_table;
537 id = usb_match_id (intf, usb_drv->id_table);
545 #ifdef CONFIG_HOTPLUG
548 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
549 * (normally /sbin/hotplug) when USB devices get added or removed.
551 * This invokes a user mode policy agent, typically helping to load driver
552 * or other modules, configure the device, and more. Drivers can provide
553 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
555 * We're called either from khubd (the typical case) or from root hub
556 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
557 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
558 * device (and this configuration!) are still present.
560 static int usb_hotplug (struct device *dev, char **envp, int num_envp,
561 char *buffer, int buffer_size)
563 struct usb_interface *intf;
564 struct usb_device *usb_dev;
572 /* driver is often null here; dev_dbg() would oops */
573 pr_debug ("usb %s: hotplug\n", dev->bus_id);
575 /* Must check driver_data here, as on remove driver is always NULL */
576 if ((dev->driver == &usb_generic_driver) ||
577 (dev->driver_data == &usb_generic_driver_data))
580 intf = to_usb_interface(dev);
581 usb_dev = interface_to_usbdev (intf);
583 if (usb_dev->devnum < 0) {
584 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
588 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
594 #ifdef CONFIG_USB_DEVICEFS
595 /* If this is available, userspace programs can directly read
596 * all the device descriptors we don't tell them about. Or
597 * even act as usermode drivers.
599 * FIXME reduce hardwired intelligence here
601 envp [i++] = scratch;
602 length += snprintf (scratch, buffer_size - length,
603 "DEVICE=/proc/bus/usb/%03d/%03d",
604 usb_dev->bus->busnum, usb_dev->devnum);
605 if ((buffer_size - length <= 0) || (i >= num_envp))
611 /* per-device configurations are common */
612 envp [i++] = scratch;
613 length += snprintf (scratch, buffer_size - length, "PRODUCT=%x/%x/%x",
614 usb_dev->descriptor.idVendor,
615 usb_dev->descriptor.idProduct,
616 usb_dev->descriptor.bcdDevice);
617 if ((buffer_size - length <= 0) || (i >= num_envp))
622 /* class-based driver binding models */
623 envp [i++] = scratch;
624 length += snprintf (scratch, buffer_size - length, "TYPE=%d/%d/%d",
625 usb_dev->descriptor.bDeviceClass,
626 usb_dev->descriptor.bDeviceSubClass,
627 usb_dev->descriptor.bDeviceProtocol);
628 if ((buffer_size - length <= 0) || (i >= num_envp))
633 if (usb_dev->descriptor.bDeviceClass == 0) {
634 struct usb_host_interface *alt = intf->cur_altsetting;
636 /* 2.4 only exposed interface zero. in 2.5, hotplug
637 * agents are called for all interfaces, and can use
638 * $DEVPATH/bInterfaceNumber if necessary.
640 envp [i++] = scratch;
641 length += snprintf (scratch, buffer_size - length,
642 "INTERFACE=%d/%d/%d",
643 alt->desc.bInterfaceClass,
644 alt->desc.bInterfaceSubClass,
645 alt->desc.bInterfaceProtocol);
646 if ((buffer_size - length <= 0) || (i >= num_envp))
659 static int usb_hotplug (struct device *dev, char **envp,
660 int num_envp, char *buffer, int buffer_size)
665 #endif /* CONFIG_HOTPLUG */
668 * usb_release_dev - free a usb device structure when all users of it are finished.
669 * @dev: device that's been disconnected
671 * Will be called only by the device core when all users of this usb device are
674 static void usb_release_dev(struct device *dev)
676 struct usb_device *udev;
678 udev = to_usb_device(dev);
680 if (udev->bus && udev->bus->op && udev->bus->op->deallocate)
681 udev->bus->op->deallocate(udev);
682 usb_destroy_configuration(udev);
683 usb_bus_put(udev->bus);
688 * usb_alloc_dev - usb device constructor (usbcore-internal)
689 * @parent: hub to which device is connected; null to allocate a root hub
690 * @bus: bus used to access the device
691 * @port: zero based index of port; ignored for root hubs
692 * Context: !in_interrupt ()
694 * Only hub drivers (including virtual root hub drivers for host
695 * controllers) should ever call this.
697 * This call may not be used in a non-sleeping context.
700 usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port)
702 struct usb_device *dev;
704 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
708 memset(dev, 0, sizeof(*dev));
710 bus = usb_bus_get(bus);
716 device_initialize(&dev->dev);
717 dev->dev.bus = &usb_bus_type;
718 dev->dev.dma_mask = bus->controller->dma_mask;
719 dev->dev.driver_data = &usb_generic_driver_data;
720 dev->dev.driver = &usb_generic_driver;
721 dev->dev.release = usb_release_dev;
722 dev->state = USB_STATE_ATTACHED;
724 /* Save readable and stable topology id, distinguishing devices
725 * by location for diagnostics, tools, driver model, etc. The
726 * string is a path along hub ports, from the root. Each device's
727 * dev->devpath will be stable until USB is re-cabled, and hubs
728 * are often labeled with these port numbers. The bus_id isn't
729 * as stable: bus->busnum changes easily from modprobe order,
730 * cardbus or pci hotplugging, and so on.
732 if (unlikely (!parent)) {
733 dev->devpath [0] = '0';
735 dev->dev.parent = bus->controller;
736 sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
738 /* match any labeling on the hubs; it's one-based */
739 if (parent->devpath [0] == '0')
740 snprintf (dev->devpath, sizeof dev->devpath,
743 snprintf (dev->devpath, sizeof dev->devpath,
744 "%s.%d", parent->devpath, port + 1);
746 dev->dev.parent = &parent->dev;
747 sprintf (&dev->dev.bus_id[0], "%d-%s",
748 bus->busnum, dev->devpath);
750 /* hub driver sets up TT records */
754 dev->parent = parent;
755 INIT_LIST_HEAD(&dev->filelist);
757 init_MUTEX(&dev->serialize);
759 if (dev->bus->op->allocate)
760 dev->bus->op->allocate(dev);
766 * usb_get_dev - increments the reference count of the usb device structure
767 * @dev: the device being referenced
769 * Each live reference to a device should be refcounted.
771 * Drivers for USB interfaces should normally record such references in
772 * their probe() methods, when they bind to an interface, and release
773 * them by calling usb_put_dev(), in their disconnect() methods.
775 * A pointer to the device with the incremented reference counter is returned.
777 struct usb_device *usb_get_dev(struct usb_device *dev)
780 get_device(&dev->dev);
785 * usb_put_dev - release a use of the usb device structure
786 * @dev: device that's been disconnected
788 * Must be called when a user of a device is finished with it. When the last
789 * user of the device calls this function, the memory of the device is freed.
791 void usb_put_dev(struct usb_device *dev)
794 put_device(&dev->dev);
798 * usb_get_intf - increments the reference count of the usb interface structure
799 * @intf: the interface being referenced
801 * Each live reference to a interface must be refcounted.
803 * Drivers for USB interfaces should normally record such references in
804 * their probe() methods, when they bind to an interface, and release
805 * them by calling usb_put_intf(), in their disconnect() methods.
807 * A pointer to the interface with the incremented reference counter is
810 struct usb_interface *usb_get_intf(struct usb_interface *intf)
813 get_device(&intf->dev);
818 * usb_put_intf - release a use of the usb interface structure
819 * @intf: interface that's been decremented
821 * Must be called when a user of an interface is finished with it. When the
822 * last user of the interface calls this function, the memory of the interface
825 void usb_put_intf(struct usb_interface *intf)
828 put_device(&intf->dev);
831 static struct usb_device *match_device(struct usb_device *dev,
832 u16 vendor_id, u16 product_id)
834 struct usb_device *ret_dev = NULL;
837 dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
838 dev->descriptor.idVendor,
839 dev->descriptor.idProduct);
841 /* see if this device matches */
842 if ((dev->descriptor.idVendor == vendor_id) &&
843 (dev->descriptor.idProduct == product_id)) {
844 dev_dbg (&dev->dev, "matched this device!\n");
845 ret_dev = usb_get_dev(dev);
849 /* look through all of the children of this device */
850 for (child = 0; child < dev->maxchild; ++child) {
851 if (dev->children[child]) {
852 ret_dev = match_device(dev->children[child],
853 vendor_id, product_id);
863 * usb_find_device - find a specific usb device in the system
864 * @vendor_id: the vendor id of the device to find
865 * @product_id: the product id of the device to find
867 * Returns a pointer to a struct usb_device if such a specified usb
868 * device is present in the system currently. The usage count of the
869 * device will be incremented if a device is found. Make sure to call
870 * usb_put_dev() when the caller is finished with the device.
872 * If a device with the specified vendor and product id is not found,
875 struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
877 struct list_head *buslist;
879 struct usb_device *dev = NULL;
881 down(&usb_bus_list_lock);
882 for (buslist = usb_bus_list.next;
883 buslist != &usb_bus_list;
884 buslist = buslist->next) {
885 bus = container_of(buslist, struct usb_bus, bus_list);
888 dev = match_device(bus->root_hub, vendor_id, product_id);
893 up(&usb_bus_list_lock);
898 * usb_get_current_frame_number - return current bus frame number
899 * @dev: the device whose bus is being queried
901 * Returns the current frame number for the USB host controller
902 * used with the given USB device. This can be used when scheduling
903 * isochronous requests.
905 * Note that different kinds of host controller have different
906 * "scheduling horizons". While one type might support scheduling only
907 * 32 frames into the future, others could support scheduling up to
908 * 1024 frames into the future.
910 int usb_get_current_frame_number(struct usb_device *dev)
912 return dev->bus->op->get_frame_number (dev);
915 /*-------------------------------------------------------------------*/
917 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
918 * extra field of the interface and endpoint descriptor structs.
921 int __usb_get_extra_descriptor(char *buffer, unsigned size,
922 unsigned char type, void **ptr)
924 struct usb_descriptor_header *header;
926 while (size >= sizeof(struct usb_descriptor_header)) {
927 header = (struct usb_descriptor_header *)buffer;
929 if (header->bLength < 2) {
931 "%s: bogus descriptor, type %d length %d\n",
933 header->bDescriptorType,
938 if (header->bDescriptorType == type) {
943 buffer += header->bLength;
944 size -= header->bLength;
950 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
951 * @dev: device the buffer will be used with
952 * @size: requested buffer size
953 * @mem_flags: affect whether allocation may block
954 * @dma: used to return DMA address of buffer
956 * Return value is either null (indicating no buffer could be allocated), or
957 * the cpu-space pointer to a buffer that may be used to perform DMA to the
958 * specified device. Such cpu-space buffers are returned along with the DMA
959 * address (through the pointer provided).
961 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
962 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
963 * mapping hardware for long idle periods. The implementation varies between
964 * platforms, depending on details of how DMA will work to this device.
965 * Using these buffers also helps prevent cacheline sharing problems on
966 * architectures where CPU caches are not DMA-coherent.
968 * When the buffer is no longer used, free it with usb_buffer_free().
970 void *usb_buffer_alloc (
971 struct usb_device *dev,
977 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
979 return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
983 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
984 * @dev: device the buffer was used with
985 * @size: requested buffer size
986 * @addr: CPU address of buffer
987 * @dma: DMA address of buffer
989 * This reclaims an I/O buffer, letting it be reused. The memory must have
990 * been allocated using usb_buffer_alloc(), and the parameters must match
991 * those provided in that allocation request.
993 void usb_buffer_free (
994 struct usb_device *dev,
1000 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
1002 dev->bus->op->buffer_free (dev->bus, size, addr, dma);
1006 * usb_buffer_map - create DMA mapping(s) for an urb
1007 * @urb: urb whose transfer_buffer/setup_packet will be mapped
1009 * Return value is either null (indicating no buffer could be mapped), or
1010 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
1011 * added to urb->transfer_flags if the operation succeeds. If the device
1012 * is connected to this system through a non-DMA controller, this operation
1015 * This call would normally be used for an urb which is reused, perhaps
1016 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1017 * calls to synchronize memory and dma state.
1019 * Reverse the effect of this call with usb_buffer_unmap().
1021 struct urb *usb_buffer_map (struct urb *urb)
1023 struct usb_bus *bus;
1024 struct device *controller;
1028 || !(bus = urb->dev->bus)
1029 || !(controller = bus->controller))
1032 if (controller->dma_mask) {
1033 urb->transfer_dma = dma_map_single (controller,
1034 urb->transfer_buffer, urb->transfer_buffer_length,
1035 usb_pipein (urb->pipe)
1036 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1037 if (usb_pipecontrol (urb->pipe))
1038 urb->setup_dma = dma_map_single (controller,
1040 sizeof (struct usb_ctrlrequest),
1042 // FIXME generic api broken like pci, can't report errors
1043 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1045 urb->transfer_dma = ~0;
1046 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1047 | URB_NO_SETUP_DMA_MAP);
1051 /* XXX DISABLED, no users currently. If you wish to re-enable this
1052 * XXX please determine whether the sync is to transfer ownership of
1053 * XXX the buffer from device to cpu or vice verse, and thusly use the
1054 * XXX appropriate _for_{cpu,device}() method. -DaveM
1059 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1060 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1062 void usb_buffer_dmasync (struct urb *urb)
1064 struct usb_bus *bus;
1065 struct device *controller;
1068 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1070 || !(bus = urb->dev->bus)
1071 || !(controller = bus->controller))
1074 if (controller->dma_mask) {
1075 dma_sync_single (controller,
1076 urb->transfer_dma, urb->transfer_buffer_length,
1077 usb_pipein (urb->pipe)
1078 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1079 if (usb_pipecontrol (urb->pipe))
1080 dma_sync_single (controller,
1082 sizeof (struct usb_ctrlrequest),
1089 * usb_buffer_unmap - free DMA mapping(s) for an urb
1090 * @urb: urb whose transfer_buffer will be unmapped
1092 * Reverses the effect of usb_buffer_map().
1094 void usb_buffer_unmap (struct urb *urb)
1096 struct usb_bus *bus;
1097 struct device *controller;
1100 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1102 || !(bus = urb->dev->bus)
1103 || !(controller = bus->controller))
1106 if (controller->dma_mask) {
1107 dma_unmap_single (controller,
1108 urb->transfer_dma, urb->transfer_buffer_length,
1109 usb_pipein (urb->pipe)
1110 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1111 if (usb_pipecontrol (urb->pipe))
1112 dma_unmap_single (controller,
1114 sizeof (struct usb_ctrlrequest),
1117 urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
1118 | URB_NO_SETUP_DMA_MAP);
1122 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1123 * @dev: device to which the scatterlist will be mapped
1124 * @pipe: endpoint defining the mapping direction
1125 * @sg: the scatterlist to map
1126 * @nents: the number of entries in the scatterlist
1128 * Return value is either < 0 (indicating no buffers could be mapped), or
1129 * the number of DMA mapping array entries in the scatterlist.
1131 * The caller is responsible for placing the resulting DMA addresses from
1132 * the scatterlist into URB transfer buffer pointers, and for setting the
1133 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1135 * Top I/O rates come from queuing URBs, instead of waiting for each one
1136 * to complete before starting the next I/O. This is particularly easy
1137 * to do with scatterlists. Just allocate and submit one URB for each DMA
1138 * mapping entry returned, stopping on the first error or when all succeed.
1139 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1141 * This call would normally be used when translating scatterlist requests,
1142 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1143 * may be able to coalesce mappings for improved I/O efficiency.
1145 * Reverse the effect of this call with usb_buffer_unmap_sg().
1147 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
1148 struct scatterlist *sg, int nents)
1150 struct usb_bus *bus;
1151 struct device *controller;
1154 || usb_pipecontrol (pipe)
1155 || !(bus = dev->bus)
1156 || !(controller = bus->controller)
1157 || !controller->dma_mask)
1160 // FIXME generic api broken like pci, can't report errors
1161 return dma_map_sg (controller, sg, nents,
1162 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1165 /* XXX DISABLED, no users currently. If you wish to re-enable this
1166 * XXX please determine whether the sync is to transfer ownership of
1167 * XXX the buffer from device to cpu or vice verse, and thusly use the
1168 * XXX appropriate _for_{cpu,device}() method. -DaveM
1173 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1174 * @dev: device to which the scatterlist will be mapped
1175 * @pipe: endpoint defining the mapping direction
1176 * @sg: the scatterlist to synchronize
1177 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1179 * Use this when you are re-using a scatterlist's data buffers for
1180 * another USB request.
1182 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
1183 struct scatterlist *sg, int n_hw_ents)
1185 struct usb_bus *bus;
1186 struct device *controller;
1189 || !(bus = dev->bus)
1190 || !(controller = bus->controller)
1191 || !controller->dma_mask)
1194 dma_sync_sg (controller, sg, n_hw_ents,
1195 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1200 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1201 * @dev: device to which the scatterlist will be mapped
1202 * @pipe: endpoint defining the mapping direction
1203 * @sg: the scatterlist to unmap
1204 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1206 * Reverses the effect of usb_buffer_map_sg().
1208 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1209 struct scatterlist *sg, int n_hw_ents)
1211 struct usb_bus *bus;
1212 struct device *controller;
1215 || !(bus = dev->bus)
1216 || !(controller = bus->controller)
1217 || !controller->dma_mask)
1220 dma_unmap_sg (controller, sg, n_hw_ents,
1221 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1224 static int usb_generic_suspend(struct device *dev, u32 state)
1226 struct usb_interface *intf;
1227 struct usb_driver *driver;
1229 if (dev->driver == &usb_generic_driver)
1230 return usb_suspend_device (to_usb_device(dev), state);
1232 if ((dev->driver == NULL) ||
1233 (dev->driver_data == &usb_generic_driver_data))
1236 intf = to_usb_interface(dev);
1237 driver = to_usb_driver(dev->driver);
1239 if (driver->suspend)
1240 return driver->suspend(intf, state);
1244 static int usb_generic_resume(struct device *dev)
1246 struct usb_interface *intf;
1247 struct usb_driver *driver;
1249 /* devices resume through their hub */
1250 if (dev->driver == &usb_generic_driver)
1251 return usb_resume_device (to_usb_device(dev));
1253 if ((dev->driver == NULL) ||
1254 (dev->driver_data == &usb_generic_driver_data))
1257 intf = to_usb_interface(dev);
1258 driver = to_usb_driver(dev->driver);
1261 return driver->resume(intf);
1265 struct bus_type usb_bus_type = {
1267 .match = usb_device_match,
1268 .hotplug = usb_hotplug,
1269 .suspend = usb_generic_suspend,
1270 .resume = usb_generic_resume,
1275 static int __init usb_setup_disable(char *str)
1281 /* format to disable USB on kernel command line is: nousb */
1282 __setup("nousb", usb_setup_disable);
1287 * for external read access to <nousb>
1289 int usb_disabled(void)
1297 static int __init usb_init(void)
1301 pr_info ("%s: USB support disabled\n", usbcore_name);
1305 retval = bus_register(&usb_bus_type);
1308 retval = usb_host_init();
1310 goto host_init_failed;
1311 retval = usb_major_init();
1313 goto major_init_failed;
1314 retval = usbfs_init();
1316 goto fs_init_failed;
1317 retval = usb_hub_init();
1319 goto hub_init_failed;
1321 retval = driver_register(&usb_generic_driver);
1329 usb_major_cleanup();
1333 bus_unregister(&usb_bus_type);
1341 static void __exit usb_exit(void)
1343 /* This will matter if shutdown/reboot does exitcalls. */
1347 driver_unregister(&usb_generic_driver);
1348 usb_major_cleanup();
1352 bus_unregister(&usb_bus_type);
1355 subsys_initcall(usb_init);
1356 module_exit(usb_exit);
1359 * USB may be built into the kernel or be built as modules.
1360 * These symbols are exported for device (or host controller)
1361 * driver modules to use.
1363 EXPORT_SYMBOL(usb_epnum_to_ep_desc);
1365 EXPORT_SYMBOL(usb_register);
1366 EXPORT_SYMBOL(usb_deregister);
1367 EXPORT_SYMBOL(usb_disabled);
1369 EXPORT_SYMBOL(usb_alloc_dev);
1370 EXPORT_SYMBOL(usb_put_dev);
1371 EXPORT_SYMBOL(usb_get_dev);
1372 EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
1374 EXPORT_SYMBOL(usb_driver_claim_interface);
1375 EXPORT_SYMBOL(usb_driver_release_interface);
1376 EXPORT_SYMBOL(usb_match_id);
1377 EXPORT_SYMBOL(usb_find_interface);
1378 EXPORT_SYMBOL(usb_ifnum_to_if);
1379 EXPORT_SYMBOL(usb_altnum_to_altsetting);
1381 EXPORT_SYMBOL(usb_reset_device);
1382 EXPORT_SYMBOL(usb_disconnect);
1384 EXPORT_SYMBOL(__usb_get_extra_descriptor);
1386 EXPORT_SYMBOL(usb_find_device);
1387 EXPORT_SYMBOL(usb_get_current_frame_number);
1389 EXPORT_SYMBOL (usb_buffer_alloc);
1390 EXPORT_SYMBOL (usb_buffer_free);
1392 EXPORT_SYMBOL (usb_buffer_map);
1394 EXPORT_SYMBOL (usb_buffer_dmasync);
1396 EXPORT_SYMBOL (usb_buffer_unmap);
1398 EXPORT_SYMBOL (usb_buffer_map_sg);
1400 EXPORT_SYMBOL (usb_buffer_dmasync_sg);
1402 EXPORT_SYMBOL (usb_buffer_unmap_sg);
1404 MODULE_LICENSE("GPL");