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