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/export.h>
28 #include <linux/usb.h>
29 #include <linux/usb/quirks.h>
30 #include <linux/usb/hcd.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 const struct usb_device_id
*id_table
,
41 struct device_driver
*driver
,
42 const char *buf
, size_t count
)
44 struct usb_dynid
*dynid
;
47 unsigned int bInterfaceClass
= 0;
48 u32 refVendor
, refProduct
;
52 fields
= sscanf(buf
, "%x %x %x %x %x", &idVendor
, &idProduct
,
53 &bInterfaceClass
, &refVendor
, &refProduct
);
57 dynid
= kzalloc(sizeof(*dynid
), GFP_KERNEL
);
61 INIT_LIST_HEAD(&dynid
->node
);
62 dynid
->id
.idVendor
= idVendor
;
63 dynid
->id
.idProduct
= idProduct
;
64 dynid
->id
.match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
65 if (fields
> 2 && bInterfaceClass
) {
66 if (bInterfaceClass
> 255) {
71 dynid
->id
.bInterfaceClass
= (u8
)bInterfaceClass
;
72 dynid
->id
.match_flags
|= USB_DEVICE_ID_MATCH_INT_CLASS
;
76 const struct usb_device_id
*id
= id_table
;
83 for (; id
->match_flags
; id
++)
84 if (id
->idVendor
== refVendor
&& id
->idProduct
== refProduct
)
87 if (id
->match_flags
) {
88 dynid
->id
.driver_info
= id
->driver_info
;
95 spin_lock(&dynids
->lock
);
96 list_add_tail(&dynid
->node
, &dynids
->list
);
97 spin_unlock(&dynids
->lock
);
99 retval
= driver_attach(driver
);
109 EXPORT_SYMBOL_GPL(usb_store_new_id
);
111 ssize_t
usb_show_dynids(struct usb_dynids
*dynids
, char *buf
)
113 struct usb_dynid
*dynid
;
116 list_for_each_entry(dynid
, &dynids
->list
, node
)
117 if (dynid
->id
.bInterfaceClass
!= 0)
118 count
+= scnprintf(&buf
[count
], PAGE_SIZE
- count
, "%04x %04x %02x\n",
119 dynid
->id
.idVendor
, dynid
->id
.idProduct
,
120 dynid
->id
.bInterfaceClass
);
122 count
+= scnprintf(&buf
[count
], PAGE_SIZE
- count
, "%04x %04x\n",
123 dynid
->id
.idVendor
, dynid
->id
.idProduct
);
126 EXPORT_SYMBOL_GPL(usb_show_dynids
);
128 static ssize_t
new_id_show(struct device_driver
*driver
, char *buf
)
130 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
132 return usb_show_dynids(&usb_drv
->dynids
, buf
);
135 static ssize_t
new_id_store(struct device_driver
*driver
,
136 const char *buf
, size_t count
)
138 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
140 return usb_store_new_id(&usb_drv
->dynids
, usb_drv
->id_table
, driver
, buf
, count
);
142 static DRIVER_ATTR_RW(new_id
);
145 * Remove a USB device ID from this driver
147 static ssize_t
remove_id_store(struct device_driver
*driver
, const char *buf
,
150 struct usb_dynid
*dynid
, *n
;
151 struct usb_driver
*usb_driver
= to_usb_driver(driver
);
156 fields
= sscanf(buf
, "%x %x", &idVendor
, &idProduct
);
160 spin_lock(&usb_driver
->dynids
.lock
);
161 list_for_each_entry_safe(dynid
, n
, &usb_driver
->dynids
.list
, node
) {
162 struct usb_device_id
*id
= &dynid
->id
;
163 if ((id
->idVendor
== idVendor
) &&
164 (id
->idProduct
== idProduct
)) {
165 list_del(&dynid
->node
);
170 spin_unlock(&usb_driver
->dynids
.lock
);
174 static ssize_t
remove_id_show(struct device_driver
*driver
, char *buf
)
176 return new_id_show(driver
, buf
);
178 static DRIVER_ATTR_RW(remove_id
);
180 static int usb_create_newid_files(struct usb_driver
*usb_drv
)
184 if (usb_drv
->no_dynamic_id
)
187 if (usb_drv
->probe
!= NULL
) {
188 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
189 &driver_attr_new_id
);
191 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
192 &driver_attr_remove_id
);
194 driver_remove_file(&usb_drv
->drvwrap
.driver
,
195 &driver_attr_new_id
);
202 static void usb_remove_newid_files(struct usb_driver
*usb_drv
)
204 if (usb_drv
->no_dynamic_id
)
207 if (usb_drv
->probe
!= NULL
) {
208 driver_remove_file(&usb_drv
->drvwrap
.driver
,
209 &driver_attr_remove_id
);
210 driver_remove_file(&usb_drv
->drvwrap
.driver
,
211 &driver_attr_new_id
);
215 static void usb_free_dynids(struct usb_driver
*usb_drv
)
217 struct usb_dynid
*dynid
, *n
;
219 spin_lock(&usb_drv
->dynids
.lock
);
220 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
221 list_del(&dynid
->node
);
224 spin_unlock(&usb_drv
->dynids
.lock
);
227 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
228 struct usb_driver
*drv
)
230 struct usb_dynid
*dynid
;
232 spin_lock(&drv
->dynids
.lock
);
233 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
234 if (usb_match_one_id(intf
, &dynid
->id
)) {
235 spin_unlock(&drv
->dynids
.lock
);
239 spin_unlock(&drv
->dynids
.lock
);
244 /* called from driver core with dev locked */
245 static int usb_probe_device(struct device
*dev
)
247 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
248 struct usb_device
*udev
= to_usb_device(dev
);
251 dev_dbg(dev
, "%s\n", __func__
);
253 /* TODO: Add real matching code */
255 /* The device should always appear to be in use
256 * unless the driver supports autosuspend.
258 if (!udriver
->supports_autosuspend
)
259 error
= usb_autoresume_device(udev
);
262 error
= udriver
->probe(udev
);
266 /* called from driver core with dev locked */
267 static int usb_unbind_device(struct device
*dev
)
269 struct usb_device
*udev
= to_usb_device(dev
);
270 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
272 udriver
->disconnect(udev
);
273 if (!udriver
->supports_autosuspend
)
274 usb_autosuspend_device(udev
);
279 * Cancel any pending scheduled resets
281 * [see usb_queue_reset_device()]
283 * Called after unconfiguring / when releasing interfaces. See
284 * comments in __usb_queue_reset_device() regarding
285 * udev->reset_running.
287 static void usb_cancel_queued_reset(struct usb_interface
*iface
)
289 if (iface
->reset_running
== 0)
290 cancel_work_sync(&iface
->reset_ws
);
293 /* called from driver core with dev locked */
294 static int usb_probe_interface(struct device
*dev
)
296 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
297 struct usb_interface
*intf
= to_usb_interface(dev
);
298 struct usb_device
*udev
= interface_to_usbdev(intf
);
299 const struct usb_device_id
*id
;
301 int lpm_disable_error
;
303 dev_dbg(dev
, "%s\n", __func__
);
305 intf
->needs_binding
= 0;
307 if (usb_device_is_owned(udev
))
310 if (udev
->authorized
== 0) {
311 dev_err(&intf
->dev
, "Device is not authorized for usage\n");
315 id
= usb_match_id(intf
, driver
->id_table
);
317 id
= usb_match_dynamic_id(intf
, driver
);
321 dev_dbg(dev
, "%s - got id\n", __func__
);
323 error
= usb_autoresume_device(udev
);
327 intf
->condition
= USB_INTERFACE_BINDING
;
329 /* Probed interfaces are initially active. They are
330 * runtime-PM-enabled only if the driver has autosuspend support.
331 * They are sensitive to their children's power states.
333 pm_runtime_set_active(dev
);
334 pm_suspend_ignore_children(dev
, false);
335 if (driver
->supports_autosuspend
)
336 pm_runtime_enable(dev
);
338 /* If the new driver doesn't allow hub-initiated LPM, and we can't
339 * disable hub-initiated LPM, then fail the probe.
341 * Otherwise, leaving LPM enabled should be harmless, because the
342 * endpoint intervals should remain the same, and the U1/U2 timeouts
343 * should remain the same.
345 * If we need to install alt setting 0 before probe, or another alt
346 * setting during probe, that should also be fine. usb_set_interface()
347 * will attempt to disable LPM, and fail if it can't disable it.
349 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
350 if (lpm_disable_error
&& driver
->disable_hub_initiated_lpm
) {
351 dev_err(&intf
->dev
, "%s Failed to disable LPM for driver %s\n.",
352 __func__
, driver
->name
);
353 error
= lpm_disable_error
;
357 /* Carry out a deferred switch to altsetting 0 */
358 if (intf
->needs_altsetting0
) {
359 error
= usb_set_interface(udev
, intf
->altsetting
[0].
360 desc
.bInterfaceNumber
, 0);
363 intf
->needs_altsetting0
= 0;
366 error
= driver
->probe(intf
, id
);
370 intf
->condition
= USB_INTERFACE_BOUND
;
372 /* If the LPM disable succeeded, balance the ref counts. */
373 if (!lpm_disable_error
)
374 usb_unlocked_enable_lpm(udev
);
376 usb_autosuspend_device(udev
);
380 usb_set_intfdata(intf
, NULL
);
381 intf
->needs_remote_wakeup
= 0;
382 intf
->condition
= USB_INTERFACE_UNBOUND
;
383 usb_cancel_queued_reset(intf
);
385 /* If the LPM disable succeeded, balance the ref counts. */
386 if (!lpm_disable_error
)
387 usb_unlocked_enable_lpm(udev
);
389 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
390 if (driver
->supports_autosuspend
)
391 pm_runtime_disable(dev
);
392 pm_runtime_set_suspended(dev
);
394 usb_autosuspend_device(udev
);
398 /* called from driver core with dev locked */
399 static int usb_unbind_interface(struct device
*dev
)
401 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
402 struct usb_interface
*intf
= to_usb_interface(dev
);
403 struct usb_device
*udev
;
404 int error
, r
, lpm_disable_error
;
406 intf
->condition
= USB_INTERFACE_UNBINDING
;
408 /* Autoresume for set_interface call below */
409 udev
= interface_to_usbdev(intf
);
410 error
= usb_autoresume_device(udev
);
412 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
413 * the driver is unbound. If LPM isn't disabled, that's fine because it
414 * wouldn't be enabled unless all the bound interfaces supported
417 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
419 /* Terminate all URBs for this interface unless the driver
420 * supports "soft" unbinding.
422 if (!driver
->soft_unbind
)
423 usb_disable_interface(udev
, intf
, false);
425 driver
->disconnect(intf
);
426 usb_cancel_queued_reset(intf
);
428 /* Reset other interface state.
429 * We cannot do a Set-Interface if the device is suspended or
430 * if it is prepared for a system sleep (since installing a new
431 * altsetting means creating new endpoint device entries).
432 * When either of these happens, defer the Set-Interface.
434 if (intf
->cur_altsetting
->desc
.bAlternateSetting
== 0) {
435 /* Already in altsetting 0 so skip Set-Interface.
436 * Just re-enable it without affecting the endpoint toggles.
438 usb_enable_interface(udev
, intf
, false);
439 } else if (!error
&& !intf
->dev
.power
.is_prepared
) {
440 r
= usb_set_interface(udev
, intf
->altsetting
[0].
441 desc
.bInterfaceNumber
, 0);
443 intf
->needs_altsetting0
= 1;
445 intf
->needs_altsetting0
= 1;
447 usb_set_intfdata(intf
, NULL
);
449 intf
->condition
= USB_INTERFACE_UNBOUND
;
450 intf
->needs_remote_wakeup
= 0;
452 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
453 if (!lpm_disable_error
)
454 usb_unlocked_enable_lpm(udev
);
456 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
457 if (driver
->supports_autosuspend
)
458 pm_runtime_disable(dev
);
459 pm_runtime_set_suspended(dev
);
461 /* Undo any residual pm_autopm_get_interface_* calls */
462 for (r
= atomic_read(&intf
->pm_usage_cnt
); r
> 0; --r
)
463 usb_autopm_put_interface_no_suspend(intf
);
464 atomic_set(&intf
->pm_usage_cnt
, 0);
467 usb_autosuspend_device(udev
);
473 * usb_driver_claim_interface - bind a driver to an interface
474 * @driver: the driver to be bound
475 * @iface: the interface to which it will be bound; must be in the
476 * usb device's active configuration
477 * @priv: driver data associated with that interface
479 * This is used by usb device drivers that need to claim more than one
480 * interface on a device when probing (audio and acm are current examples).
481 * No device driver should directly modify internal usb_interface or
482 * usb_device structure members.
484 * Few drivers should need to use this routine, since the most natural
485 * way to bind to an interface is to return the private data from
486 * the driver's probe() method.
488 * Callers must own the device lock, so driver probe() entries don't need
489 * extra locking, but other call contexts may need to explicitly claim that
492 * Return: 0 on success.
494 int usb_driver_claim_interface(struct usb_driver
*driver
,
495 struct usb_interface
*iface
, void *priv
)
497 struct device
*dev
= &iface
->dev
;
498 struct usb_device
*udev
;
500 int lpm_disable_error
;
505 udev
= interface_to_usbdev(iface
);
507 dev
->driver
= &driver
->drvwrap
.driver
;
508 usb_set_intfdata(iface
, priv
);
509 iface
->needs_binding
= 0;
511 iface
->condition
= USB_INTERFACE_BOUND
;
513 /* Disable LPM until this driver is bound. */
514 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
515 if (lpm_disable_error
&& driver
->disable_hub_initiated_lpm
) {
516 dev_err(&iface
->dev
, "%s Failed to disable LPM for driver %s\n.",
517 __func__
, driver
->name
);
521 /* Claimed interfaces are initially inactive (suspended) and
522 * runtime-PM-enabled, but only if the driver has autosuspend
523 * support. Otherwise they are marked active, to prevent the
524 * device from being autosuspended, but left disabled. In either
525 * case they are sensitive to their children's power states.
527 pm_suspend_ignore_children(dev
, false);
528 if (driver
->supports_autosuspend
)
529 pm_runtime_enable(dev
);
531 pm_runtime_set_active(dev
);
533 /* if interface was already added, bind now; else let
534 * the future device_add() bind it, bypassing probe()
536 if (device_is_registered(dev
))
537 retval
= device_bind_driver(dev
);
539 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
540 if (!lpm_disable_error
)
541 usb_unlocked_enable_lpm(udev
);
545 EXPORT_SYMBOL_GPL(usb_driver_claim_interface
);
548 * usb_driver_release_interface - unbind a driver from an interface
549 * @driver: the driver to be unbound
550 * @iface: the interface from which it will be unbound
552 * This can be used by drivers to release an interface without waiting
553 * for their disconnect() methods to be called. In typical cases this
554 * also causes the driver disconnect() method to be called.
556 * This call is synchronous, and may not be used in an interrupt context.
557 * Callers must own the device lock, so driver disconnect() entries don't
558 * need extra locking, but other call contexts may need to explicitly claim
561 void usb_driver_release_interface(struct usb_driver
*driver
,
562 struct usb_interface
*iface
)
564 struct device
*dev
= &iface
->dev
;
566 /* this should never happen, don't release something that's not ours */
567 if (!dev
->driver
|| dev
->driver
!= &driver
->drvwrap
.driver
)
570 /* don't release from within disconnect() */
571 if (iface
->condition
!= USB_INTERFACE_BOUND
)
573 iface
->condition
= USB_INTERFACE_UNBINDING
;
575 /* Release via the driver core only if the interface
576 * has already been registered
578 if (device_is_registered(dev
)) {
579 device_release_driver(dev
);
582 usb_unbind_interface(dev
);
587 EXPORT_SYMBOL_GPL(usb_driver_release_interface
);
589 /* returns 0 if no match, 1 if match */
590 int usb_match_device(struct usb_device
*dev
, const struct usb_device_id
*id
)
592 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
593 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
596 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
597 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
600 /* No need to test id->bcdDevice_lo != 0, since 0 is never
601 greater than any unsigned number. */
602 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
603 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
606 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
607 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
610 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
611 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
614 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
615 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
618 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
619 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
625 /* returns 0 if no match, 1 if match */
626 int usb_match_one_id_intf(struct usb_device
*dev
,
627 struct usb_host_interface
*intf
,
628 const struct usb_device_id
*id
)
630 /* The interface class, subclass, protocol and number should never be
631 * checked for a match if the device class is Vendor Specific,
632 * unless the match record specifies the Vendor ID. */
633 if (dev
->descriptor
.bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
634 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
635 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
636 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
637 USB_DEVICE_ID_MATCH_INT_PROTOCOL
|
638 USB_DEVICE_ID_MATCH_INT_NUMBER
)))
641 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
642 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
645 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
646 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
649 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
650 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
653 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_NUMBER
) &&
654 (id
->bInterfaceNumber
!= intf
->desc
.bInterfaceNumber
))
660 /* returns 0 if no match, 1 if match */
661 int usb_match_one_id(struct usb_interface
*interface
,
662 const struct usb_device_id
*id
)
664 struct usb_host_interface
*intf
;
665 struct usb_device
*dev
;
667 /* proc_connectinfo in devio.c may call us with id == NULL. */
671 intf
= interface
->cur_altsetting
;
672 dev
= interface_to_usbdev(interface
);
674 if (!usb_match_device(dev
, id
))
677 return usb_match_one_id_intf(dev
, intf
, id
);
679 EXPORT_SYMBOL_GPL(usb_match_one_id
);
682 * usb_match_id - find first usb_device_id matching device or interface
683 * @interface: the interface of interest
684 * @id: array of usb_device_id structures, terminated by zero entry
686 * usb_match_id searches an array of usb_device_id's and returns
687 * the first one matching the device or interface, or null.
688 * This is used when binding (or rebinding) a driver to an interface.
689 * Most USB device drivers will use this indirectly, through the usb core,
690 * but some layered driver frameworks use it directly.
691 * These device tables are exported with MODULE_DEVICE_TABLE, through
692 * modutils, to support the driver loading functionality of USB hotplugging.
694 * Return: The first matching usb_device_id, or %NULL.
698 * The "match_flags" element in a usb_device_id controls which
699 * members are used. If the corresponding bit is set, the
700 * value in the device_id must match its corresponding member
701 * in the device or interface descriptor, or else the device_id
704 * "driver_info" is normally used only by device drivers,
705 * but you can create a wildcard "matches anything" usb_device_id
706 * as a driver's "modules.usbmap" entry if you provide an id with
707 * only a nonzero "driver_info" field. If you do this, the USB device
708 * driver's probe() routine should use additional intelligence to
709 * decide whether to bind to the specified interface.
711 * What Makes Good usb_device_id Tables:
713 * The match algorithm is very simple, so that intelligence in
714 * driver selection must come from smart driver id records.
715 * Unless you have good reasons to use another selection policy,
716 * provide match elements only in related groups, and order match
717 * specifiers from specific to general. Use the macros provided
718 * for that purpose if you can.
720 * The most specific match specifiers use device descriptor
721 * data. These are commonly used with product-specific matches;
722 * the USB_DEVICE macro lets you provide vendor and product IDs,
723 * and you can also match against ranges of product revisions.
724 * These are widely used for devices with application or vendor
725 * specific bDeviceClass values.
727 * Matches based on device class/subclass/protocol specifications
728 * are slightly more general; use the USB_DEVICE_INFO macro, or
729 * its siblings. These are used with single-function devices
730 * where bDeviceClass doesn't specify that each interface has
733 * Matches based on interface class/subclass/protocol are the
734 * most general; they let drivers bind to any interface on a
735 * multiple-function device. Use the USB_INTERFACE_INFO
736 * macro, or its siblings, to match class-per-interface style
737 * devices (as recorded in bInterfaceClass).
739 * Note that an entry created by USB_INTERFACE_INFO won't match
740 * any interface if the device class is set to Vendor-Specific.
741 * This is deliberate; according to the USB spec the meanings of
742 * the interface class/subclass/protocol for these devices are also
743 * vendor-specific, and hence matching against a standard product
744 * class wouldn't work anyway. If you really want to use an
745 * interface-based match for such a device, create a match record
746 * that also specifies the vendor ID. (Unforunately there isn't a
747 * standard macro for creating records like this.)
749 * Within those groups, remember that not all combinations are
750 * meaningful. For example, don't give a product version range
751 * without vendor and product IDs; or specify a protocol without
752 * its associated class and subclass.
754 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
755 const struct usb_device_id
*id
)
757 /* proc_connectinfo in devio.c may call us with id == NULL. */
761 /* It is important to check that id->driver_info is nonzero,
762 since an entry that is all zeroes except for a nonzero
763 id->driver_info is the way to create an entry that
764 indicates that the driver want to examine every
765 device and interface. */
766 for (; id
->idVendor
|| id
->idProduct
|| id
->bDeviceClass
||
767 id
->bInterfaceClass
|| id
->driver_info
; id
++) {
768 if (usb_match_one_id(interface
, id
))
774 EXPORT_SYMBOL_GPL(usb_match_id
);
776 static int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
778 /* devices and interfaces are handled separately */
779 if (is_usb_device(dev
)) {
781 /* interface drivers never match devices */
782 if (!is_usb_device_driver(drv
))
785 /* TODO: Add real matching code */
788 } else if (is_usb_interface(dev
)) {
789 struct usb_interface
*intf
;
790 struct usb_driver
*usb_drv
;
791 const struct usb_device_id
*id
;
793 /* device drivers never match interfaces */
794 if (is_usb_device_driver(drv
))
797 intf
= to_usb_interface(dev
);
798 usb_drv
= to_usb_driver(drv
);
800 id
= usb_match_id(intf
, usb_drv
->id_table
);
804 id
= usb_match_dynamic_id(intf
, usb_drv
);
812 static int usb_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
814 struct usb_device
*usb_dev
;
816 if (is_usb_device(dev
)) {
817 usb_dev
= to_usb_device(dev
);
818 } else if (is_usb_interface(dev
)) {
819 struct usb_interface
*intf
= to_usb_interface(dev
);
821 usb_dev
= interface_to_usbdev(intf
);
826 if (usb_dev
->devnum
< 0) {
827 /* driver is often null here; dev_dbg() would oops */
828 pr_debug("usb %s: already deleted?\n", dev_name(dev
));
832 pr_debug("usb %s: bus removed?\n", dev_name(dev
));
836 /* per-device configurations are common */
837 if (add_uevent_var(env
, "PRODUCT=%x/%x/%x",
838 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
839 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
840 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
)))
843 /* class-based driver binding models */
844 if (add_uevent_var(env
, "TYPE=%d/%d/%d",
845 usb_dev
->descriptor
.bDeviceClass
,
846 usb_dev
->descriptor
.bDeviceSubClass
,
847 usb_dev
->descriptor
.bDeviceProtocol
))
854 * usb_register_device_driver - register a USB device (not interface) driver
855 * @new_udriver: USB operations for the device driver
856 * @owner: module owner of this driver.
858 * Registers a USB device driver with the USB core. The list of
859 * unattached devices will be rescanned whenever a new driver is
860 * added, allowing the new driver to attach to any recognized devices.
862 * Return: A negative error code on failure and 0 on success.
864 int usb_register_device_driver(struct usb_device_driver
*new_udriver
,
865 struct module
*owner
)
872 new_udriver
->drvwrap
.for_devices
= 1;
873 new_udriver
->drvwrap
.driver
.name
= new_udriver
->name
;
874 new_udriver
->drvwrap
.driver
.bus
= &usb_bus_type
;
875 new_udriver
->drvwrap
.driver
.probe
= usb_probe_device
;
876 new_udriver
->drvwrap
.driver
.remove
= usb_unbind_device
;
877 new_udriver
->drvwrap
.driver
.owner
= owner
;
879 retval
= driver_register(&new_udriver
->drvwrap
.driver
);
882 pr_info("%s: registered new device driver %s\n",
883 usbcore_name
, new_udriver
->name
);
885 printk(KERN_ERR
"%s: error %d registering device "
887 usbcore_name
, retval
, new_udriver
->name
);
891 EXPORT_SYMBOL_GPL(usb_register_device_driver
);
894 * usb_deregister_device_driver - unregister a USB device (not interface) driver
895 * @udriver: USB operations of the device driver to unregister
896 * Context: must be able to sleep
898 * Unlinks the specified driver from the internal USB driver list.
900 void usb_deregister_device_driver(struct usb_device_driver
*udriver
)
902 pr_info("%s: deregistering device driver %s\n",
903 usbcore_name
, udriver
->name
);
905 driver_unregister(&udriver
->drvwrap
.driver
);
907 EXPORT_SYMBOL_GPL(usb_deregister_device_driver
);
910 * usb_register_driver - register a USB interface driver
911 * @new_driver: USB operations for the interface driver
912 * @owner: module owner of this driver.
913 * @mod_name: module name string
915 * Registers a USB interface driver with the USB core. The list of
916 * unattached interfaces will be rescanned whenever a new driver is
917 * added, allowing the new driver to attach to any recognized interfaces.
919 * Return: A negative error code on failure and 0 on success.
921 * NOTE: if you want your driver to use the USB major number, you must call
922 * usb_register_dev() to enable that functionality. This function no longer
923 * takes care of that.
925 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
,
926 const char *mod_name
)
933 new_driver
->drvwrap
.for_devices
= 0;
934 new_driver
->drvwrap
.driver
.name
= new_driver
->name
;
935 new_driver
->drvwrap
.driver
.bus
= &usb_bus_type
;
936 new_driver
->drvwrap
.driver
.probe
= usb_probe_interface
;
937 new_driver
->drvwrap
.driver
.remove
= usb_unbind_interface
;
938 new_driver
->drvwrap
.driver
.owner
= owner
;
939 new_driver
->drvwrap
.driver
.mod_name
= mod_name
;
940 spin_lock_init(&new_driver
->dynids
.lock
);
941 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
943 retval
= driver_register(&new_driver
->drvwrap
.driver
);
947 retval
= usb_create_newid_files(new_driver
);
951 pr_info("%s: registered new interface driver %s\n",
952 usbcore_name
, new_driver
->name
);
958 driver_unregister(&new_driver
->drvwrap
.driver
);
960 printk(KERN_ERR
"%s: error %d registering interface "
962 usbcore_name
, retval
, new_driver
->name
);
965 EXPORT_SYMBOL_GPL(usb_register_driver
);
968 * usb_deregister - unregister a USB interface driver
969 * @driver: USB operations of the interface driver to unregister
970 * Context: must be able to sleep
972 * Unlinks the specified driver from the internal USB driver list.
974 * NOTE: If you called usb_register_dev(), you still need to call
975 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
976 * this * call will no longer do it for you.
978 void usb_deregister(struct usb_driver
*driver
)
980 pr_info("%s: deregistering interface driver %s\n",
981 usbcore_name
, driver
->name
);
983 usb_remove_newid_files(driver
);
984 driver_unregister(&driver
->drvwrap
.driver
);
985 usb_free_dynids(driver
);
987 EXPORT_SYMBOL_GPL(usb_deregister
);
989 /* Forced unbinding of a USB interface driver, either because
990 * it doesn't support pre_reset/post_reset/reset_resume or
991 * because it doesn't support suspend/resume.
993 * The caller must hold @intf's device's lock, but not its pm_mutex
994 * and not @intf->dev.sem.
996 void usb_forced_unbind_intf(struct usb_interface
*intf
)
998 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
1000 dev_dbg(&intf
->dev
, "forced unbind\n");
1001 usb_driver_release_interface(driver
, intf
);
1003 /* Mark the interface for later rebinding */
1004 intf
->needs_binding
= 1;
1007 /* Delayed forced unbinding of a USB interface driver and scan
1010 * The caller must hold @intf's device's lock, but not its pm_mutex
1011 * and not @intf->dev.sem.
1013 * Note: Rebinds will be skipped if a system sleep transition is in
1014 * progress and the PM "complete" callback hasn't occurred yet.
1016 void usb_rebind_intf(struct usb_interface
*intf
)
1020 /* Delayed unbind of an existing driver */
1021 if (intf
->dev
.driver
)
1022 usb_forced_unbind_intf(intf
);
1024 /* Try to rebind the interface */
1025 if (!intf
->dev
.power
.is_prepared
) {
1026 intf
->needs_binding
= 0;
1027 rc
= device_attach(&intf
->dev
);
1029 dev_warn(&intf
->dev
, "rebind failed: %d\n", rc
);
1035 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
1036 * There is no check for reset_resume here because it can be determined
1037 * only during resume whether reset_resume is needed.
1039 * The caller must hold @udev's device lock.
1041 static void unbind_no_pm_drivers_interfaces(struct usb_device
*udev
)
1043 struct usb_host_config
*config
;
1045 struct usb_interface
*intf
;
1046 struct usb_driver
*drv
;
1048 config
= udev
->actconfig
;
1050 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1051 intf
= config
->interface
[i
];
1053 if (intf
->dev
.driver
) {
1054 drv
= to_usb_driver(intf
->dev
.driver
);
1055 if (!drv
->suspend
|| !drv
->resume
)
1056 usb_forced_unbind_intf(intf
);
1062 /* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1063 * These interfaces have the needs_binding flag set by usb_resume_interface().
1065 * The caller must hold @udev's device lock.
1067 static void unbind_no_reset_resume_drivers_interfaces(struct usb_device
*udev
)
1069 struct usb_host_config
*config
;
1071 struct usb_interface
*intf
;
1073 config
= udev
->actconfig
;
1075 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1076 intf
= config
->interface
[i
];
1077 if (intf
->dev
.driver
&& intf
->needs_binding
)
1078 usb_forced_unbind_intf(intf
);
1083 static void do_rebind_interfaces(struct usb_device
*udev
)
1085 struct usb_host_config
*config
;
1087 struct usb_interface
*intf
;
1089 config
= udev
->actconfig
;
1091 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1092 intf
= config
->interface
[i
];
1093 if (intf
->needs_binding
)
1094 usb_rebind_intf(intf
);
1099 static int usb_suspend_device(struct usb_device
*udev
, pm_message_t msg
)
1101 struct usb_device_driver
*udriver
;
1104 if (udev
->state
== USB_STATE_NOTATTACHED
||
1105 udev
->state
== USB_STATE_SUSPENDED
)
1108 /* For devices that don't have a driver, we do a generic suspend. */
1109 if (udev
->dev
.driver
)
1110 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1112 udev
->do_remote_wakeup
= 0;
1113 udriver
= &usb_generic_driver
;
1115 status
= udriver
->suspend(udev
, msg
);
1118 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1122 static int usb_resume_device(struct usb_device
*udev
, pm_message_t msg
)
1124 struct usb_device_driver
*udriver
;
1127 if (udev
->state
== USB_STATE_NOTATTACHED
)
1130 /* Can't resume it if it doesn't have a driver. */
1131 if (udev
->dev
.driver
== NULL
) {
1136 /* Non-root devices on a full/low-speed bus must wait for their
1137 * companion high-speed root hub, in case a handoff is needed.
1139 if (!PMSG_IS_AUTO(msg
) && udev
->parent
&& udev
->bus
->hs_companion
)
1140 device_pm_wait_for_dev(&udev
->dev
,
1141 &udev
->bus
->hs_companion
->root_hub
->dev
);
1143 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
)
1144 udev
->reset_resume
= 1;
1146 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1147 status
= udriver
->resume(udev
, msg
);
1150 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1154 static int usb_suspend_interface(struct usb_device
*udev
,
1155 struct usb_interface
*intf
, pm_message_t msg
)
1157 struct usb_driver
*driver
;
1160 if (udev
->state
== USB_STATE_NOTATTACHED
||
1161 intf
->condition
== USB_INTERFACE_UNBOUND
)
1163 driver
= to_usb_driver(intf
->dev
.driver
);
1165 /* at this time we know the driver supports suspend */
1166 status
= driver
->suspend(intf
, msg
);
1167 if (status
&& !PMSG_IS_AUTO(msg
))
1168 dev_err(&intf
->dev
, "suspend error %d\n", status
);
1171 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1175 static int usb_resume_interface(struct usb_device
*udev
,
1176 struct usb_interface
*intf
, pm_message_t msg
, int reset_resume
)
1178 struct usb_driver
*driver
;
1181 if (udev
->state
== USB_STATE_NOTATTACHED
)
1184 /* Don't let autoresume interfere with unbinding */
1185 if (intf
->condition
== USB_INTERFACE_UNBINDING
)
1188 /* Can't resume it if it doesn't have a driver. */
1189 if (intf
->condition
== USB_INTERFACE_UNBOUND
) {
1191 /* Carry out a deferred switch to altsetting 0 */
1192 if (intf
->needs_altsetting0
&& !intf
->dev
.power
.is_prepared
) {
1193 usb_set_interface(udev
, intf
->altsetting
[0].
1194 desc
.bInterfaceNumber
, 0);
1195 intf
->needs_altsetting0
= 0;
1200 /* Don't resume if the interface is marked for rebinding */
1201 if (intf
->needs_binding
)
1203 driver
= to_usb_driver(intf
->dev
.driver
);
1206 if (driver
->reset_resume
) {
1207 status
= driver
->reset_resume(intf
);
1209 dev_err(&intf
->dev
, "%s error %d\n",
1210 "reset_resume", status
);
1212 intf
->needs_binding
= 1;
1213 dev_dbg(&intf
->dev
, "no reset_resume for driver %s?\n",
1217 status
= driver
->resume(intf
);
1219 dev_err(&intf
->dev
, "resume error %d\n", status
);
1223 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1225 /* Later we will unbind the driver and/or reprobe, if necessary */
1230 * usb_suspend_both - suspend a USB device and its interfaces
1231 * @udev: the usb_device to suspend
1232 * @msg: Power Management message describing this state transition
1234 * This is the central routine for suspending USB devices. It calls the
1235 * suspend methods for all the interface drivers in @udev and then calls
1236 * the suspend method for @udev itself. When the routine is called in
1237 * autosuspend, if an error occurs at any stage, all the interfaces
1238 * which were suspended are resumed so that they remain in the same
1239 * state as the device, but when called from system sleep, all error
1240 * from suspend methods of interfaces and the non-root-hub device itself
1241 * are simply ignored, so all suspended interfaces are only resumed
1242 * to the device's state when @udev is root-hub and its suspend method
1245 * Autosuspend requests originating from a child device or an interface
1246 * driver may be made without the protection of @udev's device lock, but
1247 * all other suspend calls will hold the lock. Usbcore will insure that
1248 * method calls do not arrive during bind, unbind, or reset operations.
1249 * However drivers must be prepared to handle suspend calls arriving at
1250 * unpredictable times.
1252 * This routine can run only in process context.
1254 * Return: 0 if the suspend succeeded.
1256 static int usb_suspend_both(struct usb_device
*udev
, pm_message_t msg
)
1260 struct usb_interface
*intf
;
1262 if (udev
->state
== USB_STATE_NOTATTACHED
||
1263 udev
->state
== USB_STATE_SUSPENDED
)
1266 /* Suspend all the interfaces and then udev itself */
1267 if (udev
->actconfig
) {
1268 n
= udev
->actconfig
->desc
.bNumInterfaces
;
1269 for (i
= n
- 1; i
>= 0; --i
) {
1270 intf
= udev
->actconfig
->interface
[i
];
1271 status
= usb_suspend_interface(udev
, intf
, msg
);
1273 /* Ignore errors during system sleep transitions */
1274 if (!PMSG_IS_AUTO(msg
))
1281 status
= usb_suspend_device(udev
, msg
);
1284 * Ignore errors from non-root-hub devices during
1285 * system sleep transitions. For the most part,
1286 * these devices should go to low power anyway when
1287 * the entire bus is suspended.
1289 if (udev
->parent
&& !PMSG_IS_AUTO(msg
))
1293 /* If the suspend failed, resume interfaces that did get suspended */
1295 if (udev
->actconfig
) {
1296 msg
.event
^= (PM_EVENT_SUSPEND
| PM_EVENT_RESUME
);
1298 intf
= udev
->actconfig
->interface
[i
];
1299 usb_resume_interface(udev
, intf
, msg
, 0);
1303 /* If the suspend succeeded then prevent any more URB submissions
1304 * and flush any outstanding URBs.
1307 udev
->can_submit
= 0;
1308 for (i
= 0; i
< 16; ++i
) {
1309 usb_hcd_flush_endpoint(udev
, udev
->ep_out
[i
]);
1310 usb_hcd_flush_endpoint(udev
, udev
->ep_in
[i
]);
1315 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1320 * usb_resume_both - resume a USB device and its interfaces
1321 * @udev: the usb_device to resume
1322 * @msg: Power Management message describing this state transition
1324 * This is the central routine for resuming USB devices. It calls the
1325 * the resume method for @udev and then calls the resume methods for all
1326 * the interface drivers in @udev.
1328 * Autoresume requests originating from a child device or an interface
1329 * driver may be made without the protection of @udev's device lock, but
1330 * all other resume calls will hold the lock. Usbcore will insure that
1331 * method calls do not arrive during bind, unbind, or reset operations.
1332 * However drivers must be prepared to handle resume calls arriving at
1333 * unpredictable times.
1335 * This routine can run only in process context.
1337 * Return: 0 on success.
1339 static int usb_resume_both(struct usb_device
*udev
, pm_message_t msg
)
1343 struct usb_interface
*intf
;
1345 if (udev
->state
== USB_STATE_NOTATTACHED
) {
1349 udev
->can_submit
= 1;
1351 /* Resume the device */
1352 if (udev
->state
== USB_STATE_SUSPENDED
|| udev
->reset_resume
)
1353 status
= usb_resume_device(udev
, msg
);
1355 /* Resume the interfaces */
1356 if (status
== 0 && udev
->actconfig
) {
1357 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1358 intf
= udev
->actconfig
->interface
[i
];
1359 usb_resume_interface(udev
, intf
, msg
,
1360 udev
->reset_resume
);
1363 usb_mark_last_busy(udev
);
1366 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1368 udev
->reset_resume
= 0;
1372 static void choose_wakeup(struct usb_device
*udev
, pm_message_t msg
)
1376 /* Remote wakeup is needed only when we actually go to sleep.
1377 * For things like FREEZE and QUIESCE, if the device is already
1378 * autosuspended then its current wakeup setting is okay.
1380 if (msg
.event
== PM_EVENT_FREEZE
|| msg
.event
== PM_EVENT_QUIESCE
) {
1381 if (udev
->state
!= USB_STATE_SUSPENDED
)
1382 udev
->do_remote_wakeup
= 0;
1386 /* Enable remote wakeup if it is allowed, even if no interface drivers
1389 w
= device_may_wakeup(&udev
->dev
);
1391 /* If the device is autosuspended with the wrong wakeup setting,
1392 * autoresume now so the setting can be changed.
1394 if (udev
->state
== USB_STATE_SUSPENDED
&& w
!= udev
->do_remote_wakeup
)
1395 pm_runtime_resume(&udev
->dev
);
1396 udev
->do_remote_wakeup
= w
;
1399 /* The device lock is held by the PM core */
1400 int usb_suspend(struct device
*dev
, pm_message_t msg
)
1402 struct usb_device
*udev
= to_usb_device(dev
);
1404 unbind_no_pm_drivers_interfaces(udev
);
1406 /* From now on we are sure all drivers support suspend/resume
1407 * but not necessarily reset_resume()
1408 * so we may still need to unbind and rebind upon resume
1410 choose_wakeup(udev
, msg
);
1411 return usb_suspend_both(udev
, msg
);
1414 /* The device lock is held by the PM core */
1415 int usb_resume_complete(struct device
*dev
)
1417 struct usb_device
*udev
= to_usb_device(dev
);
1419 /* For PM complete calls, all we do is rebind interfaces
1420 * whose needs_binding flag is set
1422 if (udev
->state
!= USB_STATE_NOTATTACHED
)
1423 do_rebind_interfaces(udev
);
1427 /* The device lock is held by the PM core */
1428 int usb_resume(struct device
*dev
, pm_message_t msg
)
1430 struct usb_device
*udev
= to_usb_device(dev
);
1433 /* For all calls, take the device back to full power and
1434 * tell the PM core in case it was autosuspended previously.
1435 * Unbind the interfaces that will need rebinding later,
1436 * because they fail to support reset_resume.
1437 * (This can't be done in usb_resume_interface()
1438 * above because it doesn't own the right set of locks.)
1440 status
= usb_resume_both(udev
, msg
);
1442 pm_runtime_disable(dev
);
1443 pm_runtime_set_active(dev
);
1444 pm_runtime_enable(dev
);
1445 unbind_no_reset_resume_drivers_interfaces(udev
);
1448 /* Avoid PM error messages for devices disconnected while suspended
1449 * as we'll display regular disconnect messages just a bit later.
1451 if (status
== -ENODEV
|| status
== -ESHUTDOWN
)
1456 #endif /* CONFIG_PM */
1458 #ifdef CONFIG_PM_RUNTIME
1461 * usb_enable_autosuspend - allow a USB device to be autosuspended
1462 * @udev: the USB device which may be autosuspended
1464 * This routine allows @udev to be autosuspended. An autosuspend won't
1465 * take place until the autosuspend_delay has elapsed and all the other
1466 * necessary conditions are satisfied.
1468 * The caller must hold @udev's device lock.
1470 void usb_enable_autosuspend(struct usb_device
*udev
)
1472 pm_runtime_allow(&udev
->dev
);
1474 EXPORT_SYMBOL_GPL(usb_enable_autosuspend
);
1477 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1478 * @udev: the USB device which may not be autosuspended
1480 * This routine prevents @udev from being autosuspended and wakes it up
1481 * if it is already autosuspended.
1483 * The caller must hold @udev's device lock.
1485 void usb_disable_autosuspend(struct usb_device
*udev
)
1487 pm_runtime_forbid(&udev
->dev
);
1489 EXPORT_SYMBOL_GPL(usb_disable_autosuspend
);
1492 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1493 * @udev: the usb_device to autosuspend
1495 * This routine should be called when a core subsystem is finished using
1496 * @udev and wants to allow it to autosuspend. Examples would be when
1497 * @udev's device file in usbfs is closed or after a configuration change.
1499 * @udev's usage counter is decremented; if it drops to 0 and all the
1500 * interfaces are inactive then a delayed autosuspend will be attempted.
1501 * The attempt may fail (see autosuspend_check()).
1503 * The caller must hold @udev's device lock.
1505 * This routine can run only in process context.
1507 void usb_autosuspend_device(struct usb_device
*udev
)
1511 usb_mark_last_busy(udev
);
1512 status
= pm_runtime_put_sync_autosuspend(&udev
->dev
);
1513 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1514 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1519 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1520 * @udev: the usb_device to autoresume
1522 * This routine should be called when a core subsystem wants to use @udev
1523 * and needs to guarantee that it is not suspended. No autosuspend will
1524 * occur until usb_autosuspend_device() is called. (Note that this will
1525 * not prevent suspend events originating in the PM core.) Examples would
1526 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1527 * request is received.
1529 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1530 * However if the autoresume fails then the usage counter is re-decremented.
1532 * The caller must hold @udev's device lock.
1534 * This routine can run only in process context.
1536 * Return: 0 on success. A negative error code otherwise.
1538 int usb_autoresume_device(struct usb_device
*udev
)
1542 status
= pm_runtime_get_sync(&udev
->dev
);
1544 pm_runtime_put_sync(&udev
->dev
);
1545 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1546 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1554 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1555 * @intf: the usb_interface whose counter should be decremented
1557 * This routine should be called by an interface driver when it is
1558 * finished using @intf and wants to allow it to autosuspend. A typical
1559 * example would be a character-device driver when its device file is
1562 * The routine decrements @intf's usage counter. When the counter reaches
1563 * 0, a delayed autosuspend request for @intf's device is attempted. The
1564 * attempt may fail (see autosuspend_check()).
1566 * This routine can run only in process context.
1568 void usb_autopm_put_interface(struct usb_interface
*intf
)
1570 struct usb_device
*udev
= interface_to_usbdev(intf
);
1573 usb_mark_last_busy(udev
);
1574 atomic_dec(&intf
->pm_usage_cnt
);
1575 status
= pm_runtime_put_sync(&intf
->dev
);
1576 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1577 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1580 EXPORT_SYMBOL_GPL(usb_autopm_put_interface
);
1583 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1584 * @intf: the usb_interface whose counter should be decremented
1586 * This routine does much the same thing as usb_autopm_put_interface():
1587 * It decrements @intf's usage counter and schedules a delayed
1588 * autosuspend request if the counter is <= 0. The difference is that it
1589 * does not perform any synchronization; callers should hold a private
1590 * lock and handle all synchronization issues themselves.
1592 * Typically a driver would call this routine during an URB's completion
1593 * handler, if no more URBs were pending.
1595 * This routine can run in atomic context.
1597 void usb_autopm_put_interface_async(struct usb_interface
*intf
)
1599 struct usb_device
*udev
= interface_to_usbdev(intf
);
1602 usb_mark_last_busy(udev
);
1603 atomic_dec(&intf
->pm_usage_cnt
);
1604 status
= pm_runtime_put(&intf
->dev
);
1605 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1606 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1609 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async
);
1612 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1613 * @intf: the usb_interface whose counter should be decremented
1615 * This routine decrements @intf's usage counter but does not carry out an
1618 * This routine can run in atomic context.
1620 void usb_autopm_put_interface_no_suspend(struct usb_interface
*intf
)
1622 struct usb_device
*udev
= interface_to_usbdev(intf
);
1624 usb_mark_last_busy(udev
);
1625 atomic_dec(&intf
->pm_usage_cnt
);
1626 pm_runtime_put_noidle(&intf
->dev
);
1628 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend
);
1631 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1632 * @intf: the usb_interface whose counter should be incremented
1634 * This routine should be called by an interface driver when it wants to
1635 * use @intf and needs to guarantee that it is not suspended. In addition,
1636 * the routine prevents @intf from being autosuspended subsequently. (Note
1637 * that this will not prevent suspend events originating in the PM core.)
1638 * This prevention will persist until usb_autopm_put_interface() is called
1639 * or @intf is unbound. A typical example would be a character-device
1640 * driver when its device file is opened.
1642 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1643 * However if the autoresume fails then the counter is re-decremented.
1645 * This routine can run only in process context.
1647 * Return: 0 on success.
1649 int usb_autopm_get_interface(struct usb_interface
*intf
)
1653 status
= pm_runtime_get_sync(&intf
->dev
);
1655 pm_runtime_put_sync(&intf
->dev
);
1657 atomic_inc(&intf
->pm_usage_cnt
);
1658 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1659 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1665 EXPORT_SYMBOL_GPL(usb_autopm_get_interface
);
1668 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1669 * @intf: the usb_interface whose counter should be incremented
1671 * This routine does much the same thing as
1672 * usb_autopm_get_interface(): It increments @intf's usage counter and
1673 * queues an autoresume request if the device is suspended. The
1674 * differences are that it does not perform any synchronization (callers
1675 * should hold a private lock and handle all synchronization issues
1676 * themselves), and it does not autoresume the device directly (it only
1677 * queues a request). After a successful call, the device may not yet be
1680 * This routine can run in atomic context.
1682 * Return: 0 on success. A negative error code otherwise.
1684 int usb_autopm_get_interface_async(struct usb_interface
*intf
)
1688 status
= pm_runtime_get(&intf
->dev
);
1689 if (status
< 0 && status
!= -EINPROGRESS
)
1690 pm_runtime_put_noidle(&intf
->dev
);
1692 atomic_inc(&intf
->pm_usage_cnt
);
1693 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1694 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1696 if (status
> 0 || status
== -EINPROGRESS
)
1700 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async
);
1703 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1704 * @intf: the usb_interface whose counter should be incremented
1706 * This routine increments @intf's usage counter but does not carry out an
1709 * This routine can run in atomic context.
1711 void usb_autopm_get_interface_no_resume(struct usb_interface
*intf
)
1713 struct usb_device
*udev
= interface_to_usbdev(intf
);
1715 usb_mark_last_busy(udev
);
1716 atomic_inc(&intf
->pm_usage_cnt
);
1717 pm_runtime_get_noresume(&intf
->dev
);
1719 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume
);
1721 /* Internal routine to check whether we may autosuspend a device. */
1722 static int autosuspend_check(struct usb_device
*udev
)
1725 struct usb_interface
*intf
;
1727 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1728 * any interface drivers require remote wakeup but it isn't available.
1731 if (udev
->actconfig
) {
1732 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1733 intf
= udev
->actconfig
->interface
[i
];
1735 /* We don't need to check interfaces that are
1736 * disabled for runtime PM. Either they are unbound
1737 * or else their drivers don't support autosuspend
1738 * and so they are permanently active.
1740 if (intf
->dev
.power
.disable_depth
)
1742 if (atomic_read(&intf
->dev
.power
.usage_count
) > 0)
1744 w
|= intf
->needs_remote_wakeup
;
1746 /* Don't allow autosuspend if the device will need
1747 * a reset-resume and any of its interface drivers
1748 * doesn't include support or needs remote wakeup.
1750 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
) {
1751 struct usb_driver
*driver
;
1753 driver
= to_usb_driver(intf
->dev
.driver
);
1754 if (!driver
->reset_resume
||
1755 intf
->needs_remote_wakeup
)
1760 if (w
&& !device_can_wakeup(&udev
->dev
)) {
1761 dev_dbg(&udev
->dev
, "remote wakeup needed for autosuspend\n");
1764 udev
->do_remote_wakeup
= w
;
1768 int usb_runtime_suspend(struct device
*dev
)
1770 struct usb_device
*udev
= to_usb_device(dev
);
1773 /* A USB device can be suspended if it passes the various autosuspend
1774 * checks. Runtime suspend for a USB device means suspending all the
1775 * interfaces and then the device itself.
1777 if (autosuspend_check(udev
) != 0)
1780 status
= usb_suspend_both(udev
, PMSG_AUTO_SUSPEND
);
1782 /* Allow a retry if autosuspend failed temporarily */
1783 if (status
== -EAGAIN
|| status
== -EBUSY
)
1784 usb_mark_last_busy(udev
);
1786 /* The PM core reacts badly unless the return code is 0,
1787 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1794 int usb_runtime_resume(struct device
*dev
)
1796 struct usb_device
*udev
= to_usb_device(dev
);
1799 /* Runtime resume for a USB device means resuming both the device
1800 * and all its interfaces.
1802 status
= usb_resume_both(udev
, PMSG_AUTO_RESUME
);
1806 int usb_runtime_idle(struct device
*dev
)
1808 struct usb_device
*udev
= to_usb_device(dev
);
1810 /* An idle USB device can be suspended if it passes the various
1811 * autosuspend checks.
1813 if (autosuspend_check(udev
) == 0)
1814 pm_runtime_autosuspend(dev
);
1815 /* Tell the core not to suspend it, though. */
1819 int usb_set_usb2_hardware_lpm(struct usb_device
*udev
, int enable
)
1821 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1824 if (enable
&& !udev
->usb2_hw_lpm_allowed
)
1827 if (hcd
->driver
->set_usb2_hw_lpm
) {
1828 ret
= hcd
->driver
->set_usb2_hw_lpm(hcd
, udev
, enable
);
1830 udev
->usb2_hw_lpm_enabled
= enable
;
1836 #endif /* CONFIG_PM_RUNTIME */
1838 struct bus_type usb_bus_type
= {
1840 .match
= usb_device_match
,
1841 .uevent
= usb_uevent
,