K2.6 patches and update.
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / usb / core / driver.c
blob7097ae88bc48c63612af4967e4d6f59d515cd6c2
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/workqueue.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 static int usb_create_newid_file(struct usb_driver *usb_drv)
88 int error = 0;
90 if (usb_drv->no_dynamic_id)
91 goto exit;
93 if (usb_drv->probe != NULL)
94 error = driver_create_file(&usb_drv->drvwrap.driver,
95 &driver_attr_new_id);
96 exit:
97 return error;
100 static void usb_remove_newid_file(struct usb_driver *usb_drv)
102 if (usb_drv->no_dynamic_id)
103 return;
105 if (usb_drv->probe != NULL)
106 driver_remove_file(&usb_drv->drvwrap.driver,
107 &driver_attr_new_id);
110 static void usb_free_dynids(struct usb_driver *usb_drv)
112 struct usb_dynid *dynid, *n;
114 spin_lock(&usb_drv->dynids.lock);
115 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
116 list_del(&dynid->node);
117 kfree(dynid);
119 spin_unlock(&usb_drv->dynids.lock);
121 #else
122 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
124 return 0;
127 static void usb_remove_newid_file(struct usb_driver *usb_drv)
131 static inline void usb_free_dynids(struct usb_driver *usb_drv)
134 #endif
136 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
137 struct usb_driver *drv)
139 struct usb_dynid *dynid;
141 spin_lock(&drv->dynids.lock);
142 list_for_each_entry(dynid, &drv->dynids.list, node) {
143 if (usb_match_one_id(intf, &dynid->id)) {
144 spin_unlock(&drv->dynids.lock);
145 return &dynid->id;
148 spin_unlock(&drv->dynids.lock);
149 return NULL;
153 /* called from driver core with dev locked */
154 static int usb_probe_device(struct device *dev)
156 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
157 struct usb_device *udev = to_usb_device(dev);
158 int error = -ENODEV;
160 dev_dbg(dev, "%s\n", __FUNCTION__);
162 /* TODO: Add real matching code */
164 /* The device should always appear to be in use
165 * unless the driver suports autosuspend.
167 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
169 error = udriver->probe(udev);
170 return error;
173 /* called from driver core with dev locked */
174 static int usb_unbind_device(struct device *dev)
176 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
178 udriver->disconnect(to_usb_device(dev));
179 return 0;
183 * Cancel any pending scheduled resets
185 * [see usb_queue_reset_device()]
187 * Called after unconfiguring / when releasing interfaces. See
188 * comments in __usb_queue_reset_device() regarding
189 * udev->reset_running.
191 static void usb_cancel_queued_reset(struct usb_interface *iface)
193 if (iface->reset_running == 0)
194 cancel_work_sync(&iface->reset_ws);
197 /* called from driver core with dev locked */
198 static int usb_probe_interface(struct device *dev)
200 struct usb_driver *driver = to_usb_driver(dev->driver);
201 struct usb_interface *intf = to_usb_interface(dev);
202 struct usb_device *udev = interface_to_usbdev(intf);
203 const struct usb_device_id *id;
204 int error = -ENODEV;
206 dev_dbg(dev, "%s\n", __FUNCTION__);
208 intf->needs_binding = 0;
210 id = usb_match_id(intf, driver->id_table);
211 if (!id)
212 id = usb_match_dynamic_id(intf, driver);
213 if (id) {
214 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
216 error = usb_autoresume_device(udev);
217 if (error)
218 return error;
220 /* Interface "power state" doesn't correspond to any hardware
221 * state whatsoever. We use it to record when it's bound to
222 * a driver that may start I/0: it's not frozen/quiesced.
224 mark_active(intf);
225 intf->condition = USB_INTERFACE_BINDING;
227 /* The interface should always appear to be in use
228 * unless the driver suports autosuspend.
230 intf->pm_usage_cnt = !(driver->supports_autosuspend);
232 error = driver->probe(intf, id);
233 if (error) {
234 mark_quiesced(intf);
235 intf->needs_remote_wakeup = 0;
236 intf->condition = USB_INTERFACE_UNBOUND;
237 usb_cancel_queued_reset(intf);
238 } else
239 intf->condition = USB_INTERFACE_BOUND;
241 usb_autosuspend_device(udev);
244 return error;
247 /* called from driver core with dev locked */
248 static int usb_unbind_interface(struct device *dev)
250 struct usb_driver *driver = to_usb_driver(dev->driver);
251 struct usb_interface *intf = to_usb_interface(dev);
252 struct usb_device *udev;
253 int error;
255 intf->condition = USB_INTERFACE_UNBINDING;
257 /* Autoresume for set_interface call below */
258 udev = interface_to_usbdev(intf);
259 error = usb_autoresume_device(udev);
261 /* Terminate all URBs for this interface unless the driver
262 * supports "soft" unbinding.
264 if (!driver->soft_unbind)
265 usb_disable_interface(udev, intf);
267 driver->disconnect(intf);
268 usb_cancel_queued_reset(intf);
270 /* reset other interface state */
271 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
272 usb_set_intfdata(intf, NULL);
274 intf->condition = USB_INTERFACE_UNBOUND;
275 mark_quiesced(intf);
276 intf->needs_remote_wakeup = 0;
278 if (!error)
279 usb_autosuspend_device(udev);
281 return 0;
285 * usb_driver_claim_interface - bind a driver to an interface
286 * @driver: the driver to be bound
287 * @iface: the interface to which it will be bound; must be in the
288 * usb device's active configuration
289 * @priv: driver data associated with that interface
291 * This is used by usb device drivers that need to claim more than one
292 * interface on a device when probing (audio and acm are current examples).
293 * No device driver should directly modify internal usb_interface or
294 * usb_device structure members.
296 * Few drivers should need to use this routine, since the most natural
297 * way to bind to an interface is to return the private data from
298 * the driver's probe() method.
300 * Callers must own the device lock, so driver probe() entries don't need
301 * extra locking, but other call contexts may need to explicitly claim that
302 * lock.
304 int usb_driver_claim_interface(struct usb_driver *driver,
305 struct usb_interface *iface, void *priv)
307 struct device *dev = &iface->dev;
308 struct usb_device *udev = interface_to_usbdev(iface);
309 int retval = 0;
311 if (dev->driver)
312 return -EBUSY;
314 dev->driver = &driver->drvwrap.driver;
315 usb_set_intfdata(iface, priv);
316 iface->needs_binding = 0;
318 usb_pm_lock(udev);
319 iface->condition = USB_INTERFACE_BOUND;
320 mark_active(iface);
321 iface->pm_usage_cnt = !(driver->supports_autosuspend);
322 usb_pm_unlock(udev);
324 /* if interface was already added, bind now; else let
325 * the future device_add() bind it, bypassing probe()
327 if (device_is_registered(dev))
328 retval = device_bind_driver(dev);
330 return retval;
332 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
335 * usb_driver_release_interface - unbind a driver from an interface
336 * @driver: the driver to be unbound
337 * @iface: the interface from which it will be unbound
339 * This can be used by drivers to release an interface without waiting
340 * for their disconnect() methods to be called. In typical cases this
341 * also causes the driver disconnect() method to be called.
343 * This call is synchronous, and may not be used in an interrupt context.
344 * Callers must own the device lock, so driver disconnect() entries don't
345 * need extra locking, but other call contexts may need to explicitly claim
346 * that lock.
348 void usb_driver_release_interface(struct usb_driver *driver,
349 struct usb_interface *iface)
351 struct device *dev = &iface->dev;
353 /* this should never happen, don't release something that's not ours */
354 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
355 return;
357 /* don't release from within disconnect() */
358 if (iface->condition != USB_INTERFACE_BOUND)
359 return;
360 iface->condition = USB_INTERFACE_UNBINDING;
362 /* Release via the driver core only if the interface
363 * has already been registered
365 if (device_is_registered(dev)) {
366 device_release_driver(dev);
367 } else {
368 down(&dev->sem);
369 usb_unbind_interface(dev);
370 dev->driver = NULL;
371 up(&dev->sem);
374 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
376 /* returns 0 if no match, 1 if match */
377 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
379 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
380 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
381 return 0;
383 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
384 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
385 return 0;
387 /* No need to test id->bcdDevice_lo != 0, since 0 is never
388 greater than any unsigned number. */
389 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
390 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
391 return 0;
393 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
394 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
395 return 0;
397 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
398 (id->bDeviceClass != dev->descriptor.bDeviceClass))
399 return 0;
401 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
402 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
403 return 0;
405 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
406 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
407 return 0;
409 return 1;
412 /* returns 0 if no match, 1 if match */
413 int usb_match_one_id(struct usb_interface *interface,
414 const struct usb_device_id *id)
416 struct usb_host_interface *intf;
417 struct usb_device *dev;
419 /* proc_connectinfo in devio.c may call us with id == NULL. */
420 if (id == NULL)
421 return 0;
423 intf = interface->cur_altsetting;
424 dev = interface_to_usbdev(interface);
426 if (!usb_match_device(dev, id))
427 return 0;
429 /* The interface class, subclass, and protocol should never be
430 * checked for a match if the device class is Vendor Specific,
431 * unless the match record specifies the Vendor ID. */
432 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
433 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
434 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
435 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
436 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
437 return 0;
439 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
440 (id->bInterfaceClass != intf->desc.bInterfaceClass))
441 return 0;
443 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
444 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
445 return 0;
447 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
448 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
449 return 0;
451 return 1;
453 EXPORT_SYMBOL_GPL(usb_match_one_id);
456 * usb_match_id - find first usb_device_id matching device or interface
457 * @interface: the interface of interest
458 * @id: array of usb_device_id structures, terminated by zero entry
460 * usb_match_id searches an array of usb_device_id's and returns
461 * the first one matching the device or interface, or null.
462 * This is used when binding (or rebinding) a driver to an interface.
463 * Most USB device drivers will use this indirectly, through the usb core,
464 * but some layered driver frameworks use it directly.
465 * These device tables are exported with MODULE_DEVICE_TABLE, through
466 * modutils, to support the driver loading functionality of USB hotplugging.
468 * What Matches:
470 * The "match_flags" element in a usb_device_id controls which
471 * members are used. If the corresponding bit is set, the
472 * value in the device_id must match its corresponding member
473 * in the device or interface descriptor, or else the device_id
474 * does not match.
476 * "driver_info" is normally used only by device drivers,
477 * but you can create a wildcard "matches anything" usb_device_id
478 * as a driver's "modules.usbmap" entry if you provide an id with
479 * only a nonzero "driver_info" field. If you do this, the USB device
480 * driver's probe() routine should use additional intelligence to
481 * decide whether to bind to the specified interface.
483 * What Makes Good usb_device_id Tables:
485 * The match algorithm is very simple, so that intelligence in
486 * driver selection must come from smart driver id records.
487 * Unless you have good reasons to use another selection policy,
488 * provide match elements only in related groups, and order match
489 * specifiers from specific to general. Use the macros provided
490 * for that purpose if you can.
492 * The most specific match specifiers use device descriptor
493 * data. These are commonly used with product-specific matches;
494 * the USB_DEVICE macro lets you provide vendor and product IDs,
495 * and you can also match against ranges of product revisions.
496 * These are widely used for devices with application or vendor
497 * specific bDeviceClass values.
499 * Matches based on device class/subclass/protocol specifications
500 * are slightly more general; use the USB_DEVICE_INFO macro, or
501 * its siblings. These are used with single-function devices
502 * where bDeviceClass doesn't specify that each interface has
503 * its own class.
505 * Matches based on interface class/subclass/protocol are the
506 * most general; they let drivers bind to any interface on a
507 * multiple-function device. Use the USB_INTERFACE_INFO
508 * macro, or its siblings, to match class-per-interface style
509 * devices (as recorded in bInterfaceClass).
511 * Note that an entry created by USB_INTERFACE_INFO won't match
512 * any interface if the device class is set to Vendor-Specific.
513 * This is deliberate; according to the USB spec the meanings of
514 * the interface class/subclass/protocol for these devices are also
515 * vendor-specific, and hence matching against a standard product
516 * class wouldn't work anyway. If you really want to use an
517 * interface-based match for such a device, create a match record
518 * that also specifies the vendor ID. (Unforunately there isn't a
519 * standard macro for creating records like this.)
521 * Within those groups, remember that not all combinations are
522 * meaningful. For example, don't give a product version range
523 * without vendor and product IDs; or specify a protocol without
524 * its associated class and subclass.
526 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
527 const struct usb_device_id *id)
529 /* proc_connectinfo in devio.c may call us with id == NULL. */
530 if (id == NULL)
531 return NULL;
533 /* It is important to check that id->driver_info is nonzero,
534 since an entry that is all zeroes except for a nonzero
535 id->driver_info is the way to create an entry that
536 indicates that the driver want to examine every
537 device and interface. */
538 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
539 id->bInterfaceClass || id->driver_info; id++) {
540 if (usb_match_one_id(interface, id))
541 return id;
544 return NULL;
546 EXPORT_SYMBOL_GPL(usb_match_id);
548 static int usb_device_match(struct device *dev, struct device_driver *drv)
550 /* devices and interfaces are handled separately */
551 if (is_usb_device(dev)) {
553 /* interface drivers never match devices */
554 if (!is_usb_device_driver(drv))
555 return 0;
557 /* TODO: Add real matching code */
558 return 1;
560 } else if (is_usb_interface(dev)) {
561 struct usb_interface *intf;
562 struct usb_driver *usb_drv;
563 const struct usb_device_id *id;
565 /* device drivers never match interfaces */
566 if (is_usb_device_driver(drv))
567 return 0;
569 intf = to_usb_interface(dev);
570 usb_drv = to_usb_driver(drv);
572 id = usb_match_id(intf, usb_drv->id_table);
573 if (id)
574 return 1;
576 id = usb_match_dynamic_id(intf, usb_drv);
577 if (id)
578 return 1;
581 return 0;
584 #ifdef CONFIG_HOTPLUG
585 static int usb_uevent(struct device *dev, char **envp, int num_envp,
586 char *buffer, int buffer_size)
588 struct usb_device *usb_dev;
589 int i = 0;
590 int length = 0;
592 if (!dev)
593 return -ENODEV;
595 /* driver is often null here; dev_dbg() would oops */
596 pr_debug("usb %s: uevent\n", dev->bus_id);
598 if (is_usb_device(dev)) {
599 usb_dev = to_usb_device(dev);
600 } else if (is_usb_interface(dev)) {
601 struct usb_interface *intf = to_usb_interface(dev);
603 usb_dev = interface_to_usbdev(intf);
604 } else {
605 return 0;
608 if (usb_dev->devnum < 0) {
609 pr_debug("usb %s: already deleted?\n", dev->bus_id);
610 return -ENODEV;
612 if (!usb_dev->bus) {
613 pr_debug("usb %s: bus removed?\n", dev->bus_id);
614 return -ENODEV;
617 #ifdef CONFIG_USB_DEVICEFS
618 /* If this is available, userspace programs can directly read
619 * all the device descriptors we don't tell them about. Or
620 * act as usermode drivers.
622 if (add_uevent_var(envp, num_envp, &i,
623 buffer, buffer_size, &length,
624 "DEVICE=/proc/bus/usb/%03d/%03d",
625 usb_dev->bus->busnum, usb_dev->devnum))
626 return -ENOMEM;
627 #endif
629 /* per-device configurations are common */
630 if (add_uevent_var(envp, num_envp, &i,
631 buffer, buffer_size, &length,
632 "PRODUCT=%x/%x/%x",
633 le16_to_cpu(usb_dev->descriptor.idVendor),
634 le16_to_cpu(usb_dev->descriptor.idProduct),
635 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
636 return -ENOMEM;
638 /* class-based driver binding models */
639 if (add_uevent_var(envp, num_envp, &i,
640 buffer, buffer_size, &length,
641 "TYPE=%d/%d/%d",
642 usb_dev->descriptor.bDeviceClass,
643 usb_dev->descriptor.bDeviceSubClass,
644 usb_dev->descriptor.bDeviceProtocol))
645 return -ENOMEM;
647 if (add_uevent_var(envp, num_envp, &i,
648 buffer, buffer_size, &length,
649 "BUSNUM=%03d",
650 usb_dev->bus->busnum))
651 return -ENOMEM;
653 if (add_uevent_var(envp, num_envp, &i,
654 buffer, buffer_size, &length,
655 "DEVNUM=%03d",
656 usb_dev->devnum))
657 return -ENOMEM;
659 envp[i] = NULL;
660 return 0;
663 #else
665 static int usb_uevent(struct device *dev, char **envp,
666 int num_envp, char *buffer, int buffer_size)
668 return -ENODEV;
670 #endif /* CONFIG_HOTPLUG */
673 * usb_register_device_driver - register a USB device (not interface) driver
674 * @new_udriver: USB operations for the device driver
675 * @owner: module owner of this driver.
677 * Registers a USB device driver with the USB core. The list of
678 * unattached devices will be rescanned whenever a new driver is
679 * added, allowing the new driver to attach to any recognized devices.
680 * Returns a negative error code on failure and 0 on success.
682 int usb_register_device_driver(struct usb_device_driver *new_udriver,
683 struct module *owner)
685 int retval = 0;
687 if (usb_disabled())
688 return -ENODEV;
690 new_udriver->drvwrap.for_devices = 1;
691 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
692 new_udriver->drvwrap.driver.bus = &usb_bus_type;
693 new_udriver->drvwrap.driver.probe = usb_probe_device;
694 new_udriver->drvwrap.driver.remove = usb_unbind_device;
695 new_udriver->drvwrap.driver.owner = owner;
697 retval = driver_register(&new_udriver->drvwrap.driver);
699 if (!retval) {
700 pr_info("%s: registered new device driver %s\n",
701 usbcore_name, new_udriver->name);
702 usbfs_update_special();
703 } else {
704 printk(KERN_ERR "%s: error %d registering device "
705 " driver %s\n",
706 usbcore_name, retval, new_udriver->name);
709 return retval;
711 EXPORT_SYMBOL_GPL(usb_register_device_driver);
714 * usb_deregister_device_driver - unregister a USB device (not interface) driver
715 * @udriver: USB operations of the device driver to unregister
716 * Context: must be able to sleep
718 * Unlinks the specified driver from the internal USB driver list.
720 void usb_deregister_device_driver(struct usb_device_driver *udriver)
722 pr_info("%s: deregistering device driver %s\n",
723 usbcore_name, udriver->name);
725 driver_unregister(&udriver->drvwrap.driver);
726 usbfs_update_special();
728 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
731 * usb_register_driver - register a USB interface driver
732 * @new_driver: USB operations for the interface driver
733 * @owner: module owner of this driver.
734 * @mod_name: module name string
736 * Registers a USB interface driver with the USB core. The list of
737 * unattached interfaces will be rescanned whenever a new driver is
738 * added, allowing the new driver to attach to any recognized interfaces.
739 * Returns a negative error code on failure and 0 on success.
741 * NOTE: if you want your driver to use the USB major number, you must call
742 * usb_register_dev() to enable that functionality. This function no longer
743 * takes care of that.
745 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
746 const char *mod_name)
748 int retval = 0;
750 if (usb_disabled())
751 return -ENODEV;
753 new_driver->drvwrap.for_devices = 0;
754 new_driver->drvwrap.driver.name = (char *) new_driver->name;
755 new_driver->drvwrap.driver.bus = &usb_bus_type;
756 new_driver->drvwrap.driver.probe = usb_probe_interface;
757 new_driver->drvwrap.driver.remove = usb_unbind_interface;
758 new_driver->drvwrap.driver.owner = owner;
759 new_driver->drvwrap.driver.mod_name = mod_name;
760 spin_lock_init(&new_driver->dynids.lock);
761 INIT_LIST_HEAD(&new_driver->dynids.list);
763 retval = driver_register(&new_driver->drvwrap.driver);
765 if (!retval) {
766 pr_info("%s: registered new interface driver %s\n",
767 usbcore_name, new_driver->name);
768 usbfs_update_special();
769 usb_create_newid_file(new_driver);
770 } else {
771 printk(KERN_ERR "%s: error %d registering interface "
772 " driver %s\n",
773 usbcore_name, retval, new_driver->name);
776 return retval;
778 EXPORT_SYMBOL(usb_register_driver);
781 * usb_deregister - unregister a USB interface driver
782 * @driver: USB operations of the interface driver to unregister
783 * Context: must be able to sleep
785 * Unlinks the specified driver from the internal USB driver list.
787 * NOTE: If you called usb_register_dev(), you still need to call
788 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
789 * this * call will no longer do it for you.
791 void usb_deregister(struct usb_driver *driver)
793 pr_info("%s: deregistering interface driver %s\n",
794 usbcore_name, driver->name);
796 usb_remove_newid_file(driver);
797 usb_free_dynids(driver);
798 driver_unregister(&driver->drvwrap.driver);
800 usbfs_update_special();
802 EXPORT_SYMBOL(usb_deregister);
804 /* Forced unbinding of a USB interface driver, either because
805 * it doesn't support pre_reset/post_reset/reset_resume or
806 * because it doesn't support suspend/resume.
808 * The caller must hold @intf's device's lock, but not its pm_mutex
809 * and not @intf->dev.sem.
811 void usb_forced_unbind_intf(struct usb_interface *intf)
813 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
815 dev_dbg(&intf->dev, "forced unbind\n");
816 usb_driver_release_interface(driver, intf);
818 /* Mark the interface for later rebinding */
819 intf->needs_binding = 1;
822 /* Delayed forced unbinding of a USB interface driver and scan
823 * for rebinding.
825 * The caller must hold @intf's device's lock, but not its pm_mutex
826 * and not @intf->dev.sem.
828 * FIXME: The caller must block system sleep transitions.
830 void usb_rebind_intf(struct usb_interface *intf)
832 int rc;
834 /* Delayed unbind of an existing driver */
835 if (intf->dev.driver) {
836 struct usb_driver *driver =
837 to_usb_driver(intf->dev.driver);
839 dev_dbg(&intf->dev, "forced unbind\n");
840 usb_driver_release_interface(driver, intf);
843 /* Try to rebind the interface */
844 intf->needs_binding = 0;
845 rc = device_attach(&intf->dev);
846 if (rc < 0)
847 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
850 #ifdef CONFIG_PM
852 #define DO_UNBIND 0
853 #define DO_REBIND 1
855 /* Unbind drivers for @udev's interfaces that don't support suspend/resume,
856 * or rebind interfaces that have been unbound, according to @action.
858 * The caller must hold @udev's device lock.
859 * FIXME: For rebinds, the caller must block system sleep transitions.
861 static void do_unbind_rebind(struct usb_device *udev, int action)
863 struct usb_host_config *config;
864 int i;
865 struct usb_interface *intf;
866 struct usb_driver *drv;
868 config = udev->actconfig;
869 if (config) {
870 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
871 intf = config->interface[i];
872 switch (action) {
873 case DO_UNBIND:
874 if (intf->dev.driver) {
875 drv = to_usb_driver(intf->dev.driver);
876 if (!drv->suspend || !drv->resume)
877 usb_forced_unbind_intf(intf);
879 break;
880 case DO_REBIND:
881 if (intf->needs_binding) {
883 /* FIXME: The next line is needed because we are going to probe
884 * the interface, but as far as the PM core is concerned the
885 * interface is still suspended. The problem wouldn't exist
886 * if we could rebind the interface during the interface's own
887 * resume() call, but at the time the usb_device isn't locked!
889 * The real solution will be to carry this out during the device's
890 * complete() callback. Until that is implemented, we have to
891 * use this hack.
893 // intf->dev.power.sleeping = 0;
895 usb_rebind_intf(intf);
897 break;
903 /* Caller has locked udev's pm_mutex */
904 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
906 struct usb_device_driver *udriver;
907 int status = 0;
909 if (udev->state == USB_STATE_NOTATTACHED ||
910 udev->state == USB_STATE_SUSPENDED)
911 goto done;
913 /* For devices that don't have a driver, we do a generic suspend. */
914 if (udev->dev.driver)
915 udriver = to_usb_device_driver(udev->dev.driver);
916 else {
917 udev->do_remote_wakeup = 0;
918 udriver = &usb_generic_driver;
920 status = udriver->suspend(udev, msg);
922 done:
923 dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
924 return status;
927 /* Caller has locked udev's pm_mutex */
928 static int usb_resume_device(struct usb_device *udev)
930 struct usb_device_driver *udriver;
931 int status = 0;
933 if (udev->state == USB_STATE_NOTATTACHED)
934 goto done;
936 /* Can't resume it if it doesn't have a driver. */
937 if (udev->dev.driver == NULL) {
938 status = -ENOTCONN;
939 goto done;
942 if (udev->quirks & USB_QUIRK_RESET_RESUME)
943 udev->reset_resume = 1;
945 udriver = to_usb_device_driver(udev->dev.driver);
946 status = udriver->resume(udev);
948 done:
949 dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
950 if (status == 0)
951 udev->autoresume_disabled = 0;
952 return status;
955 /* Caller has locked intf's usb_device's pm mutex */
956 static int usb_suspend_interface(struct usb_device *udev,
957 struct usb_interface *intf, pm_message_t msg)
959 struct usb_driver *driver;
960 int status = 0;
962 /* with no hardware, USB interfaces only use FREEZE and ON states */
963 if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf))
964 goto done;
966 /* This can happen; see usb_driver_release_interface() */
967 if (intf->condition == USB_INTERFACE_UNBOUND)
968 goto done;
969 driver = to_usb_driver(intf->dev.driver);
971 if (driver->suspend) {
972 status = driver->suspend(intf, msg);
973 if (status == 0)
974 mark_quiesced(intf);
975 else if (!udev->auto_pm)
976 dev_err(&intf->dev, "%s error %d\n",
977 "suspend", status);
978 } else {
979 /* Later we will unbind the driver and reprobe */
980 intf->needs_binding = 1;
981 dev_warn(&intf->dev, "no %s for driver %s?\n",
982 "suspend", driver->name);
983 mark_quiesced(intf);
986 done:
987 dev_vdbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
988 return status;
991 /* Caller has locked intf's usb_device's pm_mutex */
992 static int usb_resume_interface(struct usb_device *udev,
993 struct usb_interface *intf, int reset_resume)
995 struct usb_driver *driver;
996 int status = 0;
998 if (udev->state == USB_STATE_NOTATTACHED || is_active(intf))
999 goto done;
1001 /* Don't let autoresume interfere with unbinding */
1002 if (intf->condition == USB_INTERFACE_UNBINDING)
1003 goto done;
1005 /* Can't resume it if it doesn't have a driver. */
1006 if (intf->condition == USB_INTERFACE_UNBOUND)
1007 goto done;
1009 /* Don't resume if the interface is marked for rebinding */
1010 if (intf->needs_binding)
1011 goto done;
1012 driver = to_usb_driver(intf->dev.driver);
1014 if (reset_resume) {
1015 if (driver->reset_resume) {
1016 status = driver->reset_resume(intf);
1017 if (status)
1018 dev_err(&intf->dev, "%s error %d\n",
1019 "reset_resume", status);
1020 } else {
1021 intf->needs_binding = 1;
1022 dev_warn(&intf->dev, "no %s for driver %s?\n",
1023 "reset_resume", driver->name);
1025 } else {
1026 if (driver->resume) {
1027 status = driver->resume(intf);
1028 if (status)
1029 dev_err(&intf->dev, "%s error %d\n",
1030 "resume", status);
1031 } else {
1032 intf->needs_binding = 1;
1033 dev_warn(&intf->dev, "no %s for driver %s?\n",
1034 "resume", driver->name);
1038 done:
1039 dev_vdbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
1040 if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
1041 mark_active(intf);
1043 /* Later we will unbind the driver and/or reprobe, if necessary */
1044 return status;
1047 #ifdef CONFIG_USB_SUSPEND
1049 /* Internal routine to check whether we may autosuspend a device. */
1050 static int autosuspend_check(struct usb_device *udev, int reschedule)
1052 int i;
1053 struct usb_interface *intf;
1054 unsigned long suspend_time, j;
1056 /* For autosuspend, fail fast if anything is in use or autosuspend
1057 * is disabled. Also fail if any interfaces require remote wakeup
1058 * but it isn't available.
1060 if (udev->pm_usage_cnt > 0)
1061 return -EBUSY;
1062 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
1063 return -EPERM;
1065 suspend_time = udev->last_busy + udev->autosuspend_delay;
1066 if (udev->actconfig) {
1067 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1068 intf = udev->actconfig->interface[i];
1069 if (!is_active(intf))
1070 continue;
1071 if (intf->pm_usage_cnt > 0)
1072 return -EBUSY;
1073 if (intf->needs_remote_wakeup &&
1074 !udev->do_remote_wakeup) {
1075 dev_dbg(&udev->dev, "remote wakeup needed "
1076 "for autosuspend\n");
1077 return -EOPNOTSUPP;
1080 /* Don't allow autosuspend if the device will need
1081 * a reset-resume and any of its interface drivers
1082 * doesn't include support.
1084 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1085 struct usb_driver *driver;
1087 driver = to_usb_driver(intf->dev.driver);
1088 if (!driver->reset_resume ||
1089 intf->needs_remote_wakeup)
1090 return -EOPNOTSUPP;
1095 /* If everything is okay but the device hasn't been idle for long
1096 * enough, queue a delayed autosuspend request. If the device
1097 * _has_ been idle for long enough and the reschedule flag is set,
1098 * likewise queue a delayed (1 second) autosuspend request.
1100 j = jiffies;
1101 if (time_before(j, suspend_time))
1102 reschedule = 1;
1103 else
1104 suspend_time = j + HZ;
1105 if (reschedule) {
1106 if (!timer_pending(&udev->autosuspend.timer)) {
1107 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1108 round_jiffies_relative(suspend_time - j));
1110 return -EAGAIN;
1112 return 0;
1115 #else
1117 static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1119 return 0;
1122 #endif /* CONFIG_USB_SUSPEND */
1125 * usb_suspend_both - suspend a USB device and its interfaces
1126 * @udev: the usb_device to suspend
1127 * @msg: Power Management message describing this state transition
1129 * This is the central routine for suspending USB devices. It calls the
1130 * suspend methods for all the interface drivers in @udev and then calls
1131 * the suspend method for @udev itself. If an error occurs at any stage,
1132 * all the interfaces which were suspended are resumed so that they remain
1133 * in the same state as the device.
1135 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
1136 * checks first to make sure that neither the device itself or any of its
1137 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
1138 * are, the autosuspend fails.
1140 * If the suspend succeeds, the routine recursively queues an autosuspend
1141 * request for @udev's parent device, thereby propagating the change up
1142 * the device tree. If all of the parent's children are now suspended,
1143 * the parent will autosuspend in turn.
1145 * The suspend method calls are subject to mutual exclusion under control
1146 * of @udev's pm_mutex. Many of these calls are also under the protection
1147 * of @udev's device lock (including all requests originating outside the
1148 * USB subsystem), but autosuspend requests generated by a child device or
1149 * interface driver may not be. Usbcore will insure that the method calls
1150 * do not arrive during bind, unbind, or reset operations. However, drivers
1151 * must be prepared to handle suspend calls arriving at unpredictable times.
1152 * The only way to block such calls is to do an autoresume (preventing
1153 * autosuspends) while holding @udev's device lock (preventing outside
1154 * suspends).
1156 * The caller must hold @udev->pm_mutex.
1158 * This routine can run only in process context.
1160 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1162 int status = 0;
1163 int i = 0;
1164 struct usb_interface *intf;
1165 struct usb_device *parent = udev->parent;
1167 if (udev->state == USB_STATE_NOTATTACHED ||
1168 udev->state == USB_STATE_SUSPENDED)
1169 goto done;
1171 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1173 if (udev->auto_pm) {
1174 status = autosuspend_check(udev, 0);
1175 if (status < 0)
1176 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 while (--i >= 0) {
1194 intf = udev->actconfig->interface[i];
1195 usb_resume_interface(udev, intf, 0);
1198 /* Try another autosuspend when the interfaces aren't busy */
1199 if (udev->auto_pm)
1200 autosuspend_check(udev, status == -EBUSY);
1202 /* If the suspend succeeded then prevent any more URB submissions,
1203 * flush any outstanding URBs, and propagate the suspend up the tree.
1205 } else {
1206 cancel_delayed_work(&udev->autosuspend);
1207 udev->can_submit = 0;
1208 for (i = 0; i < 16; ++i) {
1209 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1210 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1213 /* If this is just a FREEZE or a PRETHAW, udev might
1214 * not really be suspended. Only true suspends get
1215 * propagated up the device tree.
1217 if (parent && udev->state == USB_STATE_SUSPENDED)
1218 usb_autosuspend_device(parent);
1221 done:
1222 dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1223 return status;
1227 * usb_resume_both - resume a USB device and its interfaces
1228 * @udev: the usb_device to resume
1230 * This is the central routine for resuming USB devices. It calls the
1231 * the resume method for @udev and then calls the resume methods for all
1232 * the interface drivers in @udev.
1234 * Before starting the resume, the routine calls itself recursively for
1235 * the parent device of @udev, thereby propagating the change up the device
1236 * tree and assuring that @udev will be able to resume. If the parent is
1237 * unable to resume successfully, the routine fails.
1239 * The resume method calls are subject to mutual exclusion under control
1240 * of @udev's pm_mutex. Many of these calls are also under the protection
1241 * of @udev's device lock (including all requests originating outside the
1242 * USB subsystem), but autoresume requests generated by a child device or
1243 * interface driver may not be. Usbcore will insure that the method calls
1244 * do not arrive during bind, unbind, or reset operations. However, drivers
1245 * must be prepared to handle resume calls arriving at unpredictable times.
1246 * The only way to block such calls is to do an autoresume (preventing
1247 * other autoresumes) while holding @udev's device lock (preventing outside
1248 * resumes).
1250 * The caller must hold @udev->pm_mutex.
1252 * This routine can run only in process context.
1254 static int usb_resume_both(struct usb_device *udev)
1256 int status = 0;
1257 int i;
1258 struct usb_interface *intf;
1259 struct usb_device *parent = udev->parent;
1261 cancel_delayed_work(&udev->autosuspend);
1262 if (udev->state == USB_STATE_NOTATTACHED) {
1263 status = -ENODEV;
1264 goto done;
1266 udev->can_submit = 1;
1268 /* Propagate the resume up the tree, if necessary */
1269 if (udev->state == USB_STATE_SUSPENDED) {
1270 if (udev->auto_pm && udev->autoresume_disabled) {
1271 status = -EPERM;
1272 goto done;
1274 if (parent) {
1275 status = usb_autoresume_device(parent);
1276 if (status == 0) {
1277 status = usb_resume_device(udev);
1278 if (status || udev->state ==
1279 USB_STATE_NOTATTACHED) {
1280 usb_autosuspend_device(parent);
1282 /* It's possible usb_resume_device()
1283 * failed after the port was
1284 * unsuspended, causing udev to be
1285 * logically disconnected. We don't
1286 * want usb_disconnect() to autosuspend
1287 * the parent again, so tell it that
1288 * udev disconnected while still
1289 * suspended. */
1290 if (udev->state ==
1291 USB_STATE_NOTATTACHED)
1292 udev->discon_suspended = 1;
1295 } else {
1297 /* We can't progagate beyond the USB subsystem,
1298 * so if a root hub's controller is suspended
1299 * then we're stuck. */
1300 status = usb_resume_device(udev);
1302 } else if (udev->reset_resume)
1303 status = usb_resume_device(udev);
1305 if (status == 0 && udev->actconfig) {
1306 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1307 intf = udev->actconfig->interface[i];
1308 usb_resume_interface(udev, intf, udev->reset_resume);
1312 done:
1313 dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1314 if (!status)
1315 udev->reset_resume = 0;
1316 return status;
1319 #ifdef CONFIG_USB_SUSPEND
1321 /* Internal routine to adjust a device's usage counter and change
1322 * its autosuspend state.
1324 static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1326 int status = 0;
1328 usb_pm_lock(udev);
1329 udev->auto_pm = 1;
1330 udev->pm_usage_cnt += inc_usage_cnt;
1331 WARN_ON(udev->pm_usage_cnt < 0);
1332 if (inc_usage_cnt)
1333 udev->last_busy = jiffies;
1334 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1335 if (udev->state == USB_STATE_SUSPENDED)
1336 status = usb_resume_both(udev);
1337 if (status != 0)
1338 udev->pm_usage_cnt -= inc_usage_cnt;
1339 else if (inc_usage_cnt)
1340 udev->last_busy = jiffies;
1341 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1342 status = usb_suspend_both(udev, PMSG_SUSPEND);
1344 usb_pm_unlock(udev);
1345 return status;
1348 /* usb_autosuspend_work - callback routine to autosuspend a USB device */
1349 void usb_autosuspend_work(struct work_struct *work)
1351 struct usb_device *udev =
1352 container_of(work, struct usb_device, autosuspend.work);
1354 usb_autopm_do_device(udev, 0);
1358 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1359 * @udev: the usb_device to autosuspend
1361 * This routine should be called when a core subsystem is finished using
1362 * @udev and wants to allow it to autosuspend. Examples would be when
1363 * @udev's device file in usbfs is closed or after a configuration change.
1365 * @udev's usage counter is decremented. If it or any of the usage counters
1366 * for an active interface is greater than 0, no autosuspend request will be
1367 * queued. (If an interface driver does not support autosuspend then its
1368 * usage counter is permanently positive.) Furthermore, if an interface
1369 * driver requires remote-wakeup capability during autosuspend but remote
1370 * wakeup is disabled, the autosuspend will fail.
1372 * Often the caller will hold @udev's device lock, but this is not
1373 * necessary.
1375 * This routine can run only in process context.
1377 void usb_autosuspend_device(struct usb_device *udev)
1379 int status;
1381 status = usb_autopm_do_device(udev, -1);
1382 dev_vdbg(&udev->dev, "%s: cnt %d\n",
1383 __FUNCTION__, udev->pm_usage_cnt);
1387 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1388 * @udev: the usb_device to autosuspend
1390 * This routine should be called when a core subsystem thinks @udev may
1391 * be ready to autosuspend.
1393 * @udev's usage counter left unchanged. If it or any of the usage counters
1394 * for an active interface is greater than 0, or autosuspend is not allowed
1395 * for any other reason, no autosuspend request will be queued.
1397 * This routine can run only in process context.
1399 void usb_try_autosuspend_device(struct usb_device *udev)
1401 usb_autopm_do_device(udev, 0);
1402 dev_vdbg(&udev->dev, "%s: cnt %d\n",
1403 __FUNCTION__, udev->pm_usage_cnt);
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 not
1413 * prevent suspend events originating in the PM core.) Examples would be
1414 * 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 * Often the caller will hold @udev's device lock, but this is not
1421 * necessary (and attempting it might cause deadlock).
1423 * This routine can run only in process context.
1425 int usb_autoresume_device(struct usb_device *udev)
1427 int status;
1429 status = usb_autopm_do_device(udev, 1);
1430 dev_vdbg(&udev->dev, "%s: status %d cnt %d\n",
1431 __FUNCTION__, status, udev->pm_usage_cnt);
1432 return status;
1435 /* Internal routine to adjust an interface's usage counter and change
1436 * its device's autosuspend state.
1438 static int usb_autopm_do_interface(struct usb_interface *intf,
1439 int inc_usage_cnt)
1441 struct usb_device *udev = interface_to_usbdev(intf);
1442 int status = 0;
1444 usb_pm_lock(udev);
1445 if (intf->condition == USB_INTERFACE_UNBOUND)
1446 status = -ENODEV;
1447 else {
1448 udev->auto_pm = 1;
1449 intf->pm_usage_cnt += inc_usage_cnt;
1450 udev->last_busy = jiffies;
1451 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
1452 if (udev->state == USB_STATE_SUSPENDED)
1453 status = usb_resume_both(udev);
1454 if (status != 0)
1455 intf->pm_usage_cnt -= inc_usage_cnt;
1456 else
1457 udev->last_busy = jiffies;
1458 } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) {
1459 status = usb_suspend_both(udev, PMSG_SUSPEND);
1462 usb_pm_unlock(udev);
1463 return status;
1467 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1468 * @intf: the usb_interface whose counter should be decremented
1470 * This routine should be called by an interface driver when it is
1471 * finished using @intf and wants to allow it to autosuspend. A typical
1472 * example would be a character-device driver when its device file is
1473 * closed.
1475 * The routine decrements @intf's usage counter. When the counter reaches
1476 * 0, a delayed autosuspend request for @intf's device is queued. When
1477 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1478 * the other usage counters for the sibling interfaces and @intf's
1479 * usb_device, the device and all its interfaces will be autosuspended.
1481 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1482 * core will not change its value other than the increment and decrement
1483 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1484 * may use this simple counter-oriented discipline or may set the value
1485 * any way it likes.
1487 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1488 * take place only if the device's remote-wakeup facility is enabled.
1490 * Suspend method calls queued by this routine can arrive at any time
1491 * while @intf is resumed and its usage counter is equal to 0. They are
1492 * not protected by the usb_device's lock but only by its pm_mutex.
1493 * Drivers must provide their own synchronization.
1495 * This routine can run only in process context.
1497 void usb_autopm_put_interface(struct usb_interface *intf)
1499 int status;
1501 status = usb_autopm_do_interface(intf, -1);
1502 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1503 __FUNCTION__, status, intf->pm_usage_cnt);
1505 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1508 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1509 * @intf: the usb_interface whose counter should be incremented
1511 * This routine should be called by an interface driver when it wants to
1512 * use @intf and needs to guarantee that it is not suspended. In addition,
1513 * the routine prevents @intf from being autosuspended subsequently. (Note
1514 * that this will not prevent suspend events originating in the PM core.)
1515 * This prevention will persist until usb_autopm_put_interface() is called
1516 * or @intf is unbound. A typical example would be a character-device
1517 * driver when its device file is opened.
1520 * The routine increments @intf's usage counter. (However if the
1521 * autoresume fails then the counter is re-decremented.) So long as the
1522 * counter is greater than 0, autosuspend will not be allowed for @intf
1523 * or its usb_device. When the driver is finished using @intf it should
1524 * call usb_autopm_put_interface() to decrement the usage counter and
1525 * queue a delayed autosuspend request (if the counter is <= 0).
1528 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1529 * core will not change its value other than the increment and decrement
1530 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1531 * may use this simple counter-oriented discipline or may set the value
1532 * any way it likes.
1534 * Resume method calls generated by this routine can arrive at any time
1535 * while @intf is suspended. They are not protected by the usb_device's
1536 * lock but only by its pm_mutex. Drivers must provide their own
1537 * synchronization.
1539 * This routine can run only in process context.
1541 int usb_autopm_get_interface(struct usb_interface *intf)
1543 int status;
1545 status = usb_autopm_do_interface(intf, 1);
1546 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1547 __FUNCTION__, status, intf->pm_usage_cnt);
1548 return status;
1550 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1553 * usb_autopm_set_interface - set a USB interface's autosuspend state
1554 * @intf: the usb_interface whose state should be set
1556 * This routine sets the autosuspend state of @intf's device according
1557 * to @intf's usage counter, which the caller must have set previously.
1558 * If the counter is <= 0, the device is autosuspended (if it isn't
1559 * already suspended and if nothing else prevents the autosuspend). If
1560 * the counter is > 0, the device is autoresumed (if it isn't already
1561 * awake).
1563 int usb_autopm_set_interface(struct usb_interface *intf)
1565 int status;
1567 status = usb_autopm_do_interface(intf, 0);
1568 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1569 __FUNCTION__, status, intf->pm_usage_cnt);
1570 return status;
1572 EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1574 #else
1576 void usb_autosuspend_work(struct work_struct *work)
1579 #endif /* CONFIG_USB_SUSPEND */
1582 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1583 * @udev: the usb_device to suspend
1584 * @msg: Power Management message describing this state transition
1586 * This routine handles external suspend requests: ones not generated
1587 * internally by a USB driver (autosuspend) but rather coming from the user
1588 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1589 * out regardless of @udev's usage counter or those of its interfaces,
1590 * and regardless of whether or not remote wakeup is enabled. Of course,
1591 * interface drivers still have the option of failing the suspend (if
1592 * there are unsuspended children, for example).
1594 * The caller must hold @udev's device lock.
1596 int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1598 int status;
1600 do_unbind_rebind(udev, DO_UNBIND);
1601 usb_pm_lock(udev);
1602 udev->auto_pm = 0;
1603 status = usb_suspend_both(udev, msg);
1604 usb_pm_unlock(udev);
1605 return status;
1609 * usb_external_resume_device - external resume of a USB device and its interfaces
1610 * @udev: the usb_device to resume
1612 * This routine handles external resume requests: ones not generated
1613 * internally by a USB driver (autoresume) but rather coming from the user
1614 * (via sysfs), the PM core (system resume), or the device itself (remote
1615 * wakeup). @udev's usage counter is unaffected.
1617 * The caller must hold @udev's device lock.
1619 int usb_external_resume_device(struct usb_device *udev)
1621 int status;
1623 usb_pm_lock(udev);
1624 udev->auto_pm = 0;
1625 status = usb_resume_both(udev);
1626 udev->last_busy = jiffies;
1627 usb_pm_unlock(udev);
1628 if (status == 0)
1629 do_unbind_rebind(udev, DO_REBIND);
1631 /* Now that the device is awake, we can start trying to autosuspend
1632 * it again. */
1633 if (status == 0)
1634 usb_try_autosuspend_device(udev);
1635 return status;
1638 static int usb_suspend(struct device *dev, pm_message_t message)
1640 struct usb_device *udev;
1642 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1643 return 0;
1644 udev = to_usb_device(dev);
1646 /* If udev is already suspended, we can skip this suspend and
1647 * we should also skip the upcoming system resume. High-speed
1648 * root hubs are an exception; they need to resume whenever the
1649 * system wakes up in order for USB-PERSIST port handover to work
1650 * properly.
1652 if (udev->state == USB_STATE_SUSPENDED) {
1653 if (udev->parent || udev->speed != USB_SPEED_HIGH)
1654 udev->skip_sys_resume = 1;
1655 return 0;
1658 udev->skip_sys_resume = 0;
1659 return usb_external_suspend_device(udev, message);
1662 static int usb_resume(struct device *dev)
1664 struct usb_device *udev;
1665 int status;
1667 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1668 return 0;
1669 udev = to_usb_device(dev);
1671 /* If udev->skip_sys_resume is set then udev was already suspended
1672 * when the system sleep started, so we don't want to resume it
1673 * during this system wakeup.
1675 if (udev->skip_sys_resume)
1676 return 0;
1677 status = usb_external_resume_device(udev);
1679 /* Avoid PM error messages for devices disconnected while suspended
1680 * as we'll display regular disconnect messages just a bit later.
1682 if (status == -ENODEV || status == -ESHUTDOWN)
1683 status = 0;
1684 return status;
1687 #endif /* CONFIG_PM */
1689 struct bus_type usb_bus_type = {
1690 .name = "usb",
1691 .match = usb_device_match,
1692 .uevent = usb_uevent,
1693 #ifdef CONFIG_PM
1694 .suspend = usb_suspend,
1695 .resume = usb_resume,
1696 #endif