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)
69 dynid
->id
.bInterfaceClass
= (u8
)bInterfaceClass
;
70 dynid
->id
.match_flags
|= USB_DEVICE_ID_MATCH_INT_CLASS
;
74 const struct usb_device_id
*id
= id_table
;
79 for (; id
->match_flags
; id
++)
80 if (id
->idVendor
== refVendor
&& id
->idProduct
== refProduct
)
84 dynid
->id
.driver_info
= id
->driver_info
;
89 spin_lock(&dynids
->lock
);
90 list_add_tail(&dynid
->node
, &dynids
->list
);
91 spin_unlock(&dynids
->lock
);
93 retval
= driver_attach(driver
);
99 EXPORT_SYMBOL_GPL(usb_store_new_id
);
101 ssize_t
usb_show_dynids(struct usb_dynids
*dynids
, char *buf
)
103 struct usb_dynid
*dynid
;
106 list_for_each_entry(dynid
, &dynids
->list
, node
)
107 if (dynid
->id
.bInterfaceClass
!= 0)
108 count
+= scnprintf(&buf
[count
], PAGE_SIZE
- count
, "%04x %04x %02x\n",
109 dynid
->id
.idVendor
, dynid
->id
.idProduct
,
110 dynid
->id
.bInterfaceClass
);
112 count
+= scnprintf(&buf
[count
], PAGE_SIZE
- count
, "%04x %04x\n",
113 dynid
->id
.idVendor
, dynid
->id
.idProduct
);
116 EXPORT_SYMBOL_GPL(usb_show_dynids
);
118 static ssize_t
new_id_show(struct device_driver
*driver
, char *buf
)
120 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
122 return usb_show_dynids(&usb_drv
->dynids
, buf
);
125 static ssize_t
new_id_store(struct device_driver
*driver
,
126 const char *buf
, size_t count
)
128 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
130 return usb_store_new_id(&usb_drv
->dynids
, usb_drv
->id_table
, driver
, buf
, count
);
132 static DRIVER_ATTR_RW(new_id
);
135 * Remove a USB device ID from this driver
137 static ssize_t
remove_id_store(struct device_driver
*driver
, const char *buf
,
140 struct usb_dynid
*dynid
, *n
;
141 struct usb_driver
*usb_driver
= to_usb_driver(driver
);
146 fields
= sscanf(buf
, "%x %x", &idVendor
, &idProduct
);
150 spin_lock(&usb_driver
->dynids
.lock
);
151 list_for_each_entry_safe(dynid
, n
, &usb_driver
->dynids
.list
, node
) {
152 struct usb_device_id
*id
= &dynid
->id
;
153 if ((id
->idVendor
== idVendor
) &&
154 (id
->idProduct
== idProduct
)) {
155 list_del(&dynid
->node
);
160 spin_unlock(&usb_driver
->dynids
.lock
);
164 static ssize_t
remove_id_show(struct device_driver
*driver
, char *buf
)
166 return new_id_show(driver
, buf
);
168 static DRIVER_ATTR_RW(remove_id
);
170 static int usb_create_newid_files(struct usb_driver
*usb_drv
)
174 if (usb_drv
->no_dynamic_id
)
177 if (usb_drv
->probe
!= NULL
) {
178 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
179 &driver_attr_new_id
);
181 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
182 &driver_attr_remove_id
);
184 driver_remove_file(&usb_drv
->drvwrap
.driver
,
185 &driver_attr_new_id
);
192 static void usb_remove_newid_files(struct usb_driver
*usb_drv
)
194 if (usb_drv
->no_dynamic_id
)
197 if (usb_drv
->probe
!= NULL
) {
198 driver_remove_file(&usb_drv
->drvwrap
.driver
,
199 &driver_attr_remove_id
);
200 driver_remove_file(&usb_drv
->drvwrap
.driver
,
201 &driver_attr_new_id
);
205 static void usb_free_dynids(struct usb_driver
*usb_drv
)
207 struct usb_dynid
*dynid
, *n
;
209 spin_lock(&usb_drv
->dynids
.lock
);
210 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
211 list_del(&dynid
->node
);
214 spin_unlock(&usb_drv
->dynids
.lock
);
217 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
218 struct usb_driver
*drv
)
220 struct usb_dynid
*dynid
;
222 spin_lock(&drv
->dynids
.lock
);
223 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
224 if (usb_match_one_id(intf
, &dynid
->id
)) {
225 spin_unlock(&drv
->dynids
.lock
);
229 spin_unlock(&drv
->dynids
.lock
);
234 /* called from driver core with dev locked */
235 static int usb_probe_device(struct device
*dev
)
237 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
238 struct usb_device
*udev
= to_usb_device(dev
);
241 dev_dbg(dev
, "%s\n", __func__
);
243 /* TODO: Add real matching code */
245 /* The device should always appear to be in use
246 * unless the driver supports autosuspend.
248 if (!udriver
->supports_autosuspend
)
249 error
= usb_autoresume_device(udev
);
252 error
= udriver
->probe(udev
);
256 /* called from driver core with dev locked */
257 static int usb_unbind_device(struct device
*dev
)
259 struct usb_device
*udev
= to_usb_device(dev
);
260 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
262 udriver
->disconnect(udev
);
263 if (!udriver
->supports_autosuspend
)
264 usb_autosuspend_device(udev
);
269 * Cancel any pending scheduled resets
271 * [see usb_queue_reset_device()]
273 * Called after unconfiguring / when releasing interfaces. See
274 * comments in __usb_queue_reset_device() regarding
275 * udev->reset_running.
277 static void usb_cancel_queued_reset(struct usb_interface
*iface
)
279 if (iface
->reset_running
== 0)
280 cancel_work_sync(&iface
->reset_ws
);
283 /* called from driver core with dev locked */
284 static int usb_probe_interface(struct device
*dev
)
286 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
287 struct usb_interface
*intf
= to_usb_interface(dev
);
288 struct usb_device
*udev
= interface_to_usbdev(intf
);
289 const struct usb_device_id
*id
;
291 int lpm_disable_error
;
293 dev_dbg(dev
, "%s\n", __func__
);
295 intf
->needs_binding
= 0;
297 if (usb_device_is_owned(udev
))
300 if (udev
->authorized
== 0) {
301 dev_err(&intf
->dev
, "Device is not authorized for usage\n");
305 id
= usb_match_id(intf
, driver
->id_table
);
307 id
= usb_match_dynamic_id(intf
, driver
);
311 dev_dbg(dev
, "%s - got id\n", __func__
);
313 error
= usb_autoresume_device(udev
);
317 intf
->condition
= USB_INTERFACE_BINDING
;
319 /* Probed interfaces are initially active. They are
320 * runtime-PM-enabled only if the driver has autosuspend support.
321 * They are sensitive to their children's power states.
323 pm_runtime_set_active(dev
);
324 pm_suspend_ignore_children(dev
, false);
325 if (driver
->supports_autosuspend
)
326 pm_runtime_enable(dev
);
328 /* If the new driver doesn't allow hub-initiated LPM, and we can't
329 * disable hub-initiated LPM, then fail the probe.
331 * Otherwise, leaving LPM enabled should be harmless, because the
332 * endpoint intervals should remain the same, and the U1/U2 timeouts
333 * should remain the same.
335 * If we need to install alt setting 0 before probe, or another alt
336 * setting during probe, that should also be fine. usb_set_interface()
337 * will attempt to disable LPM, and fail if it can't disable it.
339 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
340 if (lpm_disable_error
&& driver
->disable_hub_initiated_lpm
) {
341 dev_err(&intf
->dev
, "%s Failed to disable LPM for driver %s\n.",
342 __func__
, driver
->name
);
343 error
= lpm_disable_error
;
347 /* Carry out a deferred switch to altsetting 0 */
348 if (intf
->needs_altsetting0
) {
349 error
= usb_set_interface(udev
, intf
->altsetting
[0].
350 desc
.bInterfaceNumber
, 0);
353 intf
->needs_altsetting0
= 0;
356 error
= driver
->probe(intf
, id
);
360 intf
->condition
= USB_INTERFACE_BOUND
;
362 /* If the LPM disable succeeded, balance the ref counts. */
363 if (!lpm_disable_error
)
364 usb_unlocked_enable_lpm(udev
);
366 usb_autosuspend_device(udev
);
370 usb_set_intfdata(intf
, NULL
);
371 intf
->needs_remote_wakeup
= 0;
372 intf
->condition
= USB_INTERFACE_UNBOUND
;
373 usb_cancel_queued_reset(intf
);
375 /* If the LPM disable succeeded, balance the ref counts. */
376 if (!lpm_disable_error
)
377 usb_unlocked_enable_lpm(udev
);
379 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
380 if (driver
->supports_autosuspend
)
381 pm_runtime_disable(dev
);
382 pm_runtime_set_suspended(dev
);
384 usb_autosuspend_device(udev
);
388 /* called from driver core with dev locked */
389 static int usb_unbind_interface(struct device
*dev
)
391 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
392 struct usb_interface
*intf
= to_usb_interface(dev
);
393 struct usb_device
*udev
;
394 int error
, r
, lpm_disable_error
;
396 intf
->condition
= USB_INTERFACE_UNBINDING
;
398 /* Autoresume for set_interface call below */
399 udev
= interface_to_usbdev(intf
);
400 error
= usb_autoresume_device(udev
);
402 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
403 * the driver is unbound. If LPM isn't disabled, that's fine because it
404 * wouldn't be enabled unless all the bound interfaces supported
407 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
409 /* Terminate all URBs for this interface unless the driver
410 * supports "soft" unbinding.
412 if (!driver
->soft_unbind
)
413 usb_disable_interface(udev
, intf
, false);
415 driver
->disconnect(intf
);
416 usb_cancel_queued_reset(intf
);
418 /* Reset other interface state.
419 * We cannot do a Set-Interface if the device is suspended or
420 * if it is prepared for a system sleep (since installing a new
421 * altsetting means creating new endpoint device entries).
422 * When either of these happens, defer the Set-Interface.
424 if (intf
->cur_altsetting
->desc
.bAlternateSetting
== 0) {
425 /* Already in altsetting 0 so skip Set-Interface.
426 * Just re-enable it without affecting the endpoint toggles.
428 usb_enable_interface(udev
, intf
, false);
429 } else if (!error
&& !intf
->dev
.power
.is_prepared
) {
430 r
= usb_set_interface(udev
, intf
->altsetting
[0].
431 desc
.bInterfaceNumber
, 0);
433 intf
->needs_altsetting0
= 1;
435 intf
->needs_altsetting0
= 1;
437 usb_set_intfdata(intf
, NULL
);
439 intf
->condition
= USB_INTERFACE_UNBOUND
;
440 intf
->needs_remote_wakeup
= 0;
442 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
443 if (!lpm_disable_error
)
444 usb_unlocked_enable_lpm(udev
);
446 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
447 if (driver
->supports_autosuspend
)
448 pm_runtime_disable(dev
);
449 pm_runtime_set_suspended(dev
);
451 /* Undo any residual pm_autopm_get_interface_* calls */
452 for (r
= atomic_read(&intf
->pm_usage_cnt
); r
> 0; --r
)
453 usb_autopm_put_interface_no_suspend(intf
);
454 atomic_set(&intf
->pm_usage_cnt
, 0);
457 usb_autosuspend_device(udev
);
463 * usb_driver_claim_interface - bind a driver to an interface
464 * @driver: the driver to be bound
465 * @iface: the interface to which it will be bound; must be in the
466 * usb device's active configuration
467 * @priv: driver data associated with that interface
469 * This is used by usb device drivers that need to claim more than one
470 * interface on a device when probing (audio and acm are current examples).
471 * No device driver should directly modify internal usb_interface or
472 * usb_device structure members.
474 * Few drivers should need to use this routine, since the most natural
475 * way to bind to an interface is to return the private data from
476 * the driver's probe() method.
478 * Callers must own the device lock, so driver probe() entries don't need
479 * extra locking, but other call contexts may need to explicitly claim that
482 * Return: 0 on success.
484 int usb_driver_claim_interface(struct usb_driver
*driver
,
485 struct usb_interface
*iface
, void *priv
)
487 struct device
*dev
= &iface
->dev
;
488 struct usb_device
*udev
;
490 int lpm_disable_error
;
495 udev
= interface_to_usbdev(iface
);
497 dev
->driver
= &driver
->drvwrap
.driver
;
498 usb_set_intfdata(iface
, priv
);
499 iface
->needs_binding
= 0;
501 iface
->condition
= USB_INTERFACE_BOUND
;
503 /* Disable LPM until this driver is bound. */
504 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
505 if (lpm_disable_error
&& driver
->disable_hub_initiated_lpm
) {
506 dev_err(&iface
->dev
, "%s Failed to disable LPM for driver %s\n.",
507 __func__
, driver
->name
);
511 /* Claimed interfaces are initially inactive (suspended) and
512 * runtime-PM-enabled, but only if the driver has autosuspend
513 * support. Otherwise they are marked active, to prevent the
514 * device from being autosuspended, but left disabled. In either
515 * case they are sensitive to their children's power states.
517 pm_suspend_ignore_children(dev
, false);
518 if (driver
->supports_autosuspend
)
519 pm_runtime_enable(dev
);
521 pm_runtime_set_active(dev
);
523 /* if interface was already added, bind now; else let
524 * the future device_add() bind it, bypassing probe()
526 if (device_is_registered(dev
))
527 retval
= device_bind_driver(dev
);
529 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
530 if (!lpm_disable_error
)
531 usb_unlocked_enable_lpm(udev
);
535 EXPORT_SYMBOL_GPL(usb_driver_claim_interface
);
538 * usb_driver_release_interface - unbind a driver from an interface
539 * @driver: the driver to be unbound
540 * @iface: the interface from which it will be unbound
542 * This can be used by drivers to release an interface without waiting
543 * for their disconnect() methods to be called. In typical cases this
544 * also causes the driver disconnect() method to be called.
546 * This call is synchronous, and may not be used in an interrupt context.
547 * Callers must own the device lock, so driver disconnect() entries don't
548 * need extra locking, but other call contexts may need to explicitly claim
551 void usb_driver_release_interface(struct usb_driver
*driver
,
552 struct usb_interface
*iface
)
554 struct device
*dev
= &iface
->dev
;
556 /* this should never happen, don't release something that's not ours */
557 if (!dev
->driver
|| dev
->driver
!= &driver
->drvwrap
.driver
)
560 /* don't release from within disconnect() */
561 if (iface
->condition
!= USB_INTERFACE_BOUND
)
563 iface
->condition
= USB_INTERFACE_UNBINDING
;
565 /* Release via the driver core only if the interface
566 * has already been registered
568 if (device_is_registered(dev
)) {
569 device_release_driver(dev
);
572 usb_unbind_interface(dev
);
577 EXPORT_SYMBOL_GPL(usb_driver_release_interface
);
579 /* returns 0 if no match, 1 if match */
580 int usb_match_device(struct usb_device
*dev
, const struct usb_device_id
*id
)
582 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
583 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
586 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
587 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
590 /* No need to test id->bcdDevice_lo != 0, since 0 is never
591 greater than any unsigned number. */
592 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
593 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
596 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
597 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
600 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
601 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
604 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
605 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
608 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
609 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
615 /* returns 0 if no match, 1 if match */
616 int usb_match_one_id_intf(struct usb_device
*dev
,
617 struct usb_host_interface
*intf
,
618 const struct usb_device_id
*id
)
620 /* The interface class, subclass, protocol and number should never be
621 * checked for a match if the device class is Vendor Specific,
622 * unless the match record specifies the Vendor ID. */
623 if (dev
->descriptor
.bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
624 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
625 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
626 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
627 USB_DEVICE_ID_MATCH_INT_PROTOCOL
|
628 USB_DEVICE_ID_MATCH_INT_NUMBER
)))
631 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
632 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
635 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
636 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
639 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
640 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
643 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_NUMBER
) &&
644 (id
->bInterfaceNumber
!= intf
->desc
.bInterfaceNumber
))
650 /* returns 0 if no match, 1 if match */
651 int usb_match_one_id(struct usb_interface
*interface
,
652 const struct usb_device_id
*id
)
654 struct usb_host_interface
*intf
;
655 struct usb_device
*dev
;
657 /* proc_connectinfo in devio.c may call us with id == NULL. */
661 intf
= interface
->cur_altsetting
;
662 dev
= interface_to_usbdev(interface
);
664 if (!usb_match_device(dev
, id
))
667 return usb_match_one_id_intf(dev
, intf
, id
);
669 EXPORT_SYMBOL_GPL(usb_match_one_id
);
672 * usb_match_id - find first usb_device_id matching device or interface
673 * @interface: the interface of interest
674 * @id: array of usb_device_id structures, terminated by zero entry
676 * usb_match_id searches an array of usb_device_id's and returns
677 * the first one matching the device or interface, or null.
678 * This is used when binding (or rebinding) a driver to an interface.
679 * Most USB device drivers will use this indirectly, through the usb core,
680 * but some layered driver frameworks use it directly.
681 * These device tables are exported with MODULE_DEVICE_TABLE, through
682 * modutils, to support the driver loading functionality of USB hotplugging.
684 * Return: The first matching usb_device_id, or %NULL.
688 * The "match_flags" element in a usb_device_id controls which
689 * members are used. If the corresponding bit is set, the
690 * value in the device_id must match its corresponding member
691 * in the device or interface descriptor, or else the device_id
694 * "driver_info" is normally used only by device drivers,
695 * but you can create a wildcard "matches anything" usb_device_id
696 * as a driver's "modules.usbmap" entry if you provide an id with
697 * only a nonzero "driver_info" field. If you do this, the USB device
698 * driver's probe() routine should use additional intelligence to
699 * decide whether to bind to the specified interface.
701 * What Makes Good usb_device_id Tables:
703 * The match algorithm is very simple, so that intelligence in
704 * driver selection must come from smart driver id records.
705 * Unless you have good reasons to use another selection policy,
706 * provide match elements only in related groups, and order match
707 * specifiers from specific to general. Use the macros provided
708 * for that purpose if you can.
710 * The most specific match specifiers use device descriptor
711 * data. These are commonly used with product-specific matches;
712 * the USB_DEVICE macro lets you provide vendor and product IDs,
713 * and you can also match against ranges of product revisions.
714 * These are widely used for devices with application or vendor
715 * specific bDeviceClass values.
717 * Matches based on device class/subclass/protocol specifications
718 * are slightly more general; use the USB_DEVICE_INFO macro, or
719 * its siblings. These are used with single-function devices
720 * where bDeviceClass doesn't specify that each interface has
723 * Matches based on interface class/subclass/protocol are the
724 * most general; they let drivers bind to any interface on a
725 * multiple-function device. Use the USB_INTERFACE_INFO
726 * macro, or its siblings, to match class-per-interface style
727 * devices (as recorded in bInterfaceClass).
729 * Note that an entry created by USB_INTERFACE_INFO won't match
730 * any interface if the device class is set to Vendor-Specific.
731 * This is deliberate; according to the USB spec the meanings of
732 * the interface class/subclass/protocol for these devices are also
733 * vendor-specific, and hence matching against a standard product
734 * class wouldn't work anyway. If you really want to use an
735 * interface-based match for such a device, create a match record
736 * that also specifies the vendor ID. (Unforunately there isn't a
737 * standard macro for creating records like this.)
739 * Within those groups, remember that not all combinations are
740 * meaningful. For example, don't give a product version range
741 * without vendor and product IDs; or specify a protocol without
742 * its associated class and subclass.
744 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
745 const struct usb_device_id
*id
)
747 /* proc_connectinfo in devio.c may call us with id == NULL. */
751 /* It is important to check that id->driver_info is nonzero,
752 since an entry that is all zeroes except for a nonzero
753 id->driver_info is the way to create an entry that
754 indicates that the driver want to examine every
755 device and interface. */
756 for (; id
->idVendor
|| id
->idProduct
|| id
->bDeviceClass
||
757 id
->bInterfaceClass
|| id
->driver_info
; id
++) {
758 if (usb_match_one_id(interface
, id
))
764 EXPORT_SYMBOL_GPL(usb_match_id
);
766 static int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
768 /* devices and interfaces are handled separately */
769 if (is_usb_device(dev
)) {
771 /* interface drivers never match devices */
772 if (!is_usb_device_driver(drv
))
775 /* TODO: Add real matching code */
778 } else if (is_usb_interface(dev
)) {
779 struct usb_interface
*intf
;
780 struct usb_driver
*usb_drv
;
781 const struct usb_device_id
*id
;
783 /* device drivers never match interfaces */
784 if (is_usb_device_driver(drv
))
787 intf
= to_usb_interface(dev
);
788 usb_drv
= to_usb_driver(drv
);
790 id
= usb_match_id(intf
, usb_drv
->id_table
);
794 id
= usb_match_dynamic_id(intf
, usb_drv
);
802 static int usb_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
804 struct usb_device
*usb_dev
;
806 if (is_usb_device(dev
)) {
807 usb_dev
= to_usb_device(dev
);
808 } else if (is_usb_interface(dev
)) {
809 struct usb_interface
*intf
= to_usb_interface(dev
);
811 usb_dev
= interface_to_usbdev(intf
);
816 if (usb_dev
->devnum
< 0) {
817 /* driver is often null here; dev_dbg() would oops */
818 pr_debug("usb %s: already deleted?\n", dev_name(dev
));
822 pr_debug("usb %s: bus removed?\n", dev_name(dev
));
826 /* per-device configurations are common */
827 if (add_uevent_var(env
, "PRODUCT=%x/%x/%x",
828 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
829 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
830 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
)))
833 /* class-based driver binding models */
834 if (add_uevent_var(env
, "TYPE=%d/%d/%d",
835 usb_dev
->descriptor
.bDeviceClass
,
836 usb_dev
->descriptor
.bDeviceSubClass
,
837 usb_dev
->descriptor
.bDeviceProtocol
))
844 * usb_register_device_driver - register a USB device (not interface) driver
845 * @new_udriver: USB operations for the device driver
846 * @owner: module owner of this driver.
848 * Registers a USB device driver with the USB core. The list of
849 * unattached devices will be rescanned whenever a new driver is
850 * added, allowing the new driver to attach to any recognized devices.
852 * Return: A negative error code on failure and 0 on success.
854 int usb_register_device_driver(struct usb_device_driver
*new_udriver
,
855 struct module
*owner
)
862 new_udriver
->drvwrap
.for_devices
= 1;
863 new_udriver
->drvwrap
.driver
.name
= new_udriver
->name
;
864 new_udriver
->drvwrap
.driver
.bus
= &usb_bus_type
;
865 new_udriver
->drvwrap
.driver
.probe
= usb_probe_device
;
866 new_udriver
->drvwrap
.driver
.remove
= usb_unbind_device
;
867 new_udriver
->drvwrap
.driver
.owner
= owner
;
869 retval
= driver_register(&new_udriver
->drvwrap
.driver
);
872 pr_info("%s: registered new device driver %s\n",
873 usbcore_name
, new_udriver
->name
);
875 printk(KERN_ERR
"%s: error %d registering device "
877 usbcore_name
, retval
, new_udriver
->name
);
881 EXPORT_SYMBOL_GPL(usb_register_device_driver
);
884 * usb_deregister_device_driver - unregister a USB device (not interface) driver
885 * @udriver: USB operations of the device driver to unregister
886 * Context: must be able to sleep
888 * Unlinks the specified driver from the internal USB driver list.
890 void usb_deregister_device_driver(struct usb_device_driver
*udriver
)
892 pr_info("%s: deregistering device driver %s\n",
893 usbcore_name
, udriver
->name
);
895 driver_unregister(&udriver
->drvwrap
.driver
);
897 EXPORT_SYMBOL_GPL(usb_deregister_device_driver
);
900 * usb_register_driver - register a USB interface driver
901 * @new_driver: USB operations for the interface driver
902 * @owner: module owner of this driver.
903 * @mod_name: module name string
905 * Registers a USB interface driver with the USB core. The list of
906 * unattached interfaces will be rescanned whenever a new driver is
907 * added, allowing the new driver to attach to any recognized interfaces.
909 * Return: A negative error code on failure and 0 on success.
911 * NOTE: if you want your driver to use the USB major number, you must call
912 * usb_register_dev() to enable that functionality. This function no longer
913 * takes care of that.
915 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
,
916 const char *mod_name
)
923 new_driver
->drvwrap
.for_devices
= 0;
924 new_driver
->drvwrap
.driver
.name
= new_driver
->name
;
925 new_driver
->drvwrap
.driver
.bus
= &usb_bus_type
;
926 new_driver
->drvwrap
.driver
.probe
= usb_probe_interface
;
927 new_driver
->drvwrap
.driver
.remove
= usb_unbind_interface
;
928 new_driver
->drvwrap
.driver
.owner
= owner
;
929 new_driver
->drvwrap
.driver
.mod_name
= mod_name
;
930 spin_lock_init(&new_driver
->dynids
.lock
);
931 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
933 retval
= driver_register(&new_driver
->drvwrap
.driver
);
937 retval
= usb_create_newid_files(new_driver
);
941 pr_info("%s: registered new interface driver %s\n",
942 usbcore_name
, new_driver
->name
);
948 driver_unregister(&new_driver
->drvwrap
.driver
);
950 printk(KERN_ERR
"%s: error %d registering interface "
952 usbcore_name
, retval
, new_driver
->name
);
955 EXPORT_SYMBOL_GPL(usb_register_driver
);
958 * usb_deregister - unregister a USB interface driver
959 * @driver: USB operations of the interface driver to unregister
960 * Context: must be able to sleep
962 * Unlinks the specified driver from the internal USB driver list.
964 * NOTE: If you called usb_register_dev(), you still need to call
965 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
966 * this * call will no longer do it for you.
968 void usb_deregister(struct usb_driver
*driver
)
970 pr_info("%s: deregistering interface driver %s\n",
971 usbcore_name
, driver
->name
);
973 usb_remove_newid_files(driver
);
974 driver_unregister(&driver
->drvwrap
.driver
);
975 usb_free_dynids(driver
);
977 EXPORT_SYMBOL_GPL(usb_deregister
);
979 /* Forced unbinding of a USB interface driver, either because
980 * it doesn't support pre_reset/post_reset/reset_resume or
981 * because it doesn't support suspend/resume.
983 * The caller must hold @intf's device's lock, but not its pm_mutex
984 * and not @intf->dev.sem.
986 void usb_forced_unbind_intf(struct usb_interface
*intf
)
988 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
990 dev_dbg(&intf
->dev
, "forced unbind\n");
991 usb_driver_release_interface(driver
, intf
);
993 /* Mark the interface for later rebinding */
994 intf
->needs_binding
= 1;
997 /* Delayed forced unbinding of a USB interface driver and scan
1000 * The caller must hold @intf's device's lock, but not its pm_mutex
1001 * and not @intf->dev.sem.
1003 * Note: Rebinds will be skipped if a system sleep transition is in
1004 * progress and the PM "complete" callback hasn't occurred yet.
1006 void usb_rebind_intf(struct usb_interface
*intf
)
1010 /* Delayed unbind of an existing driver */
1011 if (intf
->dev
.driver
)
1012 usb_forced_unbind_intf(intf
);
1014 /* Try to rebind the interface */
1015 if (!intf
->dev
.power
.is_prepared
) {
1016 intf
->needs_binding
= 0;
1017 rc
= device_attach(&intf
->dev
);
1019 dev_warn(&intf
->dev
, "rebind failed: %d\n", rc
);
1025 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
1026 * There is no check for reset_resume here because it can be determined
1027 * only during resume whether reset_resume is needed.
1029 * The caller must hold @udev's device lock.
1031 static void unbind_no_pm_drivers_interfaces(struct usb_device
*udev
)
1033 struct usb_host_config
*config
;
1035 struct usb_interface
*intf
;
1036 struct usb_driver
*drv
;
1038 config
= udev
->actconfig
;
1040 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1041 intf
= config
->interface
[i
];
1043 if (intf
->dev
.driver
) {
1044 drv
= to_usb_driver(intf
->dev
.driver
);
1045 if (!drv
->suspend
|| !drv
->resume
)
1046 usb_forced_unbind_intf(intf
);
1052 /* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1053 * These interfaces have the needs_binding flag set by usb_resume_interface().
1055 * The caller must hold @udev's device lock.
1057 static void unbind_no_reset_resume_drivers_interfaces(struct usb_device
*udev
)
1059 struct usb_host_config
*config
;
1061 struct usb_interface
*intf
;
1063 config
= udev
->actconfig
;
1065 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1066 intf
= config
->interface
[i
];
1067 if (intf
->dev
.driver
&& intf
->needs_binding
)
1068 usb_forced_unbind_intf(intf
);
1073 static void do_rebind_interfaces(struct usb_device
*udev
)
1075 struct usb_host_config
*config
;
1077 struct usb_interface
*intf
;
1079 config
= udev
->actconfig
;
1081 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1082 intf
= config
->interface
[i
];
1083 if (intf
->needs_binding
)
1084 usb_rebind_intf(intf
);
1089 static int usb_suspend_device(struct usb_device
*udev
, pm_message_t msg
)
1091 struct usb_device_driver
*udriver
;
1094 if (udev
->state
== USB_STATE_NOTATTACHED
||
1095 udev
->state
== USB_STATE_SUSPENDED
)
1098 /* For devices that don't have a driver, we do a generic suspend. */
1099 if (udev
->dev
.driver
)
1100 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1102 udev
->do_remote_wakeup
= 0;
1103 udriver
= &usb_generic_driver
;
1105 status
= udriver
->suspend(udev
, msg
);
1108 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1112 static int usb_resume_device(struct usb_device
*udev
, pm_message_t msg
)
1114 struct usb_device_driver
*udriver
;
1117 if (udev
->state
== USB_STATE_NOTATTACHED
)
1120 /* Can't resume it if it doesn't have a driver. */
1121 if (udev
->dev
.driver
== NULL
) {
1126 /* Non-root devices on a full/low-speed bus must wait for their
1127 * companion high-speed root hub, in case a handoff is needed.
1129 if (!PMSG_IS_AUTO(msg
) && udev
->parent
&& udev
->bus
->hs_companion
)
1130 device_pm_wait_for_dev(&udev
->dev
,
1131 &udev
->bus
->hs_companion
->root_hub
->dev
);
1133 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
)
1134 udev
->reset_resume
= 1;
1136 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1137 status
= udriver
->resume(udev
, msg
);
1140 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1144 static int usb_suspend_interface(struct usb_device
*udev
,
1145 struct usb_interface
*intf
, pm_message_t msg
)
1147 struct usb_driver
*driver
;
1150 if (udev
->state
== USB_STATE_NOTATTACHED
||
1151 intf
->condition
== USB_INTERFACE_UNBOUND
)
1153 driver
= to_usb_driver(intf
->dev
.driver
);
1155 /* at this time we know the driver supports suspend */
1156 status
= driver
->suspend(intf
, msg
);
1157 if (status
&& !PMSG_IS_AUTO(msg
))
1158 dev_err(&intf
->dev
, "suspend error %d\n", status
);
1161 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1165 static int usb_resume_interface(struct usb_device
*udev
,
1166 struct usb_interface
*intf
, pm_message_t msg
, int reset_resume
)
1168 struct usb_driver
*driver
;
1171 if (udev
->state
== USB_STATE_NOTATTACHED
)
1174 /* Don't let autoresume interfere with unbinding */
1175 if (intf
->condition
== USB_INTERFACE_UNBINDING
)
1178 /* Can't resume it if it doesn't have a driver. */
1179 if (intf
->condition
== USB_INTERFACE_UNBOUND
) {
1181 /* Carry out a deferred switch to altsetting 0 */
1182 if (intf
->needs_altsetting0
&& !intf
->dev
.power
.is_prepared
) {
1183 usb_set_interface(udev
, intf
->altsetting
[0].
1184 desc
.bInterfaceNumber
, 0);
1185 intf
->needs_altsetting0
= 0;
1190 /* Don't resume if the interface is marked for rebinding */
1191 if (intf
->needs_binding
)
1193 driver
= to_usb_driver(intf
->dev
.driver
);
1196 if (driver
->reset_resume
) {
1197 status
= driver
->reset_resume(intf
);
1199 dev_err(&intf
->dev
, "%s error %d\n",
1200 "reset_resume", status
);
1202 intf
->needs_binding
= 1;
1203 dev_dbg(&intf
->dev
, "no reset_resume for driver %s?\n",
1207 status
= driver
->resume(intf
);
1209 dev_err(&intf
->dev
, "resume error %d\n", status
);
1213 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1215 /* Later we will unbind the driver and/or reprobe, if necessary */
1220 * usb_suspend_both - suspend a USB device and its interfaces
1221 * @udev: the usb_device to suspend
1222 * @msg: Power Management message describing this state transition
1224 * This is the central routine for suspending USB devices. It calls the
1225 * suspend methods for all the interface drivers in @udev and then calls
1226 * the suspend method for @udev itself. When the routine is called in
1227 * autosuspend, if an error occurs at any stage, all the interfaces
1228 * which were suspended are resumed so that they remain in the same
1229 * state as the device, but when called from system sleep, all error
1230 * from suspend methods of interfaces and the non-root-hub device itself
1231 * are simply ignored, so all suspended interfaces are only resumed
1232 * to the device's state when @udev is root-hub and its suspend method
1235 * Autosuspend requests originating from a child device or an interface
1236 * driver may be made without the protection of @udev's device lock, but
1237 * all other suspend calls will hold the lock. Usbcore will insure that
1238 * method calls do not arrive during bind, unbind, or reset operations.
1239 * However drivers must be prepared to handle suspend calls arriving at
1240 * unpredictable times.
1242 * This routine can run only in process context.
1244 * Return: 0 if the suspend succeeded.
1246 static int usb_suspend_both(struct usb_device
*udev
, pm_message_t msg
)
1250 struct usb_interface
*intf
;
1252 if (udev
->state
== USB_STATE_NOTATTACHED
||
1253 udev
->state
== USB_STATE_SUSPENDED
)
1256 /* Suspend all the interfaces and then udev itself */
1257 if (udev
->actconfig
) {
1258 n
= udev
->actconfig
->desc
.bNumInterfaces
;
1259 for (i
= n
- 1; i
>= 0; --i
) {
1260 intf
= udev
->actconfig
->interface
[i
];
1261 status
= usb_suspend_interface(udev
, intf
, msg
);
1263 /* Ignore errors during system sleep transitions */
1264 if (!PMSG_IS_AUTO(msg
))
1271 status
= usb_suspend_device(udev
, msg
);
1274 * Ignore errors from non-root-hub devices during
1275 * system sleep transitions. For the most part,
1276 * these devices should go to low power anyway when
1277 * the entire bus is suspended.
1279 if (udev
->parent
&& !PMSG_IS_AUTO(msg
))
1283 /* If the suspend failed, resume interfaces that did get suspended */
1285 if (udev
->actconfig
) {
1286 msg
.event
^= (PM_EVENT_SUSPEND
| PM_EVENT_RESUME
);
1288 intf
= udev
->actconfig
->interface
[i
];
1289 usb_resume_interface(udev
, intf
, msg
, 0);
1293 /* If the suspend succeeded then prevent any more URB submissions
1294 * and flush any outstanding URBs.
1297 udev
->can_submit
= 0;
1298 for (i
= 0; i
< 16; ++i
) {
1299 usb_hcd_flush_endpoint(udev
, udev
->ep_out
[i
]);
1300 usb_hcd_flush_endpoint(udev
, udev
->ep_in
[i
]);
1305 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1310 * usb_resume_both - resume a USB device and its interfaces
1311 * @udev: the usb_device to resume
1312 * @msg: Power Management message describing this state transition
1314 * This is the central routine for resuming USB devices. It calls the
1315 * the resume method for @udev and then calls the resume methods for all
1316 * the interface drivers in @udev.
1318 * Autoresume requests originating from a child device or an interface
1319 * driver may be made without the protection of @udev's device lock, but
1320 * all other resume calls will hold the lock. Usbcore will insure that
1321 * method calls do not arrive during bind, unbind, or reset operations.
1322 * However drivers must be prepared to handle resume calls arriving at
1323 * unpredictable times.
1325 * This routine can run only in process context.
1327 * Return: 0 on success.
1329 static int usb_resume_both(struct usb_device
*udev
, pm_message_t msg
)
1333 struct usb_interface
*intf
;
1335 if (udev
->state
== USB_STATE_NOTATTACHED
) {
1339 udev
->can_submit
= 1;
1341 /* Resume the device */
1342 if (udev
->state
== USB_STATE_SUSPENDED
|| udev
->reset_resume
)
1343 status
= usb_resume_device(udev
, msg
);
1345 /* Resume the interfaces */
1346 if (status
== 0 && udev
->actconfig
) {
1347 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1348 intf
= udev
->actconfig
->interface
[i
];
1349 usb_resume_interface(udev
, intf
, msg
,
1350 udev
->reset_resume
);
1353 usb_mark_last_busy(udev
);
1356 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1358 udev
->reset_resume
= 0;
1362 static void choose_wakeup(struct usb_device
*udev
, pm_message_t msg
)
1366 /* Remote wakeup is needed only when we actually go to sleep.
1367 * For things like FREEZE and QUIESCE, if the device is already
1368 * autosuspended then its current wakeup setting is okay.
1370 if (msg
.event
== PM_EVENT_FREEZE
|| msg
.event
== PM_EVENT_QUIESCE
) {
1371 if (udev
->state
!= USB_STATE_SUSPENDED
)
1372 udev
->do_remote_wakeup
= 0;
1376 /* Enable remote wakeup if it is allowed, even if no interface drivers
1379 w
= device_may_wakeup(&udev
->dev
);
1381 /* If the device is autosuspended with the wrong wakeup setting,
1382 * autoresume now so the setting can be changed.
1384 if (udev
->state
== USB_STATE_SUSPENDED
&& w
!= udev
->do_remote_wakeup
)
1385 pm_runtime_resume(&udev
->dev
);
1386 udev
->do_remote_wakeup
= w
;
1389 /* The device lock is held by the PM core */
1390 int usb_suspend(struct device
*dev
, pm_message_t msg
)
1392 struct usb_device
*udev
= to_usb_device(dev
);
1394 unbind_no_pm_drivers_interfaces(udev
);
1396 /* From now on we are sure all drivers support suspend/resume
1397 * but not necessarily reset_resume()
1398 * so we may still need to unbind and rebind upon resume
1400 choose_wakeup(udev
, msg
);
1401 return usb_suspend_both(udev
, msg
);
1404 /* The device lock is held by the PM core */
1405 int usb_resume_complete(struct device
*dev
)
1407 struct usb_device
*udev
= to_usb_device(dev
);
1409 /* For PM complete calls, all we do is rebind interfaces
1410 * whose needs_binding flag is set
1412 if (udev
->state
!= USB_STATE_NOTATTACHED
)
1413 do_rebind_interfaces(udev
);
1417 /* The device lock is held by the PM core */
1418 int usb_resume(struct device
*dev
, pm_message_t msg
)
1420 struct usb_device
*udev
= to_usb_device(dev
);
1423 /* For all calls, take the device back to full power and
1424 * tell the PM core in case it was autosuspended previously.
1425 * Unbind the interfaces that will need rebinding later,
1426 * because they fail to support reset_resume.
1427 * (This can't be done in usb_resume_interface()
1428 * above because it doesn't own the right set of locks.)
1430 status
= usb_resume_both(udev
, msg
);
1432 pm_runtime_disable(dev
);
1433 pm_runtime_set_active(dev
);
1434 pm_runtime_enable(dev
);
1435 unbind_no_reset_resume_drivers_interfaces(udev
);
1438 /* Avoid PM error messages for devices disconnected while suspended
1439 * as we'll display regular disconnect messages just a bit later.
1441 if (status
== -ENODEV
|| status
== -ESHUTDOWN
)
1446 #endif /* CONFIG_PM */
1448 #ifdef CONFIG_PM_RUNTIME
1451 * usb_enable_autosuspend - allow a USB device to be autosuspended
1452 * @udev: the USB device which may be autosuspended
1454 * This routine allows @udev to be autosuspended. An autosuspend won't
1455 * take place until the autosuspend_delay has elapsed and all the other
1456 * necessary conditions are satisfied.
1458 * The caller must hold @udev's device lock.
1460 void usb_enable_autosuspend(struct usb_device
*udev
)
1462 pm_runtime_allow(&udev
->dev
);
1464 EXPORT_SYMBOL_GPL(usb_enable_autosuspend
);
1467 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1468 * @udev: the USB device which may not be autosuspended
1470 * This routine prevents @udev from being autosuspended and wakes it up
1471 * if it is already autosuspended.
1473 * The caller must hold @udev's device lock.
1475 void usb_disable_autosuspend(struct usb_device
*udev
)
1477 pm_runtime_forbid(&udev
->dev
);
1479 EXPORT_SYMBOL_GPL(usb_disable_autosuspend
);
1482 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1483 * @udev: the usb_device to autosuspend
1485 * This routine should be called when a core subsystem is finished using
1486 * @udev and wants to allow it to autosuspend. Examples would be when
1487 * @udev's device file in usbfs is closed or after a configuration change.
1489 * @udev's usage counter is decremented; if it drops to 0 and all the
1490 * interfaces are inactive then a delayed autosuspend will be attempted.
1491 * The attempt may fail (see autosuspend_check()).
1493 * The caller must hold @udev's device lock.
1495 * This routine can run only in process context.
1497 void usb_autosuspend_device(struct usb_device
*udev
)
1501 usb_mark_last_busy(udev
);
1502 status
= pm_runtime_put_sync_autosuspend(&udev
->dev
);
1503 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1504 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1509 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1510 * @udev: the usb_device to autoresume
1512 * This routine should be called when a core subsystem wants to use @udev
1513 * and needs to guarantee that it is not suspended. No autosuspend will
1514 * occur until usb_autosuspend_device() is called. (Note that this will
1515 * not prevent suspend events originating in the PM core.) Examples would
1516 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1517 * request is received.
1519 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1520 * However if the autoresume fails then the usage counter is re-decremented.
1522 * The caller must hold @udev's device lock.
1524 * This routine can run only in process context.
1526 * Return: 0 on success. A negative error code otherwise.
1528 int usb_autoresume_device(struct usb_device
*udev
)
1532 status
= pm_runtime_get_sync(&udev
->dev
);
1534 pm_runtime_put_sync(&udev
->dev
);
1535 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1536 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1544 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1545 * @intf: the usb_interface whose counter should be decremented
1547 * This routine should be called by an interface driver when it is
1548 * finished using @intf and wants to allow it to autosuspend. A typical
1549 * example would be a character-device driver when its device file is
1552 * The routine decrements @intf's usage counter. When the counter reaches
1553 * 0, a delayed autosuspend request for @intf's device is attempted. The
1554 * attempt may fail (see autosuspend_check()).
1556 * This routine can run only in process context.
1558 void usb_autopm_put_interface(struct usb_interface
*intf
)
1560 struct usb_device
*udev
= interface_to_usbdev(intf
);
1563 usb_mark_last_busy(udev
);
1564 atomic_dec(&intf
->pm_usage_cnt
);
1565 status
= pm_runtime_put_sync(&intf
->dev
);
1566 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1567 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1570 EXPORT_SYMBOL_GPL(usb_autopm_put_interface
);
1573 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1574 * @intf: the usb_interface whose counter should be decremented
1576 * This routine does much the same thing as usb_autopm_put_interface():
1577 * It decrements @intf's usage counter and schedules a delayed
1578 * autosuspend request if the counter is <= 0. The difference is that it
1579 * does not perform any synchronization; callers should hold a private
1580 * lock and handle all synchronization issues themselves.
1582 * Typically a driver would call this routine during an URB's completion
1583 * handler, if no more URBs were pending.
1585 * This routine can run in atomic context.
1587 void usb_autopm_put_interface_async(struct usb_interface
*intf
)
1589 struct usb_device
*udev
= interface_to_usbdev(intf
);
1592 usb_mark_last_busy(udev
);
1593 atomic_dec(&intf
->pm_usage_cnt
);
1594 status
= pm_runtime_put(&intf
->dev
);
1595 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1596 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1599 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async
);
1602 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1603 * @intf: the usb_interface whose counter should be decremented
1605 * This routine decrements @intf's usage counter but does not carry out an
1608 * This routine can run in atomic context.
1610 void usb_autopm_put_interface_no_suspend(struct usb_interface
*intf
)
1612 struct usb_device
*udev
= interface_to_usbdev(intf
);
1614 usb_mark_last_busy(udev
);
1615 atomic_dec(&intf
->pm_usage_cnt
);
1616 pm_runtime_put_noidle(&intf
->dev
);
1618 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend
);
1621 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1622 * @intf: the usb_interface whose counter should be incremented
1624 * This routine should be called by an interface driver when it wants to
1625 * use @intf and needs to guarantee that it is not suspended. In addition,
1626 * the routine prevents @intf from being autosuspended subsequently. (Note
1627 * that this will not prevent suspend events originating in the PM core.)
1628 * This prevention will persist until usb_autopm_put_interface() is called
1629 * or @intf is unbound. A typical example would be a character-device
1630 * driver when its device file is opened.
1632 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1633 * However if the autoresume fails then the counter is re-decremented.
1635 * This routine can run only in process context.
1637 * Return: 0 on success.
1639 int usb_autopm_get_interface(struct usb_interface
*intf
)
1643 status
= pm_runtime_get_sync(&intf
->dev
);
1645 pm_runtime_put_sync(&intf
->dev
);
1647 atomic_inc(&intf
->pm_usage_cnt
);
1648 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1649 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1655 EXPORT_SYMBOL_GPL(usb_autopm_get_interface
);
1658 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1659 * @intf: the usb_interface whose counter should be incremented
1661 * This routine does much the same thing as
1662 * usb_autopm_get_interface(): It increments @intf's usage counter and
1663 * queues an autoresume request if the device is suspended. The
1664 * differences are that it does not perform any synchronization (callers
1665 * should hold a private lock and handle all synchronization issues
1666 * themselves), and it does not autoresume the device directly (it only
1667 * queues a request). After a successful call, the device may not yet be
1670 * This routine can run in atomic context.
1672 * Return: 0 on success. A negative error code otherwise.
1674 int usb_autopm_get_interface_async(struct usb_interface
*intf
)
1678 status
= pm_runtime_get(&intf
->dev
);
1679 if (status
< 0 && status
!= -EINPROGRESS
)
1680 pm_runtime_put_noidle(&intf
->dev
);
1682 atomic_inc(&intf
->pm_usage_cnt
);
1683 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1684 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1686 if (status
> 0 || status
== -EINPROGRESS
)
1690 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async
);
1693 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1694 * @intf: the usb_interface whose counter should be incremented
1696 * This routine increments @intf's usage counter but does not carry out an
1699 * This routine can run in atomic context.
1701 void usb_autopm_get_interface_no_resume(struct usb_interface
*intf
)
1703 struct usb_device
*udev
= interface_to_usbdev(intf
);
1705 usb_mark_last_busy(udev
);
1706 atomic_inc(&intf
->pm_usage_cnt
);
1707 pm_runtime_get_noresume(&intf
->dev
);
1709 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume
);
1711 /* Internal routine to check whether we may autosuspend a device. */
1712 static int autosuspend_check(struct usb_device
*udev
)
1715 struct usb_interface
*intf
;
1717 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1718 * any interface drivers require remote wakeup but it isn't available.
1721 if (udev
->actconfig
) {
1722 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1723 intf
= udev
->actconfig
->interface
[i
];
1725 /* We don't need to check interfaces that are
1726 * disabled for runtime PM. Either they are unbound
1727 * or else their drivers don't support autosuspend
1728 * and so they are permanently active.
1730 if (intf
->dev
.power
.disable_depth
)
1732 if (atomic_read(&intf
->dev
.power
.usage_count
) > 0)
1734 w
|= intf
->needs_remote_wakeup
;
1736 /* Don't allow autosuspend if the device will need
1737 * a reset-resume and any of its interface drivers
1738 * doesn't include support or needs remote wakeup.
1740 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
) {
1741 struct usb_driver
*driver
;
1743 driver
= to_usb_driver(intf
->dev
.driver
);
1744 if (!driver
->reset_resume
||
1745 intf
->needs_remote_wakeup
)
1750 if (w
&& !device_can_wakeup(&udev
->dev
)) {
1751 dev_dbg(&udev
->dev
, "remote wakeup needed for autosuspend\n");
1754 udev
->do_remote_wakeup
= w
;
1758 int usb_runtime_suspend(struct device
*dev
)
1760 struct usb_device
*udev
= to_usb_device(dev
);
1763 /* A USB device can be suspended if it passes the various autosuspend
1764 * checks. Runtime suspend for a USB device means suspending all the
1765 * interfaces and then the device itself.
1767 if (autosuspend_check(udev
) != 0)
1770 status
= usb_suspend_both(udev
, PMSG_AUTO_SUSPEND
);
1772 /* Allow a retry if autosuspend failed temporarily */
1773 if (status
== -EAGAIN
|| status
== -EBUSY
)
1774 usb_mark_last_busy(udev
);
1776 /* The PM core reacts badly unless the return code is 0,
1777 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1784 int usb_runtime_resume(struct device
*dev
)
1786 struct usb_device
*udev
= to_usb_device(dev
);
1789 /* Runtime resume for a USB device means resuming both the device
1790 * and all its interfaces.
1792 status
= usb_resume_both(udev
, PMSG_AUTO_RESUME
);
1796 int usb_runtime_idle(struct device
*dev
)
1798 struct usb_device
*udev
= to_usb_device(dev
);
1800 /* An idle USB device can be suspended if it passes the various
1801 * autosuspend checks.
1803 if (autosuspend_check(udev
) == 0)
1804 pm_runtime_autosuspend(dev
);
1805 /* Tell the core not to suspend it, though. */
1809 int usb_set_usb2_hardware_lpm(struct usb_device
*udev
, int enable
)
1811 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1814 if (enable
&& !udev
->usb2_hw_lpm_allowed
)
1817 if (hcd
->driver
->set_usb2_hw_lpm
) {
1818 ret
= hcd
->driver
->set_usb2_hw_lpm(hcd
, udev
, enable
);
1820 udev
->usb2_hw_lpm_enabled
= enable
;
1826 #endif /* CONFIG_PM_RUNTIME */
1828 struct bus_type usb_bus_type
= {
1830 .match
= usb_device_match
,
1831 .uevent
= usb_uevent
,