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 struct device_driver
*driver
,
41 const char *buf
, size_t count
)
43 struct usb_dynid
*dynid
;
46 unsigned int bInterfaceClass
= 0;
50 fields
= sscanf(buf
, "%x %x %x", &idVendor
, &idProduct
,
55 dynid
= kzalloc(sizeof(*dynid
), GFP_KERNEL
);
59 INIT_LIST_HEAD(&dynid
->node
);
60 dynid
->id
.idVendor
= idVendor
;
61 dynid
->id
.idProduct
= idProduct
;
62 dynid
->id
.match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
64 dynid
->id
.bInterfaceClass
= (u8
)bInterfaceClass
;
65 dynid
->id
.match_flags
|= USB_DEVICE_ID_MATCH_INT_CLASS
;
68 spin_lock(&dynids
->lock
);
69 list_add_tail(&dynid
->node
, &dynids
->list
);
70 spin_unlock(&dynids
->lock
);
72 retval
= driver_attach(driver
);
78 EXPORT_SYMBOL_GPL(usb_store_new_id
);
80 ssize_t
usb_show_dynids(struct usb_dynids
*dynids
, char *buf
)
82 struct usb_dynid
*dynid
;
85 list_for_each_entry(dynid
, &dynids
->list
, node
)
86 if (dynid
->id
.bInterfaceClass
!= 0)
87 count
+= scnprintf(&buf
[count
], PAGE_SIZE
- count
, "%04x %04x %02x\n",
88 dynid
->id
.idVendor
, dynid
->id
.idProduct
,
89 dynid
->id
.bInterfaceClass
);
91 count
+= scnprintf(&buf
[count
], PAGE_SIZE
- count
, "%04x %04x\n",
92 dynid
->id
.idVendor
, dynid
->id
.idProduct
);
95 EXPORT_SYMBOL_GPL(usb_show_dynids
);
97 static ssize_t
new_id_show(struct device_driver
*driver
, char *buf
)
99 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
101 return usb_show_dynids(&usb_drv
->dynids
, buf
);
104 static ssize_t
new_id_store(struct device_driver
*driver
,
105 const char *buf
, size_t count
)
107 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
109 return usb_store_new_id(&usb_drv
->dynids
, driver
, buf
, count
);
111 static DRIVER_ATTR_RW(new_id
);
114 * Remove a USB device ID from this driver
116 static ssize_t
remove_id_store(struct device_driver
*driver
, const char *buf
,
119 struct usb_dynid
*dynid
, *n
;
120 struct usb_driver
*usb_driver
= to_usb_driver(driver
);
125 fields
= sscanf(buf
, "%x %x", &idVendor
, &idProduct
);
129 spin_lock(&usb_driver
->dynids
.lock
);
130 list_for_each_entry_safe(dynid
, n
, &usb_driver
->dynids
.list
, node
) {
131 struct usb_device_id
*id
= &dynid
->id
;
132 if ((id
->idVendor
== idVendor
) &&
133 (id
->idProduct
== idProduct
)) {
134 list_del(&dynid
->node
);
139 spin_unlock(&usb_driver
->dynids
.lock
);
143 static ssize_t
remove_id_show(struct device_driver
*driver
, char *buf
)
145 return new_id_show(driver
, buf
);
147 static DRIVER_ATTR_RW(remove_id
);
149 static int usb_create_newid_files(struct usb_driver
*usb_drv
)
153 if (usb_drv
->no_dynamic_id
)
156 if (usb_drv
->probe
!= NULL
) {
157 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
158 &driver_attr_new_id
);
160 error
= driver_create_file(&usb_drv
->drvwrap
.driver
,
161 &driver_attr_remove_id
);
163 driver_remove_file(&usb_drv
->drvwrap
.driver
,
164 &driver_attr_new_id
);
171 static void usb_remove_newid_files(struct usb_driver
*usb_drv
)
173 if (usb_drv
->no_dynamic_id
)
176 if (usb_drv
->probe
!= NULL
) {
177 driver_remove_file(&usb_drv
->drvwrap
.driver
,
178 &driver_attr_remove_id
);
179 driver_remove_file(&usb_drv
->drvwrap
.driver
,
180 &driver_attr_new_id
);
184 static void usb_free_dynids(struct usb_driver
*usb_drv
)
186 struct usb_dynid
*dynid
, *n
;
188 spin_lock(&usb_drv
->dynids
.lock
);
189 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
190 list_del(&dynid
->node
);
193 spin_unlock(&usb_drv
->dynids
.lock
);
196 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
197 struct usb_driver
*drv
)
199 struct usb_dynid
*dynid
;
201 spin_lock(&drv
->dynids
.lock
);
202 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
203 if (usb_match_one_id(intf
, &dynid
->id
)) {
204 spin_unlock(&drv
->dynids
.lock
);
208 spin_unlock(&drv
->dynids
.lock
);
213 /* called from driver core with dev locked */
214 static int usb_probe_device(struct device
*dev
)
216 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
217 struct usb_device
*udev
= to_usb_device(dev
);
220 dev_dbg(dev
, "%s\n", __func__
);
222 /* TODO: Add real matching code */
224 /* The device should always appear to be in use
225 * unless the driver supports autosuspend.
227 if (!udriver
->supports_autosuspend
)
228 error
= usb_autoresume_device(udev
);
231 error
= udriver
->probe(udev
);
235 /* called from driver core with dev locked */
236 static int usb_unbind_device(struct device
*dev
)
238 struct usb_device
*udev
= to_usb_device(dev
);
239 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
241 udriver
->disconnect(udev
);
242 if (!udriver
->supports_autosuspend
)
243 usb_autosuspend_device(udev
);
248 * Cancel any pending scheduled resets
250 * [see usb_queue_reset_device()]
252 * Called after unconfiguring / when releasing interfaces. See
253 * comments in __usb_queue_reset_device() regarding
254 * udev->reset_running.
256 static void usb_cancel_queued_reset(struct usb_interface
*iface
)
258 if (iface
->reset_running
== 0)
259 cancel_work_sync(&iface
->reset_ws
);
262 /* called from driver core with dev locked */
263 static int usb_probe_interface(struct device
*dev
)
265 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
266 struct usb_interface
*intf
= to_usb_interface(dev
);
267 struct usb_device
*udev
= interface_to_usbdev(intf
);
268 const struct usb_device_id
*id
;
270 int lpm_disable_error
;
272 dev_dbg(dev
, "%s\n", __func__
);
274 intf
->needs_binding
= 0;
276 if (usb_device_is_owned(udev
))
279 if (udev
->authorized
== 0) {
280 dev_err(&intf
->dev
, "Device is not authorized for usage\n");
284 id
= usb_match_id(intf
, driver
->id_table
);
286 id
= usb_match_dynamic_id(intf
, driver
);
290 dev_dbg(dev
, "%s - got id\n", __func__
);
292 error
= usb_autoresume_device(udev
);
296 intf
->condition
= USB_INTERFACE_BINDING
;
298 /* Probed interfaces are initially active. They are
299 * runtime-PM-enabled only if the driver has autosuspend support.
300 * They are sensitive to their children's power states.
302 pm_runtime_set_active(dev
);
303 pm_suspend_ignore_children(dev
, false);
304 if (driver
->supports_autosuspend
)
305 pm_runtime_enable(dev
);
307 /* If the new driver doesn't allow hub-initiated LPM, and we can't
308 * disable hub-initiated LPM, then fail the probe.
310 * Otherwise, leaving LPM enabled should be harmless, because the
311 * endpoint intervals should remain the same, and the U1/U2 timeouts
312 * should remain the same.
314 * If we need to install alt setting 0 before probe, or another alt
315 * setting during probe, that should also be fine. usb_set_interface()
316 * will attempt to disable LPM, and fail if it can't disable it.
318 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
319 if (lpm_disable_error
&& driver
->disable_hub_initiated_lpm
) {
320 dev_err(&intf
->dev
, "%s Failed to disable LPM for driver %s\n.",
321 __func__
, driver
->name
);
322 error
= lpm_disable_error
;
326 /* Carry out a deferred switch to altsetting 0 */
327 if (intf
->needs_altsetting0
) {
328 error
= usb_set_interface(udev
, intf
->altsetting
[0].
329 desc
.bInterfaceNumber
, 0);
332 intf
->needs_altsetting0
= 0;
335 error
= driver
->probe(intf
, id
);
339 intf
->condition
= USB_INTERFACE_BOUND
;
341 /* If the LPM disable succeeded, balance the ref counts. */
342 if (!lpm_disable_error
)
343 usb_unlocked_enable_lpm(udev
);
345 usb_autosuspend_device(udev
);
349 usb_set_intfdata(intf
, NULL
);
350 intf
->needs_remote_wakeup
= 0;
351 intf
->condition
= USB_INTERFACE_UNBOUND
;
352 usb_cancel_queued_reset(intf
);
354 /* If the LPM disable succeeded, balance the ref counts. */
355 if (!lpm_disable_error
)
356 usb_unlocked_enable_lpm(udev
);
358 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
359 if (driver
->supports_autosuspend
)
360 pm_runtime_disable(dev
);
361 pm_runtime_set_suspended(dev
);
363 usb_autosuspend_device(udev
);
367 /* called from driver core with dev locked */
368 static int usb_unbind_interface(struct device
*dev
)
370 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
371 struct usb_interface
*intf
= to_usb_interface(dev
);
372 struct usb_device
*udev
;
373 int error
, r
, lpm_disable_error
;
375 intf
->condition
= USB_INTERFACE_UNBINDING
;
377 /* Autoresume for set_interface call below */
378 udev
= interface_to_usbdev(intf
);
379 error
= usb_autoresume_device(udev
);
381 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
382 * the driver is unbound. If LPM isn't disabled, that's fine because it
383 * wouldn't be enabled unless all the bound interfaces supported
386 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
388 /* Terminate all URBs for this interface unless the driver
389 * supports "soft" unbinding.
391 if (!driver
->soft_unbind
)
392 usb_disable_interface(udev
, intf
, false);
394 driver
->disconnect(intf
);
395 usb_cancel_queued_reset(intf
);
397 /* Reset other interface state.
398 * We cannot do a Set-Interface if the device is suspended or
399 * if it is prepared for a system sleep (since installing a new
400 * altsetting means creating new endpoint device entries).
401 * When either of these happens, defer the Set-Interface.
403 if (intf
->cur_altsetting
->desc
.bAlternateSetting
== 0) {
404 /* Already in altsetting 0 so skip Set-Interface.
405 * Just re-enable it without affecting the endpoint toggles.
407 usb_enable_interface(udev
, intf
, false);
408 } else if (!error
&& !intf
->dev
.power
.is_prepared
) {
409 r
= usb_set_interface(udev
, intf
->altsetting
[0].
410 desc
.bInterfaceNumber
, 0);
412 intf
->needs_altsetting0
= 1;
414 intf
->needs_altsetting0
= 1;
416 usb_set_intfdata(intf
, NULL
);
418 intf
->condition
= USB_INTERFACE_UNBOUND
;
419 intf
->needs_remote_wakeup
= 0;
421 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
422 if (!lpm_disable_error
)
423 usb_unlocked_enable_lpm(udev
);
425 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
426 if (driver
->supports_autosuspend
)
427 pm_runtime_disable(dev
);
428 pm_runtime_set_suspended(dev
);
430 /* Undo any residual pm_autopm_get_interface_* calls */
431 for (r
= atomic_read(&intf
->pm_usage_cnt
); r
> 0; --r
)
432 usb_autopm_put_interface_no_suspend(intf
);
433 atomic_set(&intf
->pm_usage_cnt
, 0);
436 usb_autosuspend_device(udev
);
442 * usb_driver_claim_interface - bind a driver to an interface
443 * @driver: the driver to be bound
444 * @iface: the interface to which it will be bound; must be in the
445 * usb device's active configuration
446 * @priv: driver data associated with that interface
448 * This is used by usb device drivers that need to claim more than one
449 * interface on a device when probing (audio and acm are current examples).
450 * No device driver should directly modify internal usb_interface or
451 * usb_device structure members.
453 * Few drivers should need to use this routine, since the most natural
454 * way to bind to an interface is to return the private data from
455 * the driver's probe() method.
457 * Callers must own the device lock, so driver probe() entries don't need
458 * extra locking, but other call contexts may need to explicitly claim that
461 * Return: 0 on success.
463 int usb_driver_claim_interface(struct usb_driver
*driver
,
464 struct usb_interface
*iface
, void *priv
)
466 struct device
*dev
= &iface
->dev
;
467 struct usb_device
*udev
;
469 int lpm_disable_error
;
474 udev
= interface_to_usbdev(iface
);
476 dev
->driver
= &driver
->drvwrap
.driver
;
477 usb_set_intfdata(iface
, priv
);
478 iface
->needs_binding
= 0;
480 iface
->condition
= USB_INTERFACE_BOUND
;
482 /* Disable LPM until this driver is bound. */
483 lpm_disable_error
= usb_unlocked_disable_lpm(udev
);
484 if (lpm_disable_error
&& driver
->disable_hub_initiated_lpm
) {
485 dev_err(&iface
->dev
, "%s Failed to disable LPM for driver %s\n.",
486 __func__
, driver
->name
);
490 /* Claimed interfaces are initially inactive (suspended) and
491 * runtime-PM-enabled, but only if the driver has autosuspend
492 * support. Otherwise they are marked active, to prevent the
493 * device from being autosuspended, but left disabled. In either
494 * case they are sensitive to their children's power states.
496 pm_suspend_ignore_children(dev
, false);
497 if (driver
->supports_autosuspend
)
498 pm_runtime_enable(dev
);
500 pm_runtime_set_active(dev
);
502 /* if interface was already added, bind now; else let
503 * the future device_add() bind it, bypassing probe()
505 if (device_is_registered(dev
))
506 retval
= device_bind_driver(dev
);
508 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
509 if (!lpm_disable_error
)
510 usb_unlocked_enable_lpm(udev
);
514 EXPORT_SYMBOL_GPL(usb_driver_claim_interface
);
517 * usb_driver_release_interface - unbind a driver from an interface
518 * @driver: the driver to be unbound
519 * @iface: the interface from which it will be unbound
521 * This can be used by drivers to release an interface without waiting
522 * for their disconnect() methods to be called. In typical cases this
523 * also causes the driver disconnect() method to be called.
525 * This call is synchronous, and may not be used in an interrupt context.
526 * Callers must own the device lock, so driver disconnect() entries don't
527 * need extra locking, but other call contexts may need to explicitly claim
530 void usb_driver_release_interface(struct usb_driver
*driver
,
531 struct usb_interface
*iface
)
533 struct device
*dev
= &iface
->dev
;
535 /* this should never happen, don't release something that's not ours */
536 if (!dev
->driver
|| dev
->driver
!= &driver
->drvwrap
.driver
)
539 /* don't release from within disconnect() */
540 if (iface
->condition
!= USB_INTERFACE_BOUND
)
542 iface
->condition
= USB_INTERFACE_UNBINDING
;
544 /* Release via the driver core only if the interface
545 * has already been registered
547 if (device_is_registered(dev
)) {
548 device_release_driver(dev
);
551 usb_unbind_interface(dev
);
556 EXPORT_SYMBOL_GPL(usb_driver_release_interface
);
558 /* returns 0 if no match, 1 if match */
559 int usb_match_device(struct usb_device
*dev
, const struct usb_device_id
*id
)
561 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
562 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
565 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
566 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
569 /* No need to test id->bcdDevice_lo != 0, since 0 is never
570 greater than any unsigned number. */
571 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
572 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
575 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
576 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
579 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
580 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
583 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
584 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
587 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
588 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
594 /* returns 0 if no match, 1 if match */
595 int usb_match_one_id_intf(struct usb_device
*dev
,
596 struct usb_host_interface
*intf
,
597 const struct usb_device_id
*id
)
599 /* The interface class, subclass, protocol and number should never be
600 * checked for a match if the device class is Vendor Specific,
601 * unless the match record specifies the Vendor ID. */
602 if (dev
->descriptor
.bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
603 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
604 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
605 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
606 USB_DEVICE_ID_MATCH_INT_PROTOCOL
|
607 USB_DEVICE_ID_MATCH_INT_NUMBER
)))
610 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
611 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
614 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
615 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
618 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
619 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
622 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_NUMBER
) &&
623 (id
->bInterfaceNumber
!= intf
->desc
.bInterfaceNumber
))
629 /* returns 0 if no match, 1 if match */
630 int usb_match_one_id(struct usb_interface
*interface
,
631 const struct usb_device_id
*id
)
633 struct usb_host_interface
*intf
;
634 struct usb_device
*dev
;
636 /* proc_connectinfo in devio.c may call us with id == NULL. */
640 intf
= interface
->cur_altsetting
;
641 dev
= interface_to_usbdev(interface
);
643 if (!usb_match_device(dev
, id
))
646 return usb_match_one_id_intf(dev
, intf
, id
);
648 EXPORT_SYMBOL_GPL(usb_match_one_id
);
651 * usb_match_id - find first usb_device_id matching device or interface
652 * @interface: the interface of interest
653 * @id: array of usb_device_id structures, terminated by zero entry
655 * usb_match_id searches an array of usb_device_id's and returns
656 * the first one matching the device or interface, or null.
657 * This is used when binding (or rebinding) a driver to an interface.
658 * Most USB device drivers will use this indirectly, through the usb core,
659 * but some layered driver frameworks use it directly.
660 * These device tables are exported with MODULE_DEVICE_TABLE, through
661 * modutils, to support the driver loading functionality of USB hotplugging.
663 * Return: The first matching usb_device_id, or %NULL.
667 * The "match_flags" element in a usb_device_id controls which
668 * members are used. If the corresponding bit is set, the
669 * value in the device_id must match its corresponding member
670 * in the device or interface descriptor, or else the device_id
673 * "driver_info" is normally used only by device drivers,
674 * but you can create a wildcard "matches anything" usb_device_id
675 * as a driver's "modules.usbmap" entry if you provide an id with
676 * only a nonzero "driver_info" field. If you do this, the USB device
677 * driver's probe() routine should use additional intelligence to
678 * decide whether to bind to the specified interface.
680 * What Makes Good usb_device_id Tables:
682 * The match algorithm is very simple, so that intelligence in
683 * driver selection must come from smart driver id records.
684 * Unless you have good reasons to use another selection policy,
685 * provide match elements only in related groups, and order match
686 * specifiers from specific to general. Use the macros provided
687 * for that purpose if you can.
689 * The most specific match specifiers use device descriptor
690 * data. These are commonly used with product-specific matches;
691 * the USB_DEVICE macro lets you provide vendor and product IDs,
692 * and you can also match against ranges of product revisions.
693 * These are widely used for devices with application or vendor
694 * specific bDeviceClass values.
696 * Matches based on device class/subclass/protocol specifications
697 * are slightly more general; use the USB_DEVICE_INFO macro, or
698 * its siblings. These are used with single-function devices
699 * where bDeviceClass doesn't specify that each interface has
702 * Matches based on interface class/subclass/protocol are the
703 * most general; they let drivers bind to any interface on a
704 * multiple-function device. Use the USB_INTERFACE_INFO
705 * macro, or its siblings, to match class-per-interface style
706 * devices (as recorded in bInterfaceClass).
708 * Note that an entry created by USB_INTERFACE_INFO won't match
709 * any interface if the device class is set to Vendor-Specific.
710 * This is deliberate; according to the USB spec the meanings of
711 * the interface class/subclass/protocol for these devices are also
712 * vendor-specific, and hence matching against a standard product
713 * class wouldn't work anyway. If you really want to use an
714 * interface-based match for such a device, create a match record
715 * that also specifies the vendor ID. (Unforunately there isn't a
716 * standard macro for creating records like this.)
718 * Within those groups, remember that not all combinations are
719 * meaningful. For example, don't give a product version range
720 * without vendor and product IDs; or specify a protocol without
721 * its associated class and subclass.
723 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
724 const struct usb_device_id
*id
)
726 /* proc_connectinfo in devio.c may call us with id == NULL. */
730 /* It is important to check that id->driver_info is nonzero,
731 since an entry that is all zeroes except for a nonzero
732 id->driver_info is the way to create an entry that
733 indicates that the driver want to examine every
734 device and interface. */
735 for (; id
->idVendor
|| id
->idProduct
|| id
->bDeviceClass
||
736 id
->bInterfaceClass
|| id
->driver_info
; id
++) {
737 if (usb_match_one_id(interface
, id
))
743 EXPORT_SYMBOL_GPL(usb_match_id
);
745 static int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
747 /* devices and interfaces are handled separately */
748 if (is_usb_device(dev
)) {
750 /* interface drivers never match devices */
751 if (!is_usb_device_driver(drv
))
754 /* TODO: Add real matching code */
757 } else if (is_usb_interface(dev
)) {
758 struct usb_interface
*intf
;
759 struct usb_driver
*usb_drv
;
760 const struct usb_device_id
*id
;
762 /* device drivers never match interfaces */
763 if (is_usb_device_driver(drv
))
766 intf
= to_usb_interface(dev
);
767 usb_drv
= to_usb_driver(drv
);
769 id
= usb_match_id(intf
, usb_drv
->id_table
);
773 id
= usb_match_dynamic_id(intf
, usb_drv
);
781 static int usb_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
783 struct usb_device
*usb_dev
;
785 if (is_usb_device(dev
)) {
786 usb_dev
= to_usb_device(dev
);
787 } else if (is_usb_interface(dev
)) {
788 struct usb_interface
*intf
= to_usb_interface(dev
);
790 usb_dev
= interface_to_usbdev(intf
);
795 if (usb_dev
->devnum
< 0) {
796 /* driver is often null here; dev_dbg() would oops */
797 pr_debug("usb %s: already deleted?\n", dev_name(dev
));
801 pr_debug("usb %s: bus removed?\n", dev_name(dev
));
805 /* per-device configurations are common */
806 if (add_uevent_var(env
, "PRODUCT=%x/%x/%x",
807 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
808 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
809 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
)))
812 /* class-based driver binding models */
813 if (add_uevent_var(env
, "TYPE=%d/%d/%d",
814 usb_dev
->descriptor
.bDeviceClass
,
815 usb_dev
->descriptor
.bDeviceSubClass
,
816 usb_dev
->descriptor
.bDeviceProtocol
))
823 * usb_register_device_driver - register a USB device (not interface) driver
824 * @new_udriver: USB operations for the device driver
825 * @owner: module owner of this driver.
827 * Registers a USB device driver with the USB core. The list of
828 * unattached devices will be rescanned whenever a new driver is
829 * added, allowing the new driver to attach to any recognized devices.
831 * Return: A negative error code on failure and 0 on success.
833 int usb_register_device_driver(struct usb_device_driver
*new_udriver
,
834 struct module
*owner
)
841 new_udriver
->drvwrap
.for_devices
= 1;
842 new_udriver
->drvwrap
.driver
.name
= (char *) new_udriver
->name
;
843 new_udriver
->drvwrap
.driver
.bus
= &usb_bus_type
;
844 new_udriver
->drvwrap
.driver
.probe
= usb_probe_device
;
845 new_udriver
->drvwrap
.driver
.remove
= usb_unbind_device
;
846 new_udriver
->drvwrap
.driver
.owner
= owner
;
848 retval
= driver_register(&new_udriver
->drvwrap
.driver
);
851 pr_info("%s: registered new device driver %s\n",
852 usbcore_name
, new_udriver
->name
);
854 printk(KERN_ERR
"%s: error %d registering device "
856 usbcore_name
, retval
, new_udriver
->name
);
860 EXPORT_SYMBOL_GPL(usb_register_device_driver
);
863 * usb_deregister_device_driver - unregister a USB device (not interface) driver
864 * @udriver: USB operations of the device driver to unregister
865 * Context: must be able to sleep
867 * Unlinks the specified driver from the internal USB driver list.
869 void usb_deregister_device_driver(struct usb_device_driver
*udriver
)
871 pr_info("%s: deregistering device driver %s\n",
872 usbcore_name
, udriver
->name
);
874 driver_unregister(&udriver
->drvwrap
.driver
);
876 EXPORT_SYMBOL_GPL(usb_deregister_device_driver
);
879 * usb_register_driver - register a USB interface driver
880 * @new_driver: USB operations for the interface driver
881 * @owner: module owner of this driver.
882 * @mod_name: module name string
884 * Registers a USB interface driver with the USB core. The list of
885 * unattached interfaces will be rescanned whenever a new driver is
886 * added, allowing the new driver to attach to any recognized interfaces.
888 * Return: A negative error code on failure and 0 on success.
890 * NOTE: if you want your driver to use the USB major number, you must call
891 * usb_register_dev() to enable that functionality. This function no longer
892 * takes care of that.
894 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
,
895 const char *mod_name
)
902 new_driver
->drvwrap
.for_devices
= 0;
903 new_driver
->drvwrap
.driver
.name
= (char *) new_driver
->name
;
904 new_driver
->drvwrap
.driver
.bus
= &usb_bus_type
;
905 new_driver
->drvwrap
.driver
.probe
= usb_probe_interface
;
906 new_driver
->drvwrap
.driver
.remove
= usb_unbind_interface
;
907 new_driver
->drvwrap
.driver
.owner
= owner
;
908 new_driver
->drvwrap
.driver
.mod_name
= mod_name
;
909 spin_lock_init(&new_driver
->dynids
.lock
);
910 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
912 retval
= driver_register(&new_driver
->drvwrap
.driver
);
916 retval
= usb_create_newid_files(new_driver
);
920 pr_info("%s: registered new interface driver %s\n",
921 usbcore_name
, new_driver
->name
);
927 driver_unregister(&new_driver
->drvwrap
.driver
);
929 printk(KERN_ERR
"%s: error %d registering interface "
931 usbcore_name
, retval
, new_driver
->name
);
934 EXPORT_SYMBOL_GPL(usb_register_driver
);
937 * usb_deregister - unregister a USB interface driver
938 * @driver: USB operations of the interface driver to unregister
939 * Context: must be able to sleep
941 * Unlinks the specified driver from the internal USB driver list.
943 * NOTE: If you called usb_register_dev(), you still need to call
944 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
945 * this * call will no longer do it for you.
947 void usb_deregister(struct usb_driver
*driver
)
949 pr_info("%s: deregistering interface driver %s\n",
950 usbcore_name
, driver
->name
);
952 usb_remove_newid_files(driver
);
953 driver_unregister(&driver
->drvwrap
.driver
);
954 usb_free_dynids(driver
);
956 EXPORT_SYMBOL_GPL(usb_deregister
);
958 /* Forced unbinding of a USB interface driver, either because
959 * it doesn't support pre_reset/post_reset/reset_resume or
960 * because it doesn't support suspend/resume.
962 * The caller must hold @intf's device's lock, but not @intf's lock.
964 void usb_forced_unbind_intf(struct usb_interface
*intf
)
966 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
968 dev_dbg(&intf
->dev
, "forced unbind\n");
969 usb_driver_release_interface(driver
, intf
);
971 /* Mark the interface for later rebinding */
972 intf
->needs_binding
= 1;
976 * Unbind drivers for @udev's marked interfaces. These interfaces have
977 * the needs_binding flag set, for example by usb_resume_interface().
979 * The caller must hold @udev's device lock.
981 static void unbind_marked_interfaces(struct usb_device
*udev
)
983 struct usb_host_config
*config
;
985 struct usb_interface
*intf
;
987 config
= udev
->actconfig
;
989 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
990 intf
= config
->interface
[i
];
991 if (intf
->dev
.driver
&& intf
->needs_binding
)
992 usb_forced_unbind_intf(intf
);
997 /* Delayed forced unbinding of a USB interface driver and scan
1000 * The caller must hold @intf's device's lock, but not @intf's lock.
1002 * Note: Rebinds will be skipped if a system sleep transition is in
1003 * progress and the PM "complete" callback hasn't occurred yet.
1005 static void usb_rebind_intf(struct usb_interface
*intf
)
1009 /* Delayed unbind of an existing driver */
1010 if (intf
->dev
.driver
)
1011 usb_forced_unbind_intf(intf
);
1013 /* Try to rebind the interface */
1014 if (!intf
->dev
.power
.is_prepared
) {
1015 intf
->needs_binding
= 0;
1016 rc
= device_attach(&intf
->dev
);
1018 dev_warn(&intf
->dev
, "rebind failed: %d\n", rc
);
1023 * Rebind drivers to @udev's marked interfaces. These interfaces have
1024 * the needs_binding flag set.
1026 * The caller must hold @udev's device lock.
1028 static void rebind_marked_interfaces(struct usb_device
*udev
)
1030 struct usb_host_config
*config
;
1032 struct usb_interface
*intf
;
1034 config
= udev
->actconfig
;
1036 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1037 intf
= config
->interface
[i
];
1038 if (intf
->needs_binding
)
1039 usb_rebind_intf(intf
);
1045 * Unbind all of @udev's marked interfaces and then rebind all of them.
1046 * This ordering is necessary because some drivers claim several interfaces
1047 * when they are first probed.
1049 * The caller must hold @udev's device lock.
1051 void usb_unbind_and_rebind_marked_interfaces(struct usb_device
*udev
)
1053 unbind_marked_interfaces(udev
);
1054 rebind_marked_interfaces(udev
);
1059 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
1060 * There is no check for reset_resume here because it can be determined
1061 * only during resume whether reset_resume is needed.
1063 * The caller must hold @udev's device lock.
1065 static void unbind_no_pm_drivers_interfaces(struct usb_device
*udev
)
1067 struct usb_host_config
*config
;
1069 struct usb_interface
*intf
;
1070 struct usb_driver
*drv
;
1072 config
= udev
->actconfig
;
1074 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
1075 intf
= config
->interface
[i
];
1077 if (intf
->dev
.driver
) {
1078 drv
= to_usb_driver(intf
->dev
.driver
);
1079 if (!drv
->suspend
|| !drv
->resume
)
1080 usb_forced_unbind_intf(intf
);
1086 static int usb_suspend_device(struct usb_device
*udev
, pm_message_t msg
)
1088 struct usb_device_driver
*udriver
;
1091 if (udev
->state
== USB_STATE_NOTATTACHED
||
1092 udev
->state
== USB_STATE_SUSPENDED
)
1095 /* For devices that don't have a driver, we do a generic suspend. */
1096 if (udev
->dev
.driver
)
1097 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1099 udev
->do_remote_wakeup
= 0;
1100 udriver
= &usb_generic_driver
;
1102 status
= udriver
->suspend(udev
, msg
);
1105 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1109 static int usb_resume_device(struct usb_device
*udev
, pm_message_t msg
)
1111 struct usb_device_driver
*udriver
;
1114 if (udev
->state
== USB_STATE_NOTATTACHED
)
1117 /* Can't resume it if it doesn't have a driver. */
1118 if (udev
->dev
.driver
== NULL
) {
1123 /* Non-root devices on a full/low-speed bus must wait for their
1124 * companion high-speed root hub, in case a handoff is needed.
1126 if (!PMSG_IS_AUTO(msg
) && udev
->parent
&& udev
->bus
->hs_companion
)
1127 device_pm_wait_for_dev(&udev
->dev
,
1128 &udev
->bus
->hs_companion
->root_hub
->dev
);
1130 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
)
1131 udev
->reset_resume
= 1;
1133 udriver
= to_usb_device_driver(udev
->dev
.driver
);
1134 status
= udriver
->resume(udev
, msg
);
1137 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1141 static int usb_suspend_interface(struct usb_device
*udev
,
1142 struct usb_interface
*intf
, pm_message_t msg
)
1144 struct usb_driver
*driver
;
1147 if (udev
->state
== USB_STATE_NOTATTACHED
||
1148 intf
->condition
== USB_INTERFACE_UNBOUND
)
1150 driver
= to_usb_driver(intf
->dev
.driver
);
1152 /* at this time we know the driver supports suspend */
1153 status
= driver
->suspend(intf
, msg
);
1154 if (status
&& !PMSG_IS_AUTO(msg
))
1155 dev_err(&intf
->dev
, "suspend error %d\n", status
);
1158 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1162 static int usb_resume_interface(struct usb_device
*udev
,
1163 struct usb_interface
*intf
, pm_message_t msg
, int reset_resume
)
1165 struct usb_driver
*driver
;
1168 if (udev
->state
== USB_STATE_NOTATTACHED
)
1171 /* Don't let autoresume interfere with unbinding */
1172 if (intf
->condition
== USB_INTERFACE_UNBINDING
)
1175 /* Can't resume it if it doesn't have a driver. */
1176 if (intf
->condition
== USB_INTERFACE_UNBOUND
) {
1178 /* Carry out a deferred switch to altsetting 0 */
1179 if (intf
->needs_altsetting0
&& !intf
->dev
.power
.is_prepared
) {
1180 usb_set_interface(udev
, intf
->altsetting
[0].
1181 desc
.bInterfaceNumber
, 0);
1182 intf
->needs_altsetting0
= 0;
1187 /* Don't resume if the interface is marked for rebinding */
1188 if (intf
->needs_binding
)
1190 driver
= to_usb_driver(intf
->dev
.driver
);
1193 if (driver
->reset_resume
) {
1194 status
= driver
->reset_resume(intf
);
1196 dev_err(&intf
->dev
, "%s error %d\n",
1197 "reset_resume", status
);
1199 intf
->needs_binding
= 1;
1200 dev_warn(&intf
->dev
, "no %s for driver %s?\n",
1201 "reset_resume", driver
->name
);
1204 status
= driver
->resume(intf
);
1206 dev_err(&intf
->dev
, "resume error %d\n", status
);
1210 dev_vdbg(&intf
->dev
, "%s: status %d\n", __func__
, status
);
1212 /* Later we will unbind the driver and/or reprobe, if necessary */
1217 * usb_suspend_both - suspend a USB device and its interfaces
1218 * @udev: the usb_device to suspend
1219 * @msg: Power Management message describing this state transition
1221 * This is the central routine for suspending USB devices. It calls the
1222 * suspend methods for all the interface drivers in @udev and then calls
1223 * the suspend method for @udev itself. When the routine is called in
1224 * autosuspend, if an error occurs at any stage, all the interfaces
1225 * which were suspended are resumed so that they remain in the same
1226 * state as the device, but when called from system sleep, all error
1227 * from suspend methods of interfaces and the non-root-hub device itself
1228 * are simply ignored, so all suspended interfaces are only resumed
1229 * to the device's state when @udev is root-hub and its suspend method
1232 * Autosuspend requests originating from a child device or an interface
1233 * driver may be made without the protection of @udev's device lock, but
1234 * all other suspend calls will hold the lock. Usbcore will insure that
1235 * method calls do not arrive during bind, unbind, or reset operations.
1236 * However drivers must be prepared to handle suspend calls arriving at
1237 * unpredictable times.
1239 * This routine can run only in process context.
1241 * Return: 0 if the suspend succeeded.
1243 static int usb_suspend_both(struct usb_device
*udev
, pm_message_t msg
)
1247 struct usb_interface
*intf
;
1249 if (udev
->state
== USB_STATE_NOTATTACHED
||
1250 udev
->state
== USB_STATE_SUSPENDED
)
1253 /* Suspend all the interfaces and then udev itself */
1254 if (udev
->actconfig
) {
1255 n
= udev
->actconfig
->desc
.bNumInterfaces
;
1256 for (i
= n
- 1; i
>= 0; --i
) {
1257 intf
= udev
->actconfig
->interface
[i
];
1258 status
= usb_suspend_interface(udev
, intf
, msg
);
1260 /* Ignore errors during system sleep transitions */
1261 if (!PMSG_IS_AUTO(msg
))
1268 status
= usb_suspend_device(udev
, msg
);
1271 * Ignore errors from non-root-hub devices during
1272 * system sleep transitions. For the most part,
1273 * these devices should go to low power anyway when
1274 * the entire bus is suspended.
1276 if (udev
->parent
&& !PMSG_IS_AUTO(msg
))
1280 /* If the suspend failed, resume interfaces that did get suspended */
1282 if (udev
->actconfig
) {
1283 msg
.event
^= (PM_EVENT_SUSPEND
| PM_EVENT_RESUME
);
1285 intf
= udev
->actconfig
->interface
[i
];
1286 usb_resume_interface(udev
, intf
, msg
, 0);
1290 /* If the suspend succeeded then prevent any more URB submissions
1291 * and flush any outstanding URBs.
1294 udev
->can_submit
= 0;
1295 for (i
= 0; i
< 16; ++i
) {
1296 usb_hcd_flush_endpoint(udev
, udev
->ep_out
[i
]);
1297 usb_hcd_flush_endpoint(udev
, udev
->ep_in
[i
]);
1302 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1307 * usb_resume_both - resume a USB device and its interfaces
1308 * @udev: the usb_device to resume
1309 * @msg: Power Management message describing this state transition
1311 * This is the central routine for resuming USB devices. It calls the
1312 * the resume method for @udev and then calls the resume methods for all
1313 * the interface drivers in @udev.
1315 * Autoresume requests originating from a child device or an interface
1316 * driver may be made without the protection of @udev's device lock, but
1317 * all other resume calls will hold the lock. Usbcore will insure that
1318 * method calls do not arrive during bind, unbind, or reset operations.
1319 * However drivers must be prepared to handle resume calls arriving at
1320 * unpredictable times.
1322 * This routine can run only in process context.
1324 * Return: 0 on success.
1326 static int usb_resume_both(struct usb_device
*udev
, pm_message_t msg
)
1330 struct usb_interface
*intf
;
1332 if (udev
->state
== USB_STATE_NOTATTACHED
) {
1336 udev
->can_submit
= 1;
1338 /* Resume the device */
1339 if (udev
->state
== USB_STATE_SUSPENDED
|| udev
->reset_resume
)
1340 status
= usb_resume_device(udev
, msg
);
1342 /* Resume the interfaces */
1343 if (status
== 0 && udev
->actconfig
) {
1344 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1345 intf
= udev
->actconfig
->interface
[i
];
1346 usb_resume_interface(udev
, intf
, msg
,
1347 udev
->reset_resume
);
1350 usb_mark_last_busy(udev
);
1353 dev_vdbg(&udev
->dev
, "%s: status %d\n", __func__
, status
);
1355 udev
->reset_resume
= 0;
1359 static void choose_wakeup(struct usb_device
*udev
, pm_message_t msg
)
1363 /* Remote wakeup is needed only when we actually go to sleep.
1364 * For things like FREEZE and QUIESCE, if the device is already
1365 * autosuspended then its current wakeup setting is okay.
1367 if (msg
.event
== PM_EVENT_FREEZE
|| msg
.event
== PM_EVENT_QUIESCE
) {
1368 if (udev
->state
!= USB_STATE_SUSPENDED
)
1369 udev
->do_remote_wakeup
= 0;
1373 /* Enable remote wakeup if it is allowed, even if no interface drivers
1376 w
= device_may_wakeup(&udev
->dev
);
1378 /* If the device is autosuspended with the wrong wakeup setting,
1379 * autoresume now so the setting can be changed.
1381 if (udev
->state
== USB_STATE_SUSPENDED
&& w
!= udev
->do_remote_wakeup
)
1382 pm_runtime_resume(&udev
->dev
);
1383 udev
->do_remote_wakeup
= w
;
1386 /* The device lock is held by the PM core */
1387 int usb_suspend(struct device
*dev
, pm_message_t msg
)
1389 struct usb_device
*udev
= to_usb_device(dev
);
1391 unbind_no_pm_drivers_interfaces(udev
);
1393 /* From now on we are sure all drivers support suspend/resume
1394 * but not necessarily reset_resume()
1395 * so we may still need to unbind and rebind upon resume
1397 choose_wakeup(udev
, msg
);
1398 return usb_suspend_both(udev
, msg
);
1401 /* The device lock is held by the PM core */
1402 int usb_resume_complete(struct device
*dev
)
1404 struct usb_device
*udev
= to_usb_device(dev
);
1406 /* For PM complete calls, all we do is rebind interfaces
1407 * whose needs_binding flag is set
1409 if (udev
->state
!= USB_STATE_NOTATTACHED
)
1410 rebind_marked_interfaces(udev
);
1414 /* The device lock is held by the PM core */
1415 int usb_resume(struct device
*dev
, pm_message_t msg
)
1417 struct usb_device
*udev
= to_usb_device(dev
);
1420 /* For all calls, take the device back to full power and
1421 * tell the PM core in case it was autosuspended previously.
1422 * Unbind the interfaces that will need rebinding later,
1423 * because they fail to support reset_resume.
1424 * (This can't be done in usb_resume_interface()
1425 * above because it doesn't own the right set of locks.)
1427 status
= usb_resume_both(udev
, msg
);
1429 pm_runtime_disable(dev
);
1430 pm_runtime_set_active(dev
);
1431 pm_runtime_enable(dev
);
1432 unbind_marked_interfaces(udev
);
1435 /* Avoid PM error messages for devices disconnected while suspended
1436 * as we'll display regular disconnect messages just a bit later.
1438 if (status
== -ENODEV
|| status
== -ESHUTDOWN
)
1443 #endif /* CONFIG_PM */
1445 #ifdef CONFIG_PM_RUNTIME
1448 * usb_enable_autosuspend - allow a USB device to be autosuspended
1449 * @udev: the USB device which may be autosuspended
1451 * This routine allows @udev to be autosuspended. An autosuspend won't
1452 * take place until the autosuspend_delay has elapsed and all the other
1453 * necessary conditions are satisfied.
1455 * The caller must hold @udev's device lock.
1457 void usb_enable_autosuspend(struct usb_device
*udev
)
1459 pm_runtime_allow(&udev
->dev
);
1461 EXPORT_SYMBOL_GPL(usb_enable_autosuspend
);
1464 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1465 * @udev: the USB device which may not be autosuspended
1467 * This routine prevents @udev from being autosuspended and wakes it up
1468 * if it is already autosuspended.
1470 * The caller must hold @udev's device lock.
1472 void usb_disable_autosuspend(struct usb_device
*udev
)
1474 pm_runtime_forbid(&udev
->dev
);
1476 EXPORT_SYMBOL_GPL(usb_disable_autosuspend
);
1479 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1480 * @udev: the usb_device to autosuspend
1482 * This routine should be called when a core subsystem is finished using
1483 * @udev and wants to allow it to autosuspend. Examples would be when
1484 * @udev's device file in usbfs is closed or after a configuration change.
1486 * @udev's usage counter is decremented; if it drops to 0 and all the
1487 * interfaces are inactive then a delayed autosuspend will be attempted.
1488 * The attempt may fail (see autosuspend_check()).
1490 * The caller must hold @udev's device lock.
1492 * This routine can run only in process context.
1494 void usb_autosuspend_device(struct usb_device
*udev
)
1498 usb_mark_last_busy(udev
);
1499 status
= pm_runtime_put_sync_autosuspend(&udev
->dev
);
1500 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1501 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1506 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1507 * @udev: the usb_device to autoresume
1509 * This routine should be called when a core subsystem wants to use @udev
1510 * and needs to guarantee that it is not suspended. No autosuspend will
1511 * occur until usb_autosuspend_device() is called. (Note that this will
1512 * not prevent suspend events originating in the PM core.) Examples would
1513 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1514 * request is received.
1516 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1517 * However if the autoresume fails then the usage counter is re-decremented.
1519 * The caller must hold @udev's device lock.
1521 * This routine can run only in process context.
1523 * Return: 0 on success. A negative error code otherwise.
1525 int usb_autoresume_device(struct usb_device
*udev
)
1529 status
= pm_runtime_get_sync(&udev
->dev
);
1531 pm_runtime_put_sync(&udev
->dev
);
1532 dev_vdbg(&udev
->dev
, "%s: cnt %d -> %d\n",
1533 __func__
, atomic_read(&udev
->dev
.power
.usage_count
),
1541 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1542 * @intf: the usb_interface whose counter should be decremented
1544 * This routine should be called by an interface driver when it is
1545 * finished using @intf and wants to allow it to autosuspend. A typical
1546 * example would be a character-device driver when its device file is
1549 * The routine decrements @intf's usage counter. When the counter reaches
1550 * 0, a delayed autosuspend request for @intf's device is attempted. The
1551 * attempt may fail (see autosuspend_check()).
1553 * This routine can run only in process context.
1555 void usb_autopm_put_interface(struct usb_interface
*intf
)
1557 struct usb_device
*udev
= interface_to_usbdev(intf
);
1560 usb_mark_last_busy(udev
);
1561 atomic_dec(&intf
->pm_usage_cnt
);
1562 status
= pm_runtime_put_sync(&intf
->dev
);
1563 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1564 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1567 EXPORT_SYMBOL_GPL(usb_autopm_put_interface
);
1570 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1571 * @intf: the usb_interface whose counter should be decremented
1573 * This routine does much the same thing as usb_autopm_put_interface():
1574 * It decrements @intf's usage counter and schedules a delayed
1575 * autosuspend request if the counter is <= 0. The difference is that it
1576 * does not perform any synchronization; callers should hold a private
1577 * lock and handle all synchronization issues themselves.
1579 * Typically a driver would call this routine during an URB's completion
1580 * handler, if no more URBs were pending.
1582 * This routine can run in atomic context.
1584 void usb_autopm_put_interface_async(struct usb_interface
*intf
)
1586 struct usb_device
*udev
= interface_to_usbdev(intf
);
1589 usb_mark_last_busy(udev
);
1590 atomic_dec(&intf
->pm_usage_cnt
);
1591 status
= pm_runtime_put(&intf
->dev
);
1592 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1593 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1596 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async
);
1599 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1600 * @intf: the usb_interface whose counter should be decremented
1602 * This routine decrements @intf's usage counter but does not carry out an
1605 * This routine can run in atomic context.
1607 void usb_autopm_put_interface_no_suspend(struct usb_interface
*intf
)
1609 struct usb_device
*udev
= interface_to_usbdev(intf
);
1611 usb_mark_last_busy(udev
);
1612 atomic_dec(&intf
->pm_usage_cnt
);
1613 pm_runtime_put_noidle(&intf
->dev
);
1615 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend
);
1618 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1619 * @intf: the usb_interface whose counter should be incremented
1621 * This routine should be called by an interface driver when it wants to
1622 * use @intf and needs to guarantee that it is not suspended. In addition,
1623 * the routine prevents @intf from being autosuspended subsequently. (Note
1624 * that this will not prevent suspend events originating in the PM core.)
1625 * This prevention will persist until usb_autopm_put_interface() is called
1626 * or @intf is unbound. A typical example would be a character-device
1627 * driver when its device file is opened.
1629 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1630 * However if the autoresume fails then the counter is re-decremented.
1632 * This routine can run only in process context.
1634 * Return: 0 on success.
1636 int usb_autopm_get_interface(struct usb_interface
*intf
)
1640 status
= pm_runtime_get_sync(&intf
->dev
);
1642 pm_runtime_put_sync(&intf
->dev
);
1644 atomic_inc(&intf
->pm_usage_cnt
);
1645 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1646 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1652 EXPORT_SYMBOL_GPL(usb_autopm_get_interface
);
1655 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1656 * @intf: the usb_interface whose counter should be incremented
1658 * This routine does much the same thing as
1659 * usb_autopm_get_interface(): It increments @intf's usage counter and
1660 * queues an autoresume request if the device is suspended. The
1661 * differences are that it does not perform any synchronization (callers
1662 * should hold a private lock and handle all synchronization issues
1663 * themselves), and it does not autoresume the device directly (it only
1664 * queues a request). After a successful call, the device may not yet be
1667 * This routine can run in atomic context.
1669 * Return: 0 on success. A negative error code otherwise.
1671 int usb_autopm_get_interface_async(struct usb_interface
*intf
)
1675 status
= pm_runtime_get(&intf
->dev
);
1676 if (status
< 0 && status
!= -EINPROGRESS
)
1677 pm_runtime_put_noidle(&intf
->dev
);
1679 atomic_inc(&intf
->pm_usage_cnt
);
1680 dev_vdbg(&intf
->dev
, "%s: cnt %d -> %d\n",
1681 __func__
, atomic_read(&intf
->dev
.power
.usage_count
),
1683 if (status
> 0 || status
== -EINPROGRESS
)
1687 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async
);
1690 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1691 * @intf: the usb_interface whose counter should be incremented
1693 * This routine increments @intf's usage counter but does not carry out an
1696 * This routine can run in atomic context.
1698 void usb_autopm_get_interface_no_resume(struct usb_interface
*intf
)
1700 struct usb_device
*udev
= interface_to_usbdev(intf
);
1702 usb_mark_last_busy(udev
);
1703 atomic_inc(&intf
->pm_usage_cnt
);
1704 pm_runtime_get_noresume(&intf
->dev
);
1706 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume
);
1708 /* Internal routine to check whether we may autosuspend a device. */
1709 static int autosuspend_check(struct usb_device
*udev
)
1712 struct usb_interface
*intf
;
1714 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1715 * any interface drivers require remote wakeup but it isn't available.
1718 if (udev
->actconfig
) {
1719 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1720 intf
= udev
->actconfig
->interface
[i
];
1722 /* We don't need to check interfaces that are
1723 * disabled for runtime PM. Either they are unbound
1724 * or else their drivers don't support autosuspend
1725 * and so they are permanently active.
1727 if (intf
->dev
.power
.disable_depth
)
1729 if (atomic_read(&intf
->dev
.power
.usage_count
) > 0)
1731 w
|= intf
->needs_remote_wakeup
;
1733 /* Don't allow autosuspend if the device will need
1734 * a reset-resume and any of its interface drivers
1735 * doesn't include support or needs remote wakeup.
1737 if (udev
->quirks
& USB_QUIRK_RESET_RESUME
) {
1738 struct usb_driver
*driver
;
1740 driver
= to_usb_driver(intf
->dev
.driver
);
1741 if (!driver
->reset_resume
||
1742 intf
->needs_remote_wakeup
)
1747 if (w
&& !device_can_wakeup(&udev
->dev
)) {
1748 dev_dbg(&udev
->dev
, "remote wakeup needed for autosuspend\n");
1751 udev
->do_remote_wakeup
= w
;
1755 int usb_runtime_suspend(struct device
*dev
)
1757 struct usb_device
*udev
= to_usb_device(dev
);
1760 /* A USB device can be suspended if it passes the various autosuspend
1761 * checks. Runtime suspend for a USB device means suspending all the
1762 * interfaces and then the device itself.
1764 if (autosuspend_check(udev
) != 0)
1767 status
= usb_suspend_both(udev
, PMSG_AUTO_SUSPEND
);
1769 /* Allow a retry if autosuspend failed temporarily */
1770 if (status
== -EAGAIN
|| status
== -EBUSY
)
1771 usb_mark_last_busy(udev
);
1774 * The PM core reacts badly unless the return code is 0,
1775 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1776 * (except for root hubs, because they don't suspend through
1777 * an upstream port like other USB devices).
1779 if (status
!= 0 && udev
->parent
)
1784 int usb_runtime_resume(struct device
*dev
)
1786 struct usb_device
*udev
= to_usb_device(dev
);
1789 /* Runtime resume for a USB device means resuming both the device
1790 * and all its interfaces.
1792 status
= usb_resume_both(udev
, PMSG_AUTO_RESUME
);
1796 int usb_runtime_idle(struct device
*dev
)
1798 struct usb_device
*udev
= to_usb_device(dev
);
1800 /* An idle USB device can be suspended if it passes the various
1801 * autosuspend checks.
1803 if (autosuspend_check(udev
) == 0)
1804 pm_runtime_autosuspend(dev
);
1805 /* Tell the core not to suspend it, though. */
1809 int usb_set_usb2_hardware_lpm(struct usb_device
*udev
, int enable
)
1811 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
1814 if (enable
&& !udev
->usb2_hw_lpm_allowed
)
1817 if (hcd
->driver
->set_usb2_hw_lpm
) {
1818 ret
= hcd
->driver
->set_usb2_hw_lpm(hcd
, udev
, enable
);
1820 udev
->usb2_hw_lpm_enabled
= enable
;
1826 #endif /* CONFIG_PM_RUNTIME */
1828 struct bus_type usb_bus_type
= {
1830 .match
= usb_device_match
,
1831 .uevent
= usb_uevent
,