blob: b10463244413c276846bbdcaebe58a0026ba4cbd [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;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070052 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080053
54 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
55 if (fields < 2)
56 return -EINVAL;
57
58 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
59 if (!dynid)
60 return -ENOMEM;
61
62 INIT_LIST_HEAD(&dynid->node);
63 dynid->id.idVendor = idVendor;
64 dynid->id.idProduct = idProduct;
65 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
66
67 spin_lock(&usb_drv->dynids.lock);
68 list_add_tail(&usb_drv->dynids.list, &dynid->node);
69 spin_unlock(&usb_drv->dynids.lock);
70
71 if (get_driver(driver)) {
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070072 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080073 put_driver(driver);
74 }
75
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070076 if (retval)
77 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080078 return count;
79}
80static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
81
82static int usb_create_newid_file(struct usb_driver *usb_drv)
83{
84 int error = 0;
85
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080086 if (usb_drv->no_dynamic_id)
87 goto exit;
88
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080089 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -040090 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080091 &driver_attr_new_id.attr);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080092exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080093 return error;
94}
95
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080096static void usb_remove_newid_file(struct usb_driver *usb_drv)
97{
98 if (usb_drv->no_dynamic_id)
99 return;
100
101 if (usb_drv->probe != NULL)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400102 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800103 &driver_attr_new_id.attr);
104}
105
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800106static void usb_free_dynids(struct usb_driver *usb_drv)
107{
108 struct usb_dynid *dynid, *n;
109
110 spin_lock(&usb_drv->dynids.lock);
111 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
112 list_del(&dynid->node);
113 kfree(dynid);
114 }
115 spin_unlock(&usb_drv->dynids.lock);
116}
117#else
118static inline int usb_create_newid_file(struct usb_driver *usb_drv)
119{
120 return 0;
121}
122
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800123static void usb_remove_newid_file(struct usb_driver *usb_drv)
124{
125}
126
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800127static inline void usb_free_dynids(struct usb_driver *usb_drv)
128{
129}
130#endif
131
132static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
133 struct usb_driver *drv)
134{
135 struct usb_dynid *dynid;
136
137 spin_lock(&drv->dynids.lock);
138 list_for_each_entry(dynid, &drv->dynids.list, node) {
139 if (usb_match_one_id(intf, &dynid->id)) {
140 spin_unlock(&drv->dynids.lock);
141 return &dynid->id;
142 }
143 }
144 spin_unlock(&drv->dynids.lock);
145 return NULL;
146}
147
148
Alan Stern8bb54ab2006-07-01 22:08:49 -0400149/* called from driver core with dev locked */
150static int usb_probe_device(struct device *dev)
151{
152 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
153 struct usb_device *udev;
154 int error = -ENODEV;
155
156 dev_dbg(dev, "%s\n", __FUNCTION__);
157
158 if (!is_usb_device(dev)) /* Sanity check */
159 return error;
160
161 udev = to_usb_device(dev);
162
Alan Stern8bb54ab2006-07-01 22:08:49 -0400163 /* TODO: Add real matching code */
164
Alan Stern645daaa2006-08-30 15:47:02 -0400165 /* The device should always appear to be in use
166 * unless the driver suports autosuspend.
167 */
168 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
169
Alan Stern8bb54ab2006-07-01 22:08:49 -0400170 error = udriver->probe(udev);
171 return error;
172}
173
174/* called from driver core with dev locked */
175static int usb_unbind_device(struct device *dev)
176{
177 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
178
179 udriver->disconnect(to_usb_device(dev));
180 return 0;
181}
182
183
184/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800185static int usb_probe_interface(struct device *dev)
186{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400187 struct usb_driver *driver = to_usb_driver(dev->driver);
188 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400189 struct usb_device *udev;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800190 const struct usb_device_id *id;
191 int error = -ENODEV;
192
193 dev_dbg(dev, "%s\n", __FUNCTION__);
194
Alan Stern8bb54ab2006-07-01 22:08:49 -0400195 if (is_usb_device(dev)) /* Sanity check */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800196 return error;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400197
198 intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400199 udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800200
201 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800202 if (!id)
203 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800204 if (id) {
205 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
206
Alan Stern645daaa2006-08-30 15:47:02 -0400207 error = usb_autoresume_device(udev, 1);
208 if (error)
209 return error;
210
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800211 /* Interface "power state" doesn't correspond to any hardware
212 * state whatsoever. We use it to record when it's bound to
213 * a driver that may start I/0: it's not frozen/quiesced.
214 */
215 mark_active(intf);
216 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400217
218 /* The interface should always appear to be in use
219 * unless the driver suports autosuspend.
220 */
221 intf->pm_usage_cnt = !(driver->supports_autosuspend);
222
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800223 error = driver->probe(intf, id);
224 if (error) {
225 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400226 intf->needs_remote_wakeup = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800227 intf->condition = USB_INTERFACE_UNBOUND;
228 } else
229 intf->condition = USB_INTERFACE_BOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400230
231 usb_autosuspend_device(udev, 1);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800232 }
233
234 return error;
235}
236
Alan Stern8bb54ab2006-07-01 22:08:49 -0400237/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800238static int usb_unbind_interface(struct device *dev)
239{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400240 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800241 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400242 struct usb_device *udev;
243 int error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800244
245 intf->condition = USB_INTERFACE_UNBINDING;
246
Alan Stern645daaa2006-08-30 15:47:02 -0400247 /* Autoresume for set_interface call below */
248 udev = interface_to_usbdev(intf);
249 error = usb_autoresume_device(udev, 1);
250
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800251 /* release all urbs for this interface */
252 usb_disable_interface(interface_to_usbdev(intf), intf);
253
Alan Stern8bb54ab2006-07-01 22:08:49 -0400254 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800255
256 /* reset other interface state */
257 usb_set_interface(interface_to_usbdev(intf),
258 intf->altsetting[0].desc.bInterfaceNumber,
259 0);
260 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400261
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800262 intf->condition = USB_INTERFACE_UNBOUND;
263 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400264 intf->needs_remote_wakeup = 0;
265
266 if (!error)
267 usb_autosuspend_device(udev, 1);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800268
269 return 0;
270}
271
Alan Stern36e56a32006-07-01 22:08:06 -0400272/**
273 * usb_driver_claim_interface - bind a driver to an interface
274 * @driver: the driver to be bound
275 * @iface: the interface to which it will be bound; must be in the
276 * usb device's active configuration
277 * @priv: driver data associated with that interface
278 *
279 * This is used by usb device drivers that need to claim more than one
280 * interface on a device when probing (audio and acm are current examples).
281 * No device driver should directly modify internal usb_interface or
282 * usb_device structure members.
283 *
284 * Few drivers should need to use this routine, since the most natural
285 * way to bind to an interface is to return the private data from
286 * the driver's probe() method.
287 *
288 * Callers must own the device lock and the driver model's usb_bus_type.subsys
289 * writelock. So driver probe() entries don't need extra locking,
290 * but other call contexts may need to explicitly claim those locks.
291 */
292int usb_driver_claim_interface(struct usb_driver *driver,
293 struct usb_interface *iface, void* priv)
294{
295 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400296 struct usb_device *udev = interface_to_usbdev(iface);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700297 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400298
299 if (dev->driver)
300 return -EBUSY;
301
Alan Stern8bb54ab2006-07-01 22:08:49 -0400302 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400303 usb_set_intfdata(iface, priv);
Alan Stern645daaa2006-08-30 15:47:02 -0400304
305 mutex_lock_nested(&udev->pm_mutex, udev->level);
Alan Stern36e56a32006-07-01 22:08:06 -0400306 iface->condition = USB_INTERFACE_BOUND;
307 mark_active(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400308 iface->pm_usage_cnt = !(driver->supports_autosuspend);
309 mutex_unlock(&udev->pm_mutex);
Alan Stern36e56a32006-07-01 22:08:06 -0400310
311 /* if interface was already added, bind now; else let
312 * the future device_add() bind it, bypassing probe()
313 */
314 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700315 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400316
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700317 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400318}
319EXPORT_SYMBOL(usb_driver_claim_interface);
320
321/**
322 * usb_driver_release_interface - unbind a driver from an interface
323 * @driver: the driver to be unbound
324 * @iface: the interface from which it will be unbound
325 *
326 * This can be used by drivers to release an interface without waiting
327 * for their disconnect() methods to be called. In typical cases this
328 * also causes the driver disconnect() method to be called.
329 *
330 * This call is synchronous, and may not be used in an interrupt context.
331 * Callers must own the device lock and the driver model's usb_bus_type.subsys
332 * writelock. So driver disconnect() entries don't need extra locking,
333 * but other call contexts may need to explicitly claim those locks.
334 */
335void usb_driver_release_interface(struct usb_driver *driver,
336 struct usb_interface *iface)
337{
338 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400339 struct usb_device *udev = interface_to_usbdev(iface);
Alan Stern36e56a32006-07-01 22:08:06 -0400340
341 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400342 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400343 return;
344
345 /* don't release from within disconnect() */
346 if (iface->condition != USB_INTERFACE_BOUND)
347 return;
348
349 /* don't release if the interface hasn't been added yet */
350 if (device_is_registered(dev)) {
351 iface->condition = USB_INTERFACE_UNBINDING;
352 device_release_driver(dev);
353 }
354
355 dev->driver = NULL;
356 usb_set_intfdata(iface, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400357
358 mutex_lock_nested(&udev->pm_mutex, udev->level);
Alan Stern36e56a32006-07-01 22:08:06 -0400359 iface->condition = USB_INTERFACE_UNBOUND;
360 mark_quiesced(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400361 iface->needs_remote_wakeup = 0;
362 mutex_unlock(&udev->pm_mutex);
Alan Stern36e56a32006-07-01 22:08:06 -0400363}
364EXPORT_SYMBOL(usb_driver_release_interface);
365
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800366/* returns 0 if no match, 1 if match */
367static int usb_match_one_id(struct usb_interface *interface,
368 const struct usb_device_id *id)
369{
370 struct usb_host_interface *intf;
371 struct usb_device *dev;
372
373 /* proc_connectinfo in devio.c may call us with id == NULL. */
374 if (id == NULL)
375 return 0;
376
377 intf = interface->cur_altsetting;
378 dev = interface_to_usbdev(interface);
379
380 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
381 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
382 return 0;
383
384 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
385 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
386 return 0;
387
388 /* No need to test id->bcdDevice_lo != 0, since 0 is never
389 greater than any unsigned number. */
390 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
391 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
392 return 0;
393
394 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
395 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
396 return 0;
397
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
399 (id->bDeviceClass != dev->descriptor.bDeviceClass))
400 return 0;
401
402 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
403 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
404 return 0;
405
406 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
407 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
408 return 0;
409
410 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
411 (id->bInterfaceClass != intf->desc.bInterfaceClass))
412 return 0;
413
414 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
415 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
416 return 0;
417
418 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
419 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
420 return 0;
421
422 return 1;
423}
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800424/**
425 * usb_match_id - find first usb_device_id matching device or interface
426 * @interface: the interface of interest
427 * @id: array of usb_device_id structures, terminated by zero entry
428 *
429 * usb_match_id searches an array of usb_device_id's and returns
430 * the first one matching the device or interface, or null.
431 * This is used when binding (or rebinding) a driver to an interface.
432 * Most USB device drivers will use this indirectly, through the usb core,
433 * but some layered driver frameworks use it directly.
434 * These device tables are exported with MODULE_DEVICE_TABLE, through
435 * modutils, to support the driver loading functionality of USB hotplugging.
436 *
437 * What Matches:
438 *
439 * The "match_flags" element in a usb_device_id controls which
440 * members are used. If the corresponding bit is set, the
441 * value in the device_id must match its corresponding member
442 * in the device or interface descriptor, or else the device_id
443 * does not match.
444 *
445 * "driver_info" is normally used only by device drivers,
446 * but you can create a wildcard "matches anything" usb_device_id
447 * as a driver's "modules.usbmap" entry if you provide an id with
448 * only a nonzero "driver_info" field. If you do this, the USB device
449 * driver's probe() routine should use additional intelligence to
450 * decide whether to bind to the specified interface.
451 *
452 * What Makes Good usb_device_id Tables:
453 *
454 * The match algorithm is very simple, so that intelligence in
455 * driver selection must come from smart driver id records.
456 * Unless you have good reasons to use another selection policy,
457 * provide match elements only in related groups, and order match
458 * specifiers from specific to general. Use the macros provided
459 * for that purpose if you can.
460 *
461 * The most specific match specifiers use device descriptor
462 * data. These are commonly used with product-specific matches;
463 * the USB_DEVICE macro lets you provide vendor and product IDs,
464 * and you can also match against ranges of product revisions.
465 * These are widely used for devices with application or vendor
466 * specific bDeviceClass values.
467 *
468 * Matches based on device class/subclass/protocol specifications
469 * are slightly more general; use the USB_DEVICE_INFO macro, or
470 * its siblings. These are used with single-function devices
471 * where bDeviceClass doesn't specify that each interface has
472 * its own class.
473 *
474 * Matches based on interface class/subclass/protocol are the
475 * most general; they let drivers bind to any interface on a
476 * multiple-function device. Use the USB_INTERFACE_INFO
477 * macro, or its siblings, to match class-per-interface style
478 * devices (as recorded in bDeviceClass).
479 *
480 * Within those groups, remember that not all combinations are
481 * meaningful. For example, don't give a product version range
482 * without vendor and product IDs; or specify a protocol without
483 * its associated class and subclass.
484 */
485const struct usb_device_id *usb_match_id(struct usb_interface *interface,
486 const struct usb_device_id *id)
487{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800488 /* proc_connectinfo in devio.c may call us with id == NULL. */
489 if (id == NULL)
490 return NULL;
491
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800492 /* It is important to check that id->driver_info is nonzero,
493 since an entry that is all zeroes except for a nonzero
494 id->driver_info is the way to create an entry that
495 indicates that the driver want to examine every
496 device and interface. */
497 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
498 id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800499 if (usb_match_one_id(interface, id))
500 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800501 }
502
503 return NULL;
504}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800505EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800506
507int usb_device_match(struct device *dev, struct device_driver *drv)
508{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400509 /* devices and interfaces are handled separately */
510 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800511
Alan Stern8bb54ab2006-07-01 22:08:49 -0400512 /* interface drivers never match devices */
513 if (!is_usb_device_driver(drv))
514 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800515
Alan Stern8bb54ab2006-07-01 22:08:49 -0400516 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800517 return 1;
518
Alan Stern8bb54ab2006-07-01 22:08:49 -0400519 } else {
520 struct usb_interface *intf;
521 struct usb_driver *usb_drv;
522 const struct usb_device_id *id;
523
524 /* device drivers never match interfaces */
525 if (is_usb_device_driver(drv))
526 return 0;
527
528 intf = to_usb_interface(dev);
529 usb_drv = to_usb_driver(drv);
530
531 id = usb_match_id(intf, usb_drv->id_table);
532 if (id)
533 return 1;
534
535 id = usb_match_dynamic_id(intf, usb_drv);
536 if (id)
537 return 1;
538 }
539
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800540 return 0;
541}
542
Alan Stern36e56a32006-07-01 22:08:06 -0400543#ifdef CONFIG_HOTPLUG
544
545/*
546 * This sends an uevent to userspace, typically helping to load driver
547 * or other modules, configure the device, and more. Drivers can provide
548 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
549 *
550 * We're called either from khubd (the typical case) or from root hub
551 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
552 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
553 * device (and this configuration!) are still present.
554 */
555static int usb_uevent(struct device *dev, char **envp, int num_envp,
556 char *buffer, int buffer_size)
557{
558 struct usb_interface *intf;
559 struct usb_device *usb_dev;
560 struct usb_host_interface *alt;
561 int i = 0;
562 int length = 0;
563
564 if (!dev)
565 return -ENODEV;
566
567 /* driver is often null here; dev_dbg() would oops */
568 pr_debug ("usb %s: uevent\n", dev->bus_id);
569
Alan Stern782da722006-07-01 22:09:35 -0400570 if (is_usb_device(dev)) {
571 usb_dev = to_usb_device(dev);
572 alt = NULL;
573 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400574 intf = to_usb_interface(dev);
575 usb_dev = interface_to_usbdev(intf);
576 alt = intf->cur_altsetting;
577 }
Alan Stern36e56a32006-07-01 22:08:06 -0400578
579 if (usb_dev->devnum < 0) {
580 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
581 return -ENODEV;
582 }
583 if (!usb_dev->bus) {
584 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
585 return -ENODEV;
586 }
587
588#ifdef CONFIG_USB_DEVICEFS
589 /* If this is available, userspace programs can directly read
590 * all the device descriptors we don't tell them about. Or
591 * even act as usermode drivers.
592 *
593 * FIXME reduce hardwired intelligence here
594 */
595 if (add_uevent_var(envp, num_envp, &i,
596 buffer, buffer_size, &length,
597 "DEVICE=/proc/bus/usb/%03d/%03d",
598 usb_dev->bus->busnum, usb_dev->devnum))
599 return -ENOMEM;
600#endif
601
602 /* per-device configurations are common */
603 if (add_uevent_var(envp, num_envp, &i,
604 buffer, buffer_size, &length,
605 "PRODUCT=%x/%x/%x",
606 le16_to_cpu(usb_dev->descriptor.idVendor),
607 le16_to_cpu(usb_dev->descriptor.idProduct),
608 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
609 return -ENOMEM;
610
611 /* class-based driver binding models */
612 if (add_uevent_var(envp, num_envp, &i,
613 buffer, buffer_size, &length,
614 "TYPE=%d/%d/%d",
615 usb_dev->descriptor.bDeviceClass,
616 usb_dev->descriptor.bDeviceSubClass,
617 usb_dev->descriptor.bDeviceProtocol))
618 return -ENOMEM;
619
Alan Stern782da722006-07-01 22:09:35 -0400620 if (!is_usb_device(dev)) {
621
622 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400623 buffer, buffer_size, &length,
624 "INTERFACE=%d/%d/%d",
625 alt->desc.bInterfaceClass,
626 alt->desc.bInterfaceSubClass,
627 alt->desc.bInterfaceProtocol))
Alan Stern782da722006-07-01 22:09:35 -0400628 return -ENOMEM;
Alan Stern36e56a32006-07-01 22:08:06 -0400629
Alan Stern782da722006-07-01 22:09:35 -0400630 if (add_uevent_var(envp, num_envp, &i,
Alan Stern36e56a32006-07-01 22:08:06 -0400631 buffer, buffer_size, &length,
632 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
633 le16_to_cpu(usb_dev->descriptor.idVendor),
634 le16_to_cpu(usb_dev->descriptor.idProduct),
635 le16_to_cpu(usb_dev->descriptor.bcdDevice),
636 usb_dev->descriptor.bDeviceClass,
637 usb_dev->descriptor.bDeviceSubClass,
638 usb_dev->descriptor.bDeviceProtocol,
639 alt->desc.bInterfaceClass,
640 alt->desc.bInterfaceSubClass,
641 alt->desc.bInterfaceProtocol))
Alan Stern782da722006-07-01 22:09:35 -0400642 return -ENOMEM;
643 }
Alan Stern36e56a32006-07-01 22:08:06 -0400644
645 envp[i] = NULL;
646
647 return 0;
648}
649
650#else
651
652static int usb_uevent(struct device *dev, char **envp,
653 int num_envp, char *buffer, int buffer_size)
654{
655 return -ENODEV;
656}
657
658#endif /* CONFIG_HOTPLUG */
659
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800660/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400661 * usb_register_device_driver - register a USB device (not interface) driver
662 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800663 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800664 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400665 * Registers a USB device driver with the USB core. The list of
666 * unattached devices will be rescanned whenever a new driver is
667 * added, allowing the new driver to attach to any recognized devices.
668 * Returns a negative error code on failure and 0 on success.
669 */
670int usb_register_device_driver(struct usb_device_driver *new_udriver,
671 struct module *owner)
672{
673 int retval = 0;
674
675 if (usb_disabled())
676 return -ENODEV;
677
678 new_udriver->drvwrap.for_devices = 1;
679 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
680 new_udriver->drvwrap.driver.bus = &usb_bus_type;
681 new_udriver->drvwrap.driver.probe = usb_probe_device;
682 new_udriver->drvwrap.driver.remove = usb_unbind_device;
683 new_udriver->drvwrap.driver.owner = owner;
684
685 retval = driver_register(&new_udriver->drvwrap.driver);
686
687 if (!retval) {
688 pr_info("%s: registered new device driver %s\n",
689 usbcore_name, new_udriver->name);
690 usbfs_update_special();
691 } else {
692 printk(KERN_ERR "%s: error %d registering device "
693 " driver %s\n",
694 usbcore_name, retval, new_udriver->name);
695 }
696
697 return retval;
698}
699EXPORT_SYMBOL_GPL(usb_register_device_driver);
700
701/**
702 * usb_deregister_device_driver - unregister a USB device (not interface) driver
703 * @udriver: USB operations of the device driver to unregister
704 * Context: must be able to sleep
705 *
706 * Unlinks the specified driver from the internal USB driver list.
707 */
708void usb_deregister_device_driver(struct usb_device_driver *udriver)
709{
710 pr_info("%s: deregistering device driver %s\n",
711 usbcore_name, udriver->name);
712
713 driver_unregister(&udriver->drvwrap.driver);
714 usbfs_update_special();
715}
716EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
717
718/**
719 * usb_register_driver - register a USB interface driver
720 * @new_driver: USB operations for the interface driver
721 * @owner: module owner of this driver.
722 *
723 * Registers a USB interface driver with the USB core. The list of
724 * unattached interfaces will be rescanned whenever a new driver is
725 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800726 * Returns a negative error code on failure and 0 on success.
727 *
728 * NOTE: if you want your driver to use the USB major number, you must call
729 * usb_register_dev() to enable that functionality. This function no longer
730 * takes care of that.
731 */
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800732int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800733{
734 int retval = 0;
735
736 if (usb_disabled())
737 return -ENODEV;
738
Alan Stern8bb54ab2006-07-01 22:08:49 -0400739 new_driver->drvwrap.for_devices = 0;
740 new_driver->drvwrap.driver.name = (char *) new_driver->name;
741 new_driver->drvwrap.driver.bus = &usb_bus_type;
742 new_driver->drvwrap.driver.probe = usb_probe_interface;
743 new_driver->drvwrap.driver.remove = usb_unbind_interface;
744 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800745 spin_lock_init(&new_driver->dynids.lock);
746 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800747
Alan Stern8bb54ab2006-07-01 22:08:49 -0400748 retval = driver_register(&new_driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800749
750 if (!retval) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400751 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800752 usbcore_name, new_driver->name);
753 usbfs_update_special();
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800754 usb_create_newid_file(new_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800755 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400756 printk(KERN_ERR "%s: error %d registering interface "
757 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800758 usbcore_name, retval, new_driver->name);
759 }
760
761 return retval;
762}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800763EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800764
765/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400766 * usb_deregister - unregister a USB interface driver
767 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800768 * Context: must be able to sleep
769 *
770 * Unlinks the specified driver from the internal USB driver list.
771 *
772 * NOTE: If you called usb_register_dev(), you still need to call
773 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
774 * this * call will no longer do it for you.
775 */
776void usb_deregister(struct usb_driver *driver)
777{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400778 pr_info("%s: deregistering interface driver %s\n",
779 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800780
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800781 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800782 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400783 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800784
785 usbfs_update_special();
786}
Greg Kroah-Hartmanb87ba0a2006-03-20 13:17:13 -0800787EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400788
789#ifdef CONFIG_PM
790
Alan Stern645daaa2006-08-30 15:47:02 -0400791/* Caller has locked udev->pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400792static int suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -0400793{
Alan Stern782da722006-07-01 22:09:35 -0400794 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400795 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400796
Alan Stern114b3682006-07-01 22:13:04 -0400797 if (udev->state == USB_STATE_NOTATTACHED ||
798 udev->state == USB_STATE_SUSPENDED)
799 goto done;
800
Alan Stern1c5df7e2006-07-01 22:13:50 -0400801 /* For devices that don't have a driver, we do a standard suspend. */
802 if (udev->dev.driver == NULL) {
Alan Stern645daaa2006-08-30 15:47:02 -0400803 udev->do_remote_wakeup = 0;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400804 status = usb_port_suspend(udev);
Alan Stern2bf40862006-07-01 22:12:19 -0400805 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400806 }
807
Alan Stern1cc8a252006-07-01 22:10:15 -0400808 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern2bf40862006-07-01 22:12:19 -0400809 status = udriver->suspend(udev, msg);
810
811done:
Alan Stern645daaa2006-08-30 15:47:02 -0400812 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400813 if (status == 0)
814 udev->dev.power.power_state.event = msg.event;
815 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400816}
Alan Stern36e56a32006-07-01 22:08:06 -0400817
Alan Stern645daaa2006-08-30 15:47:02 -0400818/* Caller has locked udev->pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400819static int resume_device(struct usb_device *udev)
820{
821 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400822 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400823
Alan Stern114b3682006-07-01 22:13:04 -0400824 if (udev->state == USB_STATE_NOTATTACHED ||
825 udev->state != USB_STATE_SUSPENDED)
Alan Stern2bf40862006-07-01 22:12:19 -0400826 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400827
Alan Stern1c5df7e2006-07-01 22:13:50 -0400828 /* Can't resume it if it doesn't have a driver. */
829 if (udev->dev.driver == NULL) {
830 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400831 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400832 }
833
Alan Stern1cc8a252006-07-01 22:10:15 -0400834 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern2bf40862006-07-01 22:12:19 -0400835 status = udriver->resume(udev);
836
837done:
Alan Stern645daaa2006-08-30 15:47:02 -0400838 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400839 if (status == 0)
840 udev->dev.power.power_state.event = PM_EVENT_ON;
841 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400842}
843
Alan Stern645daaa2006-08-30 15:47:02 -0400844/* Caller has locked intf's usb_device's pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400845static int suspend_interface(struct usb_interface *intf, pm_message_t msg)
846{
847 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400848 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400849
Alan Stern114b3682006-07-01 22:13:04 -0400850 /* with no hardware, USB interfaces only use FREEZE and ON states */
851 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
852 !is_active(intf))
853 goto done;
854
Alan Stern645daaa2006-08-30 15:47:02 -0400855 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
Alan Stern2bf40862006-07-01 22:12:19 -0400856 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400857 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400858
Alan Stern36e56a32006-07-01 22:08:06 -0400859 if (driver->suspend && driver->resume) {
Alan Stern1cc8a252006-07-01 22:10:15 -0400860 status = driver->suspend(intf, msg);
Alan Stern645daaa2006-08-30 15:47:02 -0400861 if (status == 0)
862 mark_quiesced(intf);
863 else if (!interface_to_usbdev(intf)->auto_pm)
Alan Stern1cc8a252006-07-01 22:10:15 -0400864 dev_err(&intf->dev, "%s error %d\n",
865 "suspend", status);
Alan Stern36e56a32006-07-01 22:08:06 -0400866 } else {
867 // FIXME else if there's no suspend method, disconnect...
Alan Stern645daaa2006-08-30 15:47:02 -0400868 // Not possible if auto_pm is set...
Alan Stern1cc8a252006-07-01 22:10:15 -0400869 dev_warn(&intf->dev, "no suspend for driver %s?\n",
870 driver->name);
Alan Stern36e56a32006-07-01 22:08:06 -0400871 mark_quiesced(intf);
Alan Stern36e56a32006-07-01 22:08:06 -0400872 }
Alan Stern2bf40862006-07-01 22:12:19 -0400873
874done:
Alan Stern645daaa2006-08-30 15:47:02 -0400875 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400876 if (status == 0)
877 intf->dev.power.power_state.event = msg.event;
Alan Stern36e56a32006-07-01 22:08:06 -0400878 return status;
879}
880
Alan Stern645daaa2006-08-30 15:47:02 -0400881/* Caller has locked intf's usb_device's pm_mutex */
Alan Stern1cc8a252006-07-01 22:10:15 -0400882static int resume_interface(struct usb_interface *intf)
Alan Stern36e56a32006-07-01 22:08:06 -0400883{
Alan Stern1cc8a252006-07-01 22:10:15 -0400884 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400885 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400886
Alan Stern114b3682006-07-01 22:13:04 -0400887 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
888 is_active(intf))
Alan Stern2bf40862006-07-01 22:12:19 -0400889 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -0400890
Alan Stern645daaa2006-08-30 15:47:02 -0400891 /* Don't let autoresume interfere with unbinding */
892 if (intf->condition == USB_INTERFACE_UNBINDING)
893 goto done;
894
Alan Stern1c5df7e2006-07-01 22:13:50 -0400895 /* Can't resume it if it doesn't have a driver. */
Alan Stern645daaa2006-08-30 15:47:02 -0400896 if (intf->condition == USB_INTERFACE_UNBOUND) {
Alan Stern1c5df7e2006-07-01 22:13:50 -0400897 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400898 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400899 }
Alan Stern1cc8a252006-07-01 22:10:15 -0400900 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400901
Alan Stern36e56a32006-07-01 22:08:06 -0400902 if (driver->resume) {
903 status = driver->resume(intf);
Alan Stern2bf40862006-07-01 22:12:19 -0400904 if (status)
Alan Stern1cc8a252006-07-01 22:10:15 -0400905 dev_err(&intf->dev, "%s error %d\n",
906 "resume", status);
Alan Stern2bf40862006-07-01 22:12:19 -0400907 else
908 mark_active(intf);
909 } else {
Alan Stern1cc8a252006-07-01 22:10:15 -0400910 dev_warn(&intf->dev, "no resume for driver %s?\n",
911 driver->name);
Alan Stern2bf40862006-07-01 22:12:19 -0400912 mark_active(intf);
913 }
914
915done:
Alan Stern645daaa2006-08-30 15:47:02 -0400916 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400917 if (status == 0)
918 intf->dev.power.power_state.event = PM_EVENT_ON;
919 return status;
Alan Stern36e56a32006-07-01 22:08:06 -0400920}
921
Alan Stern645daaa2006-08-30 15:47:02 -0400922/**
923 * usb_suspend_both - suspend a USB device and its interfaces
924 * @udev: the usb_device to suspend
925 * @msg: Power Management message describing this state transition
926 *
927 * This is the central routine for suspending USB devices. It calls the
928 * suspend methods for all the interface drivers in @udev and then calls
929 * the suspend method for @udev itself. If an error occurs at any stage,
930 * all the interfaces which were suspended are resumed so that they remain
931 * in the same state as the device.
932 *
933 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
934 * checks first to make sure that neither the device itself or any of its
935 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
936 * are, the autosuspend fails.
937 *
938 * If the suspend succeeds, the routine recursively queues an autosuspend
939 * request for @udev's parent device, thereby propagating the change up
940 * the device tree. If all of the parent's children are now suspended,
941 * the parent will autosuspend in turn.
942 *
943 * The suspend method calls are subject to mutual exclusion under control
944 * of @udev's pm_mutex. Many of these calls are also under the protection
945 * of @udev's device lock (including all requests originating outside the
946 * USB subsystem), but autosuspend requests generated by a child device or
947 * interface driver may not be. Usbcore will insure that the method calls
948 * do not arrive during bind, unbind, or reset operations. However, drivers
949 * must be prepared to handle suspend calls arriving at unpredictable times.
950 * The only way to block such calls is to do an autoresume (preventing
951 * autosuspends) while holding @udev's device lock (preventing outside
952 * suspends).
953 *
954 * The caller must hold @udev->pm_mutex.
955 *
956 * This routine can run only in process context.
957 */
Alan Sterna8e7c562006-07-01 22:11:02 -0400958int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
959{
960 int status = 0;
961 int i = 0;
962 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400963 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -0400964
Alan Stern645daaa2006-08-30 15:47:02 -0400965 cancel_delayed_work(&udev->autosuspend);
966 if (udev->state == USB_STATE_NOTATTACHED)
967 return 0;
968 if (udev->state == USB_STATE_SUSPENDED)
969 return 0;
970
971 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
972
973 /* For autosuspend, fail fast if anything is in use.
974 * Also fail if any interfaces require remote wakeup but it
975 * isn't available. */
976 if (udev->auto_pm) {
977 if (udev->pm_usage_cnt > 0)
978 return -EBUSY;
979 if (udev->actconfig) {
980 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
981 intf = udev->actconfig->interface[i];
982 if (!is_active(intf))
983 continue;
984 if (intf->pm_usage_cnt > 0)
985 return -EBUSY;
986 if (intf->needs_remote_wakeup &&
987 !udev->do_remote_wakeup) {
988 dev_dbg(&udev->dev,
989 "remote wakeup needed for autosuspend\n");
990 return -EOPNOTSUPP;
991 }
992 }
993 i = 0;
994 }
995 }
996
997 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -0400998 if (udev->actconfig) {
999 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1000 intf = udev->actconfig->interface[i];
1001 status = suspend_interface(intf, msg);
1002 if (status != 0)
1003 break;
1004 }
1005 }
1006 if (status == 0)
1007 status = suspend_device(udev, msg);
1008
1009 /* If the suspend failed, resume interfaces that did get suspended */
1010 if (status != 0) {
1011 while (--i >= 0) {
1012 intf = udev->actconfig->interface[i];
1013 resume_interface(intf);
1014 }
Alan Stern645daaa2006-08-30 15:47:02 -04001015
1016 /* If the suspend succeeded, propagate it up the tree */
1017 } else if (parent)
1018 usb_autosuspend_device(parent, 0);
1019
1020 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001021 return status;
1022}
1023
Alan Stern645daaa2006-08-30 15:47:02 -04001024/**
1025 * usb_resume_both - resume a USB device and its interfaces
1026 * @udev: the usb_device to resume
1027 *
1028 * This is the central routine for resuming USB devices. It calls the
1029 * the resume method for @udev and then calls the resume methods for all
1030 * the interface drivers in @udev.
1031 *
1032 * Before starting the resume, the routine calls itself recursively for
1033 * the parent device of @udev, thereby propagating the change up the device
1034 * tree and assuring that @udev will be able to resume. If the parent is
1035 * unable to resume successfully, the routine fails.
1036 *
1037 * The resume method calls are subject to mutual exclusion under control
1038 * of @udev's pm_mutex. Many of these calls are also under the protection
1039 * of @udev's device lock (including all requests originating outside the
1040 * USB subsystem), but autoresume requests generated by a child device or
1041 * interface driver may not be. Usbcore will insure that the method calls
1042 * do not arrive during bind, unbind, or reset operations. However, drivers
1043 * must be prepared to handle resume calls arriving at unpredictable times.
1044 * The only way to block such calls is to do an autoresume (preventing
1045 * other autoresumes) while holding @udev's device lock (preventing outside
1046 * resumes).
1047 *
1048 * The caller must hold @udev->pm_mutex.
1049 *
1050 * This routine can run only in process context.
1051 */
Alan Sterna8e7c562006-07-01 22:11:02 -04001052int usb_resume_both(struct usb_device *udev)
1053{
Alan Stern645daaa2006-08-30 15:47:02 -04001054 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001055 int i;
1056 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001057 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001058
Alan Stern645daaa2006-08-30 15:47:02 -04001059 cancel_delayed_work(&udev->autosuspend);
1060 if (udev->state == USB_STATE_NOTATTACHED)
1061 return -ENODEV;
1062
1063 /* Propagate the resume up the tree, if necessary */
1064 if (udev->state == USB_STATE_SUSPENDED) {
1065 if (parent) {
1066 mutex_lock_nested(&parent->pm_mutex, parent->level);
1067 parent->auto_pm = 1;
1068 status = usb_resume_both(parent);
1069 } else {
1070
1071 /* We can't progagate beyond the USB subsystem,
1072 * so if a root hub's controller is suspended
1073 * then we're stuck. */
1074 if (udev->dev.parent->power.power_state.event !=
1075 PM_EVENT_ON)
1076 status = -EHOSTUNREACH;
1077 }
Alan Stern592fbbe2006-09-19 10:08:43 -04001078 if (status == 0)
Alan Stern645daaa2006-08-30 15:47:02 -04001079 status = resume_device(udev);
1080 if (parent)
1081 mutex_unlock(&parent->pm_mutex);
Alan Stern592fbbe2006-09-19 10:08:43 -04001082 } else {
1083
1084 /* Needed only for setting udev->dev.power.power_state.event
1085 * and for possible debugging message. */
1086 status = resume_device(udev);
Alan Stern114b3682006-07-01 22:13:04 -04001087 }
1088
Alan Stern645daaa2006-08-30 15:47:02 -04001089 /* Now the parent won't suspend until we are finished */
1090
Alan Sterna8e7c562006-07-01 22:11:02 -04001091 if (status == 0 && udev->actconfig) {
1092 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1093 intf = udev->actconfig->interface[i];
1094 resume_interface(intf);
1095 }
1096 }
Alan Stern645daaa2006-08-30 15:47:02 -04001097
1098 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001099 return status;
1100}
1101
Alan Stern645daaa2006-08-30 15:47:02 -04001102#ifdef CONFIG_USB_SUSPEND
1103
1104/**
1105 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1106 * @udev - the usb_device to autosuspend
1107 * @dec_usage_cnt - flag to decrement @udev's PM-usage counter
1108 *
1109 * This routine should be called when a core subsystem is finished using
1110 * @udev and wants to allow it to autosuspend. Examples would be when
1111 * @udev's device file in usbfs is closed or after a configuration change.
1112 *
1113 * @dec_usage_cnt should be 1 if the subsystem previously incremented
1114 * @udev's usage counter (such as by passing 1 to usb_autoresume_device);
1115 * otherwise it should be 0.
1116 *
1117 * If the usage counter for @udev or any of its active interfaces is greater
1118 * than 0, the autosuspend request will not be queued. (If an interface
1119 * driver does not support autosuspend then its usage counter is permanently
1120 * positive.) Likewise, if an interface driver requires remote-wakeup
1121 * capability during autosuspend but remote wakeup is disabled, the
1122 * autosuspend will fail.
1123 *
1124 * Often the caller will hold @udev's device lock, but this is not
1125 * necessary.
1126 *
1127 * This routine can run only in process context.
1128 */
1129void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt)
1130{
1131 mutex_lock_nested(&udev->pm_mutex, udev->level);
1132 udev->pm_usage_cnt -= dec_usage_cnt;
1133 if (udev->pm_usage_cnt <= 0)
1134 schedule_delayed_work(&udev->autosuspend,
1135 USB_AUTOSUSPEND_DELAY);
1136 mutex_unlock(&udev->pm_mutex);
1137 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1138 // __FUNCTION__, udev->pm_usage_cnt);
1139}
1140
1141/**
1142 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1143 * @udev - the usb_device to autoresume
1144 * @inc_usage_cnt - flag to increment @udev's PM-usage counter
1145 *
1146 * This routine should be called when a core subsystem wants to use @udev
1147 * and needs to guarantee that it is not suspended. In addition, the
1148 * caller can prevent @udev from being autosuspended subsequently. (Note
1149 * that this will not prevent suspend events originating in the PM core.)
1150 * Examples would be when @udev's device file in usbfs is opened (autosuspend
1151 * should be prevented until the file is closed) or when a remote-wakeup
1152 * request is received (later autosuspends should not be prevented).
1153 *
1154 * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent
1155 * autosuspends. This prevention will persist until the usage counter is
1156 * decremented again (such as by passing 1 to usb_autosuspend_device).
1157 * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged.
1158 * Regardless, if the autoresume fails then the usage counter is not
1159 * incremented.
1160 *
1161 * Often the caller will hold @udev's device lock, but this is not
1162 * necessary (and attempting it might cause deadlock).
1163 *
1164 * This routine can run only in process context.
1165 */
1166int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt)
1167{
1168 int status;
1169
1170 mutex_lock_nested(&udev->pm_mutex, udev->level);
1171 udev->pm_usage_cnt += inc_usage_cnt;
1172 udev->auto_pm = 1;
1173 status = usb_resume_both(udev);
1174 if (status != 0)
1175 udev->pm_usage_cnt -= inc_usage_cnt;
1176 mutex_unlock(&udev->pm_mutex);
1177 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1178 // __FUNCTION__, status, udev->pm_usage_cnt);
1179 return status;
1180}
1181
1182/**
1183 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1184 * @intf - the usb_interface whose counter should be decremented
1185 *
1186 * This routine should be called by an interface driver when it is
1187 * finished using @intf and wants to allow it to autosuspend. A typical
1188 * example would be a character-device driver when its device file is
1189 * closed.
1190 *
1191 * The routine decrements @intf's usage counter. When the counter reaches
1192 * 0, a delayed autosuspend request for @intf's device is queued. When
1193 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1194 * the other usage counters for the sibling interfaces and @intf's
1195 * usb_device, the device and all its interfaces will be autosuspended.
1196 *
1197 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1198 * core will not change its value other than the increment and decrement
1199 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1200 * may use this simple counter-oriented discipline or may set the value
1201 * any way it likes.
1202 *
1203 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1204 * take place only if the device's remote-wakeup facility is enabled.
1205 *
1206 * Suspend method calls queued by this routine can arrive at any time
1207 * while @intf is resumed and its usage counter is equal to 0. They are
1208 * not protected by the usb_device's lock but only by its pm_mutex.
1209 * Drivers must provide their own synchronization.
1210 *
1211 * This routine can run only in process context.
1212 */
1213void usb_autopm_put_interface(struct usb_interface *intf)
1214{
1215 struct usb_device *udev = interface_to_usbdev(intf);
1216
1217 mutex_lock_nested(&udev->pm_mutex, udev->level);
1218 if (intf->condition != USB_INTERFACE_UNBOUND) {
1219 if (--intf->pm_usage_cnt <= 0)
1220 schedule_delayed_work(&udev->autosuspend,
1221 USB_AUTOSUSPEND_DELAY);
1222 }
1223 mutex_unlock(&udev->pm_mutex);
1224 // dev_dbg(&intf->dev, "%s: cnt %d\n",
1225 // __FUNCTION__, intf->pm_usage_cnt);
1226}
1227EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1228
1229/**
1230 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1231 * @intf - the usb_interface whose counter should be incremented
1232 *
1233 * This routine should be called by an interface driver when it wants to
1234 * use @intf and needs to guarantee that it is not suspended. In addition,
1235 * the routine prevents @intf from being autosuspended subsequently. (Note
1236 * that this will not prevent suspend events originating in the PM core.)
1237 * This prevention will persist until usb_autopm_put_interface() is called
1238 * or @intf is unbound. A typical example would be a character-device
1239 * driver when its device file is opened.
1240 *
1241 * The routine increments @intf's usage counter. So long as the counter
1242 * is greater than 0, autosuspend will not be allowed for @intf or its
1243 * usb_device. When the driver is finished using @intf it should call
1244 * usb_autopm_put_interface() to decrement the usage counter and queue
1245 * a delayed autosuspend request (if the counter is <= 0).
1246 *
1247 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1248 * core will not change its value other than the increment and decrement
1249 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1250 * may use this simple counter-oriented discipline or may set the value
1251 * any way it likes.
1252 *
1253 * Resume method calls generated by this routine can arrive at any time
1254 * while @intf is suspended. They are not protected by the usb_device's
1255 * lock but only by its pm_mutex. Drivers must provide their own
1256 * synchronization.
1257 *
1258 * This routine can run only in process context.
1259 */
1260int usb_autopm_get_interface(struct usb_interface *intf)
1261{
1262 struct usb_device *udev = interface_to_usbdev(intf);
1263 int status;
1264
1265 mutex_lock_nested(&udev->pm_mutex, udev->level);
1266 if (intf->condition == USB_INTERFACE_UNBOUND)
1267 status = -ENODEV;
1268 else {
1269 ++intf->pm_usage_cnt;
1270 udev->auto_pm = 1;
1271 status = usb_resume_both(udev);
1272 if (status != 0)
1273 --intf->pm_usage_cnt;
1274 }
1275 mutex_unlock(&udev->pm_mutex);
1276 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1277 // __FUNCTION__, status, intf->pm_usage_cnt);
1278 return status;
1279}
1280EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1281
1282#endif /* CONFIG_USB_SUSPEND */
1283
Alan Stern1cc8a252006-07-01 22:10:15 -04001284static int usb_suspend(struct device *dev, pm_message_t message)
1285{
1286 int status;
1287
Alan Stern645daaa2006-08-30 15:47:02 -04001288 if (is_usb_device(dev)) {
1289 struct usb_device *udev = to_usb_device(dev);
1290
1291 mutex_lock_nested(&udev->pm_mutex, udev->level);
1292 udev->auto_pm = 0;
1293 status = usb_suspend_both(udev, message);
1294 mutex_unlock(&udev->pm_mutex);
1295 } else
Alan Sterna8e7c562006-07-01 22:11:02 -04001296 status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001297 return status;
1298}
1299
1300static int usb_resume(struct device *dev)
1301{
1302 int status;
1303
Alan Sterna8e7c562006-07-01 22:11:02 -04001304 if (is_usb_device(dev)) {
Alan Stern645daaa2006-08-30 15:47:02 -04001305 struct usb_device *udev = to_usb_device(dev);
1306
1307 mutex_lock_nested(&udev->pm_mutex, udev->level);
1308 udev->auto_pm = 0;
1309 status = usb_resume_both(udev);
1310 mutex_unlock(&udev->pm_mutex);
Alan Sterna8e7c562006-07-01 22:11:02 -04001311
1312 /* Rebind drivers that had no suspend method? */
1313 } else
1314 status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001315 return status;
1316}
1317
Alan Stern36e56a32006-07-01 22:08:06 -04001318#endif /* CONFIG_PM */
1319
1320struct bus_type usb_bus_type = {
1321 .name = "usb",
1322 .match = usb_device_match,
1323 .uevent = usb_uevent,
1324#ifdef CONFIG_PM
Alan Stern782da722006-07-01 22:09:35 -04001325 .suspend = usb_suspend,
1326 .resume = usb_resume,
Alan Stern36e56a32006-07-01 22:08:06 -04001327#endif
1328};