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/usb.h>
27 #include <linux/workqueue.h>
34 * Adds a new dynamic USBdevice ID to this driver,
35 * and cause the driver to probe for all devices again.
37 ssize_t
usb_store_new_id(struct usb_dynids
*dynids
,
38 struct device_driver
*driver
,
39 const char *buf
, size_t count
)
41 struct usb_dynid
*dynid
;
47 fields
= sscanf(buf
, "%x %x", &idVendor
, &idProduct
);
51 dynid
= kzalloc(sizeof(*dynid
), GFP_KERNEL
);
55 INIT_LIST_HEAD(&dynid
->node
);
56 dynid
->id
.idVendor
= idVendor
;
57 dynid
->id
.idProduct
= idProduct
;
58 dynid
->id
.match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
60 spin_lock(&dynids
->lock
);
61 list_add_tail(&dynids
->list
, &dynid
->node
);
62 spin_unlock(&dynids
->lock
);
64 if (get_driver(driver
)) {
65 retval
= driver_attach(driver
);
73 EXPORT_SYMBOL_GPL(usb_store_new_id
);
75 static ssize_t
store_new_id(struct device_driver
*driver
,
76 const char *buf
, size_t count
)
78 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
80 return usb_store_new_id(&usb_drv
->dynids
, driver
, buf
, count
);
82 static DRIVER_ATTR(new_id
, S_IWUSR
, NULL
, store_new_id
);
84 static int usb_create_newid_file(struct usb_driver
*usb_drv
)
88 if (usb_drv
->no_dynamic_id
)
91 if (usb_drv
->probe
!= NULL
)
92 error
= sysfs_create_file(&usb_drv
->drvwrap
.driver
.kobj
,
93 &driver_attr_new_id
.attr
);
98 static void usb_remove_newid_file(struct usb_driver
*usb_drv
)
100 if (usb_drv
->no_dynamic_id
)
103 if (usb_drv
->probe
!= NULL
)
104 sysfs_remove_file(&usb_drv
->drvwrap
.driver
.kobj
,
105 &driver_attr_new_id
.attr
);
108 static void usb_free_dynids(struct usb_driver
*usb_drv
)
110 struct usb_dynid
*dynid
, *n
;
112 spin_lock(&usb_drv
->dynids
.lock
);
113 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
114 list_del(&dynid
->node
);
117 spin_unlock(&usb_drv
->dynids
.lock
);
120 static inline int usb_create_newid_file(struct usb_driver
*usb_drv
)
125 static void usb_remove_newid_file(struct usb_driver
*usb_drv
)
129 static inline void usb_free_dynids(struct usb_driver
*usb_drv
)
134 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
135 struct usb_driver
*drv
)
137 struct usb_dynid
*dynid
;
139 spin_lock(&drv
->dynids
.lock
);
140 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
141 if (usb_match_one_id(intf
, &dynid
->id
)) {
142 spin_unlock(&drv
->dynids
.lock
);
146 spin_unlock(&drv
->dynids
.lock
);
151 /* called from driver core with dev locked */
152 static int usb_probe_device(struct device
*dev
)
154 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
155 struct usb_device
*udev
;
158 dev_dbg(dev
, "%s\n", __FUNCTION__
);
160 if (!is_usb_device(dev
)) /* Sanity check */
163 udev
= to_usb_device(dev
);
165 /* TODO: Add real matching code */
167 /* The device should always appear to be in use
168 * unless the driver suports autosuspend.
170 udev
->pm_usage_cnt
= !(udriver
->supports_autosuspend
);
172 error
= udriver
->probe(udev
);
176 /* called from driver core with dev locked */
177 static int usb_unbind_device(struct device
*dev
)
179 struct usb_device_driver
*udriver
= to_usb_device_driver(dev
->driver
);
181 udriver
->disconnect(to_usb_device(dev
));
186 /* called from driver core with dev locked */
187 static int usb_probe_interface(struct device
*dev
)
189 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
190 struct usb_interface
*intf
;
191 struct usb_device
*udev
;
192 const struct usb_device_id
*id
;
195 dev_dbg(dev
, "%s\n", __FUNCTION__
);
197 if (is_usb_device(dev
)) /* Sanity check */
200 intf
= to_usb_interface(dev
);
201 udev
= interface_to_usbdev(intf
);
203 id
= usb_match_id(intf
, driver
->id_table
);
205 id
= usb_match_dynamic_id(intf
, driver
);
207 dev_dbg(dev
, "%s - got id\n", __FUNCTION__
);
209 error
= usb_autoresume_device(udev
);
213 /* Interface "power state" doesn't correspond to any hardware
214 * state whatsoever. We use it to record when it's bound to
215 * a driver that may start I/0: it's not frozen/quiesced.
218 intf
->condition
= USB_INTERFACE_BINDING
;
220 /* The interface should always appear to be in use
221 * unless the driver suports autosuspend.
223 intf
->pm_usage_cnt
= !(driver
->supports_autosuspend
);
225 error
= driver
->probe(intf
, id
);
228 intf
->needs_remote_wakeup
= 0;
229 intf
->condition
= USB_INTERFACE_UNBOUND
;
231 intf
->condition
= USB_INTERFACE_BOUND
;
233 usb_autosuspend_device(udev
);
239 /* called from driver core with dev locked */
240 static int usb_unbind_interface(struct device
*dev
)
242 struct usb_driver
*driver
= to_usb_driver(dev
->driver
);
243 struct usb_interface
*intf
= to_usb_interface(dev
);
244 struct usb_device
*udev
;
247 intf
->condition
= USB_INTERFACE_UNBINDING
;
249 /* Autoresume for set_interface call below */
250 udev
= interface_to_usbdev(intf
);
251 error
= usb_autoresume_device(udev
);
253 /* release all urbs for this interface */
254 usb_disable_interface(interface_to_usbdev(intf
), intf
);
256 driver
->disconnect(intf
);
258 /* reset other interface state */
259 usb_set_interface(interface_to_usbdev(intf
),
260 intf
->altsetting
[0].desc
.bInterfaceNumber
,
262 usb_set_intfdata(intf
, NULL
);
264 intf
->condition
= USB_INTERFACE_UNBOUND
;
266 intf
->needs_remote_wakeup
= 0;
269 usb_autosuspend_device(udev
);
275 * usb_driver_claim_interface - bind a driver to an interface
276 * @driver: the driver to be bound
277 * @iface: the interface to which it will be bound; must be in the
278 * usb device's active configuration
279 * @priv: driver data associated with that interface
281 * This is used by usb device drivers that need to claim more than one
282 * interface on a device when probing (audio and acm are current examples).
283 * No device driver should directly modify internal usb_interface or
284 * usb_device structure members.
286 * Few drivers should need to use this routine, since the most natural
287 * way to bind to an interface is to return the private data from
288 * the driver's probe() method.
290 * Callers must own the device lock, so driver probe() entries don't need
291 * extra locking, but other call contexts may need to explicitly claim that
294 int usb_driver_claim_interface(struct usb_driver
*driver
,
295 struct usb_interface
*iface
, void* priv
)
297 struct device
*dev
= &iface
->dev
;
298 struct usb_device
*udev
= interface_to_usbdev(iface
);
304 dev
->driver
= &driver
->drvwrap
.driver
;
305 usb_set_intfdata(iface
, priv
);
308 iface
->condition
= USB_INTERFACE_BOUND
;
310 iface
->pm_usage_cnt
= !(driver
->supports_autosuspend
);
313 /* if interface was already added, bind now; else let
314 * the future device_add() bind it, bypassing probe()
316 if (device_is_registered(dev
))
317 retval
= device_bind_driver(dev
);
321 EXPORT_SYMBOL(usb_driver_claim_interface
);
324 * usb_driver_release_interface - unbind a driver from an interface
325 * @driver: the driver to be unbound
326 * @iface: the interface from which it will be unbound
328 * This can be used by drivers to release an interface without waiting
329 * for their disconnect() methods to be called. In typical cases this
330 * also causes the driver disconnect() method to be called.
332 * This call is synchronous, and may not be used in an interrupt context.
333 * Callers must own the device lock, so driver disconnect() entries don't
334 * need extra locking, but other call contexts may need to explicitly claim
337 void usb_driver_release_interface(struct usb_driver
*driver
,
338 struct usb_interface
*iface
)
340 struct device
*dev
= &iface
->dev
;
341 struct usb_device
*udev
= interface_to_usbdev(iface
);
343 /* this should never happen, don't release something that's not ours */
344 if (!dev
->driver
|| dev
->driver
!= &driver
->drvwrap
.driver
)
347 /* don't release from within disconnect() */
348 if (iface
->condition
!= USB_INTERFACE_BOUND
)
351 /* don't release if the interface hasn't been added yet */
352 if (device_is_registered(dev
)) {
353 iface
->condition
= USB_INTERFACE_UNBINDING
;
354 device_release_driver(dev
);
358 usb_set_intfdata(iface
, NULL
);
361 iface
->condition
= USB_INTERFACE_UNBOUND
;
362 mark_quiesced(iface
);
363 iface
->needs_remote_wakeup
= 0;
366 EXPORT_SYMBOL(usb_driver_release_interface
);
368 /* returns 0 if no match, 1 if match */
369 int usb_match_device(struct usb_device
*dev
, const struct usb_device_id
*id
)
371 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
372 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
375 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
376 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
379 /* No need to test id->bcdDevice_lo != 0, since 0 is never
380 greater than any unsigned number. */
381 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
382 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
385 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
386 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
389 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
390 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
393 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
394 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
397 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
398 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
404 /* returns 0 if no match, 1 if match */
405 int usb_match_one_id(struct usb_interface
*interface
,
406 const struct usb_device_id
*id
)
408 struct usb_host_interface
*intf
;
409 struct usb_device
*dev
;
411 /* proc_connectinfo in devio.c may call us with id == NULL. */
415 intf
= interface
->cur_altsetting
;
416 dev
= interface_to_usbdev(interface
);
418 if (!usb_match_device(dev
, id
))
421 /* The interface class, subclass, and protocol should never be
422 * checked for a match if the device class is Vendor Specific,
423 * unless the match record specifies the Vendor ID. */
424 if (dev
->descriptor
.bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
425 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
426 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
427 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
428 USB_DEVICE_ID_MATCH_INT_PROTOCOL
)))
431 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
432 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
435 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
436 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
439 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
440 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
445 EXPORT_SYMBOL_GPL(usb_match_one_id
);
448 * usb_match_id - find first usb_device_id matching device or interface
449 * @interface: the interface of interest
450 * @id: array of usb_device_id structures, terminated by zero entry
452 * usb_match_id searches an array of usb_device_id's and returns
453 * the first one matching the device or interface, or null.
454 * This is used when binding (or rebinding) a driver to an interface.
455 * Most USB device drivers will use this indirectly, through the usb core,
456 * but some layered driver frameworks use it directly.
457 * These device tables are exported with MODULE_DEVICE_TABLE, through
458 * modutils, to support the driver loading functionality of USB hotplugging.
462 * The "match_flags" element in a usb_device_id controls which
463 * members are used. If the corresponding bit is set, the
464 * value in the device_id must match its corresponding member
465 * in the device or interface descriptor, or else the device_id
468 * "driver_info" is normally used only by device drivers,
469 * but you can create a wildcard "matches anything" usb_device_id
470 * as a driver's "modules.usbmap" entry if you provide an id with
471 * only a nonzero "driver_info" field. If you do this, the USB device
472 * driver's probe() routine should use additional intelligence to
473 * decide whether to bind to the specified interface.
475 * What Makes Good usb_device_id Tables:
477 * The match algorithm is very simple, so that intelligence in
478 * driver selection must come from smart driver id records.
479 * Unless you have good reasons to use another selection policy,
480 * provide match elements only in related groups, and order match
481 * specifiers from specific to general. Use the macros provided
482 * for that purpose if you can.
484 * The most specific match specifiers use device descriptor
485 * data. These are commonly used with product-specific matches;
486 * the USB_DEVICE macro lets you provide vendor and product IDs,
487 * and you can also match against ranges of product revisions.
488 * These are widely used for devices with application or vendor
489 * specific bDeviceClass values.
491 * Matches based on device class/subclass/protocol specifications
492 * are slightly more general; use the USB_DEVICE_INFO macro, or
493 * its siblings. These are used with single-function devices
494 * where bDeviceClass doesn't specify that each interface has
497 * Matches based on interface class/subclass/protocol are the
498 * most general; they let drivers bind to any interface on a
499 * multiple-function device. Use the USB_INTERFACE_INFO
500 * macro, or its siblings, to match class-per-interface style
501 * devices (as recorded in bInterfaceClass).
503 * Note that an entry created by USB_INTERFACE_INFO won't match
504 * any interface if the device class is set to Vendor-Specific.
505 * This is deliberate; according to the USB spec the meanings of
506 * the interface class/subclass/protocol for these devices are also
507 * vendor-specific, and hence matching against a standard product
508 * class wouldn't work anyway. If you really want to use an
509 * interface-based match for such a device, create a match record
510 * that also specifies the vendor ID. (Unforunately there isn't a
511 * standard macro for creating records like this.)
513 * Within those groups, remember that not all combinations are
514 * meaningful. For example, don't give a product version range
515 * without vendor and product IDs; or specify a protocol without
516 * its associated class and subclass.
518 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
519 const struct usb_device_id
*id
)
521 /* proc_connectinfo in devio.c may call us with id == NULL. */
525 /* It is important to check that id->driver_info is nonzero,
526 since an entry that is all zeroes except for a nonzero
527 id->driver_info is the way to create an entry that
528 indicates that the driver want to examine every
529 device and interface. */
530 for (; id
->idVendor
|| id
->bDeviceClass
|| id
->bInterfaceClass
||
531 id
->driver_info
; id
++) {
532 if (usb_match_one_id(interface
, id
))
538 EXPORT_SYMBOL_GPL_FUTURE(usb_match_id
);
540 static int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
542 /* devices and interfaces are handled separately */
543 if (is_usb_device(dev
)) {
545 /* interface drivers never match devices */
546 if (!is_usb_device_driver(drv
))
549 /* TODO: Add real matching code */
553 struct usb_interface
*intf
;
554 struct usb_driver
*usb_drv
;
555 const struct usb_device_id
*id
;
557 /* device drivers never match interfaces */
558 if (is_usb_device_driver(drv
))
561 intf
= to_usb_interface(dev
);
562 usb_drv
= to_usb_driver(drv
);
564 id
= usb_match_id(intf
, usb_drv
->id_table
);
568 id
= usb_match_dynamic_id(intf
, usb_drv
);
576 #ifdef CONFIG_HOTPLUG
577 static int usb_uevent(struct device
*dev
, char **envp
, int num_envp
,
578 char *buffer
, int buffer_size
)
580 struct usb_device
*usb_dev
;
587 /* driver is often null here; dev_dbg() would oops */
588 pr_debug ("usb %s: uevent\n", dev
->bus_id
);
590 if (is_usb_device(dev
))
591 usb_dev
= to_usb_device(dev
);
593 struct usb_interface
*intf
= to_usb_interface(dev
);
594 usb_dev
= interface_to_usbdev(intf
);
597 if (usb_dev
->devnum
< 0) {
598 pr_debug ("usb %s: already deleted?\n", dev
->bus_id
);
602 pr_debug ("usb %s: bus removed?\n", dev
->bus_id
);
606 #ifdef CONFIG_USB_DEVICEFS
607 /* If this is available, userspace programs can directly read
608 * all the device descriptors we don't tell them about. Or
609 * act as usermode drivers.
611 if (add_uevent_var(envp
, num_envp
, &i
,
612 buffer
, buffer_size
, &length
,
613 "DEVICE=/proc/bus/usb/%03d/%03d",
614 usb_dev
->bus
->busnum
, usb_dev
->devnum
))
618 /* per-device configurations are common */
619 if (add_uevent_var(envp
, num_envp
, &i
,
620 buffer
, buffer_size
, &length
,
622 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
623 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
624 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
)))
627 /* class-based driver binding models */
628 if (add_uevent_var(envp
, num_envp
, &i
,
629 buffer
, buffer_size
, &length
,
631 usb_dev
->descriptor
.bDeviceClass
,
632 usb_dev
->descriptor
.bDeviceSubClass
,
633 usb_dev
->descriptor
.bDeviceProtocol
))
636 if (add_uevent_var(envp
, num_envp
, &i
,
637 buffer
, buffer_size
, &length
,
639 usb_dev
->bus
->busnum
))
642 if (add_uevent_var(envp
, num_envp
, &i
,
643 buffer
, buffer_size
, &length
,
654 static int usb_uevent(struct device
*dev
, char **envp
,
655 int num_envp
, char *buffer
, int buffer_size
)
659 #endif /* CONFIG_HOTPLUG */
662 * usb_register_device_driver - register a USB device (not interface) driver
663 * @new_udriver: USB operations for the device driver
664 * @owner: module owner of this driver.
666 * Registers a USB device driver with the USB core. The list of
667 * unattached devices will be rescanned whenever a new driver is
668 * added, allowing the new driver to attach to any recognized devices.
669 * Returns a negative error code on failure and 0 on success.
671 int usb_register_device_driver(struct usb_device_driver
*new_udriver
,
672 struct module
*owner
)
679 new_udriver
->drvwrap
.for_devices
= 1;
680 new_udriver
->drvwrap
.driver
.name
= (char *) new_udriver
->name
;
681 new_udriver
->drvwrap
.driver
.bus
= &usb_bus_type
;
682 new_udriver
->drvwrap
.driver
.probe
= usb_probe_device
;
683 new_udriver
->drvwrap
.driver
.remove
= usb_unbind_device
;
684 new_udriver
->drvwrap
.driver
.owner
= owner
;
686 retval
= driver_register(&new_udriver
->drvwrap
.driver
);
689 pr_info("%s: registered new device driver %s\n",
690 usbcore_name
, new_udriver
->name
);
691 usbfs_update_special();
693 printk(KERN_ERR
"%s: error %d registering device "
695 usbcore_name
, retval
, new_udriver
->name
);
700 EXPORT_SYMBOL_GPL(usb_register_device_driver
);
703 * usb_deregister_device_driver - unregister a USB device (not interface) driver
704 * @udriver: USB operations of the device driver to unregister
705 * Context: must be able to sleep
707 * Unlinks the specified driver from the internal USB driver list.
709 void usb_deregister_device_driver(struct usb_device_driver
*udriver
)
711 pr_info("%s: deregistering device driver %s\n",
712 usbcore_name
, udriver
->name
);
714 driver_unregister(&udriver
->drvwrap
.driver
);
715 usbfs_update_special();
717 EXPORT_SYMBOL_GPL(usb_deregister_device_driver
);
720 * usb_register_driver - register a USB interface driver
721 * @new_driver: USB operations for the interface driver
722 * @owner: module owner of this driver.
723 * @mod_name: module name string
725 * Registers a USB interface driver with the USB core. The list of
726 * unattached interfaces will be rescanned whenever a new driver is
727 * added, allowing the new driver to attach to any recognized interfaces.
728 * Returns a negative error code on failure and 0 on success.
730 * NOTE: if you want your driver to use the USB major number, you must call
731 * usb_register_dev() to enable that functionality. This function no longer
732 * takes care of that.
734 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
,
735 const char *mod_name
)
742 new_driver
->drvwrap
.for_devices
= 0;
743 new_driver
->drvwrap
.driver
.name
= (char *) new_driver
->name
;
744 new_driver
->drvwrap
.driver
.bus
= &usb_bus_type
;
745 new_driver
->drvwrap
.driver
.probe
= usb_probe_interface
;
746 new_driver
->drvwrap
.driver
.remove
= usb_unbind_interface
;
747 new_driver
->drvwrap
.driver
.owner
= owner
;
748 new_driver
->drvwrap
.driver
.mod_name
= mod_name
;
749 spin_lock_init(&new_driver
->dynids
.lock
);
750 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
752 retval
= driver_register(&new_driver
->drvwrap
.driver
);
755 pr_info("%s: registered new interface driver %s\n",
756 usbcore_name
, new_driver
->name
);
757 usbfs_update_special();
758 usb_create_newid_file(new_driver
);
760 printk(KERN_ERR
"%s: error %d registering interface "
762 usbcore_name
, retval
, new_driver
->name
);
767 EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver
);
770 * usb_deregister - unregister a USB interface driver
771 * @driver: USB operations of the interface driver to unregister
772 * Context: must be able to sleep
774 * Unlinks the specified driver from the internal USB driver list.
776 * NOTE: If you called usb_register_dev(), you still need to call
777 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
778 * this * call will no longer do it for you.
780 void usb_deregister(struct usb_driver
*driver
)
782 pr_info("%s: deregistering interface driver %s\n",
783 usbcore_name
, driver
->name
);
785 usb_remove_newid_file(driver
);
786 usb_free_dynids(driver
);
787 driver_unregister(&driver
->drvwrap
.driver
);
789 usbfs_update_special();
791 EXPORT_SYMBOL_GPL_FUTURE(usb_deregister
);
795 /* Caller has locked udev's pm_mutex */
796 static int usb_suspend_device(struct usb_device
*udev
, pm_message_t msg
)
798 struct usb_device_driver
*udriver
;
801 if (udev
->state
== USB_STATE_NOTATTACHED
||
802 udev
->state
== USB_STATE_SUSPENDED
)
805 /* For devices that don't have a driver, we do a generic suspend. */
806 if (udev
->dev
.driver
)
807 udriver
= to_usb_device_driver(udev
->dev
.driver
);
809 udev
->do_remote_wakeup
= 0;
810 udriver
= &usb_generic_driver
;
812 status
= udriver
->suspend(udev
, msg
);
815 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
817 udev
->dev
.power
.power_state
.event
= msg
.event
;
821 /* Caller has locked udev's pm_mutex */
822 static int usb_resume_device(struct usb_device
*udev
)
824 struct usb_device_driver
*udriver
;
827 if (udev
->state
== USB_STATE_NOTATTACHED
)
829 if (udev
->state
!= USB_STATE_SUSPENDED
&& !udev
->reset_resume
)
832 /* Can't resume it if it doesn't have a driver. */
833 if (udev
->dev
.driver
== NULL
) {
838 udriver
= to_usb_device_driver(udev
->dev
.driver
);
839 status
= udriver
->resume(udev
);
842 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
844 udev
->autoresume_disabled
= 0;
845 udev
->dev
.power
.power_state
.event
= PM_EVENT_ON
;
850 /* Caller has locked intf's usb_device's pm mutex */
851 static int usb_suspend_interface(struct usb_interface
*intf
, pm_message_t msg
)
853 struct usb_driver
*driver
;
856 /* with no hardware, USB interfaces only use FREEZE and ON states */
857 if (interface_to_usbdev(intf
)->state
== USB_STATE_NOTATTACHED
||
861 if (intf
->condition
== USB_INTERFACE_UNBOUND
) /* This can't happen */
863 driver
= to_usb_driver(intf
->dev
.driver
);
865 if (driver
->suspend
&& driver
->resume
) {
866 status
= driver
->suspend(intf
, msg
);
869 else if (!interface_to_usbdev(intf
)->auto_pm
)
870 dev_err(&intf
->dev
, "%s error %d\n",
873 // FIXME else if there's no suspend method, disconnect...
874 // Not possible if auto_pm is set...
875 dev_warn(&intf
->dev
, "no suspend for driver %s?\n",
881 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
885 /* Caller has locked intf's usb_device's pm_mutex */
886 static int usb_resume_interface(struct usb_interface
*intf
, int reset_resume
)
888 struct usb_driver
*driver
;
891 if (interface_to_usbdev(intf
)->state
== USB_STATE_NOTATTACHED
||
895 /* Don't let autoresume interfere with unbinding */
896 if (intf
->condition
== USB_INTERFACE_UNBINDING
)
899 /* Can't resume it if it doesn't have a driver. */
900 if (intf
->condition
== USB_INTERFACE_UNBOUND
) {
904 driver
= to_usb_driver(intf
->dev
.driver
);
906 if (reset_resume
&& driver
->post_reset
)
907 driver
->post_reset(intf
, reset_resume
);
908 else if (driver
->resume
) {
909 status
= driver
->resume(intf
);
911 dev_err(&intf
->dev
, "%s error %d\n",
914 dev_warn(&intf
->dev
, "no resume for driver %s?\n",
918 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
924 #ifdef CONFIG_USB_SUSPEND
926 /* Internal routine to check whether we may autosuspend a device. */
927 static int autosuspend_check(struct usb_device
*udev
)
930 struct usb_interface
*intf
;
931 unsigned long suspend_time
;
933 /* For autosuspend, fail fast if anything is in use or autosuspend
934 * is disabled. Also fail if any interfaces require remote wakeup
935 * but it isn't available.
937 udev
->do_remote_wakeup
= device_may_wakeup(&udev
->dev
);
938 if (udev
->pm_usage_cnt
> 0)
940 if (udev
->autosuspend_delay
< 0 || udev
->autosuspend_disabled
)
943 suspend_time
= udev
->last_busy
+ udev
->autosuspend_delay
;
944 if (udev
->actconfig
) {
945 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
946 intf
= udev
->actconfig
->interface
[i
];
947 if (!is_active(intf
))
949 if (intf
->pm_usage_cnt
> 0)
951 if (intf
->needs_remote_wakeup
&&
952 !udev
->do_remote_wakeup
) {
953 dev_dbg(&udev
->dev
, "remote wakeup needed "
954 "for autosuspend\n");
960 /* If everything is okay but the device hasn't been idle for long
961 * enough, queue a delayed autosuspend request.
963 if (time_after(suspend_time
, jiffies
)) {
964 if (!timer_pending(&udev
->autosuspend
.timer
)) {
966 /* The value of jiffies may change between the
967 * time_after() comparison above and the subtraction
968 * below. That's okay; the system behaves sanely
969 * when a timer is registered for the present moment
972 queue_delayed_work(ksuspend_usb_wq
, &udev
->autosuspend
,
973 suspend_time
- jiffies
);
982 static inline int autosuspend_check(struct usb_device
*udev
)
987 #endif /* CONFIG_USB_SUSPEND */
990 * usb_suspend_both - suspend a USB device and its interfaces
991 * @udev: the usb_device to suspend
992 * @msg: Power Management message describing this state transition
994 * This is the central routine for suspending USB devices. It calls the
995 * suspend methods for all the interface drivers in @udev and then calls
996 * the suspend method for @udev itself. If an error occurs at any stage,
997 * all the interfaces which were suspended are resumed so that they remain
998 * in the same state as the device.
1000 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
1001 * checks first to make sure that neither the device itself or any of its
1002 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
1003 * are, the autosuspend fails.
1005 * If the suspend succeeds, the routine recursively queues an autosuspend
1006 * request for @udev's parent device, thereby propagating the change up
1007 * the device tree. If all of the parent's children are now suspended,
1008 * the parent will autosuspend in turn.
1010 * The suspend method calls are subject to mutual exclusion under control
1011 * of @udev's pm_mutex. Many of these calls are also under the protection
1012 * of @udev's device lock (including all requests originating outside the
1013 * USB subsystem), but autosuspend requests generated by a child device or
1014 * interface driver may not be. Usbcore will insure that the method calls
1015 * do not arrive during bind, unbind, or reset operations. However, drivers
1016 * must be prepared to handle suspend calls arriving at unpredictable times.
1017 * The only way to block such calls is to do an autoresume (preventing
1018 * autosuspends) while holding @udev's device lock (preventing outside
1021 * The caller must hold @udev->pm_mutex.
1023 * This routine can run only in process context.
1025 static int usb_suspend_both(struct usb_device
*udev
, pm_message_t msg
)
1029 struct usb_interface
*intf
;
1030 struct usb_device
*parent
= udev
->parent
;
1032 if (udev
->state
== USB_STATE_NOTATTACHED
||
1033 udev
->state
== USB_STATE_SUSPENDED
)
1036 udev
->do_remote_wakeup
= device_may_wakeup(&udev
->dev
);
1038 if (udev
->auto_pm
) {
1039 status
= autosuspend_check(udev
);
1044 /* Suspend all the interfaces and then udev itself */
1045 if (udev
->actconfig
) {
1046 for (; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1047 intf
= udev
->actconfig
->interface
[i
];
1048 status
= usb_suspend_interface(intf
, msg
);
1055 /* Non-root devices don't need to do anything for FREEZE
1057 if (udev
->parent
&& (msg
.event
== PM_EVENT_FREEZE
||
1058 msg
.event
== PM_EVENT_PRETHAW
))
1060 status
= usb_suspend_device(udev
, msg
);
1063 /* If the suspend failed, resume interfaces that did get suspended */
1066 intf
= udev
->actconfig
->interface
[i
];
1067 usb_resume_interface(intf
, 0);
1070 /* Try another autosuspend when the interfaces aren't busy */
1072 autosuspend_check(udev
);
1074 /* If the suspend succeeded, propagate it up the tree */
1076 cancel_delayed_work(&udev
->autosuspend
);
1078 usb_autosuspend_device(parent
);
1082 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1087 * usb_resume_both - resume a USB device and its interfaces
1088 * @udev: the usb_device to resume
1090 * This is the central routine for resuming USB devices. It calls the
1091 * the resume method for @udev and then calls the resume methods for all
1092 * the interface drivers in @udev.
1094 * Before starting the resume, the routine calls itself recursively for
1095 * the parent device of @udev, thereby propagating the change up the device
1096 * tree and assuring that @udev will be able to resume. If the parent is
1097 * unable to resume successfully, the routine fails.
1099 * The resume method calls are subject to mutual exclusion under control
1100 * of @udev's pm_mutex. Many of these calls are also under the protection
1101 * of @udev's device lock (including all requests originating outside the
1102 * USB subsystem), but autoresume requests generated by a child device or
1103 * interface driver may not be. Usbcore will insure that the method calls
1104 * do not arrive during bind, unbind, or reset operations. However, drivers
1105 * must be prepared to handle resume calls arriving at unpredictable times.
1106 * The only way to block such calls is to do an autoresume (preventing
1107 * other autoresumes) while holding @udev's device lock (preventing outside
1110 * The caller must hold @udev->pm_mutex.
1112 * This routine can run only in process context.
1114 static int usb_resume_both(struct usb_device
*udev
)
1118 struct usb_interface
*intf
;
1119 struct usb_device
*parent
= udev
->parent
;
1121 cancel_delayed_work(&udev
->autosuspend
);
1122 if (udev
->state
== USB_STATE_NOTATTACHED
) {
1127 /* Propagate the resume up the tree, if necessary */
1128 if (udev
->state
== USB_STATE_SUSPENDED
) {
1129 if (udev
->auto_pm
&& udev
->autoresume_disabled
) {
1134 status
= usb_autoresume_device(parent
);
1136 status
= usb_resume_device(udev
);
1138 usb_autosuspend_device(parent
);
1140 /* It's possible usb_resume_device()
1141 * failed after the port was
1142 * unsuspended, causing udev to be
1143 * logically disconnected. We don't
1144 * want usb_disconnect() to autosuspend
1145 * the parent again, so tell it that
1146 * udev disconnected while still
1149 USB_STATE_NOTATTACHED
)
1150 udev
->discon_suspended
= 1;
1155 /* We can't progagate beyond the USB subsystem,
1156 * so if a root hub's controller is suspended
1157 * then we're stuck. */
1158 if (udev
->dev
.parent
->power
.power_state
.event
!=
1160 status
= -EHOSTUNREACH
;
1162 status
= usb_resume_device(udev
);
1166 /* Needed for setting udev->dev.power.power_state.event,
1167 * for possible debugging message, and for reset_resume. */
1168 status
= usb_resume_device(udev
);
1171 if (status
== 0 && udev
->actconfig
) {
1172 for (i
= 0; i
< udev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1173 intf
= udev
->actconfig
->interface
[i
];
1174 usb_resume_interface(intf
, udev
->reset_resume
);
1179 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1180 udev
->reset_resume
= 0;
1184 #ifdef CONFIG_USB_SUSPEND
1186 /* Internal routine to adjust a device's usage counter and change
1187 * its autosuspend state.
1189 static int usb_autopm_do_device(struct usb_device
*udev
, int inc_usage_cnt
)
1195 udev
->pm_usage_cnt
+= inc_usage_cnt
;
1196 WARN_ON(udev
->pm_usage_cnt
< 0);
1197 if (inc_usage_cnt
>= 0 && udev
->pm_usage_cnt
> 0) {
1198 if (udev
->state
== USB_STATE_SUSPENDED
)
1199 status
= usb_resume_both(udev
);
1201 udev
->pm_usage_cnt
-= inc_usage_cnt
;
1202 else if (inc_usage_cnt
)
1203 udev
->last_busy
= jiffies
;
1204 } else if (inc_usage_cnt
<= 0 && udev
->pm_usage_cnt
<= 0) {
1206 udev
->last_busy
= jiffies
;
1207 status
= usb_suspend_both(udev
, PMSG_SUSPEND
);
1209 usb_pm_unlock(udev
);
1213 /* usb_autosuspend_work - callback routine to autosuspend a USB device */
1214 void usb_autosuspend_work(struct work_struct
*work
)
1216 struct usb_device
*udev
=
1217 container_of(work
, struct usb_device
, autosuspend
.work
);
1219 usb_autopm_do_device(udev
, 0);
1223 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1224 * @udev: the usb_device to autosuspend
1226 * This routine should be called when a core subsystem is finished using
1227 * @udev and wants to allow it to autosuspend. Examples would be when
1228 * @udev's device file in usbfs is closed or after a configuration change.
1230 * @udev's usage counter is decremented. If it or any of the usage counters
1231 * for an active interface is greater than 0, no autosuspend request will be
1232 * queued. (If an interface driver does not support autosuspend then its
1233 * usage counter is permanently positive.) Furthermore, if an interface
1234 * driver requires remote-wakeup capability during autosuspend but remote
1235 * wakeup is disabled, the autosuspend will fail.
1237 * Often the caller will hold @udev's device lock, but this is not
1240 * This routine can run only in process context.
1242 void usb_autosuspend_device(struct usb_device
*udev
)
1246 status
= usb_autopm_do_device(udev
, -1);
1247 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1248 // __FUNCTION__, udev->pm_usage_cnt);
1252 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1253 * @udev: the usb_device to autosuspend
1255 * This routine should be called when a core subsystem thinks @udev may
1256 * be ready to autosuspend.
1258 * @udev's usage counter left unchanged. If it or any of the usage counters
1259 * for an active interface is greater than 0, or autosuspend is not allowed
1260 * for any other reason, no autosuspend request will be queued.
1262 * This routine can run only in process context.
1264 void usb_try_autosuspend_device(struct usb_device
*udev
)
1266 usb_autopm_do_device(udev
, 0);
1267 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1268 // __FUNCTION__, udev->pm_usage_cnt);
1272 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1273 * @udev: the usb_device to autoresume
1275 * This routine should be called when a core subsystem wants to use @udev
1276 * and needs to guarantee that it is not suspended. No autosuspend will
1277 * occur until usb_autosuspend_device is called. (Note that this will not
1278 * prevent suspend events originating in the PM core.) Examples would be
1279 * when @udev's device file in usbfs is opened or when a remote-wakeup
1280 * request is received.
1282 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1283 * However if the autoresume fails then the usage counter is re-decremented.
1285 * Often the caller will hold @udev's device lock, but this is not
1286 * necessary (and attempting it might cause deadlock).
1288 * This routine can run only in process context.
1290 int usb_autoresume_device(struct usb_device
*udev
)
1294 status
= usb_autopm_do_device(udev
, 1);
1295 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1296 // __FUNCTION__, status, udev->pm_usage_cnt);
1300 /* Internal routine to adjust an interface's usage counter and change
1301 * its device's autosuspend state.
1303 static int usb_autopm_do_interface(struct usb_interface
*intf
,
1306 struct usb_device
*udev
= interface_to_usbdev(intf
);
1310 if (intf
->condition
== USB_INTERFACE_UNBOUND
)
1314 intf
->pm_usage_cnt
+= inc_usage_cnt
;
1315 if (inc_usage_cnt
>= 0 && intf
->pm_usage_cnt
> 0) {
1316 if (udev
->state
== USB_STATE_SUSPENDED
)
1317 status
= usb_resume_both(udev
);
1319 intf
->pm_usage_cnt
-= inc_usage_cnt
;
1320 else if (inc_usage_cnt
)
1321 udev
->last_busy
= jiffies
;
1322 } else if (inc_usage_cnt
<= 0 && intf
->pm_usage_cnt
<= 0) {
1324 udev
->last_busy
= jiffies
;
1325 status
= usb_suspend_both(udev
, PMSG_SUSPEND
);
1328 usb_pm_unlock(udev
);
1333 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1334 * @intf: the usb_interface whose counter should be decremented
1336 * This routine should be called by an interface driver when it is
1337 * finished using @intf and wants to allow it to autosuspend. A typical
1338 * example would be a character-device driver when its device file is
1341 * The routine decrements @intf's usage counter. When the counter reaches
1342 * 0, a delayed autosuspend request for @intf's device is queued. When
1343 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1344 * the other usage counters for the sibling interfaces and @intf's
1345 * usb_device, the device and all its interfaces will be autosuspended.
1347 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1348 * core will not change its value other than the increment and decrement
1349 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1350 * may use this simple counter-oriented discipline or may set the value
1353 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1354 * take place only if the device's remote-wakeup facility is enabled.
1356 * Suspend method calls queued by this routine can arrive at any time
1357 * while @intf is resumed and its usage counter is equal to 0. They are
1358 * not protected by the usb_device's lock but only by its pm_mutex.
1359 * Drivers must provide their own synchronization.
1361 * This routine can run only in process context.
1363 void usb_autopm_put_interface(struct usb_interface
*intf
)
1367 status
= usb_autopm_do_interface(intf
, -1);
1368 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1369 // __FUNCTION__, status, intf->pm_usage_cnt);
1371 EXPORT_SYMBOL_GPL(usb_autopm_put_interface
);
1374 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1375 * @intf: the usb_interface whose counter should be incremented
1377 * This routine should be called by an interface driver when it wants to
1378 * use @intf and needs to guarantee that it is not suspended. In addition,
1379 * the routine prevents @intf from being autosuspended subsequently. (Note
1380 * that this will not prevent suspend events originating in the PM core.)
1381 * This prevention will persist until usb_autopm_put_interface() is called
1382 * or @intf is unbound. A typical example would be a character-device
1383 * driver when its device file is opened.
1386 * The routine increments @intf's usage counter. (However if the
1387 * autoresume fails then the counter is re-decremented.) So long as the
1388 * counter is greater than 0, autosuspend will not be allowed for @intf
1389 * or its usb_device. When the driver is finished using @intf it should
1390 * call usb_autopm_put_interface() to decrement the usage counter and
1391 * queue a delayed autosuspend request (if the counter is <= 0).
1394 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1395 * core will not change its value other than the increment and decrement
1396 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1397 * may use this simple counter-oriented discipline or may set the value
1400 * Resume method calls generated by this routine can arrive at any time
1401 * while @intf is suspended. They are not protected by the usb_device's
1402 * lock but only by its pm_mutex. Drivers must provide their own
1405 * This routine can run only in process context.
1407 int usb_autopm_get_interface(struct usb_interface
*intf
)
1411 status
= usb_autopm_do_interface(intf
, 1);
1412 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1413 // __FUNCTION__, status, intf->pm_usage_cnt);
1416 EXPORT_SYMBOL_GPL(usb_autopm_get_interface
);
1419 * usb_autopm_set_interface - set a USB interface's autosuspend state
1420 * @intf: the usb_interface whose state should be set
1422 * This routine sets the autosuspend state of @intf's device according
1423 * to @intf's usage counter, which the caller must have set previously.
1424 * If the counter is <= 0, the device is autosuspended (if it isn't
1425 * already suspended and if nothing else prevents the autosuspend). If
1426 * the counter is > 0, the device is autoresumed (if it isn't already
1429 int usb_autopm_set_interface(struct usb_interface
*intf
)
1433 status
= usb_autopm_do_interface(intf
, 0);
1434 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1435 // __FUNCTION__, status, intf->pm_usage_cnt);
1438 EXPORT_SYMBOL_GPL(usb_autopm_set_interface
);
1442 void usb_autosuspend_work(struct work_struct
*work
)
1445 #endif /* CONFIG_USB_SUSPEND */
1448 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1449 * @udev: the usb_device to suspend
1450 * @msg: Power Management message describing this state transition
1452 * This routine handles external suspend requests: ones not generated
1453 * internally by a USB driver (autosuspend) but rather coming from the user
1454 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1455 * out regardless of @udev's usage counter or those of its interfaces,
1456 * and regardless of whether or not remote wakeup is enabled. Of course,
1457 * interface drivers still have the option of failing the suspend (if
1458 * there are unsuspended children, for example).
1460 * The caller must hold @udev's device lock.
1462 int usb_external_suspend_device(struct usb_device
*udev
, pm_message_t msg
)
1468 status
= usb_suspend_both(udev
, msg
);
1469 usb_pm_unlock(udev
);
1474 * usb_external_resume_device - external resume of a USB device and its interfaces
1475 * @udev: the usb_device to resume
1477 * This routine handles external resume requests: ones not generated
1478 * internally by a USB driver (autoresume) but rather coming from the user
1479 * (via sysfs), the PM core (system resume), or the device itself (remote
1480 * wakeup). @udev's usage counter is unaffected.
1482 * The caller must hold @udev's device lock.
1484 int usb_external_resume_device(struct usb_device
*udev
)
1490 status
= usb_resume_both(udev
);
1491 udev
->last_busy
= jiffies
;
1492 usb_pm_unlock(udev
);
1494 /* Now that the device is awake, we can start trying to autosuspend
1497 usb_try_autosuspend_device(udev
);
1501 static int usb_suspend(struct device
*dev
, pm_message_t message
)
1503 if (!is_usb_device(dev
)) /* Ignore PM for interfaces */
1505 return usb_external_suspend_device(to_usb_device(dev
), message
);
1508 static int usb_resume(struct device
*dev
)
1510 struct usb_device
*udev
;
1512 if (!is_usb_device(dev
)) /* Ignore PM for interfaces */
1514 udev
= to_usb_device(dev
);
1516 /* If autoresume is disabled then we also want to prevent resume
1517 * during system wakeup. However, a "persistent-device" reset-resume
1518 * after power loss counts as a wakeup event. So allow a
1519 * reset-resume to occur if remote wakeup is enabled. */
1520 if (udev
->autoresume_disabled
) {
1521 if (!(udev
->reset_resume
&& udev
->do_remote_wakeup
))
1524 return usb_external_resume_device(udev
);
1529 #define usb_suspend NULL
1530 #define usb_resume NULL
1532 #endif /* CONFIG_PM */
1534 struct bus_type usb_bus_type
= {
1536 .match
= usb_device_match
,
1537 .uevent
= usb_uevent
,
1538 .suspend
= usb_suspend
,
1539 .resume
= usb_resume
,