blob: 02d6db61c940a03623f46add7b10ad4c1a406d93 [file] [log] [blame]
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001/*
2 * drivers/usb/driver.c - most of the driver model stuff for usb
3 *
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
5 *
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
17 *
18 * NOTE! This is not actually a driver at all, rather this is
19 * just a collection of helper routines that implement the
Alan Stern36e56a32006-07-01 22:08:06 -040020 * matching, probing, releasing, suspending and resuming for
21 * real drivers.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080022 *
23 */
24
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080025#include <linux/device.h>
26#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040027#include <linux/usb/quirks.h>
Alan Sternbd859282006-09-19 10:14:07 -040028#include <linux/workqueue.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080029#include "hcd.h"
30#include "usb.h"
31
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080032#ifdef CONFIG_HOTPLUG
33
34/*
35 * Adds a new dynamic USBdevice ID to this driver,
36 * and cause the driver to probe for all devices again.
37 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010038ssize_t usb_store_new_id(struct usb_dynids *dynids,
39 struct device_driver *driver,
40 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080041{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080042 struct usb_dynid *dynid;
43 u32 idVendor = 0;
44 u32 idProduct = 0;
45 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070046 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080047
48 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
49 if (fields < 2)
50 return -EINVAL;
51
52 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
53 if (!dynid)
54 return -ENOMEM;
55
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;
60
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010061 spin_lock(&dynids->lock);
62 list_add_tail(&dynids->list, &dynid->node);
63 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080064
65 if (get_driver(driver)) {
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070066 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080067 put_driver(driver);
68 }
69
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070070 if (retval)
71 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080072 return count;
73}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010074EXPORT_SYMBOL_GPL(usb_store_new_id);
75
76static ssize_t store_new_id(struct device_driver *driver,
77 const char *buf, size_t count)
78{
79 struct usb_driver *usb_drv = to_usb_driver(driver);
80
81 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
82}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080083static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
84
85static int usb_create_newid_file(struct usb_driver *usb_drv)
86{
87 int error = 0;
88
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080089 if (usb_drv->no_dynamic_id)
90 goto exit;
91
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080092 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -040093 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080094 &driver_attr_new_id.attr);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080095exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080096 return error;
97}
98
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080099static void usb_remove_newid_file(struct usb_driver *usb_drv)
100{
101 if (usb_drv->no_dynamic_id)
102 return;
103
104 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400105 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800106 &driver_attr_new_id.attr);
107}
108
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800109static void usb_free_dynids(struct usb_driver *usb_drv)
110{
111 struct usb_dynid *dynid, *n;
112
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);
116 kfree(dynid);
117 }
118 spin_unlock(&usb_drv->dynids.lock);
119}
120#else
121static inline int usb_create_newid_file(struct usb_driver *usb_drv)
122{
123 return 0;
124}
125
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800126static void usb_remove_newid_file(struct usb_driver *usb_drv)
127{
128}
129
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800130static inline void usb_free_dynids(struct usb_driver *usb_drv)
131{
132}
133#endif
134
135static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
136 struct usb_driver *drv)
137{
138 struct usb_dynid *dynid;
139
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);
144 return &dynid->id;
145 }
146 }
147 spin_unlock(&drv->dynids.lock);
148 return NULL;
149}
150
151
Alan Stern8bb54ab2006-07-01 22:08:49 -0400152/* called from driver core with dev locked */
153static int usb_probe_device(struct device *dev)
154{
155 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
156 struct usb_device *udev;
157 int error = -ENODEV;
158
159 dev_dbg(dev, "%s\n", __FUNCTION__);
160
161 if (!is_usb_device(dev)) /* Sanity check */
162 return error;
163
164 udev = to_usb_device(dev);
165
Alan Stern8bb54ab2006-07-01 22:08:49 -0400166 /* TODO: Add real matching code */
167
Alan Stern645daaa2006-08-30 15:47:02 -0400168 /* The device should always appear to be in use
169 * unless the driver suports autosuspend.
170 */
171 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
172
Alan Stern8bb54ab2006-07-01 22:08:49 -0400173 error = udriver->probe(udev);
174 return error;
175}
176
177/* called from driver core with dev locked */
178static int usb_unbind_device(struct device *dev)
179{
180 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
181
182 udriver->disconnect(to_usb_device(dev));
183 return 0;
184}
185
186
187/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800188static int usb_probe_interface(struct device *dev)
189{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400190 struct usb_driver *driver = to_usb_driver(dev->driver);
191 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400192 struct usb_device *udev;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800193 const struct usb_device_id *id;
194 int error = -ENODEV;
195
196 dev_dbg(dev, "%s\n", __FUNCTION__);
197
Alan Stern8bb54ab2006-07-01 22:08:49 -0400198 if (is_usb_device(dev)) /* Sanity check */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800199 return error;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400200
201 intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400202 udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800203
204 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800205 if (!id)
206 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800207 if (id) {
208 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
209
Alan Stern94fcda12006-11-20 11:38:46 -0500210 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400211 if (error)
212 return error;
213
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800214 /* 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.
217 */
218 mark_active(intf);
219 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400220
221 /* The interface should always appear to be in use
222 * unless the driver suports autosuspend.
223 */
224 intf->pm_usage_cnt = !(driver->supports_autosuspend);
225
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800226 error = driver->probe(intf, id);
227 if (error) {
228 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400229 intf->needs_remote_wakeup = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800230 intf->condition = USB_INTERFACE_UNBOUND;
231 } else
232 intf->condition = USB_INTERFACE_BOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400233
Alan Stern94fcda12006-11-20 11:38:46 -0500234 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800235 }
236
237 return error;
238}
239
Alan Stern8bb54ab2006-07-01 22:08:49 -0400240/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800241static int usb_unbind_interface(struct device *dev)
242{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400243 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800244 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400245 struct usb_device *udev;
246 int error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800247
248 intf->condition = USB_INTERFACE_UNBINDING;
249
Alan Stern645daaa2006-08-30 15:47:02 -0400250 /* Autoresume for set_interface call below */
251 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500252 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400253
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800254 /* release all urbs for this interface */
255 usb_disable_interface(interface_to_usbdev(intf), intf);
256
Alan Stern8bb54ab2006-07-01 22:08:49 -0400257 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800258
259 /* reset other interface state */
260 usb_set_interface(interface_to_usbdev(intf),
261 intf->altsetting[0].desc.bInterfaceNumber,
262 0);
263 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400264
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800265 intf->condition = USB_INTERFACE_UNBOUND;
266 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400267 intf->needs_remote_wakeup = 0;
268
269 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500270 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800271
272 return 0;
273}
274
Alan Stern36e56a32006-07-01 22:08:06 -0400275/**
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
281 *
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.
286 *
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.
290 *
Greg Kroah-Hartman341487a2007-04-09 11:52:31 -0400291 * 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
293 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400294 */
295int usb_driver_claim_interface(struct usb_driver *driver,
296 struct usb_interface *iface, void* priv)
297{
298 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400299 struct usb_device *udev = interface_to_usbdev(iface);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700300 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400301
302 if (dev->driver)
303 return -EBUSY;
304
Alan Stern8bb54ab2006-07-01 22:08:49 -0400305 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400306 usb_set_intfdata(iface, priv);
Alan Stern645daaa2006-08-30 15:47:02 -0400307
Alan Sterne0318eb2006-09-26 14:50:20 -0400308 usb_pm_lock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400309 iface->condition = USB_INTERFACE_BOUND;
310 mark_active(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400311 iface->pm_usage_cnt = !(driver->supports_autosuspend);
Alan Sterne0318eb2006-09-26 14:50:20 -0400312 usb_pm_unlock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400313
314 /* if interface was already added, bind now; else let
315 * the future device_add() bind it, bypassing probe()
316 */
317 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700318 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400319
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700320 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400321}
322EXPORT_SYMBOL(usb_driver_claim_interface);
323
324/**
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
328 *
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.
332 *
333 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a2007-04-09 11:52:31 -0400334 * 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
336 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400337 */
338void usb_driver_release_interface(struct usb_driver *driver,
339 struct usb_interface *iface)
340{
341 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400342 struct usb_device *udev = interface_to_usbdev(iface);
Alan Stern36e56a32006-07-01 22:08:06 -0400343
344 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400345 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400346 return;
347
348 /* don't release from within disconnect() */
349 if (iface->condition != USB_INTERFACE_BOUND)
350 return;
351
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);
356 }
357
358 dev->driver = NULL;
359 usb_set_intfdata(iface, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400360
Alan Sterne0318eb2006-09-26 14:50:20 -0400361 usb_pm_lock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400362 iface->condition = USB_INTERFACE_UNBOUND;
363 mark_quiesced(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400364 iface->needs_remote_wakeup = 0;
Alan Sterne0318eb2006-09-26 14:50:20 -0400365 usb_pm_unlock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400366}
367EXPORT_SYMBOL(usb_driver_release_interface);
368
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800369/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100370int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800371{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800372 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
373 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
374 return 0;
375
376 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
377 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
378 return 0;
379
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)))
384 return 0;
385
386 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
387 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
388 return 0;
389
390 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
391 (id->bDeviceClass != dev->descriptor.bDeviceClass))
392 return 0;
393
394 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
395 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
396 return 0;
397
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
399 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
400 return 0;
401
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100402 return 1;
403}
404
405/* returns 0 if no match, 1 if match */
406int usb_match_one_id(struct usb_interface *interface,
407 const struct usb_device_id *id)
408{
409 struct usb_host_interface *intf;
410 struct usb_device *dev;
411
412 /* proc_connectinfo in devio.c may call us with id == NULL. */
413 if (id == NULL)
414 return 0;
415
416 intf = interface->cur_altsetting;
417 dev = interface_to_usbdev(interface);
418
419 if (!usb_match_device(dev, id))
420 return 0;
421
Alan Stern93c8bf42006-10-18 16:41:51 -0400422 /* 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)))
430 return 0;
431
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800432 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
433 (id->bInterfaceClass != intf->desc.bInterfaceClass))
434 return 0;
435
436 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
437 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
438 return 0;
439
440 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
441 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
442 return 0;
443
444 return 1;
445}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100446EXPORT_SYMBOL_GPL(usb_match_one_id);
447
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800448/**
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
452 *
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.
460 *
461 * What Matches:
462 *
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
467 * does not match.
468 *
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.
475 *
476 * What Makes Good usb_device_id Tables:
477 *
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.
484 *
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.
491 *
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
496 * its own class.
497 *
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
Alan Stern93c8bf42006-10-18 16:41:51 -0400502 * devices (as recorded in bInterfaceClass).
503 *
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.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800513 *
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.
518 */
519const struct usb_device_id *usb_match_id(struct usb_interface *interface,
520 const struct usb_device_id *id)
521{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800522 /* proc_connectinfo in devio.c may call us with id == NULL. */
523 if (id == NULL)
524 return NULL;
525
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800526 /* 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++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800533 if (usb_match_one_id(interface, id))
534 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800535 }
536
537 return NULL;
538}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800539EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800540
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100541static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800542{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400543 /* devices and interfaces are handled separately */
544 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800545
Alan Stern8bb54ab2006-07-01 22:08:49 -0400546 /* interface drivers never match devices */
547 if (!is_usb_device_driver(drv))
548 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800549
Alan Stern8bb54ab2006-07-01 22:08:49 -0400550 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800551 return 1;
552
Alan Stern8bb54ab2006-07-01 22:08:49 -0400553 } else {
554 struct usb_interface *intf;
555 struct usb_driver *usb_drv;
556 const struct usb_device_id *id;
557
558 /* device drivers never match interfaces */
559 if (is_usb_device_driver(drv))
560 return 0;
561
562 intf = to_usb_interface(dev);
563 usb_drv = to_usb_driver(drv);
564
565 id = usb_match_id(intf, usb_drv->id_table);
566 if (id)
567 return 1;
568
569 id = usb_match_dynamic_id(intf, usb_drv);
570 if (id)
571 return 1;
572 }
573
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800574 return 0;
575}
576
Alan Stern36e56a32006-07-01 22:08:06 -0400577#ifdef CONFIG_HOTPLUG
Alan Stern36e56a32006-07-01 22:08:06 -0400578static int usb_uevent(struct device *dev, char **envp, int num_envp,
579 char *buffer, int buffer_size)
580{
Alan Stern36e56a32006-07-01 22:08:06 -0400581 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400582 int i = 0;
583 int length = 0;
584
585 if (!dev)
586 return -ENODEV;
587
588 /* driver is often null here; dev_dbg() would oops */
589 pr_debug ("usb %s: uevent\n", dev->bus_id);
590
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100591 if (is_usb_device(dev))
Alan Stern782da722006-07-01 22:09:35 -0400592 usb_dev = to_usb_device(dev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100593 else {
594 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400595 usb_dev = interface_to_usbdev(intf);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400596 }
Alan Stern36e56a32006-07-01 22:08:06 -0400597
598 if (usb_dev->devnum < 0) {
599 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
600 return -ENODEV;
601 }
602 if (!usb_dev->bus) {
603 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
604 return -ENODEV;
605 }
606
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
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100610 * act as usermode drivers.
Alan Stern36e56a32006-07-01 22:08:06 -0400611 */
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))
616 return -ENOMEM;
617#endif
618
619 /* per-device configurations are common */
620 if (add_uevent_var(envp, num_envp, &i,
621 buffer, buffer_size, &length,
622 "PRODUCT=%x/%x/%x",
623 le16_to_cpu(usb_dev->descriptor.idVendor),
624 le16_to_cpu(usb_dev->descriptor.idProduct),
625 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
626 return -ENOMEM;
627
628 /* class-based driver binding models */
629 if (add_uevent_var(envp, num_envp, &i,
630 buffer, buffer_size, &length,
631 "TYPE=%d/%d/%d",
632 usb_dev->descriptor.bDeviceClass,
633 usb_dev->descriptor.bDeviceSubClass,
634 usb_dev->descriptor.bDeviceProtocol))
635 return -ENOMEM;
636
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100637 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400638 buffer, buffer_size, &length,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100639 "BUSNUM=%03d",
640 usb_dev->bus->busnum))
641 return -ENOMEM;
Alan Stern36e56a32006-07-01 22:08:06 -0400642
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100643 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400644 buffer, buffer_size, &length,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100645 "DEVNUM=%03d",
646 usb_dev->devnum))
647 return -ENOMEM;
Alan Stern36e56a32006-07-01 22:08:06 -0400648
649 envp[i] = NULL;
Alan Stern36e56a32006-07-01 22:08:06 -0400650 return 0;
651}
652
653#else
654
655static int usb_uevent(struct device *dev, char **envp,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100656 int num_envp, char *buffer, int buffer_size)
Alan Stern36e56a32006-07-01 22:08:06 -0400657{
658 return -ENODEV;
659}
Alan Stern36e56a32006-07-01 22:08:06 -0400660#endif /* CONFIG_HOTPLUG */
661
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800662/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400663 * usb_register_device_driver - register a USB device (not interface) driver
664 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800665 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800666 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400667 * 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.
671 */
672int usb_register_device_driver(struct usb_device_driver *new_udriver,
673 struct module *owner)
674{
675 int retval = 0;
676
677 if (usb_disabled())
678 return -ENODEV;
679
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;
686
687 retval = driver_register(&new_udriver->drvwrap.driver);
688
689 if (!retval) {
690 pr_info("%s: registered new device driver %s\n",
691 usbcore_name, new_udriver->name);
692 usbfs_update_special();
693 } else {
694 printk(KERN_ERR "%s: error %d registering device "
695 " driver %s\n",
696 usbcore_name, retval, new_udriver->name);
697 }
698
699 return retval;
700}
701EXPORT_SYMBOL_GPL(usb_register_device_driver);
702
703/**
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
707 *
708 * Unlinks the specified driver from the internal USB driver list.
709 */
710void usb_deregister_device_driver(struct usb_device_driver *udriver)
711{
712 pr_info("%s: deregistering device driver %s\n",
713 usbcore_name, udriver->name);
714
715 driver_unregister(&udriver->drvwrap.driver);
716 usbfs_update_special();
717}
718EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
719
720/**
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.
Randy Dunlap892705a2007-02-10 14:41:41 -0800724 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400725 *
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.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800729 * Returns a negative error code on failure and 0 on success.
730 *
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.
734 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800735int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
736 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800737{
738 int retval = 0;
739
740 if (usb_disabled())
741 return -ENODEV;
742
Alan Stern8bb54ab2006-07-01 22:08:49 -0400743 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;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800749 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800750 spin_lock_init(&new_driver->dynids.lock);
751 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800752
Alan Stern8bb54ab2006-07-01 22:08:49 -0400753 retval = driver_register(&new_driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800754
755 if (!retval) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400756 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800757 usbcore_name, new_driver->name);
758 usbfs_update_special();
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800759 usb_create_newid_file(new_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800760 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400761 printk(KERN_ERR "%s: error %d registering interface "
762 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800763 usbcore_name, retval, new_driver->name);
764 }
765
766 return retval;
767}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800768EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800769
770/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400771 * usb_deregister - unregister a USB interface driver
772 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800773 * Context: must be able to sleep
774 *
775 * Unlinks the specified driver from the internal USB driver list.
776 *
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.
780 */
781void usb_deregister(struct usb_driver *driver)
782{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400783 pr_info("%s: deregistering interface driver %s\n",
784 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800785
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800786 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800787 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400788 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800789
790 usbfs_update_special();
791}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800792EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400793
794#ifdef CONFIG_PM
795
Alan Sterne0318eb2006-09-26 14:50:20 -0400796/* Caller has locked udev's pm_mutex */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -0800797static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -0400798{
Alan Stern782da722006-07-01 22:09:35 -0400799 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400800 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400801
Alan Stern114b3682006-07-01 22:13:04 -0400802 if (udev->state == USB_STATE_NOTATTACHED ||
803 udev->state == USB_STATE_SUSPENDED)
804 goto done;
805
Alan Sternb6f6436d2007-05-04 11:51:54 -0400806 /* 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);
809 else {
Alan Stern645daaa2006-08-30 15:47:02 -0400810 udev->do_remote_wakeup = 0;
Alan Sternb6f6436d2007-05-04 11:51:54 -0400811 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400812 }
Alan Stern2bf40862006-07-01 22:12:19 -0400813 status = udriver->suspend(udev, msg);
814
815done:
Alan Stern645daaa2006-08-30 15:47:02 -0400816 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400817 if (status == 0)
818 udev->dev.power.power_state.event = msg.event;
819 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400820}
Alan Stern36e56a32006-07-01 22:08:06 -0400821
Alan Sterne0318eb2006-09-26 14:50:20 -0400822/* Caller has locked udev's pm_mutex */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -0800823static int usb_resume_device(struct usb_device *udev)
Alan Stern1cc8a252006-07-01 22:10:15 -0400824{
825 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400826 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400827
Alan Stern0458d5b2007-05-04 11:52:20 -0400828 if (udev->state == USB_STATE_NOTATTACHED)
829 goto done;
830 if (udev->state != USB_STATE_SUSPENDED && !udev->reset_resume)
Alan Stern2bf40862006-07-01 22:12:19 -0400831 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400832
Alan Stern1c5df7e2006-07-01 22:13:50 -0400833 /* Can't resume it if it doesn't have a driver. */
834 if (udev->dev.driver == NULL) {
835 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400836 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400837 }
838
Alan Stern6bc6cff2007-05-04 11:53:03 -0400839 if (udev->quirks & USB_QUIRK_RESET_RESUME)
840 udev->reset_resume = 1;
841
Alan Stern1cc8a252006-07-01 22:10:15 -0400842 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern2bf40862006-07-01 22:12:19 -0400843 status = udriver->resume(udev);
844
845done:
Alan Stern645daaa2006-08-30 15:47:02 -0400846 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2add5222007-03-20 14:59:39 -0400847 if (status == 0) {
848 udev->autoresume_disabled = 0;
Alan Stern2bf40862006-07-01 22:12:19 -0400849 udev->dev.power.power_state.event = PM_EVENT_ON;
Alan Stern2add5222007-03-20 14:59:39 -0400850 }
Alan Stern2bf40862006-07-01 22:12:19 -0400851 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400852}
853
Alan Sterne0318eb2006-09-26 14:50:20 -0400854/* Caller has locked intf's usb_device's pm mutex */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -0800855static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -0400856{
857 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400858 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400859
Alan Stern114b3682006-07-01 22:13:04 -0400860 /* with no hardware, USB interfaces only use FREEZE and ON states */
861 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
862 !is_active(intf))
863 goto done;
864
Alan Stern645daaa2006-08-30 15:47:02 -0400865 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
Alan Stern2bf40862006-07-01 22:12:19 -0400866 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400867 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400868
Alan Stern36e56a32006-07-01 22:08:06 -0400869 if (driver->suspend && driver->resume) {
Alan Stern1cc8a252006-07-01 22:10:15 -0400870 status = driver->suspend(intf, msg);
Alan Stern645daaa2006-08-30 15:47:02 -0400871 if (status == 0)
872 mark_quiesced(intf);
873 else if (!interface_to_usbdev(intf)->auto_pm)
Alan Stern1cc8a252006-07-01 22:10:15 -0400874 dev_err(&intf->dev, "%s error %d\n",
875 "suspend", status);
Alan Stern36e56a32006-07-01 22:08:06 -0400876 } else {
877 // FIXME else if there's no suspend method, disconnect...
Alan Stern645daaa2006-08-30 15:47:02 -0400878 // Not possible if auto_pm is set...
Alan Stern1cc8a252006-07-01 22:10:15 -0400879 dev_warn(&intf->dev, "no suspend for driver %s?\n",
880 driver->name);
Alan Stern36e56a32006-07-01 22:08:06 -0400881 mark_quiesced(intf);
Alan Stern36e56a32006-07-01 22:08:06 -0400882 }
Alan Stern2bf40862006-07-01 22:12:19 -0400883
884done:
Alan Stern645daaa2006-08-30 15:47:02 -0400885 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern36e56a32006-07-01 22:08:06 -0400886 return status;
887}
888
Alan Stern645daaa2006-08-30 15:47:02 -0400889/* Caller has locked intf's usb_device's pm_mutex */
Alan Stern0458d5b2007-05-04 11:52:20 -0400890static int usb_resume_interface(struct usb_interface *intf, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -0400891{
Alan Stern1cc8a252006-07-01 22:10:15 -0400892 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400893 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400894
Alan Stern114b3682006-07-01 22:13:04 -0400895 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
896 is_active(intf))
Alan Stern2bf40862006-07-01 22:12:19 -0400897 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -0400898
Alan Stern645daaa2006-08-30 15:47:02 -0400899 /* Don't let autoresume interfere with unbinding */
900 if (intf->condition == USB_INTERFACE_UNBINDING)
901 goto done;
902
Alan Stern1c5df7e2006-07-01 22:13:50 -0400903 /* Can't resume it if it doesn't have a driver. */
Alan Stern645daaa2006-08-30 15:47:02 -0400904 if (intf->condition == USB_INTERFACE_UNBOUND) {
Alan Stern1c5df7e2006-07-01 22:13:50 -0400905 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400906 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400907 }
Alan Stern1cc8a252006-07-01 22:10:15 -0400908 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400909
Alan Stern0458d5b2007-05-04 11:52:20 -0400910 if (reset_resume && driver->post_reset)
911 driver->post_reset(intf, reset_resume);
912 else if (driver->resume) {
Alan Stern36e56a32006-07-01 22:08:06 -0400913 status = driver->resume(intf);
Alan Stern2bf40862006-07-01 22:12:19 -0400914 if (status)
Alan Stern1cc8a252006-07-01 22:10:15 -0400915 dev_err(&intf->dev, "%s error %d\n",
916 "resume", status);
Alan Stern0458d5b2007-05-04 11:52:20 -0400917 } else
Alan Stern1cc8a252006-07-01 22:10:15 -0400918 dev_warn(&intf->dev, "no resume for driver %s?\n",
919 driver->name);
Alan Stern2bf40862006-07-01 22:12:19 -0400920
921done:
Alan Stern645daaa2006-08-30 15:47:02 -0400922 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern0458d5b2007-05-04 11:52:20 -0400923 if (status == 0)
924 mark_active(intf);
Alan Stern2bf40862006-07-01 22:12:19 -0400925 return status;
Alan Stern36e56a32006-07-01 22:08:06 -0400926}
927
Alan Stern94fcda12006-11-20 11:38:46 -0500928#ifdef CONFIG_USB_SUSPEND
929
Alan Sternaf4f7602006-10-30 17:06:45 -0500930/* Internal routine to check whether we may autosuspend a device. */
931static int autosuspend_check(struct usb_device *udev)
932{
933 int i;
934 struct usb_interface *intf;
Alan Stern8c9862e2007-04-11 12:06:16 -0400935 unsigned long suspend_time;
Alan Sternaf4f7602006-10-30 17:06:45 -0500936
Alan Sternb5e795f2007-02-20 15:00:53 -0500937 /* 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.
940 */
Alan Sternaf4f7602006-10-30 17:06:45 -0500941 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
942 if (udev->pm_usage_cnt > 0)
943 return -EBUSY;
Alan Stern2add5222007-03-20 14:59:39 -0400944 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
Alan Sternb5e795f2007-02-20 15:00:53 -0500945 return -EPERM;
946
Alan Stern19410442007-03-27 13:33:59 -0400947 suspend_time = udev->last_busy + udev->autosuspend_delay;
Alan Sternaf4f7602006-10-30 17:06:45 -0500948 if (udev->actconfig) {
949 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
950 intf = udev->actconfig->interface[i];
951 if (!is_active(intf))
952 continue;
953 if (intf->pm_usage_cnt > 0)
954 return -EBUSY;
955 if (intf->needs_remote_wakeup &&
956 !udev->do_remote_wakeup) {
957 dev_dbg(&udev->dev, "remote wakeup needed "
958 "for autosuspend\n");
959 return -EOPNOTSUPP;
960 }
961 }
962 }
Alan Stern19410442007-03-27 13:33:59 -0400963
964 /* If everything is okay but the device hasn't been idle for long
965 * enough, queue a delayed autosuspend request.
966 */
Alan Stern8c9862e2007-04-11 12:06:16 -0400967 if (time_after(suspend_time, jiffies)) {
968 if (!timer_pending(&udev->autosuspend.timer)) {
969
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
974 * or for the past.
975 */
Alan Stern19410442007-03-27 13:33:59 -0400976 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
Alan Stern8c9862e2007-04-11 12:06:16 -0400977 suspend_time - jiffies);
978 }
Alan Stern19410442007-03-27 13:33:59 -0400979 return -EAGAIN;
980 }
Alan Sternaf4f7602006-10-30 17:06:45 -0500981 return 0;
982}
983
Alan Stern94fcda12006-11-20 11:38:46 -0500984#else
985
Alan Sternef7f6c72007-04-05 16:03:49 -0400986static inline int autosuspend_check(struct usb_device *udev)
987{
988 return 0;
989}
Alan Stern94fcda12006-11-20 11:38:46 -0500990
Alan Sternb5e795f2007-02-20 15:00:53 -0500991#endif /* CONFIG_USB_SUSPEND */
Alan Stern94fcda12006-11-20 11:38:46 -0500992
Alan Stern645daaa2006-08-30 15:47:02 -0400993/**
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
997 *
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.
1003 *
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.
1008 *
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.
1013 *
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
1023 * suspends).
1024 *
1025 * The caller must hold @udev->pm_mutex.
1026 *
1027 * This routine can run only in process context.
1028 */
Alan Stern718efa62007-03-09 15:41:13 -05001029static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001030{
1031 int status = 0;
1032 int i = 0;
1033 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001034 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001035
Alan Stern19410442007-03-27 13:33:59 -04001036 if (udev->state == USB_STATE_NOTATTACHED ||
1037 udev->state == USB_STATE_SUSPENDED)
1038 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001039
1040 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1041
Alan Stern645daaa2006-08-30 15:47:02 -04001042 if (udev->auto_pm) {
Alan Sternaf4f7602006-10-30 17:06:45 -05001043 status = autosuspend_check(udev);
1044 if (status < 0)
Alan Stern19410442007-03-27 13:33:59 -04001045 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001046 }
1047
1048 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001049 if (udev->actconfig) {
1050 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1051 intf = udev->actconfig->interface[i];
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001052 status = usb_suspend_interface(intf, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001053 if (status != 0)
1054 break;
1055 }
1056 }
Alan Stern4d461092007-05-04 11:51:25 -04001057 if (status == 0) {
1058
1059 /* Non-root devices don't need to do anything for FREEZE
1060 * or PRETHAW. */
1061 if (udev->parent && (msg.event == PM_EVENT_FREEZE ||
1062 msg.event == PM_EVENT_PRETHAW))
1063 goto done;
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001064 status = usb_suspend_device(udev, msg);
Alan Stern4d461092007-05-04 11:51:25 -04001065 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001066
1067 /* If the suspend failed, resume interfaces that did get suspended */
1068 if (status != 0) {
1069 while (--i >= 0) {
1070 intf = udev->actconfig->interface[i];
Alan Stern0458d5b2007-05-04 11:52:20 -04001071 usb_resume_interface(intf, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001072 }
Alan Stern645daaa2006-08-30 15:47:02 -04001073
Alan Sternef7f6c72007-04-05 16:03:49 -04001074 /* Try another autosuspend when the interfaces aren't busy */
1075 if (udev->auto_pm)
1076 autosuspend_check(udev);
1077
Alan Stern645daaa2006-08-30 15:47:02 -04001078 /* If the suspend succeeded, propagate it up the tree */
Alan Sternef7f6c72007-04-05 16:03:49 -04001079 } else {
1080 cancel_delayed_work(&udev->autosuspend);
1081 if (parent)
1082 usb_autosuspend_device(parent);
1083 }
Alan Stern645daaa2006-08-30 15:47:02 -04001084
Alan Stern19410442007-03-27 13:33:59 -04001085 done:
Alan Stern645daaa2006-08-30 15:47:02 -04001086 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001087 return status;
1088}
1089
Alan Stern645daaa2006-08-30 15:47:02 -04001090/**
1091 * usb_resume_both - resume a USB device and its interfaces
1092 * @udev: the usb_device to resume
1093 *
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.
1097 *
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.
1102 *
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
1112 * resumes).
1113 *
1114 * The caller must hold @udev->pm_mutex.
1115 *
1116 * This routine can run only in process context.
1117 */
Alan Stern718efa62007-03-09 15:41:13 -05001118static int usb_resume_both(struct usb_device *udev)
Alan Sterna8e7c562006-07-01 22:11:02 -04001119{
Alan Stern645daaa2006-08-30 15:47:02 -04001120 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001121 int i;
1122 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001123 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001124
Alan Stern645daaa2006-08-30 15:47:02 -04001125 cancel_delayed_work(&udev->autosuspend);
Alan Stern19410442007-03-27 13:33:59 -04001126 if (udev->state == USB_STATE_NOTATTACHED) {
1127 status = -ENODEV;
1128 goto done;
1129 }
Alan Stern645daaa2006-08-30 15:47:02 -04001130
1131 /* Propagate the resume up the tree, if necessary */
1132 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern19410442007-03-27 13:33:59 -04001133 if (udev->auto_pm && udev->autoresume_disabled) {
1134 status = -EPERM;
1135 goto done;
1136 }
Alan Stern645daaa2006-08-30 15:47:02 -04001137 if (parent) {
Alan Stern94fcda12006-11-20 11:38:46 -05001138 status = usb_autoresume_device(parent);
Alan Sternee49fb52006-11-22 16:55:54 -05001139 if (status == 0) {
1140 status = usb_resume_device(udev);
1141 if (status) {
Alan Stern94fcda12006-11-20 11:38:46 -05001142 usb_autosuspend_device(parent);
Alan Sternee49fb52006-11-22 16:55:54 -05001143
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
1151 * suspended. */
1152 if (udev->state ==
1153 USB_STATE_NOTATTACHED)
1154 udev->discon_suspended = 1;
1155 }
1156 }
Alan Stern645daaa2006-08-30 15:47:02 -04001157 } else {
1158
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 !=
1163 PM_EVENT_ON)
1164 status = -EHOSTUNREACH;
Alan Sternee49fb52006-11-22 16:55:54 -05001165 else
1166 status = usb_resume_device(udev);
1167 }
Alan Stern592fbbe2006-09-19 10:08:43 -04001168 } else {
1169
Alan Stern0458d5b2007-05-04 11:52:20 -04001170 /* Needed for setting udev->dev.power.power_state.event,
1171 * for possible debugging message, and for reset_resume. */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001172 status = usb_resume_device(udev);
Alan Stern114b3682006-07-01 22:13:04 -04001173 }
1174
Alan Sterna8e7c562006-07-01 22:11:02 -04001175 if (status == 0 && udev->actconfig) {
1176 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1177 intf = udev->actconfig->interface[i];
Alan Stern0458d5b2007-05-04 11:52:20 -04001178 usb_resume_interface(intf, udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001179 }
1180 }
Alan Stern645daaa2006-08-30 15:47:02 -04001181
Alan Stern19410442007-03-27 13:33:59 -04001182 done:
Alan Stern645daaa2006-08-30 15:47:02 -04001183 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern0458d5b2007-05-04 11:52:20 -04001184 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001185 return status;
1186}
1187
Alan Stern645daaa2006-08-30 15:47:02 -04001188#ifdef CONFIG_USB_SUSPEND
1189
Alan Stern94fcda12006-11-20 11:38:46 -05001190/* Internal routine to adjust a device's usage counter and change
1191 * its autosuspend state.
1192 */
1193static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1194{
1195 int status = 0;
1196
1197 usb_pm_lock(udev);
Alan Stern19410442007-03-27 13:33:59 -04001198 udev->auto_pm = 1;
Alan Stern94fcda12006-11-20 11:38:46 -05001199 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) {
Alan Stern19410442007-03-27 13:33:59 -04001202 if (udev->state == USB_STATE_SUSPENDED)
1203 status = usb_resume_both(udev);
Alan Stern94fcda12006-11-20 11:38:46 -05001204 if (status != 0)
1205 udev->pm_usage_cnt -= inc_usage_cnt;
Alan Stern19410442007-03-27 13:33:59 -04001206 else if (inc_usage_cnt)
1207 udev->last_busy = jiffies;
1208 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1209 if (inc_usage_cnt)
1210 udev->last_busy = jiffies;
1211 status = usb_suspend_both(udev, PMSG_SUSPEND);
1212 }
Alan Stern94fcda12006-11-20 11:38:46 -05001213 usb_pm_unlock(udev);
1214 return status;
1215}
1216
Alan Stern19410442007-03-27 13:33:59 -04001217/* usb_autosuspend_work - callback routine to autosuspend a USB device */
1218void usb_autosuspend_work(struct work_struct *work)
1219{
1220 struct usb_device *udev =
1221 container_of(work, struct usb_device, autosuspend.work);
1222
1223 usb_autopm_do_device(udev, 0);
1224}
1225
Alan Stern645daaa2006-08-30 15:47:02 -04001226/**
1227 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001228 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001229 *
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.
1233 *
Alan Stern94fcda12006-11-20 11:38:46 -05001234 * @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.
Alan Stern645daaa2006-08-30 15:47:02 -04001240 *
1241 * Often the caller will hold @udev's device lock, but this is not
1242 * necessary.
1243 *
1244 * This routine can run only in process context.
1245 */
Alan Stern94fcda12006-11-20 11:38:46 -05001246void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001247{
Alan Stern94fcda12006-11-20 11:38:46 -05001248 int status;
1249
1250 status = usb_autopm_do_device(udev, -1);
Alan Stern645daaa2006-08-30 15:47:02 -04001251 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1252 // __FUNCTION__, udev->pm_usage_cnt);
1253}
1254
1255/**
Alan Stern19c26232007-02-20 15:03:32 -05001256 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1257 * @udev: the usb_device to autosuspend
1258 *
1259 * This routine should be called when a core subsystem thinks @udev may
1260 * be ready to autosuspend.
1261 *
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.
1265 *
1266 * This routine can run only in process context.
1267 */
1268void usb_try_autosuspend_device(struct usb_device *udev)
1269{
1270 usb_autopm_do_device(udev, 0);
1271 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1272 // __FUNCTION__, udev->pm_usage_cnt);
1273}
1274
1275/**
Alan Stern645daaa2006-08-30 15:47:02 -04001276 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001277 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001278 *
1279 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001280 * 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.
Alan Stern645daaa2006-08-30 15:47:02 -04001285 *
Alan Stern94fcda12006-11-20 11:38:46 -05001286 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1287 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001288 *
1289 * Often the caller will hold @udev's device lock, but this is not
1290 * necessary (and attempting it might cause deadlock).
1291 *
1292 * This routine can run only in process context.
1293 */
Alan Stern94fcda12006-11-20 11:38:46 -05001294int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001295{
1296 int status;
1297
Alan Stern94fcda12006-11-20 11:38:46 -05001298 status = usb_autopm_do_device(udev, 1);
Alan Stern645daaa2006-08-30 15:47:02 -04001299 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1300 // __FUNCTION__, status, udev->pm_usage_cnt);
1301 return status;
1302}
1303
Alan Sternaf4f7602006-10-30 17:06:45 -05001304/* Internal routine to adjust an interface's usage counter and change
1305 * its device's autosuspend state.
1306 */
1307static int usb_autopm_do_interface(struct usb_interface *intf,
1308 int inc_usage_cnt)
1309{
1310 struct usb_device *udev = interface_to_usbdev(intf);
1311 int status = 0;
1312
1313 usb_pm_lock(udev);
1314 if (intf->condition == USB_INTERFACE_UNBOUND)
1315 status = -ENODEV;
1316 else {
Alan Stern19410442007-03-27 13:33:59 -04001317 udev->auto_pm = 1;
Alan Sternaf4f7602006-10-30 17:06:45 -05001318 intf->pm_usage_cnt += inc_usage_cnt;
1319 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
Alan Stern19410442007-03-27 13:33:59 -04001320 if (udev->state == USB_STATE_SUSPENDED)
1321 status = usb_resume_both(udev);
Alan Sternaf4f7602006-10-30 17:06:45 -05001322 if (status != 0)
1323 intf->pm_usage_cnt -= inc_usage_cnt;
Alan Stern19410442007-03-27 13:33:59 -04001324 else if (inc_usage_cnt)
1325 udev->last_busy = jiffies;
1326 } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) {
1327 if (inc_usage_cnt)
1328 udev->last_busy = jiffies;
1329 status = usb_suspend_both(udev, PMSG_SUSPEND);
1330 }
Alan Sternaf4f7602006-10-30 17:06:45 -05001331 }
1332 usb_pm_unlock(udev);
1333 return status;
1334}
1335
Alan Stern645daaa2006-08-30 15:47:02 -04001336/**
1337 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001338 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001339 *
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
1343 * closed.
1344 *
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.
1350 *
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
1355 * any way it likes.
1356 *
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.
1359 *
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.
1364 *
1365 * This routine can run only in process context.
1366 */
1367void usb_autopm_put_interface(struct usb_interface *intf)
1368{
Alan Sternaf4f7602006-10-30 17:06:45 -05001369 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001370
Alan Sternaf4f7602006-10-30 17:06:45 -05001371 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);
Alan Stern645daaa2006-08-30 15:47:02 -04001374}
1375EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1376
1377/**
1378 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001379 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001380 *
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.
1388 *
Alan Stern19410442007-03-27 13:33:59 -04001389 *
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).
1396 *
Alan Stern645daaa2006-08-30 15:47:02 -04001397 *
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
1402 * any way it likes.
1403 *
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
1407 * synchronization.
1408 *
1409 * This routine can run only in process context.
1410 */
1411int usb_autopm_get_interface(struct usb_interface *intf)
1412{
Alan Sternaf4f7602006-10-30 17:06:45 -05001413 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001414
Alan Sternaf4f7602006-10-30 17:06:45 -05001415 status = usb_autopm_do_interface(intf, 1);
Alan Stern645daaa2006-08-30 15:47:02 -04001416 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1417 // __FUNCTION__, status, intf->pm_usage_cnt);
1418 return status;
1419}
1420EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1421
Alan Stern692a1862006-10-30 17:07:51 -05001422/**
1423 * usb_autopm_set_interface - set a USB interface's autosuspend state
1424 * @intf: the usb_interface whose state should be set
1425 *
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
1431 * awake).
1432 */
1433int usb_autopm_set_interface(struct usb_interface *intf)
1434{
1435 int status;
1436
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);
1440 return status;
1441}
1442EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1443
Alan Stern718efa62007-03-09 15:41:13 -05001444#else
1445
1446void usb_autosuspend_work(struct work_struct *work)
1447{}
1448
Alan Stern645daaa2006-08-30 15:47:02 -04001449#endif /* CONFIG_USB_SUSPEND */
1450
Alan Stern6b157c92007-03-13 16:37:30 -04001451/**
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
1455 *
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).
1463 *
1464 * The caller must hold @udev's device lock.
1465 */
1466int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001467{
1468 int status;
1469
Alan Stern6b157c92007-03-13 16:37:30 -04001470 usb_pm_lock(udev);
1471 udev->auto_pm = 0;
1472 status = usb_suspend_both(udev, msg);
1473 usb_pm_unlock(udev);
Alan Stern1cc8a252006-07-01 22:10:15 -04001474 return status;
1475}
1476
Alan Stern6b157c92007-03-13 16:37:30 -04001477/**
1478 * usb_external_resume_device - external resume of a USB device and its interfaces
1479 * @udev: the usb_device to resume
1480 *
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.
1485 *
1486 * The caller must hold @udev's device lock.
1487 */
1488int usb_external_resume_device(struct usb_device *udev)
1489{
1490 int status;
1491
1492 usb_pm_lock(udev);
1493 udev->auto_pm = 0;
1494 status = usb_resume_both(udev);
Alan Sternef7f6c72007-04-05 16:03:49 -04001495 udev->last_busy = jiffies;
Alan Stern6b157c92007-03-13 16:37:30 -04001496 usb_pm_unlock(udev);
1497
1498 /* Now that the device is awake, we can start trying to autosuspend
1499 * it again. */
1500 if (status == 0)
1501 usb_try_autosuspend_device(udev);
1502 return status;
1503}
1504
1505static int usb_suspend(struct device *dev, pm_message_t message)
1506{
1507 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1508 return 0;
1509 return usb_external_suspend_device(to_usb_device(dev), message);
1510}
1511
Alan Stern1cc8a252006-07-01 22:10:15 -04001512static int usb_resume(struct device *dev)
1513{
Alan Stern2add5222007-03-20 14:59:39 -04001514 struct usb_device *udev;
1515
Alan Stern6b157c92007-03-13 16:37:30 -04001516 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1517 return 0;
Alan Stern2add5222007-03-20 14:59:39 -04001518 udev = to_usb_device(dev);
Alan Stern0458d5b2007-05-04 11:52:20 -04001519
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))
1526 return -EPERM;
1527 }
Alan Stern2add5222007-03-20 14:59:39 -04001528 return usb_external_resume_device(udev);
Alan Stern1cc8a252006-07-01 22:10:15 -04001529}
1530
Alan Stern6b157c92007-03-13 16:37:30 -04001531#else
1532
1533#define usb_suspend NULL
1534#define usb_resume NULL
1535
Alan Stern36e56a32006-07-01 22:08:06 -04001536#endif /* CONFIG_PM */
1537
1538struct bus_type usb_bus_type = {
1539 .name = "usb",
1540 .match = usb_device_match,
1541 .uevent = usb_uevent,
Alan Stern782da722006-07-01 22:09:35 -04001542 .suspend = usb_suspend,
1543 .resume = usb_resume,
Alan Stern36e56a32006-07-01 22:08:06 -04001544};