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;
53 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
66 spin_lock(&usb_drv->dynids.lock);
67 list_add_tail(&usb_drv->dynids.list, &dynid->node);
68 spin_unlock(&usb_drv->dynids.lock);
70 if (get_driver(driver)) {
71 driver_attach(driver);
77 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
79 static int usb_create_newid_file(struct usb_driver *usb_drv)
83 if (usb_drv->no_dynamic_id)
86 if (usb_drv->probe != NULL)
87 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
88 &driver_attr_new_id.attr);
93 static void usb_remove_newid_file(struct usb_driver *usb_drv)
95 if (usb_drv->no_dynamic_id)
98 if (usb_drv->probe != NULL)
99 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
100 &driver_attr_new_id.attr);
103 static void usb_free_dynids(struct usb_driver *usb_drv)
105 struct usb_dynid *dynid, *n;
107 spin_lock(&usb_drv->dynids.lock);
108 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
109 list_del(&dynid->node);
112 spin_unlock(&usb_drv->dynids.lock);
115 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
120 static void usb_remove_newid_file(struct usb_driver *usb_drv)
124 static inline void usb_free_dynids(struct usb_driver *usb_drv)
129 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
130 struct usb_driver *drv)
132 struct usb_dynid *dynid;
134 spin_lock(&drv->dynids.lock);
135 list_for_each_entry(dynid, &drv->dynids.list, node) {
136 if (usb_match_one_id(intf, &dynid->id)) {
137 spin_unlock(&drv->dynids.lock);
141 spin_unlock(&drv->dynids.lock);
146 /* called from driver core with dev locked */
147 static int usb_probe_device(struct device *dev)
149 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
150 struct usb_device *udev;
153 dev_dbg(dev, "%s\n", __FUNCTION__);
155 if (!is_usb_device(dev)) /* Sanity check */
158 udev = to_usb_device(dev);
160 /* TODO: Add real matching code */
162 /* The device should always appear to be in use
163 * unless the driver suports autosuspend.
165 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
167 error = udriver->probe(udev);
171 /* called from driver core with dev locked */
172 static int usb_unbind_device(struct device *dev)
174 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
176 udriver->disconnect(to_usb_device(dev));
181 /* called from driver core with dev locked */
182 static int usb_probe_interface(struct device *dev)
184 struct usb_driver *driver = to_usb_driver(dev->driver);
185 struct usb_interface *intf;
186 struct usb_device *udev;
187 const struct usb_device_id *id;
190 dev_dbg(dev, "%s\n", __FUNCTION__);
192 if (is_usb_device(dev)) /* Sanity check */
195 intf = to_usb_interface(dev);
196 udev = interface_to_usbdev(intf);
198 id = usb_match_id(intf, driver->id_table);
200 id = usb_match_dynamic_id(intf, driver);
202 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
204 error = usb_autoresume_device(udev, 1);
208 /* Interface "power state" doesn't correspond to any hardware
209 * state whatsoever. We use it to record when it's bound to
210 * a driver that may start I/0: it's not frozen/quiesced.
213 intf->condition = USB_INTERFACE_BINDING;
215 /* The interface should always appear to be in use
216 * unless the driver suports autosuspend.
218 intf->pm_usage_cnt = !(driver->supports_autosuspend);
220 error = driver->probe(intf, id);
223 intf->needs_remote_wakeup = 0;
224 intf->condition = USB_INTERFACE_UNBOUND;
226 intf->condition = USB_INTERFACE_BOUND;
228 usb_autosuspend_device(udev, 1);
234 /* called from driver core with dev locked */
235 static int usb_unbind_interface(struct device *dev)
237 struct usb_driver *driver = to_usb_driver(dev->driver);
238 struct usb_interface *intf = to_usb_interface(dev);
239 struct usb_device *udev;
242 intf->condition = USB_INTERFACE_UNBINDING;
244 /* Autoresume for set_interface call below */
245 udev = interface_to_usbdev(intf);
246 error = usb_autoresume_device(udev, 1);
248 /* release all urbs for this interface */
249 usb_disable_interface(interface_to_usbdev(intf), intf);
251 driver->disconnect(intf);
253 /* reset other interface state */
254 usb_set_interface(interface_to_usbdev(intf),
255 intf->altsetting[0].desc.bInterfaceNumber,
257 usb_set_intfdata(intf, NULL);
259 intf->condition = USB_INTERFACE_UNBOUND;
261 intf->needs_remote_wakeup = 0;
264 usb_autosuspend_device(udev, 1);
270 * usb_driver_claim_interface - bind a driver to an interface
271 * @driver: the driver to be bound
272 * @iface: the interface to which it will be bound; must be in the
273 * usb device's active configuration
274 * @priv: driver data associated with that interface
276 * This is used by usb device drivers that need to claim more than one
277 * interface on a device when probing (audio and acm are current examples).
278 * No device driver should directly modify internal usb_interface or
279 * usb_device structure members.
281 * Few drivers should need to use this routine, since the most natural
282 * way to bind to an interface is to return the private data from
283 * the driver's probe() method.
285 * Callers must own the device lock and the driver model's usb_bus_type.subsys
286 * writelock. So driver probe() entries don't need extra locking,
287 * but other call contexts may need to explicitly claim those locks.
289 int usb_driver_claim_interface(struct usb_driver *driver,
290 struct usb_interface *iface, void* priv)
292 struct device *dev = &iface->dev;
293 struct usb_device *udev = interface_to_usbdev(iface);
298 dev->driver = &driver->drvwrap.driver;
299 usb_set_intfdata(iface, priv);
301 mutex_lock_nested(&udev->pm_mutex, udev->level);
302 iface->condition = USB_INTERFACE_BOUND;
304 iface->pm_usage_cnt = !(driver->supports_autosuspend);
305 mutex_unlock(&udev->pm_mutex);
307 /* if interface was already added, bind now; else let
308 * the future device_add() bind it, bypassing probe()
310 if (device_is_registered(dev))
311 device_bind_driver(dev);
315 EXPORT_SYMBOL(usb_driver_claim_interface);
318 * usb_driver_release_interface - unbind a driver from an interface
319 * @driver: the driver to be unbound
320 * @iface: the interface from which it will be unbound
322 * This can be used by drivers to release an interface without waiting
323 * for their disconnect() methods to be called. In typical cases this
324 * also causes the driver disconnect() method to be called.
326 * This call is synchronous, and may not be used in an interrupt context.
327 * Callers must own the device lock and the driver model's usb_bus_type.subsys
328 * writelock. So driver disconnect() entries don't need extra locking,
329 * but other call contexts may need to explicitly claim those locks.
331 void usb_driver_release_interface(struct usb_driver *driver,
332 struct usb_interface *iface)
334 struct device *dev = &iface->dev;
335 struct usb_device *udev = interface_to_usbdev(iface);
337 /* this should never happen, don't release something that's not ours */
338 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
341 /* don't release from within disconnect() */
342 if (iface->condition != USB_INTERFACE_BOUND)
345 /* don't release if the interface hasn't been added yet */
346 if (device_is_registered(dev)) {
347 iface->condition = USB_INTERFACE_UNBINDING;
348 device_release_driver(dev);
352 usb_set_intfdata(iface, NULL);
354 mutex_lock_nested(&udev->pm_mutex, udev->level);
355 iface->condition = USB_INTERFACE_UNBOUND;
356 mark_quiesced(iface);
357 iface->needs_remote_wakeup = 0;
358 mutex_unlock(&udev->pm_mutex);
360 EXPORT_SYMBOL(usb_driver_release_interface);
362 /* returns 0 if no match, 1 if match */
363 static int usb_match_one_id(struct usb_interface *interface,
364 const struct usb_device_id *id)
366 struct usb_host_interface *intf;
367 struct usb_device *dev;
369 /* proc_connectinfo in devio.c may call us with id == NULL. */
373 intf = interface->cur_altsetting;
374 dev = interface_to_usbdev(interface);
376 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
377 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
380 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
381 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
384 /* No need to test id->bcdDevice_lo != 0, since 0 is never
385 greater than any unsigned number. */
386 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
387 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
390 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
391 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
394 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
395 (id->bDeviceClass != dev->descriptor.bDeviceClass))
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
399 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
402 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
403 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
406 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
407 (id->bInterfaceClass != intf->desc.bInterfaceClass))
410 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
411 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
414 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
415 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
421 * usb_match_id - find first usb_device_id matching device or interface
422 * @interface: the interface of interest
423 * @id: array of usb_device_id structures, terminated by zero entry
425 * usb_match_id searches an array of usb_device_id's and returns
426 * the first one matching the device or interface, or null.
427 * This is used when binding (or rebinding) a driver to an interface.
428 * Most USB device drivers will use this indirectly, through the usb core,
429 * but some layered driver frameworks use it directly.
430 * These device tables are exported with MODULE_DEVICE_TABLE, through
431 * modutils, to support the driver loading functionality of USB hotplugging.
435 * The "match_flags" element in a usb_device_id controls which
436 * members are used. If the corresponding bit is set, the
437 * value in the device_id must match its corresponding member
438 * in the device or interface descriptor, or else the device_id
441 * "driver_info" is normally used only by device drivers,
442 * but you can create a wildcard "matches anything" usb_device_id
443 * as a driver's "modules.usbmap" entry if you provide an id with
444 * only a nonzero "driver_info" field. If you do this, the USB device
445 * driver's probe() routine should use additional intelligence to
446 * decide whether to bind to the specified interface.
448 * What Makes Good usb_device_id Tables:
450 * The match algorithm is very simple, so that intelligence in
451 * driver selection must come from smart driver id records.
452 * Unless you have good reasons to use another selection policy,
453 * provide match elements only in related groups, and order match
454 * specifiers from specific to general. Use the macros provided
455 * for that purpose if you can.
457 * The most specific match specifiers use device descriptor
458 * data. These are commonly used with product-specific matches;
459 * the USB_DEVICE macro lets you provide vendor and product IDs,
460 * and you can also match against ranges of product revisions.
461 * These are widely used for devices with application or vendor
462 * specific bDeviceClass values.
464 * Matches based on device class/subclass/protocol specifications
465 * are slightly more general; use the USB_DEVICE_INFO macro, or
466 * its siblings. These are used with single-function devices
467 * where bDeviceClass doesn't specify that each interface has
470 * Matches based on interface class/subclass/protocol are the
471 * most general; they let drivers bind to any interface on a
472 * multiple-function device. Use the USB_INTERFACE_INFO
473 * macro, or its siblings, to match class-per-interface style
474 * devices (as recorded in bDeviceClass).
476 * Within those groups, remember that not all combinations are
477 * meaningful. For example, don't give a product version range
478 * without vendor and product IDs; or specify a protocol without
479 * its associated class and subclass.
481 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
482 const struct usb_device_id *id)
484 /* proc_connectinfo in devio.c may call us with id == NULL. */
488 /* It is important to check that id->driver_info is nonzero,
489 since an entry that is all zeroes except for a nonzero
490 id->driver_info is the way to create an entry that
491 indicates that the driver want to examine every
492 device and interface. */
493 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
494 id->driver_info; id++) {
495 if (usb_match_one_id(interface, id))
501 EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
503 int usb_device_match(struct device *dev, struct device_driver *drv)
505 /* devices and interfaces are handled separately */
506 if (is_usb_device(dev)) {
508 /* interface drivers never match devices */
509 if (!is_usb_device_driver(drv))
512 /* TODO: Add real matching code */
516 struct usb_interface *intf;
517 struct usb_driver *usb_drv;
518 const struct usb_device_id *id;
520 /* device drivers never match interfaces */
521 if (is_usb_device_driver(drv))
524 intf = to_usb_interface(dev);
525 usb_drv = to_usb_driver(drv);
527 id = usb_match_id(intf, usb_drv->id_table);
531 id = usb_match_dynamic_id(intf, usb_drv);
539 #ifdef CONFIG_HOTPLUG
542 * This sends an uevent to userspace, typically helping to load driver
543 * or other modules, configure the device, and more. Drivers can provide
544 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
546 * We're called either from khubd (the typical case) or from root hub
547 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
548 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
549 * device (and this configuration!) are still present.
551 static int usb_uevent(struct device *dev, char **envp, int num_envp,
552 char *buffer, int buffer_size)
554 struct usb_interface *intf;
555 struct usb_device *usb_dev;
556 struct usb_host_interface *alt;
563 /* driver is often null here; dev_dbg() would oops */
564 pr_debug ("usb %s: uevent\n", dev->bus_id);
566 if (is_usb_device(dev)) {
567 usb_dev = to_usb_device(dev);
570 intf = to_usb_interface(dev);
571 usb_dev = interface_to_usbdev(intf);
572 alt = intf->cur_altsetting;
575 if (usb_dev->devnum < 0) {
576 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
580 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
584 #ifdef CONFIG_USB_DEVICEFS
585 /* If this is available, userspace programs can directly read
586 * all the device descriptors we don't tell them about. Or
587 * even act as usermode drivers.
589 * FIXME reduce hardwired intelligence here
591 if (add_uevent_var(envp, num_envp, &i,
592 buffer, buffer_size, &length,
593 "DEVICE=/proc/bus/usb/%03d/%03d",
594 usb_dev->bus->busnum, usb_dev->devnum))
598 /* per-device configurations are common */
599 if (add_uevent_var(envp, num_envp, &i,
600 buffer, buffer_size, &length,
602 le16_to_cpu(usb_dev->descriptor.idVendor),
603 le16_to_cpu(usb_dev->descriptor.idProduct),
604 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
607 /* class-based driver binding models */
608 if (add_uevent_var(envp, num_envp, &i,
609 buffer, buffer_size, &length,
611 usb_dev->descriptor.bDeviceClass,
612 usb_dev->descriptor.bDeviceSubClass,
613 usb_dev->descriptor.bDeviceProtocol))
616 if (!is_usb_device(dev)) {
618 if (add_uevent_var(envp, num_envp, &i,
619 buffer, buffer_size, &length,
620 "INTERFACE=%d/%d/%d",
621 alt->desc.bInterfaceClass,
622 alt->desc.bInterfaceSubClass,
623 alt->desc.bInterfaceProtocol))
626 if (add_uevent_var(envp, num_envp, &i,
627 buffer, buffer_size, &length,
628 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
629 le16_to_cpu(usb_dev->descriptor.idVendor),
630 le16_to_cpu(usb_dev->descriptor.idProduct),
631 le16_to_cpu(usb_dev->descriptor.bcdDevice),
632 usb_dev->descriptor.bDeviceClass,
633 usb_dev->descriptor.bDeviceSubClass,
634 usb_dev->descriptor.bDeviceProtocol,
635 alt->desc.bInterfaceClass,
636 alt->desc.bInterfaceSubClass,
637 alt->desc.bInterfaceProtocol))
648 static int usb_uevent(struct device *dev, char **envp,
649 int num_envp, char *buffer, int buffer_size)
654 #endif /* CONFIG_HOTPLUG */
657 * usb_register_device_driver - register a USB device (not interface) driver
658 * @new_udriver: USB operations for the device driver
659 * @owner: module owner of this driver.
661 * Registers a USB device driver with the USB core. The list of
662 * unattached devices will be rescanned whenever a new driver is
663 * added, allowing the new driver to attach to any recognized devices.
664 * Returns a negative error code on failure and 0 on success.
666 int usb_register_device_driver(struct usb_device_driver *new_udriver,
667 struct module *owner)
674 new_udriver->drvwrap.for_devices = 1;
675 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
676 new_udriver->drvwrap.driver.bus = &usb_bus_type;
677 new_udriver->drvwrap.driver.probe = usb_probe_device;
678 new_udriver->drvwrap.driver.remove = usb_unbind_device;
679 new_udriver->drvwrap.driver.owner = owner;
681 retval = driver_register(&new_udriver->drvwrap.driver);
684 pr_info("%s: registered new device driver %s\n",
685 usbcore_name, new_udriver->name);
686 usbfs_update_special();
688 printk(KERN_ERR "%s: error %d registering device "
690 usbcore_name, retval, new_udriver->name);
695 EXPORT_SYMBOL_GPL(usb_register_device_driver);
698 * usb_deregister_device_driver - unregister a USB device (not interface) driver
699 * @udriver: USB operations of the device driver to unregister
700 * Context: must be able to sleep
702 * Unlinks the specified driver from the internal USB driver list.
704 void usb_deregister_device_driver(struct usb_device_driver *udriver)
706 pr_info("%s: deregistering device driver %s\n",
707 usbcore_name, udriver->name);
709 driver_unregister(&udriver->drvwrap.driver);
710 usbfs_update_special();
712 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
715 * usb_register_driver - register a USB interface driver
716 * @new_driver: USB operations for the interface driver
717 * @owner: module owner of this driver.
719 * Registers a USB interface driver with the USB core. The list of
720 * unattached interfaces will be rescanned whenever a new driver is
721 * added, allowing the new driver to attach to any recognized interfaces.
722 * Returns a negative error code on failure and 0 on success.
724 * NOTE: if you want your driver to use the USB major number, you must call
725 * usb_register_dev() to enable that functionality. This function no longer
726 * takes care of that.
728 int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
735 new_driver->drvwrap.for_devices = 0;
736 new_driver->drvwrap.driver.name = (char *) new_driver->name;
737 new_driver->drvwrap.driver.bus = &usb_bus_type;
738 new_driver->drvwrap.driver.probe = usb_probe_interface;
739 new_driver->drvwrap.driver.remove = usb_unbind_interface;
740 new_driver->drvwrap.driver.owner = owner;
741 spin_lock_init(&new_driver->dynids.lock);
742 INIT_LIST_HEAD(&new_driver->dynids.list);
744 retval = driver_register(&new_driver->drvwrap.driver);
747 pr_info("%s: registered new interface driver %s\n",
748 usbcore_name, new_driver->name);
749 usbfs_update_special();
750 usb_create_newid_file(new_driver);
752 printk(KERN_ERR "%s: error %d registering interface "
754 usbcore_name, retval, new_driver->name);
759 EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
762 * usb_deregister - unregister a USB interface driver
763 * @driver: USB operations of the interface driver to unregister
764 * Context: must be able to sleep
766 * Unlinks the specified driver from the internal USB driver list.
768 * NOTE: If you called usb_register_dev(), you still need to call
769 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
770 * this * call will no longer do it for you.
772 void usb_deregister(struct usb_driver *driver)
774 pr_info("%s: deregistering interface driver %s\n",
775 usbcore_name, driver->name);
777 usb_remove_newid_file(driver);
778 usb_free_dynids(driver);
779 driver_unregister(&driver->drvwrap.driver);
781 usbfs_update_special();
783 EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
787 /* Caller has locked udev->pm_mutex */
788 static int suspend_device(struct usb_device *udev, pm_message_t msg)
790 struct usb_device_driver *udriver;
793 if (udev->state == USB_STATE_NOTATTACHED ||
794 udev->state == USB_STATE_SUSPENDED)
797 /* For devices that don't have a driver, we do a standard suspend. */
798 if (udev->dev.driver == NULL) {
799 udev->do_remote_wakeup = 0;
800 status = usb_port_suspend(udev);
804 udriver = to_usb_device_driver(udev->dev.driver);
805 status = udriver->suspend(udev, msg);
808 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
810 udev->dev.power.power_state.event = msg.event;
814 /* Caller has locked udev->pm_mutex */
815 static int resume_device(struct usb_device *udev)
817 struct usb_device_driver *udriver;
820 if (udev->state == USB_STATE_NOTATTACHED ||
821 udev->state != USB_STATE_SUSPENDED)
824 /* Can't resume it if it doesn't have a driver. */
825 if (udev->dev.driver == NULL) {
830 udriver = to_usb_device_driver(udev->dev.driver);
831 status = udriver->resume(udev);
834 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
836 udev->dev.power.power_state.event = PM_EVENT_ON;
840 /* Caller has locked intf's usb_device's pm_mutex */
841 static int suspend_interface(struct usb_interface *intf, pm_message_t msg)
843 struct usb_driver *driver;
846 /* with no hardware, USB interfaces only use FREEZE and ON states */
847 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
851 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
853 driver = to_usb_driver(intf->dev.driver);
855 if (driver->suspend && driver->resume) {
856 status = driver->suspend(intf, msg);
859 else if (!interface_to_usbdev(intf)->auto_pm)
860 dev_err(&intf->dev, "%s error %d\n",
863 // FIXME else if there's no suspend method, disconnect...
864 // Not possible if auto_pm is set...
865 dev_warn(&intf->dev, "no suspend for driver %s?\n",
871 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
873 intf->dev.power.power_state.event = msg.event;
877 /* Caller has locked intf's usb_device's pm_mutex */
878 static int resume_interface(struct usb_interface *intf)
880 struct usb_driver *driver;
883 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
887 /* Don't let autoresume interfere with unbinding */
888 if (intf->condition == USB_INTERFACE_UNBINDING)
891 /* Can't resume it if it doesn't have a driver. */
892 if (intf->condition == USB_INTERFACE_UNBOUND) {
896 driver = to_usb_driver(intf->dev.driver);
898 if (driver->resume) {
899 status = driver->resume(intf);
901 dev_err(&intf->dev, "%s error %d\n",
906 dev_warn(&intf->dev, "no resume for driver %s?\n",
912 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
914 intf->dev.power.power_state.event = PM_EVENT_ON;
919 * usb_suspend_both - suspend a USB device and its interfaces
920 * @udev: the usb_device to suspend
921 * @msg: Power Management message describing this state transition
923 * This is the central routine for suspending USB devices. It calls the
924 * suspend methods for all the interface drivers in @udev and then calls
925 * the suspend method for @udev itself. If an error occurs at any stage,
926 * all the interfaces which were suspended are resumed so that they remain
927 * in the same state as the device.
929 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
930 * checks first to make sure that neither the device itself or any of its
931 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
932 * are, the autosuspend fails.
934 * If the suspend succeeds, the routine recursively queues an autosuspend
935 * request for @udev's parent device, thereby propagating the change up
936 * the device tree. If all of the parent's children are now suspended,
937 * the parent will autosuspend in turn.
939 * The suspend method calls are subject to mutual exclusion under control
940 * of @udev's pm_mutex. Many of these calls are also under the protection
941 * of @udev's device lock (including all requests originating outside the
942 * USB subsystem), but autosuspend requests generated by a child device or
943 * interface driver may not be. Usbcore will insure that the method calls
944 * do not arrive during bind, unbind, or reset operations. However, drivers
945 * must be prepared to handle suspend calls arriving at unpredictable times.
946 * The only way to block such calls is to do an autoresume (preventing
947 * autosuspends) while holding @udev's device lock (preventing outside
950 * The caller must hold @udev->pm_mutex.
952 * This routine can run only in process context.
954 int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
958 struct usb_interface *intf;
959 struct usb_device *parent = udev->parent;
961 cancel_delayed_work(&udev->autosuspend);
962 if (udev->state == USB_STATE_NOTATTACHED)
964 if (udev->state == USB_STATE_SUSPENDED)
967 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
969 /* For autosuspend, fail fast if anything is in use.
970 * Also fail if any interfaces require remote wakeup but it
971 * isn't available. */
973 if (udev->pm_usage_cnt > 0)
975 if (udev->actconfig) {
976 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
977 intf = udev->actconfig->interface[i];
978 if (!is_active(intf))
980 if (intf->pm_usage_cnt > 0)
982 if (intf->needs_remote_wakeup &&
983 !udev->do_remote_wakeup) {
985 "remote wakeup needed for autosuspend\n");
993 /* Suspend all the interfaces and then udev itself */
994 if (udev->actconfig) {
995 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
996 intf = udev->actconfig->interface[i];
997 status = suspend_interface(intf, msg);
1003 status = suspend_device(udev, msg);
1005 /* If the suspend failed, resume interfaces that did get suspended */
1008 intf = udev->actconfig->interface[i];
1009 resume_interface(intf);
1012 /* If the suspend succeeded, propagate it up the tree */
1014 usb_autosuspend_device(parent, 0);
1016 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1021 * usb_resume_both - resume a USB device and its interfaces
1022 * @udev: the usb_device to resume
1024 * This is the central routine for resuming USB devices. It calls the
1025 * the resume method for @udev and then calls the resume methods for all
1026 * the interface drivers in @udev.
1028 * Before starting the resume, the routine calls itself recursively for
1029 * the parent device of @udev, thereby propagating the change up the device
1030 * tree and assuring that @udev will be able to resume. If the parent is
1031 * unable to resume successfully, the routine fails.
1033 * The resume method calls are subject to mutual exclusion under control
1034 * of @udev's pm_mutex. Many of these calls are also under the protection
1035 * of @udev's device lock (including all requests originating outside the
1036 * USB subsystem), but autoresume requests generated by a child device or
1037 * interface driver may not be. Usbcore will insure that the method calls
1038 * do not arrive during bind, unbind, or reset operations. However, drivers
1039 * must be prepared to handle resume calls arriving at unpredictable times.
1040 * The only way to block such calls is to do an autoresume (preventing
1041 * other autoresumes) while holding @udev's device lock (preventing outside
1044 * The caller must hold @udev->pm_mutex.
1046 * This routine can run only in process context.
1048 int usb_resume_both(struct usb_device *udev)
1052 struct usb_interface *intf;
1053 struct usb_device *parent = udev->parent;
1055 cancel_delayed_work(&udev->autosuspend);
1056 if (udev->state == USB_STATE_NOTATTACHED)
1059 /* Propagate the resume up the tree, if necessary */
1060 if (udev->state == USB_STATE_SUSPENDED) {
1062 mutex_lock_nested(&parent->pm_mutex, parent->level);
1063 parent->auto_pm = 1;
1064 status = usb_resume_both(parent);
1067 /* We can't progagate beyond the USB subsystem,
1068 * so if a root hub's controller is suspended
1069 * then we're stuck. */
1070 if (udev->dev.parent->power.power_state.event !=
1072 status = -EHOSTUNREACH;
1075 status = resume_device(udev);
1077 mutex_unlock(&parent->pm_mutex);
1080 /* Needed only for setting udev->dev.power.power_state.event
1081 * and for possible debugging message. */
1082 status = resume_device(udev);
1085 /* Now the parent won't suspend until we are finished */
1087 if (status == 0 && udev->actconfig) {
1088 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1089 intf = udev->actconfig->interface[i];
1090 resume_interface(intf);
1094 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1098 #ifdef CONFIG_USB_SUSPEND
1101 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1102 * @udev - the usb_device to autosuspend
1103 * @dec_usage_cnt - flag to decrement @udev's PM-usage counter
1105 * This routine should be called when a core subsystem is finished using
1106 * @udev and wants to allow it to autosuspend. Examples would be when
1107 * @udev's device file in usbfs is closed or after a configuration change.
1109 * @dec_usage_cnt should be 1 if the subsystem previously incremented
1110 * @udev's usage counter (such as by passing 1 to usb_autoresume_device);
1111 * otherwise it should be 0.
1113 * If the usage counter for @udev or any of its active interfaces is greater
1114 * than 0, the autosuspend request will not be queued. (If an interface
1115 * driver does not support autosuspend then its usage counter is permanently
1116 * positive.) Likewise, if an interface driver requires remote-wakeup
1117 * capability during autosuspend but remote wakeup is disabled, the
1118 * autosuspend will fail.
1120 * Often the caller will hold @udev's device lock, but this is not
1123 * This routine can run only in process context.
1125 void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt)
1127 mutex_lock_nested(&udev->pm_mutex, udev->level);
1128 udev->pm_usage_cnt -= dec_usage_cnt;
1129 if (udev->pm_usage_cnt <= 0)
1130 schedule_delayed_work(&udev->autosuspend,
1131 USB_AUTOSUSPEND_DELAY);
1132 mutex_unlock(&udev->pm_mutex);
1133 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1134 // __FUNCTION__, udev->pm_usage_cnt);
1138 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1139 * @udev - the usb_device to autoresume
1140 * @inc_usage_cnt - flag to increment @udev's PM-usage counter
1142 * This routine should be called when a core subsystem wants to use @udev
1143 * and needs to guarantee that it is not suspended. In addition, the
1144 * caller can prevent @udev from being autosuspended subsequently. (Note
1145 * that this will not prevent suspend events originating in the PM core.)
1146 * Examples would be when @udev's device file in usbfs is opened (autosuspend
1147 * should be prevented until the file is closed) or when a remote-wakeup
1148 * request is received (later autosuspends should not be prevented).
1150 * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent
1151 * autosuspends. This prevention will persist until the usage counter is
1152 * decremented again (such as by passing 1 to usb_autosuspend_device).
1153 * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged.
1154 * Regardless, if the autoresume fails then the usage counter is not
1157 * Often the caller will hold @udev's device lock, but this is not
1158 * necessary (and attempting it might cause deadlock).
1160 * This routine can run only in process context.
1162 int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt)
1166 mutex_lock_nested(&udev->pm_mutex, udev->level);
1167 udev->pm_usage_cnt += inc_usage_cnt;
1169 status = usb_resume_both(udev);
1171 udev->pm_usage_cnt -= inc_usage_cnt;
1172 mutex_unlock(&udev->pm_mutex);
1173 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1174 // __FUNCTION__, status, udev->pm_usage_cnt);
1179 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1180 * @intf - the usb_interface whose counter should be decremented
1182 * This routine should be called by an interface driver when it is
1183 * finished using @intf and wants to allow it to autosuspend. A typical
1184 * example would be a character-device driver when its device file is
1187 * The routine decrements @intf's usage counter. When the counter reaches
1188 * 0, a delayed autosuspend request for @intf's device is queued. When
1189 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1190 * the other usage counters for the sibling interfaces and @intf's
1191 * usb_device, the device and all its interfaces will be autosuspended.
1193 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1194 * core will not change its value other than the increment and decrement
1195 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1196 * may use this simple counter-oriented discipline or may set the value
1199 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1200 * take place only if the device's remote-wakeup facility is enabled.
1202 * Suspend method calls queued by this routine can arrive at any time
1203 * while @intf is resumed and its usage counter is equal to 0. They are
1204 * not protected by the usb_device's lock but only by its pm_mutex.
1205 * Drivers must provide their own synchronization.
1207 * This routine can run only in process context.
1209 void usb_autopm_put_interface(struct usb_interface *intf)
1211 struct usb_device *udev = interface_to_usbdev(intf);
1213 mutex_lock_nested(&udev->pm_mutex, udev->level);
1214 if (intf->condition != USB_INTERFACE_UNBOUND) {
1215 if (--intf->pm_usage_cnt <= 0)
1216 schedule_delayed_work(&udev->autosuspend,
1217 USB_AUTOSUSPEND_DELAY);
1219 mutex_unlock(&udev->pm_mutex);
1220 // dev_dbg(&intf->dev, "%s: cnt %d\n",
1221 // __FUNCTION__, intf->pm_usage_cnt);
1223 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1226 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1227 * @intf - the usb_interface whose counter should be incremented
1229 * This routine should be called by an interface driver when it wants to
1230 * use @intf and needs to guarantee that it is not suspended. In addition,
1231 * the routine prevents @intf from being autosuspended subsequently. (Note
1232 * that this will not prevent suspend events originating in the PM core.)
1233 * This prevention will persist until usb_autopm_put_interface() is called
1234 * or @intf is unbound. A typical example would be a character-device
1235 * driver when its device file is opened.
1237 * The routine increments @intf's usage counter. So long as the counter
1238 * is greater than 0, autosuspend will not be allowed for @intf or its
1239 * usb_device. When the driver is finished using @intf it should call
1240 * usb_autopm_put_interface() to decrement the usage counter and queue
1241 * a delayed autosuspend request (if the counter is <= 0).
1243 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1244 * core will not change its value other than the increment and decrement
1245 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1246 * may use this simple counter-oriented discipline or may set the value
1249 * Resume method calls generated by this routine can arrive at any time
1250 * while @intf is suspended. They are not protected by the usb_device's
1251 * lock but only by its pm_mutex. Drivers must provide their own
1254 * This routine can run only in process context.
1256 int usb_autopm_get_interface(struct usb_interface *intf)
1258 struct usb_device *udev = interface_to_usbdev(intf);
1261 mutex_lock_nested(&udev->pm_mutex, udev->level);
1262 if (intf->condition == USB_INTERFACE_UNBOUND)
1265 ++intf->pm_usage_cnt;
1267 status = usb_resume_both(udev);
1269 --intf->pm_usage_cnt;
1271 mutex_unlock(&udev->pm_mutex);
1272 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1273 // __FUNCTION__, status, intf->pm_usage_cnt);
1276 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1278 #endif /* CONFIG_USB_SUSPEND */
1280 static int usb_suspend(struct device *dev, pm_message_t message)
1284 if (is_usb_device(dev)) {
1285 struct usb_device *udev = to_usb_device(dev);
1287 mutex_lock_nested(&udev->pm_mutex, udev->level);
1289 status = usb_suspend_both(udev, message);
1290 mutex_unlock(&udev->pm_mutex);
1296 static int usb_resume(struct device *dev)
1300 if (is_usb_device(dev)) {
1301 struct usb_device *udev = to_usb_device(dev);
1303 mutex_lock_nested(&udev->pm_mutex, udev->level);
1305 status = usb_resume_both(udev);
1306 mutex_unlock(&udev->pm_mutex);
1308 /* Rebind drivers that had no suspend method? */
1314 #endif /* CONFIG_PM */
1316 struct bus_type usb_bus_type = {
1318 .match = usb_device_match,
1319 .uevent = usb_uevent,
1321 .suspend = usb_suspend,
1322 .resume = usb_resume,