25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <linux/export.h>
41 ssize_t usb_store_new_id(
struct usb_dynids *dynids,
45 struct usb_dynid *dynid;
52 fields =
sscanf(buf,
"%x %x %x", &idVendor, &idProduct,
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
66 dynid->id.bInterfaceClass = (
u8)bInterfaceClass;
70 spin_lock(&dynids->lock);
72 spin_unlock(&dynids->lock);
82 ssize_t usb_show_dynids(
struct usb_dynids *dynids,
char *buf)
84 struct usb_dynid *dynid;
88 if (dynid->
id.bInterfaceClass != 0)
90 dynid->
id.idVendor, dynid->
id.idProduct,
91 dynid->
id.bInterfaceClass);
94 dynid->
id.idVendor, dynid->
id.idProduct);
101 struct usb_driver *usb_drv = to_usb_driver(driver);
103 return usb_show_dynids(&usb_drv->dynids, buf);
107 const char *buf,
size_t count)
109 struct usb_driver *usb_drv = to_usb_driver(driver);
111 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
124 store_remove_id(
struct device_driver *driver,
const char *buf,
size_t count)
126 struct usb_dynid *dynid, *
n;
127 struct usb_driver *usb_driver = to_usb_driver(driver);
132 fields =
sscanf(buf,
"%x %x", &idVendor, &idProduct);
136 spin_lock(&usb_driver->dynids.lock);
146 spin_unlock(&usb_driver->dynids.lock);
151 static int usb_create_newid_files(
struct usb_driver *usb_drv)
155 if (usb_drv->no_dynamic_id)
158 if (usb_drv->probe !=
NULL) {
160 &driver_attr_new_id);
163 &driver_attr_remove_id);
166 &driver_attr_new_id);
173 static void usb_remove_newid_files(
struct usb_driver *usb_drv)
175 if (usb_drv->no_dynamic_id)
178 if (usb_drv->probe !=
NULL) {
180 &driver_attr_remove_id);
182 &driver_attr_new_id);
186 static void usb_free_dynids(
struct usb_driver *usb_drv)
188 struct usb_dynid *dynid, *
n;
190 spin_lock(&usb_drv->dynids.lock);
195 spin_unlock(&usb_drv->dynids.lock);
198 static inline int usb_create_newid_files(
struct usb_driver *usb_drv)
203 static void usb_remove_newid_files(
struct usb_driver *usb_drv)
207 static inline void usb_free_dynids(
struct usb_driver *usb_drv)
213 struct usb_driver *drv)
215 struct usb_dynid *dynid;
217 spin_lock(&drv->dynids.lock);
220 spin_unlock(&drv->dynids.lock);
224 spin_unlock(&drv->dynids.lock);
230 static int usb_probe_device(
struct device *
dev)
232 struct usb_device_driver *udriver = to_usb_device_driver(dev->
driver);
233 struct usb_device *
udev = to_usb_device(dev);
236 dev_dbg(dev,
"%s\n", __func__);
243 if (!udriver->supports_autosuspend)
244 error = usb_autoresume_device(udev);
247 error = udriver->probe(udev);
252 static int usb_unbind_device(
struct device *dev)
254 struct usb_device *udev = to_usb_device(dev);
255 struct usb_device_driver *udriver = to_usb_device_driver(dev->
driver);
257 udriver->disconnect(udev);
258 if (!udriver->supports_autosuspend)
272 static void usb_cancel_queued_reset(
struct usb_interface *iface)
274 if (iface->reset_running == 0)
279 static int usb_probe_interface(
struct device *dev)
281 struct usb_driver *driver = to_usb_driver(dev->
driver);
283 struct usb_device *udev = interface_to_usbdev(intf);
286 int lpm_disable_error;
288 dev_dbg(dev,
"%s\n", __func__);
290 intf->needs_binding = 0;
295 if (udev->authorized == 0) {
296 dev_err(&intf->dev,
"Device is not authorized for usage\n");
302 id = usb_match_dynamic_id(intf, driver);
306 dev_dbg(dev,
"%s - got id\n", __func__);
308 error = usb_autoresume_device(udev);
312 intf->condition = USB_INTERFACE_BINDING;
318 pm_runtime_set_active(dev);
319 pm_suspend_ignore_children(dev,
false);
320 if (driver->supports_autosuspend)
335 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
336 dev_err(&intf->dev,
"%s Failed to disable LPM for driver %s\n.",
337 __func__, driver->name);
338 error = lpm_disable_error;
343 if (intf->needs_altsetting0) {
345 desc.bInterfaceNumber, 0);
348 intf->needs_altsetting0 = 0;
351 error = driver->probe(intf,
id);
355 intf->condition = USB_INTERFACE_BOUND;
358 if (!lpm_disable_error)
365 usb_set_intfdata(intf,
NULL);
366 intf->needs_remote_wakeup = 0;
367 intf->condition = USB_INTERFACE_UNBOUND;
368 usb_cancel_queued_reset(intf);
371 if (!lpm_disable_error)
375 if (driver->supports_autosuspend)
376 pm_runtime_disable(dev);
377 pm_runtime_set_suspended(dev);
384 static int usb_unbind_interface(
struct device *dev)
386 struct usb_driver *driver = to_usb_driver(dev->
driver);
388 struct usb_device *
udev;
389 int error,
r, lpm_disable_error;
391 intf->condition = USB_INTERFACE_UNBINDING;
394 udev = interface_to_usbdev(intf);
395 error = usb_autoresume_device(udev);
407 if (!driver->soft_unbind)
410 driver->disconnect(intf);
411 usb_cancel_queued_reset(intf);
419 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
424 }
else if (!error && !intf->dev.power.is_prepared) {
426 desc.bInterfaceNumber, 0);
428 intf->needs_altsetting0 = 1;
430 intf->needs_altsetting0 = 1;
432 usb_set_intfdata(intf,
NULL);
434 intf->condition = USB_INTERFACE_UNBOUND;
435 intf->needs_remote_wakeup = 0;
438 if (!lpm_disable_error)
442 if (driver->supports_autosuspend)
443 pm_runtime_disable(dev);
444 pm_runtime_set_suspended(dev);
447 for (r =
atomic_read(&intf->pm_usage_cnt); r > 0; --r)
448 usb_autopm_put_interface_no_suspend(intf);
480 struct device *dev = &iface->dev;
481 struct usb_device *
udev;
483 int lpm_disable_error;
488 udev = interface_to_usbdev(iface);
490 dev->
driver = &driver->drvwrap.driver;
491 usb_set_intfdata(iface, priv);
492 iface->needs_binding = 0;
494 iface->condition = USB_INTERFACE_BOUND;
498 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
499 dev_err(&iface->dev,
"%s Failed to disable LPM for driver %s\n.",
500 __func__, driver->name);
510 pm_suspend_ignore_children(dev,
false);
511 if (driver->supports_autosuspend)
514 pm_runtime_set_active(dev);
519 if (device_is_registered(dev))
523 if (!lpm_disable_error)
547 struct device *dev = &iface->dev;
550 if (!dev->
driver || dev->
driver != &driver->drvwrap.driver)
554 if (iface->condition != USB_INTERFACE_BOUND)
556 iface->condition = USB_INTERFACE_UNBINDING;
561 if (device_is_registered(dev)) {
565 usb_unbind_interface(dev);
610 struct usb_host_interface *intf,
647 struct usb_host_interface *
intf;
648 struct usb_device *
dev;
654 intf = interface->cur_altsetting;
655 dev = interface_to_usbdev(interface);
747 for (;
id->idVendor ||
id->idProduct ||
id->bDeviceClass ||
748 id->bInterfaceClass ||
id->driver_info;
id++) {
760 if (is_usb_device(dev)) {
763 if (!is_usb_device_driver(drv))
769 }
else if (is_usb_interface(dev)) {
771 struct usb_driver *usb_drv;
775 if (is_usb_device_driver(drv))
778 intf = to_usb_interface(dev);
779 usb_drv = to_usb_driver(drv);
785 id = usb_match_dynamic_id(intf, usb_drv);
793 #ifdef CONFIG_HOTPLUG
796 struct usb_device *usb_dev;
798 if (is_usb_device(dev)) {
799 usb_dev = to_usb_device(dev);
800 }
else if (is_usb_interface(dev)) {
803 usb_dev = interface_to_usbdev(intf);
808 if (usb_dev->devnum < 0) {
810 pr_debug(
"usb %s: already deleted?\n", dev_name(dev));
814 pr_debug(
"usb %s: bus removed?\n", dev_name(dev));
827 usb_dev->descriptor.bDeviceClass,
828 usb_dev->descriptor.bDeviceSubClass,
829 usb_dev->descriptor.bDeviceProtocol))
861 new_udriver->drvwrap.for_devices = 1;
862 new_udriver->drvwrap.driver.name = (
char *) new_udriver->name;
864 new_udriver->drvwrap.driver.probe = usb_probe_device;
865 new_udriver->drvwrap.driver.remove = usb_unbind_device;
866 new_udriver->drvwrap.driver.owner = owner;
871 pr_info(
"%s: registered new device driver %s\n",
891 pr_info(
"%s: deregistering device driver %s\n",
914 const char *mod_name)
921 new_driver->drvwrap.for_devices = 0;
922 new_driver->drvwrap.driver.name = (
char *) new_driver->name;
924 new_driver->drvwrap.driver.probe = usb_probe_interface;
925 new_driver->drvwrap.driver.remove = usb_unbind_interface;
926 new_driver->drvwrap.driver.owner = owner;
927 new_driver->drvwrap.driver.mod_name = mod_name;
929 INIT_LIST_HEAD(&new_driver->dynids.list);
935 retval = usb_create_newid_files(new_driver);
939 pr_info(
"%s: registered new interface driver %s\n",
968 pr_info(
"%s: deregistering interface driver %s\n",
971 usb_remove_newid_files(driver);
973 usb_free_dynids(driver);
986 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
988 dev_dbg(&intf->dev,
"forced unbind\n");
992 intf->needs_binding = 1;
1009 if (intf->dev.driver)
1013 if (!intf->dev.power.is_prepared) {
1014 intf->needs_binding = 0;
1017 dev_warn(&intf->dev,
"rebind failed: %d\n", rc);
1029 static void unbind_no_pm_drivers_interfaces(
struct usb_device *udev)
1031 struct usb_host_config *
config;
1034 struct usb_driver *drv;
1036 config = udev->actconfig;
1038 for (i = 0; i < config->desc.bNumInterfaces; ++
i) {
1039 intf = config->interface[
i];
1041 if (intf->dev.driver) {
1042 drv = to_usb_driver(intf->dev.driver);
1043 if (!drv->suspend || !drv->resume)
1055 static void unbind_no_reset_resume_drivers_interfaces(
struct usb_device *udev)
1057 struct usb_host_config *
config;
1061 config = udev->actconfig;
1063 for (i = 0; i < config->desc.bNumInterfaces; ++
i) {
1064 intf = config->interface[
i];
1065 if (intf->dev.driver && intf->needs_binding)
1071 static void do_rebind_interfaces(
struct usb_device *udev)
1073 struct usb_host_config *
config;
1077 config = udev->actconfig;
1079 for (i = 0; i < config->desc.bNumInterfaces; ++
i) {
1080 intf = config->interface[
i];
1081 if (intf->needs_binding)
1087 static int usb_suspend_device(
struct usb_device *udev,
pm_message_t msg)
1089 struct usb_device_driver *udriver;
1097 if (udev->dev.driver)
1098 udriver = to_usb_device_driver(udev->dev.driver);
1100 udev->do_remote_wakeup = 0;
1103 status = udriver->suspend(udev, msg);
1106 dev_vdbg(&udev->dev,
"%s: status %d\n", __func__, status);
1110 static int usb_resume_device(
struct usb_device *udev,
pm_message_t msg)
1112 struct usb_device_driver *udriver;
1119 if (udev->dev.driver ==
NULL) {
1127 if (!
PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
1129 &udev->bus->hs_companion->root_hub->dev);
1132 udev->reset_resume = 1;
1134 udriver = to_usb_device_driver(udev->dev.driver);
1135 status = udriver->resume(udev, msg);
1138 dev_vdbg(&udev->dev,
"%s: status %d\n", __func__, status);
1142 static int usb_suspend_interface(
struct usb_device *udev,
1145 struct usb_driver *
driver;
1149 intf->condition == USB_INTERFACE_UNBOUND)
1151 driver = to_usb_driver(intf->dev.driver);
1154 status = driver->suspend(intf, msg);
1156 dev_err(&intf->dev,
"suspend error %d\n", status);
1159 dev_vdbg(&intf->dev,
"%s: status %d\n", __func__, status);
1163 static int usb_resume_interface(
struct usb_device *udev,
1166 struct usb_driver *
driver;
1173 if (intf->condition == USB_INTERFACE_UNBINDING)
1177 if (intf->condition == USB_INTERFACE_UNBOUND) {
1180 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
1182 desc.bInterfaceNumber, 0);
1183 intf->needs_altsetting0 = 0;
1189 if (intf->needs_binding)
1191 driver = to_usb_driver(intf->dev.driver);
1194 if (driver->reset_resume) {
1195 status = driver->reset_resume(intf);
1197 dev_err(&intf->dev,
"%s error %d\n",
1198 "reset_resume", status);
1200 intf->needs_binding = 1;
1201 dev_warn(&intf->dev,
"no %s for driver %s?\n",
1202 "reset_resume", driver->name);
1205 status = driver->resume(intf);
1207 dev_err(&intf->dev,
"resume error %d\n", status);
1211 dev_vdbg(&intf->dev,
"%s: status %d\n", __func__, status);
1237 static int usb_suspend_both(
struct usb_device *udev,
pm_message_t msg)
1248 if (udev->actconfig) {
1249 n = udev->actconfig->desc.bNumInterfaces;
1250 for (i = n - 1; i >= 0; --
i) {
1251 intf = udev->actconfig->interface[
i];
1252 status = usb_suspend_interface(udev, intf, msg);
1262 status = usb_suspend_device(udev, msg);
1278 intf = udev->actconfig->interface[
i];
1279 usb_resume_interface(udev, intf, msg, 0);
1286 udev->can_submit = 0;
1287 for (i = 0; i < 16; ++
i) {
1294 dev_vdbg(&udev->dev,
"%s: status %d\n", __func__, status);
1316 static int usb_resume_both(
struct usb_device *udev,
pm_message_t msg)
1326 udev->can_submit = 1;
1330 status = usb_resume_device(udev, msg);
1333 if (status == 0 && udev->actconfig) {
1334 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1335 intf = udev->actconfig->interface[
i];
1336 usb_resume_interface(udev, intf, msg,
1337 udev->reset_resume);
1340 usb_mark_last_busy(udev);
1343 dev_vdbg(&udev->dev,
"%s: status %d\n", __func__, status);
1345 udev->reset_resume = 0;
1349 static void choose_wakeup(
struct usb_device *udev,
pm_message_t msg)
1359 udev->do_remote_wakeup = 0;
1366 w = device_may_wakeup(&udev->dev);
1372 pm_runtime_resume(&udev->dev);
1373 udev->do_remote_wakeup =
w;
1379 struct usb_device *udev = to_usb_device(dev);
1381 unbind_no_pm_drivers_interfaces(udev);
1387 choose_wakeup(udev, msg);
1388 return usb_suspend_both(udev, msg);
1392 int usb_resume_complete(
struct device *dev)
1394 struct usb_device *udev = to_usb_device(dev);
1400 do_rebind_interfaces(udev);
1407 struct usb_device *udev = to_usb_device(dev);
1417 status = usb_resume_both(udev, msg);
1419 pm_runtime_disable(dev);
1420 pm_runtime_set_active(dev);
1422 unbind_no_reset_resume_drivers_interfaces(udev);
1435 #ifdef CONFIG_USB_SUSPEND
1447 void usb_enable_autosuspend(
struct usb_device *udev)
1462 void usb_disable_autosuspend(
struct usb_device *udev)
1488 usb_mark_last_busy(udev);
1489 status = pm_runtime_put_sync_autosuspend(&udev->dev);
1490 dev_vdbg(&udev->dev,
"%s: cnt %d -> %d\n",
1491 __func__,
atomic_read(&udev->dev.power.usage_count),
1513 int usb_autoresume_device(
struct usb_device *udev)
1517 status = pm_runtime_get_sync(&udev->dev);
1519 pm_runtime_put_sync(&udev->dev);
1520 dev_vdbg(&udev->dev,
"%s: cnt %d -> %d\n",
1521 __func__,
atomic_read(&udev->dev.power.usage_count),
1545 struct usb_device *udev = interface_to_usbdev(intf);
1548 usb_mark_last_busy(udev);
1550 status = pm_runtime_put_sync(&intf->dev);
1551 dev_vdbg(&intf->dev,
"%s: cnt %d -> %d\n",
1552 __func__,
atomic_read(&intf->dev.power.usage_count),
1572 void usb_autopm_put_interface_async(
struct usb_interface *intf)
1574 struct usb_device *udev = interface_to_usbdev(intf);
1577 usb_mark_last_busy(udev);
1579 status = pm_runtime_put(&intf->dev);
1580 dev_vdbg(&intf->dev,
"%s: cnt %d -> %d\n",
1581 __func__,
atomic_read(&intf->dev.power.usage_count),
1595 void usb_autopm_put_interface_no_suspend(
struct usb_interface *intf)
1597 struct usb_device *udev = interface_to_usbdev(intf);
1599 usb_mark_last_busy(udev);
1601 pm_runtime_put_noidle(&intf->dev);
1626 status = pm_runtime_get_sync(&intf->dev);
1628 pm_runtime_put_sync(&intf->dev);
1631 dev_vdbg(&intf->dev,
"%s: cnt %d -> %d\n",
1632 __func__,
atomic_read(&intf->dev.power.usage_count),
1655 int usb_autopm_get_interface_async(
struct usb_interface *intf)
1659 status = pm_runtime_get(&intf->dev);
1661 pm_runtime_put_noidle(&intf->dev);
1664 dev_vdbg(&intf->dev,
"%s: cnt %d -> %d\n",
1665 __func__,
atomic_read(&intf->dev.power.usage_count),
1682 void usb_autopm_get_interface_no_resume(
struct usb_interface *intf)
1684 struct usb_device *udev = interface_to_usbdev(intf);
1686 usb_mark_last_busy(udev);
1688 pm_runtime_get_noresume(&intf->dev);
1693 static int autosuspend_check(
struct usb_device *udev)
1702 if (udev->actconfig) {
1703 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1704 intf = udev->actconfig->interface[
i];
1711 if (intf->dev.power.disable_depth)
1713 if (
atomic_read(&intf->dev.power.usage_count) > 0)
1715 w |= intf->needs_remote_wakeup;
1722 struct usb_driver *
driver;
1724 driver = to_usb_driver(intf->dev.driver);
1725 if (!driver->reset_resume ||
1726 intf->needs_remote_wakeup)
1731 if (w && !device_can_wakeup(&udev->dev)) {
1732 dev_dbg(&udev->dev,
"remote wakeup needed for autosuspend\n");
1735 udev->do_remote_wakeup =
w;
1739 int usb_runtime_suspend(
struct device *dev)
1741 struct usb_device *udev = to_usb_device(dev);
1748 if (autosuspend_check(udev) != 0)
1755 usb_mark_last_busy(udev);
1765 int usb_runtime_resume(
struct device *dev)
1767 struct usb_device *udev = to_usb_device(dev);
1777 int usb_runtime_idle(
struct device *dev)
1779 struct usb_device *udev = to_usb_device(dev);
1784 if (autosuspend_check(udev) == 0)
1785 pm_runtime_autosuspend(dev);
1789 int usb_set_usb2_hardware_lpm(
struct usb_device *udev,
int enable)
1791 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1794 if (hcd->driver->set_usb2_hw_lpm) {
1795 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1797 udev->usb2_hw_lpm_enabled =
enable;
1807 .match = usb_device_match,
1808 .uevent = usb_uevent,