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/usb.h>
27 #include <linux/usb/quirks.h>
28 #include <linux/workqueue.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;
49 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
53 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
57 INIT_LIST_HEAD(&dynid->node);
58 dynid->id.idVendor = idVendor;
59 dynid->id.idProduct = idProduct;
60 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
62 spin_lock(&dynids->lock);
63 list_add_tail(&dynid->node, &dynids->list);
64 spin_unlock(&dynids->lock);
66 if (get_driver(driver)) {
67 retval = driver_attach(driver);
75 EXPORT_SYMBOL_GPL(usb_store_new_id);
77 static ssize_t store_new_id(struct device_driver *driver,
78 const char *buf, size_t count)
80 struct usb_driver *usb_drv = to_usb_driver(driver);
82 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
84 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
87 * store_remove_id - remove a USB device ID from this driver
88 * @driver: target device driver
89 * @buf: buffer for scanning device ID data
92 * Removes a dynamic usb device ID from this driver.
95 store_remove_id(struct device_driver *driver, const char *buf, size_t count)
97 struct usb_dynid *dynid, *n;
98 struct usb_driver *usb_driver = to_usb_driver(driver);
104 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
108 spin_lock(&usb_driver->dynids.lock);
109 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
110 struct usb_device_id *id = &dynid->id;
111 if ((id->idVendor == idVendor) &&
112 (id->idProduct == idProduct)) {
113 list_del(&dynid->node);
119 spin_unlock(&usb_driver->dynids.lock);
125 static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
127 static int usb_create_newid_file(struct usb_driver *usb_drv)
131 if (usb_drv->no_dynamic_id)
134 if (usb_drv->probe != NULL)
135 error = driver_create_file(&usb_drv->drvwrap.driver,
136 &driver_attr_new_id);
141 static void usb_remove_newid_file(struct usb_driver *usb_drv)
143 if (usb_drv->no_dynamic_id)
146 if (usb_drv->probe != NULL)
147 driver_remove_file(&usb_drv->drvwrap.driver,
148 &driver_attr_new_id);
152 usb_create_removeid_file(struct usb_driver *drv)
155 if (drv->probe != NULL)
156 error = driver_create_file(&drv->drvwrap.driver,
157 &driver_attr_remove_id);
161 static void usb_remove_removeid_file(struct usb_driver *drv)
163 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
166 static void usb_free_dynids(struct usb_driver *usb_drv)
168 struct usb_dynid *dynid, *n;
170 spin_lock(&usb_drv->dynids.lock);
171 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
172 list_del(&dynid->node);
175 spin_unlock(&usb_drv->dynids.lock);
178 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
183 static void usb_remove_newid_file(struct usb_driver *usb_drv)
188 usb_create_removeid_file(struct usb_driver *drv)
193 static void usb_remove_removeid_file(struct usb_driver *drv)
197 static inline void usb_free_dynids(struct usb_driver *usb_drv)
202 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
203 struct usb_driver *drv)
205 struct usb_dynid *dynid;
207 spin_lock(&drv->dynids.lock);
208 list_for_each_entry(dynid, &drv->dynids.list, node) {
209 if (usb_match_one_id(intf, &dynid->id)) {
210 spin_unlock(&drv->dynids.lock);
214 spin_unlock(&drv->dynids.lock);
219 /* called from driver core with dev locked */
220 static int usb_probe_device(struct device *dev)
222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
223 struct usb_device *udev = to_usb_device(dev);
226 dev_dbg(dev, "%s\n", __func__);
228 /* TODO: Add real matching code */
230 /* The device should always appear to be in use
231 * unless the driver suports autosuspend.
233 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
235 error = udriver->probe(udev);
239 /* called from driver core with dev locked */
240 static int usb_unbind_device(struct device *dev)
242 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
244 udriver->disconnect(to_usb_device(dev));
249 * Cancel any pending scheduled resets
251 * [see usb_queue_reset_device()]
253 * Called after unconfiguring / when releasing interfaces. See
254 * comments in __usb_queue_reset_device() regarding
255 * udev->reset_running.
257 static void usb_cancel_queued_reset(struct usb_interface *iface)
259 if (iface->reset_running == 0)
260 cancel_work_sync(&iface->reset_ws);
263 /* called from driver core with dev locked */
264 static int usb_probe_interface(struct device *dev)
266 struct usb_driver *driver = to_usb_driver(dev->driver);
267 struct usb_interface *intf = to_usb_interface(dev);
268 struct usb_device *udev = interface_to_usbdev(intf);
269 const struct usb_device_id *id;
272 dev_dbg(dev, "%s\n", __func__);
274 intf->needs_binding = 0;
276 if (usb_device_is_owned(udev))
279 if (udev->authorized == 0) {
280 dev_err(&intf->dev, "Device is not authorized for usage\n");
284 id = usb_match_id(intf, driver->id_table);
286 id = usb_match_dynamic_id(intf, driver);
290 dev_dbg(dev, "%s - got id\n", __func__);
292 error = usb_autoresume_device(udev);
296 /* Interface "power state" doesn't correspond to any hardware
297 * state whatsoever. We use it to record when it's bound to
298 * a driver that may start I/0: it's not frozen/quiesced.
301 intf->condition = USB_INTERFACE_BINDING;
303 /* The interface should always appear to be in use
304 * unless the driver suports autosuspend.
306 atomic_set(&intf->pm_usage_cnt, !driver->supports_autosuspend);
308 /* Carry out a deferred switch to altsetting 0 */
309 if (intf->needs_altsetting0) {
310 error = usb_set_interface(udev, intf->altsetting[0].
311 desc.bInterfaceNumber, 0);
314 intf->needs_altsetting0 = 0;
317 error = driver->probe(intf, id);
321 intf->condition = USB_INTERFACE_BOUND;
322 usb_autosuspend_device(udev);
327 intf->needs_remote_wakeup = 0;
328 intf->condition = USB_INTERFACE_UNBOUND;
329 usb_cancel_queued_reset(intf);
330 usb_autosuspend_device(udev);
334 /* called from driver core with dev locked */
335 static int usb_unbind_interface(struct device *dev)
337 struct usb_driver *driver = to_usb_driver(dev->driver);
338 struct usb_interface *intf = to_usb_interface(dev);
339 struct usb_device *udev;
342 intf->condition = USB_INTERFACE_UNBINDING;
344 /* Autoresume for set_interface call below */
345 udev = interface_to_usbdev(intf);
346 error = usb_autoresume_device(udev);
348 /* Terminate all URBs for this interface unless the driver
349 * supports "soft" unbinding.
351 if (!driver->soft_unbind)
352 usb_disable_interface(udev, intf, false);
354 driver->disconnect(intf);
355 usb_cancel_queued_reset(intf);
357 /* Reset other interface state.
358 * We cannot do a Set-Interface if the device is suspended or
359 * if it is prepared for a system sleep (since installing a new
360 * altsetting means creating new endpoint device entries).
361 * When either of these happens, defer the Set-Interface.
363 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
364 /* Already in altsetting 0 so skip Set-Interface.
365 * Just re-enable it without affecting the endpoint toggles.
367 usb_enable_interface(udev, intf, false);
368 } else if (!error && intf->dev.power.status == DPM_ON) {
369 r = usb_set_interface(udev, intf->altsetting[0].
370 desc.bInterfaceNumber, 0);
372 intf->needs_altsetting0 = 1;
374 intf->needs_altsetting0 = 1;
376 usb_set_intfdata(intf, NULL);
378 intf->condition = USB_INTERFACE_UNBOUND;
380 intf->needs_remote_wakeup = 0;
383 usb_autosuspend_device(udev);
389 * usb_driver_claim_interface - bind a driver to an interface
390 * @driver: the driver to be bound
391 * @iface: the interface to which it will be bound; must be in the
392 * usb device's active configuration
393 * @priv: driver data associated with that interface
395 * This is used by usb device drivers that need to claim more than one
396 * interface on a device when probing (audio and acm are current examples).
397 * No device driver should directly modify internal usb_interface or
398 * usb_device structure members.
400 * Few drivers should need to use this routine, since the most natural
401 * way to bind to an interface is to return the private data from
402 * the driver's probe() method.
404 * Callers must own the device lock, so driver probe() entries don't need
405 * extra locking, but other call contexts may need to explicitly claim that
408 int usb_driver_claim_interface(struct usb_driver *driver,
409 struct usb_interface *iface, void *priv)
411 struct device *dev = &iface->dev;
412 struct usb_device *udev = interface_to_usbdev(iface);
418 dev->driver = &driver->drvwrap.driver;
419 usb_set_intfdata(iface, priv);
420 iface->needs_binding = 0;
423 iface->condition = USB_INTERFACE_BOUND;
425 atomic_set(&iface->pm_usage_cnt, !driver->supports_autosuspend);
428 /* if interface was already added, bind now; else let
429 * the future device_add() bind it, bypassing probe()
431 if (device_is_registered(dev))
432 retval = device_bind_driver(dev);
436 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
439 * usb_driver_release_interface - unbind a driver from an interface
440 * @driver: the driver to be unbound
441 * @iface: the interface from which it will be unbound
443 * This can be used by drivers to release an interface without waiting
444 * for their disconnect() methods to be called. In typical cases this
445 * also causes the driver disconnect() method to be called.
447 * This call is synchronous, and may not be used in an interrupt context.
448 * Callers must own the device lock, so driver disconnect() entries don't
449 * need extra locking, but other call contexts may need to explicitly claim
452 void usb_driver_release_interface(struct usb_driver *driver,
453 struct usb_interface *iface)
455 struct device *dev = &iface->dev;
457 /* this should never happen, don't release something that's not ours */
458 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
461 /* don't release from within disconnect() */
462 if (iface->condition != USB_INTERFACE_BOUND)
464 iface->condition = USB_INTERFACE_UNBINDING;
466 /* Release via the driver core only if the interface
467 * has already been registered
469 if (device_is_registered(dev)) {
470 device_release_driver(dev);
473 usb_unbind_interface(dev);
478 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
480 /* returns 0 if no match, 1 if match */
481 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
483 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
484 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
487 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
488 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
491 /* No need to test id->bcdDevice_lo != 0, since 0 is never
492 greater than any unsigned number. */
493 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
494 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
497 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
498 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
501 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
502 (id->bDeviceClass != dev->descriptor.bDeviceClass))
505 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
506 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
509 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
510 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
516 /* returns 0 if no match, 1 if match */
517 int usb_match_one_id(struct usb_interface *interface,
518 const struct usb_device_id *id)
520 struct usb_host_interface *intf;
521 struct usb_device *dev;
523 /* proc_connectinfo in devio.c may call us with id == NULL. */
527 intf = interface->cur_altsetting;
528 dev = interface_to_usbdev(interface);
530 if (!usb_match_device(dev, id))
533 /* The interface class, subclass, and protocol should never be
534 * checked for a match if the device class is Vendor Specific,
535 * unless the match record specifies the Vendor ID. */
536 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
537 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
538 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
539 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
540 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
543 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
544 (id->bInterfaceClass != intf->desc.bInterfaceClass))
547 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
548 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
551 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
552 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
557 EXPORT_SYMBOL_GPL(usb_match_one_id);
560 * usb_match_id - find first usb_device_id matching device or interface
561 * @interface: the interface of interest
562 * @id: array of usb_device_id structures, terminated by zero entry
564 * usb_match_id searches an array of usb_device_id's and returns
565 * the first one matching the device or interface, or null.
566 * This is used when binding (or rebinding) a driver to an interface.
567 * Most USB device drivers will use this indirectly, through the usb core,
568 * but some layered driver frameworks use it directly.
569 * These device tables are exported with MODULE_DEVICE_TABLE, through
570 * modutils, to support the driver loading functionality of USB hotplugging.
574 * The "match_flags" element in a usb_device_id controls which
575 * members are used. If the corresponding bit is set, the
576 * value in the device_id must match its corresponding member
577 * in the device or interface descriptor, or else the device_id
580 * "driver_info" is normally used only by device drivers,
581 * but you can create a wildcard "matches anything" usb_device_id
582 * as a driver's "modules.usbmap" entry if you provide an id with
583 * only a nonzero "driver_info" field. If you do this, the USB device
584 * driver's probe() routine should use additional intelligence to
585 * decide whether to bind to the specified interface.
587 * What Makes Good usb_device_id Tables:
589 * The match algorithm is very simple, so that intelligence in
590 * driver selection must come from smart driver id records.
591 * Unless you have good reasons to use another selection policy,
592 * provide match elements only in related groups, and order match
593 * specifiers from specific to general. Use the macros provided
594 * for that purpose if you can.
596 * The most specific match specifiers use device descriptor
597 * data. These are commonly used with product-specific matches;
598 * the USB_DEVICE macro lets you provide vendor and product IDs,
599 * and you can also match against ranges of product revisions.
600 * These are widely used for devices with application or vendor
601 * specific bDeviceClass values.
603 * Matches based on device class/subclass/protocol specifications
604 * are slightly more general; use the USB_DEVICE_INFO macro, or
605 * its siblings. These are used with single-function devices
606 * where bDeviceClass doesn't specify that each interface has
609 * Matches based on interface class/subclass/protocol are the
610 * most general; they let drivers bind to any interface on a
611 * multiple-function device. Use the USB_INTERFACE_INFO
612 * macro, or its siblings, to match class-per-interface style
613 * devices (as recorded in bInterfaceClass).
615 * Note that an entry created by USB_INTERFACE_INFO won't match
616 * any interface if the device class is set to Vendor-Specific.
617 * This is deliberate; according to the USB spec the meanings of
618 * the interface class/subclass/protocol for these devices are also
619 * vendor-specific, and hence matching against a standard product
620 * class wouldn't work anyway. If you really want to use an
621 * interface-based match for such a device, create a match record
622 * that also specifies the vendor ID. (Unforunately there isn't a
623 * standard macro for creating records like this.)
625 * Within those groups, remember that not all combinations are
626 * meaningful. For example, don't give a product version range
627 * without vendor and product IDs; or specify a protocol without
628 * its associated class and subclass.
630 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
631 const struct usb_device_id *id)
633 /* proc_connectinfo in devio.c may call us with id == NULL. */
637 /* It is important to check that id->driver_info is nonzero,
638 since an entry that is all zeroes except for a nonzero
639 id->driver_info is the way to create an entry that
640 indicates that the driver want to examine every
641 device and interface. */
642 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
643 id->bInterfaceClass || id->driver_info; id++) {
644 if (usb_match_one_id(interface, id))
650 EXPORT_SYMBOL_GPL(usb_match_id);
652 static int usb_device_match(struct device *dev, struct device_driver *drv)
654 /* devices and interfaces are handled separately */
655 if (is_usb_device(dev)) {
657 /* interface drivers never match devices */
658 if (!is_usb_device_driver(drv))
661 /* TODO: Add real matching code */
664 } else if (is_usb_interface(dev)) {
665 struct usb_interface *intf;
666 struct usb_driver *usb_drv;
667 const struct usb_device_id *id;
669 /* device drivers never match interfaces */
670 if (is_usb_device_driver(drv))
673 intf = to_usb_interface(dev);
674 usb_drv = to_usb_driver(drv);
676 id = usb_match_id(intf, usb_drv->id_table);
680 id = usb_match_dynamic_id(intf, usb_drv);
688 #ifdef CONFIG_HOTPLUG
689 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
691 struct usb_device *usb_dev;
693 /* driver is often null here; dev_dbg() would oops */
694 pr_debug("usb %s: uevent\n", dev_name(dev));
696 if (is_usb_device(dev)) {
697 usb_dev = to_usb_device(dev);
698 } else if (is_usb_interface(dev)) {
699 struct usb_interface *intf = to_usb_interface(dev);
701 usb_dev = interface_to_usbdev(intf);
706 if (usb_dev->devnum < 0) {
707 pr_debug("usb %s: already deleted?\n", dev_name(dev));
711 pr_debug("usb %s: bus removed?\n", dev_name(dev));
715 #ifdef CONFIG_USB_DEVICEFS
716 /* If this is available, userspace programs can directly read
717 * all the device descriptors we don't tell them about. Or
718 * act as usermode drivers.
720 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
721 usb_dev->bus->busnum, usb_dev->devnum))
725 /* per-device configurations are common */
726 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
727 le16_to_cpu(usb_dev->descriptor.idVendor),
728 le16_to_cpu(usb_dev->descriptor.idProduct),
729 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
732 /* class-based driver binding models */
733 if (add_uevent_var(env, "TYPE=%d/%d/%d",
734 usb_dev->descriptor.bDeviceClass,
735 usb_dev->descriptor.bDeviceSubClass,
736 usb_dev->descriptor.bDeviceProtocol))
744 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
748 #endif /* CONFIG_HOTPLUG */
751 * usb_register_device_driver - register a USB device (not interface) driver
752 * @new_udriver: USB operations for the device driver
753 * @owner: module owner of this driver.
755 * Registers a USB device driver with the USB core. The list of
756 * unattached devices will be rescanned whenever a new driver is
757 * added, allowing the new driver to attach to any recognized devices.
758 * Returns a negative error code on failure and 0 on success.
760 int usb_register_device_driver(struct usb_device_driver *new_udriver,
761 struct module *owner)
768 new_udriver->drvwrap.for_devices = 1;
769 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
770 new_udriver->drvwrap.driver.bus = &usb_bus_type;
771 new_udriver->drvwrap.driver.probe = usb_probe_device;
772 new_udriver->drvwrap.driver.remove = usb_unbind_device;
773 new_udriver->drvwrap.driver.owner = owner;
775 retval = driver_register(&new_udriver->drvwrap.driver);
778 pr_info("%s: registered new device driver %s\n",
779 usbcore_name, new_udriver->name);
780 usbfs_update_special();
782 printk(KERN_ERR "%s: error %d registering device "
784 usbcore_name, retval, new_udriver->name);
789 EXPORT_SYMBOL_GPL(usb_register_device_driver);
792 * usb_deregister_device_driver - unregister a USB device (not interface) driver
793 * @udriver: USB operations of the device driver to unregister
794 * Context: must be able to sleep
796 * Unlinks the specified driver from the internal USB driver list.
798 void usb_deregister_device_driver(struct usb_device_driver *udriver)
800 pr_info("%s: deregistering device driver %s\n",
801 usbcore_name, udriver->name);
803 driver_unregister(&udriver->drvwrap.driver);
804 usbfs_update_special();
806 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
809 * usb_register_driver - register a USB interface driver
810 * @new_driver: USB operations for the interface driver
811 * @owner: module owner of this driver.
812 * @mod_name: module name string
814 * Registers a USB interface driver with the USB core. The list of
815 * unattached interfaces will be rescanned whenever a new driver is
816 * added, allowing the new driver to attach to any recognized interfaces.
817 * Returns a negative error code on failure and 0 on success.
819 * NOTE: if you want your driver to use the USB major number, you must call
820 * usb_register_dev() to enable that functionality. This function no longer
821 * takes care of that.
823 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
824 const char *mod_name)
831 new_driver->drvwrap.for_devices = 0;
832 new_driver->drvwrap.driver.name = (char *) new_driver->name;
833 new_driver->drvwrap.driver.bus = &usb_bus_type;
834 new_driver->drvwrap.driver.probe = usb_probe_interface;
835 new_driver->drvwrap.driver.remove = usb_unbind_interface;
836 new_driver->drvwrap.driver.owner = owner;
837 new_driver->drvwrap.driver.mod_name = mod_name;
838 spin_lock_init(&new_driver->dynids.lock);
839 INIT_LIST_HEAD(&new_driver->dynids.list);
841 retval = driver_register(&new_driver->drvwrap.driver);
845 usbfs_update_special();
847 retval = usb_create_newid_file(new_driver);
851 retval = usb_create_removeid_file(new_driver);
855 pr_info("%s: registered new interface driver %s\n",
856 usbcore_name, new_driver->name);
862 usb_remove_newid_file(new_driver);
864 driver_unregister(&new_driver->drvwrap.driver);
866 printk(KERN_ERR "%s: error %d registering interface "
868 usbcore_name, retval, new_driver->name);
871 EXPORT_SYMBOL_GPL(usb_register_driver);
874 * usb_deregister - unregister a USB interface driver
875 * @driver: USB operations of the interface driver to unregister
876 * Context: must be able to sleep
878 * Unlinks the specified driver from the internal USB driver list.
880 * NOTE: If you called usb_register_dev(), you still need to call
881 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
882 * this * call will no longer do it for you.
884 void usb_deregister(struct usb_driver *driver)
886 pr_info("%s: deregistering interface driver %s\n",
887 usbcore_name, driver->name);
889 usb_remove_removeid_file(driver);
890 usb_remove_newid_file(driver);
891 usb_free_dynids(driver);
892 driver_unregister(&driver->drvwrap.driver);
894 usbfs_update_special();
896 EXPORT_SYMBOL_GPL(usb_deregister);
898 /* Forced unbinding of a USB interface driver, either because
899 * it doesn't support pre_reset/post_reset/reset_resume or
900 * because it doesn't support suspend/resume.
902 * The caller must hold @intf's device's lock, but not its pm_mutex
903 * and not @intf->dev.sem.
905 void usb_forced_unbind_intf(struct usb_interface *intf)
907 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
909 dev_dbg(&intf->dev, "forced unbind\n");
910 usb_driver_release_interface(driver, intf);
912 /* Mark the interface for later rebinding */
913 intf->needs_binding = 1;
916 /* Delayed forced unbinding of a USB interface driver and scan
919 * The caller must hold @intf's device's lock, but not its pm_mutex
920 * and not @intf->dev.sem.
922 * Note: Rebinds will be skipped if a system sleep transition is in
923 * progress and the PM "complete" callback hasn't occurred yet.
925 void usb_rebind_intf(struct usb_interface *intf)
929 /* Delayed unbind of an existing driver */
930 if (intf->dev.driver) {
931 struct usb_driver *driver =
932 to_usb_driver(intf->dev.driver);
934 dev_dbg(&intf->dev, "forced unbind\n");
935 usb_driver_release_interface(driver, intf);
938 /* Try to rebind the interface */
939 if (intf->dev.power.status == DPM_ON) {
940 intf->needs_binding = 0;
941 rc = device_attach(&intf->dev);
943 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
952 /* Unbind drivers for @udev's interfaces that don't support suspend/resume,
953 * or rebind interfaces that have been unbound, according to @action.
955 * The caller must hold @udev's device lock.
957 static void do_unbind_rebind(struct usb_device *udev, int action)
959 struct usb_host_config *config;
961 struct usb_interface *intf;
962 struct usb_driver *drv;
964 config = udev->actconfig;
966 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
967 intf = config->interface[i];
970 if (intf->dev.driver) {
971 drv = to_usb_driver(intf->dev.driver);
972 if (!drv->suspend || !drv->resume)
973 usb_forced_unbind_intf(intf);
977 if (intf->needs_binding)
978 usb_rebind_intf(intf);
985 /* Caller has locked udev's pm_mutex */
986 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
988 struct usb_device_driver *udriver;
991 if (udev->state == USB_STATE_NOTATTACHED ||
992 udev->state == USB_STATE_SUSPENDED)
995 /* For devices that don't have a driver, we do a generic suspend. */
996 if (udev->dev.driver)
997 udriver = to_usb_device_driver(udev->dev.driver);
999 udev->do_remote_wakeup = 0;
1000 udriver = &usb_generic_driver;
1002 status = udriver->suspend(udev, msg);
1005 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1009 /* Caller has locked udev's pm_mutex */
1010 static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1012 struct usb_device_driver *udriver;
1015 if (udev->state == USB_STATE_NOTATTACHED)
1018 /* Can't resume it if it doesn't have a driver. */
1019 if (udev->dev.driver == NULL) {
1024 /* Non-root devices on a full/low-speed bus must wait for their
1025 * companion high-speed root hub, in case a handoff is needed.
1027 if (!(msg.event & PM_EVENT_AUTO) && udev->parent &&
1028 udev->bus->hs_companion)
1029 device_pm_wait_for_dev(&udev->dev,
1030 &udev->bus->hs_companion->root_hub->dev);
1032 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1033 udev->reset_resume = 1;
1035 udriver = to_usb_device_driver(udev->dev.driver);
1036 status = udriver->resume(udev, msg);
1039 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1043 /* Caller has locked intf's usb_device's pm mutex */
1044 static int usb_suspend_interface(struct usb_device *udev,
1045 struct usb_interface *intf, pm_message_t msg)
1047 struct usb_driver *driver;
1050 /* with no hardware, USB interfaces only use FREEZE and ON states */
1051 if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf))
1054 /* This can happen; see usb_driver_release_interface() */
1055 if (intf->condition == USB_INTERFACE_UNBOUND)
1057 driver = to_usb_driver(intf->dev.driver);
1059 if (driver->suspend) {
1060 status = driver->suspend(intf, msg);
1062 mark_quiesced(intf);
1063 else if (!(msg.event & PM_EVENT_AUTO))
1064 dev_err(&intf->dev, "%s error %d\n",
1067 /* Later we will unbind the driver and reprobe */
1068 intf->needs_binding = 1;
1069 dev_warn(&intf->dev, "no %s for driver %s?\n",
1070 "suspend", driver->name);
1071 mark_quiesced(intf);
1075 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1079 /* Caller has locked intf's usb_device's pm_mutex */
1080 static int usb_resume_interface(struct usb_device *udev,
1081 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1083 struct usb_driver *driver;
1086 if (udev->state == USB_STATE_NOTATTACHED || is_active(intf))
1089 /* Don't let autoresume interfere with unbinding */
1090 if (intf->condition == USB_INTERFACE_UNBINDING)
1093 /* Can't resume it if it doesn't have a driver. */
1094 if (intf->condition == USB_INTERFACE_UNBOUND) {
1096 /* Carry out a deferred switch to altsetting 0 */
1097 if (intf->needs_altsetting0 &&
1098 intf->dev.power.status == DPM_ON) {
1099 usb_set_interface(udev, intf->altsetting[0].
1100 desc.bInterfaceNumber, 0);
1101 intf->needs_altsetting0 = 0;
1106 /* Don't resume if the interface is marked for rebinding */
1107 if (intf->needs_binding)
1109 driver = to_usb_driver(intf->dev.driver);
1112 if (driver->reset_resume) {
1113 status = driver->reset_resume(intf);
1115 dev_err(&intf->dev, "%s error %d\n",
1116 "reset_resume", status);
1118 intf->needs_binding = 1;
1119 dev_warn(&intf->dev, "no %s for driver %s?\n",
1120 "reset_resume", driver->name);
1123 if (driver->resume) {
1124 status = driver->resume(intf);
1126 dev_err(&intf->dev, "%s error %d\n",
1129 intf->needs_binding = 1;
1130 dev_warn(&intf->dev, "no %s for driver %s?\n",
1131 "resume", driver->name);
1136 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1137 if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
1140 /* Later we will unbind the driver and/or reprobe, if necessary */
1144 #ifdef CONFIG_USB_SUSPEND
1146 /* Internal routine to check whether we may autosuspend a device. */
1147 static int autosuspend_check(struct usb_device *udev, int reschedule)
1150 struct usb_interface *intf;
1151 unsigned long suspend_time, j;
1153 /* For autosuspend, fail fast if anything is in use or autosuspend
1154 * is disabled. Also fail if any interfaces require remote wakeup
1155 * but it isn't available.
1157 if (udev->pm_usage_cnt > 0)
1159 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
1162 suspend_time = udev->last_busy + udev->autosuspend_delay;
1163 if (udev->actconfig) {
1164 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1165 intf = udev->actconfig->interface[i];
1166 if (!is_active(intf))
1168 if (atomic_read(&intf->pm_usage_cnt) > 0)
1170 if (intf->needs_remote_wakeup &&
1171 !udev->do_remote_wakeup) {
1172 dev_dbg(&udev->dev, "remote wakeup needed "
1173 "for autosuspend\n");
1177 /* Don't allow autosuspend if the device will need
1178 * a reset-resume and any of its interface drivers
1179 * doesn't include support.
1181 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1182 struct usb_driver *driver;
1184 driver = to_usb_driver(intf->dev.driver);
1185 if (!driver->reset_resume ||
1186 intf->needs_remote_wakeup)
1192 /* If everything is okay but the device hasn't been idle for long
1193 * enough, queue a delayed autosuspend request. If the device
1194 * _has_ been idle for long enough and the reschedule flag is set,
1195 * likewise queue a delayed (1 second) autosuspend request.
1198 if (time_before(j, suspend_time))
1201 suspend_time = j + HZ;
1203 if (!timer_pending(&udev->autosuspend.timer)) {
1204 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1205 round_jiffies_up_relative(suspend_time - j));
1214 static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1219 #endif /* CONFIG_USB_SUSPEND */
1222 * usb_suspend_both - suspend a USB device and its interfaces
1223 * @udev: the usb_device to suspend
1224 * @msg: Power Management message describing this state transition
1226 * This is the central routine for suspending USB devices. It calls the
1227 * suspend methods for all the interface drivers in @udev and then calls
1228 * the suspend method for @udev itself. If an error occurs at any stage,
1229 * all the interfaces which were suspended are resumed so that they remain
1230 * in the same state as the device.
1232 * If an autosuspend is in progress the routine checks first to make sure
1233 * that neither the device itself or any of its active interfaces is in use
1234 * (pm_usage_cnt is greater than 0). If they are, the autosuspend fails.
1236 * If the suspend succeeds, the routine recursively queues an autosuspend
1237 * request for @udev's parent device, thereby propagating the change up
1238 * the device tree. If all of the parent's children are now suspended,
1239 * the parent will autosuspend in turn.
1241 * The suspend method calls are subject to mutual exclusion under control
1242 * of @udev's pm_mutex. Many of these calls are also under the protection
1243 * of @udev's device lock (including all requests originating outside the
1244 * USB subsystem), but autosuspend requests generated by a child device or
1245 * interface driver may not be. Usbcore will insure that the method calls
1246 * do not arrive during bind, unbind, or reset operations. However, drivers
1247 * must be prepared to handle suspend calls arriving at unpredictable times.
1248 * The only way to block such calls is to do an autoresume (preventing
1249 * autosuspends) while holding @udev's device lock (preventing outside
1252 * The caller must hold @udev->pm_mutex.
1254 * This routine can run only in process context.
1256 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1260 struct usb_interface *intf;
1261 struct usb_device *parent = udev->parent;
1263 if (udev->state == USB_STATE_NOTATTACHED ||
1264 udev->state == USB_STATE_SUSPENDED)
1267 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1269 if (msg.event & PM_EVENT_AUTO) {
1270 status = autosuspend_check(udev, 0);
1275 /* Suspend all the interfaces and then udev itself */
1276 if (udev->actconfig) {
1277 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1278 intf = udev->actconfig->interface[i];
1279 status = usb_suspend_interface(udev, intf, msg);
1285 status = usb_suspend_device(udev, msg);
1287 /* If the suspend failed, resume interfaces that did get suspended */
1291 msg2.event = msg.event ^ (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1293 intf = udev->actconfig->interface[i];
1294 usb_resume_interface(udev, intf, msg2, 0);
1297 /* Try another autosuspend when the interfaces aren't busy */
1298 if (msg.event & PM_EVENT_AUTO)
1299 autosuspend_check(udev, status == -EBUSY);
1301 /* If the suspend succeeded then prevent any more URB submissions,
1302 * flush any outstanding URBs, and propagate the suspend up the tree.
1305 cancel_delayed_work(&udev->autosuspend);
1306 udev->can_submit = 0;
1307 for (i = 0; i < 16; ++i) {
1308 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1309 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1312 /* If this is just a FREEZE or a PRETHAW, udev might
1313 * not really be suspended. Only true suspends get
1314 * propagated up the device tree.
1316 if (parent && udev->state == USB_STATE_SUSPENDED)
1317 usb_autosuspend_device(parent);
1321 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1326 * usb_resume_both - resume a USB device and its interfaces
1327 * @udev: the usb_device to resume
1328 * @msg: Power Management message describing this state transition
1330 * This is the central routine for resuming USB devices. It calls the
1331 * the resume method for @udev and then calls the resume methods for all
1332 * the interface drivers in @udev.
1334 * Before starting the resume, the routine calls itself recursively for
1335 * the parent device of @udev, thereby propagating the change up the device
1336 * tree and assuring that @udev will be able to resume. If the parent is
1337 * unable to resume successfully, the routine fails.
1339 * The resume method calls are subject to mutual exclusion under control
1340 * of @udev's pm_mutex. Many of these calls are also under the protection
1341 * of @udev's device lock (including all requests originating outside the
1342 * USB subsystem), but autoresume requests generated by a child device or
1343 * interface driver may not be. Usbcore will insure that the method calls
1344 * do not arrive during bind, unbind, or reset operations. However, drivers
1345 * must be prepared to handle resume calls arriving at unpredictable times.
1346 * The only way to block such calls is to do an autoresume (preventing
1347 * other autoresumes) while holding @udev's device lock (preventing outside
1350 * The caller must hold @udev->pm_mutex.
1352 * This routine can run only in process context.
1354 static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1358 struct usb_interface *intf;
1359 struct usb_device *parent = udev->parent;
1361 cancel_delayed_work(&udev->autosuspend);
1362 if (udev->state == USB_STATE_NOTATTACHED) {
1366 udev->can_submit = 1;
1368 /* Propagate the resume up the tree, if necessary */
1369 if (udev->state == USB_STATE_SUSPENDED) {
1371 status = usb_autoresume_device(parent);
1373 status = usb_resume_device(udev, msg);
1374 if (status || udev->state ==
1375 USB_STATE_NOTATTACHED) {
1376 usb_autosuspend_device(parent);
1378 /* It's possible usb_resume_device()
1379 * failed after the port was
1380 * unsuspended, causing udev to be
1381 * logically disconnected. We don't
1382 * want usb_disconnect() to autosuspend
1383 * the parent again, so tell it that
1384 * udev disconnected while still
1387 USB_STATE_NOTATTACHED)
1388 udev->discon_suspended = 1;
1393 /* We can't progagate beyond the USB subsystem,
1394 * so if a root hub's controller is suspended
1395 * then we're stuck. */
1396 status = usb_resume_device(udev, msg);
1398 } else if (udev->reset_resume)
1399 status = usb_resume_device(udev, msg);
1401 if (status == 0 && udev->actconfig) {
1402 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1403 intf = udev->actconfig->interface[i];
1404 usb_resume_interface(udev, intf, msg,
1405 udev->reset_resume);
1410 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1412 udev->reset_resume = 0;
1417 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1418 * @udev: the usb_device to suspend
1419 * @msg: Power Management message describing this state transition
1421 * This routine handles external suspend requests: ones not generated
1422 * internally by a USB driver (autosuspend) but rather coming from the user
1423 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1424 * out regardless of @udev's usage counter or those of its interfaces,
1425 * and regardless of whether or not remote wakeup is enabled. Of course,
1426 * interface drivers still have the option of failing the suspend (if
1427 * there are unsuspended children, for example).
1429 * The caller must hold @udev's device lock.
1431 int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1435 do_unbind_rebind(udev, DO_UNBIND);
1437 status = usb_suspend_both(udev, msg);
1438 usb_pm_unlock(udev);
1443 * usb_external_resume_device - external resume of a USB device and its interfaces
1444 * @udev: the usb_device to resume
1445 * @msg: Power Management message describing this state transition
1447 * This routine handles external resume requests: ones not generated
1448 * internally by a USB driver (autoresume) but rather coming from the user
1449 * (via sysfs), the PM core (system resume), or the device itself (remote
1450 * wakeup). @udev's usage counter is unaffected.
1452 * The caller must hold @udev's device lock.
1454 int usb_external_resume_device(struct usb_device *udev, pm_message_t msg)
1459 status = usb_resume_both(udev, msg);
1460 udev->last_busy = jiffies;
1461 usb_pm_unlock(udev);
1463 do_unbind_rebind(udev, DO_REBIND);
1465 /* Now that the device is awake, we can start trying to autosuspend
1468 usb_try_autosuspend_device(udev);
1472 int usb_suspend(struct device *dev, pm_message_t msg)
1474 struct usb_device *udev;
1476 udev = to_usb_device(dev);
1478 /* If udev is already suspended, we can skip this suspend and
1479 * we should also skip the upcoming system resume. High-speed
1480 * root hubs are an exception; they need to resume whenever the
1481 * system wakes up in order for USB-PERSIST port handover to work
1484 if (udev->state == USB_STATE_SUSPENDED) {
1485 if (udev->parent || udev->speed != USB_SPEED_HIGH)
1486 udev->skip_sys_resume = 1;
1490 udev->skip_sys_resume = 0;
1491 return usb_external_suspend_device(udev, msg);
1494 int usb_resume(struct device *dev, pm_message_t msg)
1496 struct usb_device *udev;
1499 udev = to_usb_device(dev);
1501 /* If udev->skip_sys_resume is set then udev was already suspended
1502 * when the system sleep started, so we don't want to resume it
1503 * during this system wakeup.
1505 if (udev->skip_sys_resume)
1507 status = usb_external_resume_device(udev, msg);
1509 /* Avoid PM error messages for devices disconnected while suspended
1510 * as we'll display regular disconnect messages just a bit later.
1512 if (status == -ENODEV)
1517 #endif /* CONFIG_PM */
1519 #ifdef CONFIG_USB_SUSPEND
1522 * usb_enable_autosuspend - allow a USB device to be autosuspended
1523 * @udev: the USB device which may be autosuspended
1525 * This routine allows @udev to be autosuspended. An autosuspend won't
1526 * take place until the autosuspend_delay has elapsed and all the other
1527 * necessary conditions are satisfied.
1529 * The caller must hold @udev's device lock.
1531 int usb_enable_autosuspend(struct usb_device *udev)
1533 if (udev->autosuspend_disabled) {
1534 udev->autosuspend_disabled = 0;
1535 usb_autosuspend_device(udev);
1539 EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1542 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1543 * @udev: the USB device which may not be autosuspended
1545 * This routine prevents @udev from being autosuspended and wakes it up
1546 * if it is already autosuspended.
1548 * The caller must hold @udev's device lock.
1550 int usb_disable_autosuspend(struct usb_device *udev)
1554 if (!udev->autosuspend_disabled) {
1555 rc = usb_autoresume_device(udev);
1557 udev->autosuspend_disabled = 1;
1561 EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1563 /* Internal routine to adjust a device's usage counter and change
1564 * its autosuspend state.
1566 static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1571 udev->pm_usage_cnt += inc_usage_cnt;
1572 WARN_ON(udev->pm_usage_cnt < 0);
1574 udev->last_busy = jiffies;
1575 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1576 if (udev->state == USB_STATE_SUSPENDED)
1577 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1579 udev->pm_usage_cnt -= inc_usage_cnt;
1580 else if (inc_usage_cnt)
1581 udev->last_busy = jiffies;
1582 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1583 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1585 usb_pm_unlock(udev);
1589 /* usb_autosuspend_work - callback routine to autosuspend a USB device */
1590 void usb_autosuspend_work(struct work_struct *work)
1592 struct usb_device *udev =
1593 container_of(work, struct usb_device, autosuspend.work);
1595 usb_autopm_do_device(udev, 0);
1598 /* usb_autoresume_work - callback routine to autoresume a USB device */
1599 void usb_autoresume_work(struct work_struct *work)
1601 struct usb_device *udev =
1602 container_of(work, struct usb_device, autoresume);
1604 /* Wake it up, let the drivers do their thing, and then put it
1607 if (usb_autopm_do_device(udev, 1) == 0)
1608 usb_autopm_do_device(udev, -1);
1612 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1613 * @udev: the usb_device to autosuspend
1615 * This routine should be called when a core subsystem is finished using
1616 * @udev and wants to allow it to autosuspend. Examples would be when
1617 * @udev's device file in usbfs is closed or after a configuration change.
1619 * @udev's usage counter is decremented. If it or any of the usage counters
1620 * for an active interface is greater than 0, no autosuspend request will be
1621 * queued. (If an interface driver does not support autosuspend then its
1622 * usage counter is permanently positive.) Furthermore, if an interface
1623 * driver requires remote-wakeup capability during autosuspend but remote
1624 * wakeup is disabled, the autosuspend will fail.
1626 * The caller must hold @udev's device lock.
1628 * This routine can run only in process context.
1630 void usb_autosuspend_device(struct usb_device *udev)
1634 status = usb_autopm_do_device(udev, -1);
1635 dev_vdbg(&udev->dev, "%s: cnt %d\n",
1636 __func__, udev->pm_usage_cnt);
1640 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1641 * @udev: the usb_device to autosuspend
1643 * This routine should be called when a core subsystem thinks @udev may
1644 * be ready to autosuspend.
1646 * @udev's usage counter left unchanged. If it or any of the usage counters
1647 * for an active interface is greater than 0, or autosuspend is not allowed
1648 * for any other reason, no autosuspend request will be queued.
1650 * The caller must hold @udev's device lock.
1652 * This routine can run only in process context.
1654 void usb_try_autosuspend_device(struct usb_device *udev)
1656 usb_autopm_do_device(udev, 0);
1657 dev_vdbg(&udev->dev, "%s: cnt %d\n",
1658 __func__, udev->pm_usage_cnt);
1662 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1663 * @udev: the usb_device to autoresume
1665 * This routine should be called when a core subsystem wants to use @udev
1666 * and needs to guarantee that it is not suspended. No autosuspend will
1667 * occur until usb_autosuspend_device is called. (Note that this will not
1668 * prevent suspend events originating in the PM core.) Examples would be
1669 * when @udev's device file in usbfs is opened or when a remote-wakeup
1670 * request is received.
1672 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1673 * However if the autoresume fails then the usage counter is re-decremented.
1675 * The caller must hold @udev's device lock.
1677 * This routine can run only in process context.
1679 int usb_autoresume_device(struct usb_device *udev)
1683 status = usb_autopm_do_device(udev, 1);
1684 dev_vdbg(&udev->dev, "%s: status %d cnt %d\n",
1685 __func__, status, udev->pm_usage_cnt);
1689 /* Internal routine to adjust an interface's usage counter and change
1690 * its device's autosuspend state.
1692 static int usb_autopm_do_interface(struct usb_interface *intf,
1695 struct usb_device *udev = interface_to_usbdev(intf);
1699 if (intf->condition == USB_INTERFACE_UNBOUND)
1702 atomic_add(inc_usage_cnt, &intf->pm_usage_cnt);
1703 udev->last_busy = jiffies;
1704 if (inc_usage_cnt >= 0 &&
1705 atomic_read(&intf->pm_usage_cnt) > 0) {
1706 if (udev->state == USB_STATE_SUSPENDED)
1707 status = usb_resume_both(udev,
1710 atomic_sub(inc_usage_cnt, &intf->pm_usage_cnt);
1712 udev->last_busy = jiffies;
1713 } else if (inc_usage_cnt <= 0 &&
1714 atomic_read(&intf->pm_usage_cnt) <= 0) {
1715 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1718 usb_pm_unlock(udev);
1723 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1724 * @intf: the usb_interface whose counter should be decremented
1726 * This routine should be called by an interface driver when it is
1727 * finished using @intf and wants to allow it to autosuspend. A typical
1728 * example would be a character-device driver when its device file is
1731 * The routine decrements @intf's usage counter. When the counter reaches
1732 * 0, a delayed autosuspend request for @intf's device is queued. When
1733 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1734 * the other usage counters for the sibling interfaces and @intf's
1735 * usb_device, the device and all its interfaces will be autosuspended.
1737 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1738 * core will not change its value other than the increment and decrement
1739 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1740 * may use this simple counter-oriented discipline or may set the value
1743 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1744 * take place only if the device's remote-wakeup facility is enabled.
1746 * Suspend method calls queued by this routine can arrive at any time
1747 * while @intf is resumed and its usage counter is equal to 0. They are
1748 * not protected by the usb_device's lock but only by its pm_mutex.
1749 * Drivers must provide their own synchronization.
1751 * This routine can run only in process context.
1753 void usb_autopm_put_interface(struct usb_interface *intf)
1757 status = usb_autopm_do_interface(intf, -1);
1758 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1759 __func__, status, atomic_read(&intf->pm_usage_cnt));
1761 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1764 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1765 * @intf: the usb_interface whose counter should be decremented
1767 * This routine does essentially the same thing as
1768 * usb_autopm_put_interface(): it decrements @intf's usage counter and
1769 * queues a delayed autosuspend request if the counter is <= 0. The
1770 * difference is that it does not acquire the device's pm_mutex;
1771 * callers must handle all synchronization issues themselves.
1773 * Typically a driver would call this routine during an URB's completion
1774 * handler, if no more URBs were pending.
1776 * This routine can run in atomic context.
1778 void usb_autopm_put_interface_async(struct usb_interface *intf)
1780 struct usb_device *udev = interface_to_usbdev(intf);
1783 if (intf->condition == USB_INTERFACE_UNBOUND) {
1786 udev->last_busy = jiffies;
1787 atomic_dec(&intf->pm_usage_cnt);
1788 if (udev->autosuspend_disabled || udev->autosuspend_delay < 0)
1790 else if (atomic_read(&intf->pm_usage_cnt) <= 0 &&
1791 !timer_pending(&udev->autosuspend.timer)) {
1792 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1793 round_jiffies_up_relative(
1794 udev->autosuspend_delay));
1797 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1798 __func__, status, atomic_read(&intf->pm_usage_cnt));
1800 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1803 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1804 * @intf: the usb_interface whose counter should be incremented
1806 * This routine should be called by an interface driver when it wants to
1807 * use @intf and needs to guarantee that it is not suspended. In addition,
1808 * the routine prevents @intf from being autosuspended subsequently. (Note
1809 * that this will not prevent suspend events originating in the PM core.)
1810 * This prevention will persist until usb_autopm_put_interface() is called
1811 * or @intf is unbound. A typical example would be a character-device
1812 * driver when its device file is opened.
1815 * The routine increments @intf's usage counter. (However if the
1816 * autoresume fails then the counter is re-decremented.) So long as the
1817 * counter is greater than 0, autosuspend will not be allowed for @intf
1818 * or its usb_device. When the driver is finished using @intf it should
1819 * call usb_autopm_put_interface() to decrement the usage counter and
1820 * queue a delayed autosuspend request (if the counter is <= 0).
1823 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1824 * core will not change its value other than the increment and decrement
1825 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1826 * may use this simple counter-oriented discipline or may set the value
1829 * Resume method calls generated by this routine can arrive at any time
1830 * while @intf is suspended. They are not protected by the usb_device's
1831 * lock but only by its pm_mutex. Drivers must provide their own
1834 * This routine can run only in process context.
1836 int usb_autopm_get_interface(struct usb_interface *intf)
1840 status = usb_autopm_do_interface(intf, 1);
1841 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1842 __func__, status, atomic_read(&intf->pm_usage_cnt));
1845 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1848 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1849 * @intf: the usb_interface whose counter should be incremented
1851 * This routine does much the same thing as
1852 * usb_autopm_get_interface(): it increments @intf's usage counter and
1853 * queues an autoresume request if the result is > 0. The differences
1854 * are that it does not acquire the device's pm_mutex (callers must
1855 * handle all synchronization issues themselves), and it does not
1856 * autoresume the device directly (it only queues a request). After a
1857 * successful call, the device will generally not yet be resumed.
1859 * This routine can run in atomic context.
1861 int usb_autopm_get_interface_async(struct usb_interface *intf)
1863 struct usb_device *udev = interface_to_usbdev(intf);
1866 if (intf->condition == USB_INTERFACE_UNBOUND)
1869 atomic_inc(&intf->pm_usage_cnt);
1870 if (atomic_read(&intf->pm_usage_cnt) > 0 &&
1871 udev->state == USB_STATE_SUSPENDED)
1872 queue_work(ksuspend_usb_wq, &udev->autoresume);
1874 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1875 __func__, status, atomic_read(&intf->pm_usage_cnt));
1878 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1882 void usb_autosuspend_work(struct work_struct *work)
1885 void usb_autoresume_work(struct work_struct *work)
1888 #endif /* CONFIG_USB_SUSPEND */
1890 struct bus_type usb_bus_type = {
1892 .match = usb_device_match,
1893 .uevent = usb_uevent,