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>
36 * Adds a new dynamic USBdevice ID to this driver,
37 * and cause the driver to probe for all devices again.
39 ssize_t usb_store_new_id(struct usb_dynids *dynids,
40 struct device_driver *driver,
41 const char *buf, size_t count)
43 struct usb_dynid *dynid;
46 unsigned int bInterfaceClass = 0;
50 fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct,
55 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
59 INIT_LIST_HEAD(&dynid->node);
60 dynid->id.idVendor = idVendor;
61 dynid->id.idProduct = idProduct;
62 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
63 if (fields > 2 && bInterfaceClass) {
64 if (bInterfaceClass > 255)
67 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
68 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
71 spin_lock(&dynids->lock);
72 list_add_tail(&dynid->node, &dynids->list);
73 spin_unlock(&dynids->lock);
75 retval = driver_attach(driver);
81 EXPORT_SYMBOL_GPL(usb_store_new_id);
83 ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
85 struct usb_dynid *dynid;
88 list_for_each_entry(dynid, &dynids->list, node)
89 if (dynid->id.bInterfaceClass != 0)
90 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
91 dynid->id.idVendor, dynid->id.idProduct,
92 dynid->id.bInterfaceClass);
94 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
95 dynid->id.idVendor, dynid->id.idProduct);
98 EXPORT_SYMBOL_GPL(usb_show_dynids);
100 static ssize_t new_id_show(struct device_driver *driver, char *buf)
102 struct usb_driver *usb_drv = to_usb_driver(driver);
104 return usb_show_dynids(&usb_drv->dynids, buf);
107 static ssize_t new_id_store(struct device_driver *driver,
108 const char *buf, size_t count)
110 struct usb_driver *usb_drv = to_usb_driver(driver);
112 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
114 static DRIVER_ATTR_RW(new_id);
117 * Remove a USB device ID from this driver
119 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
122 struct usb_dynid *dynid, *n;
123 struct usb_driver *usb_driver = to_usb_driver(driver);
128 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
132 spin_lock(&usb_driver->dynids.lock);
133 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
134 struct usb_device_id *id = &dynid->id;
135 if ((id->idVendor == idVendor) &&
136 (id->idProduct == idProduct)) {
137 list_del(&dynid->node);
142 spin_unlock(&usb_driver->dynids.lock);
146 static ssize_t remove_id_show(struct device_driver *driver, char *buf)
148 return new_id_show(driver, buf);
150 static DRIVER_ATTR_RW(remove_id);
152 static int usb_create_newid_files(struct usb_driver *usb_drv)
156 if (usb_drv->no_dynamic_id)
159 if (usb_drv->probe != NULL) {
160 error = driver_create_file(&usb_drv->drvwrap.driver,
161 &driver_attr_new_id);
163 error = driver_create_file(&usb_drv->drvwrap.driver,
164 &driver_attr_remove_id);
166 driver_remove_file(&usb_drv->drvwrap.driver,
167 &driver_attr_new_id);
174 static void usb_remove_newid_files(struct usb_driver *usb_drv)
176 if (usb_drv->no_dynamic_id)
179 if (usb_drv->probe != NULL) {
180 driver_remove_file(&usb_drv->drvwrap.driver,
181 &driver_attr_remove_id);
182 driver_remove_file(&usb_drv->drvwrap.driver,
183 &driver_attr_new_id);
187 static void usb_free_dynids(struct usb_driver *usb_drv)
189 struct usb_dynid *dynid, *n;
191 spin_lock(&usb_drv->dynids.lock);
192 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
193 list_del(&dynid->node);
196 spin_unlock(&usb_drv->dynids.lock);
199 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
200 struct usb_driver *drv)
202 struct usb_dynid *dynid;
204 spin_lock(&drv->dynids.lock);
205 list_for_each_entry(dynid, &drv->dynids.list, node) {
206 if (usb_match_one_id(intf, &dynid->id)) {
207 spin_unlock(&drv->dynids.lock);
211 spin_unlock(&drv->dynids.lock);
216 /* called from driver core with dev locked */
217 static int usb_probe_device(struct device *dev)
219 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
220 struct usb_device *udev = to_usb_device(dev);
223 dev_dbg(dev, "%s\n", __func__);
225 /* TODO: Add real matching code */
227 /* The device should always appear to be in use
228 * unless the driver supports autosuspend.
230 if (!udriver->supports_autosuspend)
231 error = usb_autoresume_device(udev);
234 error = udriver->probe(udev);
238 /* called from driver core with dev locked */
239 static int usb_unbind_device(struct device *dev)
241 struct usb_device *udev = to_usb_device(dev);
242 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
244 udriver->disconnect(udev);
245 if (!udriver->supports_autosuspend)
246 usb_autosuspend_device(udev);
251 * Cancel any pending scheduled resets
253 * [see usb_queue_reset_device()]
255 * Called after unconfiguring / when releasing interfaces. See
256 * comments in __usb_queue_reset_device() regarding
257 * udev->reset_running.
259 static void usb_cancel_queued_reset(struct usb_interface *iface)
261 if (iface->reset_running == 0)
262 cancel_work_sync(&iface->reset_ws);
265 /* called from driver core with dev locked */
266 static int usb_probe_interface(struct device *dev)
268 struct usb_driver *driver = to_usb_driver(dev->driver);
269 struct usb_interface *intf = to_usb_interface(dev);
270 struct usb_device *udev = interface_to_usbdev(intf);
271 const struct usb_device_id *id;
273 int lpm_disable_error;
275 dev_dbg(dev, "%s\n", __func__);
277 intf->needs_binding = 0;
279 if (usb_device_is_owned(udev))
282 if (udev->authorized == 0) {
283 dev_err(&intf->dev, "Device is not authorized for usage\n");
287 id = usb_match_id(intf, driver->id_table);
289 id = usb_match_dynamic_id(intf, driver);
293 dev_dbg(dev, "%s - got id\n", __func__);
295 error = usb_autoresume_device(udev);
299 intf->condition = USB_INTERFACE_BINDING;
301 /* Probed interfaces are initially active. They are
302 * runtime-PM-enabled only if the driver has autosuspend support.
303 * They are sensitive to their children's power states.
305 pm_runtime_set_active(dev);
306 pm_suspend_ignore_children(dev, false);
307 if (driver->supports_autosuspend)
308 pm_runtime_enable(dev);
310 /* If the new driver doesn't allow hub-initiated LPM, and we can't
311 * disable hub-initiated LPM, then fail the probe.
313 * Otherwise, leaving LPM enabled should be harmless, because the
314 * endpoint intervals should remain the same, and the U1/U2 timeouts
315 * should remain the same.
317 * If we need to install alt setting 0 before probe, or another alt
318 * setting during probe, that should also be fine. usb_set_interface()
319 * will attempt to disable LPM, and fail if it can't disable it.
321 lpm_disable_error = usb_unlocked_disable_lpm(udev);
322 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
323 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
324 __func__, driver->name);
325 error = lpm_disable_error;
329 /* Carry out a deferred switch to altsetting 0 */
330 if (intf->needs_altsetting0) {
331 error = usb_set_interface(udev, intf->altsetting[0].
332 desc.bInterfaceNumber, 0);
335 intf->needs_altsetting0 = 0;
338 error = driver->probe(intf, id);
342 intf->condition = USB_INTERFACE_BOUND;
344 /* If the LPM disable succeeded, balance the ref counts. */
345 if (!lpm_disable_error)
346 usb_unlocked_enable_lpm(udev);
348 usb_autosuspend_device(udev);
352 usb_set_intfdata(intf, NULL);
353 intf->needs_remote_wakeup = 0;
354 intf->condition = USB_INTERFACE_UNBOUND;
355 usb_cancel_queued_reset(intf);
357 /* If the LPM disable succeeded, balance the ref counts. */
358 if (!lpm_disable_error)
359 usb_unlocked_enable_lpm(udev);
361 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
362 if (driver->supports_autosuspend)
363 pm_runtime_disable(dev);
364 pm_runtime_set_suspended(dev);
366 usb_autosuspend_device(udev);
370 /* called from driver core with dev locked */
371 static int usb_unbind_interface(struct device *dev)
373 struct usb_driver *driver = to_usb_driver(dev->driver);
374 struct usb_interface *intf = to_usb_interface(dev);
375 struct usb_device *udev;
376 int error, r, lpm_disable_error;
378 intf->condition = USB_INTERFACE_UNBINDING;
380 /* Autoresume for set_interface call below */
381 udev = interface_to_usbdev(intf);
382 error = usb_autoresume_device(udev);
384 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
385 * the driver is unbound. If LPM isn't disabled, that's fine because it
386 * wouldn't be enabled unless all the bound interfaces supported
389 lpm_disable_error = usb_unlocked_disable_lpm(udev);
391 /* Terminate all URBs for this interface unless the driver
392 * supports "soft" unbinding.
394 if (!driver->soft_unbind)
395 usb_disable_interface(udev, intf, false);
397 driver->disconnect(intf);
398 usb_cancel_queued_reset(intf);
400 /* Reset other interface state.
401 * We cannot do a Set-Interface if the device is suspended or
402 * if it is prepared for a system sleep (since installing a new
403 * altsetting means creating new endpoint device entries).
404 * When either of these happens, defer the Set-Interface.
406 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
407 /* Already in altsetting 0 so skip Set-Interface.
408 * Just re-enable it without affecting the endpoint toggles.
410 usb_enable_interface(udev, intf, false);
411 } else if (!error && !intf->dev.power.is_prepared) {
412 r = usb_set_interface(udev, intf->altsetting[0].
413 desc.bInterfaceNumber, 0);
415 intf->needs_altsetting0 = 1;
417 intf->needs_altsetting0 = 1;
419 usb_set_intfdata(intf, NULL);
421 intf->condition = USB_INTERFACE_UNBOUND;
422 intf->needs_remote_wakeup = 0;
424 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
425 if (!lpm_disable_error)
426 usb_unlocked_enable_lpm(udev);
428 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
429 if (driver->supports_autosuspend)
430 pm_runtime_disable(dev);
431 pm_runtime_set_suspended(dev);
433 /* Undo any residual pm_autopm_get_interface_* calls */
434 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
435 usb_autopm_put_interface_no_suspend(intf);
436 atomic_set(&intf->pm_usage_cnt, 0);
439 usb_autosuspend_device(udev);
445 * usb_driver_claim_interface - bind a driver to an interface
446 * @driver: the driver to be bound
447 * @iface: the interface to which it will be bound; must be in the
448 * usb device's active configuration
449 * @priv: driver data associated with that interface
451 * This is used by usb device drivers that need to claim more than one
452 * interface on a device when probing (audio and acm are current examples).
453 * No device driver should directly modify internal usb_interface or
454 * usb_device structure members.
456 * Few drivers should need to use this routine, since the most natural
457 * way to bind to an interface is to return the private data from
458 * the driver's probe() method.
460 * Callers must own the device lock, so driver probe() entries don't need
461 * extra locking, but other call contexts may need to explicitly claim that
464 * Return: 0 on success.
466 int usb_driver_claim_interface(struct usb_driver *driver,
467 struct usb_interface *iface, void *priv)
469 struct device *dev = &iface->dev;
470 struct usb_device *udev;
472 int lpm_disable_error;
477 udev = interface_to_usbdev(iface);
479 dev->driver = &driver->drvwrap.driver;
480 usb_set_intfdata(iface, priv);
481 iface->needs_binding = 0;
483 iface->condition = USB_INTERFACE_BOUND;
485 /* Disable LPM until this driver is bound. */
486 lpm_disable_error = usb_unlocked_disable_lpm(udev);
487 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
488 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
489 __func__, driver->name);
493 /* Claimed interfaces are initially inactive (suspended) and
494 * runtime-PM-enabled, but only if the driver has autosuspend
495 * support. Otherwise they are marked active, to prevent the
496 * device from being autosuspended, but left disabled. In either
497 * case they are sensitive to their children's power states.
499 pm_suspend_ignore_children(dev, false);
500 if (driver->supports_autosuspend)
501 pm_runtime_enable(dev);
503 pm_runtime_set_active(dev);
505 /* if interface was already added, bind now; else let
506 * the future device_add() bind it, bypassing probe()
508 if (device_is_registered(dev))
509 retval = device_bind_driver(dev);
511 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
512 if (!lpm_disable_error)
513 usb_unlocked_enable_lpm(udev);
517 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
520 * usb_driver_release_interface - unbind a driver from an interface
521 * @driver: the driver to be unbound
522 * @iface: the interface from which it will be unbound
524 * This can be used by drivers to release an interface without waiting
525 * for their disconnect() methods to be called. In typical cases this
526 * also causes the driver disconnect() method to be called.
528 * This call is synchronous, and may not be used in an interrupt context.
529 * Callers must own the device lock, so driver disconnect() entries don't
530 * need extra locking, but other call contexts may need to explicitly claim
533 void usb_driver_release_interface(struct usb_driver *driver,
534 struct usb_interface *iface)
536 struct device *dev = &iface->dev;
538 /* this should never happen, don't release something that's not ours */
539 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
542 /* don't release from within disconnect() */
543 if (iface->condition != USB_INTERFACE_BOUND)
545 iface->condition = USB_INTERFACE_UNBINDING;
547 /* Release via the driver core only if the interface
548 * has already been registered
550 if (device_is_registered(dev)) {
551 device_release_driver(dev);
554 usb_unbind_interface(dev);
559 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
561 /* returns 0 if no match, 1 if match */
562 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
564 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
565 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
568 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
569 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
572 /* No need to test id->bcdDevice_lo != 0, since 0 is never
573 greater than any unsigned number. */
574 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
575 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
578 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
579 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
582 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
583 (id->bDeviceClass != dev->descriptor.bDeviceClass))
586 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
587 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
590 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
591 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
597 /* returns 0 if no match, 1 if match */
598 int usb_match_one_id_intf(struct usb_device *dev,
599 struct usb_host_interface *intf,
600 const struct usb_device_id *id)
602 /* The interface class, subclass, protocol and number should never be
603 * checked for a match if the device class is Vendor Specific,
604 * unless the match record specifies the Vendor ID. */
605 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
606 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
607 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
608 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
609 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
610 USB_DEVICE_ID_MATCH_INT_NUMBER)))
613 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
614 (id->bInterfaceClass != intf->desc.bInterfaceClass))
617 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
618 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
621 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
622 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
625 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
626 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
632 /* returns 0 if no match, 1 if match */
633 int usb_match_one_id(struct usb_interface *interface,
634 const struct usb_device_id *id)
636 struct usb_host_interface *intf;
637 struct usb_device *dev;
639 /* proc_connectinfo in devio.c may call us with id == NULL. */
643 intf = interface->cur_altsetting;
644 dev = interface_to_usbdev(interface);
646 if (!usb_match_device(dev, id))
649 return usb_match_one_id_intf(dev, intf, id);
651 EXPORT_SYMBOL_GPL(usb_match_one_id);
654 * usb_match_id - find first usb_device_id matching device or interface
655 * @interface: the interface of interest
656 * @id: array of usb_device_id structures, terminated by zero entry
658 * usb_match_id searches an array of usb_device_id's and returns
659 * the first one matching the device or interface, or null.
660 * This is used when binding (or rebinding) a driver to an interface.
661 * Most USB device drivers will use this indirectly, through the usb core,
662 * but some layered driver frameworks use it directly.
663 * These device tables are exported with MODULE_DEVICE_TABLE, through
664 * modutils, to support the driver loading functionality of USB hotplugging.
666 * Return: The first matching usb_device_id, or %NULL.
670 * The "match_flags" element in a usb_device_id controls which
671 * members are used. If the corresponding bit is set, the
672 * value in the device_id must match its corresponding member
673 * in the device or interface descriptor, or else the device_id
676 * "driver_info" is normally used only by device drivers,
677 * but you can create a wildcard "matches anything" usb_device_id
678 * as a driver's "modules.usbmap" entry if you provide an id with
679 * only a nonzero "driver_info" field. If you do this, the USB device
680 * driver's probe() routine should use additional intelligence to
681 * decide whether to bind to the specified interface.
683 * What Makes Good usb_device_id Tables:
685 * The match algorithm is very simple, so that intelligence in
686 * driver selection must come from smart driver id records.
687 * Unless you have good reasons to use another selection policy,
688 * provide match elements only in related groups, and order match
689 * specifiers from specific to general. Use the macros provided
690 * for that purpose if you can.
692 * The most specific match specifiers use device descriptor
693 * data. These are commonly used with product-specific matches;
694 * the USB_DEVICE macro lets you provide vendor and product IDs,
695 * and you can also match against ranges of product revisions.
696 * These are widely used for devices with application or vendor
697 * specific bDeviceClass values.
699 * Matches based on device class/subclass/protocol specifications
700 * are slightly more general; use the USB_DEVICE_INFO macro, or
701 * its siblings. These are used with single-function devices
702 * where bDeviceClass doesn't specify that each interface has
705 * Matches based on interface class/subclass/protocol are the
706 * most general; they let drivers bind to any interface on a
707 * multiple-function device. Use the USB_INTERFACE_INFO
708 * macro, or its siblings, to match class-per-interface style
709 * devices (as recorded in bInterfaceClass).
711 * Note that an entry created by USB_INTERFACE_INFO won't match
712 * any interface if the device class is set to Vendor-Specific.
713 * This is deliberate; according to the USB spec the meanings of
714 * the interface class/subclass/protocol for these devices are also
715 * vendor-specific, and hence matching against a standard product
716 * class wouldn't work anyway. If you really want to use an
717 * interface-based match for such a device, create a match record
718 * that also specifies the vendor ID. (Unforunately there isn't a
719 * standard macro for creating records like this.)
721 * Within those groups, remember that not all combinations are
722 * meaningful. For example, don't give a product version range
723 * without vendor and product IDs; or specify a protocol without
724 * its associated class and subclass.
726 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
727 const struct usb_device_id *id)
729 /* proc_connectinfo in devio.c may call us with id == NULL. */
733 /* It is important to check that id->driver_info is nonzero,
734 since an entry that is all zeroes except for a nonzero
735 id->driver_info is the way to create an entry that
736 indicates that the driver want to examine every
737 device and interface. */
738 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
739 id->bInterfaceClass || id->driver_info; id++) {
740 if (usb_match_one_id(interface, id))
746 EXPORT_SYMBOL_GPL(usb_match_id);
748 static int usb_device_match(struct device *dev, struct device_driver *drv)
750 /* devices and interfaces are handled separately */
751 if (is_usb_device(dev)) {
753 /* interface drivers never match devices */
754 if (!is_usb_device_driver(drv))
757 /* TODO: Add real matching code */
760 } else if (is_usb_interface(dev)) {
761 struct usb_interface *intf;
762 struct usb_driver *usb_drv;
763 const struct usb_device_id *id;
765 /* device drivers never match interfaces */
766 if (is_usb_device_driver(drv))
769 intf = to_usb_interface(dev);
770 usb_drv = to_usb_driver(drv);
772 id = usb_match_id(intf, usb_drv->id_table);
776 id = usb_match_dynamic_id(intf, usb_drv);
784 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
786 struct usb_device *usb_dev;
788 if (is_usb_device(dev)) {
789 usb_dev = to_usb_device(dev);
790 } else if (is_usb_interface(dev)) {
791 struct usb_interface *intf = to_usb_interface(dev);
793 usb_dev = interface_to_usbdev(intf);
798 if (usb_dev->devnum < 0) {
799 /* driver is often null here; dev_dbg() would oops */
800 pr_debug("usb %s: already deleted?\n", dev_name(dev));
804 pr_debug("usb %s: bus removed?\n", dev_name(dev));
808 /* per-device configurations are common */
809 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
810 le16_to_cpu(usb_dev->descriptor.idVendor),
811 le16_to_cpu(usb_dev->descriptor.idProduct),
812 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
815 /* class-based driver binding models */
816 if (add_uevent_var(env, "TYPE=%d/%d/%d",
817 usb_dev->descriptor.bDeviceClass,
818 usb_dev->descriptor.bDeviceSubClass,
819 usb_dev->descriptor.bDeviceProtocol))
826 * usb_register_device_driver - register a USB device (not interface) driver
827 * @new_udriver: USB operations for the device driver
828 * @owner: module owner of this driver.
830 * Registers a USB device driver with the USB core. The list of
831 * unattached devices will be rescanned whenever a new driver is
832 * added, allowing the new driver to attach to any recognized devices.
834 * Return: A negative error code on failure and 0 on success.
836 int usb_register_device_driver(struct usb_device_driver *new_udriver,
837 struct module *owner)
844 new_udriver->drvwrap.for_devices = 1;
845 new_udriver->drvwrap.driver.name = new_udriver->name;
846 new_udriver->drvwrap.driver.bus = &usb_bus_type;
847 new_udriver->drvwrap.driver.probe = usb_probe_device;
848 new_udriver->drvwrap.driver.remove = usb_unbind_device;
849 new_udriver->drvwrap.driver.owner = owner;
851 retval = driver_register(&new_udriver->drvwrap.driver);
854 pr_info("%s: registered new device driver %s\n",
855 usbcore_name, new_udriver->name);
857 printk(KERN_ERR "%s: error %d registering device "
859 usbcore_name, retval, new_udriver->name);
863 EXPORT_SYMBOL_GPL(usb_register_device_driver);
866 * usb_deregister_device_driver - unregister a USB device (not interface) driver
867 * @udriver: USB operations of the device driver to unregister
868 * Context: must be able to sleep
870 * Unlinks the specified driver from the internal USB driver list.
872 void usb_deregister_device_driver(struct usb_device_driver *udriver)
874 pr_info("%s: deregistering device driver %s\n",
875 usbcore_name, udriver->name);
877 driver_unregister(&udriver->drvwrap.driver);
879 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
882 * usb_register_driver - register a USB interface driver
883 * @new_driver: USB operations for the interface driver
884 * @owner: module owner of this driver.
885 * @mod_name: module name string
887 * Registers a USB interface driver with the USB core. The list of
888 * unattached interfaces will be rescanned whenever a new driver is
889 * added, allowing the new driver to attach to any recognized interfaces.
891 * Return: A negative error code on failure and 0 on success.
893 * NOTE: if you want your driver to use the USB major number, you must call
894 * usb_register_dev() to enable that functionality. This function no longer
895 * takes care of that.
897 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
898 const char *mod_name)
905 new_driver->drvwrap.for_devices = 0;
906 new_driver->drvwrap.driver.name = new_driver->name;
907 new_driver->drvwrap.driver.bus = &usb_bus_type;
908 new_driver->drvwrap.driver.probe = usb_probe_interface;
909 new_driver->drvwrap.driver.remove = usb_unbind_interface;
910 new_driver->drvwrap.driver.owner = owner;
911 new_driver->drvwrap.driver.mod_name = mod_name;
912 spin_lock_init(&new_driver->dynids.lock);
913 INIT_LIST_HEAD(&new_driver->dynids.list);
915 retval = driver_register(&new_driver->drvwrap.driver);
919 retval = usb_create_newid_files(new_driver);
923 pr_info("%s: registered new interface driver %s\n",
924 usbcore_name, new_driver->name);
930 driver_unregister(&new_driver->drvwrap.driver);
932 printk(KERN_ERR "%s: error %d registering interface "
934 usbcore_name, retval, new_driver->name);
937 EXPORT_SYMBOL_GPL(usb_register_driver);
940 * usb_deregister - unregister a USB interface driver
941 * @driver: USB operations of the interface driver to unregister
942 * Context: must be able to sleep
944 * Unlinks the specified driver from the internal USB driver list.
946 * NOTE: If you called usb_register_dev(), you still need to call
947 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
948 * this * call will no longer do it for you.
950 void usb_deregister(struct usb_driver *driver)
952 pr_info("%s: deregistering interface driver %s\n",
953 usbcore_name, driver->name);
955 usb_remove_newid_files(driver);
956 driver_unregister(&driver->drvwrap.driver);
957 usb_free_dynids(driver);
959 EXPORT_SYMBOL_GPL(usb_deregister);
961 /* Forced unbinding of a USB interface driver, either because
962 * it doesn't support pre_reset/post_reset/reset_resume or
963 * because it doesn't support suspend/resume.
965 * The caller must hold @intf's device's lock, but not its pm_mutex
966 * and not @intf->dev.sem.
968 void usb_forced_unbind_intf(struct usb_interface *intf)
970 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
972 dev_dbg(&intf->dev, "forced unbind\n");
973 usb_driver_release_interface(driver, intf);
975 /* Mark the interface for later rebinding */
976 intf->needs_binding = 1;
979 /* Delayed forced unbinding of a USB interface driver and scan
982 * The caller must hold @intf's device's lock, but not its pm_mutex
983 * and not @intf->dev.sem.
985 * Note: Rebinds will be skipped if a system sleep transition is in
986 * progress and the PM "complete" callback hasn't occurred yet.
988 void usb_rebind_intf(struct usb_interface *intf)
992 /* Delayed unbind of an existing driver */
993 if (intf->dev.driver)
994 usb_forced_unbind_intf(intf);
996 /* Try to rebind the interface */
997 if (!intf->dev.power.is_prepared) {
998 intf->needs_binding = 0;
999 rc = device_attach(&intf->dev);
1001 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1007 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
1008 * There is no check for reset_resume here because it can be determined
1009 * only during resume whether reset_resume is needed.
1011 * The caller must hold @udev's device lock.
1013 static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
1015 struct usb_host_config *config;
1017 struct usb_interface *intf;
1018 struct usb_driver *drv;
1020 config = udev->actconfig;
1022 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1023 intf = config->interface[i];
1025 if (intf->dev.driver) {
1026 drv = to_usb_driver(intf->dev.driver);
1027 if (!drv->suspend || !drv->resume)
1028 usb_forced_unbind_intf(intf);
1034 /* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1035 * These interfaces have the needs_binding flag set by usb_resume_interface().
1037 * The caller must hold @udev's device lock.
1039 static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1041 struct usb_host_config *config;
1043 struct usb_interface *intf;
1045 config = udev->actconfig;
1047 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1048 intf = config->interface[i];
1049 if (intf->dev.driver && intf->needs_binding)
1050 usb_forced_unbind_intf(intf);
1055 static void do_rebind_interfaces(struct usb_device *udev)
1057 struct usb_host_config *config;
1059 struct usb_interface *intf;
1061 config = udev->actconfig;
1063 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1064 intf = config->interface[i];
1065 if (intf->needs_binding)
1066 usb_rebind_intf(intf);
1071 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1073 struct usb_device_driver *udriver;
1076 if (udev->state == USB_STATE_NOTATTACHED ||
1077 udev->state == USB_STATE_SUSPENDED)
1080 /* For devices that don't have a driver, we do a generic suspend. */
1081 if (udev->dev.driver)
1082 udriver = to_usb_device_driver(udev->dev.driver);
1084 udev->do_remote_wakeup = 0;
1085 udriver = &usb_generic_driver;
1087 status = udriver->suspend(udev, msg);
1090 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1094 static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1096 struct usb_device_driver *udriver;
1099 if (udev->state == USB_STATE_NOTATTACHED)
1102 /* Can't resume it if it doesn't have a driver. */
1103 if (udev->dev.driver == NULL) {
1108 /* Non-root devices on a full/low-speed bus must wait for their
1109 * companion high-speed root hub, in case a handoff is needed.
1111 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
1112 device_pm_wait_for_dev(&udev->dev,
1113 &udev->bus->hs_companion->root_hub->dev);
1115 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1116 udev->reset_resume = 1;
1118 udriver = to_usb_device_driver(udev->dev.driver);
1119 status = udriver->resume(udev, msg);
1122 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1126 static int usb_suspend_interface(struct usb_device *udev,
1127 struct usb_interface *intf, pm_message_t msg)
1129 struct usb_driver *driver;
1132 if (udev->state == USB_STATE_NOTATTACHED ||
1133 intf->condition == USB_INTERFACE_UNBOUND)
1135 driver = to_usb_driver(intf->dev.driver);
1137 /* at this time we know the driver supports suspend */
1138 status = driver->suspend(intf, msg);
1139 if (status && !PMSG_IS_AUTO(msg))
1140 dev_err(&intf->dev, "suspend error %d\n", status);
1143 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1147 static int usb_resume_interface(struct usb_device *udev,
1148 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1150 struct usb_driver *driver;
1153 if (udev->state == USB_STATE_NOTATTACHED)
1156 /* Don't let autoresume interfere with unbinding */
1157 if (intf->condition == USB_INTERFACE_UNBINDING)
1160 /* Can't resume it if it doesn't have a driver. */
1161 if (intf->condition == USB_INTERFACE_UNBOUND) {
1163 /* Carry out a deferred switch to altsetting 0 */
1164 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
1165 usb_set_interface(udev, intf->altsetting[0].
1166 desc.bInterfaceNumber, 0);
1167 intf->needs_altsetting0 = 0;
1172 /* Don't resume if the interface is marked for rebinding */
1173 if (intf->needs_binding)
1175 driver = to_usb_driver(intf->dev.driver);
1178 if (driver->reset_resume) {
1179 status = driver->reset_resume(intf);
1181 dev_err(&intf->dev, "%s error %d\n",
1182 "reset_resume", status);
1184 intf->needs_binding = 1;
1185 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1189 status = driver->resume(intf);
1191 dev_err(&intf->dev, "resume error %d\n", status);
1195 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1197 /* Later we will unbind the driver and/or reprobe, if necessary */
1202 * usb_suspend_both - suspend a USB device and its interfaces
1203 * @udev: the usb_device to suspend
1204 * @msg: Power Management message describing this state transition
1206 * This is the central routine for suspending USB devices. It calls the
1207 * suspend methods for all the interface drivers in @udev and then calls
1208 * the suspend method for @udev itself. When the routine is called in
1209 * autosuspend, if an error occurs at any stage, all the interfaces
1210 * which were suspended are resumed so that they remain in the same
1211 * state as the device, but when called from system sleep, all error
1212 * from suspend methods of interfaces and the non-root-hub device itself
1213 * are simply ignored, so all suspended interfaces are only resumed
1214 * to the device's state when @udev is root-hub and its suspend method
1217 * Autosuspend requests originating from a child device or an interface
1218 * driver may be made without the protection of @udev's device lock, but
1219 * all other suspend calls will hold the lock. Usbcore will insure that
1220 * method calls do not arrive during bind, unbind, or reset operations.
1221 * However drivers must be prepared to handle suspend calls arriving at
1222 * unpredictable times.
1224 * This routine can run only in process context.
1226 * Return: 0 if the suspend succeeded.
1228 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1232 struct usb_interface *intf;
1234 if (udev->state == USB_STATE_NOTATTACHED ||
1235 udev->state == USB_STATE_SUSPENDED)
1238 /* Suspend all the interfaces and then udev itself */
1239 if (udev->actconfig) {
1240 n = udev->actconfig->desc.bNumInterfaces;
1241 for (i = n - 1; i >= 0; --i) {
1242 intf = udev->actconfig->interface[i];
1243 status = usb_suspend_interface(udev, intf, msg);
1245 /* Ignore errors during system sleep transitions */
1246 if (!PMSG_IS_AUTO(msg))
1253 status = usb_suspend_device(udev, msg);
1256 * Ignore errors from non-root-hub devices during
1257 * system sleep transitions. For the most part,
1258 * these devices should go to low power anyway when
1259 * the entire bus is suspended.
1261 if (udev->parent && !PMSG_IS_AUTO(msg))
1265 /* If the suspend failed, resume interfaces that did get suspended */
1267 if (udev->actconfig) {
1268 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1270 intf = udev->actconfig->interface[i];
1271 usb_resume_interface(udev, intf, msg, 0);
1275 /* If the suspend succeeded then prevent any more URB submissions
1276 * and flush any outstanding URBs.
1279 udev->can_submit = 0;
1280 for (i = 0; i < 16; ++i) {
1281 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1282 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1287 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1292 * usb_resume_both - resume a USB device and its interfaces
1293 * @udev: the usb_device to resume
1294 * @msg: Power Management message describing this state transition
1296 * This is the central routine for resuming USB devices. It calls the
1297 * the resume method for @udev and then calls the resume methods for all
1298 * the interface drivers in @udev.
1300 * Autoresume requests originating from a child device or an interface
1301 * driver may be made without the protection of @udev's device lock, but
1302 * all other resume calls will hold the lock. Usbcore will insure that
1303 * method calls do not arrive during bind, unbind, or reset operations.
1304 * However drivers must be prepared to handle resume calls arriving at
1305 * unpredictable times.
1307 * This routine can run only in process context.
1309 * Return: 0 on success.
1311 static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1315 struct usb_interface *intf;
1317 if (udev->state == USB_STATE_NOTATTACHED) {
1321 udev->can_submit = 1;
1323 /* Resume the device */
1324 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1325 status = usb_resume_device(udev, msg);
1327 /* Resume the interfaces */
1328 if (status == 0 && udev->actconfig) {
1329 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1330 intf = udev->actconfig->interface[i];
1331 usb_resume_interface(udev, intf, msg,
1332 udev->reset_resume);
1335 usb_mark_last_busy(udev);
1338 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1340 udev->reset_resume = 0;
1344 static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1348 /* Remote wakeup is needed only when we actually go to sleep.
1349 * For things like FREEZE and QUIESCE, if the device is already
1350 * autosuspended then its current wakeup setting is okay.
1352 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1353 if (udev->state != USB_STATE_SUSPENDED)
1354 udev->do_remote_wakeup = 0;
1358 /* Enable remote wakeup if it is allowed, even if no interface drivers
1361 w = device_may_wakeup(&udev->dev);
1363 /* If the device is autosuspended with the wrong wakeup setting,
1364 * autoresume now so the setting can be changed.
1366 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1367 pm_runtime_resume(&udev->dev);
1368 udev->do_remote_wakeup = w;
1371 /* The device lock is held by the PM core */
1372 int usb_suspend(struct device *dev, pm_message_t msg)
1374 struct usb_device *udev = to_usb_device(dev);
1376 unbind_no_pm_drivers_interfaces(udev);
1378 /* From now on we are sure all drivers support suspend/resume
1379 * but not necessarily reset_resume()
1380 * so we may still need to unbind and rebind upon resume
1382 choose_wakeup(udev, msg);
1383 return usb_suspend_both(udev, msg);
1386 /* The device lock is held by the PM core */
1387 int usb_resume_complete(struct device *dev)
1389 struct usb_device *udev = to_usb_device(dev);
1391 /* For PM complete calls, all we do is rebind interfaces
1392 * whose needs_binding flag is set
1394 if (udev->state != USB_STATE_NOTATTACHED)
1395 do_rebind_interfaces(udev);
1399 /* The device lock is held by the PM core */
1400 int usb_resume(struct device *dev, pm_message_t msg)
1402 struct usb_device *udev = to_usb_device(dev);
1405 /* For all calls, take the device back to full power and
1406 * tell the PM core in case it was autosuspended previously.
1407 * Unbind the interfaces that will need rebinding later,
1408 * because they fail to support reset_resume.
1409 * (This can't be done in usb_resume_interface()
1410 * above because it doesn't own the right set of locks.)
1412 status = usb_resume_both(udev, msg);
1414 pm_runtime_disable(dev);
1415 pm_runtime_set_active(dev);
1416 pm_runtime_enable(dev);
1417 unbind_no_reset_resume_drivers_interfaces(udev);
1420 /* Avoid PM error messages for devices disconnected while suspended
1421 * as we'll display regular disconnect messages just a bit later.
1423 if (status == -ENODEV || status == -ESHUTDOWN)
1428 #endif /* CONFIG_PM */
1430 #ifdef CONFIG_PM_RUNTIME
1433 * usb_enable_autosuspend - allow a USB device to be autosuspended
1434 * @udev: the USB device which may be autosuspended
1436 * This routine allows @udev to be autosuspended. An autosuspend won't
1437 * take place until the autosuspend_delay has elapsed and all the other
1438 * necessary conditions are satisfied.
1440 * The caller must hold @udev's device lock.
1442 void usb_enable_autosuspend(struct usb_device *udev)
1444 pm_runtime_allow(&udev->dev);
1446 EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1449 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1450 * @udev: the USB device which may not be autosuspended
1452 * This routine prevents @udev from being autosuspended and wakes it up
1453 * if it is already autosuspended.
1455 * The caller must hold @udev's device lock.
1457 void usb_disable_autosuspend(struct usb_device *udev)
1459 pm_runtime_forbid(&udev->dev);
1461 EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1464 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1465 * @udev: the usb_device to autosuspend
1467 * This routine should be called when a core subsystem is finished using
1468 * @udev and wants to allow it to autosuspend. Examples would be when
1469 * @udev's device file in usbfs is closed or after a configuration change.
1471 * @udev's usage counter is decremented; if it drops to 0 and all the
1472 * interfaces are inactive then a delayed autosuspend will be attempted.
1473 * The attempt may fail (see autosuspend_check()).
1475 * The caller must hold @udev's device lock.
1477 * This routine can run only in process context.
1479 void usb_autosuspend_device(struct usb_device *udev)
1483 usb_mark_last_busy(udev);
1484 status = pm_runtime_put_sync_autosuspend(&udev->dev);
1485 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1486 __func__, atomic_read(&udev->dev.power.usage_count),
1491 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1492 * @udev: the usb_device to autoresume
1494 * This routine should be called when a core subsystem wants to use @udev
1495 * and needs to guarantee that it is not suspended. No autosuspend will
1496 * occur until usb_autosuspend_device() is called. (Note that this will
1497 * not prevent suspend events originating in the PM core.) Examples would
1498 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1499 * request is received.
1501 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1502 * However if the autoresume fails then the usage counter is re-decremented.
1504 * The caller must hold @udev's device lock.
1506 * This routine can run only in process context.
1508 * Return: 0 on success. A negative error code otherwise.
1510 int usb_autoresume_device(struct usb_device *udev)
1514 status = pm_runtime_get_sync(&udev->dev);
1516 pm_runtime_put_sync(&udev->dev);
1517 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1518 __func__, atomic_read(&udev->dev.power.usage_count),
1526 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1527 * @intf: the usb_interface whose counter should be decremented
1529 * This routine should be called by an interface driver when it is
1530 * finished using @intf and wants to allow it to autosuspend. A typical
1531 * example would be a character-device driver when its device file is
1534 * The routine decrements @intf's usage counter. When the counter reaches
1535 * 0, a delayed autosuspend request for @intf's device is attempted. The
1536 * attempt may fail (see autosuspend_check()).
1538 * This routine can run only in process context.
1540 void usb_autopm_put_interface(struct usb_interface *intf)
1542 struct usb_device *udev = interface_to_usbdev(intf);
1545 usb_mark_last_busy(udev);
1546 atomic_dec(&intf->pm_usage_cnt);
1547 status = pm_runtime_put_sync(&intf->dev);
1548 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1549 __func__, atomic_read(&intf->dev.power.usage_count),
1552 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1555 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1556 * @intf: the usb_interface whose counter should be decremented
1558 * This routine does much the same thing as usb_autopm_put_interface():
1559 * It decrements @intf's usage counter and schedules a delayed
1560 * autosuspend request if the counter is <= 0. The difference is that it
1561 * does not perform any synchronization; callers should hold a private
1562 * lock and handle all synchronization issues themselves.
1564 * Typically a driver would call this routine during an URB's completion
1565 * handler, if no more URBs were pending.
1567 * This routine can run in atomic context.
1569 void usb_autopm_put_interface_async(struct usb_interface *intf)
1571 struct usb_device *udev = interface_to_usbdev(intf);
1574 usb_mark_last_busy(udev);
1575 atomic_dec(&intf->pm_usage_cnt);
1576 status = pm_runtime_put(&intf->dev);
1577 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1578 __func__, atomic_read(&intf->dev.power.usage_count),
1581 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1584 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1585 * @intf: the usb_interface whose counter should be decremented
1587 * This routine decrements @intf's usage counter but does not carry out an
1590 * This routine can run in atomic context.
1592 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1594 struct usb_device *udev = interface_to_usbdev(intf);
1596 usb_mark_last_busy(udev);
1597 atomic_dec(&intf->pm_usage_cnt);
1598 pm_runtime_put_noidle(&intf->dev);
1600 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1603 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1604 * @intf: the usb_interface whose counter should be incremented
1606 * This routine should be called by an interface driver when it wants to
1607 * use @intf and needs to guarantee that it is not suspended. In addition,
1608 * the routine prevents @intf from being autosuspended subsequently. (Note
1609 * that this will not prevent suspend events originating in the PM core.)
1610 * This prevention will persist until usb_autopm_put_interface() is called
1611 * or @intf is unbound. A typical example would be a character-device
1612 * driver when its device file is opened.
1614 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1615 * However if the autoresume fails then the counter is re-decremented.
1617 * This routine can run only in process context.
1619 * Return: 0 on success.
1621 int usb_autopm_get_interface(struct usb_interface *intf)
1625 status = pm_runtime_get_sync(&intf->dev);
1627 pm_runtime_put_sync(&intf->dev);
1629 atomic_inc(&intf->pm_usage_cnt);
1630 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1631 __func__, atomic_read(&intf->dev.power.usage_count),
1637 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1640 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1641 * @intf: the usb_interface whose counter should be incremented
1643 * This routine does much the same thing as
1644 * usb_autopm_get_interface(): It increments @intf's usage counter and
1645 * queues an autoresume request if the device is suspended. The
1646 * differences are that it does not perform any synchronization (callers
1647 * should hold a private lock and handle all synchronization issues
1648 * themselves), and it does not autoresume the device directly (it only
1649 * queues a request). After a successful call, the device may not yet be
1652 * This routine can run in atomic context.
1654 * Return: 0 on success. A negative error code otherwise.
1656 int usb_autopm_get_interface_async(struct usb_interface *intf)
1660 status = pm_runtime_get(&intf->dev);
1661 if (status < 0 && status != -EINPROGRESS)
1662 pm_runtime_put_noidle(&intf->dev);
1664 atomic_inc(&intf->pm_usage_cnt);
1665 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1666 __func__, atomic_read(&intf->dev.power.usage_count),
1668 if (status > 0 || status == -EINPROGRESS)
1672 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1675 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1676 * @intf: the usb_interface whose counter should be incremented
1678 * This routine increments @intf's usage counter but does not carry out an
1681 * This routine can run in atomic context.
1683 void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1685 struct usb_device *udev = interface_to_usbdev(intf);
1687 usb_mark_last_busy(udev);
1688 atomic_inc(&intf->pm_usage_cnt);
1689 pm_runtime_get_noresume(&intf->dev);
1691 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1693 /* Internal routine to check whether we may autosuspend a device. */
1694 static int autosuspend_check(struct usb_device *udev)
1697 struct usb_interface *intf;
1699 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1700 * any interface drivers require remote wakeup but it isn't available.
1703 if (udev->actconfig) {
1704 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1705 intf = udev->actconfig->interface[i];
1707 /* We don't need to check interfaces that are
1708 * disabled for runtime PM. Either they are unbound
1709 * or else their drivers don't support autosuspend
1710 * and so they are permanently active.
1712 if (intf->dev.power.disable_depth)
1714 if (atomic_read(&intf->dev.power.usage_count) > 0)
1716 w |= intf->needs_remote_wakeup;
1718 /* Don't allow autosuspend if the device will need
1719 * a reset-resume and any of its interface drivers
1720 * doesn't include support or needs remote wakeup.
1722 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1723 struct usb_driver *driver;
1725 driver = to_usb_driver(intf->dev.driver);
1726 if (!driver->reset_resume ||
1727 intf->needs_remote_wakeup)
1732 if (w && !device_can_wakeup(&udev->dev)) {
1733 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1736 udev->do_remote_wakeup = w;
1740 int usb_runtime_suspend(struct device *dev)
1742 struct usb_device *udev = to_usb_device(dev);
1745 /* A USB device can be suspended if it passes the various autosuspend
1746 * checks. Runtime suspend for a USB device means suspending all the
1747 * interfaces and then the device itself.
1749 if (autosuspend_check(udev) != 0)
1752 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1754 /* Allow a retry if autosuspend failed temporarily */
1755 if (status == -EAGAIN || status == -EBUSY)
1756 usb_mark_last_busy(udev);
1758 /* The PM core reacts badly unless the return code is 0,
1759 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1766 int usb_runtime_resume(struct device *dev)
1768 struct usb_device *udev = to_usb_device(dev);
1771 /* Runtime resume for a USB device means resuming both the device
1772 * and all its interfaces.
1774 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1778 int usb_runtime_idle(struct device *dev)
1780 struct usb_device *udev = to_usb_device(dev);
1782 /* An idle USB device can be suspended if it passes the various
1783 * autosuspend checks.
1785 if (autosuspend_check(udev) == 0)
1786 pm_runtime_autosuspend(dev);
1787 /* Tell the core not to suspend it, though. */
1791 int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1793 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1796 if (enable && !udev->usb2_hw_lpm_allowed)
1799 if (hcd->driver->set_usb2_hw_lpm) {
1800 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1802 udev->usb2_hw_lpm_enabled = enable;
1808 #endif /* CONFIG_PM_RUNTIME */
1810 struct bus_type usb_bus_type = {
1812 .match = usb_device_match,
1813 .uevent = usb_uevent,