blob: 2b2000ac05ab5c57dd63d08886585c87ffb4a9e3 [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>
27#include "hcd.h"
28#include "usb.h"
29
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080030static int usb_match_one_id(struct usb_interface *interface,
31 const struct usb_device_id *id);
32
33struct usb_dynid {
34 struct list_head node;
35 struct usb_device_id id;
36};
37
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080038#ifdef CONFIG_HOTPLUG
39
40/*
41 * Adds a new dynamic USBdevice ID to this driver,
42 * and cause the driver to probe for all devices again.
43 */
44static ssize_t store_new_id(struct device_driver *driver,
45 const char *buf, size_t count)
46{
47 struct usb_driver *usb_drv = to_usb_driver(driver);
48 struct usb_dynid *dynid;
49 u32 idVendor = 0;
50 u32 idProduct = 0;
51 int fields = 0;
52
53 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
54 if (fields < 2)
55 return -EINVAL;
56
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
58 if (!dynid)
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
65
66 spin_lock(&usb_drv->dynids.lock);
67 list_add_tail(&usb_drv->dynids.list, &dynid->node);
68 spin_unlock(&usb_drv->dynids.lock);
69
70 if (get_driver(driver)) {
71 driver_attach(driver);
72 put_driver(driver);
73 }
74
75 return count;
76}
77static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
78
79static int usb_create_newid_file(struct usb_driver *usb_drv)
80{
81 int error = 0;
82
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080083 if (usb_drv->no_dynamic_id)
84 goto exit;
85
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080086 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -040087 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080088 &driver_attr_new_id.attr);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080089exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080090 return error;
91}
92
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080093static void usb_remove_newid_file(struct usb_driver *usb_drv)
94{
95 if (usb_drv->no_dynamic_id)
96 return;
97
98 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -040099 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800100 &driver_attr_new_id.attr);
101}
102
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800103static void usb_free_dynids(struct usb_driver *usb_drv)
104{
105 struct usb_dynid *dynid, *n;
106
107 spin_lock(&usb_drv->dynids.lock);
108 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
109 list_del(&dynid->node);
110 kfree(dynid);
111 }
112 spin_unlock(&usb_drv->dynids.lock);
113}
114#else
115static inline int usb_create_newid_file(struct usb_driver *usb_drv)
116{
117 return 0;
118}
119
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800120static void usb_remove_newid_file(struct usb_driver *usb_drv)
121{
122}
123
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800124static inline void usb_free_dynids(struct usb_driver *usb_drv)
125{
126}
127#endif
128
129static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
130 struct usb_driver *drv)
131{
132 struct usb_dynid *dynid;
133
134 spin_lock(&drv->dynids.lock);
135 list_for_each_entry(dynid, &drv->dynids.list, node) {
136 if (usb_match_one_id(intf, &dynid->id)) {
137 spin_unlock(&drv->dynids.lock);
138 return &dynid->id;
139 }
140 }
141 spin_unlock(&drv->dynids.lock);
142 return NULL;
143}
144
145
Alan Stern8bb54ab2006-07-01 22:08:49 -0400146/* called from driver core with dev locked */
147static int usb_probe_device(struct device *dev)
148{
149 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
150 struct usb_device *udev;
151 int error = -ENODEV;
152
153 dev_dbg(dev, "%s\n", __FUNCTION__);
154
155 if (!is_usb_device(dev)) /* Sanity check */
156 return error;
157
158 udev = to_usb_device(dev);
159
Alan Stern8bb54ab2006-07-01 22:08:49 -0400160 /* TODO: Add real matching code */
161
Alan Stern645daaa2006-08-30 15:47:02 -0400162 /* The device should always appear to be in use
163 * unless the driver suports autosuspend.
164 */
165 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
166
Alan Stern8bb54ab2006-07-01 22:08:49 -0400167 error = udriver->probe(udev);
168 return error;
169}
170
171/* called from driver core with dev locked */
172static int usb_unbind_device(struct device *dev)
173{
174 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
175
176 udriver->disconnect(to_usb_device(dev));
177 return 0;
178}
179
180
181/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800182static int usb_probe_interface(struct device *dev)
183{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400184 struct usb_driver *driver = to_usb_driver(dev->driver);
185 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400186 struct usb_device *udev;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800187 const struct usb_device_id *id;
188 int error = -ENODEV;
189
190 dev_dbg(dev, "%s\n", __FUNCTION__);
191
Alan Stern8bb54ab2006-07-01 22:08:49 -0400192 if (is_usb_device(dev)) /* Sanity check */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800193 return error;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400194
195 intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400196 udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800197
198 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800199 if (!id)
200 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800201 if (id) {
202 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
203
Alan Stern645daaa2006-08-30 15:47:02 -0400204 error = usb_autoresume_device(udev, 1);
205 if (error)
206 return error;
207
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800208 /* Interface "power state" doesn't correspond to any hardware
209 * state whatsoever. We use it to record when it's bound to
210 * a driver that may start I/0: it's not frozen/quiesced.
211 */
212 mark_active(intf);
213 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400214
215 /* The interface should always appear to be in use
216 * unless the driver suports autosuspend.
217 */
218 intf->pm_usage_cnt = !(driver->supports_autosuspend);
219
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800220 error = driver->probe(intf, id);
221 if (error) {
222 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400223 intf->needs_remote_wakeup = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800224 intf->condition = USB_INTERFACE_UNBOUND;
225 } else
226 intf->condition = USB_INTERFACE_BOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400227
228 usb_autosuspend_device(udev, 1);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800229 }
230
231 return error;
232}
233
Alan Stern8bb54ab2006-07-01 22:08:49 -0400234/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800235static int usb_unbind_interface(struct device *dev)
236{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400237 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800238 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400239 struct usb_device *udev;
240 int error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800241
242 intf->condition = USB_INTERFACE_UNBINDING;
243
Alan Stern645daaa2006-08-30 15:47:02 -0400244 /* Autoresume for set_interface call below */
245 udev = interface_to_usbdev(intf);
246 error = usb_autoresume_device(udev, 1);
247
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800248 /* release all urbs for this interface */
249 usb_disable_interface(interface_to_usbdev(intf), intf);
250
Alan Stern8bb54ab2006-07-01 22:08:49 -0400251 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800252
253 /* reset other interface state */
254 usb_set_interface(interface_to_usbdev(intf),
255 intf->altsetting[0].desc.bInterfaceNumber,
256 0);
257 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400258
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800259 intf->condition = USB_INTERFACE_UNBOUND;
260 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400261 intf->needs_remote_wakeup = 0;
262
263 if (!error)
264 usb_autosuspend_device(udev, 1);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800265
266 return 0;
267}
268
Alan Stern36e56a32006-07-01 22:08:06 -0400269/**
270 * usb_driver_claim_interface - bind a driver to an interface
271 * @driver: the driver to be bound
272 * @iface: the interface to which it will be bound; must be in the
273 * usb device's active configuration
274 * @priv: driver data associated with that interface
275 *
276 * This is used by usb device drivers that need to claim more than one
277 * interface on a device when probing (audio and acm are current examples).
278 * No device driver should directly modify internal usb_interface or
279 * usb_device structure members.
280 *
281 * Few drivers should need to use this routine, since the most natural
282 * way to bind to an interface is to return the private data from
283 * the driver's probe() method.
284 *
285 * Callers must own the device lock and the driver model's usb_bus_type.subsys
286 * writelock. So driver probe() entries don't need extra locking,
287 * but other call contexts may need to explicitly claim those locks.
288 */
289int usb_driver_claim_interface(struct usb_driver *driver,
290 struct usb_interface *iface, void* priv)
291{
292 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400293 struct usb_device *udev = interface_to_usbdev(iface);
Alan Stern36e56a32006-07-01 22:08:06 -0400294
295 if (dev->driver)
296 return -EBUSY;
297
Alan Stern8bb54ab2006-07-01 22:08:49 -0400298 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400299 usb_set_intfdata(iface, priv);
Alan Stern645daaa2006-08-30 15:47:02 -0400300
301 mutex_lock_nested(&udev->pm_mutex, udev->level);
Alan Stern36e56a32006-07-01 22:08:06 -0400302 iface->condition = USB_INTERFACE_BOUND;
303 mark_active(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400304 iface->pm_usage_cnt = !(driver->supports_autosuspend);
305 mutex_unlock(&udev->pm_mutex);
Alan Stern36e56a32006-07-01 22:08:06 -0400306
307 /* if interface was already added, bind now; else let
308 * the future device_add() bind it, bypassing probe()
309 */
310 if (device_is_registered(dev))
311 device_bind_driver(dev);
312
313 return 0;
314}
315EXPORT_SYMBOL(usb_driver_claim_interface);
316
317/**
318 * usb_driver_release_interface - unbind a driver from an interface
319 * @driver: the driver to be unbound
320 * @iface: the interface from which it will be unbound
321 *
322 * This can be used by drivers to release an interface without waiting
323 * for their disconnect() methods to be called. In typical cases this
324 * also causes the driver disconnect() method to be called.
325 *
326 * This call is synchronous, and may not be used in an interrupt context.
327 * Callers must own the device lock and the driver model's usb_bus_type.subsys
328 * writelock. So driver disconnect() entries don't need extra locking,
329 * but other call contexts may need to explicitly claim those locks.
330 */
331void usb_driver_release_interface(struct usb_driver *driver,
332 struct usb_interface *iface)
333{
334 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400335 struct usb_device *udev = interface_to_usbdev(iface);
Alan Stern36e56a32006-07-01 22:08:06 -0400336
337 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400338 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400339 return;
340
341 /* don't release from within disconnect() */
342 if (iface->condition != USB_INTERFACE_BOUND)
343 return;
344
345 /* don't release if the interface hasn't been added yet */
346 if (device_is_registered(dev)) {
347 iface->condition = USB_INTERFACE_UNBINDING;
348 device_release_driver(dev);
349 }
350
351 dev->driver = NULL;
352 usb_set_intfdata(iface, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400353
354 mutex_lock_nested(&udev->pm_mutex, udev->level);
Alan Stern36e56a32006-07-01 22:08:06 -0400355 iface->condition = USB_INTERFACE_UNBOUND;
356 mark_quiesced(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400357 iface->needs_remote_wakeup = 0;
358 mutex_unlock(&udev->pm_mutex);
Alan Stern36e56a32006-07-01 22:08:06 -0400359}
360EXPORT_SYMBOL(usb_driver_release_interface);
361
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800362/* returns 0 if no match, 1 if match */
363static int usb_match_one_id(struct usb_interface *interface,
364 const struct usb_device_id *id)
365{
366 struct usb_host_interface *intf;
367 struct usb_device *dev;
368
369 /* proc_connectinfo in devio.c may call us with id == NULL. */
370 if (id == NULL)
371 return 0;
372
373 intf = interface->cur_altsetting;
374 dev = interface_to_usbdev(interface);
375
376 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
377 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
378 return 0;
379
380 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
381 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
382 return 0;
383
384 /* No need to test id->bcdDevice_lo != 0, since 0 is never
385 greater than any unsigned number. */
386 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
387 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
388 return 0;
389
390 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
391 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
392 return 0;
393
394 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
395 (id->bDeviceClass != dev->descriptor.bDeviceClass))
396 return 0;
397
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
399 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
400 return 0;
401
402 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
403 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
404 return 0;
405
406 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
407 (id->bInterfaceClass != intf->desc.bInterfaceClass))
408 return 0;
409
410 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
411 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
412 return 0;
413
414 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
415 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
416 return 0;
417
418 return 1;
419}
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800420/**
421 * usb_match_id - find first usb_device_id matching device or interface
422 * @interface: the interface of interest
423 * @id: array of usb_device_id structures, terminated by zero entry
424 *
425 * usb_match_id searches an array of usb_device_id's and returns
426 * the first one matching the device or interface, or null.
427 * This is used when binding (or rebinding) a driver to an interface.
428 * Most USB device drivers will use this indirectly, through the usb core,
429 * but some layered driver frameworks use it directly.
430 * These device tables are exported with MODULE_DEVICE_TABLE, through
431 * modutils, to support the driver loading functionality of USB hotplugging.
432 *
433 * What Matches:
434 *
435 * The "match_flags" element in a usb_device_id controls which
436 * members are used. If the corresponding bit is set, the
437 * value in the device_id must match its corresponding member
438 * in the device or interface descriptor, or else the device_id
439 * does not match.
440 *
441 * "driver_info" is normally used only by device drivers,
442 * but you can create a wildcard "matches anything" usb_device_id
443 * as a driver's "modules.usbmap" entry if you provide an id with
444 * only a nonzero "driver_info" field. If you do this, the USB device
445 * driver's probe() routine should use additional intelligence to
446 * decide whether to bind to the specified interface.
447 *
448 * What Makes Good usb_device_id Tables:
449 *
450 * The match algorithm is very simple, so that intelligence in
451 * driver selection must come from smart driver id records.
452 * Unless you have good reasons to use another selection policy,
453 * provide match elements only in related groups, and order match
454 * specifiers from specific to general. Use the macros provided
455 * for that purpose if you can.
456 *
457 * The most specific match specifiers use device descriptor
458 * data. These are commonly used with product-specific matches;
459 * the USB_DEVICE macro lets you provide vendor and product IDs,
460 * and you can also match against ranges of product revisions.
461 * These are widely used for devices with application or vendor
462 * specific bDeviceClass values.
463 *
464 * Matches based on device class/subclass/protocol specifications
465 * are slightly more general; use the USB_DEVICE_INFO macro, or
466 * its siblings. These are used with single-function devices
467 * where bDeviceClass doesn't specify that each interface has
468 * its own class.
469 *
470 * Matches based on interface class/subclass/protocol are the
471 * most general; they let drivers bind to any interface on a
472 * multiple-function device. Use the USB_INTERFACE_INFO
473 * macro, or its siblings, to match class-per-interface style
474 * devices (as recorded in bDeviceClass).
475 *
476 * Within those groups, remember that not all combinations are
477 * meaningful. For example, don't give a product version range
478 * without vendor and product IDs; or specify a protocol without
479 * its associated class and subclass.
480 */
481const struct usb_device_id *usb_match_id(struct usb_interface *interface,
482 const struct usb_device_id *id)
483{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800484 /* proc_connectinfo in devio.c may call us with id == NULL. */
485 if (id == NULL)
486 return NULL;
487
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800488 /* It is important to check that id->driver_info is nonzero,
489 since an entry that is all zeroes except for a nonzero
490 id->driver_info is the way to create an entry that
491 indicates that the driver want to examine every
492 device and interface. */
493 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
494 id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800495 if (usb_match_one_id(interface, id))
496 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800497 }
498
499 return NULL;
500}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800501EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800502
503int usb_device_match(struct device *dev, struct device_driver *drv)
504{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400505 /* devices and interfaces are handled separately */
506 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800507
Alan Stern8bb54ab2006-07-01 22:08:49 -0400508 /* interface drivers never match devices */
509 if (!is_usb_device_driver(drv))
510 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800511
Alan Stern8bb54ab2006-07-01 22:08:49 -0400512 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800513 return 1;
514
Alan Stern8bb54ab2006-07-01 22:08:49 -0400515 } else {
516 struct usb_interface *intf;
517 struct usb_driver *usb_drv;
518 const struct usb_device_id *id;
519
520 /* device drivers never match interfaces */
521 if (is_usb_device_driver(drv))
522 return 0;
523
524 intf = to_usb_interface(dev);
525 usb_drv = to_usb_driver(drv);
526
527 id = usb_match_id(intf, usb_drv->id_table);
528 if (id)
529 return 1;
530
531 id = usb_match_dynamic_id(intf, usb_drv);
532 if (id)
533 return 1;
534 }
535
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800536 return 0;
537}
538
Alan Stern36e56a32006-07-01 22:08:06 -0400539#ifdef CONFIG_HOTPLUG
540
541/*
542 * This sends an uevent to userspace, typically helping to load driver
543 * or other modules, configure the device, and more. Drivers can provide
544 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
545 *
546 * We're called either from khubd (the typical case) or from root hub
547 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
548 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
549 * device (and this configuration!) are still present.
550 */
551static int usb_uevent(struct device *dev, char **envp, int num_envp,
552 char *buffer, int buffer_size)
553{
554 struct usb_interface *intf;
555 struct usb_device *usb_dev;
556 struct usb_host_interface *alt;
557 int i = 0;
558 int length = 0;
559
560 if (!dev)
561 return -ENODEV;
562
563 /* driver is often null here; dev_dbg() would oops */
564 pr_debug ("usb %s: uevent\n", dev->bus_id);
565
Alan Stern782da722006-07-01 22:09:35 -0400566 if (is_usb_device(dev)) {
567 usb_dev = to_usb_device(dev);
568 alt = NULL;
569 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400570 intf = to_usb_interface(dev);
571 usb_dev = interface_to_usbdev(intf);
572 alt = intf->cur_altsetting;
573 }
Alan Stern36e56a32006-07-01 22:08:06 -0400574
575 if (usb_dev->devnum < 0) {
576 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
577 return -ENODEV;
578 }
579 if (!usb_dev->bus) {
580 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
581 return -ENODEV;
582 }
583
584#ifdef CONFIG_USB_DEVICEFS
585 /* If this is available, userspace programs can directly read
586 * all the device descriptors we don't tell them about. Or
587 * even act as usermode drivers.
588 *
589 * FIXME reduce hardwired intelligence here
590 */
591 if (add_uevent_var(envp, num_envp, &i,
592 buffer, buffer_size, &length,
593 "DEVICE=/proc/bus/usb/%03d/%03d",
594 usb_dev->bus->busnum, usb_dev->devnum))
595 return -ENOMEM;
596#endif
597
598 /* per-device configurations are common */
599 if (add_uevent_var(envp, num_envp, &i,
600 buffer, buffer_size, &length,
601 "PRODUCT=%x/%x/%x",
602 le16_to_cpu(usb_dev->descriptor.idVendor),
603 le16_to_cpu(usb_dev->descriptor.idProduct),
604 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
605 return -ENOMEM;
606
607 /* class-based driver binding models */
608 if (add_uevent_var(envp, num_envp, &i,
609 buffer, buffer_size, &length,
610 "TYPE=%d/%d/%d",
611 usb_dev->descriptor.bDeviceClass,
612 usb_dev->descriptor.bDeviceSubClass,
613 usb_dev->descriptor.bDeviceProtocol))
614 return -ENOMEM;
615
Alan Stern782da722006-07-01 22:09:35 -0400616 if (!is_usb_device(dev)) {
617
618 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400619 buffer, buffer_size, &length,
620 "INTERFACE=%d/%d/%d",
621 alt->desc.bInterfaceClass,
622 alt->desc.bInterfaceSubClass,
623 alt->desc.bInterfaceProtocol))
Alan Stern782da722006-07-01 22:09:35 -0400624 return -ENOMEM;
Alan Stern36e56a32006-07-01 22:08:06 -0400625
Alan Stern782da722006-07-01 22:09:35 -0400626 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400627 buffer, buffer_size, &length,
628 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
629 le16_to_cpu(usb_dev->descriptor.idVendor),
630 le16_to_cpu(usb_dev->descriptor.idProduct),
631 le16_to_cpu(usb_dev->descriptor.bcdDevice),
632 usb_dev->descriptor.bDeviceClass,
633 usb_dev->descriptor.bDeviceSubClass,
634 usb_dev->descriptor.bDeviceProtocol,
635 alt->desc.bInterfaceClass,
636 alt->desc.bInterfaceSubClass,
637 alt->desc.bInterfaceProtocol))
Alan Stern782da722006-07-01 22:09:35 -0400638 return -ENOMEM;
639 }
Alan Stern36e56a32006-07-01 22:08:06 -0400640
641 envp[i] = NULL;
642
643 return 0;
644}
645
646#else
647
648static int usb_uevent(struct device *dev, char **envp,
649 int num_envp, char *buffer, int buffer_size)
650{
651 return -ENODEV;
652}
653
654#endif /* CONFIG_HOTPLUG */
655
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800656/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400657 * usb_register_device_driver - register a USB device (not interface) driver
658 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800659 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800660 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400661 * Registers a USB device driver with the USB core. The list of
662 * unattached devices will be rescanned whenever a new driver is
663 * added, allowing the new driver to attach to any recognized devices.
664 * Returns a negative error code on failure and 0 on success.
665 */
666int usb_register_device_driver(struct usb_device_driver *new_udriver,
667 struct module *owner)
668{
669 int retval = 0;
670
671 if (usb_disabled())
672 return -ENODEV;
673
674 new_udriver->drvwrap.for_devices = 1;
675 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
676 new_udriver->drvwrap.driver.bus = &usb_bus_type;
677 new_udriver->drvwrap.driver.probe = usb_probe_device;
678 new_udriver->drvwrap.driver.remove = usb_unbind_device;
679 new_udriver->drvwrap.driver.owner = owner;
680
681 retval = driver_register(&new_udriver->drvwrap.driver);
682
683 if (!retval) {
684 pr_info("%s: registered new device driver %s\n",
685 usbcore_name, new_udriver->name);
686 usbfs_update_special();
687 } else {
688 printk(KERN_ERR "%s: error %d registering device "
689 " driver %s\n",
690 usbcore_name, retval, new_udriver->name);
691 }
692
693 return retval;
694}
695EXPORT_SYMBOL_GPL(usb_register_device_driver);
696
697/**
698 * usb_deregister_device_driver - unregister a USB device (not interface) driver
699 * @udriver: USB operations of the device driver to unregister
700 * Context: must be able to sleep
701 *
702 * Unlinks the specified driver from the internal USB driver list.
703 */
704void usb_deregister_device_driver(struct usb_device_driver *udriver)
705{
706 pr_info("%s: deregistering device driver %s\n",
707 usbcore_name, udriver->name);
708
709 driver_unregister(&udriver->drvwrap.driver);
710 usbfs_update_special();
711}
712EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
713
714/**
715 * usb_register_driver - register a USB interface driver
716 * @new_driver: USB operations for the interface driver
717 * @owner: module owner of this driver.
718 *
719 * Registers a USB interface driver with the USB core. The list of
720 * unattached interfaces will be rescanned whenever a new driver is
721 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800722 * Returns a negative error code on failure and 0 on success.
723 *
724 * NOTE: if you want your driver to use the USB major number, you must call
725 * usb_register_dev() to enable that functionality. This function no longer
726 * takes care of that.
727 */
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800728int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800729{
730 int retval = 0;
731
732 if (usb_disabled())
733 return -ENODEV;
734
Alan Stern8bb54ab2006-07-01 22:08:49 -0400735 new_driver->drvwrap.for_devices = 0;
736 new_driver->drvwrap.driver.name = (char *) new_driver->name;
737 new_driver->drvwrap.driver.bus = &usb_bus_type;
738 new_driver->drvwrap.driver.probe = usb_probe_interface;
739 new_driver->drvwrap.driver.remove = usb_unbind_interface;
740 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800741 spin_lock_init(&new_driver->dynids.lock);
742 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800743
Alan Stern8bb54ab2006-07-01 22:08:49 -0400744 retval = driver_register(&new_driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800745
746 if (!retval) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400747 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800748 usbcore_name, new_driver->name);
749 usbfs_update_special();
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800750 usb_create_newid_file(new_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800751 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400752 printk(KERN_ERR "%s: error %d registering interface "
753 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800754 usbcore_name, retval, new_driver->name);
755 }
756
757 return retval;
758}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800759EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800760
761/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400762 * usb_deregister - unregister a USB interface driver
763 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800764 * Context: must be able to sleep
765 *
766 * Unlinks the specified driver from the internal USB driver list.
767 *
768 * NOTE: If you called usb_register_dev(), you still need to call
769 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
770 * this * call will no longer do it for you.
771 */
772void usb_deregister(struct usb_driver *driver)
773{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400774 pr_info("%s: deregistering interface driver %s\n",
775 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800776
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800777 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800778 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400779 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800780
781 usbfs_update_special();
782}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800783EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400784
785#ifdef CONFIG_PM
786
Alan Stern645daaa2006-08-30 15:47:02 -0400787/* Caller has locked udev->pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400788static int suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -0400789{
Alan Stern782da722006-07-01 22:09:35 -0400790 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400791 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400792
Alan Stern114b3682006-07-01 22:13:04 -0400793 if (udev->state == USB_STATE_NOTATTACHED ||
794 udev->state == USB_STATE_SUSPENDED)
795 goto done;
796
Alan Stern1c5df7e2006-07-01 22:13:50 -0400797 /* For devices that don't have a driver, we do a standard suspend. */
798 if (udev->dev.driver == NULL) {
Alan Stern645daaa2006-08-30 15:47:02 -0400799 udev->do_remote_wakeup = 0;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400800 status = usb_port_suspend(udev);
Alan Stern2bf40862006-07-01 22:12:19 -0400801 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400802 }
803
Alan Stern1cc8a252006-07-01 22:10:15 -0400804 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern2bf40862006-07-01 22:12:19 -0400805 status = udriver->suspend(udev, msg);
806
807done:
Alan Stern645daaa2006-08-30 15:47:02 -0400808 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400809 if (status == 0)
810 udev->dev.power.power_state.event = msg.event;
811 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400812}
Alan Stern36e56a32006-07-01 22:08:06 -0400813
Alan Stern645daaa2006-08-30 15:47:02 -0400814/* Caller has locked udev->pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400815static int resume_device(struct usb_device *udev)
816{
817 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400818 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400819
Alan Stern114b3682006-07-01 22:13:04 -0400820 if (udev->state == USB_STATE_NOTATTACHED ||
821 udev->state != USB_STATE_SUSPENDED)
Alan Stern2bf40862006-07-01 22:12:19 -0400822 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400823
Alan Stern1c5df7e2006-07-01 22:13:50 -0400824 /* Can't resume it if it doesn't have a driver. */
825 if (udev->dev.driver == NULL) {
826 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400827 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400828 }
829
Alan Stern1cc8a252006-07-01 22:10:15 -0400830 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern2bf40862006-07-01 22:12:19 -0400831 status = udriver->resume(udev);
832
833done:
Alan Stern645daaa2006-08-30 15:47:02 -0400834 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400835 if (status == 0)
836 udev->dev.power.power_state.event = PM_EVENT_ON;
837 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400838}
839
Alan Stern645daaa2006-08-30 15:47:02 -0400840/* Caller has locked intf's usb_device's pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400841static int suspend_interface(struct usb_interface *intf, pm_message_t msg)
842{
843 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400844 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400845
Alan Stern114b3682006-07-01 22:13:04 -0400846 /* with no hardware, USB interfaces only use FREEZE and ON states */
847 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
848 !is_active(intf))
849 goto done;
850
Alan Stern645daaa2006-08-30 15:47:02 -0400851 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
Alan Stern2bf40862006-07-01 22:12:19 -0400852 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400853 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400854
Alan Stern36e56a32006-07-01 22:08:06 -0400855 if (driver->suspend && driver->resume) {
Alan Stern1cc8a252006-07-01 22:10:15 -0400856 status = driver->suspend(intf, msg);
Alan Stern645daaa2006-08-30 15:47:02 -0400857 if (status == 0)
858 mark_quiesced(intf);
859 else if (!interface_to_usbdev(intf)->auto_pm)
Alan Stern1cc8a252006-07-01 22:10:15 -0400860 dev_err(&intf->dev, "%s error %d\n",
861 "suspend", status);
Alan Stern36e56a32006-07-01 22:08:06 -0400862 } else {
863 // FIXME else if there's no suspend method, disconnect...
Alan Stern645daaa2006-08-30 15:47:02 -0400864 // Not possible if auto_pm is set...
Alan Stern1cc8a252006-07-01 22:10:15 -0400865 dev_warn(&intf->dev, "no suspend for driver %s?\n",
866 driver->name);
Alan Stern36e56a32006-07-01 22:08:06 -0400867 mark_quiesced(intf);
Alan Stern36e56a32006-07-01 22:08:06 -0400868 }
Alan Stern2bf40862006-07-01 22:12:19 -0400869
870done:
Alan Stern645daaa2006-08-30 15:47:02 -0400871 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400872 if (status == 0)
873 intf->dev.power.power_state.event = msg.event;
Alan Stern36e56a32006-07-01 22:08:06 -0400874 return status;
875}
876
Alan Stern645daaa2006-08-30 15:47:02 -0400877/* Caller has locked intf's usb_device's pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400878static int resume_interface(struct usb_interface *intf)
Alan Stern36e56a32006-07-01 22:08:06 -0400879{
Alan Stern1cc8a252006-07-01 22:10:15 -0400880 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400881 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400882
Alan Stern114b3682006-07-01 22:13:04 -0400883 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
884 is_active(intf))
Alan Stern2bf40862006-07-01 22:12:19 -0400885 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -0400886
Alan Stern645daaa2006-08-30 15:47:02 -0400887 /* Don't let autoresume interfere with unbinding */
888 if (intf->condition == USB_INTERFACE_UNBINDING)
889 goto done;
890
Alan Stern1c5df7e2006-07-01 22:13:50 -0400891 /* Can't resume it if it doesn't have a driver. */
Alan Stern645daaa2006-08-30 15:47:02 -0400892 if (intf->condition == USB_INTERFACE_UNBOUND) {
Alan Stern1c5df7e2006-07-01 22:13:50 -0400893 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400894 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400895 }
Alan Stern1cc8a252006-07-01 22:10:15 -0400896 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400897
Alan Stern36e56a32006-07-01 22:08:06 -0400898 if (driver->resume) {
899 status = driver->resume(intf);
Alan Stern2bf40862006-07-01 22:12:19 -0400900 if (status)
Alan Stern1cc8a252006-07-01 22:10:15 -0400901 dev_err(&intf->dev, "%s error %d\n",
902 "resume", status);
Alan Stern2bf40862006-07-01 22:12:19 -0400903 else
904 mark_active(intf);
905 } else {
Alan Stern1cc8a252006-07-01 22:10:15 -0400906 dev_warn(&intf->dev, "no resume for driver %s?\n",
907 driver->name);
Alan Stern2bf40862006-07-01 22:12:19 -0400908 mark_active(intf);
909 }
910
911done:
Alan Stern645daaa2006-08-30 15:47:02 -0400912 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400913 if (status == 0)
914 intf->dev.power.power_state.event = PM_EVENT_ON;
915 return status;
Alan Stern36e56a32006-07-01 22:08:06 -0400916}
917
Alan Stern645daaa2006-08-30 15:47:02 -0400918/**
919 * usb_suspend_both - suspend a USB device and its interfaces
920 * @udev: the usb_device to suspend
921 * @msg: Power Management message describing this state transition
922 *
923 * This is the central routine for suspending USB devices. It calls the
924 * suspend methods for all the interface drivers in @udev and then calls
925 * the suspend method for @udev itself. If an error occurs at any stage,
926 * all the interfaces which were suspended are resumed so that they remain
927 * in the same state as the device.
928 *
929 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
930 * checks first to make sure that neither the device itself or any of its
931 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
932 * are, the autosuspend fails.
933 *
934 * If the suspend succeeds, the routine recursively queues an autosuspend
935 * request for @udev's parent device, thereby propagating the change up
936 * the device tree. If all of the parent's children are now suspended,
937 * the parent will autosuspend in turn.
938 *
939 * The suspend method calls are subject to mutual exclusion under control
940 * of @udev's pm_mutex. Many of these calls are also under the protection
941 * of @udev's device lock (including all requests originating outside the
942 * USB subsystem), but autosuspend requests generated by a child device or
943 * interface driver may not be. Usbcore will insure that the method calls
944 * do not arrive during bind, unbind, or reset operations. However, drivers
945 * must be prepared to handle suspend calls arriving at unpredictable times.
946 * The only way to block such calls is to do an autoresume (preventing
947 * autosuspends) while holding @udev's device lock (preventing outside
948 * suspends).
949 *
950 * The caller must hold @udev->pm_mutex.
951 *
952 * This routine can run only in process context.
953 */
Alan Sterna8e7c562006-07-01 22:11:02 -0400954int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
955{
956 int status = 0;
957 int i = 0;
958 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400959 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -0400960
Alan Stern645daaa2006-08-30 15:47:02 -0400961 cancel_delayed_work(&udev->autosuspend);
962 if (udev->state == USB_STATE_NOTATTACHED)
963 return 0;
964 if (udev->state == USB_STATE_SUSPENDED)
965 return 0;
966
967 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
968
969 /* For autosuspend, fail fast if anything is in use.
970 * Also fail if any interfaces require remote wakeup but it
971 * isn't available. */
972 if (udev->auto_pm) {
973 if (udev->pm_usage_cnt > 0)
974 return -EBUSY;
975 if (udev->actconfig) {
976 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
977 intf = udev->actconfig->interface[i];
978 if (!is_active(intf))
979 continue;
980 if (intf->pm_usage_cnt > 0)
981 return -EBUSY;
982 if (intf->needs_remote_wakeup &&
983 !udev->do_remote_wakeup) {
984 dev_dbg(&udev->dev,
985 "remote wakeup needed for autosuspend\n");
986 return -EOPNOTSUPP;
987 }
988 }
989 i = 0;
990 }
991 }
992
993 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -0400994 if (udev->actconfig) {
995 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
996 intf = udev->actconfig->interface[i];
997 status = suspend_interface(intf, msg);
998 if (status != 0)
999 break;
1000 }
1001 }
1002 if (status == 0)
1003 status = suspend_device(udev, msg);
1004
1005 /* If the suspend failed, resume interfaces that did get suspended */
1006 if (status != 0) {
1007 while (--i >= 0) {
1008 intf = udev->actconfig->interface[i];
1009 resume_interface(intf);
1010 }
Alan Stern645daaa2006-08-30 15:47:02 -04001011
1012 /* If the suspend succeeded, propagate it up the tree */
1013 } else if (parent)
1014 usb_autosuspend_device(parent, 0);
1015
1016 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001017 return status;
1018}
1019
Alan Stern645daaa2006-08-30 15:47:02 -04001020/**
1021 * usb_resume_both - resume a USB device and its interfaces
1022 * @udev: the usb_device to resume
1023 *
1024 * This is the central routine for resuming USB devices. It calls the
1025 * the resume method for @udev and then calls the resume methods for all
1026 * the interface drivers in @udev.
1027 *
1028 * Before starting the resume, the routine calls itself recursively for
1029 * the parent device of @udev, thereby propagating the change up the device
1030 * tree and assuring that @udev will be able to resume. If the parent is
1031 * unable to resume successfully, the routine fails.
1032 *
1033 * The resume method calls are subject to mutual exclusion under control
1034 * of @udev's pm_mutex. Many of these calls are also under the protection
1035 * of @udev's device lock (including all requests originating outside the
1036 * USB subsystem), but autoresume requests generated by a child device or
1037 * interface driver may not be. Usbcore will insure that the method calls
1038 * do not arrive during bind, unbind, or reset operations. However, drivers
1039 * must be prepared to handle resume calls arriving at unpredictable times.
1040 * The only way to block such calls is to do an autoresume (preventing
1041 * other autoresumes) while holding @udev's device lock (preventing outside
1042 * resumes).
1043 *
1044 * The caller must hold @udev->pm_mutex.
1045 *
1046 * This routine can run only in process context.
1047 */
Alan Sterna8e7c562006-07-01 22:11:02 -04001048int usb_resume_both(struct usb_device *udev)
1049{
Alan Stern645daaa2006-08-30 15:47:02 -04001050 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001051 int i;
1052 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001053 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001054
Alan Stern645daaa2006-08-30 15:47:02 -04001055 cancel_delayed_work(&udev->autosuspend);
1056 if (udev->state == USB_STATE_NOTATTACHED)
1057 return -ENODEV;
1058
1059 /* Propagate the resume up the tree, if necessary */
1060 if (udev->state == USB_STATE_SUSPENDED) {
1061 if (parent) {
1062 mutex_lock_nested(&parent->pm_mutex, parent->level);
1063 parent->auto_pm = 1;
1064 status = usb_resume_both(parent);
1065 } else {
1066
1067 /* We can't progagate beyond the USB subsystem,
1068 * so if a root hub's controller is suspended
1069 * then we're stuck. */
1070 if (udev->dev.parent->power.power_state.event !=
1071 PM_EVENT_ON)
1072 status = -EHOSTUNREACH;
1073 }
1074 if (status == 0 && udev->state == USB_STATE_SUSPENDED)
1075 status = resume_device(udev);
1076 if (parent)
1077 mutex_unlock(&parent->pm_mutex);
Alan Stern114b3682006-07-01 22:13:04 -04001078 }
1079
Alan Stern645daaa2006-08-30 15:47:02 -04001080 /* Now the parent won't suspend until we are finished */
1081
Alan Sterna8e7c562006-07-01 22:11:02 -04001082 if (status == 0 && udev->actconfig) {
1083 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1084 intf = udev->actconfig->interface[i];
1085 resume_interface(intf);
1086 }
1087 }
Alan Stern645daaa2006-08-30 15:47:02 -04001088
1089 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001090 return status;
1091}
1092
Alan Stern645daaa2006-08-30 15:47:02 -04001093#ifdef CONFIG_USB_SUSPEND
1094
1095/**
1096 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1097 * @udev - the usb_device to autosuspend
1098 * @dec_usage_cnt - flag to decrement @udev's PM-usage counter
1099 *
1100 * This routine should be called when a core subsystem is finished using
1101 * @udev and wants to allow it to autosuspend. Examples would be when
1102 * @udev's device file in usbfs is closed or after a configuration change.
1103 *
1104 * @dec_usage_cnt should be 1 if the subsystem previously incremented
1105 * @udev's usage counter (such as by passing 1 to usb_autoresume_device);
1106 * otherwise it should be 0.
1107 *
1108 * If the usage counter for @udev or any of its active interfaces is greater
1109 * than 0, the autosuspend request will not be queued. (If an interface
1110 * driver does not support autosuspend then its usage counter is permanently
1111 * positive.) Likewise, if an interface driver requires remote-wakeup
1112 * capability during autosuspend but remote wakeup is disabled, the
1113 * autosuspend will fail.
1114 *
1115 * Often the caller will hold @udev's device lock, but this is not
1116 * necessary.
1117 *
1118 * This routine can run only in process context.
1119 */
1120void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt)
1121{
1122 mutex_lock_nested(&udev->pm_mutex, udev->level);
1123 udev->pm_usage_cnt -= dec_usage_cnt;
1124 if (udev->pm_usage_cnt <= 0)
1125 schedule_delayed_work(&udev->autosuspend,
1126 USB_AUTOSUSPEND_DELAY);
1127 mutex_unlock(&udev->pm_mutex);
1128 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1129 // __FUNCTION__, udev->pm_usage_cnt);
1130}
1131
1132/**
1133 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1134 * @udev - the usb_device to autoresume
1135 * @inc_usage_cnt - flag to increment @udev's PM-usage counter
1136 *
1137 * This routine should be called when a core subsystem wants to use @udev
1138 * and needs to guarantee that it is not suspended. In addition, the
1139 * caller can prevent @udev from being autosuspended subsequently. (Note
1140 * that this will not prevent suspend events originating in the PM core.)
1141 * Examples would be when @udev's device file in usbfs is opened (autosuspend
1142 * should be prevented until the file is closed) or when a remote-wakeup
1143 * request is received (later autosuspends should not be prevented).
1144 *
1145 * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent
1146 * autosuspends. This prevention will persist until the usage counter is
1147 * decremented again (such as by passing 1 to usb_autosuspend_device).
1148 * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged.
1149 * Regardless, if the autoresume fails then the usage counter is not
1150 * incremented.
1151 *
1152 * Often the caller will hold @udev's device lock, but this is not
1153 * necessary (and attempting it might cause deadlock).
1154 *
1155 * This routine can run only in process context.
1156 */
1157int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt)
1158{
1159 int status;
1160
1161 mutex_lock_nested(&udev->pm_mutex, udev->level);
1162 udev->pm_usage_cnt += inc_usage_cnt;
1163 udev->auto_pm = 1;
1164 status = usb_resume_both(udev);
1165 if (status != 0)
1166 udev->pm_usage_cnt -= inc_usage_cnt;
1167 mutex_unlock(&udev->pm_mutex);
1168 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1169 // __FUNCTION__, status, udev->pm_usage_cnt);
1170 return status;
1171}
1172
1173/**
1174 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1175 * @intf - the usb_interface whose counter should be decremented
1176 *
1177 * This routine should be called by an interface driver when it is
1178 * finished using @intf and wants to allow it to autosuspend. A typical
1179 * example would be a character-device driver when its device file is
1180 * closed.
1181 *
1182 * The routine decrements @intf's usage counter. When the counter reaches
1183 * 0, a delayed autosuspend request for @intf's device is queued. When
1184 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1185 * the other usage counters for the sibling interfaces and @intf's
1186 * usb_device, the device and all its interfaces will be autosuspended.
1187 *
1188 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1189 * core will not change its value other than the increment and decrement
1190 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1191 * may use this simple counter-oriented discipline or may set the value
1192 * any way it likes.
1193 *
1194 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1195 * take place only if the device's remote-wakeup facility is enabled.
1196 *
1197 * Suspend method calls queued by this routine can arrive at any time
1198 * while @intf is resumed and its usage counter is equal to 0. They are
1199 * not protected by the usb_device's lock but only by its pm_mutex.
1200 * Drivers must provide their own synchronization.
1201 *
1202 * This routine can run only in process context.
1203 */
1204void usb_autopm_put_interface(struct usb_interface *intf)
1205{
1206 struct usb_device *udev = interface_to_usbdev(intf);
1207
1208 mutex_lock_nested(&udev->pm_mutex, udev->level);
1209 if (intf->condition != USB_INTERFACE_UNBOUND) {
1210 if (--intf->pm_usage_cnt <= 0)
1211 schedule_delayed_work(&udev->autosuspend,
1212 USB_AUTOSUSPEND_DELAY);
1213 }
1214 mutex_unlock(&udev->pm_mutex);
1215 // dev_dbg(&intf->dev, "%s: cnt %d\n",
1216 // __FUNCTION__, intf->pm_usage_cnt);
1217}
1218EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1219
1220/**
1221 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1222 * @intf - the usb_interface whose counter should be incremented
1223 *
1224 * This routine should be called by an interface driver when it wants to
1225 * use @intf and needs to guarantee that it is not suspended. In addition,
1226 * the routine prevents @intf from being autosuspended subsequently. (Note
1227 * that this will not prevent suspend events originating in the PM core.)
1228 * This prevention will persist until usb_autopm_put_interface() is called
1229 * or @intf is unbound. A typical example would be a character-device
1230 * driver when its device file is opened.
1231 *
1232 * The routine increments @intf's usage counter. So long as the counter
1233 * is greater than 0, autosuspend will not be allowed for @intf or its
1234 * usb_device. When the driver is finished using @intf it should call
1235 * usb_autopm_put_interface() to decrement the usage counter and queue
1236 * a delayed autosuspend request (if the counter is <= 0).
1237 *
1238 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1239 * core will not change its value other than the increment and decrement
1240 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1241 * may use this simple counter-oriented discipline or may set the value
1242 * any way it likes.
1243 *
1244 * Resume method calls generated by this routine can arrive at any time
1245 * while @intf is suspended. They are not protected by the usb_device's
1246 * lock but only by its pm_mutex. Drivers must provide their own
1247 * synchronization.
1248 *
1249 * This routine can run only in process context.
1250 */
1251int usb_autopm_get_interface(struct usb_interface *intf)
1252{
1253 struct usb_device *udev = interface_to_usbdev(intf);
1254 int status;
1255
1256 mutex_lock_nested(&udev->pm_mutex, udev->level);
1257 if (intf->condition == USB_INTERFACE_UNBOUND)
1258 status = -ENODEV;
1259 else {
1260 ++intf->pm_usage_cnt;
1261 udev->auto_pm = 1;
1262 status = usb_resume_both(udev);
1263 if (status != 0)
1264 --intf->pm_usage_cnt;
1265 }
1266 mutex_unlock(&udev->pm_mutex);
1267 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1268 // __FUNCTION__, status, intf->pm_usage_cnt);
1269 return status;
1270}
1271EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1272
1273#endif /* CONFIG_USB_SUSPEND */
1274
Alan Stern1cc8a252006-07-01 22:10:15 -04001275static int usb_suspend(struct device *dev, pm_message_t message)
1276{
1277 int status;
1278
Alan Stern645daaa2006-08-30 15:47:02 -04001279 if (is_usb_device(dev)) {
1280 struct usb_device *udev = to_usb_device(dev);
1281
1282 mutex_lock_nested(&udev->pm_mutex, udev->level);
1283 udev->auto_pm = 0;
1284 status = usb_suspend_both(udev, message);
1285 mutex_unlock(&udev->pm_mutex);
1286 } else
Alan Sterna8e7c562006-07-01 22:11:02 -04001287 status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001288 return status;
1289}
1290
1291static int usb_resume(struct device *dev)
1292{
1293 int status;
1294
Alan Sterna8e7c562006-07-01 22:11:02 -04001295 if (is_usb_device(dev)) {
Alan Stern645daaa2006-08-30 15:47:02 -04001296 struct usb_device *udev = to_usb_device(dev);
1297
1298 mutex_lock_nested(&udev->pm_mutex, udev->level);
1299 udev->auto_pm = 0;
1300 status = usb_resume_both(udev);
1301 mutex_unlock(&udev->pm_mutex);
Alan Sterna8e7c562006-07-01 22:11:02 -04001302
1303 /* Rebind drivers that had no suspend method? */
1304 } else
1305 status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001306 return status;
1307}
1308
Alan Stern36e56a32006-07-01 22:08:06 -04001309#endif /* CONFIG_PM */
1310
1311struct bus_type usb_bus_type = {
1312 .name = "usb",
1313 .match = usb_device_match,
1314 .uevent = usb_uevent,
1315#ifdef CONFIG_PM
Alan Stern782da722006-07-01 22:09:35 -04001316 .suspend = usb_suspend,
1317 .resume = usb_resume,
Alan Stern36e56a32006-07-01 22:08:06 -04001318#endif
1319};