update/add new 3G modem modules
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / usb / core / driver.c
blob8eadb22a92e81183161f0074a1d1f461f9226f93
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, protocol and number 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 USB_DEVICE_ID_MATCH_INT_NUMBER)))
438 return 0;
440 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
441 (id->bInterfaceClass != intf->desc.bInterfaceClass))
442 return 0;
444 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
445 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
446 return 0;
448 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
449 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
450 return 0;
452 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
453 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
454 return 0;
456 return 1;
458 EXPORT_SYMBOL_GPL(usb_match_one_id);
461 * usb_match_id - find first usb_device_id matching device or interface
462 * @interface: the interface of interest
463 * @id: array of usb_device_id structures, terminated by zero entry
465 * usb_match_id searches an array of usb_device_id's and returns
466 * the first one matching the device or interface, or null.
467 * This is used when binding (or rebinding) a driver to an interface.
468 * Most USB device drivers will use this indirectly, through the usb core,
469 * but some layered driver frameworks use it directly.
470 * These device tables are exported with MODULE_DEVICE_TABLE, through
471 * modutils, to support the driver loading functionality of USB hotplugging.
473 * What Matches:
475 * The "match_flags" element in a usb_device_id controls which
476 * members are used. If the corresponding bit is set, the
477 * value in the device_id must match its corresponding member
478 * in the device or interface descriptor, or else the device_id
479 * does not match.
481 * "driver_info" is normally used only by device drivers,
482 * but you can create a wildcard "matches anything" usb_device_id
483 * as a driver's "modules.usbmap" entry if you provide an id with
484 * only a nonzero "driver_info" field. If you do this, the USB device
485 * driver's probe() routine should use additional intelligence to
486 * decide whether to bind to the specified interface.
488 * What Makes Good usb_device_id Tables:
490 * The match algorithm is very simple, so that intelligence in
491 * driver selection must come from smart driver id records.
492 * Unless you have good reasons to use another selection policy,
493 * provide match elements only in related groups, and order match
494 * specifiers from specific to general. Use the macros provided
495 * for that purpose if you can.
497 * The most specific match specifiers use device descriptor
498 * data. These are commonly used with product-specific matches;
499 * the USB_DEVICE macro lets you provide vendor and product IDs,
500 * and you can also match against ranges of product revisions.
501 * These are widely used for devices with application or vendor
502 * specific bDeviceClass values.
504 * Matches based on device class/subclass/protocol specifications
505 * are slightly more general; use the USB_DEVICE_INFO macro, or
506 * its siblings. These are used with single-function devices
507 * where bDeviceClass doesn't specify that each interface has
508 * its own class.
510 * Matches based on interface class/subclass/protocol are the
511 * most general; they let drivers bind to any interface on a
512 * multiple-function device. Use the USB_INTERFACE_INFO
513 * macro, or its siblings, to match class-per-interface style
514 * devices (as recorded in bInterfaceClass).
516 * Note that an entry created by USB_INTERFACE_INFO won't match
517 * any interface if the device class is set to Vendor-Specific.
518 * This is deliberate; according to the USB spec the meanings of
519 * the interface class/subclass/protocol for these devices are also
520 * vendor-specific, and hence matching against a standard product
521 * class wouldn't work anyway. If you really want to use an
522 * interface-based match for such a device, create a match record
523 * that also specifies the vendor ID. (Unforunately there isn't a
524 * standard macro for creating records like this.)
526 * Within those groups, remember that not all combinations are
527 * meaningful. For example, don't give a product version range
528 * without vendor and product IDs; or specify a protocol without
529 * its associated class and subclass.
531 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
532 const struct usb_device_id *id)
534 /* proc_connectinfo in devio.c may call us with id == NULL. */
535 if (id == NULL)
536 return NULL;
538 /* It is important to check that id->driver_info is nonzero,
539 since an entry that is all zeroes except for a nonzero
540 id->driver_info is the way to create an entry that
541 indicates that the driver want to examine every
542 device and interface. */
543 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
544 id->bInterfaceClass || id->driver_info; id++) {
545 if (usb_match_one_id(interface, id))
546 return id;
549 return NULL;
551 EXPORT_SYMBOL_GPL(usb_match_id);
553 static int usb_device_match(struct device *dev, struct device_driver *drv)
555 /* devices and interfaces are handled separately */
556 if (is_usb_device(dev)) {
558 /* interface drivers never match devices */
559 if (!is_usb_device_driver(drv))
560 return 0;
562 /* TODO: Add real matching code */
563 return 1;
565 } else if (is_usb_interface(dev)) {
566 struct usb_interface *intf;
567 struct usb_driver *usb_drv;
568 const struct usb_device_id *id;
570 /* device drivers never match interfaces */
571 if (is_usb_device_driver(drv))
572 return 0;
574 intf = to_usb_interface(dev);
575 usb_drv = to_usb_driver(drv);
577 id = usb_match_id(intf, usb_drv->id_table);
578 if (id)
579 return 1;
581 id = usb_match_dynamic_id(intf, usb_drv);
582 if (id)
583 return 1;
586 return 0;
589 #ifdef CONFIG_HOTPLUG
590 static int usb_uevent(struct device *dev, char **envp, int num_envp,
591 char *buffer, int buffer_size)
593 struct usb_device *usb_dev;
594 int i = 0;
595 int length = 0;
597 if (!dev)
598 return -ENODEV;
600 /* driver is often null here; dev_dbg() would oops */
601 pr_debug("usb %s: uevent\n", dev->bus_id);
603 if (is_usb_device(dev)) {
604 usb_dev = to_usb_device(dev);
605 } else if (is_usb_interface(dev)) {
606 struct usb_interface *intf = to_usb_interface(dev);
608 usb_dev = interface_to_usbdev(intf);
609 } else {
610 return 0;
613 if (usb_dev->devnum < 0) {
614 pr_debug("usb %s: already deleted?\n", dev->bus_id);
615 return -ENODEV;
617 if (!usb_dev->bus) {
618 pr_debug("usb %s: bus removed?\n", dev->bus_id);
619 return -ENODEV;
622 #ifdef CONFIG_USB_DEVICEFS
623 /* If this is available, userspace programs can directly read
624 * all the device descriptors we don't tell them about. Or
625 * act as usermode drivers.
627 if (add_uevent_var(envp, num_envp, &i,
628 buffer, buffer_size, &length,
629 "DEVICE=/proc/bus/usb/%03d/%03d",
630 usb_dev->bus->busnum, usb_dev->devnum))
631 return -ENOMEM;
632 #endif
634 /* per-device configurations are common */
635 if (add_uevent_var(envp, num_envp, &i,
636 buffer, buffer_size, &length,
637 "PRODUCT=%x/%x/%x",
638 le16_to_cpu(usb_dev->descriptor.idVendor),
639 le16_to_cpu(usb_dev->descriptor.idProduct),
640 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
641 return -ENOMEM;
643 /* class-based driver binding models */
644 if (add_uevent_var(envp, num_envp, &i,
645 buffer, buffer_size, &length,
646 "TYPE=%d/%d/%d",
647 usb_dev->descriptor.bDeviceClass,
648 usb_dev->descriptor.bDeviceSubClass,
649 usb_dev->descriptor.bDeviceProtocol))
650 return -ENOMEM;
652 if (add_uevent_var(envp, num_envp, &i,
653 buffer, buffer_size, &length,
654 "BUSNUM=%03d",
655 usb_dev->bus->busnum))
656 return -ENOMEM;
658 if (add_uevent_var(envp, num_envp, &i,
659 buffer, buffer_size, &length,
660 "DEVNUM=%03d",
661 usb_dev->devnum))
662 return -ENOMEM;
664 envp[i] = NULL;
665 return 0;
668 #else
670 static int usb_uevent(struct device *dev, char **envp,
671 int num_envp, char *buffer, int buffer_size)
673 return -ENODEV;
675 #endif /* CONFIG_HOTPLUG */
678 * usb_register_device_driver - register a USB device (not interface) driver
679 * @new_udriver: USB operations for the device driver
680 * @owner: module owner of this driver.
682 * Registers a USB device driver with the USB core. The list of
683 * unattached devices will be rescanned whenever a new driver is
684 * added, allowing the new driver to attach to any recognized devices.
685 * Returns a negative error code on failure and 0 on success.
687 int usb_register_device_driver(struct usb_device_driver *new_udriver,
688 struct module *owner)
690 int retval = 0;
692 if (usb_disabled())
693 return -ENODEV;
695 new_udriver->drvwrap.for_devices = 1;
696 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
697 new_udriver->drvwrap.driver.bus = &usb_bus_type;
698 new_udriver->drvwrap.driver.probe = usb_probe_device;
699 new_udriver->drvwrap.driver.remove = usb_unbind_device;
700 new_udriver->drvwrap.driver.owner = owner;
702 retval = driver_register(&new_udriver->drvwrap.driver);
704 if (!retval) {
705 pr_info("%s: registered new device driver %s\n",
706 usbcore_name, new_udriver->name);
707 usbfs_update_special();
708 } else {
709 printk(KERN_ERR "%s: error %d registering device "
710 " driver %s\n",
711 usbcore_name, retval, new_udriver->name);
714 return retval;
716 EXPORT_SYMBOL_GPL(usb_register_device_driver);
719 * usb_deregister_device_driver - unregister a USB device (not interface) driver
720 * @udriver: USB operations of the device driver to unregister
721 * Context: must be able to sleep
723 * Unlinks the specified driver from the internal USB driver list.
725 void usb_deregister_device_driver(struct usb_device_driver *udriver)
727 pr_info("%s: deregistering device driver %s\n",
728 usbcore_name, udriver->name);
730 driver_unregister(&udriver->drvwrap.driver);
731 usbfs_update_special();
733 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
736 * usb_register_driver - register a USB interface driver
737 * @new_driver: USB operations for the interface driver
738 * @owner: module owner of this driver.
739 * @mod_name: module name string
741 * Registers a USB interface driver with the USB core. The list of
742 * unattached interfaces will be rescanned whenever a new driver is
743 * added, allowing the new driver to attach to any recognized interfaces.
744 * Returns a negative error code on failure and 0 on success.
746 * NOTE: if you want your driver to use the USB major number, you must call
747 * usb_register_dev() to enable that functionality. This function no longer
748 * takes care of that.
750 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
751 const char *mod_name)
753 int retval = 0;
755 if (usb_disabled())
756 return -ENODEV;
758 new_driver->drvwrap.for_devices = 0;
759 new_driver->drvwrap.driver.name = (char *) new_driver->name;
760 new_driver->drvwrap.driver.bus = &usb_bus_type;
761 new_driver->drvwrap.driver.probe = usb_probe_interface;
762 new_driver->drvwrap.driver.remove = usb_unbind_interface;
763 new_driver->drvwrap.driver.owner = owner;
764 new_driver->drvwrap.driver.mod_name = mod_name;
765 spin_lock_init(&new_driver->dynids.lock);
766 INIT_LIST_HEAD(&new_driver->dynids.list);
768 retval = driver_register(&new_driver->drvwrap.driver);
770 if (!retval) {
771 pr_info("%s: registered new interface driver %s\n",
772 usbcore_name, new_driver->name);
773 usbfs_update_special();
774 usb_create_newid_file(new_driver);
775 } else {
776 printk(KERN_ERR "%s: error %d registering interface "
777 " driver %s\n",
778 usbcore_name, retval, new_driver->name);
781 return retval;
783 EXPORT_SYMBOL_GPL(usb_register_driver);
786 * usb_deregister - unregister a USB interface driver
787 * @driver: USB operations of the interface driver to unregister
788 * Context: must be able to sleep
790 * Unlinks the specified driver from the internal USB driver list.
792 * NOTE: If you called usb_register_dev(), you still need to call
793 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
794 * this * call will no longer do it for you.
796 void usb_deregister(struct usb_driver *driver)
798 pr_info("%s: deregistering interface driver %s\n",
799 usbcore_name, driver->name);
801 usb_remove_newid_file(driver);
802 usb_free_dynids(driver);
803 driver_unregister(&driver->drvwrap.driver);
805 usbfs_update_special();
807 EXPORT_SYMBOL_GPL(usb_deregister);
809 /* Forced unbinding of a USB interface driver, either because
810 * it doesn't support pre_reset/post_reset/reset_resume or
811 * because it doesn't support suspend/resume.
813 * The caller must hold @intf's device's lock, but not its pm_mutex
814 * and not @intf->dev.sem.
816 void usb_forced_unbind_intf(struct usb_interface *intf)
818 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
820 dev_dbg(&intf->dev, "forced unbind\n");
821 usb_driver_release_interface(driver, intf);
823 /* Mark the interface for later rebinding */
824 intf->needs_binding = 1;
827 /* Delayed forced unbinding of a USB interface driver and scan
828 * for rebinding.
830 * The caller must hold @intf's device's lock, but not its pm_mutex
831 * and not @intf->dev.sem.
833 * FIXME: The caller must block system sleep transitions.
835 void usb_rebind_intf(struct usb_interface *intf)
837 int rc;
839 /* Delayed unbind of an existing driver */
840 if (intf->dev.driver) {
841 struct usb_driver *driver =
842 to_usb_driver(intf->dev.driver);
844 dev_dbg(&intf->dev, "forced unbind\n");
845 usb_driver_release_interface(driver, intf);
848 /* Try to rebind the interface */
849 intf->needs_binding = 0;
850 rc = device_attach(&intf->dev);
851 if (rc < 0)
852 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
855 #ifdef CONFIG_PM
857 #define DO_UNBIND 0
858 #define DO_REBIND 1
860 /* Unbind drivers for @udev's interfaces that don't support suspend/resume,
861 * or rebind interfaces that have been unbound, according to @action.
863 * The caller must hold @udev's device lock.
864 * FIXME: For rebinds, the caller must block system sleep transitions.
866 static void do_unbind_rebind(struct usb_device *udev, int action)
868 struct usb_host_config *config;
869 int i;
870 struct usb_interface *intf;
871 struct usb_driver *drv;
873 config = udev->actconfig;
874 if (config) {
875 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
876 intf = config->interface[i];
877 switch (action) {
878 case DO_UNBIND:
879 if (intf->dev.driver) {
880 drv = to_usb_driver(intf->dev.driver);
881 if (!drv->suspend || !drv->resume)
882 usb_forced_unbind_intf(intf);
884 break;
885 case DO_REBIND:
886 if (intf->needs_binding) {
888 /* FIXME: The next line is needed because we are going to probe
889 * the interface, but as far as the PM core is concerned the
890 * interface is still suspended. The problem wouldn't exist
891 * if we could rebind the interface during the interface's own
892 * resume() call, but at the time the usb_device isn't locked!
894 * The real solution will be to carry this out during the device's
895 * complete() callback. Until that is implemented, we have to
896 * use this hack.
898 // intf->dev.power.sleeping = 0;
900 usb_rebind_intf(intf);
902 break;
908 /* Caller has locked udev's pm_mutex */
909 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
911 struct usb_device_driver *udriver;
912 int status = 0;
914 if (udev->state == USB_STATE_NOTATTACHED ||
915 udev->state == USB_STATE_SUSPENDED)
916 goto done;
918 /* For devices that don't have a driver, we do a generic suspend. */
919 if (udev->dev.driver)
920 udriver = to_usb_device_driver(udev->dev.driver);
921 else {
922 udev->do_remote_wakeup = 0;
923 udriver = &usb_generic_driver;
925 status = udriver->suspend(udev, msg);
927 done:
928 dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
929 return status;
932 /* Caller has locked udev's pm_mutex */
933 static int usb_resume_device(struct usb_device *udev)
935 struct usb_device_driver *udriver;
936 int status = 0;
938 if (udev->state == USB_STATE_NOTATTACHED)
939 goto done;
941 /* Can't resume it if it doesn't have a driver. */
942 if (udev->dev.driver == NULL) {
943 status = -ENOTCONN;
944 goto done;
947 if (udev->quirks & USB_QUIRK_RESET_RESUME)
948 udev->reset_resume = 1;
950 udriver = to_usb_device_driver(udev->dev.driver);
951 status = udriver->resume(udev);
953 done:
954 dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
955 if (status == 0)
956 udev->autoresume_disabled = 0;
957 return status;
960 /* Caller has locked intf's usb_device's pm mutex */
961 static int usb_suspend_interface(struct usb_device *udev,
962 struct usb_interface *intf, pm_message_t msg)
964 struct usb_driver *driver;
965 int status = 0;
967 /* with no hardware, USB interfaces only use FREEZE and ON states */
968 if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf))
969 goto done;
971 /* This can happen; see usb_driver_release_interface() */
972 if (intf->condition == USB_INTERFACE_UNBOUND)
973 goto done;
974 driver = to_usb_driver(intf->dev.driver);
976 if (driver->suspend) {
977 status = driver->suspend(intf, msg);
978 if (status == 0)
979 mark_quiesced(intf);
980 else if (!udev->auto_pm)
981 dev_err(&intf->dev, "%s error %d\n",
982 "suspend", status);
983 } else {
984 /* Later we will unbind the driver and reprobe */
985 intf->needs_binding = 1;
986 dev_warn(&intf->dev, "no %s for driver %s?\n",
987 "suspend", driver->name);
988 mark_quiesced(intf);
991 done:
992 dev_vdbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
993 return status;
996 /* Caller has locked intf's usb_device's pm_mutex */
997 static int usb_resume_interface(struct usb_device *udev,
998 struct usb_interface *intf, int reset_resume)
1000 struct usb_driver *driver;
1001 int status = 0;
1003 if (udev->state == USB_STATE_NOTATTACHED || is_active(intf))
1004 goto done;
1006 /* Don't let autoresume interfere with unbinding */
1007 if (intf->condition == USB_INTERFACE_UNBINDING)
1008 goto done;
1010 /* Can't resume it if it doesn't have a driver. */
1011 if (intf->condition == USB_INTERFACE_UNBOUND)
1012 goto done;
1014 /* Don't resume if the interface is marked for rebinding */
1015 if (intf->needs_binding)
1016 goto done;
1017 driver = to_usb_driver(intf->dev.driver);
1019 if (reset_resume) {
1020 if (driver->reset_resume) {
1021 status = driver->reset_resume(intf);
1022 if (status)
1023 dev_err(&intf->dev, "%s error %d\n",
1024 "reset_resume", status);
1025 } else {
1026 intf->needs_binding = 1;
1027 dev_warn(&intf->dev, "no %s for driver %s?\n",
1028 "reset_resume", driver->name);
1030 } else {
1031 if (driver->resume) {
1032 status = driver->resume(intf);
1033 if (status)
1034 dev_err(&intf->dev, "%s error %d\n",
1035 "resume", status);
1036 } else {
1037 intf->needs_binding = 1;
1038 dev_warn(&intf->dev, "no %s for driver %s?\n",
1039 "resume", driver->name);
1043 done:
1044 dev_vdbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
1045 if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
1046 mark_active(intf);
1048 /* Later we will unbind the driver and/or reprobe, if necessary */
1049 return status;
1052 #ifdef CONFIG_USB_SUSPEND
1054 /* Internal routine to check whether we may autosuspend a device. */
1055 static int autosuspend_check(struct usb_device *udev, int reschedule)
1057 int i;
1058 struct usb_interface *intf;
1059 unsigned long suspend_time, j;
1061 /* For autosuspend, fail fast if anything is in use or autosuspend
1062 * is disabled. Also fail if any interfaces require remote wakeup
1063 * but it isn't available.
1065 if (udev->pm_usage_cnt > 0)
1066 return -EBUSY;
1067 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
1068 return -EPERM;
1070 suspend_time = udev->last_busy + udev->autosuspend_delay;
1071 if (udev->actconfig) {
1072 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1073 intf = udev->actconfig->interface[i];
1074 if (!is_active(intf))
1075 continue;
1076 if (intf->pm_usage_cnt > 0)
1077 return -EBUSY;
1078 if (intf->needs_remote_wakeup &&
1079 !udev->do_remote_wakeup) {
1080 dev_dbg(&udev->dev, "remote wakeup needed "
1081 "for autosuspend\n");
1082 return -EOPNOTSUPP;
1085 /* Don't allow autosuspend if the device will need
1086 * a reset-resume and any of its interface drivers
1087 * doesn't include support.
1089 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1090 struct usb_driver *driver;
1092 driver = to_usb_driver(intf->dev.driver);
1093 if (!driver->reset_resume ||
1094 intf->needs_remote_wakeup)
1095 return -EOPNOTSUPP;
1100 /* If everything is okay but the device hasn't been idle for long
1101 * enough, queue a delayed autosuspend request. If the device
1102 * _has_ been idle for long enough and the reschedule flag is set,
1103 * likewise queue a delayed (1 second) autosuspend request.
1105 j = jiffies;
1106 if (time_before(j, suspend_time))
1107 reschedule = 1;
1108 else
1109 suspend_time = j + HZ;
1110 if (reschedule) {
1111 if (!timer_pending(&udev->autosuspend.timer)) {
1112 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1113 round_jiffies_relative(suspend_time - j));
1115 return -EAGAIN;
1117 return 0;
1120 #else
1122 static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1124 return 0;
1127 #endif /* CONFIG_USB_SUSPEND */
1130 * usb_suspend_both - suspend a USB device and its interfaces
1131 * @udev: the usb_device to suspend
1132 * @msg: Power Management message describing this state transition
1134 * This is the central routine for suspending USB devices. It calls the
1135 * suspend methods for all the interface drivers in @udev and then calls
1136 * the suspend method for @udev itself. If an error occurs at any stage,
1137 * all the interfaces which were suspended are resumed so that they remain
1138 * in the same state as the device.
1140 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
1141 * checks first to make sure that neither the device itself or any of its
1142 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
1143 * are, the autosuspend fails.
1145 * If the suspend succeeds, the routine recursively queues an autosuspend
1146 * request for @udev's parent device, thereby propagating the change up
1147 * the device tree. If all of the parent's children are now suspended,
1148 * the parent will autosuspend in turn.
1150 * The suspend method calls are subject to mutual exclusion under control
1151 * of @udev's pm_mutex. Many of these calls are also under the protection
1152 * of @udev's device lock (including all requests originating outside the
1153 * USB subsystem), but autosuspend requests generated by a child device or
1154 * interface driver may not be. Usbcore will insure that the method calls
1155 * do not arrive during bind, unbind, or reset operations. However, drivers
1156 * must be prepared to handle suspend calls arriving at unpredictable times.
1157 * The only way to block such calls is to do an autoresume (preventing
1158 * autosuspends) while holding @udev's device lock (preventing outside
1159 * suspends).
1161 * The caller must hold @udev->pm_mutex.
1163 * This routine can run only in process context.
1165 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1167 int status = 0;
1168 int i = 0;
1169 struct usb_interface *intf;
1170 struct usb_device *parent = udev->parent;
1172 if (udev->state == USB_STATE_NOTATTACHED ||
1173 udev->state == USB_STATE_SUSPENDED)
1174 goto done;
1176 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1178 if (udev->auto_pm) {
1179 status = autosuspend_check(udev, 0);
1180 if (status < 0)
1181 goto done;
1184 /* Suspend all the interfaces and then udev itself */
1185 if (udev->actconfig) {
1186 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1187 intf = udev->actconfig->interface[i];
1188 status = usb_suspend_interface(udev, intf, msg);
1189 if (status != 0)
1190 break;
1193 if (status == 0)
1194 status = usb_suspend_device(udev, msg);
1196 /* If the suspend failed, resume interfaces that did get suspended */
1197 if (status != 0) {
1198 while (--i >= 0) {
1199 intf = udev->actconfig->interface[i];
1200 usb_resume_interface(udev, intf, 0);
1203 /* Try another autosuspend when the interfaces aren't busy */
1204 if (udev->auto_pm)
1205 autosuspend_check(udev, status == -EBUSY);
1207 /* If the suspend succeeded then prevent any more URB submissions,
1208 * flush any outstanding URBs, and propagate the suspend up the tree.
1210 } else {
1211 cancel_delayed_work(&udev->autosuspend);
1212 udev->can_submit = 0;
1213 for (i = 0; i < 16; ++i) {
1214 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1215 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1218 /* If this is just a FREEZE or a PRETHAW, udev might
1219 * not really be suspended. Only true suspends get
1220 * propagated up the device tree.
1222 if (parent && udev->state == USB_STATE_SUSPENDED)
1223 usb_autosuspend_device(parent);
1226 done:
1227 dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1228 return status;
1232 * usb_resume_both - resume a USB device and its interfaces
1233 * @udev: the usb_device to resume
1235 * This is the central routine for resuming USB devices. It calls the
1236 * the resume method for @udev and then calls the resume methods for all
1237 * the interface drivers in @udev.
1239 * Before starting the resume, the routine calls itself recursively for
1240 * the parent device of @udev, thereby propagating the change up the device
1241 * tree and assuring that @udev will be able to resume. If the parent is
1242 * unable to resume successfully, the routine fails.
1244 * The resume method calls are subject to mutual exclusion under control
1245 * of @udev's pm_mutex. Many of these calls are also under the protection
1246 * of @udev's device lock (including all requests originating outside the
1247 * USB subsystem), but autoresume requests generated by a child device or
1248 * interface driver may not be. Usbcore will insure that the method calls
1249 * do not arrive during bind, unbind, or reset operations. However, drivers
1250 * must be prepared to handle resume calls arriving at unpredictable times.
1251 * The only way to block such calls is to do an autoresume (preventing
1252 * other autoresumes) while holding @udev's device lock (preventing outside
1253 * resumes).
1255 * The caller must hold @udev->pm_mutex.
1257 * This routine can run only in process context.
1259 static int usb_resume_both(struct usb_device *udev)
1261 int status = 0;
1262 int i;
1263 struct usb_interface *intf;
1264 struct usb_device *parent = udev->parent;
1266 cancel_delayed_work(&udev->autosuspend);
1267 if (udev->state == USB_STATE_NOTATTACHED) {
1268 status = -ENODEV;
1269 goto done;
1271 udev->can_submit = 1;
1273 /* Propagate the resume up the tree, if necessary */
1274 if (udev->state == USB_STATE_SUSPENDED) {
1275 if (udev->auto_pm && udev->autoresume_disabled) {
1276 status = -EPERM;
1277 goto done;
1279 if (parent) {
1280 status = usb_autoresume_device(parent);
1281 if (status == 0) {
1282 status = usb_resume_device(udev);
1283 if (status || udev->state ==
1284 USB_STATE_NOTATTACHED) {
1285 usb_autosuspend_device(parent);
1287 /* It's possible usb_resume_device()
1288 * failed after the port was
1289 * unsuspended, causing udev to be
1290 * logically disconnected. We don't
1291 * want usb_disconnect() to autosuspend
1292 * the parent again, so tell it that
1293 * udev disconnected while still
1294 * suspended. */
1295 if (udev->state ==
1296 USB_STATE_NOTATTACHED)
1297 udev->discon_suspended = 1;
1300 } else {
1302 /* We can't progagate beyond the USB subsystem,
1303 * so if a root hub's controller is suspended
1304 * then we're stuck. */
1305 status = usb_resume_device(udev);
1307 } else if (udev->reset_resume)
1308 status = usb_resume_device(udev);
1310 if (status == 0 && udev->actconfig) {
1311 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1312 intf = udev->actconfig->interface[i];
1313 usb_resume_interface(udev, intf, udev->reset_resume);
1317 done:
1318 dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1319 if (!status)
1320 udev->reset_resume = 0;
1321 return status;
1324 #ifdef CONFIG_USB_SUSPEND
1326 /* Internal routine to adjust a device's usage counter and change
1327 * its autosuspend state.
1329 static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1331 int status = 0;
1333 usb_pm_lock(udev);
1334 udev->auto_pm = 1;
1335 udev->pm_usage_cnt += inc_usage_cnt;
1336 WARN_ON(udev->pm_usage_cnt < 0);
1337 if (inc_usage_cnt)
1338 udev->last_busy = jiffies;
1339 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1340 if (udev->state == USB_STATE_SUSPENDED)
1341 status = usb_resume_both(udev);
1342 if (status != 0)
1343 udev->pm_usage_cnt -= inc_usage_cnt;
1344 else if (inc_usage_cnt)
1345 udev->last_busy = jiffies;
1346 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1347 status = usb_suspend_both(udev, PMSG_SUSPEND);
1349 usb_pm_unlock(udev);
1350 return status;
1353 /* usb_autosuspend_work - callback routine to autosuspend a USB device */
1354 void usb_autosuspend_work(struct work_struct *work)
1356 struct usb_device *udev =
1357 container_of(work, struct usb_device, autosuspend.work);
1359 usb_autopm_do_device(udev, 0);
1363 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1364 * @udev: the usb_device to autosuspend
1366 * This routine should be called when a core subsystem is finished using
1367 * @udev and wants to allow it to autosuspend. Examples would be when
1368 * @udev's device file in usbfs is closed or after a configuration change.
1370 * @udev's usage counter is decremented. If it or any of the usage counters
1371 * for an active interface is greater than 0, no autosuspend request will be
1372 * queued. (If an interface driver does not support autosuspend then its
1373 * usage counter is permanently positive.) Furthermore, if an interface
1374 * driver requires remote-wakeup capability during autosuspend but remote
1375 * wakeup is disabled, the autosuspend will fail.
1377 * Often the caller will hold @udev's device lock, but this is not
1378 * necessary.
1380 * This routine can run only in process context.
1382 void usb_autosuspend_device(struct usb_device *udev)
1384 int status;
1386 status = usb_autopm_do_device(udev, -1);
1387 dev_vdbg(&udev->dev, "%s: cnt %d\n",
1388 __FUNCTION__, udev->pm_usage_cnt);
1392 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1393 * @udev: the usb_device to autosuspend
1395 * This routine should be called when a core subsystem thinks @udev may
1396 * be ready to autosuspend.
1398 * @udev's usage counter left unchanged. If it or any of the usage counters
1399 * for an active interface is greater than 0, or autosuspend is not allowed
1400 * for any other reason, no autosuspend request will be queued.
1402 * This routine can run only in process context.
1404 void usb_try_autosuspend_device(struct usb_device *udev)
1406 usb_autopm_do_device(udev, 0);
1407 dev_vdbg(&udev->dev, "%s: cnt %d\n",
1408 __FUNCTION__, udev->pm_usage_cnt);
1412 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1413 * @udev: the usb_device to autoresume
1415 * This routine should be called when a core subsystem wants to use @udev
1416 * and needs to guarantee that it is not suspended. No autosuspend will
1417 * occur until usb_autosuspend_device is called. (Note that this will not
1418 * prevent suspend events originating in the PM core.) Examples would be
1419 * when @udev's device file in usbfs is opened or when a remote-wakeup
1420 * request is received.
1422 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1423 * However if the autoresume fails then the usage counter is re-decremented.
1425 * Often the caller will hold @udev's device lock, but this is not
1426 * necessary (and attempting it might cause deadlock).
1428 * This routine can run only in process context.
1430 int usb_autoresume_device(struct usb_device *udev)
1432 int status;
1434 status = usb_autopm_do_device(udev, 1);
1435 dev_vdbg(&udev->dev, "%s: status %d cnt %d\n",
1436 __FUNCTION__, status, udev->pm_usage_cnt);
1437 return status;
1440 /* Internal routine to adjust an interface's usage counter and change
1441 * its device's autosuspend state.
1443 static int usb_autopm_do_interface(struct usb_interface *intf,
1444 int inc_usage_cnt)
1446 struct usb_device *udev = interface_to_usbdev(intf);
1447 int status = 0;
1449 usb_pm_lock(udev);
1450 if (intf->condition == USB_INTERFACE_UNBOUND)
1451 status = -ENODEV;
1452 else {
1453 udev->auto_pm = 1;
1454 intf->pm_usage_cnt += inc_usage_cnt;
1455 udev->last_busy = jiffies;
1456 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
1457 if (udev->state == USB_STATE_SUSPENDED)
1458 status = usb_resume_both(udev);
1459 if (status != 0)
1460 intf->pm_usage_cnt -= inc_usage_cnt;
1461 else
1462 udev->last_busy = jiffies;
1463 } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) {
1464 status = usb_suspend_both(udev, PMSG_SUSPEND);
1467 usb_pm_unlock(udev);
1468 return status;
1472 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1473 * @intf: the usb_interface whose counter should be decremented
1475 * This routine should be called by an interface driver when it is
1476 * finished using @intf and wants to allow it to autosuspend. A typical
1477 * example would be a character-device driver when its device file is
1478 * closed.
1480 * The routine decrements @intf's usage counter. When the counter reaches
1481 * 0, a delayed autosuspend request for @intf's device is queued. When
1482 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1483 * the other usage counters for the sibling interfaces and @intf's
1484 * usb_device, the device and all its interfaces will be autosuspended.
1486 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1487 * core will not change its value other than the increment and decrement
1488 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1489 * may use this simple counter-oriented discipline or may set the value
1490 * any way it likes.
1492 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1493 * take place only if the device's remote-wakeup facility is enabled.
1495 * Suspend method calls queued by this routine can arrive at any time
1496 * while @intf is resumed and its usage counter is equal to 0. They are
1497 * not protected by the usb_device's lock but only by its pm_mutex.
1498 * Drivers must provide their own synchronization.
1500 * This routine can run only in process context.
1502 void usb_autopm_put_interface(struct usb_interface *intf)
1504 int status;
1506 status = usb_autopm_do_interface(intf, -1);
1507 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1508 __FUNCTION__, status, intf->pm_usage_cnt);
1510 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1513 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1514 * @intf: the usb_interface whose counter should be incremented
1516 * This routine should be called by an interface driver when it wants to
1517 * use @intf and needs to guarantee that it is not suspended. In addition,
1518 * the routine prevents @intf from being autosuspended subsequently. (Note
1519 * that this will not prevent suspend events originating in the PM core.)
1520 * This prevention will persist until usb_autopm_put_interface() is called
1521 * or @intf is unbound. A typical example would be a character-device
1522 * driver when its device file is opened.
1525 * The routine increments @intf's usage counter. (However if the
1526 * autoresume fails then the counter is re-decremented.) So long as the
1527 * counter is greater than 0, autosuspend will not be allowed for @intf
1528 * or its usb_device. When the driver is finished using @intf it should
1529 * call usb_autopm_put_interface() to decrement the usage counter and
1530 * queue a delayed autosuspend request (if the counter is <= 0).
1533 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1534 * core will not change its value other than the increment and decrement
1535 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1536 * may use this simple counter-oriented discipline or may set the value
1537 * any way it likes.
1539 * Resume method calls generated by this routine can arrive at any time
1540 * while @intf is suspended. They are not protected by the usb_device's
1541 * lock but only by its pm_mutex. Drivers must provide their own
1542 * synchronization.
1544 * This routine can run only in process context.
1546 int usb_autopm_get_interface(struct usb_interface *intf)
1548 int status;
1550 status = usb_autopm_do_interface(intf, 1);
1551 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1552 __FUNCTION__, status, intf->pm_usage_cnt);
1553 return status;
1555 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1558 * usb_autopm_set_interface - set a USB interface's autosuspend state
1559 * @intf: the usb_interface whose state should be set
1561 * This routine sets the autosuspend state of @intf's device according
1562 * to @intf's usage counter, which the caller must have set previously.
1563 * If the counter is <= 0, the device is autosuspended (if it isn't
1564 * already suspended and if nothing else prevents the autosuspend). If
1565 * the counter is > 0, the device is autoresumed (if it isn't already
1566 * awake).
1568 int usb_autopm_set_interface(struct usb_interface *intf)
1570 int status;
1572 status = usb_autopm_do_interface(intf, 0);
1573 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1574 __FUNCTION__, status, intf->pm_usage_cnt);
1575 return status;
1577 EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1579 #else
1581 void usb_autosuspend_work(struct work_struct *work)
1584 #endif /* CONFIG_USB_SUSPEND */
1587 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1588 * @udev: the usb_device to suspend
1589 * @msg: Power Management message describing this state transition
1591 * This routine handles external suspend requests: ones not generated
1592 * internally by a USB driver (autosuspend) but rather coming from the user
1593 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1594 * out regardless of @udev's usage counter or those of its interfaces,
1595 * and regardless of whether or not remote wakeup is enabled. Of course,
1596 * interface drivers still have the option of failing the suspend (if
1597 * there are unsuspended children, for example).
1599 * The caller must hold @udev's device lock.
1601 int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1603 int status;
1605 do_unbind_rebind(udev, DO_UNBIND);
1606 usb_pm_lock(udev);
1607 udev->auto_pm = 0;
1608 status = usb_suspend_both(udev, msg);
1609 usb_pm_unlock(udev);
1610 return status;
1614 * usb_external_resume_device - external resume of a USB device and its interfaces
1615 * @udev: the usb_device to resume
1617 * This routine handles external resume requests: ones not generated
1618 * internally by a USB driver (autoresume) but rather coming from the user
1619 * (via sysfs), the PM core (system resume), or the device itself (remote
1620 * wakeup). @udev's usage counter is unaffected.
1622 * The caller must hold @udev's device lock.
1624 int usb_external_resume_device(struct usb_device *udev)
1626 int status;
1628 usb_pm_lock(udev);
1629 udev->auto_pm = 0;
1630 status = usb_resume_both(udev);
1631 udev->last_busy = jiffies;
1632 usb_pm_unlock(udev);
1633 if (status == 0)
1634 do_unbind_rebind(udev, DO_REBIND);
1636 /* Now that the device is awake, we can start trying to autosuspend
1637 * it again. */
1638 if (status == 0)
1639 usb_try_autosuspend_device(udev);
1640 return status;
1643 static int usb_suspend(struct device *dev, pm_message_t message)
1645 struct usb_device *udev;
1647 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1648 return 0;
1649 udev = to_usb_device(dev);
1651 /* If udev is already suspended, we can skip this suspend and
1652 * we should also skip the upcoming system resume. High-speed
1653 * root hubs are an exception; they need to resume whenever the
1654 * system wakes up in order for USB-PERSIST port handover to work
1655 * properly.
1657 if (udev->state == USB_STATE_SUSPENDED) {
1658 if (udev->parent || udev->speed != USB_SPEED_HIGH)
1659 udev->skip_sys_resume = 1;
1660 return 0;
1663 udev->skip_sys_resume = 0;
1664 return usb_external_suspend_device(udev, message);
1667 static int usb_resume(struct device *dev)
1669 struct usb_device *udev;
1670 int status;
1672 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1673 return 0;
1674 udev = to_usb_device(dev);
1676 /* If udev->skip_sys_resume is set then udev was already suspended
1677 * when the system sleep started, so we don't want to resume it
1678 * during this system wakeup.
1680 if (udev->skip_sys_resume)
1681 return 0;
1682 status = usb_external_resume_device(udev);
1684 /* Avoid PM error messages for devices disconnected while suspended
1685 * as we'll display regular disconnect messages just a bit later.
1687 if (status == -ENODEV || status == -ESHUTDOWN)
1688 status = 0;
1689 return status;
1692 #endif /* CONFIG_PM */
1694 struct bus_type usb_bus_type = {
1695 .name = "usb",
1696 .match = usb_device_match,
1697 .uevent = usb_uevent,
1698 #ifdef CONFIG_PM
1699 .suspend = usb_suspend,
1700 .resume = usb_resume,
1701 #endif