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>
30 static int usb_match_one_id(struct usb_interface *interface,
31 const struct usb_device_id *id);
34 struct list_head node;
35 struct usb_device_id id;
41 * Adds a new dynamic USBdevice ID to this driver,
42 * and cause the driver to probe for all devices again.
44 static ssize_t store_new_id(struct device_driver *driver,
45 const char *buf, size_t count)
47 struct usb_driver *usb_drv = to_usb_driver(driver);
48 struct usb_dynid *dynid;
54 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
58 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
62 INIT_LIST_HEAD(&dynid->node);
63 dynid->id.idVendor = idVendor;
64 dynid->id.idProduct = idProduct;
65 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
67 spin_lock(&usb_drv->dynids.lock);
68 list_add_tail(&usb_drv->dynids.list, &dynid->node);
69 spin_unlock(&usb_drv->dynids.lock);
71 if (get_driver(driver)) {
72 retval = driver_attach(driver);
80 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
82 static int usb_create_newid_file(struct usb_driver *usb_drv)
86 if (usb_drv->no_dynamic_id)
89 if (usb_drv->probe != NULL)
90 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
91 &driver_attr_new_id.attr);
96 static void usb_remove_newid_file(struct usb_driver *usb_drv)
98 if (usb_drv->no_dynamic_id)
101 if (usb_drv->probe != NULL)
102 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
103 &driver_attr_new_id.attr);
106 static void usb_free_dynids(struct usb_driver *usb_drv)
108 struct usb_dynid *dynid, *n;
110 spin_lock(&usb_drv->dynids.lock);
111 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
112 list_del(&dynid->node);
115 spin_unlock(&usb_drv->dynids.lock);
118 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
123 static void usb_remove_newid_file(struct usb_driver *usb_drv)
127 static inline void usb_free_dynids(struct usb_driver *usb_drv)
132 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
133 struct usb_driver *drv)
135 struct usb_dynid *dynid;
137 spin_lock(&drv->dynids.lock);
138 list_for_each_entry(dynid, &drv->dynids.list, node) {
139 if (usb_match_one_id(intf, &dynid->id)) {
140 spin_unlock(&drv->dynids.lock);
144 spin_unlock(&drv->dynids.lock);
149 /* called from driver core with dev locked */
150 static int usb_probe_device(struct device *dev)
152 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
153 struct usb_device *udev;
156 dev_dbg(dev, "%s\n", __FUNCTION__);
158 if (!is_usb_device(dev)) /* Sanity check */
161 udev = to_usb_device(dev);
163 /* TODO: Add real matching code */
165 /* The device should always appear to be in use
166 * unless the driver suports autosuspend.
168 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
170 error = udriver->probe(udev);
174 /* called from driver core with dev locked */
175 static int usb_unbind_device(struct device *dev)
177 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
179 udriver->disconnect(to_usb_device(dev));
184 /* called from driver core with dev locked */
185 static int usb_probe_interface(struct device *dev)
187 struct usb_driver *driver = to_usb_driver(dev->driver);
188 struct usb_interface *intf;
189 struct usb_device *udev;
190 const struct usb_device_id *id;
193 dev_dbg(dev, "%s\n", __FUNCTION__);
195 if (is_usb_device(dev)) /* Sanity check */
198 intf = to_usb_interface(dev);
199 udev = interface_to_usbdev(intf);
201 id = usb_match_id(intf, driver->id_table);
203 id = usb_match_dynamic_id(intf, driver);
205 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
207 error = usb_autoresume_device(udev, 1);
211 /* Interface "power state" doesn't correspond to any hardware
212 * state whatsoever. We use it to record when it's bound to
213 * a driver that may start I/0: it's not frozen/quiesced.
216 intf->condition = USB_INTERFACE_BINDING;
218 /* The interface should always appear to be in use
219 * unless the driver suports autosuspend.
221 intf->pm_usage_cnt = !(driver->supports_autosuspend);
223 error = driver->probe(intf, id);
226 intf->needs_remote_wakeup = 0;
227 intf->condition = USB_INTERFACE_UNBOUND;
229 intf->condition = USB_INTERFACE_BOUND;
231 usb_autosuspend_device(udev, 1);
237 /* called from driver core with dev locked */
238 static int usb_unbind_interface(struct device *dev)
240 struct usb_driver *driver = to_usb_driver(dev->driver);
241 struct usb_interface *intf = to_usb_interface(dev);
242 struct usb_device *udev;
245 intf->condition = USB_INTERFACE_UNBINDING;
247 /* Autoresume for set_interface call below */
248 udev = interface_to_usbdev(intf);
249 error = usb_autoresume_device(udev, 1);
251 /* release all urbs for this interface */
252 usb_disable_interface(interface_to_usbdev(intf), intf);
254 driver->disconnect(intf);
256 /* reset other interface state */
257 usb_set_interface(interface_to_usbdev(intf),
258 intf->altsetting[0].desc.bInterfaceNumber,
260 usb_set_intfdata(intf, NULL);
262 intf->condition = USB_INTERFACE_UNBOUND;
264 intf->needs_remote_wakeup = 0;
267 usb_autosuspend_device(udev, 1);
273 * usb_driver_claim_interface - bind a driver to an interface
274 * @driver: the driver to be bound
275 * @iface: the interface to which it will be bound; must be in the
276 * usb device's active configuration
277 * @priv: driver data associated with that interface
279 * This is used by usb device drivers that need to claim more than one
280 * interface on a device when probing (audio and acm are current examples).
281 * No device driver should directly modify internal usb_interface or
282 * usb_device structure members.
284 * Few drivers should need to use this routine, since the most natural
285 * way to bind to an interface is to return the private data from
286 * the driver's probe() method.
288 * Callers must own the device lock and the driver model's usb_bus_type.subsys
289 * writelock. So driver probe() entries don't need extra locking,
290 * but other call contexts may need to explicitly claim those locks.
292 int usb_driver_claim_interface(struct usb_driver *driver,
293 struct usb_interface *iface, void* priv)
295 struct device *dev = &iface->dev;
296 struct usb_device *udev = interface_to_usbdev(iface);
302 dev->driver = &driver->drvwrap.driver;
303 usb_set_intfdata(iface, priv);
305 mutex_lock_nested(&udev->pm_mutex, udev->level);
306 iface->condition = USB_INTERFACE_BOUND;
308 iface->pm_usage_cnt = !(driver->supports_autosuspend);
309 mutex_unlock(&udev->pm_mutex);
311 /* if interface was already added, bind now; else let
312 * the future device_add() bind it, bypassing probe()
314 if (device_is_registered(dev))
315 retval = device_bind_driver(dev);
319 EXPORT_SYMBOL(usb_driver_claim_interface);
322 * usb_driver_release_interface - unbind a driver from an interface
323 * @driver: the driver to be unbound
324 * @iface: the interface from which it will be unbound
326 * This can be used by drivers to release an interface without waiting
327 * for their disconnect() methods to be called. In typical cases this
328 * also causes the driver disconnect() method to be called.
330 * This call is synchronous, and may not be used in an interrupt context.
331 * Callers must own the device lock and the driver model's usb_bus_type.subsys
332 * writelock. So driver disconnect() entries don't need extra locking,
333 * but other call contexts may need to explicitly claim those locks.
335 void usb_driver_release_interface(struct usb_driver *driver,
336 struct usb_interface *iface)
338 struct device *dev = &iface->dev;
339 struct usb_device *udev = interface_to_usbdev(iface);
341 /* this should never happen, don't release something that's not ours */
342 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
345 /* don't release from within disconnect() */
346 if (iface->condition != USB_INTERFACE_BOUND)
349 /* don't release if the interface hasn't been added yet */
350 if (device_is_registered(dev)) {
351 iface->condition = USB_INTERFACE_UNBINDING;
352 device_release_driver(dev);
356 usb_set_intfdata(iface, NULL);
358 mutex_lock_nested(&udev->pm_mutex, udev->level);
359 iface->condition = USB_INTERFACE_UNBOUND;
360 mark_quiesced(iface);
361 iface->needs_remote_wakeup = 0;
362 mutex_unlock(&udev->pm_mutex);
364 EXPORT_SYMBOL(usb_driver_release_interface);
366 /* returns 0 if no match, 1 if match */
367 static int usb_match_one_id(struct usb_interface *interface,
368 const struct usb_device_id *id)
370 struct usb_host_interface *intf;
371 struct usb_device *dev;
373 /* proc_connectinfo in devio.c may call us with id == NULL. */
377 intf = interface->cur_altsetting;
378 dev = interface_to_usbdev(interface);
380 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
381 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
384 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
385 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
388 /* No need to test id->bcdDevice_lo != 0, since 0 is never
389 greater than any unsigned number. */
390 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
391 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
394 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
395 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
399 (id->bDeviceClass != dev->descriptor.bDeviceClass))
402 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
403 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
406 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
407 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
410 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
411 (id->bInterfaceClass != intf->desc.bInterfaceClass))
414 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
415 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
418 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
419 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
425 * usb_match_id - find first usb_device_id matching device or interface
426 * @interface: the interface of interest
427 * @id: array of usb_device_id structures, terminated by zero entry
429 * usb_match_id searches an array of usb_device_id's and returns
430 * the first one matching the device or interface, or null.
431 * This is used when binding (or rebinding) a driver to an interface.
432 * Most USB device drivers will use this indirectly, through the usb core,
433 * but some layered driver frameworks use it directly.
434 * These device tables are exported with MODULE_DEVICE_TABLE, through
435 * modutils, to support the driver loading functionality of USB hotplugging.
439 * The "match_flags" element in a usb_device_id controls which
440 * members are used. If the corresponding bit is set, the
441 * value in the device_id must match its corresponding member
442 * in the device or interface descriptor, or else the device_id
445 * "driver_info" is normally used only by device drivers,
446 * but you can create a wildcard "matches anything" usb_device_id
447 * as a driver's "modules.usbmap" entry if you provide an id with
448 * only a nonzero "driver_info" field. If you do this, the USB device
449 * driver's probe() routine should use additional intelligence to
450 * decide whether to bind to the specified interface.
452 * What Makes Good usb_device_id Tables:
454 * The match algorithm is very simple, so that intelligence in
455 * driver selection must come from smart driver id records.
456 * Unless you have good reasons to use another selection policy,
457 * provide match elements only in related groups, and order match
458 * specifiers from specific to general. Use the macros provided
459 * for that purpose if you can.
461 * The most specific match specifiers use device descriptor
462 * data. These are commonly used with product-specific matches;
463 * the USB_DEVICE macro lets you provide vendor and product IDs,
464 * and you can also match against ranges of product revisions.
465 * These are widely used for devices with application or vendor
466 * specific bDeviceClass values.
468 * Matches based on device class/subclass/protocol specifications
469 * are slightly more general; use the USB_DEVICE_INFO macro, or
470 * its siblings. These are used with single-function devices
471 * where bDeviceClass doesn't specify that each interface has
474 * Matches based on interface class/subclass/protocol are the
475 * most general; they let drivers bind to any interface on a
476 * multiple-function device. Use the USB_INTERFACE_INFO
477 * macro, or its siblings, to match class-per-interface style
478 * devices (as recorded in bDeviceClass).
480 * Within those groups, remember that not all combinations are
481 * meaningful. For example, don't give a product version range
482 * without vendor and product IDs; or specify a protocol without
483 * its associated class and subclass.
485 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
486 const struct usb_device_id *id)
488 /* proc_connectinfo in devio.c may call us with id == NULL. */
492 /* It is important to check that id->driver_info is nonzero,
493 since an entry that is all zeroes except for a nonzero
494 id->driver_info is the way to create an entry that
495 indicates that the driver want to examine every
496 device and interface. */
497 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
498 id->driver_info; id++) {
499 if (usb_match_one_id(interface, id))
505 EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
507 int usb_device_match(struct device *dev, struct device_driver *drv)
509 /* devices and interfaces are handled separately */
510 if (is_usb_device(dev)) {
512 /* interface drivers never match devices */
513 if (!is_usb_device_driver(drv))
516 /* TODO: Add real matching code */
520 struct usb_interface *intf;
521 struct usb_driver *usb_drv;
522 const struct usb_device_id *id;
524 /* device drivers never match interfaces */
525 if (is_usb_device_driver(drv))
528 intf = to_usb_interface(dev);
529 usb_drv = to_usb_driver(drv);
531 id = usb_match_id(intf, usb_drv->id_table);
535 id = usb_match_dynamic_id(intf, usb_drv);
543 #ifdef CONFIG_HOTPLUG
546 * This sends an uevent to userspace, typically helping to load driver
547 * or other modules, configure the device, and more. Drivers can provide
548 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
550 * We're called either from khubd (the typical case) or from root hub
551 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
552 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
553 * device (and this configuration!) are still present.
555 static int usb_uevent(struct device *dev, char **envp, int num_envp,
556 char *buffer, int buffer_size)
558 struct usb_interface *intf;
559 struct usb_device *usb_dev;
560 struct usb_host_interface *alt;
567 /* driver is often null here; dev_dbg() would oops */
568 pr_debug ("usb %s: uevent\n", dev->bus_id);
570 if (is_usb_device(dev)) {
571 usb_dev = to_usb_device(dev);
574 intf = to_usb_interface(dev);
575 usb_dev = interface_to_usbdev(intf);
576 alt = intf->cur_altsetting;
579 if (usb_dev->devnum < 0) {
580 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
584 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
588 #ifdef CONFIG_USB_DEVICEFS
589 /* If this is available, userspace programs can directly read
590 * all the device descriptors we don't tell them about. Or
591 * even act as usermode drivers.
593 * FIXME reduce hardwired intelligence here
595 if (add_uevent_var(envp, num_envp, &i,
596 buffer, buffer_size, &length,
597 "DEVICE=/proc/bus/usb/%03d/%03d",
598 usb_dev->bus->busnum, usb_dev->devnum))
602 /* per-device configurations are common */
603 if (add_uevent_var(envp, num_envp, &i,
604 buffer, buffer_size, &length,
606 le16_to_cpu(usb_dev->descriptor.idVendor),
607 le16_to_cpu(usb_dev->descriptor.idProduct),
608 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
611 /* class-based driver binding models */
612 if (add_uevent_var(envp, num_envp, &i,
613 buffer, buffer_size, &length,
615 usb_dev->descriptor.bDeviceClass,
616 usb_dev->descriptor.bDeviceSubClass,
617 usb_dev->descriptor.bDeviceProtocol))
620 if (!is_usb_device(dev)) {
622 if (add_uevent_var(envp, num_envp, &i,
623 buffer, buffer_size, &length,
624 "INTERFACE=%d/%d/%d",
625 alt->desc.bInterfaceClass,
626 alt->desc.bInterfaceSubClass,
627 alt->desc.bInterfaceProtocol))
630 if (add_uevent_var(envp, num_envp, &i,
631 buffer, buffer_size, &length,
632 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
633 le16_to_cpu(usb_dev->descriptor.idVendor),
634 le16_to_cpu(usb_dev->descriptor.idProduct),
635 le16_to_cpu(usb_dev->descriptor.bcdDevice),
636 usb_dev->descriptor.bDeviceClass,
637 usb_dev->descriptor.bDeviceSubClass,
638 usb_dev->descriptor.bDeviceProtocol,
639 alt->desc.bInterfaceClass,
640 alt->desc.bInterfaceSubClass,
641 alt->desc.bInterfaceProtocol))
652 static int usb_uevent(struct device *dev, char **envp,
653 int num_envp, char *buffer, int buffer_size)
658 #endif /* CONFIG_HOTPLUG */
661 * usb_register_device_driver - register a USB device (not interface) driver
662 * @new_udriver: USB operations for the device driver
663 * @owner: module owner of this driver.
665 * Registers a USB device driver with the USB core. The list of
666 * unattached devices will be rescanned whenever a new driver is
667 * added, allowing the new driver to attach to any recognized devices.
668 * Returns a negative error code on failure and 0 on success.
670 int usb_register_device_driver(struct usb_device_driver *new_udriver,
671 struct module *owner)
678 new_udriver->drvwrap.for_devices = 1;
679 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
680 new_udriver->drvwrap.driver.bus = &usb_bus_type;
681 new_udriver->drvwrap.driver.probe = usb_probe_device;
682 new_udriver->drvwrap.driver.remove = usb_unbind_device;
683 new_udriver->drvwrap.driver.owner = owner;
685 retval = driver_register(&new_udriver->drvwrap.driver);
688 pr_info("%s: registered new device driver %s\n",
689 usbcore_name, new_udriver->name);
690 usbfs_update_special();
692 printk(KERN_ERR "%s: error %d registering device "
694 usbcore_name, retval, new_udriver->name);
699 EXPORT_SYMBOL_GPL(usb_register_device_driver);
702 * usb_deregister_device_driver - unregister a USB device (not interface) driver
703 * @udriver: USB operations of the device driver to unregister
704 * Context: must be able to sleep
706 * Unlinks the specified driver from the internal USB driver list.
708 void usb_deregister_device_driver(struct usb_device_driver *udriver)
710 pr_info("%s: deregistering device driver %s\n",
711 usbcore_name, udriver->name);
713 driver_unregister(&udriver->drvwrap.driver);
714 usbfs_update_special();
716 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
719 * usb_register_driver - register a USB interface driver
720 * @new_driver: USB operations for the interface driver
721 * @owner: module owner of this driver.
723 * Registers a USB interface driver with the USB core. The list of
724 * unattached interfaces will be rescanned whenever a new driver is
725 * added, allowing the new driver to attach to any recognized interfaces.
726 * Returns a negative error code on failure and 0 on success.
728 * NOTE: if you want your driver to use the USB major number, you must call
729 * usb_register_dev() to enable that functionality. This function no longer
730 * takes care of that.
732 int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
739 new_driver->drvwrap.for_devices = 0;
740 new_driver->drvwrap.driver.name = (char *) new_driver->name;
741 new_driver->drvwrap.driver.bus = &usb_bus_type;
742 new_driver->drvwrap.driver.probe = usb_probe_interface;
743 new_driver->drvwrap.driver.remove = usb_unbind_interface;
744 new_driver->drvwrap.driver.owner = owner;
745 spin_lock_init(&new_driver->dynids.lock);
746 INIT_LIST_HEAD(&new_driver->dynids.list);
748 retval = driver_register(&new_driver->drvwrap.driver);
751 pr_info("%s: registered new interface driver %s\n",
752 usbcore_name, new_driver->name);
753 usbfs_update_special();
754 usb_create_newid_file(new_driver);
756 printk(KERN_ERR "%s: error %d registering interface "
758 usbcore_name, retval, new_driver->name);
763 EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
766 * usb_deregister - unregister a USB interface driver
767 * @driver: USB operations of the interface driver to unregister
768 * Context: must be able to sleep
770 * Unlinks the specified driver from the internal USB driver list.
772 * NOTE: If you called usb_register_dev(), you still need to call
773 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
774 * this * call will no longer do it for you.
776 void usb_deregister(struct usb_driver *driver)
778 pr_info("%s: deregistering interface driver %s\n",
779 usbcore_name, driver->name);
781 usb_remove_newid_file(driver);
782 usb_free_dynids(driver);
783 driver_unregister(&driver->drvwrap.driver);
785 usbfs_update_special();
787 EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
791 /* Caller has locked udev->pm_mutex */
792 static int suspend_device(struct usb_device *udev, pm_message_t msg)
794 struct usb_device_driver *udriver;
797 if (udev->state == USB_STATE_NOTATTACHED ||
798 udev->state == USB_STATE_SUSPENDED)
801 /* For devices that don't have a driver, we do a standard suspend. */
802 if (udev->dev.driver == NULL) {
803 udev->do_remote_wakeup = 0;
804 status = usb_port_suspend(udev);
808 udriver = to_usb_device_driver(udev->dev.driver);
809 status = udriver->suspend(udev, msg);
812 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
814 udev->dev.power.power_state.event = msg.event;
818 /* Caller has locked udev->pm_mutex */
819 static int resume_device(struct usb_device *udev)
821 struct usb_device_driver *udriver;
824 if (udev->state == USB_STATE_NOTATTACHED ||
825 udev->state != USB_STATE_SUSPENDED)
828 /* Can't resume it if it doesn't have a driver. */
829 if (udev->dev.driver == NULL) {
834 udriver = to_usb_device_driver(udev->dev.driver);
835 status = udriver->resume(udev);
838 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
840 udev->dev.power.power_state.event = PM_EVENT_ON;
844 /* Caller has locked intf's usb_device's pm_mutex */
845 static int suspend_interface(struct usb_interface *intf, pm_message_t msg)
847 struct usb_driver *driver;
850 /* with no hardware, USB interfaces only use FREEZE and ON states */
851 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
855 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
857 driver = to_usb_driver(intf->dev.driver);
859 if (driver->suspend && driver->resume) {
860 status = driver->suspend(intf, msg);
863 else if (!interface_to_usbdev(intf)->auto_pm)
864 dev_err(&intf->dev, "%s error %d\n",
867 // FIXME else if there's no suspend method, disconnect...
868 // Not possible if auto_pm is set...
869 dev_warn(&intf->dev, "no suspend for driver %s?\n",
875 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
877 intf->dev.power.power_state.event = msg.event;
881 /* Caller has locked intf's usb_device's pm_mutex */
882 static int resume_interface(struct usb_interface *intf)
884 struct usb_driver *driver;
887 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
891 /* Don't let autoresume interfere with unbinding */
892 if (intf->condition == USB_INTERFACE_UNBINDING)
895 /* Can't resume it if it doesn't have a driver. */
896 if (intf->condition == USB_INTERFACE_UNBOUND) {
900 driver = to_usb_driver(intf->dev.driver);
902 if (driver->resume) {
903 status = driver->resume(intf);
905 dev_err(&intf->dev, "%s error %d\n",
910 dev_warn(&intf->dev, "no resume for driver %s?\n",
916 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
918 intf->dev.power.power_state.event = PM_EVENT_ON;
923 * usb_suspend_both - suspend a USB device and its interfaces
924 * @udev: the usb_device to suspend
925 * @msg: Power Management message describing this state transition
927 * This is the central routine for suspending USB devices. It calls the
928 * suspend methods for all the interface drivers in @udev and then calls
929 * the suspend method for @udev itself. If an error occurs at any stage,
930 * all the interfaces which were suspended are resumed so that they remain
931 * in the same state as the device.
933 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
934 * checks first to make sure that neither the device itself or any of its
935 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
936 * are, the autosuspend fails.
938 * If the suspend succeeds, the routine recursively queues an autosuspend
939 * request for @udev's parent device, thereby propagating the change up
940 * the device tree. If all of the parent's children are now suspended,
941 * the parent will autosuspend in turn.
943 * The suspend method calls are subject to mutual exclusion under control
944 * of @udev's pm_mutex. Many of these calls are also under the protection
945 * of @udev's device lock (including all requests originating outside the
946 * USB subsystem), but autosuspend requests generated by a child device or
947 * interface driver may not be. Usbcore will insure that the method calls
948 * do not arrive during bind, unbind, or reset operations. However, drivers
949 * must be prepared to handle suspend calls arriving at unpredictable times.
950 * The only way to block such calls is to do an autoresume (preventing
951 * autosuspends) while holding @udev's device lock (preventing outside
954 * The caller must hold @udev->pm_mutex.
956 * This routine can run only in process context.
958 int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
962 struct usb_interface *intf;
963 struct usb_device *parent = udev->parent;
965 cancel_delayed_work(&udev->autosuspend);
966 if (udev->state == USB_STATE_NOTATTACHED)
968 if (udev->state == USB_STATE_SUSPENDED)
971 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
973 /* For autosuspend, fail fast if anything is in use.
974 * Also fail if any interfaces require remote wakeup but it
975 * isn't available. */
977 if (udev->pm_usage_cnt > 0)
979 if (udev->actconfig) {
980 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
981 intf = udev->actconfig->interface[i];
982 if (!is_active(intf))
984 if (intf->pm_usage_cnt > 0)
986 if (intf->needs_remote_wakeup &&
987 !udev->do_remote_wakeup) {
989 "remote wakeup needed for autosuspend\n");
997 /* Suspend all the interfaces and then udev itself */
998 if (udev->actconfig) {
999 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1000 intf = udev->actconfig->interface[i];
1001 status = suspend_interface(intf, msg);
1007 status = suspend_device(udev, msg);
1009 /* If the suspend failed, resume interfaces that did get suspended */
1012 intf = udev->actconfig->interface[i];
1013 resume_interface(intf);
1016 /* If the suspend succeeded, propagate it up the tree */
1018 usb_autosuspend_device(parent, 0);
1020 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1025 * usb_resume_both - resume a USB device and its interfaces
1026 * @udev: the usb_device to resume
1028 * This is the central routine for resuming USB devices. It calls the
1029 * the resume method for @udev and then calls the resume methods for all
1030 * the interface drivers in @udev.
1032 * Before starting the resume, the routine calls itself recursively for
1033 * the parent device of @udev, thereby propagating the change up the device
1034 * tree and assuring that @udev will be able to resume. If the parent is
1035 * unable to resume successfully, the routine fails.
1037 * The resume method calls are subject to mutual exclusion under control
1038 * of @udev's pm_mutex. Many of these calls are also under the protection
1039 * of @udev's device lock (including all requests originating outside the
1040 * USB subsystem), but autoresume requests generated by a child device or
1041 * interface driver may not be. Usbcore will insure that the method calls
1042 * do not arrive during bind, unbind, or reset operations. However, drivers
1043 * must be prepared to handle resume calls arriving at unpredictable times.
1044 * The only way to block such calls is to do an autoresume (preventing
1045 * other autoresumes) while holding @udev's device lock (preventing outside
1048 * The caller must hold @udev->pm_mutex.
1050 * This routine can run only in process context.
1052 int usb_resume_both(struct usb_device *udev)
1056 struct usb_interface *intf;
1057 struct usb_device *parent = udev->parent;
1059 cancel_delayed_work(&udev->autosuspend);
1060 if (udev->state == USB_STATE_NOTATTACHED)
1063 /* Propagate the resume up the tree, if necessary */
1064 if (udev->state == USB_STATE_SUSPENDED) {
1066 mutex_lock_nested(&parent->pm_mutex, parent->level);
1067 parent->auto_pm = 1;
1068 status = usb_resume_both(parent);
1071 /* We can't progagate beyond the USB subsystem,
1072 * so if a root hub's controller is suspended
1073 * then we're stuck. */
1074 if (udev->dev.parent->power.power_state.event !=
1076 status = -EHOSTUNREACH;
1079 status = resume_device(udev);
1081 mutex_unlock(&parent->pm_mutex);
1084 /* Needed only for setting udev->dev.power.power_state.event
1085 * and for possible debugging message. */
1086 status = resume_device(udev);
1089 /* Now the parent won't suspend until we are finished */
1091 if (status == 0 && udev->actconfig) {
1092 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1093 intf = udev->actconfig->interface[i];
1094 resume_interface(intf);
1098 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1102 #ifdef CONFIG_USB_SUSPEND
1105 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1106 * @udev - the usb_device to autosuspend
1107 * @dec_usage_cnt - flag to decrement @udev's PM-usage counter
1109 * This routine should be called when a core subsystem is finished using
1110 * @udev and wants to allow it to autosuspend. Examples would be when
1111 * @udev's device file in usbfs is closed or after a configuration change.
1113 * @dec_usage_cnt should be 1 if the subsystem previously incremented
1114 * @udev's usage counter (such as by passing 1 to usb_autoresume_device);
1115 * otherwise it should be 0.
1117 * If the usage counter for @udev or any of its active interfaces is greater
1118 * than 0, the autosuspend request will not be queued. (If an interface
1119 * driver does not support autosuspend then its usage counter is permanently
1120 * positive.) Likewise, if an interface driver requires remote-wakeup
1121 * capability during autosuspend but remote wakeup is disabled, the
1122 * autosuspend will fail.
1124 * Often the caller will hold @udev's device lock, but this is not
1127 * This routine can run only in process context.
1129 void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt)
1131 mutex_lock_nested(&udev->pm_mutex, udev->level);
1132 udev->pm_usage_cnt -= dec_usage_cnt;
1133 if (udev->pm_usage_cnt <= 0)
1134 schedule_delayed_work(&udev->autosuspend,
1135 USB_AUTOSUSPEND_DELAY);
1136 mutex_unlock(&udev->pm_mutex);
1137 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1138 // __FUNCTION__, udev->pm_usage_cnt);
1142 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1143 * @udev - the usb_device to autoresume
1144 * @inc_usage_cnt - flag to increment @udev's PM-usage counter
1146 * This routine should be called when a core subsystem wants to use @udev
1147 * and needs to guarantee that it is not suspended. In addition, the
1148 * caller can prevent @udev from being autosuspended subsequently. (Note
1149 * that this will not prevent suspend events originating in the PM core.)
1150 * Examples would be when @udev's device file in usbfs is opened (autosuspend
1151 * should be prevented until the file is closed) or when a remote-wakeup
1152 * request is received (later autosuspends should not be prevented).
1154 * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent
1155 * autosuspends. This prevention will persist until the usage counter is
1156 * decremented again (such as by passing 1 to usb_autosuspend_device).
1157 * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged.
1158 * Regardless, if the autoresume fails then the usage counter is not
1161 * Often the caller will hold @udev's device lock, but this is not
1162 * necessary (and attempting it might cause deadlock).
1164 * This routine can run only in process context.
1166 int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt)
1170 mutex_lock_nested(&udev->pm_mutex, udev->level);
1171 udev->pm_usage_cnt += inc_usage_cnt;
1173 status = usb_resume_both(udev);
1175 udev->pm_usage_cnt -= inc_usage_cnt;
1176 mutex_unlock(&udev->pm_mutex);
1177 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1178 // __FUNCTION__, status, udev->pm_usage_cnt);
1183 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1184 * @intf - the usb_interface whose counter should be decremented
1186 * This routine should be called by an interface driver when it is
1187 * finished using @intf and wants to allow it to autosuspend. A typical
1188 * example would be a character-device driver when its device file is
1191 * The routine decrements @intf's usage counter. When the counter reaches
1192 * 0, a delayed autosuspend request for @intf's device is queued. When
1193 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1194 * the other usage counters for the sibling interfaces and @intf's
1195 * usb_device, the device and all its interfaces will be autosuspended.
1197 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1198 * core will not change its value other than the increment and decrement
1199 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1200 * may use this simple counter-oriented discipline or may set the value
1203 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1204 * take place only if the device's remote-wakeup facility is enabled.
1206 * Suspend method calls queued by this routine can arrive at any time
1207 * while @intf is resumed and its usage counter is equal to 0. They are
1208 * not protected by the usb_device's lock but only by its pm_mutex.
1209 * Drivers must provide their own synchronization.
1211 * This routine can run only in process context.
1213 void usb_autopm_put_interface(struct usb_interface *intf)
1215 struct usb_device *udev = interface_to_usbdev(intf);
1217 mutex_lock_nested(&udev->pm_mutex, udev->level);
1218 if (intf->condition != USB_INTERFACE_UNBOUND) {
1219 if (--intf->pm_usage_cnt <= 0)
1220 schedule_delayed_work(&udev->autosuspend,
1221 USB_AUTOSUSPEND_DELAY);
1223 mutex_unlock(&udev->pm_mutex);
1224 // dev_dbg(&intf->dev, "%s: cnt %d\n",
1225 // __FUNCTION__, intf->pm_usage_cnt);
1227 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1230 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1231 * @intf - the usb_interface whose counter should be incremented
1233 * This routine should be called by an interface driver when it wants to
1234 * use @intf and needs to guarantee that it is not suspended. In addition,
1235 * the routine prevents @intf from being autosuspended subsequently. (Note
1236 * that this will not prevent suspend events originating in the PM core.)
1237 * This prevention will persist until usb_autopm_put_interface() is called
1238 * or @intf is unbound. A typical example would be a character-device
1239 * driver when its device file is opened.
1241 * The routine increments @intf's usage counter. So long as the counter
1242 * is greater than 0, autosuspend will not be allowed for @intf or its
1243 * usb_device. When the driver is finished using @intf it should call
1244 * usb_autopm_put_interface() to decrement the usage counter and queue
1245 * a delayed autosuspend request (if the counter is <= 0).
1247 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1248 * core will not change its value other than the increment and decrement
1249 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1250 * may use this simple counter-oriented discipline or may set the value
1253 * Resume method calls generated by this routine can arrive at any time
1254 * while @intf is suspended. They are not protected by the usb_device's
1255 * lock but only by its pm_mutex. Drivers must provide their own
1258 * This routine can run only in process context.
1260 int usb_autopm_get_interface(struct usb_interface *intf)
1262 struct usb_device *udev = interface_to_usbdev(intf);
1265 mutex_lock_nested(&udev->pm_mutex, udev->level);
1266 if (intf->condition == USB_INTERFACE_UNBOUND)
1269 ++intf->pm_usage_cnt;
1271 status = usb_resume_both(udev);
1273 --intf->pm_usage_cnt;
1275 mutex_unlock(&udev->pm_mutex);
1276 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1277 // __FUNCTION__, status, intf->pm_usage_cnt);
1280 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1282 #endif /* CONFIG_USB_SUSPEND */
1284 static int usb_suspend(struct device *dev, pm_message_t message)
1288 if (is_usb_device(dev)) {
1289 struct usb_device *udev = to_usb_device(dev);
1291 mutex_lock_nested(&udev->pm_mutex, udev->level);
1293 status = usb_suspend_both(udev, message);
1294 mutex_unlock(&udev->pm_mutex);
1300 static int usb_resume(struct device *dev)
1304 if (is_usb_device(dev)) {
1305 struct usb_device *udev = to_usb_device(dev);
1307 mutex_lock_nested(&udev->pm_mutex, udev->level);
1309 status = usb_resume_both(udev);
1310 mutex_unlock(&udev->pm_mutex);
1312 /* Rebind drivers that had no suspend method? */
1318 #endif /* CONFIG_PM */
1320 struct bus_type usb_bus_type = {
1322 .match = usb_device_match,
1323 .uevent = usb_uevent,
1325 .suspend = usb_suspend,
1326 .resume = usb_resume,