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; in usb_store_new_id()
62 return -ENOMEM; in usb_store_new_id()
64 INIT_LIST_HEAD(&dynid->node); in usb_store_new_id()
65 dynid->id.idVendor = idVendor; in usb_store_new_id()
66 dynid->id.idProduct = idProduct; in usb_store_new_id()
67 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; in usb_store_new_id()
70 retval = -EINVAL; in usb_store_new_id()
74 dynid->id.bInterfaceClass = (u8)bInterfaceClass; in usb_store_new_id()
75 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS; in usb_store_new_id()
79 const struct usb_device_id *id = id_table; in usb_store_new_id() local
81 if (!id) { in usb_store_new_id()
82 retval = -ENODEV; in usb_store_new_id()
86 for (; id->match_flags; id++) in usb_store_new_id()
87 if (id->idVendor == refVendor && id->idProduct == refProduct) in usb_store_new_id()
90 if (id->match_flags) { in usb_store_new_id()
91 dynid->id.driver_info = id->driver_info; in usb_store_new_id()
93 retval = -ENODEV; in usb_store_new_id()
98 spin_lock(&dynids->lock); in usb_store_new_id()
99 list_add_tail(&dynid->node, &dynids->list); in usb_store_new_id()
100 spin_unlock(&dynids->lock); in usb_store_new_id()
119 list_for_each_entry(dynid, &dynids->list, node) in usb_show_dynids()
120 if (dynid->id.bInterfaceClass != 0) in usb_show_dynids()
121 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n", in usb_show_dynids()
122 dynid->id.idVendor, dynid->id.idProduct, in usb_show_dynids()
123 dynid->id.bInterfaceClass); in usb_show_dynids()
125 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n", in usb_show_dynids()
126 dynid->id.idVendor, dynid->id.idProduct); in usb_show_dynids()
135 return usb_show_dynids(&usb_drv->dynids, buf); in new_id_show()
143 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count); in new_id_store()
148 * Remove a USB device ID from this driver
161 return -EINVAL; in remove_id_store()
163 spin_lock(&usb_driver->dynids.lock); in remove_id_store()
164 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) { in remove_id_store()
165 struct usb_device_id *id = &dynid->id; in remove_id_store() local
167 if ((id->idVendor == idVendor) && in remove_id_store()
168 (id->idProduct == idProduct)) { in remove_id_store()
169 list_del(&dynid->node); in remove_id_store()
174 spin_unlock(&usb_driver->dynids.lock); in remove_id_store()
188 if (usb_drv->no_dynamic_id) in usb_create_newid_files()
191 if (usb_drv->probe != NULL) { in usb_create_newid_files()
192 error = driver_create_file(&usb_drv->drvwrap.driver, in usb_create_newid_files()
195 error = driver_create_file(&usb_drv->drvwrap.driver, in usb_create_newid_files()
198 driver_remove_file(&usb_drv->drvwrap.driver, in usb_create_newid_files()
208 if (usb_drv->no_dynamic_id) in usb_remove_newid_files()
211 if (usb_drv->probe != NULL) { in usb_remove_newid_files()
212 driver_remove_file(&usb_drv->drvwrap.driver, in usb_remove_newid_files()
214 driver_remove_file(&usb_drv->drvwrap.driver, in usb_remove_newid_files()
223 spin_lock(&usb_drv->dynids.lock); in usb_free_dynids()
224 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { in usb_free_dynids()
225 list_del(&dynid->node); in usb_free_dynids()
228 spin_unlock(&usb_drv->dynids.lock); in usb_free_dynids()
236 spin_lock(&drv->dynids.lock); in usb_match_dynamic_id()
237 list_for_each_entry(dynid, &drv->dynids.list, node) { in usb_match_dynamic_id()
238 if (usb_match_one_id(intf, &dynid->id)) { in usb_match_dynamic_id()
239 spin_unlock(&drv->dynids.lock); in usb_match_dynamic_id()
240 return &dynid->id; in usb_match_dynamic_id()
243 spin_unlock(&drv->dynids.lock); in usb_match_dynamic_id()
249 static int usb_probe_device(struct device *dev) in usb_probe_device()
251 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); in usb_probe_device()
259 /* The device should always appear to be in use in usb_probe_device()
262 if (!udriver->supports_autosuspend) in usb_probe_device()
267 if (udriver->generic_subclass) in usb_probe_device()
272 /* Probe the USB device with the driver in hand, but only in usb_probe_device()
274 * device driver has an id_table or a match function; i.e., in usb_probe_device()
275 * when the device driver was explicitly matched against in usb_probe_device()
276 * a device. in usb_probe_device()
278 * If the device driver does not have either of these, in usb_probe_device()
279 * then we assume that it can bind to any device and is in usb_probe_device()
280 * not truly a more specialized/non-generic driver, so a in usb_probe_device()
281 * return value of -ENODEV should not force the device in usb_probe_device()
283 * can still be another, more specialized, device driver. in usb_probe_device()
288 * specialized USB device drivers for a particular device? in usb_probe_device()
290 * specialised device drivers prior to setting the in usb_probe_device()
293 error = udriver->probe(udev); in usb_probe_device()
294 if (error == -ENODEV && udriver != &usb_generic_driver && in usb_probe_device()
295 (udriver->id_table || udriver->match)) { in usb_probe_device()
296 udev->use_generic_driver = 1; in usb_probe_device()
297 return -EPROBE_DEFER; in usb_probe_device()
303 static int usb_unbind_device(struct device *dev) in usb_unbind_device()
306 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); in usb_unbind_device()
308 if (udriver->disconnect) in usb_unbind_device()
309 udriver->disconnect(udev); in usb_unbind_device()
310 if (udriver->generic_subclass) in usb_unbind_device()
312 if (!udriver->supports_autosuspend) in usb_unbind_device()
318 static int usb_probe_interface(struct device *dev) in usb_probe_interface()
320 struct usb_driver *driver = to_usb_driver(dev->driver); in usb_probe_interface()
323 const struct usb_device_id *id; in usb_probe_interface() local
324 int error = -ENODEV; in usb_probe_interface()
325 int lpm_disable_error = -ENODEV; in usb_probe_interface()
329 intf->needs_binding = 0; in usb_probe_interface()
334 if (udev->authorized == 0) { in usb_probe_interface()
335 dev_err(&intf->dev, "Device is not authorized for usage\n"); in usb_probe_interface()
337 } else if (intf->authorized == 0) { in usb_probe_interface()
338 dev_err(&intf->dev, "Interface %d is not authorized for usage\n", in usb_probe_interface()
339 intf->altsetting->desc.bInterfaceNumber); in usb_probe_interface()
343 id = usb_match_dynamic_id(intf, driver); in usb_probe_interface()
344 if (!id) in usb_probe_interface()
345 id = usb_match_id(intf, driver->id_table); in usb_probe_interface()
346 if (!id) in usb_probe_interface()
349 dev_dbg(dev, "%s - got id\n", __func__); in usb_probe_interface()
355 intf->condition = USB_INTERFACE_BINDING; in usb_probe_interface()
358 * runtime-PM-enabled only if the driver has autosuspend support. in usb_probe_interface()
363 if (driver->supports_autosuspend) in usb_probe_interface()
366 /* If the new driver doesn't allow hub-initiated LPM, and we can't in usb_probe_interface()
367 * disable hub-initiated LPM, then fail the probe. in usb_probe_interface()
377 if (driver->disable_hub_initiated_lpm) { in usb_probe_interface()
380 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n", in usb_probe_interface()
381 __func__, driver->name); in usb_probe_interface()
388 if (intf->needs_altsetting0) { in usb_probe_interface()
389 error = usb_set_interface(udev, intf->altsetting[0]. in usb_probe_interface()
393 intf->needs_altsetting0 = 0; in usb_probe_interface()
396 error = driver->probe(intf, id); in usb_probe_interface()
400 intf->condition = USB_INTERFACE_BOUND; in usb_probe_interface()
411 intf->needs_remote_wakeup = 0; in usb_probe_interface()
412 intf->condition = USB_INTERFACE_UNBOUND; in usb_probe_interface()
418 /* Unbound interfaces are always runtime-PM-disabled and -suspended */ in usb_probe_interface()
419 if (driver->supports_autosuspend) in usb_probe_interface()
428 static int usb_unbind_interface(struct device *dev) in usb_unbind_interface()
430 struct usb_driver *driver = to_usb_driver(dev->driver); in usb_unbind_interface()
435 int lpm_disable_error = -ENODEV; in usb_unbind_interface()
437 intf->condition = USB_INTERFACE_UNBINDING; in usb_unbind_interface()
443 /* If hub-initiated LPM policy may change, attempt to disable LPM until in usb_unbind_interface()
446 * hub-initiated LPM. in usb_unbind_interface()
448 if (driver->disable_hub_initiated_lpm) in usb_unbind_interface()
453 * supports "soft" unbinding and the device is still present. in usb_unbind_interface()
455 if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED) in usb_unbind_interface()
458 driver->disconnect(intf); in usb_unbind_interface()
461 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { in usb_unbind_interface()
462 ep = &intf->cur_altsetting->endpoint[i]; in usb_unbind_interface()
463 if (ep->streams == 0) in usb_unbind_interface()
479 * We cannot do a Set-Interface if the device is suspended or in usb_unbind_interface()
481 * altsetting means creating new endpoint device entries). in usb_unbind_interface()
482 * When either of these happens, defer the Set-Interface. in usb_unbind_interface()
484 if (intf->cur_altsetting->desc.bAlternateSetting == 0) { in usb_unbind_interface()
485 /* Already in altsetting 0 so skip Set-Interface. in usb_unbind_interface()
486 * Just re-enable it without affecting the endpoint toggles. in usb_unbind_interface()
489 } else if (!error && !intf->dev.power.is_prepared) { in usb_unbind_interface()
490 r = usb_set_interface(udev, intf->altsetting[0]. in usb_unbind_interface()
493 intf->needs_altsetting0 = 1; in usb_unbind_interface()
495 intf->needs_altsetting0 = 1; in usb_unbind_interface()
499 intf->condition = USB_INTERFACE_UNBOUND; in usb_unbind_interface()
500 intf->needs_remote_wakeup = 0; in usb_unbind_interface()
502 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */ in usb_unbind_interface()
506 /* Unbound interfaces are always runtime-PM-disabled and -suspended */ in usb_unbind_interface()
507 if (driver->supports_autosuspend) in usb_unbind_interface()
518 * usb_driver_claim_interface - bind a driver to an interface
521 * usb device's active configuration
524 * This is used by usb device drivers that need to claim more than one
525 * interface on a device when probing (audio and acm are current examples).
526 * No device driver should directly modify internal usb_interface or
529 * Callers must own the device lock, so driver probe() entries don't need
538 struct device *dev; in usb_driver_claim_interface()
542 return -ENODEV; in usb_driver_claim_interface()
544 dev = &iface->dev; in usb_driver_claim_interface()
545 if (dev->driver) in usb_driver_claim_interface()
546 return -EBUSY; in usb_driver_claim_interface()
549 if (!iface->authorized) in usb_driver_claim_interface()
550 return -ENODEV; in usb_driver_claim_interface()
552 dev->driver = &driver->drvwrap.driver; in usb_driver_claim_interface()
554 iface->needs_binding = 0; in usb_driver_claim_interface()
556 iface->condition = USB_INTERFACE_BOUND; in usb_driver_claim_interface()
559 * runtime-PM-enabled, but only if the driver has autosuspend in usb_driver_claim_interface()
561 * device from being autosuspended, but left disabled. In either in usb_driver_claim_interface()
565 if (driver->supports_autosuspend) in usb_driver_claim_interface()
577 dev->driver = NULL; in usb_driver_claim_interface()
579 iface->needs_remote_wakeup = 0; in usb_driver_claim_interface()
580 iface->condition = USB_INTERFACE_UNBOUND; in usb_driver_claim_interface()
583 * Unbound interfaces are always runtime-PM-disabled in usb_driver_claim_interface()
584 * and runtime-PM-suspended in usb_driver_claim_interface()
586 if (driver->supports_autosuspend) in usb_driver_claim_interface()
596 * usb_driver_release_interface - unbind a driver from an interface
605 * Callers must own the device lock, so driver disconnect() entries don't
612 struct device *dev = &iface->dev; in usb_driver_release_interface()
615 if (!dev->driver || dev->driver != &driver->drvwrap.driver) in usb_driver_release_interface()
619 if (iface->condition != USB_INTERFACE_BOUND) in usb_driver_release_interface()
621 iface->condition = USB_INTERFACE_UNBINDING; in usb_driver_release_interface()
631 dev->driver = NULL; in usb_driver_release_interface()
638 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id) in usb_match_device() argument
640 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && in usb_match_device()
641 id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) in usb_match_device()
644 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && in usb_match_device()
645 id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) in usb_match_device()
648 /* No need to test id->bcdDevice_lo != 0, since 0 is never in usb_match_device()
650 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && in usb_match_device()
651 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) in usb_match_device()
654 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && in usb_match_device()
655 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) in usb_match_device()
658 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && in usb_match_device()
659 (id->bDeviceClass != dev->descriptor.bDeviceClass)) in usb_match_device()
662 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && in usb_match_device()
663 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass)) in usb_match_device()
666 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && in usb_match_device()
667 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) in usb_match_device()
676 const struct usb_device_id *id) in usb_match_one_id_intf() argument
679 * checked for a match if the device class is Vendor Specific, in usb_match_one_id_intf()
680 * unless the match record specifies the Vendor ID. */ in usb_match_one_id_intf()
681 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC && in usb_match_one_id_intf()
682 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && in usb_match_one_id_intf()
683 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | in usb_match_one_id_intf()
689 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && in usb_match_one_id_intf()
690 (id->bInterfaceClass != intf->desc.bInterfaceClass)) in usb_match_one_id_intf()
693 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && in usb_match_one_id_intf()
694 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) in usb_match_one_id_intf()
697 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && in usb_match_one_id_intf()
698 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) in usb_match_one_id_intf()
701 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) && in usb_match_one_id_intf()
702 (id->bInterfaceNumber != intf->desc.bInterfaceNumber)) in usb_match_one_id_intf()
710 const struct usb_device_id *id) in usb_match_one_id() argument
715 /* proc_connectinfo in devio.c may call us with id == NULL. */ in usb_match_one_id()
716 if (id == NULL) in usb_match_one_id()
719 intf = interface->cur_altsetting; in usb_match_one_id()
722 if (!usb_match_device(dev, id)) in usb_match_one_id()
725 return usb_match_one_id_intf(dev, intf, id); in usb_match_one_id()
730 * usb_match_id - find first usb_device_id matching device or interface
732 * @id: array of usb_device_id structures, terminated by zero entry
735 * the first one matching the device or interface, or null.
737 * Most USB device drivers will use this indirectly, through the usb core,
739 * These device tables are exported with MODULE_DEVICE_TABLE, through
749 * in the device or interface descriptor, or else the device_id
752 * "driver_info" is normally used only by device drivers,
754 * as a driver's "modules.usbmap" entry if you provide an id with
755 * only a nonzero "driver_info" field. If you do this, the USB device
762 * driver selection must come from smart driver id records.
768 * The most specific match specifiers use device descriptor
769 * data. These are commonly used with product-specific matches;
775 * Matches based on device class/subclass/protocol specifications
777 * its siblings. These are used with single-function devices
783 * multiple-function device. Use the USB_INTERFACE_INFO
784 * macro, or its siblings, to match class-per-interface style
788 * any interface if the device class is set to Vendor-Specific.
791 * vendor-specific, and hence matching against a standard product
793 * interface-based match for such a device, create a match record
794 * that also specifies the vendor ID. (Unforunately there isn't a
803 const struct usb_device_id *id) in usb_match_id() argument
805 /* proc_connectinfo in devio.c may call us with id == NULL. */ in usb_match_id()
806 if (id == NULL) in usb_match_id()
809 /* It is important to check that id->driver_info is nonzero, in usb_match_id()
811 id->driver_info is the way to create an entry that in usb_match_id()
813 device and interface. */ in usb_match_id()
814 for (; id->idVendor || id->idProduct || id->bDeviceClass || in usb_match_id()
815 id->bInterfaceClass || id->driver_info; id++) { in usb_match_id()
816 if (usb_match_one_id(interface, id)) in usb_match_id()
817 return id; in usb_match_id()
825 const struct usb_device_id *id) in usb_device_match_id() argument
827 if (!id) in usb_device_match_id()
830 for (; id->idVendor || id->idProduct ; id++) { in usb_device_match_id()
831 if (usb_match_device(udev, id)) in usb_device_match_id()
832 return id; in usb_device_match_id()
842 if (udrv->id_table && udrv->match) in usb_driver_applicable()
843 return usb_device_match_id(udev, udrv->id_table) != NULL && in usb_driver_applicable()
844 udrv->match(udev); in usb_driver_applicable()
846 if (udrv->id_table) in usb_driver_applicable()
847 return usb_device_match_id(udev, udrv->id_table) != NULL; in usb_driver_applicable()
849 if (udrv->match) in usb_driver_applicable()
850 return udrv->match(udev); in usb_driver_applicable()
855 static int usb_device_match(struct device *dev, struct device_driver *drv) in usb_device_match()
869 /* If the device driver under consideration does not have a in usb_device_match()
873 if (!udrv->id_table && !udrv->match) in usb_device_match()
881 const struct usb_device_id *id; in usb_device_match() local
883 /* device drivers never match interfaces */ in usb_device_match()
890 id = usb_match_id(intf, usb_drv->id_table); in usb_device_match()
891 if (id) in usb_device_match()
894 id = usb_match_dynamic_id(intf, usb_drv); in usb_device_match()
895 if (id) in usb_device_match()
902 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) in usb_uevent()
916 if (usb_dev->devnum < 0) { in usb_uevent()
919 return -ENODEV; in usb_uevent()
921 if (!usb_dev->bus) { in usb_uevent()
923 return -ENODEV; in usb_uevent()
926 /* per-device configurations are common */ in usb_uevent()
928 le16_to_cpu(usb_dev->descriptor.idVendor), in usb_uevent()
929 le16_to_cpu(usb_dev->descriptor.idProduct), in usb_uevent()
930 le16_to_cpu(usb_dev->descriptor.bcdDevice))) in usb_uevent()
931 return -ENOMEM; in usb_uevent()
933 /* class-based driver binding models */ in usb_uevent()
935 usb_dev->descriptor.bDeviceClass, in usb_uevent()
936 usb_dev->descriptor.bDeviceSubClass, in usb_uevent()
937 usb_dev->descriptor.bDeviceProtocol)) in usb_uevent()
938 return -ENOMEM; in usb_uevent()
943 static int __usb_bus_reprobe_drivers(struct device *dev, void *data) in __usb_bus_reprobe_drivers()
950 if (dev->driver != &usb_generic_driver.drvwrap.driver) in __usb_bus_reprobe_drivers()
958 if (ret && ret != -EPROBE_DEFER) in __usb_bus_reprobe_drivers()
959 dev_err(dev, "Failed to reprobe device (error %d)\n", ret); in __usb_bus_reprobe_drivers()
965 * usb_register_device_driver - register a USB device (not interface) driver
966 * @new_udriver: USB operations for the device driver
969 * Registers a USB device driver with the USB core. The list of
981 return -ENODEV; in usb_register_device_driver()
983 new_udriver->drvwrap.for_devices = 1; in usb_register_device_driver()
984 new_udriver->drvwrap.driver.name = new_udriver->name; in usb_register_device_driver()
985 new_udriver->drvwrap.driver.bus = &usb_bus_type; in usb_register_device_driver()
986 new_udriver->drvwrap.driver.probe = usb_probe_device; in usb_register_device_driver()
987 new_udriver->drvwrap.driver.remove = usb_unbind_device; in usb_register_device_driver()
988 new_udriver->drvwrap.driver.owner = owner; in usb_register_device_driver()
989 new_udriver->drvwrap.driver.dev_groups = new_udriver->dev_groups; in usb_register_device_driver()
991 retval = driver_register(&new_udriver->drvwrap.driver); in usb_register_device_driver()
994 pr_info("%s: registered new device driver %s\n", in usb_register_device_driver()
995 usbcore_name, new_udriver->name); in usb_register_device_driver()
997 * Check whether any device could be better served with in usb_register_device_driver()
1003 pr_err("%s: error %d registering device driver %s\n", in usb_register_device_driver()
1004 usbcore_name, retval, new_udriver->name); in usb_register_device_driver()
1012 * usb_deregister_device_driver - unregister a USB device (not interface) driver
1013 * @udriver: USB operations of the device driver to unregister
1020 pr_info("%s: deregistering device driver %s\n", in usb_deregister_device_driver()
1021 usbcore_name, udriver->name); in usb_deregister_device_driver()
1023 driver_unregister(&udriver->drvwrap.driver); in usb_deregister_device_driver()
1028 * usb_register_driver - register a USB interface driver
1049 return -ENODEV; in usb_register_driver()
1051 new_driver->drvwrap.for_devices = 0; in usb_register_driver()
1052 new_driver->drvwrap.driver.name = new_driver->name; in usb_register_driver()
1053 new_driver->drvwrap.driver.bus = &usb_bus_type; in usb_register_driver()
1054 new_driver->drvwrap.driver.probe = usb_probe_interface; in usb_register_driver()
1055 new_driver->drvwrap.driver.remove = usb_unbind_interface; in usb_register_driver()
1056 new_driver->drvwrap.driver.owner = owner; in usb_register_driver()
1057 new_driver->drvwrap.driver.mod_name = mod_name; in usb_register_driver()
1058 new_driver->drvwrap.driver.dev_groups = new_driver->dev_groups; in usb_register_driver()
1059 spin_lock_init(&new_driver->dynids.lock); in usb_register_driver()
1060 INIT_LIST_HEAD(&new_driver->dynids.list); in usb_register_driver()
1062 retval = driver_register(&new_driver->drvwrap.driver); in usb_register_driver()
1071 usbcore_name, new_driver->name); in usb_register_driver()
1077 driver_unregister(&new_driver->drvwrap.driver); in usb_register_driver()
1080 usbcore_name, retval, new_driver->name); in usb_register_driver()
1086 * usb_deregister - unregister a USB interface driver
1099 usbcore_name, driver->name); in usb_deregister()
1102 driver_unregister(&driver->drvwrap.driver); in usb_deregister()
1111 * The caller must hold @intf's device's lock, but not @intf's lock.
1115 struct usb_driver *driver = to_usb_driver(intf->dev.driver); in usb_forced_unbind_intf()
1117 dev_dbg(&intf->dev, "forced unbind\n"); in usb_forced_unbind_intf()
1121 intf->needs_binding = 1; in usb_forced_unbind_intf()
1128 * The caller must hold @udev's device lock.
1136 config = udev->actconfig; in unbind_marked_interfaces()
1138 for (i = 0; i < config->desc.bNumInterfaces; ++i) { in unbind_marked_interfaces()
1139 intf = config->interface[i]; in unbind_marked_interfaces()
1140 if (intf->dev.driver && intf->needs_binding) in unbind_marked_interfaces()
1149 * The caller must hold @intf's device's lock, but not @intf's lock.
1159 if (intf->dev.driver) in usb_rebind_intf()
1163 if (!intf->dev.power.is_prepared) { in usb_rebind_intf()
1164 intf->needs_binding = 0; in usb_rebind_intf()
1165 rc = device_attach(&intf->dev); in usb_rebind_intf()
1166 if (rc < 0 && rc != -EPROBE_DEFER) in usb_rebind_intf()
1167 dev_warn(&intf->dev, "rebind failed: %d\n", rc); in usb_rebind_intf()
1175 * The caller must hold @udev's device lock.
1183 config = udev->actconfig; in rebind_marked_interfaces()
1185 for (i = 0; i < config->desc.bNumInterfaces; ++i) { in rebind_marked_interfaces()
1186 intf = config->interface[i]; in rebind_marked_interfaces()
1187 if (intf->needs_binding) in rebind_marked_interfaces()
1198 * The caller must hold @udev's device lock.
1212 * The caller must hold @udev's device lock.
1221 config = udev->actconfig; in unbind_no_pm_drivers_interfaces()
1223 for (i = 0; i < config->desc.bNumInterfaces; ++i) { in unbind_no_pm_drivers_interfaces()
1224 intf = config->interface[i]; in unbind_no_pm_drivers_interfaces()
1226 if (intf->dev.driver) { in unbind_no_pm_drivers_interfaces()
1227 drv = to_usb_driver(intf->dev.driver); in unbind_no_pm_drivers_interfaces()
1228 if (!drv->suspend || !drv->resume) in unbind_no_pm_drivers_interfaces()
1240 if (udev->state == USB_STATE_NOTATTACHED || in usb_suspend_device()
1241 udev->state == USB_STATE_SUSPENDED) in usb_suspend_device()
1245 if (udev->dev.driver) in usb_suspend_device()
1246 udriver = to_usb_device_driver(udev->dev.driver); in usb_suspend_device()
1248 udev->do_remote_wakeup = 0; in usb_suspend_device()
1251 if (udriver->suspend) in usb_suspend_device()
1252 status = udriver->suspend(udev, msg); in usb_suspend_device()
1253 if (status == 0 && udriver->generic_subclass) in usb_suspend_device()
1257 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); in usb_suspend_device()
1266 if (udev->state == USB_STATE_NOTATTACHED) in usb_resume_device()
1270 if (udev->dev.driver == NULL) { in usb_resume_device()
1271 status = -ENOTCONN; in usb_resume_device()
1275 /* Non-root devices on a full/low-speed bus must wait for their in usb_resume_device()
1276 * companion high-speed root hub, in case a handoff is needed. in usb_resume_device()
1278 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion) in usb_resume_device()
1279 device_pm_wait_for_dev(&udev->dev, in usb_resume_device()
1280 &udev->bus->hs_companion->root_hub->dev); in usb_resume_device()
1282 if (udev->quirks & USB_QUIRK_RESET_RESUME) in usb_resume_device()
1283 udev->reset_resume = 1; in usb_resume_device()
1285 udriver = to_usb_device_driver(udev->dev.driver); in usb_resume_device()
1286 if (udriver->generic_subclass) in usb_resume_device()
1288 if (status == 0 && udriver->resume) in usb_resume_device()
1289 status = udriver->resume(udev, msg); in usb_resume_device()
1292 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); in usb_resume_device()
1302 if (udev->state == USB_STATE_NOTATTACHED || in usb_suspend_interface()
1303 intf->condition == USB_INTERFACE_UNBOUND) in usb_suspend_interface()
1305 driver = to_usb_driver(intf->dev.driver); in usb_suspend_interface()
1308 status = driver->suspend(intf, msg); in usb_suspend_interface()
1310 dev_err(&intf->dev, "suspend error %d\n", status); in usb_suspend_interface()
1313 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); in usb_suspend_interface()
1323 if (udev->state == USB_STATE_NOTATTACHED) in usb_resume_interface()
1327 if (intf->condition == USB_INTERFACE_UNBINDING) in usb_resume_interface()
1331 if (intf->condition == USB_INTERFACE_UNBOUND) { in usb_resume_interface()
1334 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) { in usb_resume_interface()
1335 usb_set_interface(udev, intf->altsetting[0]. in usb_resume_interface()
1337 intf->needs_altsetting0 = 0; in usb_resume_interface()
1343 if (intf->needs_binding) in usb_resume_interface()
1345 driver = to_usb_driver(intf->dev.driver); in usb_resume_interface()
1348 if (driver->reset_resume) { in usb_resume_interface()
1349 status = driver->reset_resume(intf); in usb_resume_interface()
1351 dev_err(&intf->dev, "%s error %d\n", in usb_resume_interface()
1354 intf->needs_binding = 1; in usb_resume_interface()
1355 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n", in usb_resume_interface()
1356 driver->name); in usb_resume_interface()
1359 status = driver->resume(intf); in usb_resume_interface()
1361 dev_err(&intf->dev, "resume error %d\n", status); in usb_resume_interface()
1365 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); in usb_resume_interface()
1372 * usb_suspend_both - suspend a USB device and its interfaces
1381 * state as the device, but when called from system sleep, all error
1382 * from suspend methods of interfaces and the non-root-hub device itself
1384 * to the device's state when @udev is root-hub and its suspend method
1387 * Autosuspend requests originating from a child device or an interface
1388 * driver may be made without the protection of @udev's device lock, but
1404 if (udev->state == USB_STATE_NOTATTACHED || in usb_suspend_both()
1405 udev->state == USB_STATE_SUSPENDED) in usb_suspend_both()
1409 if (udev->actconfig) { in usb_suspend_both()
1410 n = udev->actconfig->desc.bNumInterfaces; in usb_suspend_both()
1411 for (i = n - 1; i >= 0; --i) { in usb_suspend_both()
1412 intf = udev->actconfig->interface[i]; in usb_suspend_both()
1426 * Ignore errors from non-root-hub devices during in usb_suspend_both()
1431 if (udev->parent && !PMSG_IS_AUTO(msg)) in usb_suspend_both()
1435 * If the device is inaccessible, don't try to resume in usb_suspend_both()
1438 if (status && status != -EBUSY) { in usb_suspend_both()
1445 dev_err(&udev->dev, in usb_suspend_both()
1446 "Failed to suspend device, error %d\n", in usb_suspend_both()
1455 if (udev->actconfig) { in usb_suspend_both()
1458 intf = udev->actconfig->interface[i]; in usb_suspend_both()
1467 udev->can_submit = 0; in usb_suspend_both()
1469 usb_hcd_flush_endpoint(udev, udev->ep_out[i]); in usb_suspend_both()
1470 usb_hcd_flush_endpoint(udev, udev->ep_in[i]); in usb_suspend_both()
1475 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); in usb_suspend_both()
1480 * usb_resume_both - resume a USB device and its interfaces
1488 * Autoresume requests originating from a child device or an interface
1489 * driver may be made without the protection of @udev's device lock, but
1505 if (udev->state == USB_STATE_NOTATTACHED) { in usb_resume_both()
1506 status = -ENODEV; in usb_resume_both()
1509 udev->can_submit = 1; in usb_resume_both()
1511 /* Resume the device */ in usb_resume_both()
1512 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) in usb_resume_both()
1516 if (status == 0 && udev->actconfig) { in usb_resume_both()
1517 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { in usb_resume_both()
1518 intf = udev->actconfig->interface[i]; in usb_resume_both()
1520 udev->reset_resume); in usb_resume_both()
1526 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); in usb_resume_both()
1528 udev->reset_resume = 0; in usb_resume_both()
1548 w = device_may_wakeup(&udev->dev); in choose_wakeup()
1552 * If the device is autosuspended with the wrong wakeup setting, in choose_wakeup()
1555 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup) in choose_wakeup()
1556 pm_runtime_resume(&udev->dev); in choose_wakeup()
1557 udev->do_remote_wakeup = w; in choose_wakeup()
1560 /* The device lock is held by the PM core */
1561 int usb_suspend(struct device *dev, pm_message_t msg) in usb_suspend()
1577 if (udev->quirks & USB_QUIRK_DISCONNECT_SUSPEND) in usb_suspend()
1583 /* The device lock is held by the PM core */
1584 int usb_resume_complete(struct device *dev) in usb_resume_complete()
1591 if (udev->state != USB_STATE_NOTATTACHED) in usb_resume_complete()
1596 /* The device lock is held by the PM core */
1597 int usb_resume(struct device *dev, pm_message_t msg) in usb_resume()
1602 /* For all calls, take the device back to full power and in usb_resume()
1620 if (status == -ENODEV || status == -ESHUTDOWN) in usb_resume()
1626 * usb_enable_autosuspend - allow a USB device to be autosuspended
1627 * @udev: the USB device which may be autosuspended
1633 * The caller must hold @udev's device lock.
1637 pm_runtime_allow(&udev->dev); in usb_enable_autosuspend()
1642 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1643 * @udev: the USB device which may not be autosuspended
1648 * The caller must hold @udev's device lock.
1652 pm_runtime_forbid(&udev->dev); in usb_disable_autosuspend()
1657 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1662 * @udev's device file in usbfs is closed or after a configuration change.
1668 * The caller must hold @udev's device lock.
1677 status = pm_runtime_put_sync_autosuspend(&udev->dev); in usb_autosuspend_device()
1678 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", in usb_autosuspend_device()
1679 __func__, atomic_read(&udev->dev.power.usage_count), in usb_autosuspend_device()
1684 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1691 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1695 * However if the autoresume fails then the usage counter is re-decremented.
1697 * The caller must hold @udev's device lock.
1707 status = pm_runtime_get_sync(&udev->dev); in usb_autoresume_device()
1709 pm_runtime_put_sync(&udev->dev); in usb_autoresume_device()
1710 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", in usb_autoresume_device()
1711 __func__, atomic_read(&udev->dev.power.usage_count), in usb_autoresume_device()
1719 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1724 * example would be a character-device driver when its device file is
1728 * 0, a delayed autosuspend request for @intf's device is attempted. The
1739 status = pm_runtime_put_sync(&intf->dev); in usb_autopm_put_interface()
1740 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", in usb_autopm_put_interface()
1741 __func__, atomic_read(&intf->dev.power.usage_count), in usb_autopm_put_interface()
1747 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1767 status = pm_runtime_put(&intf->dev); in usb_autopm_put_interface_async()
1768 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", in usb_autopm_put_interface_async()
1769 __func__, atomic_read(&intf->dev.power.usage_count), in usb_autopm_put_interface_async()
1775 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1788 pm_runtime_put_noidle(&intf->dev); in usb_autopm_put_interface_no_suspend()
1793 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1801 * or @intf is unbound. A typical example would be a character-device
1802 * driver when its device file is opened.
1805 * However if the autoresume fails then the counter is re-decremented.
1815 status = pm_runtime_get_sync(&intf->dev); in usb_autopm_get_interface()
1817 pm_runtime_put_sync(&intf->dev); in usb_autopm_get_interface()
1818 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", in usb_autopm_get_interface()
1819 __func__, atomic_read(&intf->dev.power.usage_count), in usb_autopm_get_interface()
1828 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1833 * queues an autoresume request if the device is suspended. The
1836 * themselves), and it does not autoresume the device directly (it only
1837 * queues a request). After a successful call, the device may not yet be
1848 status = pm_runtime_get(&intf->dev); in usb_autopm_get_interface_async()
1849 if (status < 0 && status != -EINPROGRESS) in usb_autopm_get_interface_async()
1850 pm_runtime_put_noidle(&intf->dev); in usb_autopm_get_interface_async()
1851 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", in usb_autopm_get_interface_async()
1852 __func__, atomic_read(&intf->dev.power.usage_count), in usb_autopm_get_interface_async()
1854 if (status > 0 || status == -EINPROGRESS) in usb_autopm_get_interface_async()
1861 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1874 pm_runtime_get_noresume(&intf->dev); in usb_autopm_get_interface_no_resume()
1878 /* Internal routine to check whether we may autosuspend a device. */
1884 if (udev->state == USB_STATE_NOTATTACHED) in autosuspend_check()
1885 return -ENODEV; in autosuspend_check()
1891 if (udev->actconfig) { in autosuspend_check()
1892 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { in autosuspend_check()
1893 intf = udev->actconfig->interface[i]; in autosuspend_check()
1900 if (intf->dev.power.disable_depth) in autosuspend_check()
1902 if (atomic_read(&intf->dev.power.usage_count) > 0) in autosuspend_check()
1903 return -EBUSY; in autosuspend_check()
1904 w |= intf->needs_remote_wakeup; in autosuspend_check()
1906 /* Don't allow autosuspend if the device will need in autosuspend_check()
1907 * a reset-resume and any of its interface drivers in autosuspend_check()
1910 if (udev->quirks & USB_QUIRK_RESET_RESUME) { in autosuspend_check()
1913 driver = to_usb_driver(intf->dev.driver); in autosuspend_check()
1914 if (!driver->reset_resume || in autosuspend_check()
1915 intf->needs_remote_wakeup) in autosuspend_check()
1916 return -EOPNOTSUPP; in autosuspend_check()
1920 if (w && !device_can_wakeup(&udev->dev)) { in autosuspend_check()
1921 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n"); in autosuspend_check()
1922 return -EOPNOTSUPP; in autosuspend_check()
1926 * If the device is a direct child of the root hub and the HCD in autosuspend_check()
1930 if (w && udev->parent == udev->bus->root_hub && in autosuspend_check()
1931 bus_to_hcd(udev->bus)->cant_recv_wakeups) { in autosuspend_check()
1932 dev_dbg(&udev->dev, "HCD doesn't handle wakeup requests\n"); in autosuspend_check()
1933 return -EOPNOTSUPP; in autosuspend_check()
1936 udev->do_remote_wakeup = w; in autosuspend_check()
1940 int usb_runtime_suspend(struct device *dev) in usb_runtime_suspend()
1945 /* A USB device can be suspended if it passes the various autosuspend in usb_runtime_suspend()
1946 * checks. Runtime suspend for a USB device means suspending all the in usb_runtime_suspend()
1947 * interfaces and then the device itself. in usb_runtime_suspend()
1950 return -EAGAIN; in usb_runtime_suspend()
1955 if (status == -EAGAIN || status == -EBUSY) in usb_runtime_suspend()
1960 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error in usb_runtime_suspend()
1964 if (status != 0 && udev->parent) in usb_runtime_suspend()
1965 return -EBUSY; in usb_runtime_suspend()
1969 int usb_runtime_resume(struct device *dev) in usb_runtime_resume()
1974 /* Runtime resume for a USB device means resuming both the device in usb_runtime_resume()
1981 int usb_runtime_idle(struct device *dev) in usb_runtime_idle()
1985 /* An idle USB device can be suspended if it passes the various in usb_runtime_idle()
1991 return -EBUSY; in usb_runtime_idle()
1996 struct usb_hcd *hcd = bus_to_hcd(udev->bus); in usb_set_usb2_hardware_lpm()
1997 int ret = -EPERM; in usb_set_usb2_hardware_lpm()
1999 if (hcd->driver->set_usb2_hw_lpm) { in usb_set_usb2_hardware_lpm()
2000 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable); in usb_set_usb2_hardware_lpm()
2002 udev->usb2_hw_lpm_enabled = enable; in usb_set_usb2_hardware_lpm()
2010 if (!udev->usb2_hw_lpm_capable || in usb_enable_usb2_hardware_lpm()
2011 !udev->usb2_hw_lpm_allowed || in usb_enable_usb2_hardware_lpm()
2012 udev->usb2_hw_lpm_enabled) in usb_enable_usb2_hardware_lpm()
2020 if (!udev->usb2_hw_lpm_enabled) in usb_disable_usb2_hardware_lpm()