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);
886 dev = match_device(bus->root_hub, vendor_id, product_id);
891 up(&usb_bus_list_lock);
896 * usb_get_current_frame_number - return current bus frame number
897 * @dev: the device whose bus is being queried
899 * Returns the current frame number for the USB host controller
900 * used with the given USB device. This can be used when scheduling
901 * isochronous requests.
903 * Note that different kinds of host controller have different
904 * "scheduling horizons". While one type might support scheduling only
905 * 32 frames into the future, others could support scheduling up to
906 * 1024 frames into the future.
908 int usb_get_current_frame_number(struct usb_device *dev)
910 return dev->bus->op->get_frame_number (dev);
913 /*-------------------------------------------------------------------*/
915 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
916 * extra field of the interface and endpoint descriptor structs.
919 int __usb_get_extra_descriptor(char *buffer, unsigned size,
920 unsigned char type, void **ptr)
922 struct usb_descriptor_header *header;
924 while (size >= sizeof(struct usb_descriptor_header)) {
925 header = (struct usb_descriptor_header *)buffer;
927 if (header->bLength < 2) {
929 "%s: bogus descriptor, type %d length %d\n",
931 header->bDescriptorType,
936 if (header->bDescriptorType == type) {
941 buffer += header->bLength;
942 size -= header->bLength;
948 * usb_disconnect - disconnect a device (usbcore-internal)
949 * @pdev: pointer to device being disconnected
950 * Context: !in_interrupt ()
952 * Something got disconnected. Get rid of it, and all of its children.
954 * Only hub drivers (including virtual root hub drivers for host
955 * controllers) should ever call this.
957 * This call is synchronous, and may not be used in an interrupt context.
959 void usb_disconnect(struct usb_device **pdev)
961 struct usb_device *dev = *pdev;
963 struct usb_operations *ops;
969 pr_debug ("%s nodev\n", __FUNCTION__);
974 pr_debug ("%s nobus\n", __FUNCTION__);
981 /* mark the device as inactive, so any further urb submissions for
982 * this device will fail.
984 dev->state = USB_STATE_NOTATTACHED;
985 down(&dev->serialize);
987 dev_info (&dev->dev, "USB disconnect, address %d\n", dev->devnum);
989 /* Free up all the children before we remove this device */
990 for (i = 0; i < USB_MAXCHILDREN; i++) {
991 struct usb_device **child = dev->children + i;
993 usb_disconnect(child);
996 /* deallocate hcd/hardware state ... nuking all pending urbs and
997 * cleaning up all state associated with the current configuration
999 usb_disable_device(dev, 0);
1001 /* Free the device number and remove the /proc/bus/usb entry */
1002 dev_dbg (&dev->dev, "unregistering device\n");
1003 usb_release_address(dev);
1004 usbfs_remove_device(dev);
1005 up(&dev->serialize);
1006 device_unregister(&dev->dev);
1010 * usb_choose_address - pick device address (usbcore-internal)
1011 * @dev: newly detected device (in DEFAULT state)
1013 * Picks a device address. It's up to the hub (or root hub) driver
1014 * to handle and manage enumeration, starting from the DEFAULT state.
1015 * Only hub drivers (but not virtual root hub drivers for host
1016 * controllers) should ever call this.
1018 void usb_choose_address(struct usb_device *dev)
1021 // FIXME needs locking for SMP!!
1022 /* why? this is called only from the hub thread,
1023 * which hopefully doesn't run on multiple CPU's simultaneously 8-)
1026 /* Try to allocate the next devnum beginning at bus->devnum_next. */
1027 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, dev->bus->devnum_next);
1029 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, 1);
1031 dev->bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1034 set_bit(devnum, dev->bus->devmap.devicemap);
1035 dev->devnum = devnum;
1040 * usb_release_address - deallocate device address (usbcore-internal)
1041 * @dev: newly removed device
1043 * Removes and deallocates the address assigned to a device.
1044 * Only hub drivers (but not virtual root hub drivers for host
1045 * controllers) should ever call this.
1047 void usb_release_address(struct usb_device *dev)
1049 if (dev->devnum > 0) {
1050 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1056 static inline void usb_show_string(struct usb_device *dev, char *id, int index)
1062 if (!(buf = kmalloc(256, GFP_KERNEL)))
1064 if (usb_string(dev, index, buf, 256) > 0)
1065 dev_printk(KERN_INFO, &dev->dev, "%s: %s\n", id, buf);
1069 static int usb_choose_configuration(struct usb_device *dev)
1073 c = dev->config[0].desc.bConfigurationValue;
1074 if (dev->descriptor.bNumConfigurations != 1) {
1075 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1076 struct usb_interface_descriptor *desc;
1078 /* heuristic: Linux is more likely to have class
1079 * drivers, so avoid vendor-specific interfaces.
1081 desc = &dev->config[i].intf_cache[0]
1083 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC)
1085 /* COMM/2/all is CDC ACM, except 0xff is MSFT RNDIS */
1086 if (desc->bInterfaceClass == USB_CLASS_COMM
1087 && desc->bInterfaceSubClass == 2
1088 && desc->bInterfaceProtocol == 0xff)
1090 c = dev->config[i].desc.bConfigurationValue;
1094 "configuration #%d chosen from %d choices\n",
1095 c, dev->descriptor.bNumConfigurations);
1101 * usb_new_device - perform initial device setup (usbcore-internal)
1102 * @dev: newly addressed device (in ADDRESS state)
1104 * This is called with devices which have been enumerated, but not yet
1105 * configured. The device descriptor is available, but not descriptors
1106 * for any device configuration. The caller owns dev->serialize, and
1107 * the device is not visible through sysfs or other filesystem code.
1109 * Returns 0 for success (device is configured and listed, with its
1110 * interfaces, in sysfs); else a negative errno value. On error, one
1111 * reference count to the device has been dropped.
1113 * This call is synchronous, and may not be used in an interrupt context.
1115 * Only the hub driver should ever call this; root hub registration
1116 * uses it only indirectly.
1118 int usb_new_device(struct usb_device *dev)
1123 err = usb_get_configuration(dev);
1125 dev_err(&dev->dev, "can't read configurations, error %d\n",
1130 /* Tell the world! */
1131 dev_dbg(&dev->dev, "new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1132 dev->descriptor.iManufacturer, dev->descriptor.iProduct, dev->descriptor.iSerialNumber);
1135 if (dev->descriptor.iProduct)
1136 usb_show_string(dev, "Product", dev->descriptor.iProduct);
1137 if (dev->descriptor.iManufacturer)
1138 usb_show_string(dev, "Manufacturer", dev->descriptor.iManufacturer);
1139 if (dev->descriptor.iSerialNumber)
1140 usb_show_string(dev, "SerialNumber", dev->descriptor.iSerialNumber);
1143 /* put device-specific files into sysfs */
1144 err = device_add (&dev->dev);
1146 dev_err(&dev->dev, "can't device_add, error %d\n", err);
1149 usb_create_sysfs_dev_files (dev);
1151 /* choose and set the configuration. that registers the interfaces
1152 * with the driver core, and lets usb device drivers bind to them.
1153 * NOTE: should interact with hub power budgeting.
1155 c = usb_choose_configuration(dev);
1156 err = usb_set_configuration(dev, c);
1158 dev_err(&dev->dev, "can't set config #%d, error %d\n", c, err);
1159 device_del(&dev->dev);
1163 /* USB device state == configured ... usable */
1165 /* add a /proc/bus/usb entry */
1166 usbfs_add_device(dev);
1170 dev->state = USB_STATE_NOTATTACHED;
1171 usb_release_address(dev);
1177 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
1178 * @dev: device the buffer will be used with
1179 * @size: requested buffer size
1180 * @mem_flags: affect whether allocation may block
1181 * @dma: used to return DMA address of buffer
1183 * Return value is either null (indicating no buffer could be allocated), or
1184 * the cpu-space pointer to a buffer that may be used to perform DMA to the
1185 * specified device. Such cpu-space buffers are returned along with the DMA
1186 * address (through the pointer provided).
1188 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
1189 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
1190 * mapping hardware for long idle periods. The implementation varies between
1191 * platforms, depending on details of how DMA will work to this device.
1192 * Using these buffers also helps prevent cacheline sharing problems on
1193 * architectures where CPU caches are not DMA-coherent.
1195 * When the buffer is no longer used, free it with usb_buffer_free().
1197 void *usb_buffer_alloc (
1198 struct usb_device *dev,
1204 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
1206 return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
1210 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
1211 * @dev: device the buffer was used with
1212 * @size: requested buffer size
1213 * @addr: CPU address of buffer
1214 * @dma: DMA address of buffer
1216 * This reclaims an I/O buffer, letting it be reused. The memory must have
1217 * been allocated using usb_buffer_alloc(), and the parameters must match
1218 * those provided in that allocation request.
1220 void usb_buffer_free (
1221 struct usb_device *dev,
1227 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
1229 dev->bus->op->buffer_free (dev->bus, size, addr, dma);
1233 * usb_buffer_map - create DMA mapping(s) for an urb
1234 * @urb: urb whose transfer_buffer/setup_packet will be mapped
1236 * Return value is either null (indicating no buffer could be mapped), or
1237 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
1238 * added to urb->transfer_flags if the operation succeeds. If the device
1239 * is connected to this system through a non-DMA controller, this operation
1242 * This call would normally be used for an urb which is reused, perhaps
1243 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1244 * calls to synchronize memory and dma state.
1246 * Reverse the effect of this call with usb_buffer_unmap().
1248 struct urb *usb_buffer_map (struct urb *urb)
1250 struct usb_bus *bus;
1251 struct device *controller;
1255 || !(bus = urb->dev->bus)
1256 || !(controller = bus->controller))
1259 if (controller->dma_mask) {
1260 urb->transfer_dma = dma_map_single (controller,
1261 urb->transfer_buffer, urb->transfer_buffer_length,
1262 usb_pipein (urb->pipe)
1263 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1264 if (usb_pipecontrol (urb->pipe))
1265 urb->setup_dma = dma_map_single (controller,
1267 sizeof (struct usb_ctrlrequest),
1269 // FIXME generic api broken like pci, can't report errors
1270 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1272 urb->transfer_dma = ~0;
1273 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1274 | URB_NO_SETUP_DMA_MAP);
1278 /* XXX DISABLED, no users currently. If you wish to re-enable this
1279 * XXX please determine whether the sync is to transfer ownership of
1280 * XXX the buffer from device to cpu or vice verse, and thusly use the
1281 * XXX appropriate _for_{cpu,device}() method. -DaveM
1286 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1287 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1289 void usb_buffer_dmasync (struct urb *urb)
1291 struct usb_bus *bus;
1292 struct device *controller;
1295 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1297 || !(bus = urb->dev->bus)
1298 || !(controller = bus->controller))
1301 if (controller->dma_mask) {
1302 dma_sync_single (controller,
1303 urb->transfer_dma, urb->transfer_buffer_length,
1304 usb_pipein (urb->pipe)
1305 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1306 if (usb_pipecontrol (urb->pipe))
1307 dma_sync_single (controller,
1309 sizeof (struct usb_ctrlrequest),
1316 * usb_buffer_unmap - free DMA mapping(s) for an urb
1317 * @urb: urb whose transfer_buffer will be unmapped
1319 * Reverses the effect of usb_buffer_map().
1321 void usb_buffer_unmap (struct urb *urb)
1323 struct usb_bus *bus;
1324 struct device *controller;
1327 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1329 || !(bus = urb->dev->bus)
1330 || !(controller = bus->controller))
1333 if (controller->dma_mask) {
1334 dma_unmap_single (controller,
1335 urb->transfer_dma, urb->transfer_buffer_length,
1336 usb_pipein (urb->pipe)
1337 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1338 if (usb_pipecontrol (urb->pipe))
1339 dma_unmap_single (controller,
1341 sizeof (struct usb_ctrlrequest),
1344 urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
1345 | URB_NO_SETUP_DMA_MAP);
1349 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1350 * @dev: device to which the scatterlist will be mapped
1351 * @pipe: endpoint defining the mapping direction
1352 * @sg: the scatterlist to map
1353 * @nents: the number of entries in the scatterlist
1355 * Return value is either < 0 (indicating no buffers could be mapped), or
1356 * the number of DMA mapping array entries in the scatterlist.
1358 * The caller is responsible for placing the resulting DMA addresses from
1359 * the scatterlist into URB transfer buffer pointers, and for setting the
1360 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1362 * Top I/O rates come from queuing URBs, instead of waiting for each one
1363 * to complete before starting the next I/O. This is particularly easy
1364 * to do with scatterlists. Just allocate and submit one URB for each DMA
1365 * mapping entry returned, stopping on the first error or when all succeed.
1366 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1368 * This call would normally be used when translating scatterlist requests,
1369 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1370 * may be able to coalesce mappings for improved I/O efficiency.
1372 * Reverse the effect of this call with usb_buffer_unmap_sg().
1374 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
1375 struct scatterlist *sg, int nents)
1377 struct usb_bus *bus;
1378 struct device *controller;
1381 || usb_pipecontrol (pipe)
1382 || !(bus = dev->bus)
1383 || !(controller = bus->controller)
1384 || !controller->dma_mask)
1387 // FIXME generic api broken like pci, can't report errors
1388 return dma_map_sg (controller, sg, nents,
1389 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1392 /* XXX DISABLED, no users currently. If you wish to re-enable this
1393 * XXX please determine whether the sync is to transfer ownership of
1394 * XXX the buffer from device to cpu or vice verse, and thusly use the
1395 * XXX appropriate _for_{cpu,device}() method. -DaveM
1400 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1401 * @dev: device to which the scatterlist will be mapped
1402 * @pipe: endpoint defining the mapping direction
1403 * @sg: the scatterlist to synchronize
1404 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1406 * Use this when you are re-using a scatterlist's data buffers for
1407 * another USB request.
1409 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
1410 struct scatterlist *sg, int n_hw_ents)
1412 struct usb_bus *bus;
1413 struct device *controller;
1416 || !(bus = dev->bus)
1417 || !(controller = bus->controller)
1418 || !controller->dma_mask)
1421 dma_sync_sg (controller, sg, n_hw_ents,
1422 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1427 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1428 * @dev: device to which the scatterlist will be mapped
1429 * @pipe: endpoint defining the mapping direction
1430 * @sg: the scatterlist to unmap
1431 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1433 * Reverses the effect of usb_buffer_map_sg().
1435 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1436 struct scatterlist *sg, int n_hw_ents)
1438 struct usb_bus *bus;
1439 struct device *controller;
1442 || !(bus = dev->bus)
1443 || !(controller = bus->controller)
1444 || !controller->dma_mask)
1447 dma_unmap_sg (controller, sg, n_hw_ents,
1448 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1451 static int usb_device_suspend(struct device *dev, u32 state)
1453 struct usb_interface *intf;
1454 struct usb_driver *driver;
1456 if ((dev->driver == NULL) ||
1457 (dev->driver == &usb_generic_driver) ||
1458 (dev->driver_data == &usb_generic_driver_data))
1461 intf = to_usb_interface(dev);
1462 driver = to_usb_driver(dev->driver);
1464 if (driver->suspend)
1465 return driver->suspend(intf, state);
1469 static int usb_device_resume(struct device *dev)
1471 struct usb_interface *intf;
1472 struct usb_driver *driver;
1474 if ((dev->driver == NULL) ||
1475 (dev->driver == &usb_generic_driver) ||
1476 (dev->driver_data == &usb_generic_driver_data))
1479 intf = to_usb_interface(dev);
1480 driver = to_usb_driver(dev->driver);
1483 return driver->resume(intf);
1487 struct bus_type usb_bus_type = {
1489 .match = usb_device_match,
1490 .hotplug = usb_hotplug,
1491 .suspend = usb_device_suspend,
1492 .resume = usb_device_resume,
1497 static int __init usb_setup_disable(char *str)
1503 /* format to disable USB on kernel command line is: nousb */
1504 __setup("nousb", usb_setup_disable);
1509 * for external read access to <nousb>
1511 int usb_disabled(void)
1519 static int __init usb_init(void)
1523 pr_info ("%s: USB support disabled\n", usbcore_name);
1527 retval = bus_register(&usb_bus_type);
1531 retval = usb_major_init();
1533 goto major_init_failed;
1534 retval = usbfs_init();
1536 goto fs_init_failed;
1537 retval = usb_hub_init();
1539 goto hub_init_failed;
1541 retval = driver_register(&usb_generic_driver);
1549 usb_major_cleanup();
1552 bus_unregister(&usb_bus_type);
1560 static void __exit usb_exit(void)
1562 /* This will matter if shutdown/reboot does exitcalls. */
1566 driver_unregister(&usb_generic_driver);
1567 usb_major_cleanup();
1571 bus_unregister(&usb_bus_type);
1574 subsys_initcall(usb_init);
1575 module_exit(usb_exit);
1578 * USB may be built into the kernel or be built as modules.
1579 * These symbols are exported for device (or host controller)
1580 * driver modules to use.
1582 EXPORT_SYMBOL(usb_epnum_to_ep_desc);
1584 EXPORT_SYMBOL(usb_register);
1585 EXPORT_SYMBOL(usb_deregister);
1586 EXPORT_SYMBOL(usb_disabled);
1588 EXPORT_SYMBOL(usb_alloc_dev);
1589 EXPORT_SYMBOL(usb_put_dev);
1590 EXPORT_SYMBOL(usb_get_dev);
1591 EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
1593 EXPORT_SYMBOL(usb_driver_claim_interface);
1594 EXPORT_SYMBOL(usb_driver_release_interface);
1595 EXPORT_SYMBOL(usb_match_id);
1596 EXPORT_SYMBOL(usb_find_interface);
1597 EXPORT_SYMBOL(usb_ifnum_to_if);
1598 EXPORT_SYMBOL(usb_altnum_to_altsetting);
1600 EXPORT_SYMBOL(usb_reset_device);
1601 EXPORT_SYMBOL(usb_disconnect);
1603 EXPORT_SYMBOL(__usb_get_extra_descriptor);
1605 EXPORT_SYMBOL(usb_find_device);
1606 EXPORT_SYMBOL(usb_get_current_frame_number);
1608 EXPORT_SYMBOL (usb_buffer_alloc);
1609 EXPORT_SYMBOL (usb_buffer_free);
1611 EXPORT_SYMBOL (usb_buffer_map);
1613 EXPORT_SYMBOL (usb_buffer_dmasync);
1615 EXPORT_SYMBOL (usb_buffer_unmap);
1617 EXPORT_SYMBOL (usb_buffer_map_sg);
1619 EXPORT_SYMBOL (usb_buffer_dmasync_sg);
1621 EXPORT_SYMBOL (usb_buffer_unmap_sg);
1623 MODULE_LICENSE("GPL");