2 * drivers/usb/driver.c - most of the driver model stuff for usb
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
6 * based on drivers/usb/usb.c which had the following copyrights:
7 * (C) Copyright Linus Torvalds 1999
8 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Andreas Gal 1999
10 * (C) Copyright Gregory P. Smith 1999
11 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
12 * (C) Copyright Randy Dunlap 2000
13 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Yggdrasil Computing, Inc. 2000
15 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright Greg Kroah-Hartman 2002-2003
18 * NOTE! This is not actually a driver at all, rather this is
19 * just a collection of helper routines that implement the
20 * matching, probing, releasing, suspending and resuming for
25 #include <linux/device.h>
26 #include <linux/usb.h>
27 #include <linux/usb/quirks.h>
28 #include <linux/workqueue.h>
35 * Adds a new dynamic USBdevice ID to this driver,
36 * and cause the driver to probe for all devices again.
38 ssize_t usb_store_new_id(struct usb_dynids *dynids,
39 struct device_driver *driver,
40 const char *buf, size_t count)
42 struct usb_dynid *dynid;
48 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
52 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
56 INIT_LIST_HEAD(&dynid->node);
57 dynid->id.idVendor = idVendor;
58 dynid->id.idProduct = idProduct;
59 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
61 spin_lock(&dynids->lock);
62 list_add_tail(&dynids->list, &dynid->node);
63 spin_unlock(&dynids->lock);
65 if (get_driver(driver)) {
66 retval = driver_attach(driver);
74 EXPORT_SYMBOL_GPL(usb_store_new_id);
76 static ssize_t store_new_id(struct device_driver *driver,
77 const char *buf, size_t count)
79 struct usb_driver *usb_drv = to_usb_driver(driver);
81 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
83 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
85 static int usb_create_newid_file(struct usb_driver *usb_drv)
89 if (usb_drv->no_dynamic_id)
92 if (usb_drv->probe != NULL)
93 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
94 &driver_attr_new_id.attr);
99 static void usb_remove_newid_file(struct usb_driver *usb_drv)
101 if (usb_drv->no_dynamic_id)
104 if (usb_drv->probe != NULL)
105 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
106 &driver_attr_new_id.attr);
109 static void usb_free_dynids(struct usb_driver *usb_drv)
111 struct usb_dynid *dynid, *n;
113 spin_lock(&usb_drv->dynids.lock);
114 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
115 list_del(&dynid->node);
118 spin_unlock(&usb_drv->dynids.lock);
121 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
126 static void usb_remove_newid_file(struct usb_driver *usb_drv)
130 static inline void usb_free_dynids(struct usb_driver *usb_drv)
135 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
136 struct usb_driver *drv)
138 struct usb_dynid *dynid;
140 spin_lock(&drv->dynids.lock);
141 list_for_each_entry(dynid, &drv->dynids.list, node) {
142 if (usb_match_one_id(intf, &dynid->id)) {
143 spin_unlock(&drv->dynids.lock);
147 spin_unlock(&drv->dynids.lock);
152 /* called from driver core with dev locked */
153 static int usb_probe_device(struct device *dev)
155 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
156 struct usb_device *udev;
159 dev_dbg(dev, "%s\n", __FUNCTION__);
161 if (!is_usb_device(dev)) /* Sanity check */
164 udev = to_usb_device(dev);
166 /* TODO: Add real matching code */
168 /* The device should always appear to be in use
169 * unless the driver suports autosuspend.
171 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
173 error = udriver->probe(udev);
177 /* called from driver core with dev locked */
178 static int usb_unbind_device(struct device *dev)
180 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
182 udriver->disconnect(to_usb_device(dev));
187 /* called from driver core with dev locked */
188 static int usb_probe_interface(struct device *dev)
190 struct usb_driver *driver = to_usb_driver(dev->driver);
191 struct usb_interface *intf;
192 struct usb_device *udev;
193 const struct usb_device_id *id;
196 dev_dbg(dev, "%s\n", __FUNCTION__);
198 if (is_usb_device(dev)) /* Sanity check */
201 intf = to_usb_interface(dev);
202 udev = interface_to_usbdev(intf);
204 id = usb_match_id(intf, driver->id_table);
206 id = usb_match_dynamic_id(intf, driver);
208 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
210 error = usb_autoresume_device(udev);
214 /* Interface "power state" doesn't correspond to any hardware
215 * state whatsoever. We use it to record when it's bound to
216 * a driver that may start I/0: it's not frozen/quiesced.
219 intf->condition = USB_INTERFACE_BINDING;
221 /* The interface should always appear to be in use
222 * unless the driver suports autosuspend.
224 intf->pm_usage_cnt = !(driver->supports_autosuspend);
226 error = driver->probe(intf, id);
229 intf->needs_remote_wakeup = 0;
230 intf->condition = USB_INTERFACE_UNBOUND;
232 intf->condition = USB_INTERFACE_BOUND;
234 usb_autosuspend_device(udev);
240 /* called from driver core with dev locked */
241 static int usb_unbind_interface(struct device *dev)
243 struct usb_driver *driver = to_usb_driver(dev->driver);
244 struct usb_interface *intf = to_usb_interface(dev);
245 struct usb_device *udev;
248 intf->condition = USB_INTERFACE_UNBINDING;
250 /* Autoresume for set_interface call below */
251 udev = interface_to_usbdev(intf);
252 error = usb_autoresume_device(udev);
254 /* release all urbs for this interface */
255 usb_disable_interface(interface_to_usbdev(intf), intf);
257 driver->disconnect(intf);
259 /* reset other interface state */
260 usb_set_interface(interface_to_usbdev(intf),
261 intf->altsetting[0].desc.bInterfaceNumber,
263 usb_set_intfdata(intf, NULL);
265 intf->condition = USB_INTERFACE_UNBOUND;
267 intf->needs_remote_wakeup = 0;
270 usb_autosuspend_device(udev);
276 * usb_driver_claim_interface - bind a driver to an interface
277 * @driver: the driver to be bound
278 * @iface: the interface to which it will be bound; must be in the
279 * usb device's active configuration
280 * @priv: driver data associated with that interface
282 * This is used by usb device drivers that need to claim more than one
283 * interface on a device when probing (audio and acm are current examples).
284 * No device driver should directly modify internal usb_interface or
285 * usb_device structure members.
287 * Few drivers should need to use this routine, since the most natural
288 * way to bind to an interface is to return the private data from
289 * the driver's probe() method.
291 * Callers must own the device lock, so driver probe() entries don't need
292 * extra locking, but other call contexts may need to explicitly claim that
295 int usb_driver_claim_interface(struct usb_driver *driver,
296 struct usb_interface *iface, void* priv)
298 struct device *dev = &iface->dev;
299 struct usb_device *udev = interface_to_usbdev(iface);
305 dev->driver = &driver->drvwrap.driver;
306 usb_set_intfdata(iface, priv);
309 iface->condition = USB_INTERFACE_BOUND;
311 iface->pm_usage_cnt = !(driver->supports_autosuspend);
314 /* if interface was already added, bind now; else let
315 * the future device_add() bind it, bypassing probe()
317 if (device_is_registered(dev))
318 retval = device_bind_driver(dev);
322 EXPORT_SYMBOL(usb_driver_claim_interface);
325 * usb_driver_release_interface - unbind a driver from an interface
326 * @driver: the driver to be unbound
327 * @iface: the interface from which it will be unbound
329 * This can be used by drivers to release an interface without waiting
330 * for their disconnect() methods to be called. In typical cases this
331 * also causes the driver disconnect() method to be called.
333 * This call is synchronous, and may not be used in an interrupt context.
334 * Callers must own the device lock, so driver disconnect() entries don't
335 * need extra locking, but other call contexts may need to explicitly claim
338 void usb_driver_release_interface(struct usb_driver *driver,
339 struct usb_interface *iface)
341 struct device *dev = &iface->dev;
342 struct usb_device *udev = interface_to_usbdev(iface);
344 /* this should never happen, don't release something that's not ours */
345 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
348 /* don't release from within disconnect() */
349 if (iface->condition != USB_INTERFACE_BOUND)
352 /* don't release if the interface hasn't been added yet */
353 if (device_is_registered(dev)) {
354 iface->condition = USB_INTERFACE_UNBINDING;
355 device_release_driver(dev);
359 usb_set_intfdata(iface, NULL);
362 iface->condition = USB_INTERFACE_UNBOUND;
363 mark_quiesced(iface);
364 iface->needs_remote_wakeup = 0;
367 EXPORT_SYMBOL(usb_driver_release_interface);
369 /* returns 0 if no match, 1 if match */
370 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
372 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
373 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
376 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
377 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
380 /* No need to test id->bcdDevice_lo != 0, since 0 is never
381 greater than any unsigned number. */
382 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
383 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
386 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
387 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
390 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
391 (id->bDeviceClass != dev->descriptor.bDeviceClass))
394 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
395 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
399 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
405 /* returns 0 if no match, 1 if match */
406 int usb_match_one_id(struct usb_interface *interface,
407 const struct usb_device_id *id)
409 struct usb_host_interface *intf;
410 struct usb_device *dev;
412 /* proc_connectinfo in devio.c may call us with id == NULL. */
416 intf = interface->cur_altsetting;
417 dev = interface_to_usbdev(interface);
419 if (!usb_match_device(dev, id))
422 /* The interface class, subclass, and protocol should never be
423 * checked for a match if the device class is Vendor Specific,
424 * unless the match record specifies the Vendor ID. */
425 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
426 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
427 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
428 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
429 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
432 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
433 (id->bInterfaceClass != intf->desc.bInterfaceClass))
436 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
437 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
440 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
441 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
446 EXPORT_SYMBOL_GPL(usb_match_one_id);
449 * usb_match_id - find first usb_device_id matching device or interface
450 * @interface: the interface of interest
451 * @id: array of usb_device_id structures, terminated by zero entry
453 * usb_match_id searches an array of usb_device_id's and returns
454 * the first one matching the device or interface, or null.
455 * This is used when binding (or rebinding) a driver to an interface.
456 * Most USB device drivers will use this indirectly, through the usb core,
457 * but some layered driver frameworks use it directly.
458 * These device tables are exported with MODULE_DEVICE_TABLE, through
459 * modutils, to support the driver loading functionality of USB hotplugging.
463 * The "match_flags" element in a usb_device_id controls which
464 * members are used. If the corresponding bit is set, the
465 * value in the device_id must match its corresponding member
466 * in the device or interface descriptor, or else the device_id
469 * "driver_info" is normally used only by device drivers,
470 * but you can create a wildcard "matches anything" usb_device_id
471 * as a driver's "modules.usbmap" entry if you provide an id with
472 * only a nonzero "driver_info" field. If you do this, the USB device
473 * driver's probe() routine should use additional intelligence to
474 * decide whether to bind to the specified interface.
476 * What Makes Good usb_device_id Tables:
478 * The match algorithm is very simple, so that intelligence in
479 * driver selection must come from smart driver id records.
480 * Unless you have good reasons to use another selection policy,
481 * provide match elements only in related groups, and order match
482 * specifiers from specific to general. Use the macros provided
483 * for that purpose if you can.
485 * The most specific match specifiers use device descriptor
486 * data. These are commonly used with product-specific matches;
487 * the USB_DEVICE macro lets you provide vendor and product IDs,
488 * and you can also match against ranges of product revisions.
489 * These are widely used for devices with application or vendor
490 * specific bDeviceClass values.
492 * Matches based on device class/subclass/protocol specifications
493 * are slightly more general; use the USB_DEVICE_INFO macro, or
494 * its siblings. These are used with single-function devices
495 * where bDeviceClass doesn't specify that each interface has
498 * Matches based on interface class/subclass/protocol are the
499 * most general; they let drivers bind to any interface on a
500 * multiple-function device. Use the USB_INTERFACE_INFO
501 * macro, or its siblings, to match class-per-interface style
502 * devices (as recorded in bInterfaceClass).
504 * Note that an entry created by USB_INTERFACE_INFO won't match
505 * any interface if the device class is set to Vendor-Specific.
506 * This is deliberate; according to the USB spec the meanings of
507 * the interface class/subclass/protocol for these devices are also
508 * vendor-specific, and hence matching against a standard product
509 * class wouldn't work anyway. If you really want to use an
510 * interface-based match for such a device, create a match record
511 * that also specifies the vendor ID. (Unforunately there isn't a
512 * standard macro for creating records like this.)
514 * Within those groups, remember that not all combinations are
515 * meaningful. For example, don't give a product version range
516 * without vendor and product IDs; or specify a protocol without
517 * its associated class and subclass.
519 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
520 const struct usb_device_id *id)
522 /* proc_connectinfo in devio.c may call us with id == NULL. */
526 /* It is important to check that id->driver_info is nonzero,
527 since an entry that is all zeroes except for a nonzero
528 id->driver_info is the way to create an entry that
529 indicates that the driver want to examine every
530 device and interface. */
531 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
532 id->driver_info; id++) {
533 if (usb_match_one_id(interface, id))
539 EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
541 static int usb_device_match(struct device *dev, struct device_driver *drv)
543 /* devices and interfaces are handled separately */
544 if (is_usb_device(dev)) {
546 /* interface drivers never match devices */
547 if (!is_usb_device_driver(drv))
550 /* TODO: Add real matching code */
554 struct usb_interface *intf;
555 struct usb_driver *usb_drv;
556 const struct usb_device_id *id;
558 /* device drivers never match interfaces */
559 if (is_usb_device_driver(drv))
562 intf = to_usb_interface(dev);
563 usb_drv = to_usb_driver(drv);
565 id = usb_match_id(intf, usb_drv->id_table);
569 id = usb_match_dynamic_id(intf, usb_drv);
577 #ifdef CONFIG_HOTPLUG
578 static int usb_uevent(struct device *dev, char **envp, int num_envp,
579 char *buffer, int buffer_size)
581 struct usb_device *usb_dev;
588 /* driver is often null here; dev_dbg() would oops */
589 pr_debug ("usb %s: uevent\n", dev->bus_id);
591 if (is_usb_device(dev))
592 usb_dev = to_usb_device(dev);
594 struct usb_interface *intf = to_usb_interface(dev);
595 usb_dev = interface_to_usbdev(intf);
598 if (usb_dev->devnum < 0) {
599 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
603 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
607 #ifdef CONFIG_USB_DEVICEFS
608 /* If this is available, userspace programs can directly read
609 * all the device descriptors we don't tell them about. Or
610 * act as usermode drivers.
612 if (add_uevent_var(envp, num_envp, &i,
613 buffer, buffer_size, &length,
614 "DEVICE=/proc/bus/usb/%03d/%03d",
615 usb_dev->bus->busnum, usb_dev->devnum))
619 /* per-device configurations are common */
620 if (add_uevent_var(envp, num_envp, &i,
621 buffer, buffer_size, &length,
623 le16_to_cpu(usb_dev->descriptor.idVendor),
624 le16_to_cpu(usb_dev->descriptor.idProduct),
625 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
628 /* class-based driver binding models */
629 if (add_uevent_var(envp, num_envp, &i,
630 buffer, buffer_size, &length,
632 usb_dev->descriptor.bDeviceClass,
633 usb_dev->descriptor.bDeviceSubClass,
634 usb_dev->descriptor.bDeviceProtocol))
637 if (add_uevent_var(envp, num_envp, &i,
638 buffer, buffer_size, &length,
640 usb_dev->bus->busnum))
643 if (add_uevent_var(envp, num_envp, &i,
644 buffer, buffer_size, &length,
655 static int usb_uevent(struct device *dev, char **envp,
656 int num_envp, char *buffer, int buffer_size)
660 #endif /* CONFIG_HOTPLUG */
663 * usb_register_device_driver - register a USB device (not interface) driver
664 * @new_udriver: USB operations for the device driver
665 * @owner: module owner of this driver.
667 * Registers a USB device driver with the USB core. The list of
668 * unattached devices will be rescanned whenever a new driver is
669 * added, allowing the new driver to attach to any recognized devices.
670 * Returns a negative error code on failure and 0 on success.
672 int usb_register_device_driver(struct usb_device_driver *new_udriver,
673 struct module *owner)
680 new_udriver->drvwrap.for_devices = 1;
681 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
682 new_udriver->drvwrap.driver.bus = &usb_bus_type;
683 new_udriver->drvwrap.driver.probe = usb_probe_device;
684 new_udriver->drvwrap.driver.remove = usb_unbind_device;
685 new_udriver->drvwrap.driver.owner = owner;
687 retval = driver_register(&new_udriver->drvwrap.driver);
690 pr_info("%s: registered new device driver %s\n",
691 usbcore_name, new_udriver->name);
692 usbfs_update_special();
694 printk(KERN_ERR "%s: error %d registering device "
696 usbcore_name, retval, new_udriver->name);
701 EXPORT_SYMBOL_GPL(usb_register_device_driver);
704 * usb_deregister_device_driver - unregister a USB device (not interface) driver
705 * @udriver: USB operations of the device driver to unregister
706 * Context: must be able to sleep
708 * Unlinks the specified driver from the internal USB driver list.
710 void usb_deregister_device_driver(struct usb_device_driver *udriver)
712 pr_info("%s: deregistering device driver %s\n",
713 usbcore_name, udriver->name);
715 driver_unregister(&udriver->drvwrap.driver);
716 usbfs_update_special();
718 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
721 * usb_register_driver - register a USB interface driver
722 * @new_driver: USB operations for the interface driver
723 * @owner: module owner of this driver.
724 * @mod_name: module name string
726 * Registers a USB interface driver with the USB core. The list of
727 * unattached interfaces will be rescanned whenever a new driver is
728 * added, allowing the new driver to attach to any recognized interfaces.
729 * Returns a negative error code on failure and 0 on success.
731 * NOTE: if you want your driver to use the USB major number, you must call
732 * usb_register_dev() to enable that functionality. This function no longer
733 * takes care of that.
735 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
736 const char *mod_name)
743 new_driver->drvwrap.for_devices = 0;
744 new_driver->drvwrap.driver.name = (char *) new_driver->name;
745 new_driver->drvwrap.driver.bus = &usb_bus_type;
746 new_driver->drvwrap.driver.probe = usb_probe_interface;
747 new_driver->drvwrap.driver.remove = usb_unbind_interface;
748 new_driver->drvwrap.driver.owner = owner;
749 new_driver->drvwrap.driver.mod_name = mod_name;
750 spin_lock_init(&new_driver->dynids.lock);
751 INIT_LIST_HEAD(&new_driver->dynids.list);
753 retval = driver_register(&new_driver->drvwrap.driver);
756 pr_info("%s: registered new interface driver %s\n",
757 usbcore_name, new_driver->name);
758 usbfs_update_special();
759 usb_create_newid_file(new_driver);
761 printk(KERN_ERR "%s: error %d registering interface "
763 usbcore_name, retval, new_driver->name);
768 EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
771 * usb_deregister - unregister a USB interface driver
772 * @driver: USB operations of the interface driver to unregister
773 * Context: must be able to sleep
775 * Unlinks the specified driver from the internal USB driver list.
777 * NOTE: If you called usb_register_dev(), you still need to call
778 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
779 * this * call will no longer do it for you.
781 void usb_deregister(struct usb_driver *driver)
783 pr_info("%s: deregistering interface driver %s\n",
784 usbcore_name, driver->name);
786 usb_remove_newid_file(driver);
787 usb_free_dynids(driver);
788 driver_unregister(&driver->drvwrap.driver);
790 usbfs_update_special();
792 EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
796 /* Caller has locked udev's pm_mutex */
797 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
799 struct usb_device_driver *udriver;
802 if (udev->state == USB_STATE_NOTATTACHED ||
803 udev->state == USB_STATE_SUSPENDED)
806 /* For devices that don't have a driver, we do a generic suspend. */
807 if (udev->dev.driver)
808 udriver = to_usb_device_driver(udev->dev.driver);
810 udev->do_remote_wakeup = 0;
811 udriver = &usb_generic_driver;
813 status = udriver->suspend(udev, msg);
816 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
818 udev->dev.power.power_state.event = msg.event;
822 /* Caller has locked udev's pm_mutex */
823 static int usb_resume_device(struct usb_device *udev)
825 struct usb_device_driver *udriver;
828 if (udev->state == USB_STATE_NOTATTACHED)
830 if (udev->state != USB_STATE_SUSPENDED && !udev->reset_resume)
833 /* Can't resume it if it doesn't have a driver. */
834 if (udev->dev.driver == NULL) {
839 if (udev->quirks & USB_QUIRK_RESET_RESUME)
840 udev->reset_resume = 1;
842 udriver = to_usb_device_driver(udev->dev.driver);
843 status = udriver->resume(udev);
846 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
848 udev->autoresume_disabled = 0;
849 udev->dev.power.power_state.event = PM_EVENT_ON;
854 /* Caller has locked intf's usb_device's pm mutex */
855 static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg)
857 struct usb_driver *driver;
860 /* with no hardware, USB interfaces only use FREEZE and ON states */
861 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
865 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
867 driver = to_usb_driver(intf->dev.driver);
869 if (driver->suspend && driver->resume) {
870 status = driver->suspend(intf, msg);
873 else if (!interface_to_usbdev(intf)->auto_pm)
874 dev_err(&intf->dev, "%s error %d\n",
877 // FIXME else if there's no suspend method, disconnect...
878 // Not possible if auto_pm is set...
879 dev_warn(&intf->dev, "no suspend for driver %s?\n",
885 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
889 /* Caller has locked intf's usb_device's pm_mutex */
890 static int usb_resume_interface(struct usb_interface *intf, int reset_resume)
892 struct usb_driver *driver;
895 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
899 /* Don't let autoresume interfere with unbinding */
900 if (intf->condition == USB_INTERFACE_UNBINDING)
903 /* Can't resume it if it doesn't have a driver. */
904 if (intf->condition == USB_INTERFACE_UNBOUND) {
908 driver = to_usb_driver(intf->dev.driver);
910 if (reset_resume && driver->post_reset)
911 driver->post_reset(intf, reset_resume);
912 else if (driver->resume) {
913 status = driver->resume(intf);
915 dev_err(&intf->dev, "%s error %d\n",
918 dev_warn(&intf->dev, "no resume for driver %s?\n",
922 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
928 #ifdef CONFIG_USB_SUSPEND
930 /* Internal routine to check whether we may autosuspend a device. */
931 static int autosuspend_check(struct usb_device *udev)
934 struct usb_interface *intf;
935 unsigned long suspend_time;
937 /* For autosuspend, fail fast if anything is in use or autosuspend
938 * is disabled. Also fail if any interfaces require remote wakeup
939 * but it isn't available.
941 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
942 if (udev->pm_usage_cnt > 0)
944 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
947 suspend_time = udev->last_busy + udev->autosuspend_delay;
948 if (udev->actconfig) {
949 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
950 intf = udev->actconfig->interface[i];
951 if (!is_active(intf))
953 if (intf->pm_usage_cnt > 0)
955 if (intf->needs_remote_wakeup &&
956 !udev->do_remote_wakeup) {
957 dev_dbg(&udev->dev, "remote wakeup needed "
958 "for autosuspend\n");
964 /* If everything is okay but the device hasn't been idle for long
965 * enough, queue a delayed autosuspend request.
967 if (time_after(suspend_time, jiffies)) {
968 if (!timer_pending(&udev->autosuspend.timer)) {
970 /* The value of jiffies may change between the
971 * time_after() comparison above and the subtraction
972 * below. That's okay; the system behaves sanely
973 * when a timer is registered for the present moment
976 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
977 suspend_time - jiffies);
986 static inline int autosuspend_check(struct usb_device *udev)
991 #endif /* CONFIG_USB_SUSPEND */
994 * usb_suspend_both - suspend a USB device and its interfaces
995 * @udev: the usb_device to suspend
996 * @msg: Power Management message describing this state transition
998 * This is the central routine for suspending USB devices. It calls the
999 * suspend methods for all the interface drivers in @udev and then calls
1000 * the suspend method for @udev itself. If an error occurs at any stage,
1001 * all the interfaces which were suspended are resumed so that they remain
1002 * in the same state as the device.
1004 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
1005 * checks first to make sure that neither the device itself or any of its
1006 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
1007 * are, the autosuspend fails.
1009 * If the suspend succeeds, the routine recursively queues an autosuspend
1010 * request for @udev's parent device, thereby propagating the change up
1011 * the device tree. If all of the parent's children are now suspended,
1012 * the parent will autosuspend in turn.
1014 * The suspend method calls are subject to mutual exclusion under control
1015 * of @udev's pm_mutex. Many of these calls are also under the protection
1016 * of @udev's device lock (including all requests originating outside the
1017 * USB subsystem), but autosuspend requests generated by a child device or
1018 * interface driver may not be. Usbcore will insure that the method calls
1019 * do not arrive during bind, unbind, or reset operations. However, drivers
1020 * must be prepared to handle suspend calls arriving at unpredictable times.
1021 * The only way to block such calls is to do an autoresume (preventing
1022 * autosuspends) while holding @udev's device lock (preventing outside
1025 * The caller must hold @udev->pm_mutex.
1027 * This routine can run only in process context.
1029 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1033 struct usb_interface *intf;
1034 struct usb_device *parent = udev->parent;
1036 if (udev->state == USB_STATE_NOTATTACHED ||
1037 udev->state == USB_STATE_SUSPENDED)
1040 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1042 if (udev->auto_pm) {
1043 status = autosuspend_check(udev);
1048 /* Suspend all the interfaces and then udev itself */
1049 if (udev->actconfig) {
1050 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1051 intf = udev->actconfig->interface[i];
1052 status = usb_suspend_interface(intf, msg);
1059 /* Non-root devices don't need to do anything for FREEZE
1061 if (udev->parent && (msg.event == PM_EVENT_FREEZE ||
1062 msg.event == PM_EVENT_PRETHAW))
1064 status = usb_suspend_device(udev, msg);
1067 /* If the suspend failed, resume interfaces that did get suspended */
1070 intf = udev->actconfig->interface[i];
1071 usb_resume_interface(intf, 0);
1074 /* Try another autosuspend when the interfaces aren't busy */
1076 autosuspend_check(udev);
1078 /* If the suspend succeeded, propagate it up the tree */
1080 cancel_delayed_work(&udev->autosuspend);
1082 usb_autosuspend_device(parent);
1086 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1091 * usb_resume_both - resume a USB device and its interfaces
1092 * @udev: the usb_device to resume
1094 * This is the central routine for resuming USB devices. It calls the
1095 * the resume method for @udev and then calls the resume methods for all
1096 * the interface drivers in @udev.
1098 * Before starting the resume, the routine calls itself recursively for
1099 * the parent device of @udev, thereby propagating the change up the device
1100 * tree and assuring that @udev will be able to resume. If the parent is
1101 * unable to resume successfully, the routine fails.
1103 * The resume method calls are subject to mutual exclusion under control
1104 * of @udev's pm_mutex. Many of these calls are also under the protection
1105 * of @udev's device lock (including all requests originating outside the
1106 * USB subsystem), but autoresume requests generated by a child device or
1107 * interface driver may not be. Usbcore will insure that the method calls
1108 * do not arrive during bind, unbind, or reset operations. However, drivers
1109 * must be prepared to handle resume calls arriving at unpredictable times.
1110 * The only way to block such calls is to do an autoresume (preventing
1111 * other autoresumes) while holding @udev's device lock (preventing outside
1114 * The caller must hold @udev->pm_mutex.
1116 * This routine can run only in process context.
1118 static int usb_resume_both(struct usb_device *udev)
1122 struct usb_interface *intf;
1123 struct usb_device *parent = udev->parent;
1125 cancel_delayed_work(&udev->autosuspend);
1126 if (udev->state == USB_STATE_NOTATTACHED) {
1131 /* Propagate the resume up the tree, if necessary */
1132 if (udev->state == USB_STATE_SUSPENDED) {
1133 if (udev->auto_pm && udev->autoresume_disabled) {
1138 status = usb_autoresume_device(parent);
1140 status = usb_resume_device(udev);
1142 usb_autosuspend_device(parent);
1144 /* It's possible usb_resume_device()
1145 * failed after the port was
1146 * unsuspended, causing udev to be
1147 * logically disconnected. We don't
1148 * want usb_disconnect() to autosuspend
1149 * the parent again, so tell it that
1150 * udev disconnected while still
1153 USB_STATE_NOTATTACHED)
1154 udev->discon_suspended = 1;
1159 /* We can't progagate beyond the USB subsystem,
1160 * so if a root hub's controller is suspended
1161 * then we're stuck. */
1162 if (udev->dev.parent->power.power_state.event !=
1164 status = -EHOSTUNREACH;
1166 status = usb_resume_device(udev);
1170 /* Needed for setting udev->dev.power.power_state.event,
1171 * for possible debugging message, and for reset_resume. */
1172 status = usb_resume_device(udev);
1175 if (status == 0 && udev->actconfig) {
1176 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1177 intf = udev->actconfig->interface[i];
1178 usb_resume_interface(intf, udev->reset_resume);
1183 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1184 udev->reset_resume = 0;
1188 #ifdef CONFIG_USB_SUSPEND
1190 /* Internal routine to adjust a device's usage counter and change
1191 * its autosuspend state.
1193 static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1199 udev->pm_usage_cnt += inc_usage_cnt;
1200 WARN_ON(udev->pm_usage_cnt < 0);
1201 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1202 if (udev->state == USB_STATE_SUSPENDED)
1203 status = usb_resume_both(udev);
1205 udev->pm_usage_cnt -= inc_usage_cnt;
1206 else if (inc_usage_cnt)
1207 udev->last_busy = jiffies;
1208 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1210 udev->last_busy = jiffies;
1211 status = usb_suspend_both(udev, PMSG_SUSPEND);
1213 usb_pm_unlock(udev);
1217 /* usb_autosuspend_work - callback routine to autosuspend a USB device */
1218 void usb_autosuspend_work(struct work_struct *work)
1220 struct usb_device *udev =
1221 container_of(work, struct usb_device, autosuspend.work);
1223 usb_autopm_do_device(udev, 0);
1227 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1228 * @udev: the usb_device to autosuspend
1230 * This routine should be called when a core subsystem is finished using
1231 * @udev and wants to allow it to autosuspend. Examples would be when
1232 * @udev's device file in usbfs is closed or after a configuration change.
1234 * @udev's usage counter is decremented. If it or any of the usage counters
1235 * for an active interface is greater than 0, no autosuspend request will be
1236 * queued. (If an interface driver does not support autosuspend then its
1237 * usage counter is permanently positive.) Furthermore, if an interface
1238 * driver requires remote-wakeup capability during autosuspend but remote
1239 * wakeup is disabled, the autosuspend will fail.
1241 * Often the caller will hold @udev's device lock, but this is not
1244 * This routine can run only in process context.
1246 void usb_autosuspend_device(struct usb_device *udev)
1250 status = usb_autopm_do_device(udev, -1);
1251 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1252 // __FUNCTION__, udev->pm_usage_cnt);
1256 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1257 * @udev: the usb_device to autosuspend
1259 * This routine should be called when a core subsystem thinks @udev may
1260 * be ready to autosuspend.
1262 * @udev's usage counter left unchanged. If it or any of the usage counters
1263 * for an active interface is greater than 0, or autosuspend is not allowed
1264 * for any other reason, no autosuspend request will be queued.
1266 * This routine can run only in process context.
1268 void usb_try_autosuspend_device(struct usb_device *udev)
1270 usb_autopm_do_device(udev, 0);
1271 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1272 // __FUNCTION__, udev->pm_usage_cnt);
1276 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1277 * @udev: the usb_device to autoresume
1279 * This routine should be called when a core subsystem wants to use @udev
1280 * and needs to guarantee that it is not suspended. No autosuspend will
1281 * occur until usb_autosuspend_device is called. (Note that this will not
1282 * prevent suspend events originating in the PM core.) Examples would be
1283 * when @udev's device file in usbfs is opened or when a remote-wakeup
1284 * request is received.
1286 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1287 * However if the autoresume fails then the usage counter is re-decremented.
1289 * Often the caller will hold @udev's device lock, but this is not
1290 * necessary (and attempting it might cause deadlock).
1292 * This routine can run only in process context.
1294 int usb_autoresume_device(struct usb_device *udev)
1298 status = usb_autopm_do_device(udev, 1);
1299 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1300 // __FUNCTION__, status, udev->pm_usage_cnt);
1304 /* Internal routine to adjust an interface's usage counter and change
1305 * its device's autosuspend state.
1307 static int usb_autopm_do_interface(struct usb_interface *intf,
1310 struct usb_device *udev = interface_to_usbdev(intf);
1314 if (intf->condition == USB_INTERFACE_UNBOUND)
1318 intf->pm_usage_cnt += inc_usage_cnt;
1319 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
1320 if (udev->state == USB_STATE_SUSPENDED)
1321 status = usb_resume_both(udev);
1323 intf->pm_usage_cnt -= inc_usage_cnt;
1324 else if (inc_usage_cnt)
1325 udev->last_busy = jiffies;
1326 } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) {
1328 udev->last_busy = jiffies;
1329 status = usb_suspend_both(udev, PMSG_SUSPEND);
1332 usb_pm_unlock(udev);
1337 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1338 * @intf: the usb_interface whose counter should be decremented
1340 * This routine should be called by an interface driver when it is
1341 * finished using @intf and wants to allow it to autosuspend. A typical
1342 * example would be a character-device driver when its device file is
1345 * The routine decrements @intf's usage counter. When the counter reaches
1346 * 0, a delayed autosuspend request for @intf's device is queued. When
1347 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1348 * the other usage counters for the sibling interfaces and @intf's
1349 * usb_device, the device and all its interfaces will be autosuspended.
1351 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1352 * core will not change its value other than the increment and decrement
1353 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1354 * may use this simple counter-oriented discipline or may set the value
1357 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1358 * take place only if the device's remote-wakeup facility is enabled.
1360 * Suspend method calls queued by this routine can arrive at any time
1361 * while @intf is resumed and its usage counter is equal to 0. They are
1362 * not protected by the usb_device's lock but only by its pm_mutex.
1363 * Drivers must provide their own synchronization.
1365 * This routine can run only in process context.
1367 void usb_autopm_put_interface(struct usb_interface *intf)
1371 status = usb_autopm_do_interface(intf, -1);
1372 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1373 // __FUNCTION__, status, intf->pm_usage_cnt);
1375 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1378 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1379 * @intf: the usb_interface whose counter should be incremented
1381 * This routine should be called by an interface driver when it wants to
1382 * use @intf and needs to guarantee that it is not suspended. In addition,
1383 * the routine prevents @intf from being autosuspended subsequently. (Note
1384 * that this will not prevent suspend events originating in the PM core.)
1385 * This prevention will persist until usb_autopm_put_interface() is called
1386 * or @intf is unbound. A typical example would be a character-device
1387 * driver when its device file is opened.
1390 * The routine increments @intf's usage counter. (However if the
1391 * autoresume fails then the counter is re-decremented.) So long as the
1392 * counter is greater than 0, autosuspend will not be allowed for @intf
1393 * or its usb_device. When the driver is finished using @intf it should
1394 * call usb_autopm_put_interface() to decrement the usage counter and
1395 * queue a delayed autosuspend request (if the counter is <= 0).
1398 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1399 * core will not change its value other than the increment and decrement
1400 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1401 * may use this simple counter-oriented discipline or may set the value
1404 * Resume method calls generated by this routine can arrive at any time
1405 * while @intf is suspended. They are not protected by the usb_device's
1406 * lock but only by its pm_mutex. Drivers must provide their own
1409 * This routine can run only in process context.
1411 int usb_autopm_get_interface(struct usb_interface *intf)
1415 status = usb_autopm_do_interface(intf, 1);
1416 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1417 // __FUNCTION__, status, intf->pm_usage_cnt);
1420 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1423 * usb_autopm_set_interface - set a USB interface's autosuspend state
1424 * @intf: the usb_interface whose state should be set
1426 * This routine sets the autosuspend state of @intf's device according
1427 * to @intf's usage counter, which the caller must have set previously.
1428 * If the counter is <= 0, the device is autosuspended (if it isn't
1429 * already suspended and if nothing else prevents the autosuspend). If
1430 * the counter is > 0, the device is autoresumed (if it isn't already
1433 int usb_autopm_set_interface(struct usb_interface *intf)
1437 status = usb_autopm_do_interface(intf, 0);
1438 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1439 // __FUNCTION__, status, intf->pm_usage_cnt);
1442 EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1446 void usb_autosuspend_work(struct work_struct *work)
1449 #endif /* CONFIG_USB_SUSPEND */
1452 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1453 * @udev: the usb_device to suspend
1454 * @msg: Power Management message describing this state transition
1456 * This routine handles external suspend requests: ones not generated
1457 * internally by a USB driver (autosuspend) but rather coming from the user
1458 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1459 * out regardless of @udev's usage counter or those of its interfaces,
1460 * and regardless of whether or not remote wakeup is enabled. Of course,
1461 * interface drivers still have the option of failing the suspend (if
1462 * there are unsuspended children, for example).
1464 * The caller must hold @udev's device lock.
1466 int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1472 status = usb_suspend_both(udev, msg);
1473 usb_pm_unlock(udev);
1478 * usb_external_resume_device - external resume of a USB device and its interfaces
1479 * @udev: the usb_device to resume
1481 * This routine handles external resume requests: ones not generated
1482 * internally by a USB driver (autoresume) but rather coming from the user
1483 * (via sysfs), the PM core (system resume), or the device itself (remote
1484 * wakeup). @udev's usage counter is unaffected.
1486 * The caller must hold @udev's device lock.
1488 int usb_external_resume_device(struct usb_device *udev)
1494 status = usb_resume_both(udev);
1495 udev->last_busy = jiffies;
1496 usb_pm_unlock(udev);
1498 /* Now that the device is awake, we can start trying to autosuspend
1501 usb_try_autosuspend_device(udev);
1505 static int usb_suspend(struct device *dev, pm_message_t message)
1507 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1509 return usb_external_suspend_device(to_usb_device(dev), message);
1512 static int usb_resume(struct device *dev)
1514 struct usb_device *udev;
1516 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1518 udev = to_usb_device(dev);
1520 /* If autoresume is disabled then we also want to prevent resume
1521 * during system wakeup. However, a "persistent-device" reset-resume
1522 * after power loss counts as a wakeup event. So allow a
1523 * reset-resume to occur if remote wakeup is enabled. */
1524 if (udev->autoresume_disabled) {
1525 if (!(udev->reset_resume && udev->do_remote_wakeup))
1528 return usb_external_resume_device(udev);
1533 #define usb_suspend NULL
1534 #define usb_resume NULL
1536 #endif /* CONFIG_PM */
1538 struct bus_type usb_bus_type = {
1540 .match = usb_device_match,
1541 .uevent = usb_uevent,
1542 .suspend = usb_suspend,
1543 .resume = usb_resume,