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__);
96 if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
99 id = usb_match_id (intf, driver->id_table);
101 dev_dbg (dev, "%s - got id\n", __FUNCTION__);
102 error = driver->probe (intf, id);
108 /* called from driver core with usb_bus_type.subsys writelock */
109 int usb_unbind_interface(struct device *dev)
111 struct usb_interface *intf = to_usb_interface(dev);
112 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
114 /* release all urbs for this interface */
115 usb_disable_interface(interface_to_usbdev(intf), intf);
117 if (driver && driver->disconnect)
118 driver->disconnect(intf);
120 /* reset other interface state */
121 usb_set_interface(interface_to_usbdev(intf),
122 intf->altsetting[0].desc.bInterfaceNumber,
124 usb_set_intfdata(intf, NULL);
130 * usb_register - register a USB driver
131 * @new_driver: USB operations for the driver
133 * Registers a USB driver with the USB core. The list of unattached
134 * interfaces will be rescanned whenever a new driver is added, allowing
135 * the new driver to attach to any recognized devices.
136 * Returns a negative error code on failure and 0 on success.
138 * NOTE: if you want your driver to use the USB major number, you must call
139 * usb_register_dev() to enable that functionality. This function no longer
140 * takes care of that.
142 int usb_register(struct usb_driver *new_driver)
149 new_driver->driver.name = (char *)new_driver->name;
150 new_driver->driver.bus = &usb_bus_type;
151 new_driver->driver.probe = usb_probe_interface;
152 new_driver->driver.remove = usb_unbind_interface;
154 retval = driver_register(&new_driver->driver);
157 pr_info("%s: registered new driver %s\n",
158 usbcore_name, new_driver->name);
159 usbfs_update_special();
161 printk(KERN_ERR "%s: error %d registering driver %s\n",
162 usbcore_name, retval, new_driver->name);
169 * usb_deregister - unregister a USB driver
170 * @driver: USB operations of the driver to unregister
171 * Context: must be able to sleep
173 * Unlinks the specified driver from the internal USB driver list.
175 * NOTE: If you called usb_register_dev(), you still need to call
176 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
177 * this * call will no longer do it for you.
179 void usb_deregister(struct usb_driver *driver)
181 pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
183 driver_unregister (&driver->driver);
185 usbfs_update_special();
189 * usb_ifnum_to_if - get the interface object with a given interface number
190 * @dev: the device whose current configuration is considered
191 * @ifnum: the desired interface
193 * This walks the device descriptor for the currently active configuration
194 * and returns a pointer to the interface with that particular interface
197 * Note that configuration descriptors are not required to assign interface
198 * numbers sequentially, so that it would be incorrect to assume that
199 * the first interface in that descriptor corresponds to interface zero.
200 * This routine helps device drivers avoid such mistakes.
201 * However, you should make sure that you do the right thing with any
202 * alternate settings available for this interfaces.
204 * Don't call this function unless you are bound to one of the interfaces
205 * on this device or you own the dev->serialize semaphore!
207 struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
209 struct usb_host_config *config = dev->actconfig;
214 for (i = 0; i < config->desc.bNumInterfaces; i++)
215 if (config->interface[i]->altsetting[0]
216 .desc.bInterfaceNumber == ifnum)
217 return config->interface[i];
223 * usb_altnum_to_altsetting - get the altsetting structure with a given
224 * alternate setting number.
225 * @intf: the interface containing the altsetting in question
226 * @altnum: the desired alternate setting number
228 * This searches the altsetting array of the specified interface for
229 * an entry with the correct bAlternateSetting value and returns a pointer
230 * to that entry, or null.
232 * Note that altsettings need not be stored sequentially by number, so
233 * it would be incorrect to assume that the first altsetting entry in
234 * the array corresponds to altsetting zero. This routine helps device
235 * drivers avoid such mistakes.
237 * Don't call this function unless you are bound to the intf interface
238 * or you own the device's ->serialize semaphore!
240 struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
245 for (i = 0; i < intf->num_altsetting; i++) {
246 if (intf->altsetting[i].desc.bAlternateSetting == altnum)
247 return &intf->altsetting[i];
253 * usb_epnum_to_ep_desc - get the endpoint object with a given endpoint number
254 * @dev: the device whose current configuration+altsettings is considered
255 * @epnum: the desired endpoint, masked with USB_DIR_IN as appropriate.
257 * This walks the device descriptor for the currently active configuration,
258 * and returns a pointer to the endpoint with that particular endpoint
261 * Note that interface descriptors are not required to list endpoint
262 * numbers in any standardized order, so that it would be wrong to
263 * assume that ep2in precedes either ep5in, ep2out, or even ep1out.
264 * This routine helps device drivers avoid such mistakes.
266 struct usb_endpoint_descriptor *
267 usb_epnum_to_ep_desc(struct usb_device *dev, unsigned epnum)
269 struct usb_host_config *config = dev->actconfig;
274 for (i = 0; i < config->desc.bNumInterfaces; i++) {
275 struct usb_interface *intf;
276 struct usb_host_interface *alt;
278 /* only endpoints in current altsetting are active */
279 intf = config->interface[i];
280 alt = intf->cur_altsetting;
282 for (k = 0; k < alt->desc.bNumEndpoints; k++)
283 if (epnum == alt->endpoint[k].desc.bEndpointAddress)
284 return &alt->endpoint[k].desc;
291 * usb_driver_claim_interface - bind a driver to an interface
292 * @driver: the driver to be bound
293 * @iface: the interface to which it will be bound; must be in the
294 * usb device's active configuration
295 * @priv: driver data associated with that interface
297 * This is used by usb device drivers that need to claim more than one
298 * interface on a device when probing (audio and acm are current examples).
299 * No device driver should directly modify internal usb_interface or
300 * usb_device structure members.
302 * Few drivers should need to use this routine, since the most natural
303 * way to bind to an interface is to return the private data from
304 * the driver's probe() method.
306 * Callers must own the driver model's usb bus writelock. So driver
307 * probe() entries don't need extra locking, but other call contexts
308 * may need to explicitly claim that lock.
310 int usb_driver_claim_interface(struct usb_driver *driver, struct usb_interface *iface, void* priv)
312 struct device *dev = &iface->dev;
317 dev->driver = &driver->driver;
318 usb_set_intfdata(iface, priv);
320 /* if interface was already added, bind now; else let
321 * the future device_add() bind it, bypassing probe()
323 if (!list_empty (&dev->bus_list))
324 device_bind_driver(dev);
330 * usb_driver_release_interface - unbind a driver from an interface
331 * @driver: the driver to be unbound
332 * @iface: the interface from which it will be unbound
334 * This can be used by drivers to release an interface without waiting
335 * for their disconnect() methods to be called. In typical cases this
336 * also causes the driver disconnect() method to be called.
338 * This call is synchronous, and may not be used in an interrupt context.
339 * Callers must own the usb_device serialize semaphore and the driver model's
340 * usb bus writelock. So driver disconnect() entries don't need extra locking,
341 * but other call contexts may need to explicitly claim those locks.
343 void usb_driver_release_interface(struct usb_driver *driver,
344 struct usb_interface *iface)
346 struct device *dev = &iface->dev;
348 /* this should never happen, don't release something that's not ours */
349 if (!dev->driver || dev->driver != &driver->driver)
352 /* don't disconnect from disconnect(), or before dev_add() */
353 if (!list_empty (&dev->driver_list) && !list_empty (&dev->bus_list))
354 device_release_driver(dev);
357 usb_set_intfdata(iface, NULL);
361 * usb_match_id - find first usb_device_id matching device or interface
362 * @interface: the interface of interest
363 * @id: array of usb_device_id structures, terminated by zero entry
365 * usb_match_id searches an array of usb_device_id's and returns
366 * the first one matching the device or interface, or null.
367 * This is used when binding (or rebinding) a driver to an interface.
368 * Most USB device drivers will use this indirectly, through the usb core,
369 * but some layered driver frameworks use it directly.
370 * These device tables are exported with MODULE_DEVICE_TABLE, through
371 * modutils and "modules.usbmap", to support the driver loading
372 * functionality of USB hotplugging.
376 * The "match_flags" element in a usb_device_id controls which
377 * members are used. If the corresponding bit is set, the
378 * value in the device_id must match its corresponding member
379 * in the device or interface descriptor, or else the device_id
382 * "driver_info" is normally used only by device drivers,
383 * but you can create a wildcard "matches anything" usb_device_id
384 * as a driver's "modules.usbmap" entry if you provide an id with
385 * only a nonzero "driver_info" field. If you do this, the USB device
386 * driver's probe() routine should use additional intelligence to
387 * decide whether to bind to the specified interface.
389 * What Makes Good usb_device_id Tables:
391 * The match algorithm is very simple, so that intelligence in
392 * driver selection must come from smart driver id records.
393 * Unless you have good reasons to use another selection policy,
394 * provide match elements only in related groups, and order match
395 * specifiers from specific to general. Use the macros provided
396 * for that purpose if you can.
398 * The most specific match specifiers use device descriptor
399 * data. These are commonly used with product-specific matches;
400 * the USB_DEVICE macro lets you provide vendor and product IDs,
401 * and you can also match against ranges of product revisions.
402 * These are widely used for devices with application or vendor
403 * specific bDeviceClass values.
405 * Matches based on device class/subclass/protocol specifications
406 * are slightly more general; use the USB_DEVICE_INFO macro, or
407 * its siblings. These are used with single-function devices
408 * where bDeviceClass doesn't specify that each interface has
411 * Matches based on interface class/subclass/protocol are the
412 * most general; they let drivers bind to any interface on a
413 * multiple-function device. Use the USB_INTERFACE_INFO
414 * macro, or its siblings, to match class-per-interface style
415 * devices (as recorded in bDeviceClass).
417 * Within those groups, remember that not all combinations are
418 * meaningful. For example, don't give a product version range
419 * without vendor and product IDs; or specify a protocol without
420 * its associated class and subclass.
422 const struct usb_device_id *
423 usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
425 struct usb_host_interface *intf;
426 struct usb_device *dev;
428 /* proc_connectinfo in devio.c may call us with id == NULL. */
432 intf = interface->cur_altsetting;
433 dev = interface_to_usbdev(interface);
435 /* It is important to check that id->driver_info is nonzero,
436 since an entry that is all zeroes except for a nonzero
437 id->driver_info is the way to create an entry that
438 indicates that the driver want to examine every
439 device and interface. */
440 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
441 id->driver_info; id++) {
443 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
444 id->idVendor != dev->descriptor.idVendor)
447 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
448 id->idProduct != dev->descriptor.idProduct)
451 /* No need to test id->bcdDevice_lo != 0, since 0 is never
452 greater than any unsigned number. */
453 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
454 (id->bcdDevice_lo > dev->descriptor.bcdDevice))
457 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
458 (id->bcdDevice_hi < dev->descriptor.bcdDevice))
461 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
462 (id->bDeviceClass != dev->descriptor.bDeviceClass))
465 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
466 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
469 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
470 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
473 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
474 (id->bInterfaceClass != intf->desc.bInterfaceClass))
477 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
478 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
481 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
482 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
492 * usb_find_interface - find usb_interface pointer for driver and device
493 * @drv: the driver whose current configuration is considered
494 * @minor: the minor number of the desired device
496 * This walks the driver device list and returns a pointer to the interface
497 * with the matching minor. Note, this only works for devices that share the
500 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
502 struct list_head *entry;
504 struct usb_interface *intf;
506 list_for_each(entry, &drv->driver.devices) {
507 dev = container_of(entry, struct device, driver_list);
509 /* can't look at usb devices, only interfaces */
510 if (dev->driver == &usb_generic_driver)
513 intf = to_usb_interface(dev);
514 if (intf->minor == -1)
516 if (intf->minor == minor)
520 /* no device found that matches */
524 static int usb_device_match (struct device *dev, struct device_driver *drv)
526 struct usb_interface *intf;
527 struct usb_driver *usb_drv;
528 const struct usb_device_id *id;
530 /* check for generic driver, which we don't match any device with */
531 if (drv == &usb_generic_driver)
534 intf = to_usb_interface(dev);
536 usb_drv = to_usb_driver(drv);
537 id = usb_drv->id_table;
539 id = usb_match_id (intf, usb_drv->id_table);
547 #ifdef CONFIG_HOTPLUG
550 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
551 * (normally /sbin/hotplug) when USB devices get added or removed.
553 * This invokes a user mode policy agent, typically helping to load driver
554 * or other modules, configure the device, and more. Drivers can provide
555 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
557 * We're called either from khubd (the typical case) or from root hub
558 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
559 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
560 * device (and this configuration!) are still present.
562 static int usb_hotplug (struct device *dev, char **envp, int num_envp,
563 char *buffer, int buffer_size)
565 struct usb_interface *intf;
566 struct usb_device *usb_dev;
574 /* driver is often null here; dev_dbg() would oops */
575 pr_debug ("usb %s: hotplug\n", dev->bus_id);
577 /* Must check driver_data here, as on remove driver is always NULL */
578 if ((dev->driver == &usb_generic_driver) ||
579 (dev->driver_data == &usb_generic_driver_data))
582 intf = to_usb_interface(dev);
583 usb_dev = interface_to_usbdev (intf);
585 if (usb_dev->devnum < 0) {
586 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
590 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
596 #ifdef CONFIG_USB_DEVICEFS
597 /* If this is available, userspace programs can directly read
598 * all the device descriptors we don't tell them about. Or
599 * even act as usermode drivers.
601 * FIXME reduce hardwired intelligence here
603 envp [i++] = scratch;
604 length += snprintf (scratch, buffer_size - length,
605 "DEVICE=/proc/bus/usb/%03d/%03d",
606 usb_dev->bus->busnum, usb_dev->devnum);
607 if ((buffer_size - length <= 0) || (i >= num_envp))
613 /* per-device configurations are common */
614 envp [i++] = scratch;
615 length += snprintf (scratch, buffer_size - length, "PRODUCT=%x/%x/%x",
616 usb_dev->descriptor.idVendor,
617 usb_dev->descriptor.idProduct,
618 usb_dev->descriptor.bcdDevice);
619 if ((buffer_size - length <= 0) || (i >= num_envp))
624 /* class-based driver binding models */
625 envp [i++] = scratch;
626 length += snprintf (scratch, buffer_size - length, "TYPE=%d/%d/%d",
627 usb_dev->descriptor.bDeviceClass,
628 usb_dev->descriptor.bDeviceSubClass,
629 usb_dev->descriptor.bDeviceProtocol);
630 if ((buffer_size - length <= 0) || (i >= num_envp))
635 if (usb_dev->descriptor.bDeviceClass == 0) {
636 struct usb_host_interface *alt = intf->cur_altsetting;
638 /* 2.4 only exposed interface zero. in 2.5, hotplug
639 * agents are called for all interfaces, and can use
640 * $DEVPATH/bInterfaceNumber if necessary.
642 envp [i++] = scratch;
643 length += snprintf (scratch, buffer_size - length,
644 "INTERFACE=%d/%d/%d",
645 alt->desc.bInterfaceClass,
646 alt->desc.bInterfaceSubClass,
647 alt->desc.bInterfaceProtocol);
648 if ((buffer_size - length <= 0) || (i >= num_envp))
661 static int usb_hotplug (struct device *dev, char **envp,
662 int num_envp, char *buffer, int buffer_size)
667 #endif /* CONFIG_HOTPLUG */
670 * usb_release_dev - free a usb device structure when all users of it are finished.
671 * @dev: device that's been disconnected
673 * Will be called only by the device core when all users of this usb device are
676 static void usb_release_dev(struct device *dev)
678 struct usb_device *udev;
680 udev = to_usb_device(dev);
682 if (udev->bus && udev->bus->op && udev->bus->op->deallocate)
683 udev->bus->op->deallocate(udev);
684 usb_destroy_configuration(udev);
685 usb_bus_put(udev->bus);
690 * usb_alloc_dev - usb device constructor (usbcore-internal)
691 * @parent: hub to which device is connected; null to allocate a root hub
692 * @bus: bus used to access the device
693 * @port: zero based index of port; ignored for root hubs
694 * Context: !in_interrupt ()
696 * Only hub drivers (including virtual root hub drivers for host
697 * controllers) should ever call this.
699 * This call may not be used in a non-sleeping context.
702 usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port)
704 struct usb_device *dev;
706 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
710 memset(dev, 0, sizeof(*dev));
712 bus = usb_bus_get(bus);
718 device_initialize(&dev->dev);
719 dev->dev.bus = &usb_bus_type;
720 dev->dev.dma_mask = bus->controller->dma_mask;
721 dev->dev.driver_data = &usb_generic_driver_data;
722 dev->dev.driver = &usb_generic_driver;
723 dev->dev.release = usb_release_dev;
724 dev->state = USB_STATE_ATTACHED;
726 /* Save readable and stable topology id, distinguishing devices
727 * by location for diagnostics, tools, driver model, etc. The
728 * string is a path along hub ports, from the root. Each device's
729 * dev->devpath will be stable until USB is re-cabled, and hubs
730 * are often labeled with these port numbers. The bus_id isn't
731 * as stable: bus->busnum changes easily from modprobe order,
732 * cardbus or pci hotplugging, and so on.
734 if (unlikely (!parent)) {
735 dev->devpath [0] = '0';
737 dev->dev.parent = bus->controller;
738 sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
740 /* match any labeling on the hubs; it's one-based */
741 if (parent->devpath [0] == '0')
742 snprintf (dev->devpath, sizeof dev->devpath,
745 snprintf (dev->devpath, sizeof dev->devpath,
746 "%s.%d", parent->devpath, port + 1);
748 dev->dev.parent = &parent->dev;
749 sprintf (&dev->dev.bus_id[0], "%d-%s",
750 bus->busnum, dev->devpath);
752 /* hub driver sets up TT records */
756 dev->parent = parent;
757 INIT_LIST_HEAD(&dev->filelist);
759 init_MUTEX(&dev->serialize);
761 if (dev->bus->op->allocate)
762 dev->bus->op->allocate(dev);
768 * usb_get_dev - increments the reference count of the usb device structure
769 * @dev: the device being referenced
771 * Each live reference to a device should be refcounted.
773 * Drivers for USB interfaces should normally record such references in
774 * their probe() methods, when they bind to an interface, and release
775 * them by calling usb_put_dev(), in their disconnect() methods.
777 * A pointer to the device with the incremented reference counter is returned.
779 struct usb_device *usb_get_dev(struct usb_device *dev)
782 get_device(&dev->dev);
787 * usb_put_dev - release a use of the usb device structure
788 * @dev: device that's been disconnected
790 * Must be called when a user of a device is finished with it. When the last
791 * user of the device calls this function, the memory of the device is freed.
793 void usb_put_dev(struct usb_device *dev)
796 put_device(&dev->dev);
800 * usb_get_intf - increments the reference count of the usb interface structure
801 * @intf: the interface being referenced
803 * Each live reference to a interface must be refcounted.
805 * Drivers for USB interfaces should normally record such references in
806 * their probe() methods, when they bind to an interface, and release
807 * them by calling usb_put_intf(), in their disconnect() methods.
809 * A pointer to the interface with the incremented reference counter is
812 struct usb_interface *usb_get_intf(struct usb_interface *intf)
815 get_device(&intf->dev);
820 * usb_put_intf - release a use of the usb interface structure
821 * @intf: interface that's been decremented
823 * Must be called when a user of an interface is finished with it. When the
824 * last user of the interface calls this function, the memory of the interface
827 void usb_put_intf(struct usb_interface *intf)
830 put_device(&intf->dev);
833 static struct usb_device *match_device(struct usb_device *dev,
834 u16 vendor_id, u16 product_id)
836 struct usb_device *ret_dev = NULL;
839 dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
840 dev->descriptor.idVendor,
841 dev->descriptor.idProduct);
843 /* see if this device matches */
844 if ((dev->descriptor.idVendor == vendor_id) &&
845 (dev->descriptor.idProduct == product_id)) {
846 dev_dbg (&dev->dev, "matched this device!\n");
847 ret_dev = usb_get_dev(dev);
851 /* look through all of the children of this device */
852 for (child = 0; child < dev->maxchild; ++child) {
853 if (dev->children[child]) {
854 ret_dev = match_device(dev->children[child],
855 vendor_id, product_id);
865 * usb_find_device - find a specific usb device in the system
866 * @vendor_id: the vendor id of the device to find
867 * @product_id: the product id of the device to find
869 * Returns a pointer to a struct usb_device if such a specified usb
870 * device is present in the system currently. The usage count of the
871 * device will be incremented if a device is found. Make sure to call
872 * usb_put_dev() when the caller is finished with the device.
874 * If a device with the specified vendor and product id is not found,
877 struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
879 struct list_head *buslist;
881 struct usb_device *dev = NULL;
883 down(&usb_bus_list_lock);
884 for (buslist = usb_bus_list.next;
885 buslist != &usb_bus_list;
886 buslist = buslist->next) {
887 bus = container_of(buslist, struct usb_bus, bus_list);
890 dev = match_device(bus->root_hub, vendor_id, product_id);
895 up(&usb_bus_list_lock);
900 * usb_get_current_frame_number - return current bus frame number
901 * @dev: the device whose bus is being queried
903 * Returns the current frame number for the USB host controller
904 * used with the given USB device. This can be used when scheduling
905 * isochronous requests.
907 * Note that different kinds of host controller have different
908 * "scheduling horizons". While one type might support scheduling only
909 * 32 frames into the future, others could support scheduling up to
910 * 1024 frames into the future.
912 int usb_get_current_frame_number(struct usb_device *dev)
914 return dev->bus->op->get_frame_number (dev);
917 /*-------------------------------------------------------------------*/
919 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
920 * extra field of the interface and endpoint descriptor structs.
923 int __usb_get_extra_descriptor(char *buffer, unsigned size,
924 unsigned char type, void **ptr)
926 struct usb_descriptor_header *header;
928 while (size >= sizeof(struct usb_descriptor_header)) {
929 header = (struct usb_descriptor_header *)buffer;
931 if (header->bLength < 2) {
933 "%s: bogus descriptor, type %d length %d\n",
935 header->bDescriptorType,
940 if (header->bDescriptorType == type) {
945 buffer += header->bLength;
946 size -= header->bLength;
952 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
953 * @dev: device the buffer will be used with
954 * @size: requested buffer size
955 * @mem_flags: affect whether allocation may block
956 * @dma: used to return DMA address of buffer
958 * Return value is either null (indicating no buffer could be allocated), or
959 * the cpu-space pointer to a buffer that may be used to perform DMA to the
960 * specified device. Such cpu-space buffers are returned along with the DMA
961 * address (through the pointer provided).
963 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
964 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
965 * mapping hardware for long idle periods. The implementation varies between
966 * platforms, depending on details of how DMA will work to this device.
967 * Using these buffers also helps prevent cacheline sharing problems on
968 * architectures where CPU caches are not DMA-coherent.
970 * When the buffer is no longer used, free it with usb_buffer_free().
972 void *usb_buffer_alloc (
973 struct usb_device *dev,
979 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
981 return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
985 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
986 * @dev: device the buffer was used with
987 * @size: requested buffer size
988 * @addr: CPU address of buffer
989 * @dma: DMA address of buffer
991 * This reclaims an I/O buffer, letting it be reused. The memory must have
992 * been allocated using usb_buffer_alloc(), and the parameters must match
993 * those provided in that allocation request.
995 void usb_buffer_free (
996 struct usb_device *dev,
1002 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
1004 dev->bus->op->buffer_free (dev->bus, size, addr, dma);
1008 * usb_buffer_map - create DMA mapping(s) for an urb
1009 * @urb: urb whose transfer_buffer/setup_packet will be mapped
1011 * Return value is either null (indicating no buffer could be mapped), or
1012 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
1013 * added to urb->transfer_flags if the operation succeeds. If the device
1014 * is connected to this system through a non-DMA controller, this operation
1017 * This call would normally be used for an urb which is reused, perhaps
1018 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1019 * calls to synchronize memory and dma state.
1021 * Reverse the effect of this call with usb_buffer_unmap().
1023 struct urb *usb_buffer_map (struct urb *urb)
1025 struct usb_bus *bus;
1026 struct device *controller;
1030 || !(bus = urb->dev->bus)
1031 || !(controller = bus->controller))
1034 if (controller->dma_mask) {
1035 urb->transfer_dma = dma_map_single (controller,
1036 urb->transfer_buffer, urb->transfer_buffer_length,
1037 usb_pipein (urb->pipe)
1038 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1039 if (usb_pipecontrol (urb->pipe))
1040 urb->setup_dma = dma_map_single (controller,
1042 sizeof (struct usb_ctrlrequest),
1044 // FIXME generic api broken like pci, can't report errors
1045 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1047 urb->transfer_dma = ~0;
1048 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1049 | URB_NO_SETUP_DMA_MAP);
1053 /* XXX DISABLED, no users currently. If you wish to re-enable this
1054 * XXX please determine whether the sync is to transfer ownership of
1055 * XXX the buffer from device to cpu or vice verse, and thusly use the
1056 * XXX appropriate _for_{cpu,device}() method. -DaveM
1061 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1062 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1064 void usb_buffer_dmasync (struct urb *urb)
1066 struct usb_bus *bus;
1067 struct device *controller;
1070 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1072 || !(bus = urb->dev->bus)
1073 || !(controller = bus->controller))
1076 if (controller->dma_mask) {
1077 dma_sync_single (controller,
1078 urb->transfer_dma, urb->transfer_buffer_length,
1079 usb_pipein (urb->pipe)
1080 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1081 if (usb_pipecontrol (urb->pipe))
1082 dma_sync_single (controller,
1084 sizeof (struct usb_ctrlrequest),
1091 * usb_buffer_unmap - free DMA mapping(s) for an urb
1092 * @urb: urb whose transfer_buffer will be unmapped
1094 * Reverses the effect of usb_buffer_map().
1096 void usb_buffer_unmap (struct urb *urb)
1098 struct usb_bus *bus;
1099 struct device *controller;
1102 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1104 || !(bus = urb->dev->bus)
1105 || !(controller = bus->controller))
1108 if (controller->dma_mask) {
1109 dma_unmap_single (controller,
1110 urb->transfer_dma, urb->transfer_buffer_length,
1111 usb_pipein (urb->pipe)
1112 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1113 if (usb_pipecontrol (urb->pipe))
1114 dma_unmap_single (controller,
1116 sizeof (struct usb_ctrlrequest),
1119 urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
1120 | URB_NO_SETUP_DMA_MAP);
1124 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1125 * @dev: device to which the scatterlist will be mapped
1126 * @pipe: endpoint defining the mapping direction
1127 * @sg: the scatterlist to map
1128 * @nents: the number of entries in the scatterlist
1130 * Return value is either < 0 (indicating no buffers could be mapped), or
1131 * the number of DMA mapping array entries in the scatterlist.
1133 * The caller is responsible for placing the resulting DMA addresses from
1134 * the scatterlist into URB transfer buffer pointers, and for setting the
1135 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1137 * Top I/O rates come from queuing URBs, instead of waiting for each one
1138 * to complete before starting the next I/O. This is particularly easy
1139 * to do with scatterlists. Just allocate and submit one URB for each DMA
1140 * mapping entry returned, stopping on the first error or when all succeed.
1141 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1143 * This call would normally be used when translating scatterlist requests,
1144 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1145 * may be able to coalesce mappings for improved I/O efficiency.
1147 * Reverse the effect of this call with usb_buffer_unmap_sg().
1149 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
1150 struct scatterlist *sg, int nents)
1152 struct usb_bus *bus;
1153 struct device *controller;
1156 || usb_pipecontrol (pipe)
1157 || !(bus = dev->bus)
1158 || !(controller = bus->controller)
1159 || !controller->dma_mask)
1162 // FIXME generic api broken like pci, can't report errors
1163 return dma_map_sg (controller, sg, nents,
1164 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1167 /* XXX DISABLED, no users currently. If you wish to re-enable this
1168 * XXX please determine whether the sync is to transfer ownership of
1169 * XXX the buffer from device to cpu or vice verse, and thusly use the
1170 * XXX appropriate _for_{cpu,device}() method. -DaveM
1175 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1176 * @dev: device to which the scatterlist will be mapped
1177 * @pipe: endpoint defining the mapping direction
1178 * @sg: the scatterlist to synchronize
1179 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1181 * Use this when you are re-using a scatterlist's data buffers for
1182 * another USB request.
1184 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
1185 struct scatterlist *sg, int n_hw_ents)
1187 struct usb_bus *bus;
1188 struct device *controller;
1191 || !(bus = dev->bus)
1192 || !(controller = bus->controller)
1193 || !controller->dma_mask)
1196 dma_sync_sg (controller, sg, n_hw_ents,
1197 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1202 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1203 * @dev: device to which the scatterlist will be mapped
1204 * @pipe: endpoint defining the mapping direction
1205 * @sg: the scatterlist to unmap
1206 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1208 * Reverses the effect of usb_buffer_map_sg().
1210 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1211 struct scatterlist *sg, int n_hw_ents)
1213 struct usb_bus *bus;
1214 struct device *controller;
1217 || !(bus = dev->bus)
1218 || !(controller = bus->controller)
1219 || !controller->dma_mask)
1222 dma_unmap_sg (controller, sg, n_hw_ents,
1223 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1226 static int usb_generic_suspend(struct device *dev, u32 state)
1228 struct usb_interface *intf;
1229 struct usb_driver *driver;
1231 if (dev->driver == &usb_generic_driver)
1232 return usb_suspend_device (to_usb_device(dev), state);
1234 if ((dev->driver == NULL) ||
1235 (dev->driver_data == &usb_generic_driver_data))
1238 intf = to_usb_interface(dev);
1239 driver = to_usb_driver(dev->driver);
1241 if (driver->suspend)
1242 return driver->suspend(intf, state);
1246 static int usb_generic_resume(struct device *dev)
1248 struct usb_interface *intf;
1249 struct usb_driver *driver;
1251 /* devices resume through their hub */
1252 if (dev->driver == &usb_generic_driver)
1253 return usb_resume_device (to_usb_device(dev));
1255 if ((dev->driver == NULL) ||
1256 (dev->driver_data == &usb_generic_driver_data))
1259 intf = to_usb_interface(dev);
1260 driver = to_usb_driver(dev->driver);
1263 return driver->resume(intf);
1267 struct bus_type usb_bus_type = {
1269 .match = usb_device_match,
1270 .hotplug = usb_hotplug,
1271 .suspend = usb_generic_suspend,
1272 .resume = usb_generic_resume,
1277 static int __init usb_setup_disable(char *str)
1283 /* format to disable USB on kernel command line is: nousb */
1284 __setup("nousb", usb_setup_disable);
1289 * for external read access to <nousb>
1291 int usb_disabled(void)
1299 static int __init usb_init(void)
1303 pr_info ("%s: USB support disabled\n", usbcore_name);
1307 retval = bus_register(&usb_bus_type);
1310 retval = usb_host_init();
1312 goto host_init_failed;
1313 retval = usb_major_init();
1315 goto major_init_failed;
1316 retval = usbfs_init();
1318 goto fs_init_failed;
1319 retval = usb_hub_init();
1321 goto hub_init_failed;
1323 retval = driver_register(&usb_generic_driver);
1331 usb_major_cleanup();
1335 bus_unregister(&usb_bus_type);
1343 static void __exit usb_exit(void)
1345 /* This will matter if shutdown/reboot does exitcalls. */
1349 driver_unregister(&usb_generic_driver);
1350 usb_major_cleanup();
1354 bus_unregister(&usb_bus_type);
1357 subsys_initcall(usb_init);
1358 module_exit(usb_exit);
1361 * USB may be built into the kernel or be built as modules.
1362 * These symbols are exported for device (or host controller)
1363 * driver modules to use.
1365 EXPORT_SYMBOL(usb_epnum_to_ep_desc);
1367 EXPORT_SYMBOL(usb_register);
1368 EXPORT_SYMBOL(usb_deregister);
1369 EXPORT_SYMBOL(usb_disabled);
1371 EXPORT_SYMBOL(usb_alloc_dev);
1372 EXPORT_SYMBOL(usb_put_dev);
1373 EXPORT_SYMBOL(usb_get_dev);
1374 EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
1376 EXPORT_SYMBOL(usb_driver_claim_interface);
1377 EXPORT_SYMBOL(usb_driver_release_interface);
1378 EXPORT_SYMBOL(usb_match_id);
1379 EXPORT_SYMBOL(usb_find_interface);
1380 EXPORT_SYMBOL(usb_ifnum_to_if);
1381 EXPORT_SYMBOL(usb_altnum_to_altsetting);
1383 EXPORT_SYMBOL(usb_reset_device);
1384 EXPORT_SYMBOL(usb_disconnect);
1386 EXPORT_SYMBOL(__usb_get_extra_descriptor);
1388 EXPORT_SYMBOL(usb_find_device);
1389 EXPORT_SYMBOL(usb_get_current_frame_number);
1391 EXPORT_SYMBOL (usb_buffer_alloc);
1392 EXPORT_SYMBOL (usb_buffer_free);
1394 EXPORT_SYMBOL (usb_buffer_map);
1396 EXPORT_SYMBOL (usb_buffer_dmasync);
1398 EXPORT_SYMBOL (usb_buffer_unmap);
1400 EXPORT_SYMBOL (usb_buffer_map_sg);
1402 EXPORT_SYMBOL (usb_buffer_dmasync_sg);
1404 EXPORT_SYMBOL (usb_buffer_unmap_sg);
1406 MODULE_LICENSE("GPL");