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
;
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");
301 id
= usb_match_dynamic_id(intf
, driver
);
303 id
= usb_match_id(intf
, driver
->id_table
);
307 dev_dbg(dev
, "%s - got id\n", __func__
);
309 error
= usb_autoresume_device(udev
);
313 intf
->condition
= USB_INTERFACE_BINDING
;
315 /* Probed interfaces are initially active. They are
316 * runtime-PM-enabled only if the driver has autosuspend support.
317 * They are sensitive to their children's power states.
319 pm_runtime_set_active(dev
);
320 pm_suspend_ignore_children(dev
, false);
321 if (driver
->supports_autosuspend
)
322 pm_runtime_enable(dev
);
324 /* If the new driver doesn't allow hub-initiated LPM, and we can't
325 * disable hub-initiated LPM, then fail the probe.
327 * Otherwise, leaving LPM enabled should be harmless, because the
328 * endpoint intervals should remain the same, and the U1/U2 timeouts
329 * should remain the same.
331 * If we need to install alt setting 0 before probe, or another alt
332 * setting during probe, that should also be fine. usb_set_interface()
333 * will attempt to disable LPM, and fail if it can't disable it.
335 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
336 if (lpm_disable_error
&& driver
->disable_hub_initiated_lpm
) {
337 dev_err(&intf
->dev
, "%s Failed to disable LPM for driver %s\n.",
338 __func__
, driver
->name
);
339 error
= lpm_disable_error
;
343 /* Carry out a deferred switch to altsetting 0 */
344 if (intf
->needs_altsetting0
) {
345 error
= usb_set_interface(udev
, intf
->altsetting
[0].
346 desc
.bInterfaceNumber
, 0);
349 intf
->needs_altsetting0
= 0;
352 error
= driver
->probe(intf
, id
);
356 intf
->condition
= USB_INTERFACE_BOUND
;
358 /* If the LPM disable succeeded, balance the ref counts. */
359 if (!lpm_disable_error
)
360 usb_unlocked_enable_lpm(udev
);
362 usb_autosuspend_device(udev
);
366 usb_set_intfdata(intf
, NULL
);
367 intf
->needs_remote_wakeup
= 0;
368 intf
->condition
= USB_INTERFACE_UNBOUND
;
370 /* If the LPM disable succeeded, balance the ref counts. */
371 if (!lpm_disable_error
)
372 usb_unlocked_enable_lpm(udev
);
374 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
375 if (driver
->supports_autosuspend
)
376 pm_runtime_disable(dev
);
377 pm_runtime_set_suspended(dev
);
379 usb_autosuspend_device(udev
);
383 /* called from driver core with dev locked */
384 static int usb_unbind_interface(struct device
*dev
)
386 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
387 struct usb_interface
*intf
= to_usb_interface(dev
);
388 struct usb_host_endpoint
*ep
, **eps
= NULL
;
389 struct usb_device
*udev
;
390 int i
, j
, error
, r
, lpm_disable_error
;
392 intf
->condition
= USB_INTERFACE_UNBINDING
;
394 /* Autoresume for set_interface call below */
395 udev
= interface_to_usbdev(intf
);
396 error
= usb_autoresume_device(udev
);
398 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
399 * the driver is unbound. If LPM isn't disabled, that's fine because it
400 * wouldn't be enabled unless all the bound interfaces supported
403 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
406 * Terminate all URBs for this interface unless the driver
407 * supports "soft" unbinding and the device is still present.
409 if (!driver
->soft_unbind
|| udev
->state
== USB_STATE_NOTATTACHED
)
410 usb_disable_interface(udev
, intf
, false);
412 driver
->disconnect(intf
);
415 for (i
= 0, j
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
416 ep
= &intf
->cur_altsetting
->endpoint
[i
];
417 if (ep
->streams
== 0)
420 eps
= kmalloc(USB_MAXENDPOINTS
* sizeof(void *),
423 dev_warn(dev
, "oom, leaking streams\n");
430 usb_free_streams(intf
, eps
, j
, GFP_KERNEL
);
434 /* Reset other interface state.
435 * We cannot do a Set-Interface if the device is suspended or
436 * if it is prepared for a system sleep (since installing a new
437 * altsetting means creating new endpoint device entries).
438 * When either of these happens, defer the Set-Interface.
440 if (intf
->cur_altsetting
->desc
.bAlternateSetting
== 0) {
441 /* Already in altsetting 0 so skip Set-Interface.
442 * Just re-enable it without affecting the endpoint toggles.
444 usb_enable_interface(udev
, intf
, false);
445 } else if (!error
&& !intf
->dev
.power
.is_prepared
) {
446 r
= usb_set_interface(udev
, intf
->altsetting
[0].
447 desc
.bInterfaceNumber
, 0);
449 intf
->needs_altsetting0
= 1;
451 intf
->needs_altsetting0
= 1;
453 usb_set_intfdata(intf
, NULL
);
455 intf
->condition
= USB_INTERFACE_UNBOUND
;
456 intf
->needs_remote_wakeup
= 0;
458 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
459 if (!lpm_disable_error
)
460 usb_unlocked_enable_lpm(udev
);
462 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
463 if (driver
->supports_autosuspend
)
464 pm_runtime_disable(dev
);
465 pm_runtime_set_suspended(dev
);
467 /* Undo any residual pm_autopm_get_interface_* calls */
468 for (r
= atomic_read(&intf
->pm_usage_cnt
); r
> 0; --r
)
469 usb_autopm_put_interface_no_suspend(intf
);
470 atomic_set(&intf
->pm_usage_cnt
, 0);
473 usb_autosuspend_device(udev
);
479 * usb_driver_claim_interface - bind a driver to an interface
480 * @driver: the driver to be bound
481 * @iface: the interface to which it will be bound; must be in the
482 * usb device's active configuration
483 * @priv: driver data associated with that interface
485 * This is used by usb device drivers that need to claim more than one
486 * interface on a device when probing (audio and acm are current examples).
487 * No device driver should directly modify internal usb_interface or
488 * usb_device structure members.
490 * Few drivers should need to use this routine, since the most natural
491 * way to bind to an interface is to return the private data from
492 * the driver's probe() method.
494 * Callers must own the device lock, so driver probe() entries don't need
495 * extra locking, but other call contexts may need to explicitly claim that
498 * Return: 0 on success.
500 int usb_driver_claim_interface(struct usb_driver
*driver
,
501 struct usb_interface
*iface
, void *priv
)
503 struct device
*dev
= &iface
->dev
;
504 struct usb_device
*udev
;
506 int lpm_disable_error
;
511 udev
= interface_to_usbdev(iface
);
513 dev
->driver
= &driver
->drvwrap
.driver
;
514 usb_set_intfdata(iface
, priv
);
515 iface
->needs_binding
= 0;
517 iface
->condition
= USB_INTERFACE_BOUND
;
519 /* Disable LPM until this driver is bound. */
520 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
521 if (lpm_disable_error
&& driver
->disable_hub_initiated_lpm
) {
522 dev_err(&iface
->dev
, "%s Failed to disable LPM for driver %s\n.",
523 __func__
, driver
->name
);
527 /* Claimed interfaces are initially inactive (suspended) and
528 * runtime-PM-enabled, but only if the driver has autosuspend
529 * support. Otherwise they are marked active, to prevent the
530 * device from being autosuspended, but left disabled. In either
531 * case they are sensitive to their children's power states.
533 pm_suspend_ignore_children(dev
, false);
534 if (driver
->supports_autosuspend
)
535 pm_runtime_enable(dev
);
537 pm_runtime_set_active(dev
);
539 /* if interface was already added, bind now; else let
540 * the future device_add() bind it, bypassing probe()
542 if (device_is_registered(dev
))
543 retval
= device_bind_driver(dev
);
545 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
546 if (!lpm_disable_error
)
547 usb_unlocked_enable_lpm(udev
);
551 EXPORT_SYMBOL_GPL(usb_driver_claim_interface
);
554 * usb_driver_release_interface - unbind a driver from an interface
555 * @driver: the driver to be unbound
556 * @iface: the interface from which it will be unbound
558 * This can be used by drivers to release an interface without waiting
559 * for their disconnect() methods to be called. In typical cases this
560 * also causes the driver disconnect() method to be called.
562 * This call is synchronous, and may not be used in an interrupt context.
563 * Callers must own the device lock, so driver disconnect() entries don't
564 * need extra locking, but other call contexts may need to explicitly claim
567 void usb_driver_release_interface(struct usb_driver
*driver
,
568 struct usb_interface
*iface
)
570 struct device
*dev
= &iface
->dev
;
572 /* this should never happen, don't release something that's not ours */
573 if (!dev
->driver
|| dev
->driver
!= &driver
->drvwrap
.driver
)
576 /* don't release from within disconnect() */
577 if (iface
->condition
!= USB_INTERFACE_BOUND
)
579 iface
->condition
= USB_INTERFACE_UNBINDING
;
581 /* Release via the driver core only if the interface
582 * has already been registered
584 if (device_is_registered(dev
)) {
585 device_release_driver(dev
);
588 usb_unbind_interface(dev
);
593 EXPORT_SYMBOL_GPL(usb_driver_release_interface
);
595 /* returns 0 if no match, 1 if match */
596 int usb_match_device(struct usb_device
*dev
, const struct usb_device_id
*id
)
598 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
599 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
602 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
603 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
606 /* No need to test id->bcdDevice_lo != 0, since 0 is never
607 greater than any unsigned number. */
608 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
609 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
612 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
613 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
616 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
617 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
620 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
621 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
624 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
625 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
631 /* returns 0 if no match, 1 if match */
632 int usb_match_one_id_intf(struct usb_device
*dev
,
633 struct usb_host_interface
*intf
,
634 const struct usb_device_id
*id
)
636 /* The interface class, subclass, protocol and number should never be
637 * checked for a match if the device class is Vendor Specific,
638 * unless the match record specifies the Vendor ID. */
639 if (dev
->descriptor
.bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
640 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
641 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
642 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
643 USB_DEVICE_ID_MATCH_INT_PROTOCOL
|
644 USB_DEVICE_ID_MATCH_INT_NUMBER
)))
647 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
648 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
651 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
652 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
655 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
656 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
659 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_NUMBER
) &&
660 (id
->bInterfaceNumber
!= intf
->desc
.bInterfaceNumber
))
666 /* returns 0 if no match, 1 if match */
667 int usb_match_one_id(struct usb_interface
*interface
,
668 const struct usb_device_id
*id
)
670 struct usb_host_interface
*intf
;
671 struct usb_device
*dev
;
673 /* proc_connectinfo in devio.c may call us with id == NULL. */
677 intf
= interface
->cur_altsetting
;
678 dev
= interface_to_usbdev(interface
);
680 if (!usb_match_device(dev
, id
))
683 return usb_match_one_id_intf(dev
, intf
, id
);
685 EXPORT_SYMBOL_GPL(usb_match_one_id
);
688 * usb_match_id - find first usb_device_id matching device or interface
689 * @interface: the interface of interest
690 * @id: array of usb_device_id structures, terminated by zero entry
692 * usb_match_id searches an array of usb_device_id's and returns
693 * the first one matching the device or interface, or null.
694 * This is used when binding (or rebinding) a driver to an interface.
695 * Most USB device drivers will use this indirectly, through the usb core,
696 * but some layered driver frameworks use it directly.
697 * These device tables are exported with MODULE_DEVICE_TABLE, through
698 * modutils, to support the driver loading functionality of USB hotplugging.
700 * Return: The first matching usb_device_id, or %NULL.
704 * The "match_flags" element in a usb_device_id controls which
705 * members are used. If the corresponding bit is set, the
706 * value in the device_id must match its corresponding member
707 * in the device or interface descriptor, or else the device_id
710 * "driver_info" is normally used only by device drivers,
711 * but you can create a wildcard "matches anything" usb_device_id
712 * as a driver's "modules.usbmap" entry if you provide an id with
713 * only a nonzero "driver_info" field. If you do this, the USB device
714 * driver's probe() routine should use additional intelligence to
715 * decide whether to bind to the specified interface.
717 * What Makes Good usb_device_id Tables:
719 * The match algorithm is very simple, so that intelligence in
720 * driver selection must come from smart driver id records.
721 * Unless you have good reasons to use another selection policy,
722 * provide match elements only in related groups, and order match
723 * specifiers from specific to general. Use the macros provided
724 * for that purpose if you can.
726 * The most specific match specifiers use device descriptor
727 * data. These are commonly used with product-specific matches;
728 * the USB_DEVICE macro lets you provide vendor and product IDs,
729 * and you can also match against ranges of product revisions.
730 * These are widely used for devices with application or vendor
731 * specific bDeviceClass values.
733 * Matches based on device class/subclass/protocol specifications
734 * are slightly more general; use the USB_DEVICE_INFO macro, or
735 * its siblings. These are used with single-function devices
736 * where bDeviceClass doesn't specify that each interface has
739 * Matches based on interface class/subclass/protocol are the
740 * most general; they let drivers bind to any interface on a
741 * multiple-function device. Use the USB_INTERFACE_INFO
742 * macro, or its siblings, to match class-per-interface style
743 * devices (as recorded in bInterfaceClass).
745 * Note that an entry created by USB_INTERFACE_INFO won't match
746 * any interface if the device class is set to Vendor-Specific.
747 * This is deliberate; according to the USB spec the meanings of
748 * the interface class/subclass/protocol for these devices are also
749 * vendor-specific, and hence matching against a standard product
750 * class wouldn't work anyway. If you really want to use an
751 * interface-based match for such a device, create a match record
752 * that also specifies the vendor ID. (Unforunately there isn't a
753 * standard macro for creating records like this.)
755 * Within those groups, remember that not all combinations are
756 * meaningful. For example, don't give a product version range
757 * without vendor and product IDs; or specify a protocol without
758 * its associated class and subclass.
760 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
761 const struct usb_device_id
*id
)
763 /* proc_connectinfo in devio.c may call us with id == NULL. */
767 /* It is important to check that id->driver_info is nonzero,
768 since an entry that is all zeroes except for a nonzero
769 id->driver_info is the way to create an entry that
770 indicates that the driver want to examine every
771 device and interface. */
772 for (; id
->idVendor
|| id
->idProduct
|| id
->bDeviceClass
||
773 id
->bInterfaceClass
|| id
->driver_info
; id
++) {
774 if (usb_match_one_id(interface
, id
))
780 EXPORT_SYMBOL_GPL(usb_match_id
);
782 static int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
784 /* devices and interfaces are handled separately */
785 if (is_usb_device(dev
)) {
787 /* interface drivers never match devices */
788 if (!is_usb_device_driver(drv
))
791 /* TODO: Add real matching code */
794 } else if (is_usb_interface(dev
)) {
795 struct usb_interface
*intf
;
796 struct usb_driver
*usb_drv
;
797 const struct usb_device_id
*id
;
799 /* device drivers never match interfaces */
800 if (is_usb_device_driver(drv
))
803 intf
= to_usb_interface(dev
);
804 usb_drv
= to_usb_driver(drv
);
806 id
= usb_match_id(intf
, usb_drv
->id_table
);
810 id
= usb_match_dynamic_id(intf
, usb_drv
);
818 static int usb_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
820 struct usb_device
*usb_dev
;
822 if (is_usb_device(dev
)) {
823 usb_dev
= to_usb_device(dev
);
824 } else if (is_usb_interface(dev
)) {
825 struct usb_interface
*intf
= to_usb_interface(dev
);
827 usb_dev
= interface_to_usbdev(intf
);
832 if (usb_dev
->devnum
< 0) {
833 /* driver is often null here; dev_dbg() would oops */
834 pr_debug("usb %s: already deleted?\n", dev_name(dev
));
838 pr_debug("usb %s: bus removed?\n", dev_name(dev
));
842 /* per-device configurations are common */
843 if (add_uevent_var(env
, "PRODUCT=%x/%x/%x",
844 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
845 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
846 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
)))
849 /* class-based driver binding models */
850 if (add_uevent_var(env
, "TYPE=%d/%d/%d",
851 usb_dev
->descriptor
.bDeviceClass
,
852 usb_dev
->descriptor
.bDeviceSubClass
,
853 usb_dev
->descriptor
.bDeviceProtocol
))
860 * usb_register_device_driver - register a USB device (not interface) driver
861 * @new_udriver: USB operations for the device driver
862 * @owner: module owner of this driver.
864 * Registers a USB device driver with the USB core. The list of
865 * unattached devices will be rescanned whenever a new driver is
866 * added, allowing the new driver to attach to any recognized devices.
868 * Return: A negative error code on failure and 0 on success.
870 int usb_register_device_driver(struct usb_device_driver
*new_udriver
,
871 struct module
*owner
)
878 new_udriver
->drvwrap
.for_devices
= 1;
879 new_udriver
->drvwrap
.driver
.name
= new_udriver
->name
;
880 new_udriver
->drvwrap
.driver
.bus
= &usb_bus_type
;
881 new_udriver
->drvwrap
.driver
.probe
= usb_probe_device
;
882 new_udriver
->drvwrap
.driver
.remove
= usb_unbind_device
;
883 new_udriver
->drvwrap
.driver
.owner
= owner
;
885 retval
= driver_register(&new_udriver
->drvwrap
.driver
);
888 pr_info("%s: registered new device driver %s\n",
889 usbcore_name
, new_udriver
->name
);
891 printk(KERN_ERR
"%s: error %d registering device "
893 usbcore_name
, retval
, new_udriver
->name
);
897 EXPORT_SYMBOL_GPL(usb_register_device_driver
);
900 * usb_deregister_device_driver - unregister a USB device (not interface) driver
901 * @udriver: USB operations of the device driver to unregister
902 * Context: must be able to sleep
904 * Unlinks the specified driver from the internal USB driver list.
906 void usb_deregister_device_driver(struct usb_device_driver
*udriver
)
908 pr_info("%s: deregistering device driver %s\n",
909 usbcore_name
, udriver
->name
);
911 driver_unregister(&udriver
->drvwrap
.driver
);
913 EXPORT_SYMBOL_GPL(usb_deregister_device_driver
);
916 * usb_register_driver - register a USB interface driver
917 * @new_driver: USB operations for the interface driver
918 * @owner: module owner of this driver.
919 * @mod_name: module name string
921 * Registers a USB interface driver with the USB core. The list of
922 * unattached interfaces will be rescanned whenever a new driver is
923 * added, allowing the new driver to attach to any recognized interfaces.
925 * Return: A negative error code on failure and 0 on success.
927 * NOTE: if you want your driver to use the USB major number, you must call
928 * usb_register_dev() to enable that functionality. This function no longer
929 * takes care of that.
931 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
,
932 const char *mod_name
)
939 new_driver
->drvwrap
.for_devices
= 0;
940 new_driver
->drvwrap
.driver
.name
= new_driver
->name
;
941 new_driver
->drvwrap
.driver
.bus
= &usb_bus_type
;
942 new_driver
->drvwrap
.driver
.probe
= usb_probe_interface
;
943 new_driver
->drvwrap
.driver
.remove
= usb_unbind_interface
;
944 new_driver
->drvwrap
.driver
.owner
= owner
;
945 new_driver
->drvwrap
.driver
.mod_name
= mod_name
;
946 spin_lock_init(&new_driver
->dynids
.lock
);
947 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
949 retval
= driver_register(&new_driver
->drvwrap
.driver
);
953 retval
= usb_create_newid_files(new_driver
);
957 pr_info("%s: registered new interface driver %s\n",
958 usbcore_name
, new_driver
->name
);
964 driver_unregister(&new_driver
->drvwrap
.driver
);
966 printk(KERN_ERR
"%s: error %d registering interface "
968 usbcore_name
, retval
, new_driver
->name
);
971 EXPORT_SYMBOL_GPL(usb_register_driver
);
974 * usb_deregister - unregister a USB interface driver
975 * @driver: USB operations of the interface driver to unregister
976 * Context: must be able to sleep
978 * Unlinks the specified driver from the internal USB driver list.
980 * NOTE: If you called usb_register_dev(), you still need to call
981 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
982 * this * call will no longer do it for you.
984 void usb_deregister(struct usb_driver
*driver
)
986 pr_info("%s: deregistering interface driver %s\n",
987 usbcore_name
, driver
->name
);
989 usb_remove_newid_files(driver
);
990 driver_unregister(&driver
->drvwrap
.driver
);
991 usb_free_dynids(driver
);
993 EXPORT_SYMBOL_GPL(usb_deregister
);
995 /* Forced unbinding of a USB interface driver, either because
996 * it doesn't support pre_reset/post_reset/reset_resume or
997 * because it doesn't support suspend/resume.
999 * The caller must hold @intf's device's lock, but not @intf's lock.
1001 void usb_forced_unbind_intf(struct usb_interface
*intf
)
1003 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
1005 dev_dbg(&intf
->dev
, "forced unbind\n");
1006 usb_driver_release_interface(driver
, intf
);
1008 /* Mark the interface for later rebinding */
1009 intf
->needs_binding
= 1;
1013 * Unbind drivers for @udev's marked interfaces. These interfaces have
1014 * the needs_binding flag set, for example by usb_resume_interface().
1016 * The caller must hold @udev's device lock.
1018 static void unbind_marked_interfaces(struct usb_device
*udev
)
1020 struct usb_host_config
*config
;
1022 struct usb_interface
*intf
;
1024 config
= udev
->actconfig
;
1026 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1027 intf
= config
->interface
[i
];
1028 if (intf
->dev
.driver
&& intf
->needs_binding
)
1029 usb_forced_unbind_intf(intf
);
1034 /* Delayed forced unbinding of a USB interface driver and scan
1037 * The caller must hold @intf's device's lock, but not @intf's lock.
1039 * Note: Rebinds will be skipped if a system sleep transition is in
1040 * progress and the PM "complete" callback hasn't occurred yet.
1042 static void usb_rebind_intf(struct usb_interface
*intf
)
1046 /* Delayed unbind of an existing driver */
1047 if (intf
->dev
.driver
)
1048 usb_forced_unbind_intf(intf
);
1050 /* Try to rebind the interface */
1051 if (!intf
->dev
.power
.is_prepared
) {
1052 intf
->needs_binding
= 0;
1053 rc
= device_attach(&intf
->dev
);
1055 dev_warn(&intf
->dev
, "rebind failed: %d\n", rc
);
1060 * Rebind drivers to @udev's marked interfaces. These interfaces have
1061 * the needs_binding flag set.
1063 * The caller must hold @udev's device lock.
1065 static void rebind_marked_interfaces(struct usb_device
*udev
)
1067 struct usb_host_config
*config
;
1069 struct usb_interface
*intf
;
1071 config
= udev
->actconfig
;
1073 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1074 intf
= config
->interface
[i
];
1075 if (intf
->needs_binding
)
1076 usb_rebind_intf(intf
);
1082 * Unbind all of @udev's marked interfaces and then rebind all of them.
1083 * This ordering is necessary because some drivers claim several interfaces
1084 * when they are first probed.
1086 * The caller must hold @udev's device lock.
1088 void usb_unbind_and_rebind_marked_interfaces(struct usb_device
*udev
)
1090 unbind_marked_interfaces(udev
);
1091 rebind_marked_interfaces(udev
);
1096 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
1097 * There is no check for reset_resume here because it can be determined
1098 * only during resume whether reset_resume is needed.
1100 * The caller must hold @udev's device lock.
1102 static void unbind_no_pm_drivers_interfaces(struct usb_device
*udev
)
1104 struct usb_host_config
*config
;
1106 struct usb_interface
*intf
;
1107 struct usb_driver
*drv
;
1109 config
= udev
->actconfig
;
1111 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1112 intf
= config
->interface
[i
];
1114 if (intf
->dev
.driver
) {
1115 drv
= to_usb_driver(intf
->dev
.driver
);
1116 if (!drv
->suspend
|| !drv
->resume
)
1117 usb_forced_unbind_intf(intf
);
1123 static int usb_suspend_device(struct usb_device
*udev
, pm_message_t msg
)
1125 struct usb_device_driver
*udriver
;
1128 if (udev
->state
== USB_STATE_NOTATTACHED
||
1129 udev
->state
== USB_STATE_SUSPENDED
)
1132 /* For devices that don't have a driver, we do a generic suspend. */
1133 if (udev
->dev
.driver
)
1134 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1136 udev
->do_remote_wakeup
= 0;
1137 udriver
= &usb_generic_driver
;
1139 status
= udriver
->suspend(udev
, msg
);
1142 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1146 static int usb_resume_device(struct usb_device
*udev
, pm_message_t msg
)
1148 struct usb_device_driver
*udriver
;
1151 if (udev
->state
== USB_STATE_NOTATTACHED
)
1154 /* Can't resume it if it doesn't have a driver. */
1155 if (udev
->dev
.driver
== NULL
) {
1160 /* Non-root devices on a full/low-speed bus must wait for their
1161 * companion high-speed root hub, in case a handoff is needed.
1163 if (!PMSG_IS_AUTO(msg
) && udev
->parent
&& udev
->bus
->hs_companion
)
1164 device_pm_wait_for_dev(&udev
->dev
,
1165 &udev
->bus
->hs_companion
->root_hub
->dev
);
1167 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
)
1168 udev
->reset_resume
= 1;
1170 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1171 status
= udriver
->resume(udev
, msg
);
1174 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1178 static int usb_suspend_interface(struct usb_device
*udev
,
1179 struct usb_interface
*intf
, pm_message_t msg
)
1181 struct usb_driver
*driver
;
1184 if (udev
->state
== USB_STATE_NOTATTACHED
||
1185 intf
->condition
== USB_INTERFACE_UNBOUND
)
1187 driver
= to_usb_driver(intf
->dev
.driver
);
1189 /* at this time we know the driver supports suspend */
1190 status
= driver
->suspend(intf
, msg
);
1191 if (status
&& !PMSG_IS_AUTO(msg
))
1192 dev_err(&intf
->dev
, "suspend error %d\n", status
);
1195 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1199 static int usb_resume_interface(struct usb_device
*udev
,
1200 struct usb_interface
*intf
, pm_message_t msg
, int reset_resume
)
1202 struct usb_driver
*driver
;
1205 if (udev
->state
== USB_STATE_NOTATTACHED
)
1208 /* Don't let autoresume interfere with unbinding */
1209 if (intf
->condition
== USB_INTERFACE_UNBINDING
)
1212 /* Can't resume it if it doesn't have a driver. */
1213 if (intf
->condition
== USB_INTERFACE_UNBOUND
) {
1215 /* Carry out a deferred switch to altsetting 0 */
1216 if (intf
->needs_altsetting0
&& !intf
->dev
.power
.is_prepared
) {
1217 usb_set_interface(udev
, intf
->altsetting
[0].
1218 desc
.bInterfaceNumber
, 0);
1219 intf
->needs_altsetting0
= 0;
1224 /* Don't resume if the interface is marked for rebinding */
1225 if (intf
->needs_binding
)
1227 driver
= to_usb_driver(intf
->dev
.driver
);
1230 if (driver
->reset_resume
) {
1231 status
= driver
->reset_resume(intf
);
1233 dev_err(&intf
->dev
, "%s error %d\n",
1234 "reset_resume", status
);
1236 intf
->needs_binding
= 1;
1237 dev_dbg(&intf
->dev
, "no reset_resume for driver %s?\n",
1241 status
= driver
->resume(intf
);
1243 dev_err(&intf
->dev
, "resume error %d\n", status
);
1247 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1249 /* Later we will unbind the driver and/or reprobe, if necessary */
1254 * usb_suspend_both - suspend a USB device and its interfaces
1255 * @udev: the usb_device to suspend
1256 * @msg: Power Management message describing this state transition
1258 * This is the central routine for suspending USB devices. It calls the
1259 * suspend methods for all the interface drivers in @udev and then calls
1260 * the suspend method for @udev itself. When the routine is called in
1261 * autosuspend, if an error occurs at any stage, all the interfaces
1262 * which were suspended are resumed so that they remain in the same
1263 * state as the device, but when called from system sleep, all error
1264 * from suspend methods of interfaces and the non-root-hub device itself
1265 * are simply ignored, so all suspended interfaces are only resumed
1266 * to the device's state when @udev is root-hub and its suspend method
1269 * Autosuspend requests originating from a child device or an interface
1270 * driver may be made without the protection of @udev's device lock, but
1271 * all other suspend calls will hold the lock. Usbcore will insure that
1272 * method calls do not arrive during bind, unbind, or reset operations.
1273 * However drivers must be prepared to handle suspend calls arriving at
1274 * unpredictable times.
1276 * This routine can run only in process context.
1278 * Return: 0 if the suspend succeeded.
1280 static int usb_suspend_both(struct usb_device
*udev
, pm_message_t msg
)
1284 struct usb_interface
*intf
;
1286 if (udev
->state
== USB_STATE_NOTATTACHED
||
1287 udev
->state
== USB_STATE_SUSPENDED
)
1290 /* Suspend all the interfaces and then udev itself */
1291 if (udev
->actconfig
) {
1292 n
= udev
->actconfig
->desc
.bNumInterfaces
;
1293 for (i
= n
- 1; i
>= 0; --i
) {
1294 intf
= udev
->actconfig
->interface
[i
];
1295 status
= usb_suspend_interface(udev
, intf
, msg
);
1297 /* Ignore errors during system sleep transitions */
1298 if (!PMSG_IS_AUTO(msg
))
1305 status
= usb_suspend_device(udev
, msg
);
1308 * Ignore errors from non-root-hub devices during
1309 * system sleep transitions. For the most part,
1310 * these devices should go to low power anyway when
1311 * the entire bus is suspended.
1313 if (udev
->parent
&& !PMSG_IS_AUTO(msg
))
1317 /* If the suspend failed, resume interfaces that did get suspended */
1319 if (udev
->actconfig
) {
1320 msg
.event
^= (PM_EVENT_SUSPEND
| PM_EVENT_RESUME
);
1322 intf
= udev
->actconfig
->interface
[i
];
1323 usb_resume_interface(udev
, intf
, msg
, 0);
1327 /* If the suspend succeeded then prevent any more URB submissions
1328 * and flush any outstanding URBs.
1331 udev
->can_submit
= 0;
1332 for (i
= 0; i
< 16; ++i
) {
1333 usb_hcd_flush_endpoint(udev
, udev
->ep_out
[i
]);
1334 usb_hcd_flush_endpoint(udev
, udev
->ep_in
[i
]);
1339 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1344 * usb_resume_both - resume a USB device and its interfaces
1345 * @udev: the usb_device to resume
1346 * @msg: Power Management message describing this state transition
1348 * This is the central routine for resuming USB devices. It calls the
1349 * the resume method for @udev and then calls the resume methods for all
1350 * the interface drivers in @udev.
1352 * Autoresume requests originating from a child device or an interface
1353 * driver may be made without the protection of @udev's device lock, but
1354 * all other resume calls will hold the lock. Usbcore will insure that
1355 * method calls do not arrive during bind, unbind, or reset operations.
1356 * However drivers must be prepared to handle resume calls arriving at
1357 * unpredictable times.
1359 * This routine can run only in process context.
1361 * Return: 0 on success.
1363 static int usb_resume_both(struct usb_device
*udev
, pm_message_t msg
)
1367 struct usb_interface
*intf
;
1369 if (udev
->state
== USB_STATE_NOTATTACHED
) {
1373 udev
->can_submit
= 1;
1375 /* Resume the device */
1376 if (udev
->state
== USB_STATE_SUSPENDED
|| udev
->reset_resume
)
1377 status
= usb_resume_device(udev
, msg
);
1379 /* Resume the interfaces */
1380 if (status
== 0 && udev
->actconfig
) {
1381 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1382 intf
= udev
->actconfig
->interface
[i
];
1383 usb_resume_interface(udev
, intf
, msg
,
1384 udev
->reset_resume
);
1387 usb_mark_last_busy(udev
);
1390 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1392 udev
->reset_resume
= 0;
1396 static void choose_wakeup(struct usb_device
*udev
, pm_message_t msg
)
1400 /* Remote wakeup is needed only when we actually go to sleep.
1401 * For things like FREEZE and QUIESCE, if the device is already
1402 * autosuspended then its current wakeup setting is okay.
1404 if (msg
.event
== PM_EVENT_FREEZE
|| msg
.event
== PM_EVENT_QUIESCE
) {
1405 if (udev
->state
!= USB_STATE_SUSPENDED
)
1406 udev
->do_remote_wakeup
= 0;
1410 /* Enable remote wakeup if it is allowed, even if no interface drivers
1413 w
= device_may_wakeup(&udev
->dev
);
1415 /* If the device is autosuspended with the wrong wakeup setting,
1416 * autoresume now so the setting can be changed.
1418 if (udev
->state
== USB_STATE_SUSPENDED
&& w
!= udev
->do_remote_wakeup
)
1419 pm_runtime_resume(&udev
->dev
);
1420 udev
->do_remote_wakeup
= w
;
1423 /* The device lock is held by the PM core */
1424 int usb_suspend(struct device
*dev
, pm_message_t msg
)
1426 struct usb_device
*udev
= to_usb_device(dev
);
1428 unbind_no_pm_drivers_interfaces(udev
);
1430 /* From now on we are sure all drivers support suspend/resume
1431 * but not necessarily reset_resume()
1432 * so we may still need to unbind and rebind upon resume
1434 choose_wakeup(udev
, msg
);
1435 return usb_suspend_both(udev
, msg
);
1438 /* The device lock is held by the PM core */
1439 int usb_resume_complete(struct device
*dev
)
1441 struct usb_device
*udev
= to_usb_device(dev
);
1443 /* For PM complete calls, all we do is rebind interfaces
1444 * whose needs_binding flag is set
1446 if (udev
->state
!= USB_STATE_NOTATTACHED
)
1447 rebind_marked_interfaces(udev
);
1451 /* The device lock is held by the PM core */
1452 int usb_resume(struct device
*dev
, pm_message_t msg
)
1454 struct usb_device
*udev
= to_usb_device(dev
);
1457 /* For all calls, take the device back to full power and
1458 * tell the PM core in case it was autosuspended previously.
1459 * Unbind the interfaces that will need rebinding later,
1460 * because they fail to support reset_resume.
1461 * (This can't be done in usb_resume_interface()
1462 * above because it doesn't own the right set of locks.)
1464 status
= usb_resume_both(udev
, msg
);
1466 pm_runtime_disable(dev
);
1467 pm_runtime_set_active(dev
);
1468 pm_runtime_enable(dev
);
1469 unbind_marked_interfaces(udev
);
1472 /* Avoid PM error messages for devices disconnected while suspended
1473 * as we'll display regular disconnect messages just a bit later.
1475 if (status
== -ENODEV
|| status
== -ESHUTDOWN
)
1481 * usb_enable_autosuspend - allow a USB device to be autosuspended
1482 * @udev: the USB device which may be autosuspended
1484 * This routine allows @udev to be autosuspended. An autosuspend won't
1485 * take place until the autosuspend_delay has elapsed and all the other
1486 * necessary conditions are satisfied.
1488 * The caller must hold @udev's device lock.
1490 void usb_enable_autosuspend(struct usb_device
*udev
)
1492 pm_runtime_allow(&udev
->dev
);
1494 EXPORT_SYMBOL_GPL(usb_enable_autosuspend
);
1497 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1498 * @udev: the USB device which may not be autosuspended
1500 * This routine prevents @udev from being autosuspended and wakes it up
1501 * if it is already autosuspended.
1503 * The caller must hold @udev's device lock.
1505 void usb_disable_autosuspend(struct usb_device
*udev
)
1507 pm_runtime_forbid(&udev
->dev
);
1509 EXPORT_SYMBOL_GPL(usb_disable_autosuspend
);
1512 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1513 * @udev: the usb_device to autosuspend
1515 * This routine should be called when a core subsystem is finished using
1516 * @udev and wants to allow it to autosuspend. Examples would be when
1517 * @udev's device file in usbfs is closed or after a configuration change.
1519 * @udev's usage counter is decremented; if it drops to 0 and all the
1520 * interfaces are inactive then a delayed autosuspend will be attempted.
1521 * The attempt may fail (see autosuspend_check()).
1523 * The caller must hold @udev's device lock.
1525 * This routine can run only in process context.
1527 void usb_autosuspend_device(struct usb_device
*udev
)
1531 usb_mark_last_busy(udev
);
1532 status
= pm_runtime_put_sync_autosuspend(&udev
->dev
);
1533 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1534 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1539 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1540 * @udev: the usb_device to autoresume
1542 * This routine should be called when a core subsystem wants to use @udev
1543 * and needs to guarantee that it is not suspended. No autosuspend will
1544 * occur until usb_autosuspend_device() is called. (Note that this will
1545 * not prevent suspend events originating in the PM core.) Examples would
1546 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1547 * request is received.
1549 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1550 * However if the autoresume fails then the usage counter is re-decremented.
1552 * The caller must hold @udev's device lock.
1554 * This routine can run only in process context.
1556 * Return: 0 on success. A negative error code otherwise.
1558 int usb_autoresume_device(struct usb_device
*udev
)
1562 status
= pm_runtime_get_sync(&udev
->dev
);
1564 pm_runtime_put_sync(&udev
->dev
);
1565 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1566 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1574 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1575 * @intf: the usb_interface whose counter should be decremented
1577 * This routine should be called by an interface driver when it is
1578 * finished using @intf and wants to allow it to autosuspend. A typical
1579 * example would be a character-device driver when its device file is
1582 * The routine decrements @intf's usage counter. When the counter reaches
1583 * 0, a delayed autosuspend request for @intf's device is attempted. The
1584 * attempt may fail (see autosuspend_check()).
1586 * This routine can run only in process context.
1588 void usb_autopm_put_interface(struct usb_interface
*intf
)
1590 struct usb_device
*udev
= interface_to_usbdev(intf
);
1593 usb_mark_last_busy(udev
);
1594 atomic_dec(&intf
->pm_usage_cnt
);
1595 status
= pm_runtime_put_sync(&intf
->dev
);
1596 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1597 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1600 EXPORT_SYMBOL_GPL(usb_autopm_put_interface
);
1603 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1604 * @intf: the usb_interface whose counter should be decremented
1606 * This routine does much the same thing as usb_autopm_put_interface():
1607 * It decrements @intf's usage counter and schedules a delayed
1608 * autosuspend request if the counter is <= 0. The difference is that it
1609 * does not perform any synchronization; callers should hold a private
1610 * lock and handle all synchronization issues themselves.
1612 * Typically a driver would call this routine during an URB's completion
1613 * handler, if no more URBs were pending.
1615 * This routine can run in atomic context.
1617 void usb_autopm_put_interface_async(struct usb_interface
*intf
)
1619 struct usb_device
*udev
= interface_to_usbdev(intf
);
1622 usb_mark_last_busy(udev
);
1623 atomic_dec(&intf
->pm_usage_cnt
);
1624 status
= pm_runtime_put(&intf
->dev
);
1625 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1626 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1629 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async
);
1632 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1633 * @intf: the usb_interface whose counter should be decremented
1635 * This routine decrements @intf's usage counter but does not carry out an
1638 * This routine can run in atomic context.
1640 void usb_autopm_put_interface_no_suspend(struct usb_interface
*intf
)
1642 struct usb_device
*udev
= interface_to_usbdev(intf
);
1644 usb_mark_last_busy(udev
);
1645 atomic_dec(&intf
->pm_usage_cnt
);
1646 pm_runtime_put_noidle(&intf
->dev
);
1648 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend
);
1651 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1652 * @intf: the usb_interface whose counter should be incremented
1654 * This routine should be called by an interface driver when it wants to
1655 * use @intf and needs to guarantee that it is not suspended. In addition,
1656 * the routine prevents @intf from being autosuspended subsequently. (Note
1657 * that this will not prevent suspend events originating in the PM core.)
1658 * This prevention will persist until usb_autopm_put_interface() is called
1659 * or @intf is unbound. A typical example would be a character-device
1660 * driver when its device file is opened.
1662 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1663 * However if the autoresume fails then the counter is re-decremented.
1665 * This routine can run only in process context.
1667 * Return: 0 on success.
1669 int usb_autopm_get_interface(struct usb_interface
*intf
)
1673 status
= pm_runtime_get_sync(&intf
->dev
);
1675 pm_runtime_put_sync(&intf
->dev
);
1677 atomic_inc(&intf
->pm_usage_cnt
);
1678 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1679 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1685 EXPORT_SYMBOL_GPL(usb_autopm_get_interface
);
1688 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1689 * @intf: the usb_interface whose counter should be incremented
1691 * This routine does much the same thing as
1692 * usb_autopm_get_interface(): It increments @intf's usage counter and
1693 * queues an autoresume request if the device is suspended. The
1694 * differences are that it does not perform any synchronization (callers
1695 * should hold a private lock and handle all synchronization issues
1696 * themselves), and it does not autoresume the device directly (it only
1697 * queues a request). After a successful call, the device may not yet be
1700 * This routine can run in atomic context.
1702 * Return: 0 on success. A negative error code otherwise.
1704 int usb_autopm_get_interface_async(struct usb_interface
*intf
)
1708 status
= pm_runtime_get(&intf
->dev
);
1709 if (status
< 0 && status
!= -EINPROGRESS
)
1710 pm_runtime_put_noidle(&intf
->dev
);
1712 atomic_inc(&intf
->pm_usage_cnt
);
1713 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1714 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1716 if (status
> 0 || status
== -EINPROGRESS
)
1720 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async
);
1723 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1724 * @intf: the usb_interface whose counter should be incremented
1726 * This routine increments @intf's usage counter but does not carry out an
1729 * This routine can run in atomic context.
1731 void usb_autopm_get_interface_no_resume(struct usb_interface
*intf
)
1733 struct usb_device
*udev
= interface_to_usbdev(intf
);
1735 usb_mark_last_busy(udev
);
1736 atomic_inc(&intf
->pm_usage_cnt
);
1737 pm_runtime_get_noresume(&intf
->dev
);
1739 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume
);
1741 /* Internal routine to check whether we may autosuspend a device. */
1742 static int autosuspend_check(struct usb_device
*udev
)
1745 struct usb_interface
*intf
;
1747 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1748 * any interface drivers require remote wakeup but it isn't available.
1751 if (udev
->actconfig
) {
1752 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1753 intf
= udev
->actconfig
->interface
[i
];
1755 /* We don't need to check interfaces that are
1756 * disabled for runtime PM. Either they are unbound
1757 * or else their drivers don't support autosuspend
1758 * and so they are permanently active.
1760 if (intf
->dev
.power
.disable_depth
)
1762 if (atomic_read(&intf
->dev
.power
.usage_count
) > 0)
1764 w
|= intf
->needs_remote_wakeup
;
1766 /* Don't allow autosuspend if the device will need
1767 * a reset-resume and any of its interface drivers
1768 * doesn't include support or needs remote wakeup.
1770 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
) {
1771 struct usb_driver
*driver
;
1773 driver
= to_usb_driver(intf
->dev
.driver
);
1774 if (!driver
->reset_resume
||
1775 intf
->needs_remote_wakeup
)
1780 if (w
&& !device_can_wakeup(&udev
->dev
)) {
1781 dev_dbg(&udev
->dev
, "remote wakeup needed for autosuspend\n");
1786 * If the device is a direct child of the root hub and the HCD
1787 * doesn't handle wakeup requests, don't allow autosuspend when
1790 if (w
&& udev
->parent
== udev
->bus
->root_hub
&&
1791 bus_to_hcd(udev
->bus
)->cant_recv_wakeups
) {
1792 dev_dbg(&udev
->dev
, "HCD doesn't handle wakeup requests\n");
1796 udev
->do_remote_wakeup
= w
;
1800 int usb_runtime_suspend(struct device
*dev
)
1802 struct usb_device
*udev
= to_usb_device(dev
);
1805 /* A USB device can be suspended if it passes the various autosuspend
1806 * checks. Runtime suspend for a USB device means suspending all the
1807 * interfaces and then the device itself.
1809 if (autosuspend_check(udev
) != 0)
1812 status
= usb_suspend_both(udev
, PMSG_AUTO_SUSPEND
);
1814 /* Allow a retry if autosuspend failed temporarily */
1815 if (status
== -EAGAIN
|| status
== -EBUSY
)
1816 usb_mark_last_busy(udev
);
1819 * The PM core reacts badly unless the return code is 0,
1820 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1821 * (except for root hubs, because they don't suspend through
1822 * an upstream port like other USB devices).
1824 if (status
!= 0 && udev
->parent
)
1829 int usb_runtime_resume(struct device
*dev
)
1831 struct usb_device
*udev
= to_usb_device(dev
);
1834 /* Runtime resume for a USB device means resuming both the device
1835 * and all its interfaces.
1837 status
= usb_resume_both(udev
, PMSG_AUTO_RESUME
);
1841 int usb_runtime_idle(struct device
*dev
)
1843 struct usb_device
*udev
= to_usb_device(dev
);
1845 /* An idle USB device can be suspended if it passes the various
1846 * autosuspend checks.
1848 if (autosuspend_check(udev
) == 0)
1849 pm_runtime_autosuspend(dev
);
1850 /* Tell the core not to suspend it, though. */
1854 int usb_set_usb2_hardware_lpm(struct usb_device
*udev
, int enable
)
1856 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1859 if (enable
&& !udev
->usb2_hw_lpm_allowed
)
1862 if (hcd
->driver
->set_usb2_hw_lpm
) {
1863 ret
= hcd
->driver
->set_usb2_hw_lpm(hcd
, udev
, enable
);
1865 udev
->usb2_hw_lpm_enabled
= enable
;
1871 #endif /* CONFIG_PM */
1873 struct bus_type usb_bus_type
= {
1875 .match
= usb_device_match
,
1876 .uevent
= usb_uevent
,