2 * drivers/usb/driver.c - most of the driver model stuff for usb
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
6 * based on drivers/usb/usb.c which had the following copyrights:
7 * (C) Copyright Linus Torvalds 1999
8 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Andreas Gal 1999
10 * (C) Copyright Gregory P. Smith 1999
11 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
12 * (C) Copyright Randy Dunlap 2000
13 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Yggdrasil Computing, Inc. 2000
15 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright Greg Kroah-Hartman 2002-2003
18 * NOTE! This is not actually a driver at all, rather this is
19 * just a collection of helper routines that implement the
20 * matching, probing, releasing, suspending and resuming for
25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <linux/usb.h>
28 #include <linux/usb/quirks.h>
29 #include <linux/pm_runtime.h>
37 * Adds a new dynamic USBdevice ID to this driver,
38 * and cause the driver to probe for all devices again.
40 ssize_t
usb_store_new_id(struct usb_dynids
*dynids
,
41 struct device_driver
*driver
,
42 const char *buf
, size_t count
)
44 struct usb_dynid
*dynid
;
50 fields
= sscanf(buf
, "%x %x", &idVendor
, &idProduct
);
54 dynid
= kzalloc(sizeof(*dynid
), GFP_KERNEL
);
58 INIT_LIST_HEAD(&dynid
->node
);
59 dynid
->id
.idVendor
= idVendor
;
60 dynid
->id
.idProduct
= idProduct
;
61 dynid
->id
.match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
63 spin_lock(&dynids
->lock
);
64 list_add_tail(&dynid
->node
, &dynids
->list
);
65 spin_unlock(&dynids
->lock
);
67 if (get_driver(driver
)) {
68 retval
= driver_attach(driver
);
76 EXPORT_SYMBOL_GPL(usb_store_new_id
);
78 static ssize_t
store_new_id(struct device_driver
*driver
,
79 const char *buf
, size_t count
)
81 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
83 return usb_store_new_id(&usb_drv
->dynids
, driver
, buf
, count
);
85 static DRIVER_ATTR(new_id
, S_IWUSR
, NULL
, store_new_id
);
88 * store_remove_id - remove a USB device ID from this driver
89 * @driver: target device driver
90 * @buf: buffer for scanning device ID data
93 * Removes a dynamic usb device ID from this driver.
96 store_remove_id(struct device_driver
*driver
, const char *buf
, size_t count
)
98 struct usb_dynid
*dynid
, *n
;
99 struct usb_driver
*usb_driver
= to_usb_driver(driver
);
105 fields
= sscanf(buf
, "%x %x", &idVendor
, &idProduct
);
109 spin_lock(&usb_driver
->dynids
.lock
);
110 list_for_each_entry_safe(dynid
, n
, &usb_driver
->dynids
.list
, node
) {
111 struct usb_device_id
*id
= &dynid
->id
;
112 if ((id
->idVendor
== idVendor
) &&
113 (id
->idProduct
== idProduct
)) {
114 list_del(&dynid
->node
);
120 spin_unlock(&usb_driver
->dynids
.lock
);
126 static DRIVER_ATTR(remove_id
, S_IWUSR
, NULL
, store_remove_id
);
128 static int usb_create_newid_file(struct usb_driver
*usb_drv
)
132 if (usb_drv
->no_dynamic_id
)
135 if (usb_drv
->probe
!= NULL
)
136 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
137 &driver_attr_new_id
);
142 static void usb_remove_newid_file(struct usb_driver
*usb_drv
)
144 if (usb_drv
->no_dynamic_id
)
147 if (usb_drv
->probe
!= NULL
)
148 driver_remove_file(&usb_drv
->drvwrap
.driver
,
149 &driver_attr_new_id
);
153 usb_create_removeid_file(struct usb_driver
*drv
)
156 if (drv
->probe
!= NULL
)
157 error
= driver_create_file(&drv
->drvwrap
.driver
,
158 &driver_attr_remove_id
);
162 static void usb_remove_removeid_file(struct usb_driver
*drv
)
164 driver_remove_file(&drv
->drvwrap
.driver
, &driver_attr_remove_id
);
167 static void usb_free_dynids(struct usb_driver
*usb_drv
)
169 struct usb_dynid
*dynid
, *n
;
171 spin_lock(&usb_drv
->dynids
.lock
);
172 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
173 list_del(&dynid
->node
);
176 spin_unlock(&usb_drv
->dynids
.lock
);
179 static inline int usb_create_newid_file(struct usb_driver
*usb_drv
)
184 static void usb_remove_newid_file(struct usb_driver
*usb_drv
)
189 usb_create_removeid_file(struct usb_driver
*drv
)
194 static void usb_remove_removeid_file(struct usb_driver
*drv
)
198 static inline void usb_free_dynids(struct usb_driver
*usb_drv
)
203 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
204 struct usb_driver
*drv
)
206 struct usb_dynid
*dynid
;
208 spin_lock(&drv
->dynids
.lock
);
209 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
210 if (usb_match_one_id(intf
, &dynid
->id
)) {
211 spin_unlock(&drv
->dynids
.lock
);
215 spin_unlock(&drv
->dynids
.lock
);
220 /* called from driver core with dev locked */
221 static int usb_probe_device(struct device
*dev
)
223 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
224 struct usb_device
*udev
= to_usb_device(dev
);
227 dev_dbg(dev
, "%s\n", __func__
);
229 /* TODO: Add real matching code */
231 /* The device should always appear to be in use
232 * unless the driver suports autosuspend.
234 if (!udriver
->supports_autosuspend
)
235 error
= usb_autoresume_device(udev
);
238 error
= udriver
->probe(udev
);
242 /* called from driver core with dev locked */
243 static int usb_unbind_device(struct device
*dev
)
245 struct usb_device
*udev
= to_usb_device(dev
);
246 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
248 udriver
->disconnect(udev
);
249 if (!udriver
->supports_autosuspend
)
250 usb_autosuspend_device(udev
);
255 * Cancel any pending scheduled resets
257 * [see usb_queue_reset_device()]
259 * Called after unconfiguring / when releasing interfaces. See
260 * comments in __usb_queue_reset_device() regarding
261 * udev->reset_running.
263 static void usb_cancel_queued_reset(struct usb_interface
*iface
)
265 if (iface
->reset_running
== 0)
266 cancel_work_sync(&iface
->reset_ws
);
269 /* called from driver core with dev locked */
270 static int usb_probe_interface(struct device
*dev
)
272 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
273 struct usb_interface
*intf
= to_usb_interface(dev
);
274 struct usb_device
*udev
= interface_to_usbdev(intf
);
275 const struct usb_device_id
*id
;
278 dev_dbg(dev
, "%s\n", __func__
);
280 intf
->needs_binding
= 0;
282 if (usb_device_is_owned(udev
))
285 if (udev
->authorized
== 0) {
286 dev_err(&intf
->dev
, "Device is not authorized for usage\n");
290 id
= usb_match_id(intf
, driver
->id_table
);
292 id
= usb_match_dynamic_id(intf
, driver
);
296 dev_dbg(dev
, "%s - got id\n", __func__
);
298 error
= usb_autoresume_device(udev
);
302 intf
->condition
= USB_INTERFACE_BINDING
;
304 /* Bound interfaces are initially active. They are
305 * runtime-PM-enabled only if the driver has autosuspend support.
306 * They are sensitive to their children's power states.
308 pm_runtime_set_active(dev
);
309 pm_suspend_ignore_children(dev
, false);
310 if (driver
->supports_autosuspend
)
311 pm_runtime_enable(dev
);
313 /* Carry out a deferred switch to altsetting 0 */
314 if (intf
->needs_altsetting0
) {
315 error
= usb_set_interface(udev
, intf
->altsetting
[0].
316 desc
.bInterfaceNumber
, 0);
319 intf
->needs_altsetting0
= 0;
322 error
= driver
->probe(intf
, id
);
326 intf
->condition
= USB_INTERFACE_BOUND
;
327 usb_autosuspend_device(udev
);
331 intf
->needs_remote_wakeup
= 0;
332 intf
->condition
= USB_INTERFACE_UNBOUND
;
333 usb_cancel_queued_reset(intf
);
335 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
336 pm_runtime_disable(dev
);
337 pm_runtime_set_suspended(dev
);
339 usb_autosuspend_device(udev
);
343 /* called from driver core with dev locked */
344 static int usb_unbind_interface(struct device
*dev
)
346 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
347 struct usb_interface
*intf
= to_usb_interface(dev
);
348 struct usb_device
*udev
;
351 intf
->condition
= USB_INTERFACE_UNBINDING
;
353 /* Autoresume for set_interface call below */
354 udev
= interface_to_usbdev(intf
);
355 error
= usb_autoresume_device(udev
);
357 /* Terminate all URBs for this interface unless the driver
358 * supports "soft" unbinding.
360 if (!driver
->soft_unbind
)
361 usb_disable_interface(udev
, intf
, false);
363 driver
->disconnect(intf
);
364 usb_cancel_queued_reset(intf
);
366 /* Reset other interface state.
367 * We cannot do a Set-Interface if the device is suspended or
368 * if it is prepared for a system sleep (since installing a new
369 * altsetting means creating new endpoint device entries).
370 * When either of these happens, defer the Set-Interface.
372 if (intf
->cur_altsetting
->desc
.bAlternateSetting
== 0) {
373 /* Already in altsetting 0 so skip Set-Interface.
374 * Just re-enable it without affecting the endpoint toggles.
376 usb_enable_interface(udev
, intf
, false);
377 } else if (!error
&& intf
->dev
.power
.status
== DPM_ON
) {
378 r
= usb_set_interface(udev
, intf
->altsetting
[0].
379 desc
.bInterfaceNumber
, 0);
381 intf
->needs_altsetting0
= 1;
383 intf
->needs_altsetting0
= 1;
385 usb_set_intfdata(intf
, NULL
);
387 intf
->condition
= USB_INTERFACE_UNBOUND
;
388 intf
->needs_remote_wakeup
= 0;
390 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
391 pm_runtime_disable(dev
);
392 pm_runtime_set_suspended(dev
);
394 /* Undo any residual pm_autopm_get_interface_* calls */
395 for (r
= atomic_read(&intf
->pm_usage_cnt
); r
> 0; --r
)
396 usb_autopm_put_interface_no_suspend(intf
);
397 atomic_set(&intf
->pm_usage_cnt
, 0);
400 usb_autosuspend_device(udev
);
406 * usb_driver_claim_interface - bind a driver to an interface
407 * @driver: the driver to be bound
408 * @iface: the interface to which it will be bound; must be in the
409 * usb device's active configuration
410 * @priv: driver data associated with that interface
412 * This is used by usb device drivers that need to claim more than one
413 * interface on a device when probing (audio and acm are current examples).
414 * No device driver should directly modify internal usb_interface or
415 * usb_device structure members.
417 * Few drivers should need to use this routine, since the most natural
418 * way to bind to an interface is to return the private data from
419 * the driver's probe() method.
421 * Callers must own the device lock, so driver probe() entries don't need
422 * extra locking, but other call contexts may need to explicitly claim that
425 int usb_driver_claim_interface(struct usb_driver
*driver
,
426 struct usb_interface
*iface
, void *priv
)
428 struct device
*dev
= &iface
->dev
;
434 dev
->driver
= &driver
->drvwrap
.driver
;
435 usb_set_intfdata(iface
, priv
);
436 iface
->needs_binding
= 0;
438 iface
->condition
= USB_INTERFACE_BOUND
;
440 /* Bound interfaces are initially active. They are
441 * runtime-PM-enabled only if the driver has autosuspend support.
442 * They are sensitive to their children's power states.
444 pm_runtime_set_active(dev
);
445 pm_suspend_ignore_children(dev
, false);
446 if (driver
->supports_autosuspend
)
447 pm_runtime_enable(dev
);
449 /* if interface was already added, bind now; else let
450 * the future device_add() bind it, bypassing probe()
452 if (device_is_registered(dev
))
453 retval
= device_bind_driver(dev
);
457 EXPORT_SYMBOL_GPL(usb_driver_claim_interface
);
460 * usb_driver_release_interface - unbind a driver from an interface
461 * @driver: the driver to be unbound
462 * @iface: the interface from which it will be unbound
464 * This can be used by drivers to release an interface without waiting
465 * for their disconnect() methods to be called. In typical cases this
466 * also causes the driver disconnect() method to be called.
468 * This call is synchronous, and may not be used in an interrupt context.
469 * Callers must own the device lock, so driver disconnect() entries don't
470 * need extra locking, but other call contexts may need to explicitly claim
473 void usb_driver_release_interface(struct usb_driver
*driver
,
474 struct usb_interface
*iface
)
476 struct device
*dev
= &iface
->dev
;
478 /* this should never happen, don't release something that's not ours */
479 if (!dev
->driver
|| dev
->driver
!= &driver
->drvwrap
.driver
)
482 /* don't release from within disconnect() */
483 if (iface
->condition
!= USB_INTERFACE_BOUND
)
485 iface
->condition
= USB_INTERFACE_UNBINDING
;
487 /* Release via the driver core only if the interface
488 * has already been registered
490 if (device_is_registered(dev
)) {
491 device_release_driver(dev
);
494 usb_unbind_interface(dev
);
499 EXPORT_SYMBOL_GPL(usb_driver_release_interface
);
501 /* returns 0 if no match, 1 if match */
502 int usb_match_device(struct usb_device
*dev
, const struct usb_device_id
*id
)
504 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
505 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
508 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
509 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
512 /* No need to test id->bcdDevice_lo != 0, since 0 is never
513 greater than any unsigned number. */
514 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
515 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
518 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
519 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
522 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
523 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
526 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
527 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
530 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
531 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
537 /* returns 0 if no match, 1 if match */
538 int usb_match_one_id(struct usb_interface
*interface
,
539 const struct usb_device_id
*id
)
541 struct usb_host_interface
*intf
;
542 struct usb_device
*dev
;
544 /* proc_connectinfo in devio.c may call us with id == NULL. */
548 intf
= interface
->cur_altsetting
;
549 dev
= interface_to_usbdev(interface
);
551 if (!usb_match_device(dev
, id
))
554 /* The interface class, subclass, and protocol should never be
555 * checked for a match if the device class is Vendor Specific,
556 * unless the match record specifies the Vendor ID. */
557 if (dev
->descriptor
.bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
558 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
559 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
560 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
561 USB_DEVICE_ID_MATCH_INT_PROTOCOL
)))
564 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
565 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
568 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
569 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
572 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
573 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
578 EXPORT_SYMBOL_GPL(usb_match_one_id
);
581 * usb_match_id - find first usb_device_id matching device or interface
582 * @interface: the interface of interest
583 * @id: array of usb_device_id structures, terminated by zero entry
585 * usb_match_id searches an array of usb_device_id's and returns
586 * the first one matching the device or interface, or null.
587 * This is used when binding (or rebinding) a driver to an interface.
588 * Most USB device drivers will use this indirectly, through the usb core,
589 * but some layered driver frameworks use it directly.
590 * These device tables are exported with MODULE_DEVICE_TABLE, through
591 * modutils, to support the driver loading functionality of USB hotplugging.
595 * The "match_flags" element in a usb_device_id controls which
596 * members are used. If the corresponding bit is set, the
597 * value in the device_id must match its corresponding member
598 * in the device or interface descriptor, or else the device_id
601 * "driver_info" is normally used only by device drivers,
602 * but you can create a wildcard "matches anything" usb_device_id
603 * as a driver's "modules.usbmap" entry if you provide an id with
604 * only a nonzero "driver_info" field. If you do this, the USB device
605 * driver's probe() routine should use additional intelligence to
606 * decide whether to bind to the specified interface.
608 * What Makes Good usb_device_id Tables:
610 * The match algorithm is very simple, so that intelligence in
611 * driver selection must come from smart driver id records.
612 * Unless you have good reasons to use another selection policy,
613 * provide match elements only in related groups, and order match
614 * specifiers from specific to general. Use the macros provided
615 * for that purpose if you can.
617 * The most specific match specifiers use device descriptor
618 * data. These are commonly used with product-specific matches;
619 * the USB_DEVICE macro lets you provide vendor and product IDs,
620 * and you can also match against ranges of product revisions.
621 * These are widely used for devices with application or vendor
622 * specific bDeviceClass values.
624 * Matches based on device class/subclass/protocol specifications
625 * are slightly more general; use the USB_DEVICE_INFO macro, or
626 * its siblings. These are used with single-function devices
627 * where bDeviceClass doesn't specify that each interface has
630 * Matches based on interface class/subclass/protocol are the
631 * most general; they let drivers bind to any interface on a
632 * multiple-function device. Use the USB_INTERFACE_INFO
633 * macro, or its siblings, to match class-per-interface style
634 * devices (as recorded in bInterfaceClass).
636 * Note that an entry created by USB_INTERFACE_INFO won't match
637 * any interface if the device class is set to Vendor-Specific.
638 * This is deliberate; according to the USB spec the meanings of
639 * the interface class/subclass/protocol for these devices are also
640 * vendor-specific, and hence matching against a standard product
641 * class wouldn't work anyway. If you really want to use an
642 * interface-based match for such a device, create a match record
643 * that also specifies the vendor ID. (Unforunately there isn't a
644 * standard macro for creating records like this.)
646 * Within those groups, remember that not all combinations are
647 * meaningful. For example, don't give a product version range
648 * without vendor and product IDs; or specify a protocol without
649 * its associated class and subclass.
651 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
652 const struct usb_device_id
*id
)
654 /* proc_connectinfo in devio.c may call us with id == NULL. */
658 /* It is important to check that id->driver_info is nonzero,
659 since an entry that is all zeroes except for a nonzero
660 id->driver_info is the way to create an entry that
661 indicates that the driver want to examine every
662 device and interface. */
663 for (; id
->idVendor
|| id
->idProduct
|| id
->bDeviceClass
||
664 id
->bInterfaceClass
|| id
->driver_info
; id
++) {
665 if (usb_match_one_id(interface
, id
))
671 EXPORT_SYMBOL_GPL(usb_match_id
);
673 static int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
675 /* devices and interfaces are handled separately */
676 if (is_usb_device(dev
)) {
678 /* interface drivers never match devices */
679 if (!is_usb_device_driver(drv
))
682 /* TODO: Add real matching code */
685 } else if (is_usb_interface(dev
)) {
686 struct usb_interface
*intf
;
687 struct usb_driver
*usb_drv
;
688 const struct usb_device_id
*id
;
690 /* device drivers never match interfaces */
691 if (is_usb_device_driver(drv
))
694 intf
= to_usb_interface(dev
);
695 usb_drv
= to_usb_driver(drv
);
697 id
= usb_match_id(intf
, usb_drv
->id_table
);
701 id
= usb_match_dynamic_id(intf
, usb_drv
);
709 #ifdef CONFIG_HOTPLUG
710 static int usb_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
712 struct usb_device
*usb_dev
;
714 if (is_usb_device(dev
)) {
715 usb_dev
= to_usb_device(dev
);
716 } else if (is_usb_interface(dev
)) {
717 struct usb_interface
*intf
= to_usb_interface(dev
);
719 usb_dev
= interface_to_usbdev(intf
);
724 if (usb_dev
->devnum
< 0) {
725 /* driver is often null here; dev_dbg() would oops */
726 pr_debug("usb %s: already deleted?\n", dev_name(dev
));
730 pr_debug("usb %s: bus removed?\n", dev_name(dev
));
734 #ifdef CONFIG_USB_DEVICEFS
735 /* If this is available, userspace programs can directly read
736 * all the device descriptors we don't tell them about. Or
737 * act as usermode drivers.
739 if (add_uevent_var(env
, "DEVICE=/proc/bus/usb/%03d/%03d",
740 usb_dev
->bus
->busnum
, usb_dev
->devnum
))
744 /* per-device configurations are common */
745 if (add_uevent_var(env
, "PRODUCT=%x/%x/%x",
746 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
747 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
748 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
)))
751 /* class-based driver binding models */
752 if (add_uevent_var(env
, "TYPE=%d/%d/%d",
753 usb_dev
->descriptor
.bDeviceClass
,
754 usb_dev
->descriptor
.bDeviceSubClass
,
755 usb_dev
->descriptor
.bDeviceProtocol
))
763 static int usb_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
767 #endif /* CONFIG_HOTPLUG */
770 * usb_register_device_driver - register a USB device (not interface) driver
771 * @new_udriver: USB operations for the device driver
772 * @owner: module owner of this driver.
774 * Registers a USB device driver with the USB core. The list of
775 * unattached devices will be rescanned whenever a new driver is
776 * added, allowing the new driver to attach to any recognized devices.
777 * Returns a negative error code on failure and 0 on success.
779 int usb_register_device_driver(struct usb_device_driver
*new_udriver
,
780 struct module
*owner
)
787 new_udriver
->drvwrap
.for_devices
= 1;
788 new_udriver
->drvwrap
.driver
.name
= (char *) new_udriver
->name
;
789 new_udriver
->drvwrap
.driver
.bus
= &usb_bus_type
;
790 new_udriver
->drvwrap
.driver
.probe
= usb_probe_device
;
791 new_udriver
->drvwrap
.driver
.remove
= usb_unbind_device
;
792 new_udriver
->drvwrap
.driver
.owner
= owner
;
794 retval
= driver_register(&new_udriver
->drvwrap
.driver
);
797 pr_info("%s: registered new device driver %s\n",
798 usbcore_name
, new_udriver
->name
);
799 usbfs_update_special();
801 printk(KERN_ERR
"%s: error %d registering device "
803 usbcore_name
, retval
, new_udriver
->name
);
808 EXPORT_SYMBOL_GPL(usb_register_device_driver
);
811 * usb_deregister_device_driver - unregister a USB device (not interface) driver
812 * @udriver: USB operations of the device driver to unregister
813 * Context: must be able to sleep
815 * Unlinks the specified driver from the internal USB driver list.
817 void usb_deregister_device_driver(struct usb_device_driver
*udriver
)
819 pr_info("%s: deregistering device driver %s\n",
820 usbcore_name
, udriver
->name
);
822 driver_unregister(&udriver
->drvwrap
.driver
);
823 usbfs_update_special();
825 EXPORT_SYMBOL_GPL(usb_deregister_device_driver
);
828 * usb_register_driver - register a USB interface driver
829 * @new_driver: USB operations for the interface driver
830 * @owner: module owner of this driver.
831 * @mod_name: module name string
833 * Registers a USB interface driver with the USB core. The list of
834 * unattached interfaces will be rescanned whenever a new driver is
835 * added, allowing the new driver to attach to any recognized interfaces.
836 * Returns a negative error code on failure and 0 on success.
838 * NOTE: if you want your driver to use the USB major number, you must call
839 * usb_register_dev() to enable that functionality. This function no longer
840 * takes care of that.
842 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
,
843 const char *mod_name
)
850 new_driver
->drvwrap
.for_devices
= 0;
851 new_driver
->drvwrap
.driver
.name
= (char *) new_driver
->name
;
852 new_driver
->drvwrap
.driver
.bus
= &usb_bus_type
;
853 new_driver
->drvwrap
.driver
.probe
= usb_probe_interface
;
854 new_driver
->drvwrap
.driver
.remove
= usb_unbind_interface
;
855 new_driver
->drvwrap
.driver
.owner
= owner
;
856 new_driver
->drvwrap
.driver
.mod_name
= mod_name
;
857 spin_lock_init(&new_driver
->dynids
.lock
);
858 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
860 retval
= driver_register(&new_driver
->drvwrap
.driver
);
864 usbfs_update_special();
866 retval
= usb_create_newid_file(new_driver
);
870 retval
= usb_create_removeid_file(new_driver
);
874 pr_info("%s: registered new interface driver %s\n",
875 usbcore_name
, new_driver
->name
);
881 usb_remove_newid_file(new_driver
);
883 driver_unregister(&new_driver
->drvwrap
.driver
);
885 printk(KERN_ERR
"%s: error %d registering interface "
887 usbcore_name
, retval
, new_driver
->name
);
890 EXPORT_SYMBOL_GPL(usb_register_driver
);
893 * usb_deregister - unregister a USB interface driver
894 * @driver: USB operations of the interface driver to unregister
895 * Context: must be able to sleep
897 * Unlinks the specified driver from the internal USB driver list.
899 * NOTE: If you called usb_register_dev(), you still need to call
900 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
901 * this * call will no longer do it for you.
903 void usb_deregister(struct usb_driver
*driver
)
905 pr_info("%s: deregistering interface driver %s\n",
906 usbcore_name
, driver
->name
);
908 usb_remove_removeid_file(driver
);
909 usb_remove_newid_file(driver
);
910 usb_free_dynids(driver
);
911 driver_unregister(&driver
->drvwrap
.driver
);
913 usbfs_update_special();
915 EXPORT_SYMBOL_GPL(usb_deregister
);
917 /* Forced unbinding of a USB interface driver, either because
918 * it doesn't support pre_reset/post_reset/reset_resume or
919 * because it doesn't support suspend/resume.
921 * The caller must hold @intf's device's lock, but not its pm_mutex
922 * and not @intf->dev.sem.
924 void usb_forced_unbind_intf(struct usb_interface
*intf
)
926 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
928 dev_dbg(&intf
->dev
, "forced unbind\n");
929 usb_driver_release_interface(driver
, intf
);
931 /* Mark the interface for later rebinding */
932 intf
->needs_binding
= 1;
935 /* Delayed forced unbinding of a USB interface driver and scan
938 * The caller must hold @intf's device's lock, but not its pm_mutex
939 * and not @intf->dev.sem.
941 * Note: Rebinds will be skipped if a system sleep transition is in
942 * progress and the PM "complete" callback hasn't occurred yet.
944 void usb_rebind_intf(struct usb_interface
*intf
)
948 /* Delayed unbind of an existing driver */
949 if (intf
->dev
.driver
) {
950 struct usb_driver
*driver
=
951 to_usb_driver(intf
->dev
.driver
);
953 dev_dbg(&intf
->dev
, "forced unbind\n");
954 usb_driver_release_interface(driver
, intf
);
957 /* Try to rebind the interface */
958 if (intf
->dev
.power
.status
== DPM_ON
) {
959 intf
->needs_binding
= 0;
960 rc
= device_attach(&intf
->dev
);
962 dev_warn(&intf
->dev
, "rebind failed: %d\n", rc
);
971 /* Unbind drivers for @udev's interfaces that don't support suspend/resume,
972 * or rebind interfaces that have been unbound, according to @action.
974 * The caller must hold @udev's device lock.
976 static void do_unbind_rebind(struct usb_device
*udev
, int action
)
978 struct usb_host_config
*config
;
980 struct usb_interface
*intf
;
981 struct usb_driver
*drv
;
983 config
= udev
->actconfig
;
985 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
986 intf
= config
->interface
[i
];
989 if (intf
->dev
.driver
) {
990 drv
= to_usb_driver(intf
->dev
.driver
);
991 if (!drv
->suspend
|| !drv
->resume
)
992 usb_forced_unbind_intf(intf
);
996 if (intf
->needs_binding
)
997 usb_rebind_intf(intf
);
1004 static int usb_suspend_device(struct usb_device
*udev
, pm_message_t msg
)
1006 struct usb_device_driver
*udriver
;
1009 if (udev
->state
== USB_STATE_NOTATTACHED
||
1010 udev
->state
== USB_STATE_SUSPENDED
)
1013 /* For devices that don't have a driver, we do a generic suspend. */
1014 if (udev
->dev
.driver
)
1015 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1017 udev
->do_remote_wakeup
= 0;
1018 udriver
= &usb_generic_driver
;
1020 status
= udriver
->suspend(udev
, msg
);
1023 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1027 static int usb_resume_device(struct usb_device
*udev
, pm_message_t msg
)
1029 struct usb_device_driver
*udriver
;
1032 if (udev
->state
== USB_STATE_NOTATTACHED
)
1035 /* Can't resume it if it doesn't have a driver. */
1036 if (udev
->dev
.driver
== NULL
) {
1041 /* Non-root devices on a full/low-speed bus must wait for their
1042 * companion high-speed root hub, in case a handoff is needed.
1044 if (!(msg
.event
& PM_EVENT_AUTO
) && udev
->parent
&&
1045 udev
->bus
->hs_companion
)
1046 device_pm_wait_for_dev(&udev
->dev
,
1047 &udev
->bus
->hs_companion
->root_hub
->dev
);
1049 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
)
1050 udev
->reset_resume
= 1;
1052 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1053 status
= udriver
->resume(udev
, msg
);
1056 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1060 static int usb_suspend_interface(struct usb_device
*udev
,
1061 struct usb_interface
*intf
, pm_message_t msg
)
1063 struct usb_driver
*driver
;
1066 if (udev
->state
== USB_STATE_NOTATTACHED
||
1067 intf
->condition
== USB_INTERFACE_UNBOUND
)
1069 driver
= to_usb_driver(intf
->dev
.driver
);
1071 if (driver
->suspend
) {
1072 status
= driver
->suspend(intf
, msg
);
1073 if (status
&& !(msg
.event
& PM_EVENT_AUTO
))
1074 dev_err(&intf
->dev
, "%s error %d\n",
1077 /* Later we will unbind the driver and reprobe */
1078 intf
->needs_binding
= 1;
1079 dev_warn(&intf
->dev
, "no %s for driver %s?\n",
1080 "suspend", driver
->name
);
1084 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1088 static int usb_resume_interface(struct usb_device
*udev
,
1089 struct usb_interface
*intf
, pm_message_t msg
, int reset_resume
)
1091 struct usb_driver
*driver
;
1094 if (udev
->state
== USB_STATE_NOTATTACHED
)
1097 /* Don't let autoresume interfere with unbinding */
1098 if (intf
->condition
== USB_INTERFACE_UNBINDING
)
1101 /* Can't resume it if it doesn't have a driver. */
1102 if (intf
->condition
== USB_INTERFACE_UNBOUND
) {
1104 /* Carry out a deferred switch to altsetting 0 */
1105 if (intf
->needs_altsetting0
&&
1106 intf
->dev
.power
.status
== DPM_ON
) {
1107 usb_set_interface(udev
, intf
->altsetting
[0].
1108 desc
.bInterfaceNumber
, 0);
1109 intf
->needs_altsetting0
= 0;
1114 /* Don't resume if the interface is marked for rebinding */
1115 if (intf
->needs_binding
)
1117 driver
= to_usb_driver(intf
->dev
.driver
);
1120 if (driver
->reset_resume
) {
1121 status
= driver
->reset_resume(intf
);
1123 dev_err(&intf
->dev
, "%s error %d\n",
1124 "reset_resume", status
);
1126 intf
->needs_binding
= 1;
1127 dev_warn(&intf
->dev
, "no %s for driver %s?\n",
1128 "reset_resume", driver
->name
);
1131 if (driver
->resume
) {
1132 status
= driver
->resume(intf
);
1134 dev_err(&intf
->dev
, "%s error %d\n",
1137 intf
->needs_binding
= 1;
1138 dev_warn(&intf
->dev
, "no %s for driver %s?\n",
1139 "resume", driver
->name
);
1144 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1146 /* Later we will unbind the driver and/or reprobe, if necessary */
1151 * usb_suspend_both - suspend a USB device and its interfaces
1152 * @udev: the usb_device to suspend
1153 * @msg: Power Management message describing this state transition
1155 * This is the central routine for suspending USB devices. It calls the
1156 * suspend methods for all the interface drivers in @udev and then calls
1157 * the suspend method for @udev itself. If an error occurs at any stage,
1158 * all the interfaces which were suspended are resumed so that they remain
1159 * in the same state as the device.
1161 * Autosuspend requests originating from a child device or an interface
1162 * driver may be made without the protection of @udev's device lock, but
1163 * all other suspend calls will hold the lock. Usbcore will insure that
1164 * method calls do not arrive during bind, unbind, or reset operations.
1165 * However drivers must be prepared to handle suspend calls arriving at
1166 * unpredictable times.
1168 * This routine can run only in process context.
1170 static int usb_suspend_both(struct usb_device
*udev
, pm_message_t msg
)
1174 struct usb_interface
*intf
;
1176 if (udev
->state
== USB_STATE_NOTATTACHED
||
1177 udev
->state
== USB_STATE_SUSPENDED
)
1180 /* Suspend all the interfaces and then udev itself */
1181 if (udev
->actconfig
) {
1182 for (; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1183 intf
= udev
->actconfig
->interface
[i
];
1184 status
= usb_suspend_interface(udev
, intf
, msg
);
1190 status
= usb_suspend_device(udev
, msg
);
1192 /* If the suspend failed, resume interfaces that did get suspended */
1194 msg
.event
^= (PM_EVENT_SUSPEND
| PM_EVENT_RESUME
);
1196 intf
= udev
->actconfig
->interface
[i
];
1197 usb_resume_interface(udev
, intf
, msg
, 0);
1200 /* If the suspend succeeded then prevent any more URB submissions
1201 * and flush any outstanding URBs.
1204 udev
->can_submit
= 0;
1205 for (i
= 0; i
< 16; ++i
) {
1206 usb_hcd_flush_endpoint(udev
, udev
->ep_out
[i
]);
1207 usb_hcd_flush_endpoint(udev
, udev
->ep_in
[i
]);
1212 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1217 * usb_resume_both - resume a USB device and its interfaces
1218 * @udev: the usb_device to resume
1219 * @msg: Power Management message describing this state transition
1221 * This is the central routine for resuming USB devices. It calls the
1222 * the resume method for @udev and then calls the resume methods for all
1223 * the interface drivers in @udev.
1225 * Autoresume requests originating from a child device or an interface
1226 * driver may be made without the protection of @udev's device lock, but
1227 * all other resume calls will hold the lock. Usbcore will insure that
1228 * method calls do not arrive during bind, unbind, or reset operations.
1229 * However drivers must be prepared to handle resume calls arriving at
1230 * unpredictable times.
1232 * This routine can run only in process context.
1234 static int usb_resume_both(struct usb_device
*udev
, pm_message_t msg
)
1238 struct usb_interface
*intf
;
1240 if (udev
->state
== USB_STATE_NOTATTACHED
) {
1244 udev
->can_submit
= 1;
1246 /* Resume the device */
1247 if (udev
->state
== USB_STATE_SUSPENDED
|| udev
->reset_resume
)
1248 status
= usb_resume_device(udev
, msg
);
1250 /* Resume the interfaces */
1251 if (status
== 0 && udev
->actconfig
) {
1252 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1253 intf
= udev
->actconfig
->interface
[i
];
1254 usb_resume_interface(udev
, intf
, msg
,
1255 udev
->reset_resume
);
1260 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1262 udev
->reset_resume
= 0;
1266 /* The device lock is held by the PM core */
1267 int usb_suspend(struct device
*dev
, pm_message_t msg
)
1269 struct usb_device
*udev
= to_usb_device(dev
);
1271 do_unbind_rebind(udev
, DO_UNBIND
);
1272 udev
->do_remote_wakeup
= device_may_wakeup(&udev
->dev
);
1273 return usb_suspend_both(udev
, msg
);
1276 /* The device lock is held by the PM core */
1277 int usb_resume(struct device
*dev
, pm_message_t msg
)
1279 struct usb_device
*udev
= to_usb_device(dev
);
1282 /* For PM complete calls, all we do is rebind interfaces */
1283 if (msg
.event
== PM_EVENT_ON
) {
1284 if (udev
->state
!= USB_STATE_NOTATTACHED
)
1285 do_unbind_rebind(udev
, DO_REBIND
);
1288 /* For all other calls, take the device back to full power and
1289 * tell the PM core in case it was autosuspended previously.
1292 status
= usb_resume_both(udev
, msg
);
1294 pm_runtime_disable(dev
);
1295 pm_runtime_set_active(dev
);
1296 pm_runtime_enable(dev
);
1297 udev
->last_busy
= jiffies
;
1301 /* Avoid PM error messages for devices disconnected while suspended
1302 * as we'll display regular disconnect messages just a bit later.
1304 if (status
== -ENODEV
)
1309 #endif /* CONFIG_PM */
1311 #ifdef CONFIG_USB_SUSPEND
1314 * usb_enable_autosuspend - allow a USB device to be autosuspended
1315 * @udev: the USB device which may be autosuspended
1317 * This routine allows @udev to be autosuspended. An autosuspend won't
1318 * take place until the autosuspend_delay has elapsed and all the other
1319 * necessary conditions are satisfied.
1321 * The caller must hold @udev's device lock.
1323 int usb_enable_autosuspend(struct usb_device
*udev
)
1325 if (udev
->autosuspend_disabled
) {
1326 udev
->autosuspend_disabled
= 0;
1327 usb_autosuspend_device(udev
);
1331 EXPORT_SYMBOL_GPL(usb_enable_autosuspend
);
1334 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1335 * @udev: the USB device which may not be autosuspended
1337 * This routine prevents @udev from being autosuspended and wakes it up
1338 * if it is already autosuspended.
1340 * The caller must hold @udev's device lock.
1342 int usb_disable_autosuspend(struct usb_device
*udev
)
1346 if (!udev
->autosuspend_disabled
) {
1347 rc
= usb_autoresume_device(udev
);
1349 udev
->autosuspend_disabled
= 1;
1353 EXPORT_SYMBOL_GPL(usb_disable_autosuspend
);
1356 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1357 * @udev: the usb_device to autosuspend
1359 * This routine should be called when a core subsystem is finished using
1360 * @udev and wants to allow it to autosuspend. Examples would be when
1361 * @udev's device file in usbfs is closed or after a configuration change.
1363 * @udev's usage counter is decremented; if it drops to 0 and all the
1364 * interfaces are inactive then a delayed autosuspend will be attempted.
1365 * The attempt may fail (see autosuspend_check()).
1367 * The caller must hold @udev's device lock.
1369 * This routine can run only in process context.
1371 void usb_autosuspend_device(struct usb_device
*udev
)
1375 udev
->last_busy
= jiffies
;
1376 status
= pm_runtime_put_sync(&udev
->dev
);
1377 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1378 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1383 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1384 * @udev: the usb_device to autosuspend
1386 * This routine should be called when a core subsystem thinks @udev may
1387 * be ready to autosuspend.
1389 * @udev's usage counter left unchanged. If it is 0 and all the interfaces
1390 * are inactive then an autosuspend will be attempted. The attempt may
1391 * fail or be delayed.
1393 * The caller must hold @udev's device lock.
1395 * This routine can run only in process context.
1397 void usb_try_autosuspend_device(struct usb_device
*udev
)
1401 status
= pm_runtime_idle(&udev
->dev
);
1402 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1403 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1408 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1409 * @udev: the usb_device to autoresume
1411 * This routine should be called when a core subsystem wants to use @udev
1412 * and needs to guarantee that it is not suspended. No autosuspend will
1413 * occur until usb_autosuspend_device() is called. (Note that this will
1414 * not prevent suspend events originating in the PM core.) Examples would
1415 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1416 * request is received.
1418 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1419 * However if the autoresume fails then the usage counter is re-decremented.
1421 * The caller must hold @udev's device lock.
1423 * This routine can run only in process context.
1425 int usb_autoresume_device(struct usb_device
*udev
)
1429 status
= pm_runtime_get_sync(&udev
->dev
);
1431 pm_runtime_put_sync(&udev
->dev
);
1432 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1433 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1441 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1442 * @intf: the usb_interface whose counter should be decremented
1444 * This routine should be called by an interface driver when it is
1445 * finished using @intf and wants to allow it to autosuspend. A typical
1446 * example would be a character-device driver when its device file is
1449 * The routine decrements @intf's usage counter. When the counter reaches
1450 * 0, a delayed autosuspend request for @intf's device is attempted. The
1451 * attempt may fail (see autosuspend_check()).
1453 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1454 * take place only if the device's remote-wakeup facility is enabled.
1456 * This routine can run only in process context.
1458 void usb_autopm_put_interface(struct usb_interface
*intf
)
1460 struct usb_device
*udev
= interface_to_usbdev(intf
);
1463 udev
->last_busy
= jiffies
;
1464 atomic_dec(&intf
->pm_usage_cnt
);
1465 status
= pm_runtime_put_sync(&intf
->dev
);
1466 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1467 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1470 EXPORT_SYMBOL_GPL(usb_autopm_put_interface
);
1473 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1474 * @intf: the usb_interface whose counter should be decremented
1476 * This routine does much the same thing as usb_autopm_put_interface():
1477 * It decrements @intf's usage counter and schedules a delayed
1478 * autosuspend request if the counter is <= 0. The difference is that it
1479 * does not perform any synchronization; callers should hold a private
1480 * lock and handle all synchronization issues themselves.
1482 * Typically a driver would call this routine during an URB's completion
1483 * handler, if no more URBs were pending.
1485 * This routine can run in atomic context.
1487 void usb_autopm_put_interface_async(struct usb_interface
*intf
)
1489 struct usb_device
*udev
= interface_to_usbdev(intf
);
1490 unsigned long last_busy
;
1493 last_busy
= udev
->last_busy
;
1494 udev
->last_busy
= jiffies
;
1495 atomic_dec(&intf
->pm_usage_cnt
);
1496 pm_runtime_put_noidle(&intf
->dev
);
1498 if (!udev
->autosuspend_disabled
) {
1499 /* Optimization: Don't schedule a delayed autosuspend if
1500 * the timer is already running and the expiration time
1503 * We have to use the interface's timer. Attempts to
1504 * schedule a suspend for the device would fail because
1505 * the interface is still active.
1507 if (intf
->dev
.power
.timer_expires
== 0 ||
1508 round_jiffies_up(last_busy
) !=
1509 round_jiffies_up(jiffies
)) {
1510 status
= pm_schedule_suspend(&intf
->dev
,
1512 round_jiffies_up_relative(
1513 udev
->autosuspend_delay
)));
1516 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1517 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1520 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async
);
1523 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1524 * @intf: the usb_interface whose counter should be decremented
1526 * This routine decrements @intf's usage counter but does not carry out an
1529 * This routine can run in atomic context.
1531 void usb_autopm_put_interface_no_suspend(struct usb_interface
*intf
)
1533 struct usb_device
*udev
= interface_to_usbdev(intf
);
1535 udev
->last_busy
= jiffies
;
1536 atomic_dec(&intf
->pm_usage_cnt
);
1537 pm_runtime_put_noidle(&intf
->dev
);
1539 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend
);
1542 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1543 * @intf: the usb_interface whose counter should be incremented
1545 * This routine should be called by an interface driver when it wants to
1546 * use @intf and needs to guarantee that it is not suspended. In addition,
1547 * the routine prevents @intf from being autosuspended subsequently. (Note
1548 * that this will not prevent suspend events originating in the PM core.)
1549 * This prevention will persist until usb_autopm_put_interface() is called
1550 * or @intf is unbound. A typical example would be a character-device
1551 * driver when its device file is opened.
1553 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1554 * However if the autoresume fails then the counter is re-decremented.
1556 * This routine can run only in process context.
1558 int usb_autopm_get_interface(struct usb_interface
*intf
)
1562 status
= pm_runtime_get_sync(&intf
->dev
);
1564 pm_runtime_put_sync(&intf
->dev
);
1566 atomic_inc(&intf
->pm_usage_cnt
);
1567 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1568 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1574 EXPORT_SYMBOL_GPL(usb_autopm_get_interface
);
1577 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1578 * @intf: the usb_interface whose counter should be incremented
1580 * This routine does much the same thing as
1581 * usb_autopm_get_interface(): It increments @intf's usage counter and
1582 * queues an autoresume request if the device is suspended. The
1583 * differences are that it does not perform any synchronization (callers
1584 * should hold a private lock and handle all synchronization issues
1585 * themselves), and it does not autoresume the device directly (it only
1586 * queues a request). After a successful call, the device may not yet be
1589 * This routine can run in atomic context.
1591 int usb_autopm_get_interface_async(struct usb_interface
*intf
)
1596 /* Don't request a resume unless the interface is already suspending
1597 * or suspended. Doing so would force a running suspend timer to be
1600 pm_runtime_get_noresume(&intf
->dev
);
1601 s
= ACCESS_ONCE(intf
->dev
.power
.runtime_status
);
1602 if (s
== RPM_SUSPENDING
|| s
== RPM_SUSPENDED
)
1603 status
= pm_request_resume(&intf
->dev
);
1605 if (status
< 0 && status
!= -EINPROGRESS
)
1606 pm_runtime_put_noidle(&intf
->dev
);
1608 atomic_inc(&intf
->pm_usage_cnt
);
1609 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1610 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1616 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async
);
1619 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1620 * @intf: the usb_interface whose counter should be incremented
1622 * This routine increments @intf's usage counter but does not carry out an
1625 * This routine can run in atomic context.
1627 void usb_autopm_get_interface_no_resume(struct usb_interface
*intf
)
1629 struct usb_device
*udev
= interface_to_usbdev(intf
);
1631 udev
->last_busy
= jiffies
;
1632 atomic_inc(&intf
->pm_usage_cnt
);
1633 pm_runtime_get_noresume(&intf
->dev
);
1635 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume
);
1637 /* Internal routine to check whether we may autosuspend a device. */
1638 static int autosuspend_check(struct usb_device
*udev
)
1641 struct usb_interface
*intf
;
1642 unsigned long suspend_time
, j
;
1644 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1645 * any interface drivers require remote wakeup but it isn't available.
1647 udev
->do_remote_wakeup
= device_may_wakeup(&udev
->dev
);
1648 if (udev
->actconfig
) {
1649 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1650 intf
= udev
->actconfig
->interface
[i
];
1652 /* We don't need to check interfaces that are
1653 * disabled for runtime PM. Either they are unbound
1654 * or else their drivers don't support autosuspend
1655 * and so they are permanently active.
1657 if (intf
->dev
.power
.disable_depth
)
1659 if (atomic_read(&intf
->dev
.power
.usage_count
) > 0)
1661 if (intf
->needs_remote_wakeup
&&
1662 !udev
->do_remote_wakeup
) {
1663 dev_dbg(&udev
->dev
, "remote wakeup needed "
1664 "for autosuspend\n");
1668 /* Don't allow autosuspend if the device will need
1669 * a reset-resume and any of its interface drivers
1670 * doesn't include support or needs remote wakeup.
1672 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
) {
1673 struct usb_driver
*driver
;
1675 driver
= to_usb_driver(intf
->dev
.driver
);
1676 if (!driver
->reset_resume
||
1677 intf
->needs_remote_wakeup
)
1683 /* If everything is okay but the device hasn't been idle for long
1684 * enough, queue a delayed autosuspend request.
1686 j
= ACCESS_ONCE(jiffies
);
1687 suspend_time
= udev
->last_busy
+ udev
->autosuspend_delay
;
1688 if (time_before(j
, suspend_time
)) {
1689 pm_schedule_suspend(&udev
->dev
, jiffies_to_msecs(
1690 round_jiffies_up_relative(suspend_time
- j
)));
1696 static int usb_runtime_suspend(struct device
*dev
)
1700 /* A USB device can be suspended if it passes the various autosuspend
1701 * checks. Runtime suspend for a USB device means suspending all the
1702 * interfaces and then the device itself.
1704 if (is_usb_device(dev
)) {
1705 struct usb_device
*udev
= to_usb_device(dev
);
1707 if (autosuspend_check(udev
) != 0)
1710 status
= usb_suspend_both(udev
, PMSG_AUTO_SUSPEND
);
1712 /* If an interface fails the suspend, adjust the last_busy
1713 * time so that we don't get another suspend attempt right
1717 udev
->last_busy
= jiffies
+
1718 (udev
->autosuspend_delay
== 0 ?
1722 /* Prevent the parent from suspending immediately after */
1723 else if (udev
->parent
) {
1724 udev
->parent
->last_busy
= jiffies
;
1728 /* Runtime suspend for a USB interface doesn't mean anything. */
1732 static int usb_runtime_resume(struct device
*dev
)
1734 /* Runtime resume for a USB device means resuming both the device
1735 * and all its interfaces.
1737 if (is_usb_device(dev
)) {
1738 struct usb_device
*udev
= to_usb_device(dev
);
1741 status
= usb_resume_both(udev
, PMSG_AUTO_RESUME
);
1742 udev
->last_busy
= jiffies
;
1746 /* Runtime resume for a USB interface doesn't mean anything. */
1750 static int usb_runtime_idle(struct device
*dev
)
1752 /* An idle USB device can be suspended if it passes the various
1753 * autosuspend checks. An idle interface can be suspended at
1756 if (is_usb_device(dev
)) {
1757 struct usb_device
*udev
= to_usb_device(dev
);
1759 if (autosuspend_check(udev
) != 0)
1763 pm_runtime_suspend(dev
);
1767 static struct dev_pm_ops usb_bus_pm_ops
= {
1768 .runtime_suspend
= usb_runtime_suspend
,
1769 .runtime_resume
= usb_runtime_resume
,
1770 .runtime_idle
= usb_runtime_idle
,
1775 #define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
1777 #endif /* CONFIG_USB_SUSPEND */
1779 struct bus_type usb_bus_type
= {
1781 .match
= usb_device_match
,
1782 .uevent
= usb_uevent
,
1783 .pm
= &usb_bus_pm_ops
,