Driver core: create lock/unlock functions for struct device
[linux-2.6.git] / drivers / usb / core / driver.c
blobf3c233806fa3415a5489842f3ccbf4030da55f8f
1 /*
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
21 * real drivers.
25 #include <linux/device.h>
26 #include <linux/usb.h>
27 #include <linux/usb/quirks.h>
28 #include <linux/pm_runtime.h>
29 #include "hcd.h"
30 #include "usb.h"
33 #ifdef CONFIG_HOTPLUG
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;
44 u32 idVendor = 0;
45 u32 idProduct = 0;
46 int fields = 0;
47 int retval = 0;
49 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
50 if (fields < 2)
51 return -EINVAL;
53 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
54 if (!dynid)
55 return -ENOMEM;
57 INIT_LIST_HEAD(&dynid->node);
58 dynid->id.idVendor = idVendor;
59 dynid->id.idProduct = idProduct;
60 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
62 spin_lock(&dynids->lock);
63 list_add_tail(&dynid->node, &dynids->list);
64 spin_unlock(&dynids->lock);
66 if (get_driver(driver)) {
67 retval = driver_attach(driver);
68 put_driver(driver);
71 if (retval)
72 return retval;
73 return count;
75 EXPORT_SYMBOL_GPL(usb_store_new_id);
77 static ssize_t store_new_id(struct device_driver *driver,
78 const char *buf, size_t count)
80 struct usb_driver *usb_drv = to_usb_driver(driver);
82 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
84 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
86 /**
87 * store_remove_id - remove a USB device ID from this driver
88 * @driver: target device driver
89 * @buf: buffer for scanning device ID data
90 * @count: input size
92 * Removes a dynamic usb device ID from this driver.
94 static ssize_t
95 store_remove_id(struct device_driver *driver, const char *buf, size_t count)
97 struct usb_dynid *dynid, *n;
98 struct usb_driver *usb_driver = to_usb_driver(driver);
99 u32 idVendor = 0;
100 u32 idProduct = 0;
101 int fields = 0;
102 int retval = 0;
104 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
105 if (fields < 2)
106 return -EINVAL;
108 spin_lock(&usb_driver->dynids.lock);
109 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
110 struct usb_device_id *id = &dynid->id;
111 if ((id->idVendor == idVendor) &&
112 (id->idProduct == idProduct)) {
113 list_del(&dynid->node);
114 kfree(dynid);
115 retval = 0;
116 break;
119 spin_unlock(&usb_driver->dynids.lock);
121 if (retval)
122 return retval;
123 return count;
125 static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
127 static int usb_create_newid_file(struct usb_driver *usb_drv)
129 int error = 0;
131 if (usb_drv->no_dynamic_id)
132 goto exit;
134 if (usb_drv->probe != NULL)
135 error = driver_create_file(&usb_drv->drvwrap.driver,
136 &driver_attr_new_id);
137 exit:
138 return error;
141 static void usb_remove_newid_file(struct usb_driver *usb_drv)
143 if (usb_drv->no_dynamic_id)
144 return;
146 if (usb_drv->probe != NULL)
147 driver_remove_file(&usb_drv->drvwrap.driver,
148 &driver_attr_new_id);
151 static int
152 usb_create_removeid_file(struct usb_driver *drv)
154 int error = 0;
155 if (drv->probe != NULL)
156 error = driver_create_file(&drv->drvwrap.driver,
157 &driver_attr_remove_id);
158 return error;
161 static void usb_remove_removeid_file(struct usb_driver *drv)
163 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
166 static void usb_free_dynids(struct usb_driver *usb_drv)
168 struct usb_dynid *dynid, *n;
170 spin_lock(&usb_drv->dynids.lock);
171 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
172 list_del(&dynid->node);
173 kfree(dynid);
175 spin_unlock(&usb_drv->dynids.lock);
177 #else
178 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
180 return 0;
183 static void usb_remove_newid_file(struct usb_driver *usb_drv)
187 static int
188 usb_create_removeid_file(struct usb_driver *drv)
190 return 0;
193 static void usb_remove_removeid_file(struct usb_driver *drv)
197 static inline void usb_free_dynids(struct usb_driver *usb_drv)
200 #endif
202 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
203 struct usb_driver *drv)
205 struct usb_dynid *dynid;
207 spin_lock(&drv->dynids.lock);
208 list_for_each_entry(dynid, &drv->dynids.list, node) {
209 if (usb_match_one_id(intf, &dynid->id)) {
210 spin_unlock(&drv->dynids.lock);
211 return &dynid->id;
214 spin_unlock(&drv->dynids.lock);
215 return NULL;
219 /* called from driver core with dev locked */
220 static int usb_probe_device(struct device *dev)
222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
223 struct usb_device *udev = to_usb_device(dev);
224 int error = 0;
226 dev_dbg(dev, "%s\n", __func__);
228 /* TODO: Add real matching code */
230 /* The device should always appear to be in use
231 * unless the driver suports autosuspend.
233 if (!udriver->supports_autosuspend)
234 error = usb_autoresume_device(udev);
236 if (!error)
237 error = udriver->probe(udev);
238 return error;
241 /* called from driver core with dev locked */
242 static int usb_unbind_device(struct device *dev)
244 struct usb_device *udev = to_usb_device(dev);
245 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
247 udriver->disconnect(udev);
248 if (!udriver->supports_autosuspend)
249 usb_autosuspend_device(udev);
250 return 0;
254 * Cancel any pending scheduled resets
256 * [see usb_queue_reset_device()]
258 * Called after unconfiguring / when releasing interfaces. See
259 * comments in __usb_queue_reset_device() regarding
260 * udev->reset_running.
262 static void usb_cancel_queued_reset(struct usb_interface *iface)
264 if (iface->reset_running == 0)
265 cancel_work_sync(&iface->reset_ws);
268 /* called from driver core with dev locked */
269 static int usb_probe_interface(struct device *dev)
271 struct usb_driver *driver = to_usb_driver(dev->driver);
272 struct usb_interface *intf = to_usb_interface(dev);
273 struct usb_device *udev = interface_to_usbdev(intf);
274 const struct usb_device_id *id;
275 int error = -ENODEV;
277 dev_dbg(dev, "%s\n", __func__);
279 intf->needs_binding = 0;
281 if (usb_device_is_owned(udev))
282 return error;
284 if (udev->authorized == 0) {
285 dev_err(&intf->dev, "Device is not authorized for usage\n");
286 return error;
289 id = usb_match_id(intf, driver->id_table);
290 if (!id)
291 id = usb_match_dynamic_id(intf, driver);
292 if (!id)
293 return error;
295 dev_dbg(dev, "%s - got id\n", __func__);
297 error = usb_autoresume_device(udev);
298 if (error)
299 return error;
301 intf->condition = USB_INTERFACE_BINDING;
303 /* Bound interfaces are initially active. They are
304 * runtime-PM-enabled only if the driver has autosuspend support.
305 * They are sensitive to their children's power states.
307 pm_runtime_set_active(dev);
308 pm_suspend_ignore_children(dev, false);
309 if (driver->supports_autosuspend)
310 pm_runtime_enable(dev);
312 /* Carry out a deferred switch to altsetting 0 */
313 if (intf->needs_altsetting0) {
314 error = usb_set_interface(udev, intf->altsetting[0].
315 desc.bInterfaceNumber, 0);
316 if (error < 0)
317 goto err;
318 intf->needs_altsetting0 = 0;
321 error = driver->probe(intf, id);
322 if (error)
323 goto err;
325 intf->condition = USB_INTERFACE_BOUND;
326 usb_autosuspend_device(udev);
327 return error;
329 err:
330 intf->needs_remote_wakeup = 0;
331 intf->condition = USB_INTERFACE_UNBOUND;
332 usb_cancel_queued_reset(intf);
334 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
335 pm_runtime_disable(dev);
336 pm_runtime_set_suspended(dev);
338 usb_autosuspend_device(udev);
339 return error;
342 /* called from driver core with dev locked */
343 static int usb_unbind_interface(struct device *dev)
345 struct usb_driver *driver = to_usb_driver(dev->driver);
346 struct usb_interface *intf = to_usb_interface(dev);
347 struct usb_device *udev;
348 int error, r;
350 intf->condition = USB_INTERFACE_UNBINDING;
352 /* Autoresume for set_interface call below */
353 udev = interface_to_usbdev(intf);
354 error = usb_autoresume_device(udev);
356 /* Terminate all URBs for this interface unless the driver
357 * supports "soft" unbinding.
359 if (!driver->soft_unbind)
360 usb_disable_interface(udev, intf, false);
362 driver->disconnect(intf);
363 usb_cancel_queued_reset(intf);
365 /* Reset other interface state.
366 * We cannot do a Set-Interface if the device is suspended or
367 * if it is prepared for a system sleep (since installing a new
368 * altsetting means creating new endpoint device entries).
369 * When either of these happens, defer the Set-Interface.
371 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
372 /* Already in altsetting 0 so skip Set-Interface.
373 * Just re-enable it without affecting the endpoint toggles.
375 usb_enable_interface(udev, intf, false);
376 } else if (!error && intf->dev.power.status == DPM_ON) {
377 r = usb_set_interface(udev, intf->altsetting[0].
378 desc.bInterfaceNumber, 0);
379 if (r < 0)
380 intf->needs_altsetting0 = 1;
381 } else {
382 intf->needs_altsetting0 = 1;
384 usb_set_intfdata(intf, NULL);
386 intf->condition = USB_INTERFACE_UNBOUND;
387 intf->needs_remote_wakeup = 0;
389 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
390 pm_runtime_disable(dev);
391 pm_runtime_set_suspended(dev);
393 /* Undo any residual pm_autopm_get_interface_* calls */
394 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
395 usb_autopm_put_interface_no_suspend(intf);
396 atomic_set(&intf->pm_usage_cnt, 0);
398 if (!error)
399 usb_autosuspend_device(udev);
401 return 0;
405 * usb_driver_claim_interface - bind a driver to an interface
406 * @driver: the driver to be bound
407 * @iface: the interface to which it will be bound; must be in the
408 * usb device's active configuration
409 * @priv: driver data associated with that interface
411 * This is used by usb device drivers that need to claim more than one
412 * interface on a device when probing (audio and acm are current examples).
413 * No device driver should directly modify internal usb_interface or
414 * usb_device structure members.
416 * Few drivers should need to use this routine, since the most natural
417 * way to bind to an interface is to return the private data from
418 * the driver's probe() method.
420 * Callers must own the device lock, so driver probe() entries don't need
421 * extra locking, but other call contexts may need to explicitly claim that
422 * lock.
424 int usb_driver_claim_interface(struct usb_driver *driver,
425 struct usb_interface *iface, void *priv)
427 struct device *dev = &iface->dev;
428 int retval = 0;
430 if (dev->driver)
431 return -EBUSY;
433 dev->driver = &driver->drvwrap.driver;
434 usb_set_intfdata(iface, priv);
435 iface->needs_binding = 0;
437 iface->condition = USB_INTERFACE_BOUND;
439 /* Bound interfaces are initially active. They are
440 * runtime-PM-enabled only if the driver has autosuspend support.
441 * They are sensitive to their children's power states.
443 pm_runtime_set_active(dev);
444 pm_suspend_ignore_children(dev, false);
445 if (driver->supports_autosuspend)
446 pm_runtime_enable(dev);
448 /* if interface was already added, bind now; else let
449 * the future device_add() bind it, bypassing probe()
451 if (device_is_registered(dev))
452 retval = device_bind_driver(dev);
454 return retval;
456 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
459 * usb_driver_release_interface - unbind a driver from an interface
460 * @driver: the driver to be unbound
461 * @iface: the interface from which it will be unbound
463 * This can be used by drivers to release an interface without waiting
464 * for their disconnect() methods to be called. In typical cases this
465 * also causes the driver disconnect() method to be called.
467 * This call is synchronous, and may not be used in an interrupt context.
468 * Callers must own the device lock, so driver disconnect() entries don't
469 * need extra locking, but other call contexts may need to explicitly claim
470 * that lock.
472 void usb_driver_release_interface(struct usb_driver *driver,
473 struct usb_interface *iface)
475 struct device *dev = &iface->dev;
477 /* this should never happen, don't release something that's not ours */
478 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
479 return;
481 /* don't release from within disconnect() */
482 if (iface->condition != USB_INTERFACE_BOUND)
483 return;
484 iface->condition = USB_INTERFACE_UNBINDING;
486 /* Release via the driver core only if the interface
487 * has already been registered
489 if (device_is_registered(dev)) {
490 device_release_driver(dev);
491 } else {
492 device_lock(dev);
493 usb_unbind_interface(dev);
494 dev->driver = NULL;
495 device_unlock(dev);
498 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
500 /* returns 0 if no match, 1 if match */
501 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
503 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
504 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
505 return 0;
507 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
508 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
509 return 0;
511 /* No need to test id->bcdDevice_lo != 0, since 0 is never
512 greater than any unsigned number. */
513 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
514 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
515 return 0;
517 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
518 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
519 return 0;
521 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
522 (id->bDeviceClass != dev->descriptor.bDeviceClass))
523 return 0;
525 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
526 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
527 return 0;
529 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
530 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
531 return 0;
533 return 1;
536 /* returns 0 if no match, 1 if match */
537 int usb_match_one_id(struct usb_interface *interface,
538 const struct usb_device_id *id)
540 struct usb_host_interface *intf;
541 struct usb_device *dev;
543 /* proc_connectinfo in devio.c may call us with id == NULL. */
544 if (id == NULL)
545 return 0;
547 intf = interface->cur_altsetting;
548 dev = interface_to_usbdev(interface);
550 if (!usb_match_device(dev, id))
551 return 0;
553 /* The interface class, subclass, and protocol should never be
554 * checked for a match if the device class is Vendor Specific,
555 * unless the match record specifies the Vendor ID. */
556 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
557 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
558 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
559 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
560 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
561 return 0;
563 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
564 (id->bInterfaceClass != intf->desc.bInterfaceClass))
565 return 0;
567 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
568 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
569 return 0;
571 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
572 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
573 return 0;
575 return 1;
577 EXPORT_SYMBOL_GPL(usb_match_one_id);
580 * usb_match_id - find first usb_device_id matching device or interface
581 * @interface: the interface of interest
582 * @id: array of usb_device_id structures, terminated by zero entry
584 * usb_match_id searches an array of usb_device_id's and returns
585 * the first one matching the device or interface, or null.
586 * This is used when binding (or rebinding) a driver to an interface.
587 * Most USB device drivers will use this indirectly, through the usb core,
588 * but some layered driver frameworks use it directly.
589 * These device tables are exported with MODULE_DEVICE_TABLE, through
590 * modutils, to support the driver loading functionality of USB hotplugging.
592 * What Matches:
594 * The "match_flags" element in a usb_device_id controls which
595 * members are used. If the corresponding bit is set, the
596 * value in the device_id must match its corresponding member
597 * in the device or interface descriptor, or else the device_id
598 * does not match.
600 * "driver_info" is normally used only by device drivers,
601 * but you can create a wildcard "matches anything" usb_device_id
602 * as a driver's "modules.usbmap" entry if you provide an id with
603 * only a nonzero "driver_info" field. If you do this, the USB device
604 * driver's probe() routine should use additional intelligence to
605 * decide whether to bind to the specified interface.
607 * What Makes Good usb_device_id Tables:
609 * The match algorithm is very simple, so that intelligence in
610 * driver selection must come from smart driver id records.
611 * Unless you have good reasons to use another selection policy,
612 * provide match elements only in related groups, and order match
613 * specifiers from specific to general. Use the macros provided
614 * for that purpose if you can.
616 * The most specific match specifiers use device descriptor
617 * data. These are commonly used with product-specific matches;
618 * the USB_DEVICE macro lets you provide vendor and product IDs,
619 * and you can also match against ranges of product revisions.
620 * These are widely used for devices with application or vendor
621 * specific bDeviceClass values.
623 * Matches based on device class/subclass/protocol specifications
624 * are slightly more general; use the USB_DEVICE_INFO macro, or
625 * its siblings. These are used with single-function devices
626 * where bDeviceClass doesn't specify that each interface has
627 * its own class.
629 * Matches based on interface class/subclass/protocol are the
630 * most general; they let drivers bind to any interface on a
631 * multiple-function device. Use the USB_INTERFACE_INFO
632 * macro, or its siblings, to match class-per-interface style
633 * devices (as recorded in bInterfaceClass).
635 * Note that an entry created by USB_INTERFACE_INFO won't match
636 * any interface if the device class is set to Vendor-Specific.
637 * This is deliberate; according to the USB spec the meanings of
638 * the interface class/subclass/protocol for these devices are also
639 * vendor-specific, and hence matching against a standard product
640 * class wouldn't work anyway. If you really want to use an
641 * interface-based match for such a device, create a match record
642 * that also specifies the vendor ID. (Unforunately there isn't a
643 * standard macro for creating records like this.)
645 * Within those groups, remember that not all combinations are
646 * meaningful. For example, don't give a product version range
647 * without vendor and product IDs; or specify a protocol without
648 * its associated class and subclass.
650 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
651 const struct usb_device_id *id)
653 /* proc_connectinfo in devio.c may call us with id == NULL. */
654 if (id == NULL)
655 return NULL;
657 /* It is important to check that id->driver_info is nonzero,
658 since an entry that is all zeroes except for a nonzero
659 id->driver_info is the way to create an entry that
660 indicates that the driver want to examine every
661 device and interface. */
662 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
663 id->bInterfaceClass || id->driver_info; id++) {
664 if (usb_match_one_id(interface, id))
665 return id;
668 return NULL;
670 EXPORT_SYMBOL_GPL(usb_match_id);
672 static int usb_device_match(struct device *dev, struct device_driver *drv)
674 /* devices and interfaces are handled separately */
675 if (is_usb_device(dev)) {
677 /* interface drivers never match devices */
678 if (!is_usb_device_driver(drv))
679 return 0;
681 /* TODO: Add real matching code */
682 return 1;
684 } else if (is_usb_interface(dev)) {
685 struct usb_interface *intf;
686 struct usb_driver *usb_drv;
687 const struct usb_device_id *id;
689 /* device drivers never match interfaces */
690 if (is_usb_device_driver(drv))
691 return 0;
693 intf = to_usb_interface(dev);
694 usb_drv = to_usb_driver(drv);
696 id = usb_match_id(intf, usb_drv->id_table);
697 if (id)
698 return 1;
700 id = usb_match_dynamic_id(intf, usb_drv);
701 if (id)
702 return 1;
705 return 0;
708 #ifdef CONFIG_HOTPLUG
709 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
711 struct usb_device *usb_dev;
713 if (is_usb_device(dev)) {
714 usb_dev = to_usb_device(dev);
715 } else if (is_usb_interface(dev)) {
716 struct usb_interface *intf = to_usb_interface(dev);
718 usb_dev = interface_to_usbdev(intf);
719 } else {
720 return 0;
723 if (usb_dev->devnum < 0) {
724 /* driver is often null here; dev_dbg() would oops */
725 pr_debug("usb %s: already deleted?\n", dev_name(dev));
726 return -ENODEV;
728 if (!usb_dev->bus) {
729 pr_debug("usb %s: bus removed?\n", dev_name(dev));
730 return -ENODEV;
733 #ifdef CONFIG_USB_DEVICEFS
734 /* If this is available, userspace programs can directly read
735 * all the device descriptors we don't tell them about. Or
736 * act as usermode drivers.
738 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
739 usb_dev->bus->busnum, usb_dev->devnum))
740 return -ENOMEM;
741 #endif
743 /* per-device configurations are common */
744 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
745 le16_to_cpu(usb_dev->descriptor.idVendor),
746 le16_to_cpu(usb_dev->descriptor.idProduct),
747 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
748 return -ENOMEM;
750 /* class-based driver binding models */
751 if (add_uevent_var(env, "TYPE=%d/%d/%d",
752 usb_dev->descriptor.bDeviceClass,
753 usb_dev->descriptor.bDeviceSubClass,
754 usb_dev->descriptor.bDeviceProtocol))
755 return -ENOMEM;
757 return 0;
760 #else
762 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
764 return -ENODEV;
766 #endif /* CONFIG_HOTPLUG */
769 * usb_register_device_driver - register a USB device (not interface) driver
770 * @new_udriver: USB operations for the device driver
771 * @owner: module owner of this driver.
773 * Registers a USB device driver with the USB core. The list of
774 * unattached devices will be rescanned whenever a new driver is
775 * added, allowing the new driver to attach to any recognized devices.
776 * Returns a negative error code on failure and 0 on success.
778 int usb_register_device_driver(struct usb_device_driver *new_udriver,
779 struct module *owner)
781 int retval = 0;
783 if (usb_disabled())
784 return -ENODEV;
786 new_udriver->drvwrap.for_devices = 1;
787 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
788 new_udriver->drvwrap.driver.bus = &usb_bus_type;
789 new_udriver->drvwrap.driver.probe = usb_probe_device;
790 new_udriver->drvwrap.driver.remove = usb_unbind_device;
791 new_udriver->drvwrap.driver.owner = owner;
793 retval = driver_register(&new_udriver->drvwrap.driver);
795 if (!retval) {
796 pr_info("%s: registered new device driver %s\n",
797 usbcore_name, new_udriver->name);
798 usbfs_update_special();
799 } else {
800 printk(KERN_ERR "%s: error %d registering device "
801 " driver %s\n",
802 usbcore_name, retval, new_udriver->name);
805 return retval;
807 EXPORT_SYMBOL_GPL(usb_register_device_driver);
810 * usb_deregister_device_driver - unregister a USB device (not interface) driver
811 * @udriver: USB operations of the device driver to unregister
812 * Context: must be able to sleep
814 * Unlinks the specified driver from the internal USB driver list.
816 void usb_deregister_device_driver(struct usb_device_driver *udriver)
818 pr_info("%s: deregistering device driver %s\n",
819 usbcore_name, udriver->name);
821 driver_unregister(&udriver->drvwrap.driver);
822 usbfs_update_special();
824 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
827 * usb_register_driver - register a USB interface driver
828 * @new_driver: USB operations for the interface driver
829 * @owner: module owner of this driver.
830 * @mod_name: module name string
832 * Registers a USB interface driver with the USB core. The list of
833 * unattached interfaces will be rescanned whenever a new driver is
834 * added, allowing the new driver to attach to any recognized interfaces.
835 * Returns a negative error code on failure and 0 on success.
837 * NOTE: if you want your driver to use the USB major number, you must call
838 * usb_register_dev() to enable that functionality. This function no longer
839 * takes care of that.
841 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
842 const char *mod_name)
844 int retval = 0;
846 if (usb_disabled())
847 return -ENODEV;
849 new_driver->drvwrap.for_devices = 0;
850 new_driver->drvwrap.driver.name = (char *) new_driver->name;
851 new_driver->drvwrap.driver.bus = &usb_bus_type;
852 new_driver->drvwrap.driver.probe = usb_probe_interface;
853 new_driver->drvwrap.driver.remove = usb_unbind_interface;
854 new_driver->drvwrap.driver.owner = owner;
855 new_driver->drvwrap.driver.mod_name = mod_name;
856 spin_lock_init(&new_driver->dynids.lock);
857 INIT_LIST_HEAD(&new_driver->dynids.list);
859 retval = driver_register(&new_driver->drvwrap.driver);
860 if (retval)
861 goto out;
863 usbfs_update_special();
865 retval = usb_create_newid_file(new_driver);
866 if (retval)
867 goto out_newid;
869 retval = usb_create_removeid_file(new_driver);
870 if (retval)
871 goto out_removeid;
873 pr_info("%s: registered new interface driver %s\n",
874 usbcore_name, new_driver->name);
876 out:
877 return retval;
879 out_removeid:
880 usb_remove_newid_file(new_driver);
881 out_newid:
882 driver_unregister(&new_driver->drvwrap.driver);
884 printk(KERN_ERR "%s: error %d registering interface "
885 " driver %s\n",
886 usbcore_name, retval, new_driver->name);
887 goto out;
889 EXPORT_SYMBOL_GPL(usb_register_driver);
892 * usb_deregister - unregister a USB interface driver
893 * @driver: USB operations of the interface driver to unregister
894 * Context: must be able to sleep
896 * Unlinks the specified driver from the internal USB driver list.
898 * NOTE: If you called usb_register_dev(), you still need to call
899 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
900 * this * call will no longer do it for you.
902 void usb_deregister(struct usb_driver *driver)
904 pr_info("%s: deregistering interface driver %s\n",
905 usbcore_name, driver->name);
907 usb_remove_removeid_file(driver);
908 usb_remove_newid_file(driver);
909 usb_free_dynids(driver);
910 driver_unregister(&driver->drvwrap.driver);
912 usbfs_update_special();
914 EXPORT_SYMBOL_GPL(usb_deregister);
916 /* Forced unbinding of a USB interface driver, either because
917 * it doesn't support pre_reset/post_reset/reset_resume or
918 * because it doesn't support suspend/resume.
920 * The caller must hold @intf's device's lock, but not its pm_mutex
921 * and not @intf->dev.sem.
923 void usb_forced_unbind_intf(struct usb_interface *intf)
925 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
927 dev_dbg(&intf->dev, "forced unbind\n");
928 usb_driver_release_interface(driver, intf);
930 /* Mark the interface for later rebinding */
931 intf->needs_binding = 1;
934 /* Delayed forced unbinding of a USB interface driver and scan
935 * for rebinding.
937 * The caller must hold @intf's device's lock, but not its pm_mutex
938 * and not @intf->dev.sem.
940 * Note: Rebinds will be skipped if a system sleep transition is in
941 * progress and the PM "complete" callback hasn't occurred yet.
943 void usb_rebind_intf(struct usb_interface *intf)
945 int rc;
947 /* Delayed unbind of an existing driver */
948 if (intf->dev.driver) {
949 struct usb_driver *driver =
950 to_usb_driver(intf->dev.driver);
952 dev_dbg(&intf->dev, "forced unbind\n");
953 usb_driver_release_interface(driver, intf);
956 /* Try to rebind the interface */
957 if (intf->dev.power.status == DPM_ON) {
958 intf->needs_binding = 0;
959 rc = device_attach(&intf->dev);
960 if (rc < 0)
961 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
965 #ifdef CONFIG_PM
967 #define DO_UNBIND 0
968 #define DO_REBIND 1
970 /* Unbind drivers for @udev's interfaces that don't support suspend/resume,
971 * or rebind interfaces that have been unbound, according to @action.
973 * The caller must hold @udev's device lock.
975 static void do_unbind_rebind(struct usb_device *udev, int action)
977 struct usb_host_config *config;
978 int i;
979 struct usb_interface *intf;
980 struct usb_driver *drv;
982 config = udev->actconfig;
983 if (config) {
984 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
985 intf = config->interface[i];
986 switch (action) {
987 case DO_UNBIND:
988 if (intf->dev.driver) {
989 drv = to_usb_driver(intf->dev.driver);
990 if (!drv->suspend || !drv->resume)
991 usb_forced_unbind_intf(intf);
993 break;
994 case DO_REBIND:
995 if (intf->needs_binding)
996 usb_rebind_intf(intf);
997 break;
1003 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1005 struct usb_device_driver *udriver;
1006 int status = 0;
1008 if (udev->state == USB_STATE_NOTATTACHED ||
1009 udev->state == USB_STATE_SUSPENDED)
1010 goto done;
1012 /* For devices that don't have a driver, we do a generic suspend. */
1013 if (udev->dev.driver)
1014 udriver = to_usb_device_driver(udev->dev.driver);
1015 else {
1016 udev->do_remote_wakeup = 0;
1017 udriver = &usb_generic_driver;
1019 status = udriver->suspend(udev, msg);
1021 done:
1022 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1023 return status;
1026 static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1028 struct usb_device_driver *udriver;
1029 int status = 0;
1031 if (udev->state == USB_STATE_NOTATTACHED)
1032 goto done;
1034 /* Can't resume it if it doesn't have a driver. */
1035 if (udev->dev.driver == NULL) {
1036 status = -ENOTCONN;
1037 goto done;
1040 /* Non-root devices on a full/low-speed bus must wait for their
1041 * companion high-speed root hub, in case a handoff is needed.
1043 if (!(msg.event & PM_EVENT_AUTO) && udev->parent &&
1044 udev->bus->hs_companion)
1045 device_pm_wait_for_dev(&udev->dev,
1046 &udev->bus->hs_companion->root_hub->dev);
1048 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1049 udev->reset_resume = 1;
1051 udriver = to_usb_device_driver(udev->dev.driver);
1052 status = udriver->resume(udev, msg);
1054 done:
1055 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1056 return status;
1059 static int usb_suspend_interface(struct usb_device *udev,
1060 struct usb_interface *intf, pm_message_t msg)
1062 struct usb_driver *driver;
1063 int status = 0;
1065 if (udev->state == USB_STATE_NOTATTACHED ||
1066 intf->condition == USB_INTERFACE_UNBOUND)
1067 goto done;
1068 driver = to_usb_driver(intf->dev.driver);
1070 if (driver->suspend) {
1071 status = driver->suspend(intf, msg);
1072 if (status && !(msg.event & PM_EVENT_AUTO))
1073 dev_err(&intf->dev, "%s error %d\n",
1074 "suspend", status);
1075 } else {
1076 /* Later we will unbind the driver and reprobe */
1077 intf->needs_binding = 1;
1078 dev_warn(&intf->dev, "no %s for driver %s?\n",
1079 "suspend", driver->name);
1082 done:
1083 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1084 return status;
1087 static int usb_resume_interface(struct usb_device *udev,
1088 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1090 struct usb_driver *driver;
1091 int status = 0;
1093 if (udev->state == USB_STATE_NOTATTACHED)
1094 goto done;
1096 /* Don't let autoresume interfere with unbinding */
1097 if (intf->condition == USB_INTERFACE_UNBINDING)
1098 goto done;
1100 /* Can't resume it if it doesn't have a driver. */
1101 if (intf->condition == USB_INTERFACE_UNBOUND) {
1103 /* Carry out a deferred switch to altsetting 0 */
1104 if (intf->needs_altsetting0 &&
1105 intf->dev.power.status == DPM_ON) {
1106 usb_set_interface(udev, intf->altsetting[0].
1107 desc.bInterfaceNumber, 0);
1108 intf->needs_altsetting0 = 0;
1110 goto done;
1113 /* Don't resume if the interface is marked for rebinding */
1114 if (intf->needs_binding)
1115 goto done;
1116 driver = to_usb_driver(intf->dev.driver);
1118 if (reset_resume) {
1119 if (driver->reset_resume) {
1120 status = driver->reset_resume(intf);
1121 if (status)
1122 dev_err(&intf->dev, "%s error %d\n",
1123 "reset_resume", status);
1124 } else {
1125 intf->needs_binding = 1;
1126 dev_warn(&intf->dev, "no %s for driver %s?\n",
1127 "reset_resume", driver->name);
1129 } else {
1130 if (driver->resume) {
1131 status = driver->resume(intf);
1132 if (status)
1133 dev_err(&intf->dev, "%s error %d\n",
1134 "resume", status);
1135 } else {
1136 intf->needs_binding = 1;
1137 dev_warn(&intf->dev, "no %s for driver %s?\n",
1138 "resume", driver->name);
1142 done:
1143 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1145 /* Later we will unbind the driver and/or reprobe, if necessary */
1146 return status;
1150 * usb_suspend_both - suspend a USB device and its interfaces
1151 * @udev: the usb_device to suspend
1152 * @msg: Power Management message describing this state transition
1154 * This is the central routine for suspending USB devices. It calls the
1155 * suspend methods for all the interface drivers in @udev and then calls
1156 * the suspend method for @udev itself. If an error occurs at any stage,
1157 * all the interfaces which were suspended are resumed so that they remain
1158 * in the same state as the device.
1160 * Autosuspend requests originating from a child device or an interface
1161 * driver may be made without the protection of @udev's device lock, but
1162 * all other suspend calls will hold the lock. Usbcore will insure that
1163 * method calls do not arrive during bind, unbind, or reset operations.
1164 * However drivers must be prepared to handle suspend calls arriving at
1165 * unpredictable times.
1167 * This routine can run only in process context.
1169 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1171 int status = 0;
1172 int i = 0;
1173 struct usb_interface *intf;
1175 if (udev->state == USB_STATE_NOTATTACHED ||
1176 udev->state == USB_STATE_SUSPENDED)
1177 goto done;
1179 /* Suspend all the interfaces and then udev itself */
1180 if (udev->actconfig) {
1181 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1182 intf = udev->actconfig->interface[i];
1183 status = usb_suspend_interface(udev, intf, msg);
1184 if (status != 0)
1185 break;
1188 if (status == 0)
1189 status = usb_suspend_device(udev, msg);
1191 /* If the suspend failed, resume interfaces that did get suspended */
1192 if (status != 0) {
1193 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1194 while (--i >= 0) {
1195 intf = udev->actconfig->interface[i];
1196 usb_resume_interface(udev, intf, msg, 0);
1199 /* If the suspend succeeded then prevent any more URB submissions
1200 * and flush any outstanding URBs.
1202 } else {
1203 udev->can_submit = 0;
1204 for (i = 0; i < 16; ++i) {
1205 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1206 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1210 done:
1211 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1212 return status;
1216 * usb_resume_both - resume a USB device and its interfaces
1217 * @udev: the usb_device to resume
1218 * @msg: Power Management message describing this state transition
1220 * This is the central routine for resuming USB devices. It calls the
1221 * the resume method for @udev and then calls the resume methods for all
1222 * the interface drivers in @udev.
1224 * Autoresume requests originating from a child device or an interface
1225 * driver may be made without the protection of @udev's device lock, but
1226 * all other resume calls will hold the lock. Usbcore will insure that
1227 * method calls do not arrive during bind, unbind, or reset operations.
1228 * However drivers must be prepared to handle resume calls arriving at
1229 * unpredictable times.
1231 * This routine can run only in process context.
1233 static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1235 int status = 0;
1236 int i;
1237 struct usb_interface *intf;
1239 if (udev->state == USB_STATE_NOTATTACHED) {
1240 status = -ENODEV;
1241 goto done;
1243 udev->can_submit = 1;
1245 /* Resume the device */
1246 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1247 status = usb_resume_device(udev, msg);
1249 /* Resume the interfaces */
1250 if (status == 0 && udev->actconfig) {
1251 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1252 intf = udev->actconfig->interface[i];
1253 usb_resume_interface(udev, intf, msg,
1254 udev->reset_resume);
1258 done:
1259 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1260 if (!status)
1261 udev->reset_resume = 0;
1262 return status;
1265 /* The device lock is held by the PM core */
1266 int usb_suspend(struct device *dev, pm_message_t msg)
1268 struct usb_device *udev = to_usb_device(dev);
1270 do_unbind_rebind(udev, DO_UNBIND);
1271 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1272 return usb_suspend_both(udev, msg);
1275 /* The device lock is held by the PM core */
1276 int usb_resume(struct device *dev, pm_message_t msg)
1278 struct usb_device *udev = to_usb_device(dev);
1279 int status;
1281 /* For PM complete calls, all we do is rebind interfaces */
1282 if (msg.event == PM_EVENT_ON) {
1283 if (udev->state != USB_STATE_NOTATTACHED)
1284 do_unbind_rebind(udev, DO_REBIND);
1285 status = 0;
1287 /* For all other calls, take the device back to full power and
1288 * tell the PM core in case it was autosuspended previously.
1290 } else {
1291 status = usb_resume_both(udev, msg);
1292 if (status == 0) {
1293 pm_runtime_disable(dev);
1294 pm_runtime_set_active(dev);
1295 pm_runtime_enable(dev);
1296 udev->last_busy = jiffies;
1300 /* Avoid PM error messages for devices disconnected while suspended
1301 * as we'll display regular disconnect messages just a bit later.
1303 if (status == -ENODEV)
1304 status = 0;
1305 return status;
1308 #endif /* CONFIG_PM */
1310 #ifdef CONFIG_USB_SUSPEND
1313 * usb_enable_autosuspend - allow a USB device to be autosuspended
1314 * @udev: the USB device which may be autosuspended
1316 * This routine allows @udev to be autosuspended. An autosuspend won't
1317 * take place until the autosuspend_delay has elapsed and all the other
1318 * necessary conditions are satisfied.
1320 * The caller must hold @udev's device lock.
1322 int usb_enable_autosuspend(struct usb_device *udev)
1324 if (udev->autosuspend_disabled) {
1325 udev->autosuspend_disabled = 0;
1326 usb_autosuspend_device(udev);
1328 return 0;
1330 EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1333 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1334 * @udev: the USB device which may not be autosuspended
1336 * This routine prevents @udev from being autosuspended and wakes it up
1337 * if it is already autosuspended.
1339 * The caller must hold @udev's device lock.
1341 int usb_disable_autosuspend(struct usb_device *udev)
1343 int rc = 0;
1345 if (!udev->autosuspend_disabled) {
1346 rc = usb_autoresume_device(udev);
1347 if (rc == 0)
1348 udev->autosuspend_disabled = 1;
1350 return rc;
1352 EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1355 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1356 * @udev: the usb_device to autosuspend
1358 * This routine should be called when a core subsystem is finished using
1359 * @udev and wants to allow it to autosuspend. Examples would be when
1360 * @udev's device file in usbfs is closed or after a configuration change.
1362 * @udev's usage counter is decremented; if it drops to 0 and all the
1363 * interfaces are inactive then a delayed autosuspend will be attempted.
1364 * The attempt may fail (see autosuspend_check()).
1366 * The caller must hold @udev's device lock.
1368 * This routine can run only in process context.
1370 void usb_autosuspend_device(struct usb_device *udev)
1372 int status;
1374 udev->last_busy = jiffies;
1375 status = pm_runtime_put_sync(&udev->dev);
1376 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1377 __func__, atomic_read(&udev->dev.power.usage_count),
1378 status);
1382 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1383 * @udev: the usb_device to autosuspend
1385 * This routine should be called when a core subsystem thinks @udev may
1386 * be ready to autosuspend.
1388 * @udev's usage counter left unchanged. If it is 0 and all the interfaces
1389 * are inactive then an autosuspend will be attempted. The attempt may
1390 * fail or be delayed.
1392 * The caller must hold @udev's device lock.
1394 * This routine can run only in process context.
1396 void usb_try_autosuspend_device(struct usb_device *udev)
1398 int status;
1400 status = pm_runtime_idle(&udev->dev);
1401 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1402 __func__, atomic_read(&udev->dev.power.usage_count),
1403 status);
1407 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1408 * @udev: the usb_device to autoresume
1410 * This routine should be called when a core subsystem wants to use @udev
1411 * and needs to guarantee that it is not suspended. No autosuspend will
1412 * occur until usb_autosuspend_device() is called. (Note that this will
1413 * not prevent suspend events originating in the PM core.) Examples would
1414 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1415 * request is received.
1417 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1418 * However if the autoresume fails then the usage counter is re-decremented.
1420 * The caller must hold @udev's device lock.
1422 * This routine can run only in process context.
1424 int usb_autoresume_device(struct usb_device *udev)
1426 int status;
1428 status = pm_runtime_get_sync(&udev->dev);
1429 if (status < 0)
1430 pm_runtime_put_sync(&udev->dev);
1431 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1432 __func__, atomic_read(&udev->dev.power.usage_count),
1433 status);
1434 if (status > 0)
1435 status = 0;
1436 return status;
1440 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1441 * @intf: the usb_interface whose counter should be decremented
1443 * This routine should be called by an interface driver when it is
1444 * finished using @intf and wants to allow it to autosuspend. A typical
1445 * example would be a character-device driver when its device file is
1446 * closed.
1448 * The routine decrements @intf's usage counter. When the counter reaches
1449 * 0, a delayed autosuspend request for @intf's device is attempted. The
1450 * attempt may fail (see autosuspend_check()).
1452 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1453 * take place only if the device's remote-wakeup facility is enabled.
1455 * This routine can run only in process context.
1457 void usb_autopm_put_interface(struct usb_interface *intf)
1459 struct usb_device *udev = interface_to_usbdev(intf);
1460 int status;
1462 udev->last_busy = jiffies;
1463 atomic_dec(&intf->pm_usage_cnt);
1464 status = pm_runtime_put_sync(&intf->dev);
1465 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1466 __func__, atomic_read(&intf->dev.power.usage_count),
1467 status);
1469 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1472 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1473 * @intf: the usb_interface whose counter should be decremented
1475 * This routine does much the same thing as usb_autopm_put_interface():
1476 * It decrements @intf's usage counter and schedules a delayed
1477 * autosuspend request if the counter is <= 0. The difference is that it
1478 * does not perform any synchronization; callers should hold a private
1479 * lock and handle all synchronization issues themselves.
1481 * Typically a driver would call this routine during an URB's completion
1482 * handler, if no more URBs were pending.
1484 * This routine can run in atomic context.
1486 void usb_autopm_put_interface_async(struct usb_interface *intf)
1488 struct usb_device *udev = interface_to_usbdev(intf);
1489 unsigned long last_busy;
1490 int status = 0;
1492 last_busy = udev->last_busy;
1493 udev->last_busy = jiffies;
1494 atomic_dec(&intf->pm_usage_cnt);
1495 pm_runtime_put_noidle(&intf->dev);
1497 if (!udev->autosuspend_disabled) {
1498 /* Optimization: Don't schedule a delayed autosuspend if
1499 * the timer is already running and the expiration time
1500 * wouldn't change.
1502 * We have to use the interface's timer. Attempts to
1503 * schedule a suspend for the device would fail because
1504 * the interface is still active.
1506 if (intf->dev.power.timer_expires == 0 ||
1507 round_jiffies_up(last_busy) !=
1508 round_jiffies_up(jiffies)) {
1509 status = pm_schedule_suspend(&intf->dev,
1510 jiffies_to_msecs(
1511 round_jiffies_up_relative(
1512 udev->autosuspend_delay)));
1515 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1516 __func__, atomic_read(&intf->dev.power.usage_count),
1517 status);
1519 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1522 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1523 * @intf: the usb_interface whose counter should be decremented
1525 * This routine decrements @intf's usage counter but does not carry out an
1526 * autosuspend.
1528 * This routine can run in atomic context.
1530 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1532 struct usb_device *udev = interface_to_usbdev(intf);
1534 udev->last_busy = jiffies;
1535 atomic_dec(&intf->pm_usage_cnt);
1536 pm_runtime_put_noidle(&intf->dev);
1538 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1541 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1542 * @intf: the usb_interface whose counter should be incremented
1544 * This routine should be called by an interface driver when it wants to
1545 * use @intf and needs to guarantee that it is not suspended. In addition,
1546 * the routine prevents @intf from being autosuspended subsequently. (Note
1547 * that this will not prevent suspend events originating in the PM core.)
1548 * This prevention will persist until usb_autopm_put_interface() is called
1549 * or @intf is unbound. A typical example would be a character-device
1550 * driver when its device file is opened.
1552 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1553 * However if the autoresume fails then the counter is re-decremented.
1555 * This routine can run only in process context.
1557 int usb_autopm_get_interface(struct usb_interface *intf)
1559 int status;
1561 status = pm_runtime_get_sync(&intf->dev);
1562 if (status < 0)
1563 pm_runtime_put_sync(&intf->dev);
1564 else
1565 atomic_inc(&intf->pm_usage_cnt);
1566 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1567 __func__, atomic_read(&intf->dev.power.usage_count),
1568 status);
1569 if (status > 0)
1570 status = 0;
1571 return status;
1573 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1576 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1577 * @intf: the usb_interface whose counter should be incremented
1579 * This routine does much the same thing as
1580 * usb_autopm_get_interface(): It increments @intf's usage counter and
1581 * queues an autoresume request if the device is suspended. The
1582 * differences are that it does not perform any synchronization (callers
1583 * should hold a private lock and handle all synchronization issues
1584 * themselves), and it does not autoresume the device directly (it only
1585 * queues a request). After a successful call, the device may not yet be
1586 * resumed.
1588 * This routine can run in atomic context.
1590 int usb_autopm_get_interface_async(struct usb_interface *intf)
1592 int status = 0;
1593 enum rpm_status s;
1595 /* Don't request a resume unless the interface is already suspending
1596 * or suspended. Doing so would force a running suspend timer to be
1597 * cancelled.
1599 pm_runtime_get_noresume(&intf->dev);
1600 s = ACCESS_ONCE(intf->dev.power.runtime_status);
1601 if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
1602 status = pm_request_resume(&intf->dev);
1604 if (status < 0 && status != -EINPROGRESS)
1605 pm_runtime_put_noidle(&intf->dev);
1606 else
1607 atomic_inc(&intf->pm_usage_cnt);
1608 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1609 __func__, atomic_read(&intf->dev.power.usage_count),
1610 status);
1611 if (status > 0)
1612 status = 0;
1613 return status;
1615 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1618 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1619 * @intf: the usb_interface whose counter should be incremented
1621 * This routine increments @intf's usage counter but does not carry out an
1622 * autoresume.
1624 * This routine can run in atomic context.
1626 void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1628 struct usb_device *udev = interface_to_usbdev(intf);
1630 udev->last_busy = jiffies;
1631 atomic_inc(&intf->pm_usage_cnt);
1632 pm_runtime_get_noresume(&intf->dev);
1634 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1636 /* Internal routine to check whether we may autosuspend a device. */
1637 static int autosuspend_check(struct usb_device *udev)
1639 int i;
1640 struct usb_interface *intf;
1641 unsigned long suspend_time, j;
1643 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1644 * any interface drivers require remote wakeup but it isn't available.
1646 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1647 if (udev->actconfig) {
1648 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1649 intf = udev->actconfig->interface[i];
1651 /* We don't need to check interfaces that are
1652 * disabled for runtime PM. Either they are unbound
1653 * or else their drivers don't support autosuspend
1654 * and so they are permanently active.
1656 if (intf->dev.power.disable_depth)
1657 continue;
1658 if (atomic_read(&intf->dev.power.usage_count) > 0)
1659 return -EBUSY;
1660 if (intf->needs_remote_wakeup &&
1661 !udev->do_remote_wakeup) {
1662 dev_dbg(&udev->dev, "remote wakeup needed "
1663 "for autosuspend\n");
1664 return -EOPNOTSUPP;
1667 /* Don't allow autosuspend if the device will need
1668 * a reset-resume and any of its interface drivers
1669 * doesn't include support or needs remote wakeup.
1671 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1672 struct usb_driver *driver;
1674 driver = to_usb_driver(intf->dev.driver);
1675 if (!driver->reset_resume ||
1676 intf->needs_remote_wakeup)
1677 return -EOPNOTSUPP;
1682 /* If everything is okay but the device hasn't been idle for long
1683 * enough, queue a delayed autosuspend request.
1685 j = ACCESS_ONCE(jiffies);
1686 suspend_time = udev->last_busy + udev->autosuspend_delay;
1687 if (time_before(j, suspend_time)) {
1688 pm_schedule_suspend(&udev->dev, jiffies_to_msecs(
1689 round_jiffies_up_relative(suspend_time - j)));
1690 return -EAGAIN;
1692 return 0;
1695 static int usb_runtime_suspend(struct device *dev)
1697 int status = 0;
1699 /* A USB device can be suspended if it passes the various autosuspend
1700 * checks. Runtime suspend for a USB device means suspending all the
1701 * interfaces and then the device itself.
1703 if (is_usb_device(dev)) {
1704 struct usb_device *udev = to_usb_device(dev);
1706 if (autosuspend_check(udev) != 0)
1707 return -EAGAIN;
1709 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1711 /* If an interface fails the suspend, adjust the last_busy
1712 * time so that we don't get another suspend attempt right
1713 * away.
1715 if (status) {
1716 udev->last_busy = jiffies +
1717 (udev->autosuspend_delay == 0 ?
1718 HZ/2 : 0);
1721 /* Prevent the parent from suspending immediately after */
1722 else if (udev->parent) {
1723 udev->parent->last_busy = jiffies;
1727 /* Runtime suspend for a USB interface doesn't mean anything. */
1728 return status;
1731 static int usb_runtime_resume(struct device *dev)
1733 /* Runtime resume for a USB device means resuming both the device
1734 * and all its interfaces.
1736 if (is_usb_device(dev)) {
1737 struct usb_device *udev = to_usb_device(dev);
1738 int status;
1740 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1741 udev->last_busy = jiffies;
1742 return status;
1745 /* Runtime resume for a USB interface doesn't mean anything. */
1746 return 0;
1749 static int usb_runtime_idle(struct device *dev)
1751 /* An idle USB device can be suspended if it passes the various
1752 * autosuspend checks. An idle interface can be suspended at
1753 * any time.
1755 if (is_usb_device(dev)) {
1756 struct usb_device *udev = to_usb_device(dev);
1758 if (autosuspend_check(udev) != 0)
1759 return 0;
1762 pm_runtime_suspend(dev);
1763 return 0;
1766 static struct dev_pm_ops usb_bus_pm_ops = {
1767 .runtime_suspend = usb_runtime_suspend,
1768 .runtime_resume = usb_runtime_resume,
1769 .runtime_idle = usb_runtime_idle,
1772 #else
1774 #define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
1776 #endif /* CONFIG_USB_SUSPEND */
1778 struct bus_type usb_bus_type = {
1779 .name = "usb",
1780 .match = usb_device_match,
1781 .uevent = usb_uevent,
1782 .pm = &usb_bus_pm_ops,