2 * drivers/usb/driver.c - most of the driver model stuff for usb
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
6 * based on drivers/usb/usb.c which had the following copyrights:
7 * (C) Copyright Linus Torvalds 1999
8 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Andreas Gal 1999
10 * (C) Copyright Gregory P. Smith 1999
11 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
12 * (C) Copyright Randy Dunlap 2000
13 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Yggdrasil Computing, Inc. 2000
15 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright Greg Kroah-Hartman 2002-2003
18 * NOTE! This is not actually a driver at all, rather this is
19 * just a collection of helper routines that implement the
20 * matching, probing, releasing, suspending and resuming for
25 #include <linux/device.h>
26 #include <linux/usb.h>
27 #include <linux/workqueue.h>
31 static int usb_match_one_id(struct usb_interface *interface,
32 const struct usb_device_id *id);
35 struct list_head node;
36 struct usb_device_id id;
42 * Adds a new dynamic USBdevice ID to this driver,
43 * and cause the driver to probe for all devices again.
45 static ssize_t store_new_id(struct device_driver *driver,
46 const char *buf, size_t count)
48 struct usb_driver *usb_drv = to_usb_driver(driver);
49 struct usb_dynid *dynid;
55 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
59 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
63 INIT_LIST_HEAD(&dynid->node);
64 dynid->id.idVendor = idVendor;
65 dynid->id.idProduct = idProduct;
66 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
68 spin_lock(&usb_drv->dynids.lock);
69 list_add_tail(&usb_drv->dynids.list, &dynid->node);
70 spin_unlock(&usb_drv->dynids.lock);
72 if (get_driver(driver)) {
73 retval = driver_attach(driver);
81 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
83 static int usb_create_newid_file(struct usb_driver *usb_drv)
87 if (usb_drv->no_dynamic_id)
90 if (usb_drv->probe != NULL)
91 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
92 &driver_attr_new_id.attr);
97 static void usb_remove_newid_file(struct usb_driver *usb_drv)
99 if (usb_drv->no_dynamic_id)
102 if (usb_drv->probe != NULL)
103 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
104 &driver_attr_new_id.attr);
107 static void usb_free_dynids(struct usb_driver *usb_drv)
109 struct usb_dynid *dynid, *n;
111 spin_lock(&usb_drv->dynids.lock);
112 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
113 list_del(&dynid->node);
116 spin_unlock(&usb_drv->dynids.lock);
119 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
124 static void usb_remove_newid_file(struct usb_driver *usb_drv)
128 static inline void usb_free_dynids(struct usb_driver *usb_drv)
133 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
134 struct usb_driver *drv)
136 struct usb_dynid *dynid;
138 spin_lock(&drv->dynids.lock);
139 list_for_each_entry(dynid, &drv->dynids.list, node) {
140 if (usb_match_one_id(intf, &dynid->id)) {
141 spin_unlock(&drv->dynids.lock);
145 spin_unlock(&drv->dynids.lock);
150 /* called from driver core with dev locked */
151 static int usb_probe_device(struct device *dev)
153 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
154 struct usb_device *udev;
157 dev_dbg(dev, "%s\n", __FUNCTION__);
159 if (!is_usb_device(dev)) /* Sanity check */
162 udev = to_usb_device(dev);
164 /* TODO: Add real matching code */
166 /* The device should always appear to be in use
167 * unless the driver suports autosuspend.
169 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
171 error = udriver->probe(udev);
175 /* called from driver core with dev locked */
176 static int usb_unbind_device(struct device *dev)
178 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
180 udriver->disconnect(to_usb_device(dev));
185 /* called from driver core with dev locked */
186 static int usb_probe_interface(struct device *dev)
188 struct usb_driver *driver = to_usb_driver(dev->driver);
189 struct usb_interface *intf;
190 struct usb_device *udev;
191 const struct usb_device_id *id;
194 dev_dbg(dev, "%s\n", __FUNCTION__);
196 if (is_usb_device(dev)) /* Sanity check */
199 intf = to_usb_interface(dev);
200 udev = interface_to_usbdev(intf);
202 id = usb_match_id(intf, driver->id_table);
204 id = usb_match_dynamic_id(intf, driver);
206 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
208 error = usb_autoresume_device(udev);
212 /* Interface "power state" doesn't correspond to any hardware
213 * state whatsoever. We use it to record when it's bound to
214 * a driver that may start I/0: it's not frozen/quiesced.
217 intf->condition = USB_INTERFACE_BINDING;
219 /* The interface should always appear to be in use
220 * unless the driver suports autosuspend.
222 intf->pm_usage_cnt = !(driver->supports_autosuspend);
224 error = driver->probe(intf, id);
227 intf->needs_remote_wakeup = 0;
228 intf->condition = USB_INTERFACE_UNBOUND;
230 intf->condition = USB_INTERFACE_BOUND;
232 usb_autosuspend_device(udev);
238 /* called from driver core with dev locked */
239 static int usb_unbind_interface(struct device *dev)
241 struct usb_driver *driver = to_usb_driver(dev->driver);
242 struct usb_interface *intf = to_usb_interface(dev);
243 struct usb_device *udev;
246 intf->condition = USB_INTERFACE_UNBINDING;
248 /* Autoresume for set_interface call below */
249 udev = interface_to_usbdev(intf);
250 error = usb_autoresume_device(udev);
252 /* release all urbs for this interface */
253 usb_disable_interface(interface_to_usbdev(intf), intf);
255 driver->disconnect(intf);
257 /* reset other interface state */
258 usb_set_interface(interface_to_usbdev(intf),
259 intf->altsetting[0].desc.bInterfaceNumber,
261 usb_set_intfdata(intf, NULL);
263 intf->condition = USB_INTERFACE_UNBOUND;
265 intf->needs_remote_wakeup = 0;
268 usb_autosuspend_device(udev);
274 * usb_driver_claim_interface - bind a driver to an interface
275 * @driver: the driver to be bound
276 * @iface: the interface to which it will be bound; must be in the
277 * usb device's active configuration
278 * @priv: driver data associated with that interface
280 * This is used by usb device drivers that need to claim more than one
281 * interface on a device when probing (audio and acm are current examples).
282 * No device driver should directly modify internal usb_interface or
283 * usb_device structure members.
285 * Few drivers should need to use this routine, since the most natural
286 * way to bind to an interface is to return the private data from
287 * the driver's probe() method.
289 * Callers must own the device lock and the driver model's usb_bus_type.subsys
290 * writelock. So driver probe() entries don't need extra locking,
291 * but other call contexts may need to explicitly claim those locks.
293 int usb_driver_claim_interface(struct usb_driver *driver,
294 struct usb_interface *iface, void* priv)
296 struct device *dev = &iface->dev;
297 struct usb_device *udev = interface_to_usbdev(iface);
303 dev->driver = &driver->drvwrap.driver;
304 usb_set_intfdata(iface, priv);
307 iface->condition = USB_INTERFACE_BOUND;
309 iface->pm_usage_cnt = !(driver->supports_autosuspend);
312 /* if interface was already added, bind now; else let
313 * the future device_add() bind it, bypassing probe()
315 if (device_is_registered(dev))
316 retval = device_bind_driver(dev);
320 EXPORT_SYMBOL(usb_driver_claim_interface);
323 * usb_driver_release_interface - unbind a driver from an interface
324 * @driver: the driver to be unbound
325 * @iface: the interface from which it will be unbound
327 * This can be used by drivers to release an interface without waiting
328 * for their disconnect() methods to be called. In typical cases this
329 * also causes the driver disconnect() method to be called.
331 * This call is synchronous, and may not be used in an interrupt context.
332 * Callers must own the device lock and the driver model's usb_bus_type.subsys
333 * writelock. So driver disconnect() entries don't need extra locking,
334 * but other call contexts may need to explicitly claim those locks.
336 void usb_driver_release_interface(struct usb_driver *driver,
337 struct usb_interface *iface)
339 struct device *dev = &iface->dev;
340 struct usb_device *udev = interface_to_usbdev(iface);
342 /* this should never happen, don't release something that's not ours */
343 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
346 /* don't release from within disconnect() */
347 if (iface->condition != USB_INTERFACE_BOUND)
350 /* don't release if the interface hasn't been added yet */
351 if (device_is_registered(dev)) {
352 iface->condition = USB_INTERFACE_UNBINDING;
353 device_release_driver(dev);
357 usb_set_intfdata(iface, NULL);
360 iface->condition = USB_INTERFACE_UNBOUND;
361 mark_quiesced(iface);
362 iface->needs_remote_wakeup = 0;
365 EXPORT_SYMBOL(usb_driver_release_interface);
367 /* returns 0 if no match, 1 if match */
368 static int usb_match_one_id(struct usb_interface *interface,
369 const struct usb_device_id *id)
371 struct usb_host_interface *intf;
372 struct usb_device *dev;
374 /* proc_connectinfo in devio.c may call us with id == NULL. */
378 intf = interface->cur_altsetting;
379 dev = interface_to_usbdev(interface);
381 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
382 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
385 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
386 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
389 /* No need to test id->bcdDevice_lo != 0, since 0 is never
390 greater than any unsigned number. */
391 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
392 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
395 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
396 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
399 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
400 (id->bDeviceClass != dev->descriptor.bDeviceClass))
403 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
404 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
407 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
408 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
411 /* The interface class, subclass, and protocol should never be
412 * checked for a match if the device class is Vendor Specific,
413 * unless the match record specifies the Vendor ID. */
414 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
415 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
416 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
417 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
418 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
421 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
422 (id->bInterfaceClass != intf->desc.bInterfaceClass))
425 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
426 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
429 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
430 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
436 * usb_match_id - find first usb_device_id matching device or interface
437 * @interface: the interface of interest
438 * @id: array of usb_device_id structures, terminated by zero entry
440 * usb_match_id searches an array of usb_device_id's and returns
441 * the first one matching the device or interface, or null.
442 * This is used when binding (or rebinding) a driver to an interface.
443 * Most USB device drivers will use this indirectly, through the usb core,
444 * but some layered driver frameworks use it directly.
445 * These device tables are exported with MODULE_DEVICE_TABLE, through
446 * modutils, to support the driver loading functionality of USB hotplugging.
450 * The "match_flags" element in a usb_device_id controls which
451 * members are used. If the corresponding bit is set, the
452 * value in the device_id must match its corresponding member
453 * in the device or interface descriptor, or else the device_id
456 * "driver_info" is normally used only by device drivers,
457 * but you can create a wildcard "matches anything" usb_device_id
458 * as a driver's "modules.usbmap" entry if you provide an id with
459 * only a nonzero "driver_info" field. If you do this, the USB device
460 * driver's probe() routine should use additional intelligence to
461 * decide whether to bind to the specified interface.
463 * What Makes Good usb_device_id Tables:
465 * The match algorithm is very simple, so that intelligence in
466 * driver selection must come from smart driver id records.
467 * Unless you have good reasons to use another selection policy,
468 * provide match elements only in related groups, and order match
469 * specifiers from specific to general. Use the macros provided
470 * for that purpose if you can.
472 * The most specific match specifiers use device descriptor
473 * data. These are commonly used with product-specific matches;
474 * the USB_DEVICE macro lets you provide vendor and product IDs,
475 * and you can also match against ranges of product revisions.
476 * These are widely used for devices with application or vendor
477 * specific bDeviceClass values.
479 * Matches based on device class/subclass/protocol specifications
480 * are slightly more general; use the USB_DEVICE_INFO macro, or
481 * its siblings. These are used with single-function devices
482 * where bDeviceClass doesn't specify that each interface has
485 * Matches based on interface class/subclass/protocol are the
486 * most general; they let drivers bind to any interface on a
487 * multiple-function device. Use the USB_INTERFACE_INFO
488 * macro, or its siblings, to match class-per-interface style
489 * devices (as recorded in bInterfaceClass).
491 * Note that an entry created by USB_INTERFACE_INFO won't match
492 * any interface if the device class is set to Vendor-Specific.
493 * This is deliberate; according to the USB spec the meanings of
494 * the interface class/subclass/protocol for these devices are also
495 * vendor-specific, and hence matching against a standard product
496 * class wouldn't work anyway. If you really want to use an
497 * interface-based match for such a device, create a match record
498 * that also specifies the vendor ID. (Unforunately there isn't a
499 * standard macro for creating records like this.)
501 * Within those groups, remember that not all combinations are
502 * meaningful. For example, don't give a product version range
503 * without vendor and product IDs; or specify a protocol without
504 * its associated class and subclass.
506 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
507 const struct usb_device_id *id)
509 /* proc_connectinfo in devio.c may call us with id == NULL. */
513 /* It is important to check that id->driver_info is nonzero,
514 since an entry that is all zeroes except for a nonzero
515 id->driver_info is the way to create an entry that
516 indicates that the driver want to examine every
517 device and interface. */
518 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
519 id->driver_info; id++) {
520 if (usb_match_one_id(interface, id))
526 EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
528 static int usb_device_match(struct device *dev, struct device_driver *drv)
530 /* devices and interfaces are handled separately */
531 if (is_usb_device(dev)) {
533 /* interface drivers never match devices */
534 if (!is_usb_device_driver(drv))
537 /* TODO: Add real matching code */
541 struct usb_interface *intf;
542 struct usb_driver *usb_drv;
543 const struct usb_device_id *id;
545 /* device drivers never match interfaces */
546 if (is_usb_device_driver(drv))
549 intf = to_usb_interface(dev);
550 usb_drv = to_usb_driver(drv);
552 id = usb_match_id(intf, usb_drv->id_table);
556 id = usb_match_dynamic_id(intf, usb_drv);
564 #ifdef CONFIG_HOTPLUG
567 * This sends an uevent to userspace, typically helping to load driver
568 * or other modules, configure the device, and more. Drivers can provide
569 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
571 * We're called either from khubd (the typical case) or from root hub
572 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
573 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
574 * device (and this configuration!) are still present.
576 static int usb_uevent(struct device *dev, char **envp, int num_envp,
577 char *buffer, int buffer_size)
579 struct usb_interface *intf;
580 struct usb_device *usb_dev;
581 struct usb_host_interface *alt;
588 /* driver is often null here; dev_dbg() would oops */
589 pr_debug ("usb %s: uevent\n", dev->bus_id);
591 if (is_usb_device(dev)) {
592 usb_dev = to_usb_device(dev);
595 intf = to_usb_interface(dev);
596 usb_dev = interface_to_usbdev(intf);
597 alt = intf->cur_altsetting;
600 if (usb_dev->devnum < 0) {
601 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
605 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
609 #ifdef CONFIG_USB_DEVICEFS
610 /* If this is available, userspace programs can directly read
611 * all the device descriptors we don't tell them about. Or
612 * even act as usermode drivers.
614 * FIXME reduce hardwired intelligence here
616 if (add_uevent_var(envp, num_envp, &i,
617 buffer, buffer_size, &length,
618 "DEVICE=/proc/bus/usb/%03d/%03d",
619 usb_dev->bus->busnum, usb_dev->devnum))
623 /* per-device configurations are common */
624 if (add_uevent_var(envp, num_envp, &i,
625 buffer, buffer_size, &length,
627 le16_to_cpu(usb_dev->descriptor.idVendor),
628 le16_to_cpu(usb_dev->descriptor.idProduct),
629 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
632 /* class-based driver binding models */
633 if (add_uevent_var(envp, num_envp, &i,
634 buffer, buffer_size, &length,
636 usb_dev->descriptor.bDeviceClass,
637 usb_dev->descriptor.bDeviceSubClass,
638 usb_dev->descriptor.bDeviceProtocol))
641 if (!is_usb_device(dev)) {
643 if (add_uevent_var(envp, num_envp, &i,
644 buffer, buffer_size, &length,
645 "INTERFACE=%d/%d/%d",
646 alt->desc.bInterfaceClass,
647 alt->desc.bInterfaceSubClass,
648 alt->desc.bInterfaceProtocol))
651 if (add_uevent_var(envp, num_envp, &i,
652 buffer, buffer_size, &length,
653 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
654 le16_to_cpu(usb_dev->descriptor.idVendor),
655 le16_to_cpu(usb_dev->descriptor.idProduct),
656 le16_to_cpu(usb_dev->descriptor.bcdDevice),
657 usb_dev->descriptor.bDeviceClass,
658 usb_dev->descriptor.bDeviceSubClass,
659 usb_dev->descriptor.bDeviceProtocol,
660 alt->desc.bInterfaceClass,
661 alt->desc.bInterfaceSubClass,
662 alt->desc.bInterfaceProtocol))
673 static int usb_uevent(struct device *dev, char **envp,
674 int num_envp, char *buffer, int buffer_size)
679 #endif /* CONFIG_HOTPLUG */
682 * usb_register_device_driver - register a USB device (not interface) driver
683 * @new_udriver: USB operations for the device driver
684 * @owner: module owner of this driver.
686 * Registers a USB device driver with the USB core. The list of
687 * unattached devices will be rescanned whenever a new driver is
688 * added, allowing the new driver to attach to any recognized devices.
689 * Returns a negative error code on failure and 0 on success.
691 int usb_register_device_driver(struct usb_device_driver *new_udriver,
692 struct module *owner)
699 new_udriver->drvwrap.for_devices = 1;
700 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
701 new_udriver->drvwrap.driver.bus = &usb_bus_type;
702 new_udriver->drvwrap.driver.probe = usb_probe_device;
703 new_udriver->drvwrap.driver.remove = usb_unbind_device;
704 new_udriver->drvwrap.driver.owner = owner;
706 retval = driver_register(&new_udriver->drvwrap.driver);
709 pr_info("%s: registered new device driver %s\n",
710 usbcore_name, new_udriver->name);
711 usbfs_update_special();
713 printk(KERN_ERR "%s: error %d registering device "
715 usbcore_name, retval, new_udriver->name);
720 EXPORT_SYMBOL_GPL(usb_register_device_driver);
723 * usb_deregister_device_driver - unregister a USB device (not interface) driver
724 * @udriver: USB operations of the device driver to unregister
725 * Context: must be able to sleep
727 * Unlinks the specified driver from the internal USB driver list.
729 void usb_deregister_device_driver(struct usb_device_driver *udriver)
731 pr_info("%s: deregistering device driver %s\n",
732 usbcore_name, udriver->name);
734 driver_unregister(&udriver->drvwrap.driver);
735 usbfs_update_special();
737 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
740 * usb_register_driver - register a USB interface driver
741 * @new_driver: USB operations for the interface driver
742 * @owner: module owner of this driver.
744 * Registers a USB interface driver with the USB core. The list of
745 * unattached interfaces will be rescanned whenever a new driver is
746 * added, allowing the new driver to attach to any recognized interfaces.
747 * Returns a negative error code on failure and 0 on success.
749 * NOTE: if you want your driver to use the USB major number, you must call
750 * usb_register_dev() to enable that functionality. This function no longer
751 * takes care of that.
753 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
754 const char *mod_name)
761 new_driver->drvwrap.for_devices = 0;
762 new_driver->drvwrap.driver.name = (char *) new_driver->name;
763 new_driver->drvwrap.driver.bus = &usb_bus_type;
764 new_driver->drvwrap.driver.probe = usb_probe_interface;
765 new_driver->drvwrap.driver.remove = usb_unbind_interface;
766 new_driver->drvwrap.driver.owner = owner;
767 new_driver->drvwrap.driver.mod_name = mod_name;
768 spin_lock_init(&new_driver->dynids.lock);
769 INIT_LIST_HEAD(&new_driver->dynids.list);
771 retval = driver_register(&new_driver->drvwrap.driver);
774 pr_info("%s: registered new interface driver %s\n",
775 usbcore_name, new_driver->name);
776 usbfs_update_special();
777 usb_create_newid_file(new_driver);
779 printk(KERN_ERR "%s: error %d registering interface "
781 usbcore_name, retval, new_driver->name);
786 EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
789 * usb_deregister - unregister a USB interface driver
790 * @driver: USB operations of the interface driver to unregister
791 * Context: must be able to sleep
793 * Unlinks the specified driver from the internal USB driver list.
795 * NOTE: If you called usb_register_dev(), you still need to call
796 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
797 * this * call will no longer do it for you.
799 void usb_deregister(struct usb_driver *driver)
801 pr_info("%s: deregistering interface driver %s\n",
802 usbcore_name, driver->name);
804 usb_remove_newid_file(driver);
805 usb_free_dynids(driver);
806 driver_unregister(&driver->drvwrap.driver);
808 usbfs_update_special();
810 EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
814 /* Caller has locked udev's pm_mutex */
815 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
817 struct usb_device_driver *udriver;
820 if (udev->state == USB_STATE_NOTATTACHED ||
821 udev->state == USB_STATE_SUSPENDED)
824 /* For devices that don't have a driver, we do a standard suspend. */
825 if (udev->dev.driver == NULL) {
826 udev->do_remote_wakeup = 0;
827 status = usb_port_suspend(udev);
831 udriver = to_usb_device_driver(udev->dev.driver);
832 status = udriver->suspend(udev, msg);
835 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
837 udev->dev.power.power_state.event = msg.event;
841 /* Caller has locked udev's pm_mutex */
842 static int usb_resume_device(struct usb_device *udev)
844 struct usb_device_driver *udriver;
847 if (udev->state == USB_STATE_NOTATTACHED ||
848 udev->state != USB_STATE_SUSPENDED)
851 /* Can't resume it if it doesn't have a driver. */
852 if (udev->dev.driver == NULL) {
857 udriver = to_usb_device_driver(udev->dev.driver);
858 status = udriver->resume(udev);
861 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
863 udev->dev.power.power_state.event = PM_EVENT_ON;
867 /* Caller has locked intf's usb_device's pm mutex */
868 static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg)
870 struct usb_driver *driver;
873 /* with no hardware, USB interfaces only use FREEZE and ON states */
874 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
878 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
880 driver = to_usb_driver(intf->dev.driver);
882 if (driver->suspend && driver->resume) {
883 status = driver->suspend(intf, msg);
886 else if (!interface_to_usbdev(intf)->auto_pm)
887 dev_err(&intf->dev, "%s error %d\n",
890 // FIXME else if there's no suspend method, disconnect...
891 // Not possible if auto_pm is set...
892 dev_warn(&intf->dev, "no suspend for driver %s?\n",
898 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
900 intf->dev.power.power_state.event = msg.event;
904 /* Caller has locked intf's usb_device's pm_mutex */
905 static int usb_resume_interface(struct usb_interface *intf)
907 struct usb_driver *driver;
910 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
914 /* Don't let autoresume interfere with unbinding */
915 if (intf->condition == USB_INTERFACE_UNBINDING)
918 /* Can't resume it if it doesn't have a driver. */
919 if (intf->condition == USB_INTERFACE_UNBOUND) {
923 driver = to_usb_driver(intf->dev.driver);
925 if (driver->resume) {
926 status = driver->resume(intf);
928 dev_err(&intf->dev, "%s error %d\n",
933 dev_warn(&intf->dev, "no resume for driver %s?\n",
939 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
941 intf->dev.power.power_state.event = PM_EVENT_ON;
945 #ifdef CONFIG_USB_SUSPEND
947 /* Internal routine to check whether we may autosuspend a device. */
948 static int autosuspend_check(struct usb_device *udev)
951 struct usb_interface *intf;
953 /* For autosuspend, fail fast if anything is in use.
954 * Also fail if any interfaces require remote wakeup but it
955 * isn't available. */
956 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
957 if (udev->pm_usage_cnt > 0)
959 if (udev->actconfig) {
960 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
961 intf = udev->actconfig->interface[i];
962 if (!is_active(intf))
964 if (intf->pm_usage_cnt > 0)
966 if (intf->needs_remote_wakeup &&
967 !udev->do_remote_wakeup) {
968 dev_dbg(&udev->dev, "remote wakeup needed "
969 "for autosuspend\n");
979 #define autosuspend_check(udev) 0
984 * usb_suspend_both - suspend a USB device and its interfaces
985 * @udev: the usb_device to suspend
986 * @msg: Power Management message describing this state transition
988 * This is the central routine for suspending USB devices. It calls the
989 * suspend methods for all the interface drivers in @udev and then calls
990 * the suspend method for @udev itself. If an error occurs at any stage,
991 * all the interfaces which were suspended are resumed so that they remain
992 * in the same state as the device.
994 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
995 * checks first to make sure that neither the device itself or any of its
996 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
997 * are, the autosuspend fails.
999 * If the suspend succeeds, the routine recursively queues an autosuspend
1000 * request for @udev's parent device, thereby propagating the change up
1001 * the device tree. If all of the parent's children are now suspended,
1002 * the parent will autosuspend in turn.
1004 * The suspend method calls are subject to mutual exclusion under control
1005 * of @udev's pm_mutex. Many of these calls are also under the protection
1006 * of @udev's device lock (including all requests originating outside the
1007 * USB subsystem), but autosuspend requests generated by a child device or
1008 * interface driver may not be. Usbcore will insure that the method calls
1009 * do not arrive during bind, unbind, or reset operations. However, drivers
1010 * must be prepared to handle suspend calls arriving at unpredictable times.
1011 * The only way to block such calls is to do an autoresume (preventing
1012 * autosuspends) while holding @udev's device lock (preventing outside
1015 * The caller must hold @udev->pm_mutex.
1017 * This routine can run only in process context.
1019 int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1023 struct usb_interface *intf;
1024 struct usb_device *parent = udev->parent;
1026 cancel_delayed_work(&udev->autosuspend);
1027 if (udev->state == USB_STATE_NOTATTACHED)
1029 if (udev->state == USB_STATE_SUSPENDED)
1032 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1034 if (udev->auto_pm) {
1035 status = autosuspend_check(udev);
1040 /* Suspend all the interfaces and then udev itself */
1041 if (udev->actconfig) {
1042 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1043 intf = udev->actconfig->interface[i];
1044 status = usb_suspend_interface(intf, msg);
1050 status = usb_suspend_device(udev, msg);
1052 /* If the suspend failed, resume interfaces that did get suspended */
1055 intf = udev->actconfig->interface[i];
1056 usb_resume_interface(intf);
1059 /* If the suspend succeeded, propagate it up the tree */
1061 usb_autosuspend_device(parent);
1063 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1068 * usb_resume_both - resume a USB device and its interfaces
1069 * @udev: the usb_device to resume
1071 * This is the central routine for resuming USB devices. It calls the
1072 * the resume method for @udev and then calls the resume methods for all
1073 * the interface drivers in @udev.
1075 * Before starting the resume, the routine calls itself recursively for
1076 * the parent device of @udev, thereby propagating the change up the device
1077 * tree and assuring that @udev will be able to resume. If the parent is
1078 * unable to resume successfully, the routine fails.
1080 * The resume method calls are subject to mutual exclusion under control
1081 * of @udev's pm_mutex. Many of these calls are also under the protection
1082 * of @udev's device lock (including all requests originating outside the
1083 * USB subsystem), but autoresume requests generated by a child device or
1084 * interface driver may not be. Usbcore will insure that the method calls
1085 * do not arrive during bind, unbind, or reset operations. However, drivers
1086 * must be prepared to handle resume calls arriving at unpredictable times.
1087 * The only way to block such calls is to do an autoresume (preventing
1088 * other autoresumes) while holding @udev's device lock (preventing outside
1091 * The caller must hold @udev->pm_mutex.
1093 * This routine can run only in process context.
1095 int usb_resume_both(struct usb_device *udev)
1099 struct usb_interface *intf;
1100 struct usb_device *parent = udev->parent;
1102 cancel_delayed_work(&udev->autosuspend);
1103 if (udev->state == USB_STATE_NOTATTACHED)
1106 /* Propagate the resume up the tree, if necessary */
1107 if (udev->state == USB_STATE_SUSPENDED) {
1109 status = usb_autoresume_device(parent);
1111 status = usb_resume_device(udev);
1113 usb_autosuspend_device(parent);
1115 /* It's possible usb_resume_device()
1116 * failed after the port was
1117 * unsuspended, causing udev to be
1118 * logically disconnected. We don't
1119 * want usb_disconnect() to autosuspend
1120 * the parent again, so tell it that
1121 * udev disconnected while still
1124 USB_STATE_NOTATTACHED)
1125 udev->discon_suspended = 1;
1130 /* We can't progagate beyond the USB subsystem,
1131 * so if a root hub's controller is suspended
1132 * then we're stuck. */
1133 if (udev->dev.parent->power.power_state.event !=
1135 status = -EHOSTUNREACH;
1137 status = usb_resume_device(udev);
1141 /* Needed only for setting udev->dev.power.power_state.event
1142 * and for possible debugging message. */
1143 status = usb_resume_device(udev);
1146 if (status == 0 && udev->actconfig) {
1147 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1148 intf = udev->actconfig->interface[i];
1149 usb_resume_interface(intf);
1153 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1157 #ifdef CONFIG_USB_SUSPEND
1159 /* Internal routine to adjust a device's usage counter and change
1160 * its autosuspend state.
1162 static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1167 udev->pm_usage_cnt += inc_usage_cnt;
1168 WARN_ON(udev->pm_usage_cnt < 0);
1169 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1171 status = usb_resume_both(udev);
1173 udev->pm_usage_cnt -= inc_usage_cnt;
1174 } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0)
1175 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1176 USB_AUTOSUSPEND_DELAY);
1177 usb_pm_unlock(udev);
1182 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1183 * @udev: the usb_device to autosuspend
1185 * This routine should be called when a core subsystem is finished using
1186 * @udev and wants to allow it to autosuspend. Examples would be when
1187 * @udev's device file in usbfs is closed or after a configuration change.
1189 * @udev's usage counter is decremented. If it or any of the usage counters
1190 * for an active interface is greater than 0, no autosuspend request will be
1191 * queued. (If an interface driver does not support autosuspend then its
1192 * usage counter is permanently positive.) Furthermore, if an interface
1193 * driver requires remote-wakeup capability during autosuspend but remote
1194 * wakeup is disabled, the autosuspend will fail.
1196 * Often the caller will hold @udev's device lock, but this is not
1199 * This routine can run only in process context.
1201 void usb_autosuspend_device(struct usb_device *udev)
1205 status = usb_autopm_do_device(udev, -1);
1206 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1207 // __FUNCTION__, udev->pm_usage_cnt);
1211 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1212 * @udev: the usb_device to autoresume
1214 * This routine should be called when a core subsystem wants to use @udev
1215 * and needs to guarantee that it is not suspended. No autosuspend will
1216 * occur until usb_autosuspend_device is called. (Note that this will not
1217 * prevent suspend events originating in the PM core.) Examples would be
1218 * when @udev's device file in usbfs is opened or when a remote-wakeup
1219 * request is received.
1221 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1222 * However if the autoresume fails then the usage counter is re-decremented.
1224 * Often the caller will hold @udev's device lock, but this is not
1225 * necessary (and attempting it might cause deadlock).
1227 * This routine can run only in process context.
1229 int usb_autoresume_device(struct usb_device *udev)
1233 status = usb_autopm_do_device(udev, 1);
1234 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1235 // __FUNCTION__, status, udev->pm_usage_cnt);
1239 /* Internal routine to adjust an interface's usage counter and change
1240 * its device's autosuspend state.
1242 static int usb_autopm_do_interface(struct usb_interface *intf,
1245 struct usb_device *udev = interface_to_usbdev(intf);
1249 if (intf->condition == USB_INTERFACE_UNBOUND)
1252 intf->pm_usage_cnt += inc_usage_cnt;
1253 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
1255 status = usb_resume_both(udev);
1257 intf->pm_usage_cnt -= inc_usage_cnt;
1258 } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0)
1259 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1260 USB_AUTOSUSPEND_DELAY);
1262 usb_pm_unlock(udev);
1267 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1268 * @intf: the usb_interface whose counter should be decremented
1270 * This routine should be called by an interface driver when it is
1271 * finished using @intf and wants to allow it to autosuspend. A typical
1272 * example would be a character-device driver when its device file is
1275 * The routine decrements @intf's usage counter. When the counter reaches
1276 * 0, a delayed autosuspend request for @intf's device is queued. When
1277 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1278 * the other usage counters for the sibling interfaces and @intf's
1279 * usb_device, the device and all its interfaces will be autosuspended.
1281 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1282 * core will not change its value other than the increment and decrement
1283 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1284 * may use this simple counter-oriented discipline or may set the value
1287 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1288 * take place only if the device's remote-wakeup facility is enabled.
1290 * Suspend method calls queued by this routine can arrive at any time
1291 * while @intf is resumed and its usage counter is equal to 0. They are
1292 * not protected by the usb_device's lock but only by its pm_mutex.
1293 * Drivers must provide their own synchronization.
1295 * This routine can run only in process context.
1297 void usb_autopm_put_interface(struct usb_interface *intf)
1301 status = usb_autopm_do_interface(intf, -1);
1302 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1303 // __FUNCTION__, status, intf->pm_usage_cnt);
1305 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1308 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1309 * @intf: the usb_interface whose counter should be incremented
1311 * This routine should be called by an interface driver when it wants to
1312 * use @intf and needs to guarantee that it is not suspended. In addition,
1313 * the routine prevents @intf from being autosuspended subsequently. (Note
1314 * that this will not prevent suspend events originating in the PM core.)
1315 * This prevention will persist until usb_autopm_put_interface() is called
1316 * or @intf is unbound. A typical example would be a character-device
1317 * driver when its device file is opened.
1319 * The routine increments @intf's usage counter. So long as the counter
1320 * is greater than 0, autosuspend will not be allowed for @intf or its
1321 * usb_device. When the driver is finished using @intf it should call
1322 * usb_autopm_put_interface() to decrement the usage counter and queue
1323 * a delayed autosuspend request (if the counter is <= 0).
1325 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1326 * core will not change its value other than the increment and decrement
1327 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1328 * may use this simple counter-oriented discipline or may set the value
1331 * Resume method calls generated by this routine can arrive at any time
1332 * while @intf is suspended. They are not protected by the usb_device's
1333 * lock but only by its pm_mutex. Drivers must provide their own
1336 * This routine can run only in process context.
1338 int usb_autopm_get_interface(struct usb_interface *intf)
1342 status = usb_autopm_do_interface(intf, 1);
1343 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1344 // __FUNCTION__, status, intf->pm_usage_cnt);
1347 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1350 * usb_autopm_set_interface - set a USB interface's autosuspend state
1351 * @intf: the usb_interface whose state should be set
1353 * This routine sets the autosuspend state of @intf's device according
1354 * to @intf's usage counter, which the caller must have set previously.
1355 * If the counter is <= 0, the device is autosuspended (if it isn't
1356 * already suspended and if nothing else prevents the autosuspend). If
1357 * the counter is > 0, the device is autoresumed (if it isn't already
1360 int usb_autopm_set_interface(struct usb_interface *intf)
1364 status = usb_autopm_do_interface(intf, 0);
1365 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1366 // __FUNCTION__, status, intf->pm_usage_cnt);
1369 EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1371 #endif /* CONFIG_USB_SUSPEND */
1373 static int usb_suspend(struct device *dev, pm_message_t message)
1377 if (is_usb_device(dev)) {
1378 struct usb_device *udev = to_usb_device(dev);
1382 status = usb_suspend_both(udev, message);
1383 usb_pm_unlock(udev);
1389 static int usb_resume(struct device *dev)
1393 if (is_usb_device(dev)) {
1394 struct usb_device *udev = to_usb_device(dev);
1398 status = usb_resume_both(udev);
1399 usb_pm_unlock(udev);
1401 /* Rebind drivers that had no suspend method? */
1407 #endif /* CONFIG_PM */
1409 struct bus_type usb_bus_type = {
1411 .match = usb_device_match,
1412 .uevent = usb_uevent,
1414 .suspend = usb_suspend,
1415 .resume = usb_resume,