libusual: change block scope variable to function scope
[deliverable/linux.git] / drivers / usb / core / driver.c
CommitLineData
ddae41be
GKH
1/*
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
36e56a34
AS
20 * matching, probing, releasing, suspending and resuming for
21 * real drivers.
ddae41be
GKH
22 *
23 */
24
ddae41be
GKH
25#include <linux/device.h>
26#include <linux/usb.h>
bd859281 27#include <linux/workqueue.h>
ddae41be
GKH
28#include "hcd.h"
29#include "usb.h"
30
733260ff
GKH
31#ifdef CONFIG_HOTPLUG
32
33/*
34 * Adds a new dynamic USBdevice ID to this driver,
35 * and cause the driver to probe for all devices again.
36 */
93bacefc
GKH
37ssize_t usb_store_new_id(struct usb_dynids *dynids,
38 struct device_driver *driver,
39 const char *buf, size_t count)
733260ff 40{
733260ff
GKH
41 struct usb_dynid *dynid;
42 u32 idVendor = 0;
43 u32 idProduct = 0;
44 int fields = 0;
1b21d5e1 45 int retval = 0;
733260ff
GKH
46
47 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
48 if (fields < 2)
49 return -EINVAL;
50
51 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
52 if (!dynid)
53 return -ENOMEM;
54
55 INIT_LIST_HEAD(&dynid->node);
56 dynid->id.idVendor = idVendor;
57 dynid->id.idProduct = idProduct;
58 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
59
93bacefc
GKH
60 spin_lock(&dynids->lock);
61 list_add_tail(&dynids->list, &dynid->node);
62 spin_unlock(&dynids->lock);
733260ff
GKH
63
64 if (get_driver(driver)) {
1b21d5e1 65 retval = driver_attach(driver);
733260ff
GKH
66 put_driver(driver);
67 }
68
1b21d5e1
GKH
69 if (retval)
70 return retval;
733260ff
GKH
71 return count;
72}
93bacefc
GKH
73EXPORT_SYMBOL_GPL(usb_store_new_id);
74
75static ssize_t store_new_id(struct device_driver *driver,
76 const char *buf, size_t count)
77{
78 struct usb_driver *usb_drv = to_usb_driver(driver);
79
80 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
81}
733260ff
GKH
82static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
83
84static int usb_create_newid_file(struct usb_driver *usb_drv)
85{
86 int error = 0;
87
ba9dc657
GKH
88 if (usb_drv->no_dynamic_id)
89 goto exit;
90
733260ff 91 if (usb_drv->probe != NULL)
8bb54ab5 92 error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
733260ff 93 &driver_attr_new_id.attr);
ba9dc657 94exit:
733260ff
GKH
95 return error;
96}
97
ba9dc657
GKH
98static void usb_remove_newid_file(struct usb_driver *usb_drv)
99{
100 if (usb_drv->no_dynamic_id)
101 return;
102
103 if (usb_drv->probe != NULL)
8bb54ab5 104 sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
ba9dc657
GKH
105 &driver_attr_new_id.attr);
106}
107
733260ff
GKH
108static void usb_free_dynids(struct usb_driver *usb_drv)
109{
110 struct usb_dynid *dynid, *n;
111
112 spin_lock(&usb_drv->dynids.lock);
113 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
114 list_del(&dynid->node);
115 kfree(dynid);
116 }
117 spin_unlock(&usb_drv->dynids.lock);
118}
119#else
120static inline int usb_create_newid_file(struct usb_driver *usb_drv)
121{
122 return 0;
123}
124
ba9dc657
GKH
125static void usb_remove_newid_file(struct usb_driver *usb_drv)
126{
127}
128
733260ff
GKH
129static inline void usb_free_dynids(struct usb_driver *usb_drv)
130{
131}
132#endif
133
134static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
135 struct usb_driver *drv)
136{
137 struct usb_dynid *dynid;
138
139 spin_lock(&drv->dynids.lock);
140 list_for_each_entry(dynid, &drv->dynids.list, node) {
141 if (usb_match_one_id(intf, &dynid->id)) {
142 spin_unlock(&drv->dynids.lock);
143 return &dynid->id;
144 }
145 }
146 spin_unlock(&drv->dynids.lock);
147 return NULL;
148}
149
150
8bb54ab5
AS
151/* called from driver core with dev locked */
152static int usb_probe_device(struct device *dev)
153{
154 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
155 struct usb_device *udev;
156 int error = -ENODEV;
157
158 dev_dbg(dev, "%s\n", __FUNCTION__);
159
160 if (!is_usb_device(dev)) /* Sanity check */
161 return error;
162
163 udev = to_usb_device(dev);
164
8bb54ab5
AS
165 /* TODO: Add real matching code */
166
645daaab
AS
167 /* The device should always appear to be in use
168 * unless the driver suports autosuspend.
169 */
170 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
171
8bb54ab5
AS
172 error = udriver->probe(udev);
173 return error;
174}
175
176/* called from driver core with dev locked */
177static int usb_unbind_device(struct device *dev)
178{
179 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
180
181 udriver->disconnect(to_usb_device(dev));
182 return 0;
183}
184
185
186/* called from driver core with dev locked */
ddae41be
GKH
187static int usb_probe_interface(struct device *dev)
188{
8bb54ab5
AS
189 struct usb_driver *driver = to_usb_driver(dev->driver);
190 struct usb_interface *intf;
645daaab 191 struct usb_device *udev;
ddae41be
GKH
192 const struct usb_device_id *id;
193 int error = -ENODEV;
194
195 dev_dbg(dev, "%s\n", __FUNCTION__);
196
8bb54ab5 197 if (is_usb_device(dev)) /* Sanity check */
ddae41be 198 return error;
8bb54ab5
AS
199
200 intf = to_usb_interface(dev);
645daaab 201 udev = interface_to_usbdev(intf);
ddae41be
GKH
202
203 id = usb_match_id(intf, driver->id_table);
733260ff
GKH
204 if (!id)
205 id = usb_match_dynamic_id(intf, driver);
ddae41be
GKH
206 if (id) {
207 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
208
94fcda1f 209 error = usb_autoresume_device(udev);
645daaab
AS
210 if (error)
211 return error;
212
ddae41be
GKH
213 /* Interface "power state" doesn't correspond to any hardware
214 * state whatsoever. We use it to record when it's bound to
215 * a driver that may start I/0: it's not frozen/quiesced.
216 */
217 mark_active(intf);
218 intf->condition = USB_INTERFACE_BINDING;
645daaab
AS
219
220 /* The interface should always appear to be in use
221 * unless the driver suports autosuspend.
222 */
223 intf->pm_usage_cnt = !(driver->supports_autosuspend);
224
ddae41be
GKH
225 error = driver->probe(intf, id);
226 if (error) {
227 mark_quiesced(intf);
645daaab 228 intf->needs_remote_wakeup = 0;
ddae41be
GKH
229 intf->condition = USB_INTERFACE_UNBOUND;
230 } else
231 intf->condition = USB_INTERFACE_BOUND;
645daaab 232
94fcda1f 233 usb_autosuspend_device(udev);
ddae41be
GKH
234 }
235
236 return error;
237}
238
8bb54ab5 239/* called from driver core with dev locked */
ddae41be
GKH
240static int usb_unbind_interface(struct device *dev)
241{
8bb54ab5 242 struct usb_driver *driver = to_usb_driver(dev->driver);
ddae41be 243 struct usb_interface *intf = to_usb_interface(dev);
645daaab
AS
244 struct usb_device *udev;
245 int error;
ddae41be
GKH
246
247 intf->condition = USB_INTERFACE_UNBINDING;
248
645daaab
AS
249 /* Autoresume for set_interface call below */
250 udev = interface_to_usbdev(intf);
94fcda1f 251 error = usb_autoresume_device(udev);
645daaab 252
ddae41be
GKH
253 /* release all urbs for this interface */
254 usb_disable_interface(interface_to_usbdev(intf), intf);
255
8bb54ab5 256 driver->disconnect(intf);
ddae41be
GKH
257
258 /* reset other interface state */
259 usb_set_interface(interface_to_usbdev(intf),
260 intf->altsetting[0].desc.bInterfaceNumber,
261 0);
262 usb_set_intfdata(intf, NULL);
645daaab 263
ddae41be
GKH
264 intf->condition = USB_INTERFACE_UNBOUND;
265 mark_quiesced(intf);
645daaab
AS
266 intf->needs_remote_wakeup = 0;
267
268 if (!error)
94fcda1f 269 usb_autosuspend_device(udev);
ddae41be
GKH
270
271 return 0;
272}
273
36e56a34
AS
274/**
275 * usb_driver_claim_interface - bind a driver to an interface
276 * @driver: the driver to be bound
277 * @iface: the interface to which it will be bound; must be in the
278 * usb device's active configuration
279 * @priv: driver data associated with that interface
280 *
281 * This is used by usb device drivers that need to claim more than one
282 * interface on a device when probing (audio and acm are current examples).
283 * No device driver should directly modify internal usb_interface or
284 * usb_device structure members.
285 *
286 * Few drivers should need to use this routine, since the most natural
287 * way to bind to an interface is to return the private data from
288 * the driver's probe() method.
289 *
341487a8
GKH
290 * Callers must own the device lock, so driver probe() entries don't need
291 * extra locking, but other call contexts may need to explicitly claim that
292 * lock.
36e56a34
AS
293 */
294int usb_driver_claim_interface(struct usb_driver *driver,
295 struct usb_interface *iface, void* priv)
296{
297 struct device *dev = &iface->dev;
645daaab 298 struct usb_device *udev = interface_to_usbdev(iface);
1b21d5e1 299 int retval = 0;
36e56a34
AS
300
301 if (dev->driver)
302 return -EBUSY;
303
8bb54ab5 304 dev->driver = &driver->drvwrap.driver;
36e56a34 305 usb_set_intfdata(iface, priv);
645daaab 306
e0318ebf 307 usb_pm_lock(udev);
36e56a34
AS
308 iface->condition = USB_INTERFACE_BOUND;
309 mark_active(iface);
645daaab 310 iface->pm_usage_cnt = !(driver->supports_autosuspend);
e0318ebf 311 usb_pm_unlock(udev);
36e56a34
AS
312
313 /* if interface was already added, bind now; else let
314 * the future device_add() bind it, bypassing probe()
315 */
316 if (device_is_registered(dev))
1b21d5e1 317 retval = device_bind_driver(dev);
36e56a34 318
1b21d5e1 319 return retval;
36e56a34
AS
320}
321EXPORT_SYMBOL(usb_driver_claim_interface);
322
323/**
324 * usb_driver_release_interface - unbind a driver from an interface
325 * @driver: the driver to be unbound
326 * @iface: the interface from which it will be unbound
327 *
328 * This can be used by drivers to release an interface without waiting
329 * for their disconnect() methods to be called. In typical cases this
330 * also causes the driver disconnect() method to be called.
331 *
332 * This call is synchronous, and may not be used in an interrupt context.
341487a8
GKH
333 * Callers must own the device lock, so driver disconnect() entries don't
334 * need extra locking, but other call contexts may need to explicitly claim
335 * that lock.
36e56a34
AS
336 */
337void usb_driver_release_interface(struct usb_driver *driver,
338 struct usb_interface *iface)
339{
340 struct device *dev = &iface->dev;
645daaab 341 struct usb_device *udev = interface_to_usbdev(iface);
36e56a34
AS
342
343 /* this should never happen, don't release something that's not ours */
8bb54ab5 344 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
36e56a34
AS
345 return;
346
347 /* don't release from within disconnect() */
348 if (iface->condition != USB_INTERFACE_BOUND)
349 return;
350
351 /* don't release if the interface hasn't been added yet */
352 if (device_is_registered(dev)) {
353 iface->condition = USB_INTERFACE_UNBINDING;
354 device_release_driver(dev);
355 }
356
357 dev->driver = NULL;
358 usb_set_intfdata(iface, NULL);
645daaab 359
e0318ebf 360 usb_pm_lock(udev);
36e56a34
AS
361 iface->condition = USB_INTERFACE_UNBOUND;
362 mark_quiesced(iface);
645daaab 363 iface->needs_remote_wakeup = 0;
e0318ebf 364 usb_pm_unlock(udev);
36e56a34
AS
365}
366EXPORT_SYMBOL(usb_driver_release_interface);
367
733260ff 368/* returns 0 if no match, 1 if match */
bb417020 369int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
733260ff 370{
733260ff
GKH
371 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
372 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
373 return 0;
374
375 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
376 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
377 return 0;
378
379 /* No need to test id->bcdDevice_lo != 0, since 0 is never
380 greater than any unsigned number. */
381 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
382 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
383 return 0;
384
385 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
386 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
387 return 0;
388
389 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
390 (id->bDeviceClass != dev->descriptor.bDeviceClass))
391 return 0;
392
393 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
394 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
395 return 0;
396
397 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
398 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
399 return 0;
400
bb417020
GKH
401 return 1;
402}
403
404/* returns 0 if no match, 1 if match */
405int usb_match_one_id(struct usb_interface *interface,
406 const struct usb_device_id *id)
407{
408 struct usb_host_interface *intf;
409 struct usb_device *dev;
410
411 /* proc_connectinfo in devio.c may call us with id == NULL. */
412 if (id == NULL)
413 return 0;
414
415 intf = interface->cur_altsetting;
416 dev = interface_to_usbdev(interface);
417
418 if (!usb_match_device(dev, id))
419 return 0;
420
93c8bf45
AS
421 /* The interface class, subclass, and protocol should never be
422 * checked for a match if the device class is Vendor Specific,
423 * unless the match record specifies the Vendor ID. */
424 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
425 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
426 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
427 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
428 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
429 return 0;
430
733260ff
GKH
431 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
432 (id->bInterfaceClass != intf->desc.bInterfaceClass))
433 return 0;
434
435 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
436 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
437 return 0;
438
439 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
440 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
441 return 0;
442
443 return 1;
444}
93bacefc
GKH
445EXPORT_SYMBOL_GPL(usb_match_one_id);
446
ddae41be
GKH
447/**
448 * usb_match_id - find first usb_device_id matching device or interface
449 * @interface: the interface of interest
450 * @id: array of usb_device_id structures, terminated by zero entry
451 *
452 * usb_match_id searches an array of usb_device_id's and returns
453 * the first one matching the device or interface, or null.
454 * This is used when binding (or rebinding) a driver to an interface.
455 * Most USB device drivers will use this indirectly, through the usb core,
456 * but some layered driver frameworks use it directly.
457 * These device tables are exported with MODULE_DEVICE_TABLE, through
458 * modutils, to support the driver loading functionality of USB hotplugging.
459 *
460 * What Matches:
461 *
462 * The "match_flags" element in a usb_device_id controls which
463 * members are used. If the corresponding bit is set, the
464 * value in the device_id must match its corresponding member
465 * in the device or interface descriptor, or else the device_id
466 * does not match.
467 *
468 * "driver_info" is normally used only by device drivers,
469 * but you can create a wildcard "matches anything" usb_device_id
470 * as a driver's "modules.usbmap" entry if you provide an id with
471 * only a nonzero "driver_info" field. If you do this, the USB device
472 * driver's probe() routine should use additional intelligence to
473 * decide whether to bind to the specified interface.
474 *
475 * What Makes Good usb_device_id Tables:
476 *
477 * The match algorithm is very simple, so that intelligence in
478 * driver selection must come from smart driver id records.
479 * Unless you have good reasons to use another selection policy,
480 * provide match elements only in related groups, and order match
481 * specifiers from specific to general. Use the macros provided
482 * for that purpose if you can.
483 *
484 * The most specific match specifiers use device descriptor
485 * data. These are commonly used with product-specific matches;
486 * the USB_DEVICE macro lets you provide vendor and product IDs,
487 * and you can also match against ranges of product revisions.
488 * These are widely used for devices with application or vendor
489 * specific bDeviceClass values.
490 *
491 * Matches based on device class/subclass/protocol specifications
492 * are slightly more general; use the USB_DEVICE_INFO macro, or
493 * its siblings. These are used with single-function devices
494 * where bDeviceClass doesn't specify that each interface has
495 * its own class.
496 *
497 * Matches based on interface class/subclass/protocol are the
498 * most general; they let drivers bind to any interface on a
499 * multiple-function device. Use the USB_INTERFACE_INFO
500 * macro, or its siblings, to match class-per-interface style
93c8bf45
AS
501 * devices (as recorded in bInterfaceClass).
502 *
503 * Note that an entry created by USB_INTERFACE_INFO won't match
504 * any interface if the device class is set to Vendor-Specific.
505 * This is deliberate; according to the USB spec the meanings of
506 * the interface class/subclass/protocol for these devices are also
507 * vendor-specific, and hence matching against a standard product
508 * class wouldn't work anyway. If you really want to use an
509 * interface-based match for such a device, create a match record
510 * that also specifies the vendor ID. (Unforunately there isn't a
511 * standard macro for creating records like this.)
ddae41be
GKH
512 *
513 * Within those groups, remember that not all combinations are
514 * meaningful. For example, don't give a product version range
515 * without vendor and product IDs; or specify a protocol without
516 * its associated class and subclass.
517 */
518const struct usb_device_id *usb_match_id(struct usb_interface *interface,
519 const struct usb_device_id *id)
520{
ddae41be
GKH
521 /* proc_connectinfo in devio.c may call us with id == NULL. */
522 if (id == NULL)
523 return NULL;
524
ddae41be
GKH
525 /* It is important to check that id->driver_info is nonzero,
526 since an entry that is all zeroes except for a nonzero
527 id->driver_info is the way to create an entry that
528 indicates that the driver want to examine every
529 device and interface. */
530 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
531 id->driver_info; id++) {
733260ff
GKH
532 if (usb_match_one_id(interface, id))
533 return id;
ddae41be
GKH
534 }
535
536 return NULL;
537}
b87ba0a3 538EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
ddae41be 539
8bb22d2b 540static int usb_device_match(struct device *dev, struct device_driver *drv)
ddae41be 541{
8bb54ab5
AS
542 /* devices and interfaces are handled separately */
543 if (is_usb_device(dev)) {
ddae41be 544
8bb54ab5
AS
545 /* interface drivers never match devices */
546 if (!is_usb_device_driver(drv))
547 return 0;
ddae41be 548
8bb54ab5 549 /* TODO: Add real matching code */
ddae41be
GKH
550 return 1;
551
8bb54ab5
AS
552 } else {
553 struct usb_interface *intf;
554 struct usb_driver *usb_drv;
555 const struct usb_device_id *id;
556
557 /* device drivers never match interfaces */
558 if (is_usb_device_driver(drv))
559 return 0;
560
561 intf = to_usb_interface(dev);
562 usb_drv = to_usb_driver(drv);
563
564 id = usb_match_id(intf, usb_drv->id_table);
565 if (id)
566 return 1;
567
568 id = usb_match_dynamic_id(intf, usb_drv);
569 if (id)
570 return 1;
571 }
572
ddae41be
GKH
573 return 0;
574}
575
36e56a34
AS
576#ifdef CONFIG_HOTPLUG
577
578/*
579 * This sends an uevent to userspace, typically helping to load driver
580 * or other modules, configure the device, and more. Drivers can provide
581 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
582 *
583 * We're called either from khubd (the typical case) or from root hub
584 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
585 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
586 * device (and this configuration!) are still present.
587 */
588static int usb_uevent(struct device *dev, char **envp, int num_envp,
589 char *buffer, int buffer_size)
590{
591 struct usb_interface *intf;
592 struct usb_device *usb_dev;
593 struct usb_host_interface *alt;
594 int i = 0;
595 int length = 0;
596
597 if (!dev)
598 return -ENODEV;
599
600 /* driver is often null here; dev_dbg() would oops */
601 pr_debug ("usb %s: uevent\n", dev->bus_id);
602
782da727
AS
603 if (is_usb_device(dev)) {
604 usb_dev = to_usb_device(dev);
605 alt = NULL;
606 } else {
8bb54ab5
AS
607 intf = to_usb_interface(dev);
608 usb_dev = interface_to_usbdev(intf);
609 alt = intf->cur_altsetting;
610 }
36e56a34
AS
611
612 if (usb_dev->devnum < 0) {
613 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
614 return -ENODEV;
615 }
616 if (!usb_dev->bus) {
617 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
618 return -ENODEV;
619 }
620
621#ifdef CONFIG_USB_DEVICEFS
622 /* If this is available, userspace programs can directly read
623 * all the device descriptors we don't tell them about. Or
624 * even act as usermode drivers.
625 *
626 * FIXME reduce hardwired intelligence here
627 */
628 if (add_uevent_var(envp, num_envp, &i,
629 buffer, buffer_size, &length,
630 "DEVICE=/proc/bus/usb/%03d/%03d",
631 usb_dev->bus->busnum, usb_dev->devnum))
632 return -ENOMEM;
633#endif
634
635 /* per-device configurations are common */
636 if (add_uevent_var(envp, num_envp, &i,
637 buffer, buffer_size, &length,
638 "PRODUCT=%x/%x/%x",
639 le16_to_cpu(usb_dev->descriptor.idVendor),
640 le16_to_cpu(usb_dev->descriptor.idProduct),
641 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
642 return -ENOMEM;
643
644 /* class-based driver binding models */
645 if (add_uevent_var(envp, num_envp, &i,
646 buffer, buffer_size, &length,
647 "TYPE=%d/%d/%d",
648 usb_dev->descriptor.bDeviceClass,
649 usb_dev->descriptor.bDeviceSubClass,
650 usb_dev->descriptor.bDeviceProtocol))
651 return -ENOMEM;
652
782da727
AS
653 if (!is_usb_device(dev)) {
654
655 if (add_uevent_var(envp, num_envp, &i,
36e56a34
AS
656 buffer, buffer_size, &length,
657 "INTERFACE=%d/%d/%d",
658 alt->desc.bInterfaceClass,
659 alt->desc.bInterfaceSubClass,
660 alt->desc.bInterfaceProtocol))
782da727 661 return -ENOMEM;
36e56a34 662
782da727 663 if (add_uevent_var(envp, num_envp, &i,
36e56a34
AS
664 buffer, buffer_size, &length,
665 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
666 le16_to_cpu(usb_dev->descriptor.idVendor),
667 le16_to_cpu(usb_dev->descriptor.idProduct),
668 le16_to_cpu(usb_dev->descriptor.bcdDevice),
669 usb_dev->descriptor.bDeviceClass,
670 usb_dev->descriptor.bDeviceSubClass,
671 usb_dev->descriptor.bDeviceProtocol,
672 alt->desc.bInterfaceClass,
673 alt->desc.bInterfaceSubClass,
674 alt->desc.bInterfaceProtocol))
782da727
AS
675 return -ENOMEM;
676 }
36e56a34
AS
677
678 envp[i] = NULL;
679
680 return 0;
681}
682
683#else
684
685static int usb_uevent(struct device *dev, char **envp,
686 int num_envp, char *buffer, int buffer_size)
687{
688 return -ENODEV;
689}
690
691#endif /* CONFIG_HOTPLUG */
692
ddae41be 693/**
8bb54ab5
AS
694 * usb_register_device_driver - register a USB device (not interface) driver
695 * @new_udriver: USB operations for the device driver
2143acc6 696 * @owner: module owner of this driver.
ddae41be 697 *
8bb54ab5
AS
698 * Registers a USB device driver with the USB core. The list of
699 * unattached devices will be rescanned whenever a new driver is
700 * added, allowing the new driver to attach to any recognized devices.
701 * Returns a negative error code on failure and 0 on success.
702 */
703int usb_register_device_driver(struct usb_device_driver *new_udriver,
704 struct module *owner)
705{
706 int retval = 0;
707
708 if (usb_disabled())
709 return -ENODEV;
710
711 new_udriver->drvwrap.for_devices = 1;
712 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
713 new_udriver->drvwrap.driver.bus = &usb_bus_type;
714 new_udriver->drvwrap.driver.probe = usb_probe_device;
715 new_udriver->drvwrap.driver.remove = usb_unbind_device;
716 new_udriver->drvwrap.driver.owner = owner;
717
718 retval = driver_register(&new_udriver->drvwrap.driver);
719
720 if (!retval) {
721 pr_info("%s: registered new device driver %s\n",
722 usbcore_name, new_udriver->name);
723 usbfs_update_special();
724 } else {
725 printk(KERN_ERR "%s: error %d registering device "
726 " driver %s\n",
727 usbcore_name, retval, new_udriver->name);
728 }
729
730 return retval;
731}
732EXPORT_SYMBOL_GPL(usb_register_device_driver);
733
734/**
735 * usb_deregister_device_driver - unregister a USB device (not interface) driver
736 * @udriver: USB operations of the device driver to unregister
737 * Context: must be able to sleep
738 *
739 * Unlinks the specified driver from the internal USB driver list.
740 */
741void usb_deregister_device_driver(struct usb_device_driver *udriver)
742{
743 pr_info("%s: deregistering device driver %s\n",
744 usbcore_name, udriver->name);
745
746 driver_unregister(&udriver->drvwrap.driver);
747 usbfs_update_special();
748}
749EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
750
751/**
752 * usb_register_driver - register a USB interface driver
753 * @new_driver: USB operations for the interface driver
754 * @owner: module owner of this driver.
892705a1 755 * @mod_name: module name string
8bb54ab5
AS
756 *
757 * Registers a USB interface driver with the USB core. The list of
758 * unattached interfaces will be rescanned whenever a new driver is
759 * added, allowing the new driver to attach to any recognized interfaces.
ddae41be
GKH
760 * Returns a negative error code on failure and 0 on success.
761 *
762 * NOTE: if you want your driver to use the USB major number, you must call
763 * usb_register_dev() to enable that functionality. This function no longer
764 * takes care of that.
765 */
80f745fb
GKH
766int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
767 const char *mod_name)
ddae41be
GKH
768{
769 int retval = 0;
770
771 if (usb_disabled())
772 return -ENODEV;
773
8bb54ab5
AS
774 new_driver->drvwrap.for_devices = 0;
775 new_driver->drvwrap.driver.name = (char *) new_driver->name;
776 new_driver->drvwrap.driver.bus = &usb_bus_type;
777 new_driver->drvwrap.driver.probe = usb_probe_interface;
778 new_driver->drvwrap.driver.remove = usb_unbind_interface;
779 new_driver->drvwrap.driver.owner = owner;
80f745fb 780 new_driver->drvwrap.driver.mod_name = mod_name;
733260ff
GKH
781 spin_lock_init(&new_driver->dynids.lock);
782 INIT_LIST_HEAD(&new_driver->dynids.list);
ddae41be 783
8bb54ab5 784 retval = driver_register(&new_driver->drvwrap.driver);
ddae41be
GKH
785
786 if (!retval) {
8bb54ab5 787 pr_info("%s: registered new interface driver %s\n",
ddae41be
GKH
788 usbcore_name, new_driver->name);
789 usbfs_update_special();
733260ff 790 usb_create_newid_file(new_driver);
ddae41be 791 } else {
8bb54ab5
AS
792 printk(KERN_ERR "%s: error %d registering interface "
793 " driver %s\n",
ddae41be
GKH
794 usbcore_name, retval, new_driver->name);
795 }
796
797 return retval;
798}
b87ba0a3 799EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
ddae41be
GKH
800
801/**
8bb54ab5
AS
802 * usb_deregister - unregister a USB interface driver
803 * @driver: USB operations of the interface driver to unregister
ddae41be
GKH
804 * Context: must be able to sleep
805 *
806 * Unlinks the specified driver from the internal USB driver list.
807 *
808 * NOTE: If you called usb_register_dev(), you still need to call
809 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
810 * this * call will no longer do it for you.
811 */
812void usb_deregister(struct usb_driver *driver)
813{
8bb54ab5
AS
814 pr_info("%s: deregistering interface driver %s\n",
815 usbcore_name, driver->name);
ddae41be 816
ba9dc657 817 usb_remove_newid_file(driver);
733260ff 818 usb_free_dynids(driver);
8bb54ab5 819 driver_unregister(&driver->drvwrap.driver);
ddae41be
GKH
820
821 usbfs_update_special();
822}
b87ba0a3 823EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
36e56a34
AS
824
825#ifdef CONFIG_PM
826
e0318ebf 827/* Caller has locked udev's pm_mutex */
d5ec1686 828static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
36e56a34 829{
782da727 830 struct usb_device_driver *udriver;
2bf4086d 831 int status = 0;
36e56a34 832
114b368c
AS
833 if (udev->state == USB_STATE_NOTATTACHED ||
834 udev->state == USB_STATE_SUSPENDED)
835 goto done;
836
1c5df7e7
AS
837 /* For devices that don't have a driver, we do a standard suspend. */
838 if (udev->dev.driver == NULL) {
645daaab 839 udev->do_remote_wakeup = 0;
1c5df7e7 840 status = usb_port_suspend(udev);
2bf4086d 841 goto done;
1c5df7e7
AS
842 }
843
1cc8a25d 844 udriver = to_usb_device_driver(udev->dev.driver);
2bf4086d
AS
845 status = udriver->suspend(udev, msg);
846
847done:
645daaab 848 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
2bf4086d
AS
849 if (status == 0)
850 udev->dev.power.power_state.event = msg.event;
851 return status;
1cc8a25d
AS
852}
853
e0318ebf 854/* Caller has locked udev's pm_mutex */
d5ec1686 855static int usb_resume_device(struct usb_device *udev)
1cc8a25d
AS
856{
857 struct usb_device_driver *udriver;
2bf4086d 858 int status = 0;
36e56a34 859
114b368c
AS
860 if (udev->state == USB_STATE_NOTATTACHED ||
861 udev->state != USB_STATE_SUSPENDED)
2bf4086d 862 goto done;
1cc8a25d 863
1c5df7e7
AS
864 /* Can't resume it if it doesn't have a driver. */
865 if (udev->dev.driver == NULL) {
866 status = -ENOTCONN;
2bf4086d 867 goto done;
1c5df7e7
AS
868 }
869
1cc8a25d 870 udriver = to_usb_device_driver(udev->dev.driver);
2bf4086d
AS
871 status = udriver->resume(udev);
872
873done:
645daaab 874 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
2bf4086d
AS
875 if (status == 0)
876 udev->dev.power.power_state.event = PM_EVENT_ON;
877 return status;
1cc8a25d
AS
878}
879
e0318ebf 880/* Caller has locked intf's usb_device's pm mutex */
d5ec1686 881static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg)
1cc8a25d
AS
882{
883 struct usb_driver *driver;
2bf4086d 884 int status = 0;
1cc8a25d 885
114b368c
AS
886 /* with no hardware, USB interfaces only use FREEZE and ON states */
887 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
888 !is_active(intf))
2bf4086d 889 goto done;
1cc8a25d 890
645daaab 891 if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
2bf4086d 892 goto done;
114b368c 893 driver = to_usb_driver(intf->dev.driver);
36e56a34
AS
894
895 if (driver->suspend && driver->resume) {
1cc8a25d 896 status = driver->suspend(intf, msg);
645daaab
AS
897 if (status == 0)
898 mark_quiesced(intf);
899 else if (!interface_to_usbdev(intf)->auto_pm)
1cc8a25d
AS
900 dev_err(&intf->dev, "%s error %d\n",
901 "suspend", status);
36e56a34
AS
902 } else {
903 // FIXME else if there's no suspend method, disconnect...
645daaab 904 // Not possible if auto_pm is set...
1cc8a25d
AS
905 dev_warn(&intf->dev, "no suspend for driver %s?\n",
906 driver->name);
36e56a34 907 mark_quiesced(intf);
36e56a34 908 }
2bf4086d
AS
909
910done:
645daaab 911 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
2bf4086d
AS
912 if (status == 0)
913 intf->dev.power.power_state.event = msg.event;
36e56a34
AS
914 return status;
915}
916
645daaab 917/* Caller has locked intf's usb_device's pm_mutex */
d5ec1686 918static int usb_resume_interface(struct usb_interface *intf)
36e56a34 919{
1cc8a25d 920 struct usb_driver *driver;
2bf4086d 921 int status = 0;
36e56a34 922
114b368c
AS
923 if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
924 is_active(intf))
2bf4086d 925 goto done;
36e56a34 926
645daaab
AS
927 /* Don't let autoresume interfere with unbinding */
928 if (intf->condition == USB_INTERFACE_UNBINDING)
929 goto done;
930
1c5df7e7 931 /* Can't resume it if it doesn't have a driver. */
645daaab 932 if (intf->condition == USB_INTERFACE_UNBOUND) {
1c5df7e7 933 status = -ENOTCONN;
2bf4086d 934 goto done;
1c5df7e7 935 }
1cc8a25d 936 driver = to_usb_driver(intf->dev.driver);
36e56a34 937
36e56a34
AS
938 if (driver->resume) {
939 status = driver->resume(intf);
2bf4086d 940 if (status)
1cc8a25d
AS
941 dev_err(&intf->dev, "%s error %d\n",
942 "resume", status);
2bf4086d
AS
943 else
944 mark_active(intf);
945 } else {
1cc8a25d
AS
946 dev_warn(&intf->dev, "no resume for driver %s?\n",
947 driver->name);
2bf4086d
AS
948 mark_active(intf);
949 }
950
951done:
645daaab 952 // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
2bf4086d
AS
953 if (status == 0)
954 intf->dev.power.power_state.event = PM_EVENT_ON;
955 return status;
36e56a34
AS
956}
957
94fcda1f
AS
958#ifdef CONFIG_USB_SUSPEND
959
af4f7606
AS
960/* Internal routine to check whether we may autosuspend a device. */
961static int autosuspend_check(struct usb_device *udev)
962{
963 int i;
964 struct usb_interface *intf;
965
b5e795f8
AS
966 /* For autosuspend, fail fast if anything is in use or autosuspend
967 * is disabled. Also fail if any interfaces require remote wakeup
968 * but it isn't available.
969 */
af4f7606
AS
970 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
971 if (udev->pm_usage_cnt > 0)
972 return -EBUSY;
b5e795f8
AS
973 if (!udev->autosuspend_delay)
974 return -EPERM;
975
af4f7606
AS
976 if (udev->actconfig) {
977 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
978 intf = udev->actconfig->interface[i];
979 if (!is_active(intf))
980 continue;
981 if (intf->pm_usage_cnt > 0)
982 return -EBUSY;
983 if (intf->needs_remote_wakeup &&
984 !udev->do_remote_wakeup) {
985 dev_dbg(&udev->dev, "remote wakeup needed "
986 "for autosuspend\n");
987 return -EOPNOTSUPP;
988 }
989 }
990 }
991 return 0;
992}
993
94fcda1f
AS
994#else
995
996#define autosuspend_check(udev) 0
997
b5e795f8 998#endif /* CONFIG_USB_SUSPEND */
94fcda1f 999
645daaab
AS
1000/**
1001 * usb_suspend_both - suspend a USB device and its interfaces
1002 * @udev: the usb_device to suspend
1003 * @msg: Power Management message describing this state transition
1004 *
1005 * This is the central routine for suspending USB devices. It calls the
1006 * suspend methods for all the interface drivers in @udev and then calls
1007 * the suspend method for @udev itself. If an error occurs at any stage,
1008 * all the interfaces which were suspended are resumed so that they remain
1009 * in the same state as the device.
1010 *
1011 * If an autosuspend is in progress (@udev->auto_pm is set), the routine
1012 * checks first to make sure that neither the device itself or any of its
1013 * active interfaces is in use (pm_usage_cnt is greater than 0). If they
1014 * are, the autosuspend fails.
1015 *
1016 * If the suspend succeeds, the routine recursively queues an autosuspend
1017 * request for @udev's parent device, thereby propagating the change up
1018 * the device tree. If all of the parent's children are now suspended,
1019 * the parent will autosuspend in turn.
1020 *
1021 * The suspend method calls are subject to mutual exclusion under control
1022 * of @udev's pm_mutex. Many of these calls are also under the protection
1023 * of @udev's device lock (including all requests originating outside the
1024 * USB subsystem), but autosuspend requests generated by a child device or
1025 * interface driver may not be. Usbcore will insure that the method calls
1026 * do not arrive during bind, unbind, or reset operations. However, drivers
1027 * must be prepared to handle suspend calls arriving at unpredictable times.
1028 * The only way to block such calls is to do an autoresume (preventing
1029 * autosuspends) while holding @udev's device lock (preventing outside
1030 * suspends).
1031 *
1032 * The caller must hold @udev->pm_mutex.
1033 *
1034 * This routine can run only in process context.
1035 */
a8e7c565
AS
1036int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1037{
1038 int status = 0;
1039 int i = 0;
1040 struct usb_interface *intf;
645daaab
AS
1041 struct usb_device *parent = udev->parent;
1042
1043 cancel_delayed_work(&udev->autosuspend);
1044 if (udev->state == USB_STATE_NOTATTACHED)
1045 return 0;
1046 if (udev->state == USB_STATE_SUSPENDED)
1047 return 0;
a8e7c565 1048
645daaab
AS
1049 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1050
645daaab 1051 if (udev->auto_pm) {
af4f7606
AS
1052 status = autosuspend_check(udev);
1053 if (status < 0)
1054 return status;
645daaab
AS
1055 }
1056
1057 /* Suspend all the interfaces and then udev itself */
a8e7c565
AS
1058 if (udev->actconfig) {
1059 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1060 intf = udev->actconfig->interface[i];
d5ec1686 1061 status = usb_suspend_interface(intf, msg);
a8e7c565
AS
1062 if (status != 0)
1063 break;
1064 }
1065 }
1066 if (status == 0)
d5ec1686 1067 status = usb_suspend_device(udev, msg);
a8e7c565
AS
1068
1069 /* If the suspend failed, resume interfaces that did get suspended */
1070 if (status != 0) {
1071 while (--i >= 0) {
1072 intf = udev->actconfig->interface[i];
d5ec1686 1073 usb_resume_interface(intf);
a8e7c565 1074 }
645daaab
AS
1075
1076 /* If the suspend succeeded, propagate it up the tree */
1077 } else if (parent)
94fcda1f 1078 usb_autosuspend_device(parent);
645daaab
AS
1079
1080 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
a8e7c565
AS
1081 return status;
1082}
1083
645daaab
AS
1084/**
1085 * usb_resume_both - resume a USB device and its interfaces
1086 * @udev: the usb_device to resume
1087 *
1088 * This is the central routine for resuming USB devices. It calls the
1089 * the resume method for @udev and then calls the resume methods for all
1090 * the interface drivers in @udev.
1091 *
1092 * Before starting the resume, the routine calls itself recursively for
1093 * the parent device of @udev, thereby propagating the change up the device
1094 * tree and assuring that @udev will be able to resume. If the parent is
1095 * unable to resume successfully, the routine fails.
1096 *
1097 * The resume method calls are subject to mutual exclusion under control
1098 * of @udev's pm_mutex. Many of these calls are also under the protection
1099 * of @udev's device lock (including all requests originating outside the
1100 * USB subsystem), but autoresume requests generated by a child device or
1101 * interface driver may not be. Usbcore will insure that the method calls
1102 * do not arrive during bind, unbind, or reset operations. However, drivers
1103 * must be prepared to handle resume calls arriving at unpredictable times.
1104 * The only way to block such calls is to do an autoresume (preventing
1105 * other autoresumes) while holding @udev's device lock (preventing outside
1106 * resumes).
1107 *
1108 * The caller must hold @udev->pm_mutex.
1109 *
1110 * This routine can run only in process context.
1111 */
a8e7c565
AS
1112int usb_resume_both(struct usb_device *udev)
1113{
645daaab 1114 int status = 0;
a8e7c565
AS
1115 int i;
1116 struct usb_interface *intf;
645daaab
AS
1117 struct usb_device *parent = udev->parent;
1118
1119 cancel_delayed_work(&udev->autosuspend);
1120 if (udev->state == USB_STATE_NOTATTACHED)
1121 return -ENODEV;
a8e7c565 1122
645daaab
AS
1123 /* Propagate the resume up the tree, if necessary */
1124 if (udev->state == USB_STATE_SUSPENDED) {
1125 if (parent) {
94fcda1f 1126 status = usb_autoresume_device(parent);
ee49fb5d
AS
1127 if (status == 0) {
1128 status = usb_resume_device(udev);
1129 if (status) {
94fcda1f 1130 usb_autosuspend_device(parent);
ee49fb5d
AS
1131
1132 /* It's possible usb_resume_device()
1133 * failed after the port was
1134 * unsuspended, causing udev to be
1135 * logically disconnected. We don't
1136 * want usb_disconnect() to autosuspend
1137 * the parent again, so tell it that
1138 * udev disconnected while still
1139 * suspended. */
1140 if (udev->state ==
1141 USB_STATE_NOTATTACHED)
1142 udev->discon_suspended = 1;
1143 }
1144 }
645daaab
AS
1145 } else {
1146
1147 /* We can't progagate beyond the USB subsystem,
1148 * so if a root hub's controller is suspended
1149 * then we're stuck. */
1150 if (udev->dev.parent->power.power_state.event !=
1151 PM_EVENT_ON)
1152 status = -EHOSTUNREACH;
ee49fb5d
AS
1153 else
1154 status = usb_resume_device(udev);
1155 }
592fbbe4
AS
1156 } else {
1157
1158 /* Needed only for setting udev->dev.power.power_state.event
1159 * and for possible debugging message. */
d5ec1686 1160 status = usb_resume_device(udev);
114b368c
AS
1161 }
1162
a8e7c565
AS
1163 if (status == 0 && udev->actconfig) {
1164 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1165 intf = udev->actconfig->interface[i];
d5ec1686 1166 usb_resume_interface(intf);
a8e7c565
AS
1167 }
1168 }
645daaab
AS
1169
1170 // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
1171 return status;
1172}
1173
1174#ifdef CONFIG_USB_SUSPEND
1175
94fcda1f
AS
1176/* Internal routine to adjust a device's usage counter and change
1177 * its autosuspend state.
1178 */
1179static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1180{
1181 int status = 0;
1182
1183 usb_pm_lock(udev);
1184 udev->pm_usage_cnt += inc_usage_cnt;
1185 WARN_ON(udev->pm_usage_cnt < 0);
1186 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1187 udev->auto_pm = 1;
1188 status = usb_resume_both(udev);
1189 if (status != 0)
1190 udev->pm_usage_cnt -= inc_usage_cnt;
1191 } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0)
1192 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
b5e795f8 1193 udev->autosuspend_delay);
94fcda1f
AS
1194 usb_pm_unlock(udev);
1195 return status;
1196}
1197
645daaab
AS
1198/**
1199 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
701f35af 1200 * @udev: the usb_device to autosuspend
645daaab
AS
1201 *
1202 * This routine should be called when a core subsystem is finished using
1203 * @udev and wants to allow it to autosuspend. Examples would be when
1204 * @udev's device file in usbfs is closed or after a configuration change.
1205 *
94fcda1f
AS
1206 * @udev's usage counter is decremented. If it or any of the usage counters
1207 * for an active interface is greater than 0, no autosuspend request will be
1208 * queued. (If an interface driver does not support autosuspend then its
1209 * usage counter is permanently positive.) Furthermore, if an interface
1210 * driver requires remote-wakeup capability during autosuspend but remote
1211 * wakeup is disabled, the autosuspend will fail.
645daaab
AS
1212 *
1213 * Often the caller will hold @udev's device lock, but this is not
1214 * necessary.
1215 *
1216 * This routine can run only in process context.
1217 */
94fcda1f 1218void usb_autosuspend_device(struct usb_device *udev)
645daaab 1219{
94fcda1f
AS
1220 int status;
1221
1222 status = usb_autopm_do_device(udev, -1);
645daaab
AS
1223 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1224 // __FUNCTION__, udev->pm_usage_cnt);
19c26239
AS
1225}
1226
1227/**
1228 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1229 * @udev: the usb_device to autosuspend
1230 *
1231 * This routine should be called when a core subsystem thinks @udev may
1232 * be ready to autosuspend.
1233 *
1234 * @udev's usage counter left unchanged. If it or any of the usage counters
1235 * for an active interface is greater than 0, or autosuspend is not allowed
1236 * for any other reason, no autosuspend request will be queued.
1237 *
1238 * This routine can run only in process context.
1239 */
1240void usb_try_autosuspend_device(struct usb_device *udev)
1241{
1242 usb_autopm_do_device(udev, 0);
1243 // dev_dbg(&udev->dev, "%s: cnt %d\n",
1244 // __FUNCTION__, udev->pm_usage_cnt);
645daaab
AS
1245}
1246
1247/**
1248 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
701f35af 1249 * @udev: the usb_device to autoresume
645daaab
AS
1250 *
1251 * This routine should be called when a core subsystem wants to use @udev
94fcda1f
AS
1252 * and needs to guarantee that it is not suspended. No autosuspend will
1253 * occur until usb_autosuspend_device is called. (Note that this will not
1254 * prevent suspend events originating in the PM core.) Examples would be
1255 * when @udev's device file in usbfs is opened or when a remote-wakeup
1256 * request is received.
645daaab 1257 *
94fcda1f
AS
1258 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1259 * However if the autoresume fails then the usage counter is re-decremented.
645daaab
AS
1260 *
1261 * Often the caller will hold @udev's device lock, but this is not
1262 * necessary (and attempting it might cause deadlock).
1263 *
1264 * This routine can run only in process context.
1265 */
94fcda1f 1266int usb_autoresume_device(struct usb_device *udev)
645daaab
AS
1267{
1268 int status;
1269
94fcda1f 1270 status = usb_autopm_do_device(udev, 1);
645daaab
AS
1271 // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
1272 // __FUNCTION__, status, udev->pm_usage_cnt);
1273 return status;
1274}
1275
af4f7606
AS
1276/* Internal routine to adjust an interface's usage counter and change
1277 * its device's autosuspend state.
1278 */
1279static int usb_autopm_do_interface(struct usb_interface *intf,
1280 int inc_usage_cnt)
1281{
1282 struct usb_device *udev = interface_to_usbdev(intf);
1283 int status = 0;
1284
1285 usb_pm_lock(udev);
1286 if (intf->condition == USB_INTERFACE_UNBOUND)
1287 status = -ENODEV;
1288 else {
1289 intf->pm_usage_cnt += inc_usage_cnt;
1290 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
1291 udev->auto_pm = 1;
1292 status = usb_resume_both(udev);
1293 if (status != 0)
1294 intf->pm_usage_cnt -= inc_usage_cnt;
1295 } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0)
1296 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
b5e795f8 1297 udev->autosuspend_delay);
af4f7606
AS
1298 }
1299 usb_pm_unlock(udev);
1300 return status;
1301}
1302
645daaab
AS
1303/**
1304 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
701f35af 1305 * @intf: the usb_interface whose counter should be decremented
645daaab
AS
1306 *
1307 * This routine should be called by an interface driver when it is
1308 * finished using @intf and wants to allow it to autosuspend. A typical
1309 * example would be a character-device driver when its device file is
1310 * closed.
1311 *
1312 * The routine decrements @intf's usage counter. When the counter reaches
1313 * 0, a delayed autosuspend request for @intf's device is queued. When
1314 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1315 * the other usage counters for the sibling interfaces and @intf's
1316 * usb_device, the device and all its interfaces will be autosuspended.
1317 *
1318 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1319 * core will not change its value other than the increment and decrement
1320 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1321 * may use this simple counter-oriented discipline or may set the value
1322 * any way it likes.
1323 *
1324 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1325 * take place only if the device's remote-wakeup facility is enabled.
1326 *
1327 * Suspend method calls queued by this routine can arrive at any time
1328 * while @intf is resumed and its usage counter is equal to 0. They are
1329 * not protected by the usb_device's lock but only by its pm_mutex.
1330 * Drivers must provide their own synchronization.
1331 *
1332 * This routine can run only in process context.
1333 */
1334void usb_autopm_put_interface(struct usb_interface *intf)
1335{
af4f7606 1336 int status;
645daaab 1337
af4f7606
AS
1338 status = usb_autopm_do_interface(intf, -1);
1339 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1340 // __FUNCTION__, status, intf->pm_usage_cnt);
645daaab
AS
1341}
1342EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1343
1344/**
1345 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
701f35af 1346 * @intf: the usb_interface whose counter should be incremented
645daaab
AS
1347 *
1348 * This routine should be called by an interface driver when it wants to
1349 * use @intf and needs to guarantee that it is not suspended. In addition,
1350 * the routine prevents @intf from being autosuspended subsequently. (Note
1351 * that this will not prevent suspend events originating in the PM core.)
1352 * This prevention will persist until usb_autopm_put_interface() is called
1353 * or @intf is unbound. A typical example would be a character-device
1354 * driver when its device file is opened.
1355 *
1356 * The routine increments @intf's usage counter. So long as the counter
1357 * is greater than 0, autosuspend will not be allowed for @intf or its
1358 * usb_device. When the driver is finished using @intf it should call
1359 * usb_autopm_put_interface() to decrement the usage counter and queue
1360 * a delayed autosuspend request (if the counter is <= 0).
1361 *
1362 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1363 * core will not change its value other than the increment and decrement
1364 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1365 * may use this simple counter-oriented discipline or may set the value
1366 * any way it likes.
1367 *
1368 * Resume method calls generated by this routine can arrive at any time
1369 * while @intf is suspended. They are not protected by the usb_device's
1370 * lock but only by its pm_mutex. Drivers must provide their own
1371 * synchronization.
1372 *
1373 * This routine can run only in process context.
1374 */
1375int usb_autopm_get_interface(struct usb_interface *intf)
1376{
af4f7606 1377 int status;
645daaab 1378
af4f7606 1379 status = usb_autopm_do_interface(intf, 1);
645daaab
AS
1380 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1381 // __FUNCTION__, status, intf->pm_usage_cnt);
a8e7c565
AS
1382 return status;
1383}
645daaab
AS
1384EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1385
692a186c
AS
1386/**
1387 * usb_autopm_set_interface - set a USB interface's autosuspend state
1388 * @intf: the usb_interface whose state should be set
1389 *
1390 * This routine sets the autosuspend state of @intf's device according
1391 * to @intf's usage counter, which the caller must have set previously.
1392 * If the counter is <= 0, the device is autosuspended (if it isn't
1393 * already suspended and if nothing else prevents the autosuspend). If
1394 * the counter is > 0, the device is autoresumed (if it isn't already
1395 * awake).
1396 */
1397int usb_autopm_set_interface(struct usb_interface *intf)
1398{
1399 int status;
1400
1401 status = usb_autopm_do_interface(intf, 0);
1402 // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
1403 // __FUNCTION__, status, intf->pm_usage_cnt);
1404 return status;
1405}
1406EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1407
645daaab 1408#endif /* CONFIG_USB_SUSPEND */
a8e7c565 1409
1cc8a25d
AS
1410static int usb_suspend(struct device *dev, pm_message_t message)
1411{
1412 int status;
1413
645daaab
AS
1414 if (is_usb_device(dev)) {
1415 struct usb_device *udev = to_usb_device(dev);
1416
e0318ebf 1417 usb_pm_lock(udev);
645daaab
AS
1418 udev->auto_pm = 0;
1419 status = usb_suspend_both(udev, message);
e0318ebf 1420 usb_pm_unlock(udev);
645daaab 1421 } else
a8e7c565 1422 status = 0;
1cc8a25d
AS
1423 return status;
1424}
1425
1426static int usb_resume(struct device *dev)
1427{
1428 int status;
1429
a8e7c565 1430 if (is_usb_device(dev)) {
645daaab
AS
1431 struct usb_device *udev = to_usb_device(dev);
1432
e0318ebf 1433 usb_pm_lock(udev);
645daaab
AS
1434 udev->auto_pm = 0;
1435 status = usb_resume_both(udev);
e0318ebf 1436 usb_pm_unlock(udev);
a8e7c565
AS
1437
1438 /* Rebind drivers that had no suspend method? */
1439 } else
1440 status = 0;
1cc8a25d
AS
1441 return status;
1442}
1443
36e56a34
AS
1444#endif /* CONFIG_PM */
1445
1446struct bus_type usb_bus_type = {
1447 .name = "usb",
1448 .match = usb_device_match,
1449 .uevent = usb_uevent,
1450#ifdef CONFIG_PM
782da727
AS
1451 .suspend = usb_suspend,
1452 .resume = usb_resume,
36e56a34
AS
1453#endif
1454};
This page took 0.253726 seconds and 5 git commands to generate.