blob: 593386eb974dc6f34b12d2419ae0357b03a69f4b [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 Sternbd859282006-09-19 10:14:07 -040027#include <linux/workqueue.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080028#include "hcd.h"
29#include "usb.h"
30
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080031#ifdef CONFIG_HOTPLUG
32
33/*
34 * Adds a new dynamic USBdevice ID to this driver,
35 * and cause the driver to probe for all devices again.
36 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010037ssize_t usb_store_new_id(struct usb_dynids *dynids,
38 struct device_driver *driver,
39 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080040{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080041 struct usb_dynid *dynid;
42 u32 idVendor = 0;
43 u32 idProduct = 0;
44 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070045 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080046
47 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
48 if (fields < 2)
49 return -EINVAL;
50
51 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
52 if (!dynid)
53 return -ENOMEM;
54
55 INIT_LIST_HEAD(&dynid->node);
56 dynid->id.idVendor = idVendor;
57 dynid->id.idProduct = idProduct;
58 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
59
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010060 spin_lock(&dynids->lock);
61 list_add_tail(&dynids->list, &dynid->node);
62 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080063
64 if (get_driver(driver)) {
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070065 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080066 put_driver(driver);
67 }
68
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070069 if (retval)
70 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080071 return count;
72}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010073EXPORT_SYMBOL_GPL(usb_store_new_id);
74
75static ssize_t store_new_id(struct device_driver *driver,
76 const char *buf, size_t count)
77{
78 struct usb_driver *usb_drv = to_usb_driver(driver);
79
80 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
81}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080082static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
83
84static int usb_create_newid_file(struct usb_driver *usb_drv)
85{
86 int error = 0;
87
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080088 if (usb_drv->no_dynamic_id)
89 goto exit;
90
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080091 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -040092 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080093 &driver_attr_new_id.attr);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080094exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080095 return error;
96}
97
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080098static void usb_remove_newid_file(struct usb_driver *usb_drv)
99{
100 if (usb_drv->no_dynamic_id)
101 return;
102
103 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400104 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800105 &driver_attr_new_id.attr);
106}
107
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800108static void usb_free_dynids(struct usb_driver *usb_drv)
109{
110 struct usb_dynid *dynid, *n;
111
112 spin_lock(&usb_drv->dynids.lock);
113 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
114 list_del(&dynid->node);
115 kfree(dynid);
116 }
117 spin_unlock(&usb_drv->dynids.lock);
118}
119#else
120static inline int usb_create_newid_file(struct usb_driver *usb_drv)
121{
122 return 0;
123}
124
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800125static void usb_remove_newid_file(struct usb_driver *usb_drv)
126{
127}
128
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800129static inline void usb_free_dynids(struct usb_driver *usb_drv)
130{
131}
132#endif
133
134static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
135 struct usb_driver *drv)
136{
137 struct usb_dynid *dynid;
138
139 spin_lock(&drv->dynids.lock);
140 list_for_each_entry(dynid, &drv->dynids.list, node) {
141 if (usb_match_one_id(intf, &dynid->id)) {
142 spin_unlock(&drv->dynids.lock);
143 return &dynid->id;
144 }
145 }
146 spin_unlock(&drv->dynids.lock);
147 return NULL;
148}
149
150
Alan Stern8bb54ab2006-07-01 22:08:49 -0400151/* called from driver core with dev locked */
152static int usb_probe_device(struct device *dev)
153{
154 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
155 struct usb_device *udev;
156 int error = -ENODEV;
157
158 dev_dbg(dev, "%s\n", __FUNCTION__);
159
160 if (!is_usb_device(dev)) /* Sanity check */
161 return error;
162
163 udev = to_usb_device(dev);
164
Alan Stern8bb54ab2006-07-01 22:08:49 -0400165 /* TODO: Add real matching code */
166
Alan Stern645daaa2006-08-30 15:47:02 -0400167 /* The device should always appear to be in use
168 * unless the driver suports autosuspend.
169 */
170 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
171
Alan Stern8bb54ab2006-07-01 22:08:49 -0400172 error = udriver->probe(udev);
173 return error;
174}
175
176/* called from driver core with dev locked */
177static int usb_unbind_device(struct device *dev)
178{
179 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
180
181 udriver->disconnect(to_usb_device(dev));
182 return 0;
183}
184
185
186/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800187static int usb_probe_interface(struct device *dev)
188{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400189 struct usb_driver *driver = to_usb_driver(dev->driver);
190 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400191 struct usb_device *udev;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800192 const struct usb_device_id *id;
193 int error = -ENODEV;
194
195 dev_dbg(dev, "%s\n", __FUNCTION__);
196
Alan Stern8bb54ab2006-07-01 22:08:49 -0400197 if (is_usb_device(dev)) /* Sanity check */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800198 return error;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400199
200 intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400201 udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800202
203 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800204 if (!id)
205 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800206 if (id) {
207 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
208
Alan Stern94fcda12006-11-20 11:38:46 -0500209 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400210 if (error)
211 return error;
212
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800213 /* Interface "power state" doesn't correspond to any hardware
214 * state whatsoever. We use it to record when it's bound to
215 * a driver that may start I/0: it's not frozen/quiesced.
216 */
217 mark_active(intf);
218 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400219
220 /* The interface should always appear to be in use
221 * unless the driver suports autosuspend.
222 */
223 intf->pm_usage_cnt = !(driver->supports_autosuspend);
224
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800225 error = driver->probe(intf, id);
226 if (error) {
227 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400228 intf->needs_remote_wakeup = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800229 intf->condition = USB_INTERFACE_UNBOUND;
230 } else
231 intf->condition = USB_INTERFACE_BOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400232
Alan Stern94fcda12006-11-20 11:38:46 -0500233 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800234 }
235
236 return error;
237}
238
Alan Stern8bb54ab2006-07-01 22:08:49 -0400239/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800240static int usb_unbind_interface(struct device *dev)
241{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400242 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800243 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400244 struct usb_device *udev;
245 int error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800246
247 intf->condition = USB_INTERFACE_UNBINDING;
248
Alan Stern645daaa2006-08-30 15:47:02 -0400249 /* Autoresume for set_interface call below */
250 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500251 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400252
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800253 /* release all urbs for this interface */
254 usb_disable_interface(interface_to_usbdev(intf), intf);
255
Alan Stern8bb54ab2006-07-01 22:08:49 -0400256 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800257
258 /* reset other interface state */
259 usb_set_interface(interface_to_usbdev(intf),
260 intf->altsetting[0].desc.bInterfaceNumber,
261 0);
262 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400263
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800264 intf->condition = USB_INTERFACE_UNBOUND;
265 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400266 intf->needs_remote_wakeup = 0;
267
268 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500269 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800270
271 return 0;
272}
273
Alan Stern36e56a32006-07-01 22:08:06 -0400274/**
275 * usb_driver_claim_interface - bind a driver to an interface
276 * @driver: the driver to be bound
277 * @iface: the interface to which it will be bound; must be in the
278 * usb device's active configuration
279 * @priv: driver data associated with that interface
280 *
281 * This is used by usb device drivers that need to claim more than one
282 * interface on a device when probing (audio and acm are current examples).
283 * No device driver should directly modify internal usb_interface or
284 * usb_device structure members.
285 *
286 * Few drivers should need to use this routine, since the most natural
287 * way to bind to an interface is to return the private data from
288 * the driver's probe() method.
289 *
Greg Kroah-Hartman341487a2007-04-09 11:52:31 -0400290 * Callers must own the device lock, so driver probe() entries don't need
291 * extra locking, but other call contexts may need to explicitly claim that
292 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400293 */
294int usb_driver_claim_interface(struct usb_driver *driver,
295 struct usb_interface *iface, void* priv)
296{
297 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400298 struct usb_device *udev = interface_to_usbdev(iface);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700299 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400300
301 if (dev->driver)
302 return -EBUSY;
303
Alan Stern8bb54ab2006-07-01 22:08:49 -0400304 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400305 usb_set_intfdata(iface, priv);
Alan Stern645daaa2006-08-30 15:47:02 -0400306
Alan Sterne0318eb2006-09-26 14:50:20 -0400307 usb_pm_lock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400308 iface->condition = USB_INTERFACE_BOUND;
309 mark_active(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400310 iface->pm_usage_cnt = !(driver->supports_autosuspend);
Alan Sterne0318eb2006-09-26 14:50:20 -0400311 usb_pm_unlock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400312
313 /* if interface was already added, bind now; else let
314 * the future device_add() bind it, bypassing probe()
315 */
316 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700317 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400318
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700319 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400320}
321EXPORT_SYMBOL(usb_driver_claim_interface);
322
323/**
324 * usb_driver_release_interface - unbind a driver from an interface
325 * @driver: the driver to be unbound
326 * @iface: the interface from which it will be unbound
327 *
328 * This can be used by drivers to release an interface without waiting
329 * for their disconnect() methods to be called. In typical cases this
330 * also causes the driver disconnect() method to be called.
331 *
332 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a2007-04-09 11:52:31 -0400333 * Callers must own the device lock, so driver disconnect() entries don't
334 * need extra locking, but other call contexts may need to explicitly claim
335 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400336 */
337void usb_driver_release_interface(struct usb_driver *driver,
338 struct usb_interface *iface)
339{
340 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400341 struct usb_device *udev = interface_to_usbdev(iface);
Alan Stern36e56a32006-07-01 22:08:06 -0400342
343 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400344 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400345 return;
346
347 /* don't release from within disconnect() */
348 if (iface->condition != USB_INTERFACE_BOUND)
349 return;
350
351 /* don't release if the interface hasn't been added yet */
352 if (device_is_registered(dev)) {
353 iface->condition = USB_INTERFACE_UNBINDING;
354 device_release_driver(dev);
355 }
356
357 dev->driver = NULL;
358 usb_set_intfdata(iface, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400359
Alan Sterne0318eb2006-09-26 14:50:20 -0400360 usb_pm_lock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400361 iface->condition = USB_INTERFACE_UNBOUND;
362 mark_quiesced(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400363 iface->needs_remote_wakeup = 0;
Alan Sterne0318eb2006-09-26 14:50:20 -0400364 usb_pm_unlock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400365}
366EXPORT_SYMBOL(usb_driver_release_interface);
367
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800368/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100369int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800370{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800371 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
372 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
373 return 0;
374
375 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
376 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
377 return 0;
378
379 /* No need to test id->bcdDevice_lo != 0, since 0 is never
380 greater than any unsigned number. */
381 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
382 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
383 return 0;
384
385 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
386 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
387 return 0;
388
389 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
390 (id->bDeviceClass != dev->descriptor.bDeviceClass))
391 return 0;
392
393 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
394 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
395 return 0;
396
397 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
398 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
399 return 0;
400
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100401 return 1;
402}
403
404/* returns 0 if no match, 1 if match */
405int usb_match_one_id(struct usb_interface *interface,
406 const struct usb_device_id *id)
407{
408 struct usb_host_interface *intf;
409 struct usb_device *dev;
410
411 /* proc_connectinfo in devio.c may call us with id == NULL. */
412 if (id == NULL)
413 return 0;
414
415 intf = interface->cur_altsetting;
416 dev = interface_to_usbdev(interface);
417
418 if (!usb_match_device(dev, id))
419 return 0;
420
Alan Stern93c8bf42006-10-18 16:41:51 -0400421 /* The interface class, subclass, and protocol should never be
422 * checked for a match if the device class is Vendor Specific,
423 * unless the match record specifies the Vendor ID. */
424 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
425 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
426 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
427 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
428 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
429 return 0;
430
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800431 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
432 (id->bInterfaceClass != intf->desc.bInterfaceClass))
433 return 0;
434
435 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
436 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
437 return 0;
438
439 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
440 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
441 return 0;
442
443 return 1;
444}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100445EXPORT_SYMBOL_GPL(usb_match_one_id);
446
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800447/**
448 * usb_match_id - find first usb_device_id matching device or interface
449 * @interface: the interface of interest
450 * @id: array of usb_device_id structures, terminated by zero entry
451 *
452 * usb_match_id searches an array of usb_device_id's and returns
453 * the first one matching the device or interface, or null.
454 * This is used when binding (or rebinding) a driver to an interface.
455 * Most USB device drivers will use this indirectly, through the usb core,
456 * but some layered driver frameworks use it directly.
457 * These device tables are exported with MODULE_DEVICE_TABLE, through
458 * modutils, to support the driver loading functionality of USB hotplugging.
459 *
460 * What Matches:
461 *
462 * The "match_flags" element in a usb_device_id controls which
463 * members are used. If the corresponding bit is set, the
464 * value in the device_id must match its corresponding member
465 * in the device or interface descriptor, or else the device_id
466 * does not match.
467 *
468 * "driver_info" is normally used only by device drivers,
469 * but you can create a wildcard "matches anything" usb_device_id
470 * as a driver's "modules.usbmap" entry if you provide an id with
471 * only a nonzero "driver_info" field. If you do this, the USB device
472 * driver's probe() routine should use additional intelligence to
473 * decide whether to bind to the specified interface.
474 *
475 * What Makes Good usb_device_id Tables:
476 *
477 * The match algorithm is very simple, so that intelligence in
478 * driver selection must come from smart driver id records.
479 * Unless you have good reasons to use another selection policy,
480 * provide match elements only in related groups, and order match
481 * specifiers from specific to general. Use the macros provided
482 * for that purpose if you can.
483 *
484 * The most specific match specifiers use device descriptor
485 * data. These are commonly used with product-specific matches;
486 * the USB_DEVICE macro lets you provide vendor and product IDs,
487 * and you can also match against ranges of product revisions.
488 * These are widely used for devices with application or vendor
489 * specific bDeviceClass values.
490 *
491 * Matches based on device class/subclass/protocol specifications
492 * are slightly more general; use the USB_DEVICE_INFO macro, or
493 * its siblings. These are used with single-function devices
494 * where bDeviceClass doesn't specify that each interface has
495 * its own class.
496 *
497 * Matches based on interface class/subclass/protocol are the
498 * most general; they let drivers bind to any interface on a
499 * multiple-function device. Use the USB_INTERFACE_INFO
500 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400501 * devices (as recorded in bInterfaceClass).
502 *
503 * Note that an entry created by USB_INTERFACE_INFO won't match
504 * any interface if the device class is set to Vendor-Specific.
505 * This is deliberate; according to the USB spec the meanings of
506 * the interface class/subclass/protocol for these devices are also
507 * vendor-specific, and hence matching against a standard product
508 * class wouldn't work anyway. If you really want to use an
509 * interface-based match for such a device, create a match record
510 * that also specifies the vendor ID. (Unforunately there isn't a
511 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800512 *
513 * Within those groups, remember that not all combinations are
514 * meaningful. For example, don't give a product version range
515 * without vendor and product IDs; or specify a protocol without
516 * its associated class and subclass.
517 */
518const struct usb_device_id *usb_match_id(struct usb_interface *interface,
519 const struct usb_device_id *id)
520{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800521 /* proc_connectinfo in devio.c may call us with id == NULL. */
522 if (id == NULL)
523 return NULL;
524
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800525 /* It is important to check that id->driver_info is nonzero,
526 since an entry that is all zeroes except for a nonzero
527 id->driver_info is the way to create an entry that
528 indicates that the driver want to examine every
529 device and interface. */
530 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
531 id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800532 if (usb_match_one_id(interface, id))
533 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800534 }
535
536 return NULL;
537}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800538EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800539
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100540static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800541{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400542 /* devices and interfaces are handled separately */
543 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800544
Alan Stern8bb54ab2006-07-01 22:08:49 -0400545 /* interface drivers never match devices */
546 if (!is_usb_device_driver(drv))
547 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800548
Alan Stern8bb54ab2006-07-01 22:08:49 -0400549 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800550 return 1;
551
Alan Stern8bb54ab2006-07-01 22:08:49 -0400552 } else {
553 struct usb_interface *intf;
554 struct usb_driver *usb_drv;
555 const struct usb_device_id *id;
556
557 /* device drivers never match interfaces */
558 if (is_usb_device_driver(drv))
559 return 0;
560
561 intf = to_usb_interface(dev);
562 usb_drv = to_usb_driver(drv);
563
564 id = usb_match_id(intf, usb_drv->id_table);
565 if (id)
566 return 1;
567
568 id = usb_match_dynamic_id(intf, usb_drv);
569 if (id)
570 return 1;
571 }
572
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800573 return 0;
574}
575
Alan Stern36e56a32006-07-01 22:08:06 -0400576#ifdef CONFIG_HOTPLUG
Alan Stern36e56a32006-07-01 22:08:06 -0400577static int usb_uevent(struct device *dev, char **envp, int num_envp,
578 char *buffer, int buffer_size)
579{
Alan Stern36e56a32006-07-01 22:08:06 -0400580 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400581 int i = 0;
582 int length = 0;
583
584 if (!dev)
585 return -ENODEV;
586
587 /* driver is often null here; dev_dbg() would oops */
588 pr_debug ("usb %s: uevent\n", dev->bus_id);
589
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100590 if (is_usb_device(dev))
Alan Stern782da722006-07-01 22:09:35 -0400591 usb_dev = to_usb_device(dev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100592 else {
593 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400594 usb_dev = interface_to_usbdev(intf);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400595 }
Alan Stern36e56a32006-07-01 22:08:06 -0400596
597 if (usb_dev->devnum < 0) {
598 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
599 return -ENODEV;
600 }
601 if (!usb_dev->bus) {
602 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
603 return -ENODEV;
604 }
605
606#ifdef CONFIG_USB_DEVICEFS
607 /* If this is available, userspace programs can directly read
608 * all the device descriptors we don't tell them about. Or
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100609 * act as usermode drivers.
Alan Stern36e56a32006-07-01 22:08:06 -0400610 */
611 if (add_uevent_var(envp, num_envp, &i,
612 buffer, buffer_size, &length,
613 "DEVICE=/proc/bus/usb/%03d/%03d",
614 usb_dev->bus->busnum, usb_dev->devnum))
615 return -ENOMEM;
616#endif
617
618 /* per-device configurations are common */
619 if (add_uevent_var(envp, num_envp, &i,
620 buffer, buffer_size, &length,
621 "PRODUCT=%x/%x/%x",
622 le16_to_cpu(usb_dev->descriptor.idVendor),
623 le16_to_cpu(usb_dev->descriptor.idProduct),
624 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
625 return -ENOMEM;
626
627 /* class-based driver binding models */
628 if (add_uevent_var(envp, num_envp, &i,
629 buffer, buffer_size, &length,
630 "TYPE=%d/%d/%d",
631 usb_dev->descriptor.bDeviceClass,
632 usb_dev->descriptor.bDeviceSubClass,
633 usb_dev->descriptor.bDeviceProtocol))
634 return -ENOMEM;
635
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100636 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400637 buffer, buffer_size, &length,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100638 "BUSNUM=%03d",
639 usb_dev->bus->busnum))
640 return -ENOMEM;
Alan Stern36e56a32006-07-01 22:08:06 -0400641
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100642 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400643 buffer, buffer_size, &length,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100644 "DEVNUM=%03d",
645 usb_dev->devnum))
646 return -ENOMEM;
Alan Stern36e56a32006-07-01 22:08:06 -0400647
648 envp[i] = NULL;
Alan Stern36e56a32006-07-01 22:08:06 -0400649 return 0;
650}
651
652#else
653
654static int usb_uevent(struct device *dev, char **envp,
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100655 int num_envp, char *buffer, int buffer_size)
Alan Stern36e56a32006-07-01 22:08:06 -0400656{
657 return -ENODEV;
658}
Alan Stern36e56a32006-07-01 22:08:06 -0400659#endif /* CONFIG_HOTPLUG */
660
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800661/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400662 * usb_register_device_driver - register a USB device (not interface) driver
663 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800664 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800665 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400666 * Registers a USB device driver with the USB core. The list of
667 * unattached devices will be rescanned whenever a new driver is
668 * added, allowing the new driver to attach to any recognized devices.
669 * Returns a negative error code on failure and 0 on success.
670 */
671int usb_register_device_driver(struct usb_device_driver *new_udriver,
672 struct module *owner)
673{
674 int retval = 0;
675
676 if (usb_disabled())
677 return -ENODEV;
678
679 new_udriver->drvwrap.for_devices = 1;
680 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
681 new_udriver->drvwrap.driver.bus = &usb_bus_type;
682 new_udriver->drvwrap.driver.probe = usb_probe_device;
683 new_udriver->drvwrap.driver.remove = usb_unbind_device;
684 new_udriver->drvwrap.driver.owner = owner;
685
686 retval = driver_register(&new_udriver->drvwrap.driver);
687
688 if (!retval) {
689 pr_info("%s: registered new device driver %s\n",
690 usbcore_name, new_udriver->name);
691 usbfs_update_special();
692 } else {
693 printk(KERN_ERR "%s: error %d registering device "
694 " driver %s\n",
695 usbcore_name, retval, new_udriver->name);
696 }
697
698 return retval;
699}
700EXPORT_SYMBOL_GPL(usb_register_device_driver);
701
702/**
703 * usb_deregister_device_driver - unregister a USB device (not interface) driver
704 * @udriver: USB operations of the device driver to unregister
705 * Context: must be able to sleep
706 *
707 * Unlinks the specified driver from the internal USB driver list.
708 */
709void usb_deregister_device_driver(struct usb_device_driver *udriver)
710{
711 pr_info("%s: deregistering device driver %s\n",
712 usbcore_name, udriver->name);
713
714 driver_unregister(&udriver->drvwrap.driver);
715 usbfs_update_special();
716}
717EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
718
719/**
720 * usb_register_driver - register a USB interface driver
721 * @new_driver: USB operations for the interface driver
722 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800723 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400724 *
725 * Registers a USB interface driver with the USB core. The list of
726 * unattached interfaces will be rescanned whenever a new driver is
727 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800728 * Returns a negative error code on failure and 0 on success.
729 *
730 * NOTE: if you want your driver to use the USB major number, you must call
731 * usb_register_dev() to enable that functionality. This function no longer
732 * takes care of that.
733 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800734int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
735 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800736{
737 int retval = 0;
738
739 if (usb_disabled())
740 return -ENODEV;
741
Alan Stern8bb54ab2006-07-01 22:08:49 -0400742 new_driver->drvwrap.for_devices = 0;
743 new_driver->drvwrap.driver.name = (char *) new_driver->name;
744 new_driver->drvwrap.driver.bus = &usb_bus_type;
745 new_driver->drvwrap.driver.probe = usb_probe_interface;
746 new_driver->drvwrap.driver.remove = usb_unbind_interface;
747 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800748 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800749 spin_lock_init(&new_driver->dynids.lock);
750 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800751
Alan Stern8bb54ab2006-07-01 22:08:49 -0400752 retval = driver_register(&new_driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800753
754 if (!retval) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400755 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800756 usbcore_name, new_driver->name);
757 usbfs_update_special();
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800758 usb_create_newid_file(new_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800759 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400760 printk(KERN_ERR "%s: error %d registering interface "
761 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800762 usbcore_name, retval, new_driver->name);
763 }
764
765 return retval;
766}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800767EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800768
769/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400770 * usb_deregister - unregister a USB interface driver
771 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800772 * Context: must be able to sleep
773 *
774 * Unlinks the specified driver from the internal USB driver list.
775 *
776 * NOTE: If you called usb_register_dev(), you still need to call
777 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
778 * this * call will no longer do it for you.
779 */
780void usb_deregister(struct usb_driver *driver)
781{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400782 pr_info("%s: deregistering interface driver %s\n",
783 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800784
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800785 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800786 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400787 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800788
789 usbfs_update_special();
790}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800791EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400792
793#ifdef CONFIG_PM
794
Alan Sterne0318eb2006-09-26 14:50:20 -0400795/* Caller has locked udev's pm_mutex */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -0800796static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -0400797{
Alan Stern782da722006-07-01 22:09:35 -0400798 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400799 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400800
Alan Stern114b3682006-07-01 22:13:04 -0400801 if (udev->state == USB_STATE_NOTATTACHED ||
802 udev->state == USB_STATE_SUSPENDED)
803 goto done;
804
Alan Stern1c5df7e2006-07-01 22:13:50 -0400805 /* For devices that don't have a driver, we do a standard suspend. */
806 if (udev->dev.driver == NULL) {
Alan Stern645daaa2006-08-30 15:47:02 -0400807 udev->do_remote_wakeup = 0;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400808 status = usb_port_suspend(udev);
Alan Stern2bf40862006-07-01 22:12:19 -0400809 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400810 }
811
Alan Stern1cc8a252006-07-01 22:10:15 -0400812 udriver = to_usb_device_driver(udev->dev.driver);
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 Stern114b3682006-07-01 22:13:04 -0400828 if (udev->state == USB_STATE_NOTATTACHED ||
829 udev->state != USB_STATE_SUSPENDED)
Alan Stern2bf40862006-07-01 22:12:19 -0400830 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400831
Alan Stern1c5df7e2006-07-01 22:13:50 -0400832 /* Can't resume it if it doesn't have a driver. */
833 if (udev->dev.driver == NULL) {
834 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400835 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400836 }
837
Alan Stern1cc8a252006-07-01 22:10:15 -0400838 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern2bf40862006-07-01 22:12:19 -0400839 status = udriver->resume(udev);
840
841done:
Alan Stern645daaa2006-08-30 15:47:02 -0400842 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2add5222007-03-20 14:59:39 -0400843 if (status == 0) {
844 udev->autoresume_disabled = 0;
Alan Stern2bf40862006-07-01 22:12:19 -0400845 udev->dev.power.power_state.event = PM_EVENT_ON;
Alan Stern2add5222007-03-20 14:59:39 -0400846 }
Alan Stern2bf40862006-07-01 22:12:19 -0400847 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400848}
849
Alan Sterne0318eb2006-09-26 14:50:20 -0400850/* Caller has locked intf's usb_device's pm mutex */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -0800851static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -0400852{
853 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400854 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400855
Alan Stern114b3682006-07-01 22:13:04 -0400856 /* with no hardware, USB interfaces only use FREEZE and ON states */
857 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
858 !is_active(intf))
859 goto done;
860
Alan Stern645daaa2006-08-30 15:47:02 -0400861 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
Alan Stern2bf40862006-07-01 22:12:19 -0400862 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400863 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400864
Alan Stern36e56a32006-07-01 22:08:06 -0400865 if (driver->suspend && driver->resume) {
Alan Stern1cc8a252006-07-01 22:10:15 -0400866 status = driver->suspend(intf, msg);
Alan Stern645daaa2006-08-30 15:47:02 -0400867 if (status == 0)
868 mark_quiesced(intf);
869 else if (!interface_to_usbdev(intf)->auto_pm)
Alan Stern1cc8a252006-07-01 22:10:15 -0400870 dev_err(&intf->dev, "%s error %d\n",
871 "suspend", status);
Alan Stern36e56a32006-07-01 22:08:06 -0400872 } else {
873 // FIXME else if there's no suspend method, disconnect...
Alan Stern645daaa2006-08-30 15:47:02 -0400874 // Not possible if auto_pm is set...
Alan Stern1cc8a252006-07-01 22:10:15 -0400875 dev_warn(&intf->dev, "no suspend for driver %s?\n",
876 driver->name);
Alan Stern36e56a32006-07-01 22:08:06 -0400877 mark_quiesced(intf);
Alan Stern36e56a32006-07-01 22:08:06 -0400878 }
Alan Stern2bf40862006-07-01 22:12:19 -0400879
880done:
Alan Stern645daaa2006-08-30 15:47:02 -0400881 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400882 if (status == 0)
883 intf->dev.power.power_state.event = msg.event;
Alan Stern36e56a32006-07-01 22:08:06 -0400884 return status;
885}
886
Alan Stern645daaa2006-08-30 15:47:02 -0400887/* Caller has locked intf's usb_device's pm_mutex */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -0800888static int usb_resume_interface(struct usb_interface *intf)
Alan Stern36e56a32006-07-01 22:08:06 -0400889{
Alan Stern1cc8a252006-07-01 22:10:15 -0400890 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400891 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400892
Alan Stern114b3682006-07-01 22:13:04 -0400893 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
894 is_active(intf))
Alan Stern2bf40862006-07-01 22:12:19 -0400895 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -0400896
Alan Stern645daaa2006-08-30 15:47:02 -0400897 /* Don't let autoresume interfere with unbinding */
898 if (intf->condition == USB_INTERFACE_UNBINDING)
899 goto done;
900
Alan Stern1c5df7e2006-07-01 22:13:50 -0400901 /* Can't resume it if it doesn't have a driver. */
Alan Stern645daaa2006-08-30 15:47:02 -0400902 if (intf->condition == USB_INTERFACE_UNBOUND) {
Alan Stern1c5df7e2006-07-01 22:13:50 -0400903 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400904 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400905 }
Alan Stern1cc8a252006-07-01 22:10:15 -0400906 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400907
Alan Stern36e56a32006-07-01 22:08:06 -0400908 if (driver->resume) {
909 status = driver->resume(intf);
Alan Stern2bf40862006-07-01 22:12:19 -0400910 if (status)
Alan Stern1cc8a252006-07-01 22:10:15 -0400911 dev_err(&intf->dev, "%s error %d\n",
912 "resume", status);
Alan Stern2bf40862006-07-01 22:12:19 -0400913 else
914 mark_active(intf);
915 } else {
Alan Stern1cc8a252006-07-01 22:10:15 -0400916 dev_warn(&intf->dev, "no resume for driver %s?\n",
917 driver->name);
Alan Stern2bf40862006-07-01 22:12:19 -0400918 mark_active(intf);
919 }
920
921done:
Alan Stern645daaa2006-08-30 15:47:02 -0400922 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400923 if (status == 0)
924 intf->dev.power.power_state.event = PM_EVENT_ON;
925 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;
935
Alan Sternb5e795f2007-02-20 15:00:53 -0500936 /* For autosuspend, fail fast if anything is in use or autosuspend
937 * is disabled. Also fail if any interfaces require remote wakeup
938 * but it isn't available.
939 */
Alan Sternaf4f7602006-10-30 17:06:45 -0500940 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
941 if (udev->pm_usage_cnt > 0)
942 return -EBUSY;
Alan Stern2add5222007-03-20 14:59:39 -0400943 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
Alan Sternb5e795f2007-02-20 15:00:53 -0500944 return -EPERM;
945
Alan Sternaf4f7602006-10-30 17:06:45 -0500946 if (udev->actconfig) {
947 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
948 intf = udev->actconfig->interface[i];
949 if (!is_active(intf))
950 continue;
951 if (intf->pm_usage_cnt > 0)
952 return -EBUSY;
953 if (intf->needs_remote_wakeup &&
954 !udev->do_remote_wakeup) {
955 dev_dbg(&udev->dev, "remote wakeup needed "
956 "for autosuspend\n");
957 return -EOPNOTSUPP;
958 }
959 }
960 }
961 return 0;
962}
963
Alan Stern94fcda12006-11-20 11:38:46 -0500964#else
965
966#define autosuspend_check(udev) 0
967
Alan Sternb5e795f2007-02-20 15:00:53 -0500968#endif /* CONFIG_USB_SUSPEND */
Alan Stern94fcda12006-11-20 11:38:46 -0500969
Alan Stern645daaa2006-08-30 15:47:02 -0400970/**
971 * usb_suspend_both - suspend a USB device and its interfaces
972 * @udev: the usb_device to suspend
973 * @msg: Power Management message describing this state transition
974 *
975 * This is the central routine for suspending USB devices. It calls the
976 * suspend methods for all the interface drivers in @udev and then calls
977 * the suspend method for @udev itself. If an error occurs at any stage,
978 * all the interfaces which were suspended are resumed so that they remain
979 * in the same state as the device.
980 *
981 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
982 * checks first to make sure that neither the device itself or any of its
983 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
984 * are, the autosuspend fails.
985 *
986 * If the suspend succeeds, the routine recursively queues an autosuspend
987 * request for @udev's parent device, thereby propagating the change up
988 * the device tree. If all of the parent's children are now suspended,
989 * the parent will autosuspend in turn.
990 *
991 * The suspend method calls are subject to mutual exclusion under control
992 * of @udev's pm_mutex. Many of these calls are also under the protection
993 * of @udev's device lock (including all requests originating outside the
994 * USB subsystem), but autosuspend requests generated by a child device or
995 * interface driver may not be. Usbcore will insure that the method calls
996 * do not arrive during bind, unbind, or reset operations. However, drivers
997 * must be prepared to handle suspend calls arriving at unpredictable times.
998 * The only way to block such calls is to do an autoresume (preventing
999 * autosuspends) while holding @udev's device lock (preventing outside
1000 * suspends).
1001 *
1002 * The caller must hold @udev->pm_mutex.
1003 *
1004 * This routine can run only in process context.
1005 */
Alan Stern718efa62007-03-09 15:41:13 -05001006static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001007{
1008 int status = 0;
1009 int i = 0;
1010 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001011 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001012
Alan Stern645daaa2006-08-30 15:47:02 -04001013 cancel_delayed_work(&udev->autosuspend);
1014 if (udev->state == USB_STATE_NOTATTACHED)
1015 return 0;
1016 if (udev->state == USB_STATE_SUSPENDED)
1017 return 0;
1018
1019 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1020
Alan Stern645daaa2006-08-30 15:47:02 -04001021 if (udev->auto_pm) {
Alan Sternaf4f7602006-10-30 17:06:45 -05001022 status = autosuspend_check(udev);
1023 if (status < 0)
1024 return status;
Alan Stern645daaa2006-08-30 15:47:02 -04001025 }
1026
1027 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001028 if (udev->actconfig) {
1029 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1030 intf = udev->actconfig->interface[i];
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001031 status = usb_suspend_interface(intf, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001032 if (status != 0)
1033 break;
1034 }
1035 }
1036 if (status == 0)
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001037 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001038
1039 /* If the suspend failed, resume interfaces that did get suspended */
1040 if (status != 0) {
1041 while (--i >= 0) {
1042 intf = udev->actconfig->interface[i];
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001043 usb_resume_interface(intf);
Alan Sterna8e7c562006-07-01 22:11:02 -04001044 }
Alan Stern645daaa2006-08-30 15:47:02 -04001045
1046 /* If the suspend succeeded, propagate it up the tree */
1047 } else if (parent)
Alan Stern94fcda12006-11-20 11:38:46 -05001048 usb_autosuspend_device(parent);
Alan Stern645daaa2006-08-30 15:47:02 -04001049
1050 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001051 return status;
1052}
1053
Alan Stern645daaa2006-08-30 15:47:02 -04001054/**
1055 * usb_resume_both - resume a USB device and its interfaces
1056 * @udev: the usb_device to resume
1057 *
1058 * This is the central routine for resuming USB devices. It calls the
1059 * the resume method for @udev and then calls the resume methods for all
1060 * the interface drivers in @udev.
1061 *
1062 * Before starting the resume, the routine calls itself recursively for
1063 * the parent device of @udev, thereby propagating the change up the device
1064 * tree and assuring that @udev will be able to resume. If the parent is
1065 * unable to resume successfully, the routine fails.
1066 *
1067 * The resume method calls are subject to mutual exclusion under control
1068 * of @udev's pm_mutex. Many of these calls are also under the protection
1069 * of @udev's device lock (including all requests originating outside the
1070 * USB subsystem), but autoresume requests generated by a child device or
1071 * interface driver may not be. Usbcore will insure that the method calls
1072 * do not arrive during bind, unbind, or reset operations. However, drivers
1073 * must be prepared to handle resume calls arriving at unpredictable times.
1074 * The only way to block such calls is to do an autoresume (preventing
1075 * other autoresumes) while holding @udev's device lock (preventing outside
1076 * resumes).
1077 *
1078 * The caller must hold @udev->pm_mutex.
1079 *
1080 * This routine can run only in process context.
1081 */
Alan Stern718efa62007-03-09 15:41:13 -05001082static int usb_resume_both(struct usb_device *udev)
Alan Sterna8e7c562006-07-01 22:11:02 -04001083{
Alan Stern645daaa2006-08-30 15:47:02 -04001084 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001085 int i;
1086 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001087 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001088
Alan Stern2add5222007-03-20 14:59:39 -04001089 if (udev->auto_pm && udev->autoresume_disabled)
1090 return -EPERM;
Alan Stern645daaa2006-08-30 15:47:02 -04001091 cancel_delayed_work(&udev->autosuspend);
1092 if (udev->state == USB_STATE_NOTATTACHED)
1093 return -ENODEV;
1094
1095 /* Propagate the resume up the tree, if necessary */
1096 if (udev->state == USB_STATE_SUSPENDED) {
1097 if (parent) {
Alan Stern94fcda12006-11-20 11:38:46 -05001098 status = usb_autoresume_device(parent);
Alan Sternee49fb52006-11-22 16:55:54 -05001099 if (status == 0) {
1100 status = usb_resume_device(udev);
1101 if (status) {
Alan Stern94fcda12006-11-20 11:38:46 -05001102 usb_autosuspend_device(parent);
Alan Sternee49fb52006-11-22 16:55:54 -05001103
1104 /* It's possible usb_resume_device()
1105 * failed after the port was
1106 * unsuspended, causing udev to be
1107 * logically disconnected. We don't
1108 * want usb_disconnect() to autosuspend
1109 * the parent again, so tell it that
1110 * udev disconnected while still
1111 * suspended. */
1112 if (udev->state ==
1113 USB_STATE_NOTATTACHED)
1114 udev->discon_suspended = 1;
1115 }
1116 }
Alan Stern645daaa2006-08-30 15:47:02 -04001117 } else {
1118
1119 /* We can't progagate beyond the USB subsystem,
1120 * so if a root hub's controller is suspended
1121 * then we're stuck. */
1122 if (udev->dev.parent->power.power_state.event !=
1123 PM_EVENT_ON)
1124 status = -EHOSTUNREACH;
Alan Sternee49fb52006-11-22 16:55:54 -05001125 else
1126 status = usb_resume_device(udev);
1127 }
Alan Stern592fbbe2006-09-19 10:08:43 -04001128 } else {
1129
1130 /* Needed only for setting udev->dev.power.power_state.event
1131 * and for possible debugging message. */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001132 status = usb_resume_device(udev);
Alan Stern114b3682006-07-01 22:13:04 -04001133 }
1134
Alan Sterna8e7c562006-07-01 22:11:02 -04001135 if (status == 0 && udev->actconfig) {
1136 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1137 intf = udev->actconfig->interface[i];
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001138 usb_resume_interface(intf);
Alan Sterna8e7c562006-07-01 22:11:02 -04001139 }
1140 }
Alan Stern645daaa2006-08-30 15:47:02 -04001141
1142 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001143 return status;
1144}
1145
Alan Stern645daaa2006-08-30 15:47:02 -04001146#ifdef CONFIG_USB_SUSPEND
1147
Alan Stern718efa62007-03-09 15:41:13 -05001148/* usb_autosuspend_work - callback routine to autosuspend a USB device */
1149void usb_autosuspend_work(struct work_struct *work)
1150{
1151 struct usb_device *udev =
1152 container_of(work, struct usb_device, autosuspend.work);
1153
1154 usb_pm_lock(udev);
1155 udev->auto_pm = 1;
1156 usb_suspend_both(udev, PMSG_SUSPEND);
1157 usb_pm_unlock(udev);
1158}
1159
Alan Stern94fcda12006-11-20 11:38:46 -05001160/* Internal routine to adjust a device's usage counter and change
1161 * its autosuspend state.
1162 */
1163static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1164{
1165 int status = 0;
1166
1167 usb_pm_lock(udev);
1168 udev->pm_usage_cnt += inc_usage_cnt;
1169 WARN_ON(udev->pm_usage_cnt < 0);
1170 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1171 udev->auto_pm = 1;
1172 status = usb_resume_both(udev);
1173 if (status != 0)
1174 udev->pm_usage_cnt -= inc_usage_cnt;
1175 } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0)
1176 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
Alan Sternb5e795f2007-02-20 15:00:53 -05001177 udev->autosuspend_delay);
Alan Stern94fcda12006-11-20 11:38:46 -05001178 usb_pm_unlock(udev);
1179 return status;
1180}
1181
Alan Stern645daaa2006-08-30 15:47:02 -04001182/**
1183 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001184 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001185 *
1186 * This routine should be called when a core subsystem is finished using
1187 * @udev and wants to allow it to autosuspend. Examples would be when
1188 * @udev's device file in usbfs is closed or after a configuration change.
1189 *
Alan Stern94fcda12006-11-20 11:38:46 -05001190 * @udev's usage counter is decremented. If it or any of the usage counters
1191 * for an active interface is greater than 0, no autosuspend request will be
1192 * queued. (If an interface driver does not support autosuspend then its
1193 * usage counter is permanently positive.) Furthermore, if an interface
1194 * driver requires remote-wakeup capability during autosuspend but remote
1195 * wakeup is disabled, the autosuspend will fail.
Alan Stern645daaa2006-08-30 15:47:02 -04001196 *
1197 * Often the caller will hold @udev's device lock, but this is not
1198 * necessary.
1199 *
1200 * This routine can run only in process context.
1201 */
Alan Stern94fcda12006-11-20 11:38:46 -05001202void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001203{
Alan Stern94fcda12006-11-20 11:38:46 -05001204 int status;
1205
1206 status = usb_autopm_do_device(udev, -1);
Alan Stern645daaa2006-08-30 15:47:02 -04001207 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1208 // __FUNCTION__, udev->pm_usage_cnt);
1209}
1210
1211/**
Alan Stern19c26232007-02-20 15:03:32 -05001212 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1213 * @udev: the usb_device to autosuspend
1214 *
1215 * This routine should be called when a core subsystem thinks @udev may
1216 * be ready to autosuspend.
1217 *
1218 * @udev's usage counter left unchanged. If it or any of the usage counters
1219 * for an active interface is greater than 0, or autosuspend is not allowed
1220 * for any other reason, no autosuspend request will be queued.
1221 *
1222 * This routine can run only in process context.
1223 */
1224void usb_try_autosuspend_device(struct usb_device *udev)
1225{
1226 usb_autopm_do_device(udev, 0);
1227 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1228 // __FUNCTION__, udev->pm_usage_cnt);
1229}
1230
1231/**
Alan Stern645daaa2006-08-30 15:47:02 -04001232 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001233 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001234 *
1235 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001236 * and needs to guarantee that it is not suspended. No autosuspend will
1237 * occur until usb_autosuspend_device is called. (Note that this will not
1238 * prevent suspend events originating in the PM core.) Examples would be
1239 * when @udev's device file in usbfs is opened or when a remote-wakeup
1240 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001241 *
Alan Stern94fcda12006-11-20 11:38:46 -05001242 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1243 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001244 *
1245 * Often the caller will hold @udev's device lock, but this is not
1246 * necessary (and attempting it might cause deadlock).
1247 *
1248 * This routine can run only in process context.
1249 */
Alan Stern94fcda12006-11-20 11:38:46 -05001250int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001251{
1252 int status;
1253
Alan Stern94fcda12006-11-20 11:38:46 -05001254 status = usb_autopm_do_device(udev, 1);
Alan Stern645daaa2006-08-30 15:47:02 -04001255 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1256 // __FUNCTION__, status, udev->pm_usage_cnt);
1257 return status;
1258}
1259
Alan Sternaf4f7602006-10-30 17:06:45 -05001260/* Internal routine to adjust an interface's usage counter and change
1261 * its device's autosuspend state.
1262 */
1263static int usb_autopm_do_interface(struct usb_interface *intf,
1264 int inc_usage_cnt)
1265{
1266 struct usb_device *udev = interface_to_usbdev(intf);
1267 int status = 0;
1268
1269 usb_pm_lock(udev);
1270 if (intf->condition == USB_INTERFACE_UNBOUND)
1271 status = -ENODEV;
1272 else {
1273 intf->pm_usage_cnt += inc_usage_cnt;
1274 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
1275 udev->auto_pm = 1;
1276 status = usb_resume_both(udev);
1277 if (status != 0)
1278 intf->pm_usage_cnt -= inc_usage_cnt;
1279 } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0)
1280 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
Alan Sternb5e795f2007-02-20 15:00:53 -05001281 udev->autosuspend_delay);
Alan Sternaf4f7602006-10-30 17:06:45 -05001282 }
1283 usb_pm_unlock(udev);
1284 return status;
1285}
1286
Alan Stern645daaa2006-08-30 15:47:02 -04001287/**
1288 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001289 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001290 *
1291 * This routine should be called by an interface driver when it is
1292 * finished using @intf and wants to allow it to autosuspend. A typical
1293 * example would be a character-device driver when its device file is
1294 * closed.
1295 *
1296 * The routine decrements @intf's usage counter. When the counter reaches
1297 * 0, a delayed autosuspend request for @intf's device is queued. When
1298 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1299 * the other usage counters for the sibling interfaces and @intf's
1300 * usb_device, the device and all its interfaces will be autosuspended.
1301 *
1302 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1303 * core will not change its value other than the increment and decrement
1304 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1305 * may use this simple counter-oriented discipline or may set the value
1306 * any way it likes.
1307 *
1308 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1309 * take place only if the device's remote-wakeup facility is enabled.
1310 *
1311 * Suspend method calls queued by this routine can arrive at any time
1312 * while @intf is resumed and its usage counter is equal to 0. They are
1313 * not protected by the usb_device's lock but only by its pm_mutex.
1314 * Drivers must provide their own synchronization.
1315 *
1316 * This routine can run only in process context.
1317 */
1318void usb_autopm_put_interface(struct usb_interface *intf)
1319{
Alan Sternaf4f7602006-10-30 17:06:45 -05001320 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001321
Alan Sternaf4f7602006-10-30 17:06:45 -05001322 status = usb_autopm_do_interface(intf, -1);
1323 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1324 // __FUNCTION__, status, intf->pm_usage_cnt);
Alan Stern645daaa2006-08-30 15:47:02 -04001325}
1326EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1327
1328/**
1329 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001330 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001331 *
1332 * This routine should be called by an interface driver when it wants to
1333 * use @intf and needs to guarantee that it is not suspended. In addition,
1334 * the routine prevents @intf from being autosuspended subsequently. (Note
1335 * that this will not prevent suspend events originating in the PM core.)
1336 * This prevention will persist until usb_autopm_put_interface() is called
1337 * or @intf is unbound. A typical example would be a character-device
1338 * driver when its device file is opened.
1339 *
1340 * The routine increments @intf's usage counter. So long as the counter
1341 * is greater than 0, autosuspend will not be allowed for @intf or its
1342 * usb_device. When the driver is finished using @intf it should call
1343 * usb_autopm_put_interface() to decrement the usage counter and queue
1344 * a delayed autosuspend request (if the counter is <= 0).
1345 *
1346 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1347 * core will not change its value other than the increment and decrement
1348 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1349 * may use this simple counter-oriented discipline or may set the value
1350 * any way it likes.
1351 *
1352 * Resume method calls generated by this routine can arrive at any time
1353 * while @intf is suspended. They are not protected by the usb_device's
1354 * lock but only by its pm_mutex. Drivers must provide their own
1355 * synchronization.
1356 *
1357 * This routine can run only in process context.
1358 */
1359int usb_autopm_get_interface(struct usb_interface *intf)
1360{
Alan Sternaf4f7602006-10-30 17:06:45 -05001361 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001362
Alan Sternaf4f7602006-10-30 17:06:45 -05001363 status = usb_autopm_do_interface(intf, 1);
Alan Stern645daaa2006-08-30 15:47:02 -04001364 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1365 // __FUNCTION__, status, intf->pm_usage_cnt);
1366 return status;
1367}
1368EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1369
Alan Stern692a1862006-10-30 17:07:51 -05001370/**
1371 * usb_autopm_set_interface - set a USB interface's autosuspend state
1372 * @intf: the usb_interface whose state should be set
1373 *
1374 * This routine sets the autosuspend state of @intf's device according
1375 * to @intf's usage counter, which the caller must have set previously.
1376 * If the counter is <= 0, the device is autosuspended (if it isn't
1377 * already suspended and if nothing else prevents the autosuspend). If
1378 * the counter is > 0, the device is autoresumed (if it isn't already
1379 * awake).
1380 */
1381int usb_autopm_set_interface(struct usb_interface *intf)
1382{
1383 int status;
1384
1385 status = usb_autopm_do_interface(intf, 0);
1386 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1387 // __FUNCTION__, status, intf->pm_usage_cnt);
1388 return status;
1389}
1390EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1391
Alan Stern718efa62007-03-09 15:41:13 -05001392#else
1393
1394void usb_autosuspend_work(struct work_struct *work)
1395{}
1396
Alan Stern645daaa2006-08-30 15:47:02 -04001397#endif /* CONFIG_USB_SUSPEND */
1398
Alan Stern6b157c92007-03-13 16:37:30 -04001399/**
1400 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1401 * @udev: the usb_device to suspend
1402 * @msg: Power Management message describing this state transition
1403 *
1404 * This routine handles external suspend requests: ones not generated
1405 * internally by a USB driver (autosuspend) but rather coming from the user
1406 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1407 * out regardless of @udev's usage counter or those of its interfaces,
1408 * and regardless of whether or not remote wakeup is enabled. Of course,
1409 * interface drivers still have the option of failing the suspend (if
1410 * there are unsuspended children, for example).
1411 *
1412 * The caller must hold @udev's device lock.
1413 */
1414int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001415{
1416 int status;
1417
Alan Stern6b157c92007-03-13 16:37:30 -04001418 usb_pm_lock(udev);
1419 udev->auto_pm = 0;
1420 status = usb_suspend_both(udev, msg);
1421 usb_pm_unlock(udev);
Alan Stern1cc8a252006-07-01 22:10:15 -04001422 return status;
1423}
1424
Alan Stern6b157c92007-03-13 16:37:30 -04001425/**
1426 * usb_external_resume_device - external resume of a USB device and its interfaces
1427 * @udev: the usb_device to resume
1428 *
1429 * This routine handles external resume requests: ones not generated
1430 * internally by a USB driver (autoresume) but rather coming from the user
1431 * (via sysfs), the PM core (system resume), or the device itself (remote
1432 * wakeup). @udev's usage counter is unaffected.
1433 *
1434 * The caller must hold @udev's device lock.
1435 */
1436int usb_external_resume_device(struct usb_device *udev)
1437{
1438 int status;
1439
1440 usb_pm_lock(udev);
1441 udev->auto_pm = 0;
1442 status = usb_resume_both(udev);
1443 usb_pm_unlock(udev);
1444
1445 /* Now that the device is awake, we can start trying to autosuspend
1446 * it again. */
1447 if (status == 0)
1448 usb_try_autosuspend_device(udev);
1449 return status;
1450}
1451
1452static int usb_suspend(struct device *dev, pm_message_t message)
1453{
1454 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1455 return 0;
1456 return usb_external_suspend_device(to_usb_device(dev), message);
1457}
1458
Alan Stern1cc8a252006-07-01 22:10:15 -04001459static int usb_resume(struct device *dev)
1460{
Alan Stern2add5222007-03-20 14:59:39 -04001461 struct usb_device *udev;
1462
Alan Stern6b157c92007-03-13 16:37:30 -04001463 if (!is_usb_device(dev)) /* Ignore PM for interfaces */
1464 return 0;
Alan Stern2add5222007-03-20 14:59:39 -04001465 udev = to_usb_device(dev);
1466 if (udev->autoresume_disabled)
1467 return -EPERM;
1468 return usb_external_resume_device(udev);
Alan Stern1cc8a252006-07-01 22:10:15 -04001469}
1470
Alan Stern6b157c92007-03-13 16:37:30 -04001471#else
1472
1473#define usb_suspend NULL
1474#define usb_resume NULL
1475
Alan Stern36e56a32006-07-01 22:08:06 -04001476#endif /* CONFIG_PM */
1477
1478struct bus_type usb_bus_type = {
1479 .name = "usb",
1480 .match = usb_device_match,
1481 .uevent = usb_uevent,
Alan Stern782da722006-07-01 22:09:35 -04001482 .suspend = usb_suspend,
1483 .resume = usb_resume,
Alan Stern36e56a32006-07-01 22:08:06 -04001484};