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/slab.h>
27 #include <linux/export.h>
28 #include <linux/usb.h>
29 #include <linux/usb/quirks.h>
30 #include <linux/usb/hcd.h>
38 * Adds a new dynamic USBdevice ID to this driver,
39 * and cause the driver to probe for all devices again.
41 ssize_t usb_store_new_id(struct usb_dynids *dynids,
42 struct device_driver *driver,
43 const char *buf, size_t count)
45 struct usb_dynid *dynid;
48 unsigned int bInterfaceClass = 0;
52 fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct,
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
66 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
67 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
70 spin_lock(&dynids->lock);
71 list_add_tail(&dynid->node, &dynids->list);
72 spin_unlock(&dynids->lock);
74 retval = driver_attach(driver);
80 EXPORT_SYMBOL_GPL(usb_store_new_id);
82 static ssize_t store_new_id(struct device_driver *driver,
83 const char *buf, size_t count)
85 struct usb_driver *usb_drv = to_usb_driver(driver);
87 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
89 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
92 * store_remove_id - remove a USB device ID from this driver
93 * @driver: target device driver
94 * @buf: buffer for scanning device ID data
97 * Removes a dynamic usb device ID from this driver.
100 store_remove_id(struct device_driver *driver, const char *buf, size_t count)
102 struct usb_dynid *dynid, *n;
103 struct usb_driver *usb_driver = to_usb_driver(driver);
109 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
113 spin_lock(&usb_driver->dynids.lock);
114 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
115 struct usb_device_id *id = &dynid->id;
116 if ((id->idVendor == idVendor) &&
117 (id->idProduct == idProduct)) {
118 list_del(&dynid->node);
124 spin_unlock(&usb_driver->dynids.lock);
130 static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
132 static int usb_create_newid_files(struct usb_driver *usb_drv)
136 if (usb_drv->no_dynamic_id)
139 if (usb_drv->probe != NULL) {
140 error = driver_create_file(&usb_drv->drvwrap.driver,
141 &driver_attr_new_id);
143 error = driver_create_file(&usb_drv->drvwrap.driver,
144 &driver_attr_remove_id);
146 driver_remove_file(&usb_drv->drvwrap.driver,
147 &driver_attr_new_id);
154 static void usb_remove_newid_files(struct usb_driver *usb_drv)
156 if (usb_drv->no_dynamic_id)
159 if (usb_drv->probe != NULL) {
160 driver_remove_file(&usb_drv->drvwrap.driver,
161 &driver_attr_remove_id);
162 driver_remove_file(&usb_drv->drvwrap.driver,
163 &driver_attr_new_id);
167 static void usb_free_dynids(struct usb_driver *usb_drv)
169 struct usb_dynid *dynid, *n;
171 spin_lock(&usb_drv->dynids.lock);
172 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
173 list_del(&dynid->node);
176 spin_unlock(&usb_drv->dynids.lock);
179 static inline int usb_create_newid_files(struct usb_driver *usb_drv)
184 static void usb_remove_newid_files(struct usb_driver *usb_drv)
188 static inline void usb_free_dynids(struct usb_driver *usb_drv)
193 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
194 struct usb_driver *drv)
196 struct usb_dynid *dynid;
198 spin_lock(&drv->dynids.lock);
199 list_for_each_entry(dynid, &drv->dynids.list, node) {
200 if (usb_match_one_id(intf, &dynid->id)) {
201 spin_unlock(&drv->dynids.lock);
205 spin_unlock(&drv->dynids.lock);
210 /* called from driver core with dev locked */
211 static int usb_probe_device(struct device *dev)
213 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
214 struct usb_device *udev = to_usb_device(dev);
217 dev_dbg(dev, "%s\n", __func__);
219 /* TODO: Add real matching code */
221 /* The device should always appear to be in use
222 * unless the driver suports autosuspend.
224 if (!udriver->supports_autosuspend)
225 error = usb_autoresume_device(udev);
228 error = udriver->probe(udev);
232 /* called from driver core with dev locked */
233 static int usb_unbind_device(struct device *dev)
235 struct usb_device *udev = to_usb_device(dev);
236 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
238 udriver->disconnect(udev);
239 if (!udriver->supports_autosuspend)
240 usb_autosuspend_device(udev);
245 * Cancel any pending scheduled resets
247 * [see usb_queue_reset_device()]
249 * Called after unconfiguring / when releasing interfaces. See
250 * comments in __usb_queue_reset_device() regarding
251 * udev->reset_running.
253 static void usb_cancel_queued_reset(struct usb_interface *iface)
255 if (iface->reset_running == 0)
256 cancel_work_sync(&iface->reset_ws);
259 /* called from driver core with dev locked */
260 static int usb_probe_interface(struct device *dev)
262 struct usb_driver *driver = to_usb_driver(dev->driver);
263 struct usb_interface *intf = to_usb_interface(dev);
264 struct usb_device *udev = interface_to_usbdev(intf);
265 const struct usb_device_id *id;
268 dev_dbg(dev, "%s\n", __func__);
270 intf->needs_binding = 0;
272 if (usb_device_is_owned(udev))
275 if (udev->authorized == 0) {
276 dev_err(&intf->dev, "Device is not authorized for usage\n");
280 id = usb_match_id(intf, driver->id_table);
282 id = usb_match_dynamic_id(intf, driver);
286 dev_dbg(dev, "%s - got id\n", __func__);
288 error = usb_autoresume_device(udev);
292 intf->condition = USB_INTERFACE_BINDING;
294 /* Probed interfaces are initially active. They are
295 * runtime-PM-enabled only if the driver has autosuspend support.
296 * They are sensitive to their children's power states.
298 pm_runtime_set_active(dev);
299 pm_suspend_ignore_children(dev, false);
300 if (driver->supports_autosuspend)
301 pm_runtime_enable(dev);
303 /* Carry out a deferred switch to altsetting 0 */
304 if (intf->needs_altsetting0) {
305 error = usb_set_interface(udev, intf->altsetting[0].
306 desc.bInterfaceNumber, 0);
309 intf->needs_altsetting0 = 0;
312 error = driver->probe(intf, id);
316 intf->condition = USB_INTERFACE_BOUND;
317 usb_autosuspend_device(udev);
321 intf->needs_remote_wakeup = 0;
322 intf->condition = USB_INTERFACE_UNBOUND;
323 usb_cancel_queued_reset(intf);
325 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
326 if (driver->supports_autosuspend)
327 pm_runtime_disable(dev);
328 pm_runtime_set_suspended(dev);
330 usb_autosuspend_device(udev);
334 /* called from driver core with dev locked */
335 static int usb_unbind_interface(struct device *dev)
337 struct usb_driver *driver = to_usb_driver(dev->driver);
338 struct usb_interface *intf = to_usb_interface(dev);
339 struct usb_device *udev;
342 intf->condition = USB_INTERFACE_UNBINDING;
344 /* Autoresume for set_interface call below */
345 udev = interface_to_usbdev(intf);
346 error = usb_autoresume_device(udev);
348 /* Terminate all URBs for this interface unless the driver
349 * supports "soft" unbinding.
351 if (!driver->soft_unbind)
352 usb_disable_interface(udev, intf, false);
354 driver->disconnect(intf);
355 usb_cancel_queued_reset(intf);
357 /* Reset other interface state.
358 * We cannot do a Set-Interface if the device is suspended or
359 * if it is prepared for a system sleep (since installing a new
360 * altsetting means creating new endpoint device entries).
361 * When either of these happens, defer the Set-Interface.
363 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
364 /* Already in altsetting 0 so skip Set-Interface.
365 * Just re-enable it without affecting the endpoint toggles.
367 usb_enable_interface(udev, intf, false);
368 } else if (!error && !intf->dev.power.is_prepared) {
369 r = usb_set_interface(udev, intf->altsetting[0].
370 desc.bInterfaceNumber, 0);
372 intf->needs_altsetting0 = 1;
374 intf->needs_altsetting0 = 1;
376 usb_set_intfdata(intf, NULL);
378 intf->condition = USB_INTERFACE_UNBOUND;
379 intf->needs_remote_wakeup = 0;
381 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
382 if (driver->supports_autosuspend)
383 pm_runtime_disable(dev);
384 pm_runtime_set_suspended(dev);
386 /* Undo any residual pm_autopm_get_interface_* calls */
387 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
388 usb_autopm_put_interface_no_suspend(intf);
389 atomic_set(&intf->pm_usage_cnt, 0);
392 usb_autosuspend_device(udev);
398 * usb_driver_claim_interface - bind a driver to an interface
399 * @driver: the driver to be bound
400 * @iface: the interface to which it will be bound; must be in the
401 * usb device's active configuration
402 * @priv: driver data associated with that interface
404 * This is used by usb device drivers that need to claim more than one
405 * interface on a device when probing (audio and acm are current examples).
406 * No device driver should directly modify internal usb_interface or
407 * usb_device structure members.
409 * Few drivers should need to use this routine, since the most natural
410 * way to bind to an interface is to return the private data from
411 * the driver's probe() method.
413 * Callers must own the device lock, so driver probe() entries don't need
414 * extra locking, but other call contexts may need to explicitly claim that
417 int usb_driver_claim_interface(struct usb_driver *driver,
418 struct usb_interface *iface, void *priv)
420 struct device *dev = &iface->dev;
426 dev->driver = &driver->drvwrap.driver;
427 usb_set_intfdata(iface, priv);
428 iface->needs_binding = 0;
430 iface->condition = USB_INTERFACE_BOUND;
432 /* Claimed interfaces are initially inactive (suspended) and
433 * runtime-PM-enabled, but only if the driver has autosuspend
434 * support. Otherwise they are marked active, to prevent the
435 * device from being autosuspended, but left disabled. In either
436 * case they are sensitive to their children's power states.
438 pm_suspend_ignore_children(dev, false);
439 if (driver->supports_autosuspend)
440 pm_runtime_enable(dev);
442 pm_runtime_set_active(dev);
444 /* if interface was already added, bind now; else let
445 * the future device_add() bind it, bypassing probe()
447 if (device_is_registered(dev))
448 retval = device_bind_driver(dev);
452 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
455 * usb_driver_release_interface - unbind a driver from an interface
456 * @driver: the driver to be unbound
457 * @iface: the interface from which it will be unbound
459 * This can be used by drivers to release an interface without waiting
460 * for their disconnect() methods to be called. In typical cases this
461 * also causes the driver disconnect() method to be called.
463 * This call is synchronous, and may not be used in an interrupt context.
464 * Callers must own the device lock, so driver disconnect() entries don't
465 * need extra locking, but other call contexts may need to explicitly claim
468 void usb_driver_release_interface(struct usb_driver *driver,
469 struct usb_interface *iface)
471 struct device *dev = &iface->dev;
473 /* this should never happen, don't release something that's not ours */
474 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
477 /* don't release from within disconnect() */
478 if (iface->condition != USB_INTERFACE_BOUND)
480 iface->condition = USB_INTERFACE_UNBINDING;
482 /* Release via the driver core only if the interface
483 * has already been registered
485 if (device_is_registered(dev)) {
486 device_release_driver(dev);
489 usb_unbind_interface(dev);
494 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
496 /* returns 0 if no match, 1 if match */
497 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
499 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
500 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
503 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
504 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
507 /* No need to test id->bcdDevice_lo != 0, since 0 is never
508 greater than any unsigned number. */
509 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
510 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
513 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
514 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
517 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
518 (id->bDeviceClass != dev->descriptor.bDeviceClass))
521 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
522 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
525 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
526 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
532 /* returns 0 if no match, 1 if match */
533 int usb_match_one_id(struct usb_interface *interface,
534 const struct usb_device_id *id)
536 struct usb_host_interface *intf;
537 struct usb_device *dev;
539 /* proc_connectinfo in devio.c may call us with id == NULL. */
543 intf = interface->cur_altsetting;
544 dev = interface_to_usbdev(interface);
546 if (!usb_match_device(dev, id))
549 /* The interface class, subclass, and protocol should never be
550 * checked for a match if the device class is Vendor Specific,
551 * unless the match record specifies the Vendor ID. */
552 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
553 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
554 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
555 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
556 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
559 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
560 (id->bInterfaceClass != intf->desc.bInterfaceClass))
563 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
564 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
567 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
568 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
573 EXPORT_SYMBOL_GPL(usb_match_one_id);
576 * usb_match_id - find first usb_device_id matching device or interface
577 * @interface: the interface of interest
578 * @id: array of usb_device_id structures, terminated by zero entry
580 * usb_match_id searches an array of usb_device_id's and returns
581 * the first one matching the device or interface, or null.
582 * This is used when binding (or rebinding) a driver to an interface.
583 * Most USB device drivers will use this indirectly, through the usb core,
584 * but some layered driver frameworks use it directly.
585 * These device tables are exported with MODULE_DEVICE_TABLE, through
586 * modutils, to support the driver loading functionality of USB hotplugging.
590 * The "match_flags" element in a usb_device_id controls which
591 * members are used. If the corresponding bit is set, the
592 * value in the device_id must match its corresponding member
593 * in the device or interface descriptor, or else the device_id
596 * "driver_info" is normally used only by device drivers,
597 * but you can create a wildcard "matches anything" usb_device_id
598 * as a driver's "modules.usbmap" entry if you provide an id with
599 * only a nonzero "driver_info" field. If you do this, the USB device
600 * driver's probe() routine should use additional intelligence to
601 * decide whether to bind to the specified interface.
603 * What Makes Good usb_device_id Tables:
605 * The match algorithm is very simple, so that intelligence in
606 * driver selection must come from smart driver id records.
607 * Unless you have good reasons to use another selection policy,
608 * provide match elements only in related groups, and order match
609 * specifiers from specific to general. Use the macros provided
610 * for that purpose if you can.
612 * The most specific match specifiers use device descriptor
613 * data. These are commonly used with product-specific matches;
614 * the USB_DEVICE macro lets you provide vendor and product IDs,
615 * and you can also match against ranges of product revisions.
616 * These are widely used for devices with application or vendor
617 * specific bDeviceClass values.
619 * Matches based on device class/subclass/protocol specifications
620 * are slightly more general; use the USB_DEVICE_INFO macro, or
621 * its siblings. These are used with single-function devices
622 * where bDeviceClass doesn't specify that each interface has
625 * Matches based on interface class/subclass/protocol are the
626 * most general; they let drivers bind to any interface on a
627 * multiple-function device. Use the USB_INTERFACE_INFO
628 * macro, or its siblings, to match class-per-interface style
629 * devices (as recorded in bInterfaceClass).
631 * Note that an entry created by USB_INTERFACE_INFO won't match
632 * any interface if the device class is set to Vendor-Specific.
633 * This is deliberate; according to the USB spec the meanings of
634 * the interface class/subclass/protocol for these devices are also
635 * vendor-specific, and hence matching against a standard product
636 * class wouldn't work anyway. If you really want to use an
637 * interface-based match for such a device, create a match record
638 * that also specifies the vendor ID. (Unforunately there isn't a
639 * standard macro for creating records like this.)
641 * Within those groups, remember that not all combinations are
642 * meaningful. For example, don't give a product version range
643 * without vendor and product IDs; or specify a protocol without
644 * its associated class and subclass.
646 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
647 const struct usb_device_id *id)
649 /* proc_connectinfo in devio.c may call us with id == NULL. */
653 /* It is important to check that id->driver_info is nonzero,
654 since an entry that is all zeroes except for a nonzero
655 id->driver_info is the way to create an entry that
656 indicates that the driver want to examine every
657 device and interface. */
658 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
659 id->bInterfaceClass || id->driver_info; id++) {
660 if (usb_match_one_id(interface, id))
666 EXPORT_SYMBOL_GPL(usb_match_id);
668 static int usb_device_match(struct device *dev, struct device_driver *drv)
670 /* devices and interfaces are handled separately */
671 if (is_usb_device(dev)) {
673 /* interface drivers never match devices */
674 if (!is_usb_device_driver(drv))
677 /* TODO: Add real matching code */
680 } else if (is_usb_interface(dev)) {
681 struct usb_interface *intf;
682 struct usb_driver *usb_drv;
683 const struct usb_device_id *id;
685 /* device drivers never match interfaces */
686 if (is_usb_device_driver(drv))
689 intf = to_usb_interface(dev);
690 usb_drv = to_usb_driver(drv);
692 id = usb_match_id(intf, usb_drv->id_table);
696 id = usb_match_dynamic_id(intf, usb_drv);
704 #ifdef CONFIG_HOTPLUG
705 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
707 struct usb_device *usb_dev;
709 if (is_usb_device(dev)) {
710 usb_dev = to_usb_device(dev);
711 } else if (is_usb_interface(dev)) {
712 struct usb_interface *intf = to_usb_interface(dev);
714 usb_dev = interface_to_usbdev(intf);
719 if (usb_dev->devnum < 0) {
720 /* driver is often null here; dev_dbg() would oops */
721 pr_debug("usb %s: already deleted?\n", dev_name(dev));
725 pr_debug("usb %s: bus removed?\n", dev_name(dev));
729 #ifdef CONFIG_USB_DEVICEFS
730 /* If this is available, userspace programs can directly read
731 * all the device descriptors we don't tell them about. Or
732 * act as usermode drivers.
734 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
735 usb_dev->bus->busnum, usb_dev->devnum))
739 /* per-device configurations are common */
740 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
741 le16_to_cpu(usb_dev->descriptor.idVendor),
742 le16_to_cpu(usb_dev->descriptor.idProduct),
743 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
746 /* class-based driver binding models */
747 if (add_uevent_var(env, "TYPE=%d/%d/%d",
748 usb_dev->descriptor.bDeviceClass,
749 usb_dev->descriptor.bDeviceSubClass,
750 usb_dev->descriptor.bDeviceProtocol))
758 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
762 #endif /* CONFIG_HOTPLUG */
765 * usb_register_device_driver - register a USB device (not interface) driver
766 * @new_udriver: USB operations for the device driver
767 * @owner: module owner of this driver.
769 * Registers a USB device driver with the USB core. The list of
770 * unattached devices will be rescanned whenever a new driver is
771 * added, allowing the new driver to attach to any recognized devices.
772 * Returns a negative error code on failure and 0 on success.
774 int usb_register_device_driver(struct usb_device_driver *new_udriver,
775 struct module *owner)
782 new_udriver->drvwrap.for_devices = 1;
783 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
784 new_udriver->drvwrap.driver.bus = &usb_bus_type;
785 new_udriver->drvwrap.driver.probe = usb_probe_device;
786 new_udriver->drvwrap.driver.remove = usb_unbind_device;
787 new_udriver->drvwrap.driver.owner = owner;
789 retval = driver_register(&new_udriver->drvwrap.driver);
792 pr_info("%s: registered new device driver %s\n",
793 usbcore_name, new_udriver->name);
794 usbfs_update_special();
796 printk(KERN_ERR "%s: error %d registering device "
798 usbcore_name, retval, new_udriver->name);
803 EXPORT_SYMBOL_GPL(usb_register_device_driver);
806 * usb_deregister_device_driver - unregister a USB device (not interface) driver
807 * @udriver: USB operations of the device driver to unregister
808 * Context: must be able to sleep
810 * Unlinks the specified driver from the internal USB driver list.
812 void usb_deregister_device_driver(struct usb_device_driver *udriver)
814 pr_info("%s: deregistering device driver %s\n",
815 usbcore_name, udriver->name);
817 driver_unregister(&udriver->drvwrap.driver);
818 usbfs_update_special();
820 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
823 * usb_register_driver - register a USB interface driver
824 * @new_driver: USB operations for the interface driver
825 * @owner: module owner of this driver.
826 * @mod_name: module name string
828 * Registers a USB interface driver with the USB core. The list of
829 * unattached interfaces will be rescanned whenever a new driver is
830 * added, allowing the new driver to attach to any recognized interfaces.
831 * Returns a negative error code on failure and 0 on success.
833 * NOTE: if you want your driver to use the USB major number, you must call
834 * usb_register_dev() to enable that functionality. This function no longer
835 * takes care of that.
837 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
838 const char *mod_name)
845 new_driver->drvwrap.for_devices = 0;
846 new_driver->drvwrap.driver.name = (char *) new_driver->name;
847 new_driver->drvwrap.driver.bus = &usb_bus_type;
848 new_driver->drvwrap.driver.probe = usb_probe_interface;
849 new_driver->drvwrap.driver.remove = usb_unbind_interface;
850 new_driver->drvwrap.driver.owner = owner;
851 new_driver->drvwrap.driver.mod_name = mod_name;
852 spin_lock_init(&new_driver->dynids.lock);
853 INIT_LIST_HEAD(&new_driver->dynids.list);
855 retval = driver_register(&new_driver->drvwrap.driver);
859 usbfs_update_special();
861 retval = usb_create_newid_files(new_driver);
865 pr_info("%s: registered new interface driver %s\n",
866 usbcore_name, new_driver->name);
872 driver_unregister(&new_driver->drvwrap.driver);
874 printk(KERN_ERR "%s: error %d registering interface "
876 usbcore_name, retval, new_driver->name);
879 EXPORT_SYMBOL_GPL(usb_register_driver);
882 * usb_deregister - unregister a USB interface driver
883 * @driver: USB operations of the interface driver to unregister
884 * Context: must be able to sleep
886 * Unlinks the specified driver from the internal USB driver list.
888 * NOTE: If you called usb_register_dev(), you still need to call
889 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
890 * this * call will no longer do it for you.
892 void usb_deregister(struct usb_driver *driver)
894 pr_info("%s: deregistering interface driver %s\n",
895 usbcore_name, driver->name);
897 usb_remove_newid_files(driver);
898 driver_unregister(&driver->drvwrap.driver);
899 usb_free_dynids(driver);
901 usbfs_update_special();
903 EXPORT_SYMBOL_GPL(usb_deregister);
905 /* Forced unbinding of a USB interface driver, either because
906 * it doesn't support pre_reset/post_reset/reset_resume or
907 * because it doesn't support suspend/resume.
909 * The caller must hold @intf's device's lock, but not its pm_mutex
910 * and not @intf->dev.sem.
912 void usb_forced_unbind_intf(struct usb_interface *intf)
914 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
916 dev_dbg(&intf->dev, "forced unbind\n");
917 usb_driver_release_interface(driver, intf);
919 /* Mark the interface for later rebinding */
920 intf->needs_binding = 1;
923 /* Delayed forced unbinding of a USB interface driver and scan
926 * The caller must hold @intf's device's lock, but not its pm_mutex
927 * and not @intf->dev.sem.
929 * Note: Rebinds will be skipped if a system sleep transition is in
930 * progress and the PM "complete" callback hasn't occurred yet.
932 void usb_rebind_intf(struct usb_interface *intf)
936 /* Delayed unbind of an existing driver */
937 if (intf->dev.driver)
938 usb_forced_unbind_intf(intf);
940 /* Try to rebind the interface */
941 if (!intf->dev.power.is_prepared) {
942 intf->needs_binding = 0;
943 rc = device_attach(&intf->dev);
945 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
951 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
952 * There is no check for reset_resume here because it can be determined
953 * only during resume whether reset_resume is needed.
955 * The caller must hold @udev's device lock.
957 static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
959 struct usb_host_config *config;
961 struct usb_interface *intf;
962 struct usb_driver *drv;
964 config = udev->actconfig;
966 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
967 intf = config->interface[i];
969 if (intf->dev.driver) {
970 drv = to_usb_driver(intf->dev.driver);
971 if (!drv->suspend || !drv->resume)
972 usb_forced_unbind_intf(intf);
978 /* Unbind drivers for @udev's interfaces that failed to support reset-resume.
979 * These interfaces have the needs_binding flag set by usb_resume_interface().
981 * The caller must hold @udev's device lock.
983 static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
985 struct usb_host_config *config;
987 struct usb_interface *intf;
989 config = udev->actconfig;
991 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
992 intf = config->interface[i];
993 if (intf->dev.driver && intf->needs_binding)
994 usb_forced_unbind_intf(intf);
999 static void do_rebind_interfaces(struct usb_device *udev)
1001 struct usb_host_config *config;
1003 struct usb_interface *intf;
1005 config = udev->actconfig;
1007 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1008 intf = config->interface[i];
1009 if (intf->needs_binding)
1010 usb_rebind_intf(intf);
1015 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1017 struct usb_device_driver *udriver;
1020 if (udev->state == USB_STATE_NOTATTACHED ||
1021 udev->state == USB_STATE_SUSPENDED)
1024 /* For devices that don't have a driver, we do a generic suspend. */
1025 if (udev->dev.driver)
1026 udriver = to_usb_device_driver(udev->dev.driver);
1028 udev->do_remote_wakeup = 0;
1029 udriver = &usb_generic_driver;
1031 status = udriver->suspend(udev, msg);
1034 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1038 static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1040 struct usb_device_driver *udriver;
1043 if (udev->state == USB_STATE_NOTATTACHED)
1046 /* Can't resume it if it doesn't have a driver. */
1047 if (udev->dev.driver == NULL) {
1052 /* Non-root devices on a full/low-speed bus must wait for their
1053 * companion high-speed root hub, in case a handoff is needed.
1055 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
1056 device_pm_wait_for_dev(&udev->dev,
1057 &udev->bus->hs_companion->root_hub->dev);
1059 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1060 udev->reset_resume = 1;
1062 udriver = to_usb_device_driver(udev->dev.driver);
1063 status = udriver->resume(udev, msg);
1066 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1070 static int usb_suspend_interface(struct usb_device *udev,
1071 struct usb_interface *intf, pm_message_t msg)
1073 struct usb_driver *driver;
1076 if (udev->state == USB_STATE_NOTATTACHED ||
1077 intf->condition == USB_INTERFACE_UNBOUND)
1079 driver = to_usb_driver(intf->dev.driver);
1081 /* at this time we know the driver supports suspend */
1082 status = driver->suspend(intf, msg);
1083 if (status && !PMSG_IS_AUTO(msg))
1084 dev_err(&intf->dev, "suspend error %d\n", status);
1087 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1091 static int usb_resume_interface(struct usb_device *udev,
1092 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1094 struct usb_driver *driver;
1097 if (udev->state == USB_STATE_NOTATTACHED)
1100 /* Don't let autoresume interfere with unbinding */
1101 if (intf->condition == USB_INTERFACE_UNBINDING)
1104 /* Can't resume it if it doesn't have a driver. */
1105 if (intf->condition == USB_INTERFACE_UNBOUND) {
1107 /* Carry out a deferred switch to altsetting 0 */
1108 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
1109 usb_set_interface(udev, intf->altsetting[0].
1110 desc.bInterfaceNumber, 0);
1111 intf->needs_altsetting0 = 0;
1116 /* Don't resume if the interface is marked for rebinding */
1117 if (intf->needs_binding)
1119 driver = to_usb_driver(intf->dev.driver);
1122 if (driver->reset_resume) {
1123 status = driver->reset_resume(intf);
1125 dev_err(&intf->dev, "%s error %d\n",
1126 "reset_resume", status);
1128 intf->needs_binding = 1;
1129 dev_warn(&intf->dev, "no %s for driver %s?\n",
1130 "reset_resume", driver->name);
1133 status = driver->resume(intf);
1135 dev_err(&intf->dev, "resume error %d\n", status);
1139 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1141 /* Later we will unbind the driver and/or reprobe, if necessary */
1146 * usb_suspend_both - suspend a USB device and its interfaces
1147 * @udev: the usb_device to suspend
1148 * @msg: Power Management message describing this state transition
1150 * This is the central routine for suspending USB devices. It calls the
1151 * suspend methods for all the interface drivers in @udev and then calls
1152 * the suspend method for @udev itself. If an error occurs at any stage,
1153 * all the interfaces which were suspended are resumed so that they remain
1154 * in the same state as the device.
1156 * Autosuspend requests originating from a child device or an interface
1157 * driver may be made without the protection of @udev's device lock, but
1158 * all other suspend calls will hold the lock. Usbcore will insure that
1159 * method calls do not arrive during bind, unbind, or reset operations.
1160 * However drivers must be prepared to handle suspend calls arriving at
1161 * unpredictable times.
1163 * This routine can run only in process context.
1165 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1169 struct usb_interface *intf;
1171 if (udev->state == USB_STATE_NOTATTACHED ||
1172 udev->state == USB_STATE_SUSPENDED)
1175 /* Suspend all the interfaces and then udev itself */
1176 if (udev->actconfig) {
1177 n = udev->actconfig->desc.bNumInterfaces;
1178 for (i = n - 1; i >= 0; --i) {
1179 intf = udev->actconfig->interface[i];
1180 status = usb_suspend_interface(udev, intf, msg);
1182 /* Ignore errors during system sleep transitions */
1183 if (!PMSG_IS_AUTO(msg))
1190 status = usb_suspend_device(udev, msg);
1192 /* Again, ignore errors during system sleep transitions */
1193 if (!PMSG_IS_AUTO(msg))
1197 /* If the suspend failed, resume interfaces that did get suspended */
1199 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1201 intf = udev->actconfig->interface[i];
1202 usb_resume_interface(udev, intf, msg, 0);
1205 /* If the suspend succeeded then prevent any more URB submissions
1206 * and flush any outstanding URBs.
1209 udev->can_submit = 0;
1210 for (i = 0; i < 16; ++i) {
1211 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1212 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1217 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1222 * usb_resume_both - resume a USB device and its interfaces
1223 * @udev: the usb_device to resume
1224 * @msg: Power Management message describing this state transition
1226 * This is the central routine for resuming USB devices. It calls the
1227 * the resume method for @udev and then calls the resume methods for all
1228 * the interface drivers in @udev.
1230 * Autoresume requests originating from a child device or an interface
1231 * driver may be made without the protection of @udev's device lock, but
1232 * all other resume calls will hold the lock. Usbcore will insure that
1233 * method calls do not arrive during bind, unbind, or reset operations.
1234 * However drivers must be prepared to handle resume calls arriving at
1235 * unpredictable times.
1237 * This routine can run only in process context.
1239 static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1243 struct usb_interface *intf;
1245 if (udev->state == USB_STATE_NOTATTACHED) {
1249 udev->can_submit = 1;
1251 /* Resume the device */
1252 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1253 status = usb_resume_device(udev, msg);
1255 /* Resume the interfaces */
1256 if (status == 0 && udev->actconfig) {
1257 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1258 intf = udev->actconfig->interface[i];
1259 usb_resume_interface(udev, intf, msg,
1260 udev->reset_resume);
1263 usb_mark_last_busy(udev);
1266 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1268 udev->reset_resume = 0;
1272 static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1276 /* Remote wakeup is needed only when we actually go to sleep.
1277 * For things like FREEZE and QUIESCE, if the device is already
1278 * autosuspended then its current wakeup setting is okay.
1280 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1281 if (udev->state != USB_STATE_SUSPENDED)
1282 udev->do_remote_wakeup = 0;
1286 /* Enable remote wakeup if it is allowed, even if no interface drivers
1289 w = device_may_wakeup(&udev->dev);
1291 /* If the device is autosuspended with the wrong wakeup setting,
1292 * autoresume now so the setting can be changed.
1294 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1295 pm_runtime_resume(&udev->dev);
1296 udev->do_remote_wakeup = w;
1299 /* The device lock is held by the PM core */
1300 int usb_suspend(struct device *dev, pm_message_t msg)
1302 struct usb_device *udev = to_usb_device(dev);
1304 unbind_no_pm_drivers_interfaces(udev);
1306 /* From now on we are sure all drivers support suspend/resume
1307 * but not necessarily reset_resume()
1308 * so we may still need to unbind and rebind upon resume
1310 choose_wakeup(udev, msg);
1311 return usb_suspend_both(udev, msg);
1314 /* The device lock is held by the PM core */
1315 int usb_resume_complete(struct device *dev)
1317 struct usb_device *udev = to_usb_device(dev);
1319 /* For PM complete calls, all we do is rebind interfaces
1320 * whose needs_binding flag is set
1322 if (udev->state != USB_STATE_NOTATTACHED)
1323 do_rebind_interfaces(udev);
1327 /* The device lock is held by the PM core */
1328 int usb_resume(struct device *dev, pm_message_t msg)
1330 struct usb_device *udev = to_usb_device(dev);
1333 /* For all calls, take the device back to full power and
1334 * tell the PM core in case it was autosuspended previously.
1335 * Unbind the interfaces that will need rebinding later,
1336 * because they fail to support reset_resume.
1337 * (This can't be done in usb_resume_interface()
1338 * above because it doesn't own the right set of locks.)
1340 status = usb_resume_both(udev, msg);
1342 pm_runtime_disable(dev);
1343 pm_runtime_set_active(dev);
1344 pm_runtime_enable(dev);
1345 unbind_no_reset_resume_drivers_interfaces(udev);
1348 /* Avoid PM error messages for devices disconnected while suspended
1349 * as we'll display regular disconnect messages just a bit later.
1351 if (status == -ENODEV || status == -ESHUTDOWN)
1356 #endif /* CONFIG_PM */
1358 #ifdef CONFIG_USB_SUSPEND
1361 * usb_enable_autosuspend - allow a USB device to be autosuspended
1362 * @udev: the USB device which may be autosuspended
1364 * This routine allows @udev to be autosuspended. An autosuspend won't
1365 * take place until the autosuspend_delay has elapsed and all the other
1366 * necessary conditions are satisfied.
1368 * The caller must hold @udev's device lock.
1370 void usb_enable_autosuspend(struct usb_device *udev)
1372 pm_runtime_allow(&udev->dev);
1374 EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1377 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1378 * @udev: the USB device which may not be autosuspended
1380 * This routine prevents @udev from being autosuspended and wakes it up
1381 * if it is already autosuspended.
1383 * The caller must hold @udev's device lock.
1385 void usb_disable_autosuspend(struct usb_device *udev)
1387 pm_runtime_forbid(&udev->dev);
1389 EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1392 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1393 * @udev: the usb_device to autosuspend
1395 * This routine should be called when a core subsystem is finished using
1396 * @udev and wants to allow it to autosuspend. Examples would be when
1397 * @udev's device file in usbfs is closed or after a configuration change.
1399 * @udev's usage counter is decremented; if it drops to 0 and all the
1400 * interfaces are inactive then a delayed autosuspend will be attempted.
1401 * The attempt may fail (see autosuspend_check()).
1403 * The caller must hold @udev's device lock.
1405 * This routine can run only in process context.
1407 void usb_autosuspend_device(struct usb_device *udev)
1411 usb_mark_last_busy(udev);
1412 status = pm_runtime_put_sync_autosuspend(&udev->dev);
1413 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1414 __func__, atomic_read(&udev->dev.power.usage_count),
1419 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1420 * @udev: the usb_device to autoresume
1422 * This routine should be called when a core subsystem wants to use @udev
1423 * and needs to guarantee that it is not suspended. No autosuspend will
1424 * occur until usb_autosuspend_device() is called. (Note that this will
1425 * not prevent suspend events originating in the PM core.) Examples would
1426 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1427 * request is received.
1429 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1430 * However if the autoresume fails then the usage counter is re-decremented.
1432 * The caller must hold @udev's device lock.
1434 * This routine can run only in process context.
1436 int usb_autoresume_device(struct usb_device *udev)
1440 status = pm_runtime_get_sync(&udev->dev);
1442 pm_runtime_put_sync(&udev->dev);
1443 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1444 __func__, atomic_read(&udev->dev.power.usage_count),
1452 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1453 * @intf: the usb_interface whose counter should be decremented
1455 * This routine should be called by an interface driver when it is
1456 * finished using @intf and wants to allow it to autosuspend. A typical
1457 * example would be a character-device driver when its device file is
1460 * The routine decrements @intf's usage counter. When the counter reaches
1461 * 0, a delayed autosuspend request for @intf's device is attempted. The
1462 * attempt may fail (see autosuspend_check()).
1464 * This routine can run only in process context.
1466 void usb_autopm_put_interface(struct usb_interface *intf)
1468 struct usb_device *udev = interface_to_usbdev(intf);
1471 usb_mark_last_busy(udev);
1472 atomic_dec(&intf->pm_usage_cnt);
1473 status = pm_runtime_put_sync(&intf->dev);
1474 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1475 __func__, atomic_read(&intf->dev.power.usage_count),
1478 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1481 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1482 * @intf: the usb_interface whose counter should be decremented
1484 * This routine does much the same thing as usb_autopm_put_interface():
1485 * It decrements @intf's usage counter and schedules a delayed
1486 * autosuspend request if the counter is <= 0. The difference is that it
1487 * does not perform any synchronization; callers should hold a private
1488 * lock and handle all synchronization issues themselves.
1490 * Typically a driver would call this routine during an URB's completion
1491 * handler, if no more URBs were pending.
1493 * This routine can run in atomic context.
1495 void usb_autopm_put_interface_async(struct usb_interface *intf)
1497 struct usb_device *udev = interface_to_usbdev(intf);
1500 usb_mark_last_busy(udev);
1501 atomic_dec(&intf->pm_usage_cnt);
1502 status = pm_runtime_put(&intf->dev);
1503 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1504 __func__, atomic_read(&intf->dev.power.usage_count),
1507 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1510 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1511 * @intf: the usb_interface whose counter should be decremented
1513 * This routine decrements @intf's usage counter but does not carry out an
1516 * This routine can run in atomic context.
1518 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1520 struct usb_device *udev = interface_to_usbdev(intf);
1522 usb_mark_last_busy(udev);
1523 atomic_dec(&intf->pm_usage_cnt);
1524 pm_runtime_put_noidle(&intf->dev);
1526 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1529 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1530 * @intf: the usb_interface whose counter should be incremented
1532 * This routine should be called by an interface driver when it wants to
1533 * use @intf and needs to guarantee that it is not suspended. In addition,
1534 * the routine prevents @intf from being autosuspended subsequently. (Note
1535 * that this will not prevent suspend events originating in the PM core.)
1536 * This prevention will persist until usb_autopm_put_interface() is called
1537 * or @intf is unbound. A typical example would be a character-device
1538 * driver when its device file is opened.
1540 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1541 * However if the autoresume fails then the counter is re-decremented.
1543 * This routine can run only in process context.
1545 int usb_autopm_get_interface(struct usb_interface *intf)
1549 status = pm_runtime_get_sync(&intf->dev);
1551 pm_runtime_put_sync(&intf->dev);
1553 atomic_inc(&intf->pm_usage_cnt);
1554 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1555 __func__, atomic_read(&intf->dev.power.usage_count),
1561 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1564 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1565 * @intf: the usb_interface whose counter should be incremented
1567 * This routine does much the same thing as
1568 * usb_autopm_get_interface(): It increments @intf's usage counter and
1569 * queues an autoresume request if the device is suspended. The
1570 * differences are that it does not perform any synchronization (callers
1571 * should hold a private lock and handle all synchronization issues
1572 * themselves), and it does not autoresume the device directly (it only
1573 * queues a request). After a successful call, the device may not yet be
1576 * This routine can run in atomic context.
1578 int usb_autopm_get_interface_async(struct usb_interface *intf)
1582 status = pm_runtime_get(&intf->dev);
1583 if (status < 0 && status != -EINPROGRESS)
1584 pm_runtime_put_noidle(&intf->dev);
1586 atomic_inc(&intf->pm_usage_cnt);
1587 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1588 __func__, atomic_read(&intf->dev.power.usage_count),
1590 if (status > 0 || status == -EINPROGRESS)
1594 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1597 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1598 * @intf: the usb_interface whose counter should be incremented
1600 * This routine increments @intf's usage counter but does not carry out an
1603 * This routine can run in atomic context.
1605 void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1607 struct usb_device *udev = interface_to_usbdev(intf);
1609 usb_mark_last_busy(udev);
1610 atomic_inc(&intf->pm_usage_cnt);
1611 pm_runtime_get_noresume(&intf->dev);
1613 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1615 /* Internal routine to check whether we may autosuspend a device. */
1616 static int autosuspend_check(struct usb_device *udev)
1619 struct usb_interface *intf;
1621 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1622 * any interface drivers require remote wakeup but it isn't available.
1625 if (udev->actconfig) {
1626 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1627 intf = udev->actconfig->interface[i];
1629 /* We don't need to check interfaces that are
1630 * disabled for runtime PM. Either they are unbound
1631 * or else their drivers don't support autosuspend
1632 * and so they are permanently active.
1634 if (intf->dev.power.disable_depth)
1636 if (atomic_read(&intf->dev.power.usage_count) > 0)
1638 w |= intf->needs_remote_wakeup;
1640 /* Don't allow autosuspend if the device will need
1641 * a reset-resume and any of its interface drivers
1642 * doesn't include support or needs remote wakeup.
1644 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1645 struct usb_driver *driver;
1647 driver = to_usb_driver(intf->dev.driver);
1648 if (!driver->reset_resume ||
1649 intf->needs_remote_wakeup)
1654 if (w && !device_can_wakeup(&udev->dev)) {
1655 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1658 udev->do_remote_wakeup = w;
1662 int usb_runtime_suspend(struct device *dev)
1664 struct usb_device *udev = to_usb_device(dev);
1667 /* A USB device can be suspended if it passes the various autosuspend
1668 * checks. Runtime suspend for a USB device means suspending all the
1669 * interfaces and then the device itself.
1671 if (autosuspend_check(udev) != 0)
1674 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1676 /* Allow a retry if autosuspend failed temporarily */
1677 if (status == -EAGAIN || status == -EBUSY)
1678 usb_mark_last_busy(udev);
1680 /* The PM core reacts badly unless the return code is 0,
1681 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1688 int usb_runtime_resume(struct device *dev)
1690 struct usb_device *udev = to_usb_device(dev);
1693 /* Runtime resume for a USB device means resuming both the device
1694 * and all its interfaces.
1696 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1700 int usb_runtime_idle(struct device *dev)
1702 struct usb_device *udev = to_usb_device(dev);
1704 /* An idle USB device can be suspended if it passes the various
1705 * autosuspend checks.
1707 if (autosuspend_check(udev) == 0)
1708 pm_runtime_autosuspend(dev);
1712 int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1714 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1717 if (hcd->driver->set_usb2_hw_lpm) {
1718 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1720 udev->usb2_hw_lpm_enabled = enable;
1726 #endif /* CONFIG_USB_SUSPEND */
1728 struct bus_type usb_bus_type = {
1730 .match = usb_device_match,
1731 .uevent = usb_uevent,