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
;
164 if ((id
->idVendor
== idVendor
) &&
165 (id
->idProduct
== idProduct
)) {
166 list_del(&dynid
->node
);
171 spin_unlock(&usb_driver
->dynids
.lock
);
175 static ssize_t
remove_id_show(struct device_driver
*driver
, char *buf
)
177 return new_id_show(driver
, buf
);
179 static DRIVER_ATTR_RW(remove_id
);
181 static int usb_create_newid_files(struct usb_driver
*usb_drv
)
185 if (usb_drv
->no_dynamic_id
)
188 if (usb_drv
->probe
!= NULL
) {
189 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
190 &driver_attr_new_id
);
192 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
193 &driver_attr_remove_id
);
195 driver_remove_file(&usb_drv
->drvwrap
.driver
,
196 &driver_attr_new_id
);
203 static void usb_remove_newid_files(struct usb_driver
*usb_drv
)
205 if (usb_drv
->no_dynamic_id
)
208 if (usb_drv
->probe
!= NULL
) {
209 driver_remove_file(&usb_drv
->drvwrap
.driver
,
210 &driver_attr_remove_id
);
211 driver_remove_file(&usb_drv
->drvwrap
.driver
,
212 &driver_attr_new_id
);
216 static void usb_free_dynids(struct usb_driver
*usb_drv
)
218 struct usb_dynid
*dynid
, *n
;
220 spin_lock(&usb_drv
->dynids
.lock
);
221 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
222 list_del(&dynid
->node
);
225 spin_unlock(&usb_drv
->dynids
.lock
);
228 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
229 struct usb_driver
*drv
)
231 struct usb_dynid
*dynid
;
233 spin_lock(&drv
->dynids
.lock
);
234 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
235 if (usb_match_one_id(intf
, &dynid
->id
)) {
236 spin_unlock(&drv
->dynids
.lock
);
240 spin_unlock(&drv
->dynids
.lock
);
245 /* called from driver core with dev locked */
246 static int usb_probe_device(struct device
*dev
)
248 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
249 struct usb_device
*udev
= to_usb_device(dev
);
252 dev_dbg(dev
, "%s\n", __func__
);
254 /* TODO: Add real matching code */
256 /* The device should always appear to be in use
257 * unless the driver supports autosuspend.
259 if (!udriver
->supports_autosuspend
)
260 error
= usb_autoresume_device(udev
);
263 error
= udriver
->probe(udev
);
267 /* called from driver core with dev locked */
268 static int usb_unbind_device(struct device
*dev
)
270 struct usb_device
*udev
= to_usb_device(dev
);
271 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
273 udriver
->disconnect(udev
);
274 if (!udriver
->supports_autosuspend
)
275 usb_autosuspend_device(udev
);
279 /* called from driver core with dev locked */
280 static int usb_probe_interface(struct device
*dev
)
282 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
283 struct usb_interface
*intf
= to_usb_interface(dev
);
284 struct usb_device
*udev
= interface_to_usbdev(intf
);
285 const struct usb_device_id
*id
;
287 int lpm_disable_error
= -ENODEV
;
289 dev_dbg(dev
, "%s\n", __func__
);
291 intf
->needs_binding
= 0;
293 if (usb_device_is_owned(udev
))
296 if (udev
->authorized
== 0) {
297 dev_err(&intf
->dev
, "Device is not authorized for usage\n");
299 } else if (intf
->authorized
== 0) {
300 dev_err(&intf
->dev
, "Interface %d is not authorized for usage\n",
301 intf
->altsetting
->desc
.bInterfaceNumber
);
305 id
= usb_match_dynamic_id(intf
, driver
);
307 id
= usb_match_id(intf
, driver
->id_table
);
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 if (driver
->disable_hub_initiated_lpm
) {
340 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
341 if (lpm_disable_error
) {
342 dev_err(&intf
->dev
, "%s Failed to disable LPM for driver %s\n.",
343 __func__
, driver
->name
);
344 error
= lpm_disable_error
;
349 /* Carry out a deferred switch to altsetting 0 */
350 if (intf
->needs_altsetting0
) {
351 error
= usb_set_interface(udev
, intf
->altsetting
[0].
352 desc
.bInterfaceNumber
, 0);
355 intf
->needs_altsetting0
= 0;
358 error
= driver
->probe(intf
, id
);
362 intf
->condition
= USB_INTERFACE_BOUND
;
364 /* If the LPM disable succeeded, balance the ref counts. */
365 if (!lpm_disable_error
)
366 usb_unlocked_enable_lpm(udev
);
368 usb_autosuspend_device(udev
);
372 usb_set_intfdata(intf
, NULL
);
373 intf
->needs_remote_wakeup
= 0;
374 intf
->condition
= USB_INTERFACE_UNBOUND
;
376 /* If the LPM disable succeeded, balance the ref counts. */
377 if (!lpm_disable_error
)
378 usb_unlocked_enable_lpm(udev
);
380 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
381 if (driver
->supports_autosuspend
)
382 pm_runtime_disable(dev
);
383 pm_runtime_set_suspended(dev
);
385 usb_autosuspend_device(udev
);
389 /* called from driver core with dev locked */
390 static int usb_unbind_interface(struct device
*dev
)
392 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
393 struct usb_interface
*intf
= to_usb_interface(dev
);
394 struct usb_host_endpoint
*ep
, **eps
= NULL
;
395 struct usb_device
*udev
;
397 int lpm_disable_error
= -ENODEV
;
399 intf
->condition
= USB_INTERFACE_UNBINDING
;
401 /* Autoresume for set_interface call below */
402 udev
= interface_to_usbdev(intf
);
403 error
= usb_autoresume_device(udev
);
405 /* If hub-initiated LPM policy may change, attempt to disable LPM until
406 * the driver is unbound. If LPM isn't disabled, that's fine because it
407 * wouldn't be enabled unless all the bound interfaces supported
410 if (driver
->disable_hub_initiated_lpm
)
411 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
414 * Terminate all URBs for this interface unless the driver
415 * supports "soft" unbinding and the device is still present.
417 if (!driver
->soft_unbind
|| udev
->state
== USB_STATE_NOTATTACHED
)
418 usb_disable_interface(udev
, intf
, false);
420 driver
->disconnect(intf
);
423 for (i
= 0, j
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
424 ep
= &intf
->cur_altsetting
->endpoint
[i
];
425 if (ep
->streams
== 0)
428 eps
= kmalloc_array(USB_MAXENDPOINTS
, sizeof(void *),
436 usb_free_streams(intf
, eps
, j
, GFP_KERNEL
);
440 /* Reset other interface state.
441 * We cannot do a Set-Interface if the device is suspended or
442 * if it is prepared for a system sleep (since installing a new
443 * altsetting means creating new endpoint device entries).
444 * When either of these happens, defer the Set-Interface.
446 if (intf
->cur_altsetting
->desc
.bAlternateSetting
== 0) {
447 /* Already in altsetting 0 so skip Set-Interface.
448 * Just re-enable it without affecting the endpoint toggles.
450 usb_enable_interface(udev
, intf
, false);
451 } else if (!error
&& !intf
->dev
.power
.is_prepared
) {
452 r
= usb_set_interface(udev
, intf
->altsetting
[0].
453 desc
.bInterfaceNumber
, 0);
455 intf
->needs_altsetting0
= 1;
457 intf
->needs_altsetting0
= 1;
459 usb_set_intfdata(intf
, NULL
);
461 intf
->condition
= USB_INTERFACE_UNBOUND
;
462 intf
->needs_remote_wakeup
= 0;
464 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
465 if (!lpm_disable_error
)
466 usb_unlocked_enable_lpm(udev
);
468 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
469 if (driver
->supports_autosuspend
)
470 pm_runtime_disable(dev
);
471 pm_runtime_set_suspended(dev
);
473 /* Undo any residual pm_autopm_get_interface_* calls */
474 for (r
= atomic_read(&intf
->pm_usage_cnt
); r
> 0; --r
)
475 usb_autopm_put_interface_no_suspend(intf
);
476 atomic_set(&intf
->pm_usage_cnt
, 0);
479 usb_autosuspend_device(udev
);
485 * usb_driver_claim_interface - bind a driver to an interface
486 * @driver: the driver to be bound
487 * @iface: the interface to which it will be bound; must be in the
488 * usb device's active configuration
489 * @priv: driver data associated with that interface
491 * This is used by usb device drivers that need to claim more than one
492 * interface on a device when probing (audio and acm are current examples).
493 * No device driver should directly modify internal usb_interface or
494 * usb_device structure members.
496 * Few drivers should need to use this routine, since the most natural
497 * way to bind to an interface is to return the private data from
498 * the driver's probe() method.
500 * Callers must own the device lock, so driver probe() entries don't need
501 * extra locking, but other call contexts may need to explicitly claim that
504 * Return: 0 on success.
506 int usb_driver_claim_interface(struct usb_driver
*driver
,
507 struct usb_interface
*iface
, void *priv
)
510 struct usb_device
*udev
;
520 /* reject claim if interface is not authorized */
521 if (!iface
->authorized
)
524 udev
= interface_to_usbdev(iface
);
526 dev
->driver
= &driver
->drvwrap
.driver
;
527 usb_set_intfdata(iface
, priv
);
528 iface
->needs_binding
= 0;
530 iface
->condition
= USB_INTERFACE_BOUND
;
532 /* Claimed interfaces are initially inactive (suspended) and
533 * runtime-PM-enabled, but only if the driver has autosuspend
534 * support. Otherwise they are marked active, to prevent the
535 * device from being autosuspended, but left disabled. In either
536 * case they are sensitive to their children's power states.
538 pm_suspend_ignore_children(dev
, false);
539 if (driver
->supports_autosuspend
)
540 pm_runtime_enable(dev
);
542 pm_runtime_set_active(dev
);
544 /* if interface was already added, bind now; else let
545 * the future device_add() bind it, bypassing probe()
547 if (device_is_registered(dev
))
548 retval
= device_bind_driver(dev
);
552 usb_set_intfdata(iface
, NULL
);
553 iface
->needs_remote_wakeup
= 0;
554 iface
->condition
= USB_INTERFACE_UNBOUND
;
557 * Unbound interfaces are always runtime-PM-disabled
558 * and runtime-PM-suspended
560 if (driver
->supports_autosuspend
)
561 pm_runtime_disable(dev
);
562 pm_runtime_set_suspended(dev
);
567 EXPORT_SYMBOL_GPL(usb_driver_claim_interface
);
570 * usb_driver_release_interface - unbind a driver from an interface
571 * @driver: the driver to be unbound
572 * @iface: the interface from which it will be unbound
574 * This can be used by drivers to release an interface without waiting
575 * for their disconnect() methods to be called. In typical cases this
576 * also causes the driver disconnect() method to be called.
578 * This call is synchronous, and may not be used in an interrupt context.
579 * Callers must own the device lock, so driver disconnect() entries don't
580 * need extra locking, but other call contexts may need to explicitly claim
583 void usb_driver_release_interface(struct usb_driver
*driver
,
584 struct usb_interface
*iface
)
586 struct device
*dev
= &iface
->dev
;
588 /* this should never happen, don't release something that's not ours */
589 if (!dev
->driver
|| dev
->driver
!= &driver
->drvwrap
.driver
)
592 /* don't release from within disconnect() */
593 if (iface
->condition
!= USB_INTERFACE_BOUND
)
595 iface
->condition
= USB_INTERFACE_UNBINDING
;
597 /* Release via the driver core only if the interface
598 * has already been registered
600 if (device_is_registered(dev
)) {
601 device_release_driver(dev
);
604 usb_unbind_interface(dev
);
609 EXPORT_SYMBOL_GPL(usb_driver_release_interface
);
611 /* returns 0 if no match, 1 if match */
612 int usb_match_device(struct usb_device
*dev
, const struct usb_device_id
*id
)
614 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
615 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
618 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
619 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
622 /* No need to test id->bcdDevice_lo != 0, since 0 is never
623 greater than any unsigned number. */
624 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
625 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
628 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
629 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
632 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
633 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
636 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
637 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
640 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
641 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
647 /* returns 0 if no match, 1 if match */
648 int usb_match_one_id_intf(struct usb_device
*dev
,
649 struct usb_host_interface
*intf
,
650 const struct usb_device_id
*id
)
652 /* The interface class, subclass, protocol and number should never be
653 * checked for a match if the device class is Vendor Specific,
654 * unless the match record specifies the Vendor ID. */
655 if (dev
->descriptor
.bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
656 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
657 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
658 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
659 USB_DEVICE_ID_MATCH_INT_PROTOCOL
|
660 USB_DEVICE_ID_MATCH_INT_NUMBER
)))
663 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
664 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
667 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
668 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
671 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
672 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
675 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_NUMBER
) &&
676 (id
->bInterfaceNumber
!= intf
->desc
.bInterfaceNumber
))
682 /* returns 0 if no match, 1 if match */
683 int usb_match_one_id(struct usb_interface
*interface
,
684 const struct usb_device_id
*id
)
686 struct usb_host_interface
*intf
;
687 struct usb_device
*dev
;
689 /* proc_connectinfo in devio.c may call us with id == NULL. */
693 intf
= interface
->cur_altsetting
;
694 dev
= interface_to_usbdev(interface
);
696 if (!usb_match_device(dev
, id
))
699 return usb_match_one_id_intf(dev
, intf
, id
);
701 EXPORT_SYMBOL_GPL(usb_match_one_id
);
704 * usb_match_id - find first usb_device_id matching device or interface
705 * @interface: the interface of interest
706 * @id: array of usb_device_id structures, terminated by zero entry
708 * usb_match_id searches an array of usb_device_id's and returns
709 * the first one matching the device or interface, or null.
710 * This is used when binding (or rebinding) a driver to an interface.
711 * Most USB device drivers will use this indirectly, through the usb core,
712 * but some layered driver frameworks use it directly.
713 * These device tables are exported with MODULE_DEVICE_TABLE, through
714 * modutils, to support the driver loading functionality of USB hotplugging.
716 * Return: The first matching usb_device_id, or %NULL.
720 * The "match_flags" element in a usb_device_id controls which
721 * members are used. If the corresponding bit is set, the
722 * value in the device_id must match its corresponding member
723 * in the device or interface descriptor, or else the device_id
726 * "driver_info" is normally used only by device drivers,
727 * but you can create a wildcard "matches anything" usb_device_id
728 * as a driver's "modules.usbmap" entry if you provide an id with
729 * only a nonzero "driver_info" field. If you do this, the USB device
730 * driver's probe() routine should use additional intelligence to
731 * decide whether to bind to the specified interface.
733 * What Makes Good usb_device_id Tables:
735 * The match algorithm is very simple, so that intelligence in
736 * driver selection must come from smart driver id records.
737 * Unless you have good reasons to use another selection policy,
738 * provide match elements only in related groups, and order match
739 * specifiers from specific to general. Use the macros provided
740 * for that purpose if you can.
742 * The most specific match specifiers use device descriptor
743 * data. These are commonly used with product-specific matches;
744 * the USB_DEVICE macro lets you provide vendor and product IDs,
745 * and you can also match against ranges of product revisions.
746 * These are widely used for devices with application or vendor
747 * specific bDeviceClass values.
749 * Matches based on device class/subclass/protocol specifications
750 * are slightly more general; use the USB_DEVICE_INFO macro, or
751 * its siblings. These are used with single-function devices
752 * where bDeviceClass doesn't specify that each interface has
755 * Matches based on interface class/subclass/protocol are the
756 * most general; they let drivers bind to any interface on a
757 * multiple-function device. Use the USB_INTERFACE_INFO
758 * macro, or its siblings, to match class-per-interface style
759 * devices (as recorded in bInterfaceClass).
761 * Note that an entry created by USB_INTERFACE_INFO won't match
762 * any interface if the device class is set to Vendor-Specific.
763 * This is deliberate; according to the USB spec the meanings of
764 * the interface class/subclass/protocol for these devices are also
765 * vendor-specific, and hence matching against a standard product
766 * class wouldn't work anyway. If you really want to use an
767 * interface-based match for such a device, create a match record
768 * that also specifies the vendor ID. (Unforunately there isn't a
769 * standard macro for creating records like this.)
771 * Within those groups, remember that not all combinations are
772 * meaningful. For example, don't give a product version range
773 * without vendor and product IDs; or specify a protocol without
774 * its associated class and subclass.
776 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
777 const struct usb_device_id
*id
)
779 /* proc_connectinfo in devio.c may call us with id == NULL. */
783 /* It is important to check that id->driver_info is nonzero,
784 since an entry that is all zeroes except for a nonzero
785 id->driver_info is the way to create an entry that
786 indicates that the driver want to examine every
787 device and interface. */
788 for (; id
->idVendor
|| id
->idProduct
|| id
->bDeviceClass
||
789 id
->bInterfaceClass
|| id
->driver_info
; id
++) {
790 if (usb_match_one_id(interface
, id
))
796 EXPORT_SYMBOL_GPL(usb_match_id
);
798 static int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
800 /* devices and interfaces are handled separately */
801 if (is_usb_device(dev
)) {
803 /* interface drivers never match devices */
804 if (!is_usb_device_driver(drv
))
807 /* TODO: Add real matching code */
810 } else if (is_usb_interface(dev
)) {
811 struct usb_interface
*intf
;
812 struct usb_driver
*usb_drv
;
813 const struct usb_device_id
*id
;
815 /* device drivers never match interfaces */
816 if (is_usb_device_driver(drv
))
819 intf
= to_usb_interface(dev
);
820 usb_drv
= to_usb_driver(drv
);
822 id
= usb_match_id(intf
, usb_drv
->id_table
);
826 id
= usb_match_dynamic_id(intf
, usb_drv
);
834 static int usb_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
836 struct usb_device
*usb_dev
;
838 if (is_usb_device(dev
)) {
839 usb_dev
= to_usb_device(dev
);
840 } else if (is_usb_interface(dev
)) {
841 struct usb_interface
*intf
= to_usb_interface(dev
);
843 usb_dev
= interface_to_usbdev(intf
);
848 if (usb_dev
->devnum
< 0) {
849 /* driver is often null here; dev_dbg() would oops */
850 pr_debug("usb %s: already deleted?\n", dev_name(dev
));
854 pr_debug("usb %s: bus removed?\n", dev_name(dev
));
858 /* per-device configurations are common */
859 if (add_uevent_var(env
, "PRODUCT=%x/%x/%x",
860 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
861 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
862 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
)))
865 /* class-based driver binding models */
866 if (add_uevent_var(env
, "TYPE=%d/%d/%d",
867 usb_dev
->descriptor
.bDeviceClass
,
868 usb_dev
->descriptor
.bDeviceSubClass
,
869 usb_dev
->descriptor
.bDeviceProtocol
))
876 * usb_register_device_driver - register a USB device (not interface) driver
877 * @new_udriver: USB operations for the device driver
878 * @owner: module owner of this driver.
880 * Registers a USB device driver with the USB core. The list of
881 * unattached devices will be rescanned whenever a new driver is
882 * added, allowing the new driver to attach to any recognized devices.
884 * Return: A negative error code on failure and 0 on success.
886 int usb_register_device_driver(struct usb_device_driver
*new_udriver
,
887 struct module
*owner
)
894 new_udriver
->drvwrap
.for_devices
= 1;
895 new_udriver
->drvwrap
.driver
.name
= new_udriver
->name
;
896 new_udriver
->drvwrap
.driver
.bus
= &usb_bus_type
;
897 new_udriver
->drvwrap
.driver
.probe
= usb_probe_device
;
898 new_udriver
->drvwrap
.driver
.remove
= usb_unbind_device
;
899 new_udriver
->drvwrap
.driver
.owner
= owner
;
901 retval
= driver_register(&new_udriver
->drvwrap
.driver
);
904 pr_info("%s: registered new device driver %s\n",
905 usbcore_name
, new_udriver
->name
);
907 printk(KERN_ERR
"%s: error %d registering device "
909 usbcore_name
, retval
, new_udriver
->name
);
913 EXPORT_SYMBOL_GPL(usb_register_device_driver
);
916 * usb_deregister_device_driver - unregister a USB device (not interface) driver
917 * @udriver: USB operations of the device driver to unregister
918 * Context: must be able to sleep
920 * Unlinks the specified driver from the internal USB driver list.
922 void usb_deregister_device_driver(struct usb_device_driver
*udriver
)
924 pr_info("%s: deregistering device driver %s\n",
925 usbcore_name
, udriver
->name
);
927 driver_unregister(&udriver
->drvwrap
.driver
);
929 EXPORT_SYMBOL_GPL(usb_deregister_device_driver
);
932 * usb_register_driver - register a USB interface driver
933 * @new_driver: USB operations for the interface driver
934 * @owner: module owner of this driver.
935 * @mod_name: module name string
937 * Registers a USB interface driver with the USB core. The list of
938 * unattached interfaces will be rescanned whenever a new driver is
939 * added, allowing the new driver to attach to any recognized interfaces.
941 * Return: A negative error code on failure and 0 on success.
943 * NOTE: if you want your driver to use the USB major number, you must call
944 * usb_register_dev() to enable that functionality. This function no longer
945 * takes care of that.
947 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
,
948 const char *mod_name
)
955 new_driver
->drvwrap
.for_devices
= 0;
956 new_driver
->drvwrap
.driver
.name
= new_driver
->name
;
957 new_driver
->drvwrap
.driver
.bus
= &usb_bus_type
;
958 new_driver
->drvwrap
.driver
.probe
= usb_probe_interface
;
959 new_driver
->drvwrap
.driver
.remove
= usb_unbind_interface
;
960 new_driver
->drvwrap
.driver
.owner
= owner
;
961 new_driver
->drvwrap
.driver
.mod_name
= mod_name
;
962 spin_lock_init(&new_driver
->dynids
.lock
);
963 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
965 retval
= driver_register(&new_driver
->drvwrap
.driver
);
969 retval
= usb_create_newid_files(new_driver
);
973 pr_info("%s: registered new interface driver %s\n",
974 usbcore_name
, new_driver
->name
);
980 driver_unregister(&new_driver
->drvwrap
.driver
);
982 printk(KERN_ERR
"%s: error %d registering interface "
984 usbcore_name
, retval
, new_driver
->name
);
987 EXPORT_SYMBOL_GPL(usb_register_driver
);
990 * usb_deregister - unregister a USB interface driver
991 * @driver: USB operations of the interface driver to unregister
992 * Context: must be able to sleep
994 * Unlinks the specified driver from the internal USB driver list.
996 * NOTE: If you called usb_register_dev(), you still need to call
997 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
998 * this * call will no longer do it for you.
1000 void usb_deregister(struct usb_driver
*driver
)
1002 pr_info("%s: deregistering interface driver %s\n",
1003 usbcore_name
, driver
->name
);
1005 usb_remove_newid_files(driver
);
1006 driver_unregister(&driver
->drvwrap
.driver
);
1007 usb_free_dynids(driver
);
1009 EXPORT_SYMBOL_GPL(usb_deregister
);
1011 /* Forced unbinding of a USB interface driver, either because
1012 * it doesn't support pre_reset/post_reset/reset_resume or
1013 * because it doesn't support suspend/resume.
1015 * The caller must hold @intf's device's lock, but not @intf's lock.
1017 void usb_forced_unbind_intf(struct usb_interface
*intf
)
1019 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
1021 dev_dbg(&intf
->dev
, "forced unbind\n");
1022 usb_driver_release_interface(driver
, intf
);
1024 /* Mark the interface for later rebinding */
1025 intf
->needs_binding
= 1;
1029 * Unbind drivers for @udev's marked interfaces. These interfaces have
1030 * the needs_binding flag set, for example by usb_resume_interface().
1032 * The caller must hold @udev's device lock.
1034 static void unbind_marked_interfaces(struct usb_device
*udev
)
1036 struct usb_host_config
*config
;
1038 struct usb_interface
*intf
;
1040 config
= udev
->actconfig
;
1042 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1043 intf
= config
->interface
[i
];
1044 if (intf
->dev
.driver
&& intf
->needs_binding
)
1045 usb_forced_unbind_intf(intf
);
1050 /* Delayed forced unbinding of a USB interface driver and scan
1053 * The caller must hold @intf's device's lock, but not @intf's lock.
1055 * Note: Rebinds will be skipped if a system sleep transition is in
1056 * progress and the PM "complete" callback hasn't occurred yet.
1058 static void usb_rebind_intf(struct usb_interface
*intf
)
1062 /* Delayed unbind of an existing driver */
1063 if (intf
->dev
.driver
)
1064 usb_forced_unbind_intf(intf
);
1066 /* Try to rebind the interface */
1067 if (!intf
->dev
.power
.is_prepared
) {
1068 intf
->needs_binding
= 0;
1069 rc
= device_attach(&intf
->dev
);
1071 dev_warn(&intf
->dev
, "rebind failed: %d\n", rc
);
1076 * Rebind drivers to @udev's marked interfaces. These interfaces have
1077 * the needs_binding flag set.
1079 * The caller must hold @udev's device lock.
1081 static void rebind_marked_interfaces(struct usb_device
*udev
)
1083 struct usb_host_config
*config
;
1085 struct usb_interface
*intf
;
1087 config
= udev
->actconfig
;
1089 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1090 intf
= config
->interface
[i
];
1091 if (intf
->needs_binding
)
1092 usb_rebind_intf(intf
);
1098 * Unbind all of @udev's marked interfaces and then rebind all of them.
1099 * This ordering is necessary because some drivers claim several interfaces
1100 * when they are first probed.
1102 * The caller must hold @udev's device lock.
1104 void usb_unbind_and_rebind_marked_interfaces(struct usb_device
*udev
)
1106 unbind_marked_interfaces(udev
);
1107 rebind_marked_interfaces(udev
);
1112 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
1113 * There is no check for reset_resume here because it can be determined
1114 * only during resume whether reset_resume is needed.
1116 * The caller must hold @udev's device lock.
1118 static void unbind_no_pm_drivers_interfaces(struct usb_device
*udev
)
1120 struct usb_host_config
*config
;
1122 struct usb_interface
*intf
;
1123 struct usb_driver
*drv
;
1125 config
= udev
->actconfig
;
1127 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1128 intf
= config
->interface
[i
];
1130 if (intf
->dev
.driver
) {
1131 drv
= to_usb_driver(intf
->dev
.driver
);
1132 if (!drv
->suspend
|| !drv
->resume
)
1133 usb_forced_unbind_intf(intf
);
1139 static int usb_suspend_device(struct usb_device
*udev
, pm_message_t msg
)
1141 struct usb_device_driver
*udriver
;
1144 if (udev
->state
== USB_STATE_NOTATTACHED
||
1145 udev
->state
== USB_STATE_SUSPENDED
)
1148 /* For devices that don't have a driver, we do a generic suspend. */
1149 if (udev
->dev
.driver
)
1150 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1152 udev
->do_remote_wakeup
= 0;
1153 udriver
= &usb_generic_driver
;
1155 status
= udriver
->suspend(udev
, msg
);
1158 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1162 static int usb_resume_device(struct usb_device
*udev
, pm_message_t msg
)
1164 struct usb_device_driver
*udriver
;
1167 if (udev
->state
== USB_STATE_NOTATTACHED
)
1170 /* Can't resume it if it doesn't have a driver. */
1171 if (udev
->dev
.driver
== NULL
) {
1176 /* Non-root devices on a full/low-speed bus must wait for their
1177 * companion high-speed root hub, in case a handoff is needed.
1179 if (!PMSG_IS_AUTO(msg
) && udev
->parent
&& udev
->bus
->hs_companion
)
1180 device_pm_wait_for_dev(&udev
->dev
,
1181 &udev
->bus
->hs_companion
->root_hub
->dev
);
1183 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
)
1184 udev
->reset_resume
= 1;
1186 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1187 status
= udriver
->resume(udev
, msg
);
1190 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1194 static int usb_suspend_interface(struct usb_device
*udev
,
1195 struct usb_interface
*intf
, pm_message_t msg
)
1197 struct usb_driver
*driver
;
1200 if (udev
->state
== USB_STATE_NOTATTACHED
||
1201 intf
->condition
== USB_INTERFACE_UNBOUND
)
1203 driver
= to_usb_driver(intf
->dev
.driver
);
1205 /* at this time we know the driver supports suspend */
1206 status
= driver
->suspend(intf
, msg
);
1207 if (status
&& !PMSG_IS_AUTO(msg
))
1208 dev_err(&intf
->dev
, "suspend error %d\n", status
);
1211 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1215 static int usb_resume_interface(struct usb_device
*udev
,
1216 struct usb_interface
*intf
, pm_message_t msg
, int reset_resume
)
1218 struct usb_driver
*driver
;
1221 if (udev
->state
== USB_STATE_NOTATTACHED
)
1224 /* Don't let autoresume interfere with unbinding */
1225 if (intf
->condition
== USB_INTERFACE_UNBINDING
)
1228 /* Can't resume it if it doesn't have a driver. */
1229 if (intf
->condition
== USB_INTERFACE_UNBOUND
) {
1231 /* Carry out a deferred switch to altsetting 0 */
1232 if (intf
->needs_altsetting0
&& !intf
->dev
.power
.is_prepared
) {
1233 usb_set_interface(udev
, intf
->altsetting
[0].
1234 desc
.bInterfaceNumber
, 0);
1235 intf
->needs_altsetting0
= 0;
1240 /* Don't resume if the interface is marked for rebinding */
1241 if (intf
->needs_binding
)
1243 driver
= to_usb_driver(intf
->dev
.driver
);
1246 if (driver
->reset_resume
) {
1247 status
= driver
->reset_resume(intf
);
1249 dev_err(&intf
->dev
, "%s error %d\n",
1250 "reset_resume", status
);
1252 intf
->needs_binding
= 1;
1253 dev_dbg(&intf
->dev
, "no reset_resume for driver %s?\n",
1257 status
= driver
->resume(intf
);
1259 dev_err(&intf
->dev
, "resume error %d\n", status
);
1263 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1265 /* Later we will unbind the driver and/or reprobe, if necessary */
1270 * usb_suspend_both - suspend a USB device and its interfaces
1271 * @udev: the usb_device to suspend
1272 * @msg: Power Management message describing this state transition
1274 * This is the central routine for suspending USB devices. It calls the
1275 * suspend methods for all the interface drivers in @udev and then calls
1276 * the suspend method for @udev itself. When the routine is called in
1277 * autosuspend, if an error occurs at any stage, all the interfaces
1278 * which were suspended are resumed so that they remain in the same
1279 * state as the device, but when called from system sleep, all error
1280 * from suspend methods of interfaces and the non-root-hub device itself
1281 * are simply ignored, so all suspended interfaces are only resumed
1282 * to the device's state when @udev is root-hub and its suspend method
1285 * Autosuspend requests originating from a child device or an interface
1286 * driver may be made without the protection of @udev's device lock, but
1287 * all other suspend calls will hold the lock. Usbcore will insure that
1288 * method calls do not arrive during bind, unbind, or reset operations.
1289 * However drivers must be prepared to handle suspend calls arriving at
1290 * unpredictable times.
1292 * This routine can run only in process context.
1294 * Return: 0 if the suspend succeeded.
1296 static int usb_suspend_both(struct usb_device
*udev
, pm_message_t msg
)
1300 struct usb_interface
*intf
;
1302 if (udev
->state
== USB_STATE_NOTATTACHED
||
1303 udev
->state
== USB_STATE_SUSPENDED
)
1306 /* Suspend all the interfaces and then udev itself */
1307 if (udev
->actconfig
) {
1308 n
= udev
->actconfig
->desc
.bNumInterfaces
;
1309 for (i
= n
- 1; i
>= 0; --i
) {
1310 intf
= udev
->actconfig
->interface
[i
];
1311 status
= usb_suspend_interface(udev
, intf
, msg
);
1313 /* Ignore errors during system sleep transitions */
1314 if (!PMSG_IS_AUTO(msg
))
1321 status
= usb_suspend_device(udev
, msg
);
1324 * Ignore errors from non-root-hub devices during
1325 * system sleep transitions. For the most part,
1326 * these devices should go to low power anyway when
1327 * the entire bus is suspended.
1329 if (udev
->parent
&& !PMSG_IS_AUTO(msg
))
1333 * If the device is inaccessible, don't try to resume
1334 * suspended interfaces and just return the error.
1336 if (status
&& status
!= -EBUSY
) {
1340 err
= usb_get_status(udev
, USB_RECIP_DEVICE
, 0,
1344 "Failed to suspend device, error %d\n",
1351 /* If the suspend failed, resume interfaces that did get suspended */
1353 if (udev
->actconfig
) {
1354 msg
.event
^= (PM_EVENT_SUSPEND
| PM_EVENT_RESUME
);
1356 intf
= udev
->actconfig
->interface
[i
];
1357 usb_resume_interface(udev
, intf
, msg
, 0);
1361 /* If the suspend succeeded then prevent any more URB submissions
1362 * and flush any outstanding URBs.
1365 udev
->can_submit
= 0;
1366 for (i
= 0; i
< 16; ++i
) {
1367 usb_hcd_flush_endpoint(udev
, udev
->ep_out
[i
]);
1368 usb_hcd_flush_endpoint(udev
, udev
->ep_in
[i
]);
1373 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1378 * usb_resume_both - resume a USB device and its interfaces
1379 * @udev: the usb_device to resume
1380 * @msg: Power Management message describing this state transition
1382 * This is the central routine for resuming USB devices. It calls the
1383 * the resume method for @udev and then calls the resume methods for all
1384 * the interface drivers in @udev.
1386 * Autoresume requests originating from a child device or an interface
1387 * driver may be made without the protection of @udev's device lock, but
1388 * all other resume calls will hold the lock. Usbcore will insure that
1389 * method calls do not arrive during bind, unbind, or reset operations.
1390 * However drivers must be prepared to handle resume calls arriving at
1391 * unpredictable times.
1393 * This routine can run only in process context.
1395 * Return: 0 on success.
1397 static int usb_resume_both(struct usb_device
*udev
, pm_message_t msg
)
1401 struct usb_interface
*intf
;
1403 if (udev
->state
== USB_STATE_NOTATTACHED
) {
1407 udev
->can_submit
= 1;
1409 /* Resume the device */
1410 if (udev
->state
== USB_STATE_SUSPENDED
|| udev
->reset_resume
)
1411 status
= usb_resume_device(udev
, msg
);
1413 /* Resume the interfaces */
1414 if (status
== 0 && udev
->actconfig
) {
1415 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1416 intf
= udev
->actconfig
->interface
[i
];
1417 usb_resume_interface(udev
, intf
, msg
,
1418 udev
->reset_resume
);
1421 usb_mark_last_busy(udev
);
1424 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1426 udev
->reset_resume
= 0;
1430 static void choose_wakeup(struct usb_device
*udev
, pm_message_t msg
)
1434 /* Remote wakeup is needed only when we actually go to sleep.
1435 * For things like FREEZE and QUIESCE, if the device is already
1436 * autosuspended then its current wakeup setting is okay.
1438 if (msg
.event
== PM_EVENT_FREEZE
|| msg
.event
== PM_EVENT_QUIESCE
) {
1439 if (udev
->state
!= USB_STATE_SUSPENDED
)
1440 udev
->do_remote_wakeup
= 0;
1444 /* Enable remote wakeup if it is allowed, even if no interface drivers
1447 w
= device_may_wakeup(&udev
->dev
);
1449 /* If the device is autosuspended with the wrong wakeup setting,
1450 * autoresume now so the setting can be changed.
1452 if (udev
->state
== USB_STATE_SUSPENDED
&& w
!= udev
->do_remote_wakeup
)
1453 pm_runtime_resume(&udev
->dev
);
1454 udev
->do_remote_wakeup
= w
;
1457 /* The device lock is held by the PM core */
1458 int usb_suspend(struct device
*dev
, pm_message_t msg
)
1460 struct usb_device
*udev
= to_usb_device(dev
);
1462 unbind_no_pm_drivers_interfaces(udev
);
1464 /* From now on we are sure all drivers support suspend/resume
1465 * but not necessarily reset_resume()
1466 * so we may still need to unbind and rebind upon resume
1468 choose_wakeup(udev
, msg
);
1469 return usb_suspend_both(udev
, msg
);
1472 /* The device lock is held by the PM core */
1473 int usb_resume_complete(struct device
*dev
)
1475 struct usb_device
*udev
= to_usb_device(dev
);
1477 /* For PM complete calls, all we do is rebind interfaces
1478 * whose needs_binding flag is set
1480 if (udev
->state
!= USB_STATE_NOTATTACHED
)
1481 rebind_marked_interfaces(udev
);
1485 /* The device lock is held by the PM core */
1486 int usb_resume(struct device
*dev
, pm_message_t msg
)
1488 struct usb_device
*udev
= to_usb_device(dev
);
1491 /* For all calls, take the device back to full power and
1492 * tell the PM core in case it was autosuspended previously.
1493 * Unbind the interfaces that will need rebinding later,
1494 * because they fail to support reset_resume.
1495 * (This can't be done in usb_resume_interface()
1496 * above because it doesn't own the right set of locks.)
1498 status
= usb_resume_both(udev
, msg
);
1500 pm_runtime_disable(dev
);
1501 pm_runtime_set_active(dev
);
1502 pm_runtime_enable(dev
);
1503 unbind_marked_interfaces(udev
);
1506 /* Avoid PM error messages for devices disconnected while suspended
1507 * as we'll display regular disconnect messages just a bit later.
1509 if (status
== -ENODEV
|| status
== -ESHUTDOWN
)
1515 * usb_enable_autosuspend - allow a USB device to be autosuspended
1516 * @udev: the USB device which may be autosuspended
1518 * This routine allows @udev to be autosuspended. An autosuspend won't
1519 * take place until the autosuspend_delay has elapsed and all the other
1520 * necessary conditions are satisfied.
1522 * The caller must hold @udev's device lock.
1524 void usb_enable_autosuspend(struct usb_device
*udev
)
1526 pm_runtime_allow(&udev
->dev
);
1528 EXPORT_SYMBOL_GPL(usb_enable_autosuspend
);
1531 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1532 * @udev: the USB device which may not be autosuspended
1534 * This routine prevents @udev from being autosuspended and wakes it up
1535 * if it is already autosuspended.
1537 * The caller must hold @udev's device lock.
1539 void usb_disable_autosuspend(struct usb_device
*udev
)
1541 pm_runtime_forbid(&udev
->dev
);
1543 EXPORT_SYMBOL_GPL(usb_disable_autosuspend
);
1546 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1547 * @udev: the usb_device to autosuspend
1549 * This routine should be called when a core subsystem is finished using
1550 * @udev and wants to allow it to autosuspend. Examples would be when
1551 * @udev's device file in usbfs is closed or after a configuration change.
1553 * @udev's usage counter is decremented; if it drops to 0 and all the
1554 * interfaces are inactive then a delayed autosuspend will be attempted.
1555 * The attempt may fail (see autosuspend_check()).
1557 * The caller must hold @udev's device lock.
1559 * This routine can run only in process context.
1561 void usb_autosuspend_device(struct usb_device
*udev
)
1565 usb_mark_last_busy(udev
);
1566 status
= pm_runtime_put_sync_autosuspend(&udev
->dev
);
1567 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1568 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1573 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1574 * @udev: the usb_device to autoresume
1576 * This routine should be called when a core subsystem wants to use @udev
1577 * and needs to guarantee that it is not suspended. No autosuspend will
1578 * occur until usb_autosuspend_device() is called. (Note that this will
1579 * not prevent suspend events originating in the PM core.) Examples would
1580 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1581 * request is received.
1583 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1584 * However if the autoresume fails then the usage counter is re-decremented.
1586 * The caller must hold @udev's device lock.
1588 * This routine can run only in process context.
1590 * Return: 0 on success. A negative error code otherwise.
1592 int usb_autoresume_device(struct usb_device
*udev
)
1596 status
= pm_runtime_get_sync(&udev
->dev
);
1598 pm_runtime_put_sync(&udev
->dev
);
1599 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1600 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1608 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1609 * @intf: the usb_interface whose counter should be decremented
1611 * This routine should be called by an interface driver when it is
1612 * finished using @intf and wants to allow it to autosuspend. A typical
1613 * example would be a character-device driver when its device file is
1616 * The routine decrements @intf's usage counter. When the counter reaches
1617 * 0, a delayed autosuspend request for @intf's device is attempted. The
1618 * attempt may fail (see autosuspend_check()).
1620 * This routine can run only in process context.
1622 void usb_autopm_put_interface(struct usb_interface
*intf
)
1624 struct usb_device
*udev
= interface_to_usbdev(intf
);
1627 usb_mark_last_busy(udev
);
1628 atomic_dec(&intf
->pm_usage_cnt
);
1629 status
= pm_runtime_put_sync(&intf
->dev
);
1630 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1631 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1634 EXPORT_SYMBOL_GPL(usb_autopm_put_interface
);
1637 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1638 * @intf: the usb_interface whose counter should be decremented
1640 * This routine does much the same thing as usb_autopm_put_interface():
1641 * It decrements @intf's usage counter and schedules a delayed
1642 * autosuspend request if the counter is <= 0. The difference is that it
1643 * does not perform any synchronization; callers should hold a private
1644 * lock and handle all synchronization issues themselves.
1646 * Typically a driver would call this routine during an URB's completion
1647 * handler, if no more URBs were pending.
1649 * This routine can run in atomic context.
1651 void usb_autopm_put_interface_async(struct usb_interface
*intf
)
1653 struct usb_device
*udev
= interface_to_usbdev(intf
);
1656 usb_mark_last_busy(udev
);
1657 atomic_dec(&intf
->pm_usage_cnt
);
1658 status
= pm_runtime_put(&intf
->dev
);
1659 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1660 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1663 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async
);
1666 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1667 * @intf: the usb_interface whose counter should be decremented
1669 * This routine decrements @intf's usage counter but does not carry out an
1672 * This routine can run in atomic context.
1674 void usb_autopm_put_interface_no_suspend(struct usb_interface
*intf
)
1676 struct usb_device
*udev
= interface_to_usbdev(intf
);
1678 usb_mark_last_busy(udev
);
1679 atomic_dec(&intf
->pm_usage_cnt
);
1680 pm_runtime_put_noidle(&intf
->dev
);
1682 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend
);
1685 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1686 * @intf: the usb_interface whose counter should be incremented
1688 * This routine should be called by an interface driver when it wants to
1689 * use @intf and needs to guarantee that it is not suspended. In addition,
1690 * the routine prevents @intf from being autosuspended subsequently. (Note
1691 * that this will not prevent suspend events originating in the PM core.)
1692 * This prevention will persist until usb_autopm_put_interface() is called
1693 * or @intf is unbound. A typical example would be a character-device
1694 * driver when its device file is opened.
1696 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1697 * However if the autoresume fails then the counter is re-decremented.
1699 * This routine can run only in process context.
1701 * Return: 0 on success.
1703 int usb_autopm_get_interface(struct usb_interface
*intf
)
1707 status
= pm_runtime_get_sync(&intf
->dev
);
1709 pm_runtime_put_sync(&intf
->dev
);
1711 atomic_inc(&intf
->pm_usage_cnt
);
1712 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1713 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1719 EXPORT_SYMBOL_GPL(usb_autopm_get_interface
);
1722 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1723 * @intf: the usb_interface whose counter should be incremented
1725 * This routine does much the same thing as
1726 * usb_autopm_get_interface(): It increments @intf's usage counter and
1727 * queues an autoresume request if the device is suspended. The
1728 * differences are that it does not perform any synchronization (callers
1729 * should hold a private lock and handle all synchronization issues
1730 * themselves), and it does not autoresume the device directly (it only
1731 * queues a request). After a successful call, the device may not yet be
1734 * This routine can run in atomic context.
1736 * Return: 0 on success. A negative error code otherwise.
1738 int usb_autopm_get_interface_async(struct usb_interface
*intf
)
1742 status
= pm_runtime_get(&intf
->dev
);
1743 if (status
< 0 && status
!= -EINPROGRESS
)
1744 pm_runtime_put_noidle(&intf
->dev
);
1746 atomic_inc(&intf
->pm_usage_cnt
);
1747 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1748 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1750 if (status
> 0 || status
== -EINPROGRESS
)
1754 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async
);
1757 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1758 * @intf: the usb_interface whose counter should be incremented
1760 * This routine increments @intf's usage counter but does not carry out an
1763 * This routine can run in atomic context.
1765 void usb_autopm_get_interface_no_resume(struct usb_interface
*intf
)
1767 struct usb_device
*udev
= interface_to_usbdev(intf
);
1769 usb_mark_last_busy(udev
);
1770 atomic_inc(&intf
->pm_usage_cnt
);
1771 pm_runtime_get_noresume(&intf
->dev
);
1773 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume
);
1775 /* Internal routine to check whether we may autosuspend a device. */
1776 static int autosuspend_check(struct usb_device
*udev
)
1779 struct usb_interface
*intf
;
1781 if (udev
->state
== USB_STATE_NOTATTACHED
)
1784 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1785 * any interface drivers require remote wakeup but it isn't available.
1788 if (udev
->actconfig
) {
1789 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1790 intf
= udev
->actconfig
->interface
[i
];
1792 /* We don't need to check interfaces that are
1793 * disabled for runtime PM. Either they are unbound
1794 * or else their drivers don't support autosuspend
1795 * and so they are permanently active.
1797 if (intf
->dev
.power
.disable_depth
)
1799 if (atomic_read(&intf
->dev
.power
.usage_count
) > 0)
1801 w
|= intf
->needs_remote_wakeup
;
1803 /* Don't allow autosuspend if the device will need
1804 * a reset-resume and any of its interface drivers
1805 * doesn't include support or needs remote wakeup.
1807 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
) {
1808 struct usb_driver
*driver
;
1810 driver
= to_usb_driver(intf
->dev
.driver
);
1811 if (!driver
->reset_resume
||
1812 intf
->needs_remote_wakeup
)
1817 if (w
&& !device_can_wakeup(&udev
->dev
)) {
1818 dev_dbg(&udev
->dev
, "remote wakeup needed for autosuspend\n");
1823 * If the device is a direct child of the root hub and the HCD
1824 * doesn't handle wakeup requests, don't allow autosuspend when
1827 if (w
&& udev
->parent
== udev
->bus
->root_hub
&&
1828 bus_to_hcd(udev
->bus
)->cant_recv_wakeups
) {
1829 dev_dbg(&udev
->dev
, "HCD doesn't handle wakeup requests\n");
1833 udev
->do_remote_wakeup
= w
;
1837 int usb_runtime_suspend(struct device
*dev
)
1839 struct usb_device
*udev
= to_usb_device(dev
);
1842 /* A USB device can be suspended if it passes the various autosuspend
1843 * checks. Runtime suspend for a USB device means suspending all the
1844 * interfaces and then the device itself.
1846 if (autosuspend_check(udev
) != 0)
1849 status
= usb_suspend_both(udev
, PMSG_AUTO_SUSPEND
);
1851 /* Allow a retry if autosuspend failed temporarily */
1852 if (status
== -EAGAIN
|| status
== -EBUSY
)
1853 usb_mark_last_busy(udev
);
1856 * The PM core reacts badly unless the return code is 0,
1857 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1858 * (except for root hubs, because they don't suspend through
1859 * an upstream port like other USB devices).
1861 if (status
!= 0 && udev
->parent
)
1866 int usb_runtime_resume(struct device
*dev
)
1868 struct usb_device
*udev
= to_usb_device(dev
);
1871 /* Runtime resume for a USB device means resuming both the device
1872 * and all its interfaces.
1874 status
= usb_resume_both(udev
, PMSG_AUTO_RESUME
);
1878 int usb_runtime_idle(struct device
*dev
)
1880 struct usb_device
*udev
= to_usb_device(dev
);
1882 /* An idle USB device can be suspended if it passes the various
1883 * autosuspend checks.
1885 if (autosuspend_check(udev
) == 0)
1886 pm_runtime_autosuspend(dev
);
1887 /* Tell the core not to suspend it, though. */
1891 int usb_set_usb2_hardware_lpm(struct usb_device
*udev
, int enable
)
1893 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1896 if (enable
&& !udev
->usb2_hw_lpm_allowed
)
1899 if (hcd
->driver
->set_usb2_hw_lpm
) {
1900 ret
= hcd
->driver
->set_usb2_hw_lpm(hcd
, udev
, enable
);
1902 udev
->usb2_hw_lpm_enabled
= enable
;
1908 #endif /* CONFIG_PM */
1910 struct bus_type usb_bus_type
= {
1912 .match
= usb_device_match
,
1913 .uevent
= usb_uevent
,