blob: ee18d187ca17dce97133fbe08ccdde246eb1b52d [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 -080031static int usb_match_one_id(struct usb_interface *interface,
32 const struct usb_device_id *id);
33
34struct usb_dynid {
35 struct list_head node;
36 struct usb_device_id id;
37};
38
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080039#ifdef CONFIG_HOTPLUG
40
41/*
42 * Adds a new dynamic USBdevice ID to this driver,
43 * and cause the driver to probe for all devices again.
44 */
45static ssize_t store_new_id(struct device_driver *driver,
46 const char *buf, size_t count)
47{
48 struct usb_driver *usb_drv = to_usb_driver(driver);
49 struct usb_dynid *dynid;
50 u32 idVendor = 0;
51 u32 idProduct = 0;
52 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070053 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080054
55 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
56 if (fields < 2)
57 return -EINVAL;
58
59 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
60 if (!dynid)
61 return -ENOMEM;
62
63 INIT_LIST_HEAD(&dynid->node);
64 dynid->id.idVendor = idVendor;
65 dynid->id.idProduct = idProduct;
66 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
67
68 spin_lock(&usb_drv->dynids.lock);
69 list_add_tail(&usb_drv->dynids.list, &dynid->node);
70 spin_unlock(&usb_drv->dynids.lock);
71
72 if (get_driver(driver)) {
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070073 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080074 put_driver(driver);
75 }
76
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070077 if (retval)
78 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080079 return count;
80}
81static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
82
83static int usb_create_newid_file(struct usb_driver *usb_drv)
84{
85 int error = 0;
86
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080087 if (usb_drv->no_dynamic_id)
88 goto exit;
89
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080090 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -040091 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080092 &driver_attr_new_id.attr);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080093exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080094 return error;
95}
96
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080097static void usb_remove_newid_file(struct usb_driver *usb_drv)
98{
99 if (usb_drv->no_dynamic_id)
100 return;
101
102 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400103 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800104 &driver_attr_new_id.attr);
105}
106
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800107static void usb_free_dynids(struct usb_driver *usb_drv)
108{
109 struct usb_dynid *dynid, *n;
110
111 spin_lock(&usb_drv->dynids.lock);
112 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
113 list_del(&dynid->node);
114 kfree(dynid);
115 }
116 spin_unlock(&usb_drv->dynids.lock);
117}
118#else
119static inline int usb_create_newid_file(struct usb_driver *usb_drv)
120{
121 return 0;
122}
123
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800124static void usb_remove_newid_file(struct usb_driver *usb_drv)
125{
126}
127
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800128static inline void usb_free_dynids(struct usb_driver *usb_drv)
129{
130}
131#endif
132
133static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
134 struct usb_driver *drv)
135{
136 struct usb_dynid *dynid;
137
138 spin_lock(&drv->dynids.lock);
139 list_for_each_entry(dynid, &drv->dynids.list, node) {
140 if (usb_match_one_id(intf, &dynid->id)) {
141 spin_unlock(&drv->dynids.lock);
142 return &dynid->id;
143 }
144 }
145 spin_unlock(&drv->dynids.lock);
146 return NULL;
147}
148
149
Alan Stern8bb54ab2006-07-01 22:08:49 -0400150/* called from driver core with dev locked */
151static int usb_probe_device(struct device *dev)
152{
153 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
154 struct usb_device *udev;
155 int error = -ENODEV;
156
157 dev_dbg(dev, "%s\n", __FUNCTION__);
158
159 if (!is_usb_device(dev)) /* Sanity check */
160 return error;
161
162 udev = to_usb_device(dev);
163
Alan Stern8bb54ab2006-07-01 22:08:49 -0400164 /* TODO: Add real matching code */
165
Alan Stern645daaa2006-08-30 15:47:02 -0400166 /* The device should always appear to be in use
167 * unless the driver suports autosuspend.
168 */
169 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
170
Alan Stern8bb54ab2006-07-01 22:08:49 -0400171 error = udriver->probe(udev);
172 return error;
173}
174
175/* called from driver core with dev locked */
176static int usb_unbind_device(struct device *dev)
177{
178 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
179
180 udriver->disconnect(to_usb_device(dev));
181 return 0;
182}
183
184
185/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800186static int usb_probe_interface(struct device *dev)
187{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400188 struct usb_driver *driver = to_usb_driver(dev->driver);
189 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400190 struct usb_device *udev;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800191 const struct usb_device_id *id;
192 int error = -ENODEV;
193
194 dev_dbg(dev, "%s\n", __FUNCTION__);
195
Alan Stern8bb54ab2006-07-01 22:08:49 -0400196 if (is_usb_device(dev)) /* Sanity check */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800197 return error;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400198
199 intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400200 udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800201
202 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800203 if (!id)
204 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800205 if (id) {
206 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
207
Alan Stern645daaa2006-08-30 15:47:02 -0400208 error = usb_autoresume_device(udev, 1);
209 if (error)
210 return error;
211
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800212 /* Interface "power state" doesn't correspond to any hardware
213 * state whatsoever. We use it to record when it's bound to
214 * a driver that may start I/0: it's not frozen/quiesced.
215 */
216 mark_active(intf);
217 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400218
219 /* The interface should always appear to be in use
220 * unless the driver suports autosuspend.
221 */
222 intf->pm_usage_cnt = !(driver->supports_autosuspend);
223
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800224 error = driver->probe(intf, id);
225 if (error) {
226 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400227 intf->needs_remote_wakeup = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800228 intf->condition = USB_INTERFACE_UNBOUND;
229 } else
230 intf->condition = USB_INTERFACE_BOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400231
232 usb_autosuspend_device(udev, 1);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800233 }
234
235 return error;
236}
237
Alan Stern8bb54ab2006-07-01 22:08:49 -0400238/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800239static int usb_unbind_interface(struct device *dev)
240{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400241 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800242 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400243 struct usb_device *udev;
244 int error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800245
246 intf->condition = USB_INTERFACE_UNBINDING;
247
Alan Stern645daaa2006-08-30 15:47:02 -0400248 /* Autoresume for set_interface call below */
249 udev = interface_to_usbdev(intf);
250 error = usb_autoresume_device(udev, 1);
251
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800252 /* release all urbs for this interface */
253 usb_disable_interface(interface_to_usbdev(intf), intf);
254
Alan Stern8bb54ab2006-07-01 22:08:49 -0400255 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800256
257 /* reset other interface state */
258 usb_set_interface(interface_to_usbdev(intf),
259 intf->altsetting[0].desc.bInterfaceNumber,
260 0);
261 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400262
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800263 intf->condition = USB_INTERFACE_UNBOUND;
264 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400265 intf->needs_remote_wakeup = 0;
266
267 if (!error)
268 usb_autosuspend_device(udev, 1);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800269
270 return 0;
271}
272
Alan Stern36e56a32006-07-01 22:08:06 -0400273/**
274 * usb_driver_claim_interface - bind a driver to an interface
275 * @driver: the driver to be bound
276 * @iface: the interface to which it will be bound; must be in the
277 * usb device's active configuration
278 * @priv: driver data associated with that interface
279 *
280 * This is used by usb device drivers that need to claim more than one
281 * interface on a device when probing (audio and acm are current examples).
282 * No device driver should directly modify internal usb_interface or
283 * usb_device structure members.
284 *
285 * Few drivers should need to use this routine, since the most natural
286 * way to bind to an interface is to return the private data from
287 * the driver's probe() method.
288 *
289 * Callers must own the device lock and the driver model's usb_bus_type.subsys
290 * writelock. So driver probe() entries don't need extra locking,
291 * but other call contexts may need to explicitly claim those locks.
292 */
293int usb_driver_claim_interface(struct usb_driver *driver,
294 struct usb_interface *iface, void* priv)
295{
296 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400297 struct usb_device *udev = interface_to_usbdev(iface);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700298 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400299
300 if (dev->driver)
301 return -EBUSY;
302
Alan Stern8bb54ab2006-07-01 22:08:49 -0400303 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400304 usb_set_intfdata(iface, priv);
Alan Stern645daaa2006-08-30 15:47:02 -0400305
306 mutex_lock_nested(&udev->pm_mutex, udev->level);
Alan Stern36e56a32006-07-01 22:08:06 -0400307 iface->condition = USB_INTERFACE_BOUND;
308 mark_active(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400309 iface->pm_usage_cnt = !(driver->supports_autosuspend);
310 mutex_unlock(&udev->pm_mutex);
Alan Stern36e56a32006-07-01 22:08:06 -0400311
312 /* if interface was already added, bind now; else let
313 * the future device_add() bind it, bypassing probe()
314 */
315 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700316 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400317
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700318 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400319}
320EXPORT_SYMBOL(usb_driver_claim_interface);
321
322/**
323 * usb_driver_release_interface - unbind a driver from an interface
324 * @driver: the driver to be unbound
325 * @iface: the interface from which it will be unbound
326 *
327 * This can be used by drivers to release an interface without waiting
328 * for their disconnect() methods to be called. In typical cases this
329 * also causes the driver disconnect() method to be called.
330 *
331 * This call is synchronous, and may not be used in an interrupt context.
332 * Callers must own the device lock and the driver model's usb_bus_type.subsys
333 * writelock. So driver disconnect() entries don't need extra locking,
334 * but other call contexts may need to explicitly claim those locks.
335 */
336void usb_driver_release_interface(struct usb_driver *driver,
337 struct usb_interface *iface)
338{
339 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400340 struct usb_device *udev = interface_to_usbdev(iface);
Alan Stern36e56a32006-07-01 22:08:06 -0400341
342 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400343 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400344 return;
345
346 /* don't release from within disconnect() */
347 if (iface->condition != USB_INTERFACE_BOUND)
348 return;
349
350 /* don't release if the interface hasn't been added yet */
351 if (device_is_registered(dev)) {
352 iface->condition = USB_INTERFACE_UNBINDING;
353 device_release_driver(dev);
354 }
355
356 dev->driver = NULL;
357 usb_set_intfdata(iface, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400358
359 mutex_lock_nested(&udev->pm_mutex, udev->level);
Alan Stern36e56a32006-07-01 22:08:06 -0400360 iface->condition = USB_INTERFACE_UNBOUND;
361 mark_quiesced(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400362 iface->needs_remote_wakeup = 0;
363 mutex_unlock(&udev->pm_mutex);
Alan Stern36e56a32006-07-01 22:08:06 -0400364}
365EXPORT_SYMBOL(usb_driver_release_interface);
366
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800367/* returns 0 if no match, 1 if match */
368static int usb_match_one_id(struct usb_interface *interface,
369 const struct usb_device_id *id)
370{
371 struct usb_host_interface *intf;
372 struct usb_device *dev;
373
374 /* proc_connectinfo in devio.c may call us with id == NULL. */
375 if (id == NULL)
376 return 0;
377
378 intf = interface->cur_altsetting;
379 dev = interface_to_usbdev(interface);
380
381 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
382 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
383 return 0;
384
385 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
386 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
387 return 0;
388
389 /* No need to test id->bcdDevice_lo != 0, since 0 is never
390 greater than any unsigned number. */
391 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
392 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
393 return 0;
394
395 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
396 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
397 return 0;
398
399 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
400 (id->bDeviceClass != dev->descriptor.bDeviceClass))
401 return 0;
402
403 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
404 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
405 return 0;
406
407 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
408 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
409 return 0;
410
411 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
412 (id->bInterfaceClass != intf->desc.bInterfaceClass))
413 return 0;
414
415 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
416 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
417 return 0;
418
419 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
420 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
421 return 0;
422
423 return 1;
424}
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800425/**
426 * usb_match_id - find first usb_device_id matching device or interface
427 * @interface: the interface of interest
428 * @id: array of usb_device_id structures, terminated by zero entry
429 *
430 * usb_match_id searches an array of usb_device_id's and returns
431 * the first one matching the device or interface, or null.
432 * This is used when binding (or rebinding) a driver to an interface.
433 * Most USB device drivers will use this indirectly, through the usb core,
434 * but some layered driver frameworks use it directly.
435 * These device tables are exported with MODULE_DEVICE_TABLE, through
436 * modutils, to support the driver loading functionality of USB hotplugging.
437 *
438 * What Matches:
439 *
440 * The "match_flags" element in a usb_device_id controls which
441 * members are used. If the corresponding bit is set, the
442 * value in the device_id must match its corresponding member
443 * in the device or interface descriptor, or else the device_id
444 * does not match.
445 *
446 * "driver_info" is normally used only by device drivers,
447 * but you can create a wildcard "matches anything" usb_device_id
448 * as a driver's "modules.usbmap" entry if you provide an id with
449 * only a nonzero "driver_info" field. If you do this, the USB device
450 * driver's probe() routine should use additional intelligence to
451 * decide whether to bind to the specified interface.
452 *
453 * What Makes Good usb_device_id Tables:
454 *
455 * The match algorithm is very simple, so that intelligence in
456 * driver selection must come from smart driver id records.
457 * Unless you have good reasons to use another selection policy,
458 * provide match elements only in related groups, and order match
459 * specifiers from specific to general. Use the macros provided
460 * for that purpose if you can.
461 *
462 * The most specific match specifiers use device descriptor
463 * data. These are commonly used with product-specific matches;
464 * the USB_DEVICE macro lets you provide vendor and product IDs,
465 * and you can also match against ranges of product revisions.
466 * These are widely used for devices with application or vendor
467 * specific bDeviceClass values.
468 *
469 * Matches based on device class/subclass/protocol specifications
470 * are slightly more general; use the USB_DEVICE_INFO macro, or
471 * its siblings. These are used with single-function devices
472 * where bDeviceClass doesn't specify that each interface has
473 * its own class.
474 *
475 * Matches based on interface class/subclass/protocol are the
476 * most general; they let drivers bind to any interface on a
477 * multiple-function device. Use the USB_INTERFACE_INFO
478 * macro, or its siblings, to match class-per-interface style
479 * devices (as recorded in bDeviceClass).
480 *
481 * Within those groups, remember that not all combinations are
482 * meaningful. For example, don't give a product version range
483 * without vendor and product IDs; or specify a protocol without
484 * its associated class and subclass.
485 */
486const struct usb_device_id *usb_match_id(struct usb_interface *interface,
487 const struct usb_device_id *id)
488{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800489 /* proc_connectinfo in devio.c may call us with id == NULL. */
490 if (id == NULL)
491 return NULL;
492
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800493 /* It is important to check that id->driver_info is nonzero,
494 since an entry that is all zeroes except for a nonzero
495 id->driver_info is the way to create an entry that
496 indicates that the driver want to examine every
497 device and interface. */
498 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
499 id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800500 if (usb_match_one_id(interface, id))
501 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800502 }
503
504 return NULL;
505}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800506EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800507
508int usb_device_match(struct device *dev, struct device_driver *drv)
509{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400510 /* devices and interfaces are handled separately */
511 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800512
Alan Stern8bb54ab2006-07-01 22:08:49 -0400513 /* interface drivers never match devices */
514 if (!is_usb_device_driver(drv))
515 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800516
Alan Stern8bb54ab2006-07-01 22:08:49 -0400517 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800518 return 1;
519
Alan Stern8bb54ab2006-07-01 22:08:49 -0400520 } else {
521 struct usb_interface *intf;
522 struct usb_driver *usb_drv;
523 const struct usb_device_id *id;
524
525 /* device drivers never match interfaces */
526 if (is_usb_device_driver(drv))
527 return 0;
528
529 intf = to_usb_interface(dev);
530 usb_drv = to_usb_driver(drv);
531
532 id = usb_match_id(intf, usb_drv->id_table);
533 if (id)
534 return 1;
535
536 id = usb_match_dynamic_id(intf, usb_drv);
537 if (id)
538 return 1;
539 }
540
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800541 return 0;
542}
543
Alan Stern36e56a32006-07-01 22:08:06 -0400544#ifdef CONFIG_HOTPLUG
545
546/*
547 * This sends an uevent to userspace, typically helping to load driver
548 * or other modules, configure the device, and more. Drivers can provide
549 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
550 *
551 * We're called either from khubd (the typical case) or from root hub
552 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
553 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
554 * device (and this configuration!) are still present.
555 */
556static int usb_uevent(struct device *dev, char **envp, int num_envp,
557 char *buffer, int buffer_size)
558{
559 struct usb_interface *intf;
560 struct usb_device *usb_dev;
561 struct usb_host_interface *alt;
562 int i = 0;
563 int length = 0;
564
565 if (!dev)
566 return -ENODEV;
567
568 /* driver is often null here; dev_dbg() would oops */
569 pr_debug ("usb %s: uevent\n", dev->bus_id);
570
Alan Stern782da722006-07-01 22:09:35 -0400571 if (is_usb_device(dev)) {
572 usb_dev = to_usb_device(dev);
573 alt = NULL;
574 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400575 intf = to_usb_interface(dev);
576 usb_dev = interface_to_usbdev(intf);
577 alt = intf->cur_altsetting;
578 }
Alan Stern36e56a32006-07-01 22:08:06 -0400579
580 if (usb_dev->devnum < 0) {
581 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
582 return -ENODEV;
583 }
584 if (!usb_dev->bus) {
585 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
586 return -ENODEV;
587 }
588
589#ifdef CONFIG_USB_DEVICEFS
590 /* If this is available, userspace programs can directly read
591 * all the device descriptors we don't tell them about. Or
592 * even act as usermode drivers.
593 *
594 * FIXME reduce hardwired intelligence here
595 */
596 if (add_uevent_var(envp, num_envp, &i,
597 buffer, buffer_size, &length,
598 "DEVICE=/proc/bus/usb/%03d/%03d",
599 usb_dev->bus->busnum, usb_dev->devnum))
600 return -ENOMEM;
601#endif
602
603 /* per-device configurations are common */
604 if (add_uevent_var(envp, num_envp, &i,
605 buffer, buffer_size, &length,
606 "PRODUCT=%x/%x/%x",
607 le16_to_cpu(usb_dev->descriptor.idVendor),
608 le16_to_cpu(usb_dev->descriptor.idProduct),
609 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
610 return -ENOMEM;
611
612 /* class-based driver binding models */
613 if (add_uevent_var(envp, num_envp, &i,
614 buffer, buffer_size, &length,
615 "TYPE=%d/%d/%d",
616 usb_dev->descriptor.bDeviceClass,
617 usb_dev->descriptor.bDeviceSubClass,
618 usb_dev->descriptor.bDeviceProtocol))
619 return -ENOMEM;
620
Alan Stern782da722006-07-01 22:09:35 -0400621 if (!is_usb_device(dev)) {
622
623 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400624 buffer, buffer_size, &length,
625 "INTERFACE=%d/%d/%d",
626 alt->desc.bInterfaceClass,
627 alt->desc.bInterfaceSubClass,
628 alt->desc.bInterfaceProtocol))
Alan Stern782da722006-07-01 22:09:35 -0400629 return -ENOMEM;
Alan Stern36e56a32006-07-01 22:08:06 -0400630
Alan Stern782da722006-07-01 22:09:35 -0400631 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400632 buffer, buffer_size, &length,
633 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
634 le16_to_cpu(usb_dev->descriptor.idVendor),
635 le16_to_cpu(usb_dev->descriptor.idProduct),
636 le16_to_cpu(usb_dev->descriptor.bcdDevice),
637 usb_dev->descriptor.bDeviceClass,
638 usb_dev->descriptor.bDeviceSubClass,
639 usb_dev->descriptor.bDeviceProtocol,
640 alt->desc.bInterfaceClass,
641 alt->desc.bInterfaceSubClass,
642 alt->desc.bInterfaceProtocol))
Alan Stern782da722006-07-01 22:09:35 -0400643 return -ENOMEM;
644 }
Alan Stern36e56a32006-07-01 22:08:06 -0400645
646 envp[i] = NULL;
647
648 return 0;
649}
650
651#else
652
653static int usb_uevent(struct device *dev, char **envp,
654 int num_envp, char *buffer, int buffer_size)
655{
656 return -ENODEV;
657}
658
659#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.
723 *
724 * Registers a USB interface driver with the USB core. The list of
725 * unattached interfaces will be rescanned whenever a new driver is
726 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800727 * Returns a negative error code on failure and 0 on success.
728 *
729 * NOTE: if you want your driver to use the USB major number, you must call
730 * usb_register_dev() to enable that functionality. This function no longer
731 * takes care of that.
732 */
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800733int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800734{
735 int retval = 0;
736
737 if (usb_disabled())
738 return -ENODEV;
739
Alan Stern8bb54ab2006-07-01 22:08:49 -0400740 new_driver->drvwrap.for_devices = 0;
741 new_driver->drvwrap.driver.name = (char *) new_driver->name;
742 new_driver->drvwrap.driver.bus = &usb_bus_type;
743 new_driver->drvwrap.driver.probe = usb_probe_interface;
744 new_driver->drvwrap.driver.remove = usb_unbind_interface;
745 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800746 spin_lock_init(&new_driver->dynids.lock);
747 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800748
Alan Stern8bb54ab2006-07-01 22:08:49 -0400749 retval = driver_register(&new_driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800750
751 if (!retval) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400752 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800753 usbcore_name, new_driver->name);
754 usbfs_update_special();
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800755 usb_create_newid_file(new_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800756 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400757 printk(KERN_ERR "%s: error %d registering interface "
758 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800759 usbcore_name, retval, new_driver->name);
760 }
761
762 return retval;
763}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800764EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800765
766/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400767 * usb_deregister - unregister a USB interface driver
768 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800769 * Context: must be able to sleep
770 *
771 * Unlinks the specified driver from the internal USB driver list.
772 *
773 * NOTE: If you called usb_register_dev(), you still need to call
774 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
775 * this * call will no longer do it for you.
776 */
777void usb_deregister(struct usb_driver *driver)
778{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400779 pr_info("%s: deregistering interface driver %s\n",
780 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800781
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800782 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800783 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400784 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800785
786 usbfs_update_special();
787}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800788EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400789
790#ifdef CONFIG_PM
791
Alan Stern645daaa2006-08-30 15:47:02 -0400792/* Caller has locked udev->pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400793static int suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -0400794{
Alan Stern782da722006-07-01 22:09:35 -0400795 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400796 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400797
Alan Stern114b3682006-07-01 22:13:04 -0400798 if (udev->state == USB_STATE_NOTATTACHED ||
799 udev->state == USB_STATE_SUSPENDED)
800 goto done;
801
Alan Stern1c5df7e2006-07-01 22:13:50 -0400802 /* For devices that don't have a driver, we do a standard suspend. */
803 if (udev->dev.driver == NULL) {
Alan Stern645daaa2006-08-30 15:47:02 -0400804 udev->do_remote_wakeup = 0;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400805 status = usb_port_suspend(udev);
Alan Stern2bf40862006-07-01 22:12:19 -0400806 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400807 }
808
Alan Stern1cc8a252006-07-01 22:10:15 -0400809 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern2bf40862006-07-01 22:12:19 -0400810 status = udriver->suspend(udev, msg);
811
812done:
Alan Stern645daaa2006-08-30 15:47:02 -0400813 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400814 if (status == 0)
815 udev->dev.power.power_state.event = msg.event;
816 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400817}
Alan Stern36e56a32006-07-01 22:08:06 -0400818
Alan Stern645daaa2006-08-30 15:47:02 -0400819/* Caller has locked udev->pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400820static int resume_device(struct usb_device *udev)
821{
822 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400823 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400824
Alan Stern114b3682006-07-01 22:13:04 -0400825 if (udev->state == USB_STATE_NOTATTACHED ||
826 udev->state != USB_STATE_SUSPENDED)
Alan Stern2bf40862006-07-01 22:12:19 -0400827 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400828
Alan Stern1c5df7e2006-07-01 22:13:50 -0400829 /* Can't resume it if it doesn't have a driver. */
830 if (udev->dev.driver == NULL) {
831 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400832 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400833 }
834
Alan Stern1cc8a252006-07-01 22:10:15 -0400835 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern2bf40862006-07-01 22:12:19 -0400836 status = udriver->resume(udev);
837
838done:
Alan Stern645daaa2006-08-30 15:47:02 -0400839 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400840 if (status == 0)
841 udev->dev.power.power_state.event = PM_EVENT_ON;
842 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400843}
844
Alan Stern645daaa2006-08-30 15:47:02 -0400845/* Caller has locked intf's usb_device's pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400846static int suspend_interface(struct usb_interface *intf, pm_message_t msg)
847{
848 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400849 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400850
Alan Stern114b3682006-07-01 22:13:04 -0400851 /* with no hardware, USB interfaces only use FREEZE and ON states */
852 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
853 !is_active(intf))
854 goto done;
855
Alan Stern645daaa2006-08-30 15:47:02 -0400856 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
Alan Stern2bf40862006-07-01 22:12:19 -0400857 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400858 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400859
Alan Stern36e56a32006-07-01 22:08:06 -0400860 if (driver->suspend && driver->resume) {
Alan Stern1cc8a252006-07-01 22:10:15 -0400861 status = driver->suspend(intf, msg);
Alan Stern645daaa2006-08-30 15:47:02 -0400862 if (status == 0)
863 mark_quiesced(intf);
864 else if (!interface_to_usbdev(intf)->auto_pm)
Alan Stern1cc8a252006-07-01 22:10:15 -0400865 dev_err(&intf->dev, "%s error %d\n",
866 "suspend", status);
Alan Stern36e56a32006-07-01 22:08:06 -0400867 } else {
868 // FIXME else if there's no suspend method, disconnect...
Alan Stern645daaa2006-08-30 15:47:02 -0400869 // Not possible if auto_pm is set...
Alan Stern1cc8a252006-07-01 22:10:15 -0400870 dev_warn(&intf->dev, "no suspend for driver %s?\n",
871 driver->name);
Alan Stern36e56a32006-07-01 22:08:06 -0400872 mark_quiesced(intf);
Alan Stern36e56a32006-07-01 22:08:06 -0400873 }
Alan Stern2bf40862006-07-01 22:12:19 -0400874
875done:
Alan Stern645daaa2006-08-30 15:47:02 -0400876 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400877 if (status == 0)
878 intf->dev.power.power_state.event = msg.event;
Alan Stern36e56a32006-07-01 22:08:06 -0400879 return status;
880}
881
Alan Stern645daaa2006-08-30 15:47:02 -0400882/* Caller has locked intf's usb_device's pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400883static int resume_interface(struct usb_interface *intf)
Alan Stern36e56a32006-07-01 22:08:06 -0400884{
Alan Stern1cc8a252006-07-01 22:10:15 -0400885 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400886 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400887
Alan Stern114b3682006-07-01 22:13:04 -0400888 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
889 is_active(intf))
Alan Stern2bf40862006-07-01 22:12:19 -0400890 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -0400891
Alan Stern645daaa2006-08-30 15:47:02 -0400892 /* Don't let autoresume interfere with unbinding */
893 if (intf->condition == USB_INTERFACE_UNBINDING)
894 goto done;
895
Alan Stern1c5df7e2006-07-01 22:13:50 -0400896 /* Can't resume it if it doesn't have a driver. */
Alan Stern645daaa2006-08-30 15:47:02 -0400897 if (intf->condition == USB_INTERFACE_UNBOUND) {
Alan Stern1c5df7e2006-07-01 22:13:50 -0400898 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400899 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400900 }
Alan Stern1cc8a252006-07-01 22:10:15 -0400901 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400902
Alan Stern36e56a32006-07-01 22:08:06 -0400903 if (driver->resume) {
904 status = driver->resume(intf);
Alan Stern2bf40862006-07-01 22:12:19 -0400905 if (status)
Alan Stern1cc8a252006-07-01 22:10:15 -0400906 dev_err(&intf->dev, "%s error %d\n",
907 "resume", status);
Alan Stern2bf40862006-07-01 22:12:19 -0400908 else
909 mark_active(intf);
910 } else {
Alan Stern1cc8a252006-07-01 22:10:15 -0400911 dev_warn(&intf->dev, "no resume for driver %s?\n",
912 driver->name);
Alan Stern2bf40862006-07-01 22:12:19 -0400913 mark_active(intf);
914 }
915
916done:
Alan Stern645daaa2006-08-30 15:47:02 -0400917 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400918 if (status == 0)
919 intf->dev.power.power_state.event = PM_EVENT_ON;
920 return status;
Alan Stern36e56a32006-07-01 22:08:06 -0400921}
922
Alan Stern645daaa2006-08-30 15:47:02 -0400923/**
924 * usb_suspend_both - suspend a USB device and its interfaces
925 * @udev: the usb_device to suspend
926 * @msg: Power Management message describing this state transition
927 *
928 * This is the central routine for suspending USB devices. It calls the
929 * suspend methods for all the interface drivers in @udev and then calls
930 * the suspend method for @udev itself. If an error occurs at any stage,
931 * all the interfaces which were suspended are resumed so that they remain
932 * in the same state as the device.
933 *
934 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
935 * checks first to make sure that neither the device itself or any of its
936 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
937 * are, the autosuspend fails.
938 *
939 * If the suspend succeeds, the routine recursively queues an autosuspend
940 * request for @udev's parent device, thereby propagating the change up
941 * the device tree. If all of the parent's children are now suspended,
942 * the parent will autosuspend in turn.
943 *
944 * The suspend method calls are subject to mutual exclusion under control
945 * of @udev's pm_mutex. Many of these calls are also under the protection
946 * of @udev's device lock (including all requests originating outside the
947 * USB subsystem), but autosuspend requests generated by a child device or
948 * interface driver may not be. Usbcore will insure that the method calls
949 * do not arrive during bind, unbind, or reset operations. However, drivers
950 * must be prepared to handle suspend calls arriving at unpredictable times.
951 * The only way to block such calls is to do an autoresume (preventing
952 * autosuspends) while holding @udev's device lock (preventing outside
953 * suspends).
954 *
955 * The caller must hold @udev->pm_mutex.
956 *
957 * This routine can run only in process context.
958 */
Alan Sterna8e7c562006-07-01 22:11:02 -0400959int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
960{
961 int status = 0;
962 int i = 0;
963 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400964 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -0400965
Alan Stern645daaa2006-08-30 15:47:02 -0400966 cancel_delayed_work(&udev->autosuspend);
967 if (udev->state == USB_STATE_NOTATTACHED)
968 return 0;
969 if (udev->state == USB_STATE_SUSPENDED)
970 return 0;
971
972 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
973
974 /* For autosuspend, fail fast if anything is in use.
975 * Also fail if any interfaces require remote wakeup but it
976 * isn't available. */
977 if (udev->auto_pm) {
978 if (udev->pm_usage_cnt > 0)
979 return -EBUSY;
980 if (udev->actconfig) {
981 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
982 intf = udev->actconfig->interface[i];
983 if (!is_active(intf))
984 continue;
985 if (intf->pm_usage_cnt > 0)
986 return -EBUSY;
987 if (intf->needs_remote_wakeup &&
988 !udev->do_remote_wakeup) {
989 dev_dbg(&udev->dev,
990 "remote wakeup needed for autosuspend\n");
991 return -EOPNOTSUPP;
992 }
993 }
994 i = 0;
995 }
996 }
997
998 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -0400999 if (udev->actconfig) {
1000 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1001 intf = udev->actconfig->interface[i];
1002 status = suspend_interface(intf, msg);
1003 if (status != 0)
1004 break;
1005 }
1006 }
1007 if (status == 0)
1008 status = suspend_device(udev, msg);
1009
1010 /* If the suspend failed, resume interfaces that did get suspended */
1011 if (status != 0) {
1012 while (--i >= 0) {
1013 intf = udev->actconfig->interface[i];
1014 resume_interface(intf);
1015 }
Alan Stern645daaa2006-08-30 15:47:02 -04001016
1017 /* If the suspend succeeded, propagate it up the tree */
1018 } else if (parent)
1019 usb_autosuspend_device(parent, 0);
1020
1021 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001022 return status;
1023}
1024
Alan Stern645daaa2006-08-30 15:47:02 -04001025/**
1026 * usb_resume_both - resume a USB device and its interfaces
1027 * @udev: the usb_device to resume
1028 *
1029 * This is the central routine for resuming USB devices. It calls the
1030 * the resume method for @udev and then calls the resume methods for all
1031 * the interface drivers in @udev.
1032 *
1033 * Before starting the resume, the routine calls itself recursively for
1034 * the parent device of @udev, thereby propagating the change up the device
1035 * tree and assuring that @udev will be able to resume. If the parent is
1036 * unable to resume successfully, the routine fails.
1037 *
1038 * The resume method calls are subject to mutual exclusion under control
1039 * of @udev's pm_mutex. Many of these calls are also under the protection
1040 * of @udev's device lock (including all requests originating outside the
1041 * USB subsystem), but autoresume requests generated by a child device or
1042 * interface driver may not be. Usbcore will insure that the method calls
1043 * do not arrive during bind, unbind, or reset operations. However, drivers
1044 * must be prepared to handle resume calls arriving at unpredictable times.
1045 * The only way to block such calls is to do an autoresume (preventing
1046 * other autoresumes) while holding @udev's device lock (preventing outside
1047 * resumes).
1048 *
1049 * The caller must hold @udev->pm_mutex.
1050 *
1051 * This routine can run only in process context.
1052 */
Alan Sterna8e7c562006-07-01 22:11:02 -04001053int usb_resume_both(struct usb_device *udev)
1054{
Alan Stern645daaa2006-08-30 15:47:02 -04001055 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001056 int i;
1057 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001058 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001059
Alan Stern645daaa2006-08-30 15:47:02 -04001060 cancel_delayed_work(&udev->autosuspend);
1061 if (udev->state == USB_STATE_NOTATTACHED)
1062 return -ENODEV;
1063
1064 /* Propagate the resume up the tree, if necessary */
1065 if (udev->state == USB_STATE_SUSPENDED) {
1066 if (parent) {
1067 mutex_lock_nested(&parent->pm_mutex, parent->level);
1068 parent->auto_pm = 1;
1069 status = usb_resume_both(parent);
1070 } else {
1071
1072 /* We can't progagate beyond the USB subsystem,
1073 * so if a root hub's controller is suspended
1074 * then we're stuck. */
1075 if (udev->dev.parent->power.power_state.event !=
1076 PM_EVENT_ON)
1077 status = -EHOSTUNREACH;
1078 }
Alan Stern592fbbe2006-09-19 10:08:43 -04001079 if (status == 0)
Alan Stern645daaa2006-08-30 15:47:02 -04001080 status = resume_device(udev);
1081 if (parent)
1082 mutex_unlock(&parent->pm_mutex);
Alan Stern592fbbe2006-09-19 10:08:43 -04001083 } else {
1084
1085 /* Needed only for setting udev->dev.power.power_state.event
1086 * and for possible debugging message. */
1087 status = resume_device(udev);
Alan Stern114b3682006-07-01 22:13:04 -04001088 }
1089
Alan Stern645daaa2006-08-30 15:47:02 -04001090 /* Now the parent won't suspend until we are finished */
1091
Alan Sterna8e7c562006-07-01 22:11:02 -04001092 if (status == 0 && udev->actconfig) {
1093 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1094 intf = udev->actconfig->interface[i];
1095 resume_interface(intf);
1096 }
1097 }
Alan Stern645daaa2006-08-30 15:47:02 -04001098
1099 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001100 return status;
1101}
1102
Alan Stern645daaa2006-08-30 15:47:02 -04001103#ifdef CONFIG_USB_SUSPEND
1104
1105/**
1106 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001107 * @udev: the usb_device to autosuspend
1108 * @dec_usage_cnt: flag to decrement @udev's PM-usage counter
Alan Stern645daaa2006-08-30 15:47:02 -04001109 *
1110 * This routine should be called when a core subsystem is finished using
1111 * @udev and wants to allow it to autosuspend. Examples would be when
1112 * @udev's device file in usbfs is closed or after a configuration change.
1113 *
1114 * @dec_usage_cnt should be 1 if the subsystem previously incremented
1115 * @udev's usage counter (such as by passing 1 to usb_autoresume_device);
1116 * otherwise it should be 0.
1117 *
1118 * If the usage counter for @udev or any of its active interfaces is greater
1119 * than 0, the autosuspend request will not be queued. (If an interface
1120 * driver does not support autosuspend then its usage counter is permanently
1121 * positive.) Likewise, if an interface driver requires remote-wakeup
1122 * capability during autosuspend but remote wakeup is disabled, the
1123 * autosuspend will fail.
1124 *
1125 * Often the caller will hold @udev's device lock, but this is not
1126 * necessary.
1127 *
1128 * This routine can run only in process context.
1129 */
1130void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt)
1131{
1132 mutex_lock_nested(&udev->pm_mutex, udev->level);
1133 udev->pm_usage_cnt -= dec_usage_cnt;
1134 if (udev->pm_usage_cnt <= 0)
Alan Sternbd859282006-09-19 10:14:07 -04001135 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
Alan Stern645daaa2006-08-30 15:47:02 -04001136 USB_AUTOSUSPEND_DELAY);
1137 mutex_unlock(&udev->pm_mutex);
1138 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1139 // __FUNCTION__, udev->pm_usage_cnt);
1140}
1141
1142/**
1143 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001144 * @udev: the usb_device to autoresume
1145 * @inc_usage_cnt: flag to increment @udev's PM-usage counter
Alan Stern645daaa2006-08-30 15:47:02 -04001146 *
1147 * This routine should be called when a core subsystem wants to use @udev
1148 * and needs to guarantee that it is not suspended. In addition, the
1149 * caller can prevent @udev from being autosuspended subsequently. (Note
1150 * that this will not prevent suspend events originating in the PM core.)
1151 * Examples would be when @udev's device file in usbfs is opened (autosuspend
1152 * should be prevented until the file is closed) or when a remote-wakeup
1153 * request is received (later autosuspends should not be prevented).
1154 *
1155 * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent
1156 * autosuspends. This prevention will persist until the usage counter is
1157 * decremented again (such as by passing 1 to usb_autosuspend_device).
1158 * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged.
1159 * Regardless, if the autoresume fails then the usage counter is not
1160 * incremented.
1161 *
1162 * Often the caller will hold @udev's device lock, but this is not
1163 * necessary (and attempting it might cause deadlock).
1164 *
1165 * This routine can run only in process context.
1166 */
1167int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt)
1168{
1169 int status;
1170
1171 mutex_lock_nested(&udev->pm_mutex, udev->level);
1172 udev->pm_usage_cnt += inc_usage_cnt;
1173 udev->auto_pm = 1;
1174 status = usb_resume_both(udev);
1175 if (status != 0)
1176 udev->pm_usage_cnt -= inc_usage_cnt;
1177 mutex_unlock(&udev->pm_mutex);
1178 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1179 // __FUNCTION__, status, udev->pm_usage_cnt);
1180 return status;
1181}
1182
1183/**
1184 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001185 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001186 *
1187 * This routine should be called by an interface driver when it is
1188 * finished using @intf and wants to allow it to autosuspend. A typical
1189 * example would be a character-device driver when its device file is
1190 * closed.
1191 *
1192 * The routine decrements @intf's usage counter. When the counter reaches
1193 * 0, a delayed autosuspend request for @intf's device is queued. When
1194 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1195 * the other usage counters for the sibling interfaces and @intf's
1196 * usb_device, the device and all its interfaces will be autosuspended.
1197 *
1198 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1199 * core will not change its value other than the increment and decrement
1200 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1201 * may use this simple counter-oriented discipline or may set the value
1202 * any way it likes.
1203 *
1204 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1205 * take place only if the device's remote-wakeup facility is enabled.
1206 *
1207 * Suspend method calls queued by this routine can arrive at any time
1208 * while @intf is resumed and its usage counter is equal to 0. They are
1209 * not protected by the usb_device's lock but only by its pm_mutex.
1210 * Drivers must provide their own synchronization.
1211 *
1212 * This routine can run only in process context.
1213 */
1214void usb_autopm_put_interface(struct usb_interface *intf)
1215{
1216 struct usb_device *udev = interface_to_usbdev(intf);
1217
1218 mutex_lock_nested(&udev->pm_mutex, udev->level);
Alan Sternbd859282006-09-19 10:14:07 -04001219 if (intf->condition != USB_INTERFACE_UNBOUND &&
1220 --intf->pm_usage_cnt <= 0) {
1221 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1222 USB_AUTOSUSPEND_DELAY);
Alan Stern645daaa2006-08-30 15:47:02 -04001223 }
1224 mutex_unlock(&udev->pm_mutex);
1225 // dev_dbg(&intf->dev, "%s: cnt %d\n",
1226 // __FUNCTION__, intf->pm_usage_cnt);
1227}
1228EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1229
1230/**
1231 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001232 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001233 *
1234 * This routine should be called by an interface driver when it wants to
1235 * use @intf and needs to guarantee that it is not suspended. In addition,
1236 * the routine prevents @intf from being autosuspended subsequently. (Note
1237 * that this will not prevent suspend events originating in the PM core.)
1238 * This prevention will persist until usb_autopm_put_interface() is called
1239 * or @intf is unbound. A typical example would be a character-device
1240 * driver when its device file is opened.
1241 *
1242 * The routine increments @intf's usage counter. So long as the counter
1243 * is greater than 0, autosuspend will not be allowed for @intf or its
1244 * usb_device. When the driver is finished using @intf it should call
1245 * usb_autopm_put_interface() to decrement the usage counter and queue
1246 * a delayed autosuspend request (if the counter is <= 0).
1247 *
1248 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1249 * core will not change its value other than the increment and decrement
1250 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1251 * may use this simple counter-oriented discipline or may set the value
1252 * any way it likes.
1253 *
1254 * Resume method calls generated by this routine can arrive at any time
1255 * while @intf is suspended. They are not protected by the usb_device's
1256 * lock but only by its pm_mutex. Drivers must provide their own
1257 * synchronization.
1258 *
1259 * This routine can run only in process context.
1260 */
1261int usb_autopm_get_interface(struct usb_interface *intf)
1262{
1263 struct usb_device *udev = interface_to_usbdev(intf);
1264 int status;
1265
1266 mutex_lock_nested(&udev->pm_mutex, udev->level);
1267 if (intf->condition == USB_INTERFACE_UNBOUND)
1268 status = -ENODEV;
1269 else {
1270 ++intf->pm_usage_cnt;
1271 udev->auto_pm = 1;
1272 status = usb_resume_both(udev);
1273 if (status != 0)
1274 --intf->pm_usage_cnt;
1275 }
1276 mutex_unlock(&udev->pm_mutex);
1277 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1278 // __FUNCTION__, status, intf->pm_usage_cnt);
1279 return status;
1280}
1281EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1282
1283#endif /* CONFIG_USB_SUSPEND */
1284
Alan Stern1cc8a252006-07-01 22:10:15 -04001285static int usb_suspend(struct device *dev, pm_message_t message)
1286{
1287 int status;
1288
Alan Stern645daaa2006-08-30 15:47:02 -04001289 if (is_usb_device(dev)) {
1290 struct usb_device *udev = to_usb_device(dev);
1291
1292 mutex_lock_nested(&udev->pm_mutex, udev->level);
1293 udev->auto_pm = 0;
1294 status = usb_suspend_both(udev, message);
1295 mutex_unlock(&udev->pm_mutex);
1296 } else
Alan Sterna8e7c562006-07-01 22:11:02 -04001297 status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001298 return status;
1299}
1300
1301static int usb_resume(struct device *dev)
1302{
1303 int status;
1304
Alan Sterna8e7c562006-07-01 22:11:02 -04001305 if (is_usb_device(dev)) {
Alan Stern645daaa2006-08-30 15:47:02 -04001306 struct usb_device *udev = to_usb_device(dev);
1307
1308 mutex_lock_nested(&udev->pm_mutex, udev->level);
1309 udev->auto_pm = 0;
1310 status = usb_resume_both(udev);
1311 mutex_unlock(&udev->pm_mutex);
Alan Sterna8e7c562006-07-01 22:11:02 -04001312
1313 /* Rebind drivers that had no suspend method? */
1314 } else
1315 status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001316 return status;
1317}
1318
Alan Stern36e56a32006-07-01 22:08:06 -04001319#endif /* CONFIG_PM */
1320
1321struct bus_type usb_bus_type = {
1322 .name = "usb",
1323 .match = usb_device_match,
1324 .uevent = usb_uevent,
1325#ifdef CONFIG_PM
Alan Stern782da722006-07-01 22:09:35 -04001326 .suspend = usb_suspend,
1327 .resume = usb_resume,
Alan Stern36e56a32006-07-01 22:08:06 -04001328#endif
1329};