Lines Matching +full:device +full:- +full:id

1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/usb/core/driver.c - most of the driver model stuff for usb
5 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
9 * (C) Copyright Johannes Erdfelt 1999-2001
14 * (C) Copyright David Brownell 2000-2004
17 * (C) Copyright Greg Kroah-Hartman 2002-2003
28 #include <linux/device.h>
39 * Adds a new dynamic USBdevice ID to this driver,
58 return -EINVAL;
62 return -ENOMEM;
64 INIT_LIST_HEAD(&dynid->node);
65 dynid->id.idVendor = idVendor;
66 dynid->id.idProduct = idProduct;
67 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
70 retval = -EINVAL;
74 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
75 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
79 const struct usb_device_id *id = id_table;
81 if (!id) {
82 retval = -ENODEV;
86 for (; id->match_flags; id++)
87 if (id->idVendor == refVendor && id->idProduct == refProduct)
90 if (id->match_flags) {
91 dynid->id.driver_info = id->driver_info;
93 retval = -ENODEV;
99 list_add_tail(&dynid->node, &dynids->list);
120 list_for_each_entry(dynid, &dynids->list, node)
121 if (dynid->id.bInterfaceClass != 0)
123 dynid->id.idVendor, dynid->id.idProduct,
124 dynid->id.bInterfaceClass);
127 dynid->id.idVendor, dynid->id.idProduct);
136 return usb_show_dynids(&usb_drv->dynids, buf);
144 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
149 * Remove a USB device ID from this driver
162 return -EINVAL;
165 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
166 struct usb_device_id *id = &dynid->id;
168 if ((id->idVendor == idVendor) &&
169 (id->idProduct == idProduct)) {
170 list_del(&dynid->node);
188 if (usb_drv->no_dynamic_id)
191 if (usb_drv->probe != NULL) {
192 error = driver_create_file(&usb_drv->driver,
195 error = driver_create_file(&usb_drv->driver,
198 driver_remove_file(&usb_drv->driver,
208 if (usb_drv->no_dynamic_id)
211 if (usb_drv->probe != NULL) {
212 driver_remove_file(&usb_drv->driver,
214 driver_remove_file(&usb_drv->driver,
224 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
225 list_del(&dynid->node);
236 list_for_each_entry(dynid, &drv->dynids.list, node) {
237 if (usb_match_one_id(intf, &dynid->id)) {
238 return &dynid->id;
246 static int usb_probe_device(struct device *dev)
248 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
256 /* The device should always appear to be in use
259 if (!udriver->supports_autosuspend)
264 if (udriver->generic_subclass)
269 /* Probe the USB device with the driver in hand, but only
271 * device driver has an id_table or a match function; i.e.,
272 * when the device driver was explicitly matched against
273 * a device.
275 * If the device driver does not have either of these,
276 * then we assume that it can bind to any device and is
277 * not truly a more specialized/non-generic driver, so a
278 * return value of -ENODEV should not force the device
280 * can still be another, more specialized, device driver.
285 * specialized USB device drivers for a particular device?
287 * specialised device drivers prior to setting the
290 if (udriver->probe)
291 error = udriver->probe(udev);
292 else if (!udriver->generic_subclass)
293 error = -EINVAL;
294 if (error == -ENODEV && udriver != &usb_generic_driver &&
295 (udriver->id_table || udriver->match)) {
296 udev->use_generic_driver = 1;
297 return -EPROBE_DEFER;
303 static int usb_unbind_device(struct device *dev)
306 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
308 if (udriver->disconnect)
309 udriver->disconnect(udev);
310 if (udriver->generic_subclass)
312 if (!udriver->supports_autosuspend)
318 static int usb_probe_interface(struct device *dev)
320 struct usb_driver *driver = to_usb_driver(dev->driver);
323 const struct usb_device_id *id;
324 int error = -ENODEV;
325 int lpm_disable_error = -ENODEV;
329 intf->needs_binding = 0;
334 if (udev->authorized == 0) {
335 dev_info(&intf->dev, "Device is not authorized for usage\n");
337 } else if (intf->authorized == 0) {
338 dev_info(&intf->dev, "Interface %d is not authorized for usage\n",
339 intf->altsetting->desc.bInterfaceNumber);
343 id = usb_match_dynamic_id(intf, driver);
344 if (!id)
345 id = usb_match_id(intf, driver->id_table);
346 if (!id)
349 dev_dbg(dev, "%s - got id\n", __func__);
355 intf->condition = USB_INTERFACE_BINDING;
358 * runtime-PM-enabled only if the driver has autosuspend support.
363 if (driver->supports_autosuspend)
366 /* If the new driver doesn't allow hub-initiated LPM, and we can't
367 * disable hub-initiated LPM, then fail the probe.
377 if (driver->disable_hub_initiated_lpm) {
380 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n",
381 __func__, driver->name);
388 if (intf->needs_altsetting0) {
389 error = usb_set_interface(udev, intf->altsetting[0].
393 intf->needs_altsetting0 = 0;
396 error = driver->probe(intf, id);
400 intf->condition = USB_INTERFACE_BOUND;
411 intf->needs_remote_wakeup = 0;
412 intf->condition = USB_INTERFACE_UNBOUND;
418 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
419 if (driver->supports_autosuspend)
428 static int usb_unbind_interface(struct device *dev)
430 struct usb_driver *driver = to_usb_driver(dev->driver);
435 int lpm_disable_error = -ENODEV;
437 intf->condition = USB_INTERFACE_UNBINDING;
443 /* If hub-initiated LPM policy may change, attempt to disable LPM until
446 * hub-initiated LPM.
448 if (driver->disable_hub_initiated_lpm)
453 * supports "soft" unbinding and the device is still present.
455 if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
458 driver->disconnect(intf);
461 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
462 ep = &intf->cur_altsetting->endpoint[i];
463 if (ep->streams == 0)
479 * We cannot do a Set-Interface if the device is suspended or
481 * altsetting means creating new endpoint device entries).
482 * When either of these happens, defer the Set-Interface.
484 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
485 /* Already in altsetting 0 so skip Set-Interface.
486 * Just re-enable it without affecting the endpoint toggles.
489 } else if (!error && !intf->dev.power.is_prepared) {
490 r = usb_set_interface(udev, intf->altsetting[0].
493 intf->needs_altsetting0 = 1;
495 intf->needs_altsetting0 = 1;
499 intf->condition = USB_INTERFACE_UNBOUND;
500 intf->needs_remote_wakeup = 0;
502 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
506 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
507 if (driver->supports_autosuspend)
517 static void usb_shutdown_interface(struct device *dev)
522 if (!dev->driver)
525 driver = to_usb_driver(dev->driver);
526 if (driver->shutdown)
527 driver->shutdown(intf);
531 * usb_driver_claim_interface - bind a driver to an interface
534 * usb device's active configuration
537 * This is used by usb device drivers that need to claim more than one
538 * interface on a device when probing (audio and acm are current examples).
539 * No device driver should directly modify internal usb_interface or
542 * Callers must own the device lock, so driver probe() entries don't need
551 struct device *dev;
555 return -ENODEV;
557 dev = &iface->dev;
558 if (dev->driver)
559 return -EBUSY;
562 if (!iface->authorized)
563 return -ENODEV;
565 dev->driver = &driver->driver;
567 iface->needs_binding = 0;
569 iface->condition = USB_INTERFACE_BOUND;
572 * runtime-PM-enabled, but only if the driver has autosuspend
574 * device from being autosuspended, but left disabled. In either
578 if (driver->supports_autosuspend)
590 dev->driver = NULL;
592 iface->needs_remote_wakeup = 0;
593 iface->condition = USB_INTERFACE_UNBOUND;
596 * Unbound interfaces are always runtime-PM-disabled
597 * and runtime-PM-suspended
599 if (driver->supports_autosuspend)
609 * usb_driver_release_interface - unbind a driver from an interface
618 * Callers must own the device lock, so driver disconnect() entries don't
625 struct device *dev = &iface->dev;
628 if (!dev->driver || dev->driver != &driver->driver)
632 if (iface->condition != USB_INTERFACE_BOUND)
634 iface->condition = USB_INTERFACE_UNBINDING;
644 dev->driver = NULL;
651 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
653 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
654 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
657 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
658 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
661 /* No need to test id->bcdDevice_lo != 0, since 0 is never
663 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
664 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
667 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
668 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
671 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
672 (id->bDeviceClass != dev->descriptor.bDeviceClass))
675 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
676 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
679 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
680 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
689 const struct usb_device_id *id)
692 * checked for a match if the device class is Vendor Specific,
693 * unless the match record specifies the Vendor ID. */
694 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
695 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
696 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
702 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
703 (id->bInterfaceClass != intf->desc.bInterfaceClass))
706 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
707 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
710 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
711 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
714 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
715 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
723 const struct usb_device_id *id)
728 /* proc_connectinfo in devio.c may call us with id == NULL. */
729 if (id == NULL)
732 intf = interface->cur_altsetting;
735 if (!usb_match_device(dev, id))
738 return usb_match_one_id_intf(dev, intf, id);
743 * usb_match_id - find first usb_device_id matching device or interface
745 * @id: array of usb_device_id structures, terminated by zero entry
748 * the first one matching the device or interface, or null.
750 * Most USB device drivers will use this indirectly, through the usb core,
752 * These device tables are exported with MODULE_DEVICE_TABLE, through
762 * in the device or interface descriptor, or else the device_id
765 * "driver_info" is normally used only by device drivers,
767 * as a driver's "modules.usbmap" entry if you provide an id with
768 * only a nonzero "driver_info" field. If you do this, the USB device
775 * driver selection must come from smart driver id records.
781 * The most specific match specifiers use device descriptor
782 * data. These are commonly used with product-specific matches;
788 * Matches based on device class/subclass/protocol specifications
790 * its siblings. These are used with single-function devices
796 * multiple-function device. Use the USB_INTERFACE_INFO
797 * macro, or its siblings, to match class-per-interface style
801 * any interface if the device class is set to Vendor-Specific.
804 * vendor-specific, and hence matching against a standard product
806 * interface-based match for such a device, create a match record
807 * that also specifies the vendor ID. (Unforunately there isn't a
816 const struct usb_device_id *id)
818 /* proc_connectinfo in devio.c may call us with id == NULL. */
819 if (id == NULL)
822 /* It is important to check that id->driver_info is nonzero,
824 id->driver_info is the way to create an entry that
826 device and interface. */
827 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
828 id->bInterfaceClass || id->driver_info; id++) {
829 if (usb_match_one_id(interface, id))
830 return id;
838 const struct usb_device_id *id)
840 if (!id)
843 for (; id->idVendor || id->idProduct ; id++) {
844 if (usb_match_device(udev, id))
845 return id;
855 if (udrv->id_table && udrv->match)
856 return usb_device_match_id(udev, udrv->id_table) != NULL &&
857 udrv->match(udev);
859 if (udrv->id_table)
860 return usb_device_match_id(udev, udrv->id_table) != NULL;
862 if (udrv->match)
863 return udrv->match(udev);
868 static int usb_device_match(struct device *dev, const struct device_driver *drv)
882 /* If the device driver under consideration does not have a
886 if (!udrv->id_table && !udrv->match)
894 const struct usb_device_id *id;
896 /* device drivers never match interfaces */
903 id = usb_match_id(intf, usb_drv->id_table);
904 if (id)
907 id = usb_match_dynamic_id(intf, usb_drv);
908 if (id)
915 static int usb_uevent(const struct device *dev, struct kobj_uevent_env *env)
929 if (usb_dev->devnum < 0) {
932 return -ENODEV;
934 if (!usb_dev->bus) {
936 return -ENODEV;
939 /* per-device configurations are common */
941 le16_to_cpu(usb_dev->descriptor.idVendor),
942 le16_to_cpu(usb_dev->descriptor.idProduct),
943 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
944 return -ENOMEM;
946 /* class-based driver binding models */
948 usb_dev->descriptor.bDeviceClass,
949 usb_dev->descriptor.bDeviceSubClass,
950 usb_dev->descriptor.bDeviceProtocol))
951 return -ENOMEM;
956 static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
963 if (dev->driver != &usb_generic_driver.driver)
971 if (ret && ret != -EPROBE_DEFER)
972 dev_err(dev, "Failed to reprobe device (error %d)\n", ret);
979 return drv->probe == usb_probe_device;
983 * usb_register_device_driver - register a USB device (not interface) driver
984 * @new_udriver: USB operations for the device driver
987 * Registers a USB device driver with the USB core. The list of
999 return -ENODEV;
1001 new_udriver->driver.name = new_udriver->name;
1002 new_udriver->driver.bus = &usb_bus_type;
1003 new_udriver->driver.probe = usb_probe_device;
1004 new_udriver->driver.remove = usb_unbind_device;
1005 new_udriver->driver.owner = owner;
1006 new_udriver->driver.dev_groups = new_udriver->dev_groups;
1008 retval = driver_register(&new_udriver->driver);
1011 pr_info("%s: registered new device driver %s\n",
1012 usbcore_name, new_udriver->name);
1014 * Check whether any device could be better served with
1020 pr_err("%s: error %d registering device driver %s\n",
1021 usbcore_name, retval, new_udriver->name);
1029 * usb_deregister_device_driver - unregister a USB device (not interface) driver
1030 * @udriver: USB operations of the device driver to unregister
1037 pr_info("%s: deregistering device driver %s\n",
1038 usbcore_name, udriver->name);
1040 driver_unregister(&udriver->driver);
1045 * usb_register_driver - register a USB interface driver
1066 return -ENODEV;
1068 new_driver->driver.name = new_driver->name;
1069 new_driver->driver.bus = &usb_bus_type;
1070 new_driver->driver.probe = usb_probe_interface;
1071 new_driver->driver.remove = usb_unbind_interface;
1072 new_driver->driver.shutdown = usb_shutdown_interface;
1073 new_driver->driver.owner = owner;
1074 new_driver->driver.mod_name = mod_name;
1075 new_driver->driver.dev_groups = new_driver->dev_groups;
1076 INIT_LIST_HEAD(&new_driver->dynids.list);
1078 retval = driver_register(&new_driver->driver);
1087 usbcore_name, new_driver->name);
1092 driver_unregister(&new_driver->driver);
1095 usbcore_name, retval, new_driver->name);
1101 * usb_deregister - unregister a USB interface driver
1114 usbcore_name, driver->name);
1117 driver_unregister(&driver->driver);
1126 * The caller must hold @intf's device's lock, but not @intf's lock.
1130 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1132 dev_dbg(&intf->dev, "forced unbind\n");
1136 intf->needs_binding = 1;
1143 * The caller must hold @udev's device lock.
1151 config = udev->actconfig;
1153 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1154 intf = config->interface[i];
1155 if (intf->dev.driver && intf->needs_binding)
1164 * The caller must hold @intf's device's lock, but not @intf's lock.
1174 if (intf->dev.driver)
1178 if (!intf->dev.power.is_prepared) {
1179 intf->needs_binding = 0;
1180 rc = device_attach(&intf->dev);
1181 if (rc < 0 && rc != -EPROBE_DEFER)
1182 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1190 * The caller must hold @udev's device lock.
1198 config = udev->actconfig;
1200 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1201 intf = config->interface[i];
1202 if (intf->needs_binding)
1213 * The caller must hold @udev's device lock.
1227 * The caller must hold @udev's device lock.
1236 config = udev->actconfig;
1238 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1239 intf = config->interface[i];
1241 if (intf->dev.driver) {
1242 drv = to_usb_driver(intf->dev.driver);
1243 if (!drv->suspend || !drv->resume)
1255 if (udev->state == USB_STATE_NOTATTACHED ||
1256 udev->state == USB_STATE_SUSPENDED)
1260 if (udev->dev.driver)
1261 udriver = to_usb_device_driver(udev->dev.driver);
1263 udev->do_remote_wakeup = 0;
1266 if (udriver->suspend)
1267 status = udriver->suspend(udev, msg);
1268 if (status == 0 && udriver->generic_subclass)
1272 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1281 if (udev->state == USB_STATE_NOTATTACHED)
1285 if (udev->dev.driver == NULL) {
1286 status = -ENOTCONN;
1290 /* Non-root devices on a full/low-speed bus must wait for their
1291 * companion high-speed root hub, in case a handoff is needed.
1293 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
1294 device_pm_wait_for_dev(&udev->dev,
1295 &udev->bus->hs_companion->root_hub->dev);
1297 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1298 udev->reset_resume = 1;
1300 udriver = to_usb_device_driver(udev->dev.driver);
1301 if (udriver->generic_subclass)
1303 if (status == 0 && udriver->resume)
1304 status = udriver->resume(udev, msg);
1307 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1317 if (udev->state == USB_STATE_NOTATTACHED ||
1318 intf->condition == USB_INTERFACE_UNBOUND)
1320 driver = to_usb_driver(intf->dev.driver);
1323 status = driver->suspend(intf, msg);
1325 dev_err(&intf->dev, "suspend error %d\n", status);
1328 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1338 if (udev->state == USB_STATE_NOTATTACHED)
1342 if (intf->condition == USB_INTERFACE_UNBINDING)
1346 if (intf->condition == USB_INTERFACE_UNBOUND) {
1349 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
1350 usb_set_interface(udev, intf->altsetting[0].
1352 intf->needs_altsetting0 = 0;
1358 if (intf->needs_binding)
1360 driver = to_usb_driver(intf->dev.driver);
1363 if (driver->reset_resume) {
1364 status = driver->reset_resume(intf);
1366 dev_err(&intf->dev, "%s error %d\n",
1369 intf->needs_binding = 1;
1370 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1371 driver->name);
1374 status = driver->resume(intf);
1376 dev_err(&intf->dev, "resume error %d\n", status);
1380 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1387 * usb_suspend_both - suspend a USB device and its interfaces
1396 * state as the device, but when called from system sleep, all error
1397 * from suspend methods of interfaces and the non-root-hub device itself
1399 * to the device's state when @udev is root-hub and its suspend method
1402 * Autosuspend requests originating from a child device or an interface
1403 * driver may be made without the protection of @udev's device lock, but
1419 if (udev->state == USB_STATE_NOTATTACHED ||
1420 udev->state == USB_STATE_SUSPENDED)
1424 dev_dbg(&udev->dev, "device offloaded, skip suspend.\n");
1425 udev->offload_at_suspend = 1;
1429 if (udev->actconfig) {
1430 n = udev->actconfig->desc.bNumInterfaces;
1431 for (i = n - 1; i >= 0; --i) {
1432 intf = udev->actconfig->interface[i];
1439 if (udev->offload_at_suspend &&
1440 intf->needs_remote_wakeup) {
1441 dev_dbg(&intf->dev,
1442 "device offloaded, skip suspend.\n");
1455 if (!udev->offload_at_suspend)
1459 * Ignore errors from non-root-hub devices during
1464 if (udev->parent && !PMSG_IS_AUTO(msg))
1468 * If the device is inaccessible, don't try to resume
1471 if (status && status != -EBUSY) {
1478 dev_err(&udev->dev,
1479 "Failed to suspend device, error %d\n",
1488 if (udev->actconfig) {
1491 intf = udev->actconfig->interface[i];
1500 udev->can_submit = 0;
1501 if (!udev->offload_at_suspend) {
1503 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1504 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1510 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1515 * usb_resume_both - resume a USB device and its interfaces
1523 * Autoresume requests originating from a child device or an interface
1524 * driver may be made without the protection of @udev's device lock, but
1540 if (udev->state == USB_STATE_NOTATTACHED) {
1541 status = -ENODEV;
1544 udev->can_submit = 1;
1546 /* Resume the device */
1547 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) {
1548 if (!udev->offload_at_suspend)
1551 dev_dbg(&udev->dev,
1552 "device offloaded, skip resume.\n");
1556 if (status == 0 && udev->actconfig) {
1557 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1558 intf = udev->actconfig->interface[i];
1565 if (udev->offload_at_suspend &&
1566 intf->needs_remote_wakeup) {
1567 dev_dbg(&intf->dev,
1568 "device offloaded, skip resume.\n");
1572 udev->reset_resume);
1575 udev->offload_at_suspend = 0;
1579 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1581 udev->reset_resume = 0;
1601 w = device_may_wakeup(&udev->dev);
1605 * If the device is autosuspended with the wrong wakeup setting,
1608 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1609 pm_runtime_resume(&udev->dev);
1610 udev->do_remote_wakeup = w;
1613 /* The device lock is held by the PM core */
1614 int usb_suspend(struct device *dev, pm_message_t msg)
1630 if (udev->quirks & USB_QUIRK_DISCONNECT_SUSPEND)
1636 /* The device lock is held by the PM core */
1637 int usb_resume_complete(struct device *dev)
1644 if (udev->state != USB_STATE_NOTATTACHED)
1649 /* The device lock is held by the PM core */
1650 int usb_resume(struct device *dev, pm_message_t msg)
1655 /* For all calls, take the device back to full power and
1673 if (status == -ENODEV || status == -ESHUTDOWN)
1679 * usb_enable_autosuspend - allow a USB device to be autosuspended
1680 * @udev: the USB device which may be autosuspended
1686 * The caller must hold @udev's device lock.
1690 pm_runtime_allow(&udev->dev);
1695 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1696 * @udev: the USB device which may not be autosuspended
1701 * The caller must hold @udev's device lock.
1705 pm_runtime_forbid(&udev->dev);
1710 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1715 * @udev's device file in usbfs is closed or after a configuration change.
1721 * The caller must hold @udev's device lock.
1730 status = pm_runtime_put_sync_autosuspend(&udev->dev);
1731 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1732 __func__, atomic_read(&udev->dev.power.usage_count),
1737 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1744 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1748 * However if the autoresume fails then the usage counter is re-decremented.
1750 * The caller must hold @udev's device lock.
1760 status = pm_runtime_resume_and_get(&udev->dev);
1761 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1762 __func__, atomic_read(&udev->dev.power.usage_count),
1768 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1773 * example would be a character-device driver when its device file is
1777 * 0, a delayed autosuspend request for @intf's device is attempted. The
1788 status = pm_runtime_put_sync(&intf->dev);
1789 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1790 __func__, atomic_read(&intf->dev.power.usage_count),
1796 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1816 status = pm_runtime_put(&intf->dev);
1817 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1818 __func__, atomic_read(&intf->dev.power.usage_count),
1824 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1837 pm_runtime_put_noidle(&intf->dev);
1842 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1850 * or @intf is unbound. A typical example would be a character-device
1851 * driver when its device file is opened.
1854 * However if the autoresume fails then the counter is re-decremented.
1864 status = pm_runtime_resume_and_get(&intf->dev);
1865 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1866 __func__, atomic_read(&intf->dev.power.usage_count),
1873 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1878 * queues an autoresume request if the device is suspended. The
1881 * themselves), and it does not autoresume the device directly (it only
1882 * queues a request). After a successful call, the device may not yet be
1893 status = pm_runtime_get(&intf->dev);
1894 if (status < 0 && status != -EINPROGRESS)
1895 pm_runtime_put_noidle(&intf->dev);
1896 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1897 __func__, atomic_read(&intf->dev.power.usage_count),
1899 if (status > 0 || status == -EINPROGRESS)
1906 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1919 pm_runtime_get_noresume(&intf->dev);
1923 /* Internal routine to check whether we may autosuspend a device. */
1929 if (udev->state == USB_STATE_NOTATTACHED)
1930 return -ENODEV;
1936 if (udev->actconfig) {
1937 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1938 intf = udev->actconfig->interface[i];
1945 if (intf->dev.power.disable_depth)
1947 if (atomic_read(&intf->dev.power.usage_count) > 0)
1948 return -EBUSY;
1949 w |= intf->needs_remote_wakeup;
1951 /* Don't allow autosuspend if the device will need
1952 * a reset-resume and any of its interface drivers
1955 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1958 driver = to_usb_driver(intf->dev.driver);
1959 if (!driver->reset_resume ||
1960 intf->needs_remote_wakeup)
1961 return -EOPNOTSUPP;
1965 if (w && !device_can_wakeup(&udev->dev)) {
1966 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1967 return -EOPNOTSUPP;
1971 * If the device is a direct child of the root hub and the HCD
1975 if (w && udev->parent == udev->bus->root_hub &&
1976 bus_to_hcd(udev->bus)->cant_recv_wakeups) {
1977 dev_dbg(&udev->dev, "HCD doesn't handle wakeup requests\n");
1978 return -EOPNOTSUPP;
1981 udev->do_remote_wakeup = w;
1985 int usb_runtime_suspend(struct device *dev)
1990 /* A USB device can be suspended if it passes the various autosuspend
1991 * checks. Runtime suspend for a USB device means suspending all the
1992 * interfaces and then the device itself.
1995 return -EAGAIN;
2000 if (status == -EAGAIN || status == -EBUSY)
2005 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
2009 if (status != 0 && udev->parent)
2010 return -EBUSY;
2014 int usb_runtime_resume(struct device *dev)
2019 /* Runtime resume for a USB device means resuming both the device
2026 int usb_runtime_idle(struct device *dev)
2030 /* An idle USB device can be suspended if it passes the various
2036 return -EBUSY;
2041 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2042 int ret = -EPERM;
2044 if (hcd->driver->set_usb2_hw_lpm) {
2045 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
2047 udev->usb2_hw_lpm_enabled = enable;
2055 if (!udev->usb2_hw_lpm_capable ||
2056 !udev->usb2_hw_lpm_allowed ||
2057 udev->usb2_hw_lpm_enabled)
2065 if (!udev->usb2_hw_lpm_enabled)