IB/mlx4: Use vmalloc for WR buffers when needed
[linux-2.6-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 25#include <linux/device.h>
5a0e3ad6 26#include <linux/slab.h>
f940fcd8 27#include <linux/export.h>
ddae41be 28#include <linux/usb.h>
6bc6cff5 29#include <linux/usb/quirks.h>
27729aad 30#include <linux/usb/hcd.h>
27729aad 31
ddae41be
GKH
32#include "usb.h"
33
20dfdad7 34
733260ff
GKH
35/*
36 * Adds a new dynamic USBdevice ID to this driver,
37 * and cause the driver to probe for all devices again.
38 */
93bacefc 39ssize_t usb_store_new_id(struct usb_dynids *dynids,
2fc82c2d 40 const struct usb_device_id *id_table,
93bacefc
GKH
41 struct device_driver *driver,
42 const char *buf, size_t count)
733260ff 43{
733260ff
GKH
44 struct usb_dynid *dynid;
45 u32 idVendor = 0;
46 u32 idProduct = 0;
ff231db8 47 unsigned int bInterfaceClass = 0;
2fc82c2d 48 u32 refVendor, refProduct;
733260ff 49 int fields = 0;
1b21d5e1 50 int retval = 0;
733260ff 51
2fc82c2d
WS
52 fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass, &refVendor, &refProduct);
733260ff
GKH
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;
c63fe8f6 65 if (fields > 2 && bInterfaceClass) {
7f196caf
CE
66 if (bInterfaceClass > 255) {
67 retval = -EINVAL;
68 goto fail;
69 }
c63fe8f6 70
ff231db8
JD
71 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
72 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
73 }
733260ff 74
2fc82c2d
WS
75 if (fields > 4) {
76 const struct usb_device_id *id = id_table;
77
7f196caf
CE
78 if (!id) {
79 retval = -ENODEV;
80 goto fail;
81 }
1b9fb31f 82
2fc82c2d 83 for (; id->match_flags; id++)
52a6966c 84 if (id->idVendor == refVendor && id->idProduct == refProduct)
2fc82c2d 85 break;
52a6966c 86
7f196caf 87 if (id->match_flags) {
52a6966c 88 dynid->id.driver_info = id->driver_info;
7f196caf
CE
89 } else {
90 retval = -ENODEV;
91 goto fail;
92 }
2fc82c2d
WS
93 }
94
93bacefc 95 spin_lock(&dynids->lock);
e5dd0115 96 list_add_tail(&dynid->node, &dynids->list);
93bacefc 97 spin_unlock(&dynids->lock);
733260ff 98
cef9bc56 99 retval = driver_attach(driver);
733260ff 100
1b21d5e1
GKH
101 if (retval)
102 return retval;
733260ff 103 return count;
7f196caf
CE
104
105fail:
106 kfree(dynid);
107 return retval;
733260ff 108}
93bacefc
GKH
109EXPORT_SYMBOL_GPL(usb_store_new_id);
110
ef206f3f 111ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
e6bbcef0
BM
112{
113 struct usb_dynid *dynid;
e6bbcef0
BM
114 size_t count = 0;
115
ef206f3f 116 list_for_each_entry(dynid, &dynids->list, node)
e6bbcef0
BM
117 if (dynid->id.bInterfaceClass != 0)
118 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
119 dynid->id.idVendor, dynid->id.idProduct,
120 dynid->id.bInterfaceClass);
121 else
122 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
123 dynid->id.idVendor, dynid->id.idProduct);
124 return count;
125}
ef206f3f
BM
126EXPORT_SYMBOL_GPL(usb_show_dynids);
127
598d0361 128static ssize_t new_id_show(struct device_driver *driver, char *buf)
ef206f3f
BM
129{
130 struct usb_driver *usb_drv = to_usb_driver(driver);
131
132 return usb_show_dynids(&usb_drv->dynids, buf);
133}
e6bbcef0 134
598d0361 135static ssize_t new_id_store(struct device_driver *driver,
93bacefc
GKH
136 const char *buf, size_t count)
137{
138 struct usb_driver *usb_drv = to_usb_driver(driver);
139
2fc82c2d 140 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
93bacefc 141}
598d0361 142static DRIVER_ATTR_RW(new_id);
733260ff 143
598d0361
GKH
144/*
145 * Remove a USB device ID from this driver
0c7a2b72 146 */
598d0361
GKH
147static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
148 size_t count)
0c7a2b72
CR
149{
150 struct usb_dynid *dynid, *n;
151 struct usb_driver *usb_driver = to_usb_driver(driver);
ac08de32
AC
152 u32 idVendor;
153 u32 idProduct;
154 int fields;
0c7a2b72
CR
155
156 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
157 if (fields < 2)
158 return -EINVAL;
159
160 spin_lock(&usb_driver->dynids.lock);
161 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
162 struct usb_device_id *id = &dynid->id;
79a02744 163
0c7a2b72
CR
164 if ((id->idVendor == idVendor) &&
165 (id->idProduct == idProduct)) {
166 list_del(&dynid->node);
167 kfree(dynid);
0c7a2b72
CR
168 break;
169 }
170 }
171 spin_unlock(&usb_driver->dynids.lock);
0c7a2b72
CR
172 return count;
173}
598d0361
GKH
174
175static ssize_t remove_id_show(struct device_driver *driver, char *buf)
176{
177 return new_id_show(driver, buf);
178}
179static DRIVER_ATTR_RW(remove_id);
0c7a2b72 180
ed283e9f 181static int usb_create_newid_files(struct usb_driver *usb_drv)
733260ff
GKH
182{
183 int error = 0;
184
ba9dc657
GKH
185 if (usb_drv->no_dynamic_id)
186 goto exit;
187
ed283e9f 188 if (usb_drv->probe != NULL) {
15147ffd
GKH
189 error = driver_create_file(&usb_drv->drvwrap.driver,
190 &driver_attr_new_id);
ed283e9f
AS
191 if (error == 0) {
192 error = driver_create_file(&usb_drv->drvwrap.driver,
193 &driver_attr_remove_id);
194 if (error)
195 driver_remove_file(&usb_drv->drvwrap.driver,
196 &driver_attr_new_id);
197 }
198 }
ba9dc657 199exit:
733260ff
GKH
200 return error;
201}
202
ed283e9f 203static void usb_remove_newid_files(struct usb_driver *usb_drv)
ba9dc657
GKH
204{
205 if (usb_drv->no_dynamic_id)
206 return;
207
ed283e9f 208 if (usb_drv->probe != NULL) {
15147ffd 209 driver_remove_file(&usb_drv->drvwrap.driver,
0c7a2b72 210 &driver_attr_remove_id);
ed283e9f
AS
211 driver_remove_file(&usb_drv->drvwrap.driver,
212 &driver_attr_new_id);
213 }
0c7a2b72
CR
214}
215
733260ff
GKH
216static void usb_free_dynids(struct usb_driver *usb_drv)
217{
218 struct usb_dynid *dynid, *n;
219
220 spin_lock(&usb_drv->dynids.lock);
221 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
222 list_del(&dynid->node);
223 kfree(dynid);
224 }
225 spin_unlock(&usb_drv->dynids.lock);
226}
733260ff
GKH
227
228static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
229 struct usb_driver *drv)
230{
231 struct usb_dynid *dynid;
232
233 spin_lock(&drv->dynids.lock);
234 list_for_each_entry(dynid, &drv->dynids.list, node) {
235 if (usb_match_one_id(intf, &dynid->id)) {
236 spin_unlock(&drv->dynids.lock);
237 return &dynid->id;
238 }
239 }
240 spin_unlock(&drv->dynids.lock);
241 return NULL;
242}
243
244
8bb54ab5
AS
245/* called from driver core with dev locked */
246static int usb_probe_device(struct device *dev)
247{
248 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
55129666 249 struct usb_device *udev = to_usb_device(dev);
9bbdf1e0 250 int error = 0;
8bb54ab5 251
441b62c1 252 dev_dbg(dev, "%s\n", __func__);
8bb54ab5 253
8bb54ab5
AS
254 /* TODO: Add real matching code */
255
645daaab 256 /* The device should always appear to be in use
02582e9b 257 * unless the driver supports autosuspend.
645daaab 258 */
9bbdf1e0
AS
259 if (!udriver->supports_autosuspend)
260 error = usb_autoresume_device(udev);
645daaab 261
9bbdf1e0
AS
262 if (!error)
263 error = udriver->probe(udev);
8bb54ab5
AS
264 return error;
265}
266
267/* called from driver core with dev locked */
268static int usb_unbind_device(struct device *dev)
269{
9bbdf1e0 270 struct usb_device *udev = to_usb_device(dev);
8bb54ab5
AS
271 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
272
9bbdf1e0
AS
273 udriver->disconnect(udev);
274 if (!udriver->supports_autosuspend)
275 usb_autosuspend_device(udev);
8bb54ab5
AS
276 return 0;
277}
278
8bb54ab5 279/* called from driver core with dev locked */
ddae41be
GKH
280static int usb_probe_interface(struct device *dev)
281{
8bb54ab5 282 struct usb_driver *driver = to_usb_driver(dev->driver);
55129666
KS
283 struct usb_interface *intf = to_usb_interface(dev);
284 struct usb_device *udev = interface_to_usbdev(intf);
ddae41be
GKH
285 const struct usb_device_id *id;
286 int error = -ENODEV;
8306095f 287 int lpm_disable_error;
ddae41be 288
441b62c1 289 dev_dbg(dev, "%s\n", __func__);
ddae41be 290
78d9a487 291 intf->needs_binding = 0;
ddae41be 292
7cbe5dca 293 if (usb_device_is_owned(udev))
0f3dda9f 294 return error;
7cbe5dca 295
2c044a48
GKH
296 if (udev->authorized == 0) {
297 dev_err(&intf->dev, "Device is not authorized for usage\n");
0f3dda9f 298 return error;
8d1f8573
SK
299 } else if (intf->authorized == 0) {
300 dev_err(&intf->dev, "Interface %d is not authorized for usage\n",
301 intf->altsetting->desc.bInterfaceNumber);
302 return error;
2c044a48 303 }
72230abb 304
31c6bf70 305 id = usb_match_dynamic_id(intf, driver);
733260ff 306 if (!id)
31c6bf70 307 id = usb_match_id(intf, driver->id_table);
0f3dda9f
AS
308 if (!id)
309 return error;
55151d7d 310
0f3dda9f
AS
311 dev_dbg(dev, "%s - got id\n", __func__);
312
313 error = usb_autoresume_device(udev);
314 if (error)
315 return error;
316
0f3dda9f 317 intf->condition = USB_INTERFACE_BINDING;
645daaab 318
571dc79d 319 /* Probed interfaces are initially active. They are
9bbdf1e0
AS
320 * runtime-PM-enabled only if the driver has autosuspend support.
321 * They are sensitive to their children's power states.
0f3dda9f 322 */
9bbdf1e0
AS
323 pm_runtime_set_active(dev);
324 pm_suspend_ignore_children(dev, false);
325 if (driver->supports_autosuspend)
326 pm_runtime_enable(dev);
0f3dda9f 327
8306095f
SS
328 /* If the new driver doesn't allow hub-initiated LPM, and we can't
329 * disable hub-initiated LPM, then fail the probe.
330 *
331 * Otherwise, leaving LPM enabled should be harmless, because the
332 * endpoint intervals should remain the same, and the U1/U2 timeouts
333 * should remain the same.
334 *
335 * If we need to install alt setting 0 before probe, or another alt
336 * setting during probe, that should also be fine. usb_set_interface()
337 * will attempt to disable LPM, and fail if it can't disable it.
338 */
339 lpm_disable_error = usb_unlocked_disable_lpm(udev);
340 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
341 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
342 __func__, driver->name);
343 error = lpm_disable_error;
344 goto err;
345 }
346
0f3dda9f
AS
347 /* Carry out a deferred switch to altsetting 0 */
348 if (intf->needs_altsetting0) {
349 error = usb_set_interface(udev, intf->altsetting[0].
350 desc.bInterfaceNumber, 0);
351 if (error < 0)
352 goto err;
353 intf->needs_altsetting0 = 0;
ddae41be
GKH
354 }
355
0f3dda9f
AS
356 error = driver->probe(intf, id);
357 if (error)
358 goto err;
359
360 intf->condition = USB_INTERFACE_BOUND;
8306095f
SS
361
362 /* If the LPM disable succeeded, balance the ref counts. */
363 if (!lpm_disable_error)
364 usb_unlocked_enable_lpm(udev);
365
0f3dda9f 366 usb_autosuspend_device(udev);
ddae41be 367 return error;
1e5ea5e3 368
0f3dda9f 369 err:
e714fad0 370 usb_set_intfdata(intf, NULL);
1e5ea5e3
ON
371 intf->needs_remote_wakeup = 0;
372 intf->condition = USB_INTERFACE_UNBOUND;
9bbdf1e0 373
d01f87c0
SS
374 /* If the LPM disable succeeded, balance the ref counts. */
375 if (!lpm_disable_error)
376 usb_unlocked_enable_lpm(udev);
377
9bbdf1e0 378 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
89842ae6
AS
379 if (driver->supports_autosuspend)
380 pm_runtime_disable(dev);
9bbdf1e0
AS
381 pm_runtime_set_suspended(dev);
382
1e5ea5e3
ON
383 usb_autosuspend_device(udev);
384 return error;
ddae41be
GKH
385}
386
8bb54ab5 387/* called from driver core with dev locked */
ddae41be
GKH
388static int usb_unbind_interface(struct device *dev)
389{
8bb54ab5 390 struct usb_driver *driver = to_usb_driver(dev->driver);
ddae41be 391 struct usb_interface *intf = to_usb_interface(dev);
6343e8bf 392 struct usb_host_endpoint *ep, **eps = NULL;
645daaab 393 struct usb_device *udev;
6343e8bf 394 int i, j, error, r, lpm_disable_error;
ddae41be
GKH
395
396 intf->condition = USB_INTERFACE_UNBINDING;
397
645daaab
AS
398 /* Autoresume for set_interface call below */
399 udev = interface_to_usbdev(intf);
94fcda1f 400 error = usb_autoresume_device(udev);
645daaab 401
8306095f
SS
402 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
403 * the driver is unbound. If LPM isn't disabled, that's fine because it
404 * wouldn't be enabled unless all the bound interfaces supported
405 * hub-initiated LPM.
406 */
407 lpm_disable_error = usb_unlocked_disable_lpm(udev);
408
1299cff9
AS
409 /*
410 * Terminate all URBs for this interface unless the driver
411 * supports "soft" unbinding and the device is still present.
9da82bd4 412 */
1299cff9 413 if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
ddeac4e7 414 usb_disable_interface(udev, intf, false);
ddae41be 415
8bb54ab5 416 driver->disconnect(intf);
ddae41be 417
6343e8bf
HG
418 /* Free streams */
419 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
420 ep = &intf->cur_altsetting->endpoint[i];
421 if (ep->streams == 0)
422 continue;
423 if (j == 0) {
9766f251 424 eps = kmalloc_array(USB_MAXENDPOINTS, sizeof(void *),
6343e8bf 425 GFP_KERNEL);
9766f251 426 if (!eps)
6343e8bf 427 break;
6343e8bf
HG
428 }
429 eps[j++] = ep;
430 }
431 if (j) {
432 usb_free_streams(intf, eps, j, GFP_KERNEL);
433 kfree(eps);
434 }
435
55151d7d
AS
436 /* Reset other interface state.
437 * We cannot do a Set-Interface if the device is suspended or
438 * if it is prepared for a system sleep (since installing a new
439 * altsetting means creating new endpoint device entries).
440 * When either of these happens, defer the Set-Interface.
441 */
2caf7fcd
AS
442 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
443 /* Already in altsetting 0 so skip Set-Interface.
444 * Just re-enable it without affecting the endpoint toggles.
445 */
446 usb_enable_interface(udev, intf, false);
f76b168b 447 } else if (!error && !intf->dev.power.is_prepared) {
1e5ea5e3 448 r = usb_set_interface(udev, intf->altsetting[0].
55151d7d 449 desc.bInterfaceNumber, 0);
1e5ea5e3
ON
450 if (r < 0)
451 intf->needs_altsetting0 = 1;
452 } else {
55151d7d 453 intf->needs_altsetting0 = 1;
1e5ea5e3 454 }
ddae41be 455 usb_set_intfdata(intf, NULL);
645daaab 456
ddae41be 457 intf->condition = USB_INTERFACE_UNBOUND;
645daaab
AS
458 intf->needs_remote_wakeup = 0;
459
8306095f
SS
460 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
461 if (!lpm_disable_error)
462 usb_unlocked_enable_lpm(udev);
463
9bbdf1e0 464 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
89842ae6
AS
465 if (driver->supports_autosuspend)
466 pm_runtime_disable(dev);
9bbdf1e0
AS
467 pm_runtime_set_suspended(dev);
468
469 /* Undo any residual pm_autopm_get_interface_* calls */
470 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
471 usb_autopm_put_interface_no_suspend(intf);
472 atomic_set(&intf->pm_usage_cnt, 0);
473
645daaab 474 if (!error)
94fcda1f 475 usb_autosuspend_device(udev);
ddae41be
GKH
476
477 return 0;
478}
479
36e56a34
AS
480/**
481 * usb_driver_claim_interface - bind a driver to an interface
482 * @driver: the driver to be bound
483 * @iface: the interface to which it will be bound; must be in the
484 * usb device's active configuration
485 * @priv: driver data associated with that interface
486 *
487 * This is used by usb device drivers that need to claim more than one
488 * interface on a device when probing (audio and acm are current examples).
489 * No device driver should directly modify internal usb_interface or
490 * usb_device structure members.
491 *
492 * Few drivers should need to use this routine, since the most natural
493 * way to bind to an interface is to return the private data from
494 * the driver's probe() method.
495 *
341487a8
GKH
496 * Callers must own the device lock, so driver probe() entries don't need
497 * extra locking, but other call contexts may need to explicitly claim that
498 * lock.
626f090c
YB
499 *
500 * Return: 0 on success.
36e56a34
AS
501 */
502int usb_driver_claim_interface(struct usb_driver *driver,
2c044a48 503 struct usb_interface *iface, void *priv)
36e56a34
AS
504{
505 struct device *dev = &iface->dev;
8306095f 506 struct usb_device *udev;
1b21d5e1 507 int retval = 0;
8306095f 508 int lpm_disable_error;
36e56a34
AS
509
510 if (dev->driver)
511 return -EBUSY;
512
8d1f8573
SK
513 /* reject claim if interface is not authorized */
514 if (!iface->authorized)
515 return -ENODEV;
516
8306095f
SS
517 udev = interface_to_usbdev(iface);
518
8bb54ab5 519 dev->driver = &driver->drvwrap.driver;
36e56a34 520 usb_set_intfdata(iface, priv);
78d9a487 521 iface->needs_binding = 0;
645daaab 522
36e56a34 523 iface->condition = USB_INTERFACE_BOUND;
9bbdf1e0 524
8306095f
SS
525 /* Disable LPM until this driver is bound. */
526 lpm_disable_error = usb_unlocked_disable_lpm(udev);
527 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
528 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
529 __func__, driver->name);
530 return -ENOMEM;
531 }
532
89842ae6
AS
533 /* Claimed interfaces are initially inactive (suspended) and
534 * runtime-PM-enabled, but only if the driver has autosuspend
535 * support. Otherwise they are marked active, to prevent the
536 * device from being autosuspended, but left disabled. In either
537 * case they are sensitive to their children's power states.
9bbdf1e0 538 */
9bbdf1e0
AS
539 pm_suspend_ignore_children(dev, false);
540 if (driver->supports_autosuspend)
541 pm_runtime_enable(dev);
89842ae6
AS
542 else
543 pm_runtime_set_active(dev);
36e56a34
AS
544
545 /* if interface was already added, bind now; else let
546 * the future device_add() bind it, bypassing probe()
547 */
548 if (device_is_registered(dev))
1b21d5e1 549 retval = device_bind_driver(dev);
36e56a34 550
8306095f
SS
551 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
552 if (!lpm_disable_error)
553 usb_unlocked_enable_lpm(udev);
554
1b21d5e1 555 return retval;
36e56a34 556}
782e70c6 557EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
36e56a34
AS
558
559/**
560 * usb_driver_release_interface - unbind a driver from an interface
561 * @driver: the driver to be unbound
562 * @iface: the interface from which it will be unbound
563 *
564 * This can be used by drivers to release an interface without waiting
565 * for their disconnect() methods to be called. In typical cases this
566 * also causes the driver disconnect() method to be called.
567 *
568 * This call is synchronous, and may not be used in an interrupt context.
341487a8
GKH
569 * Callers must own the device lock, so driver disconnect() entries don't
570 * need extra locking, but other call contexts may need to explicitly claim
571 * that lock.
36e56a34
AS
572 */
573void usb_driver_release_interface(struct usb_driver *driver,
574 struct usb_interface *iface)
575{
576 struct device *dev = &iface->dev;
577
578 /* this should never happen, don't release something that's not ours */
8bb54ab5 579 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
36e56a34
AS
580 return;
581
582 /* don't release from within disconnect() */
583 if (iface->condition != USB_INTERFACE_BOUND)
584 return;
91f8d063 585 iface->condition = USB_INTERFACE_UNBINDING;
36e56a34 586
91f8d063
AS
587 /* Release via the driver core only if the interface
588 * has already been registered
589 */
36e56a34 590 if (device_is_registered(dev)) {
36e56a34 591 device_release_driver(dev);
dc023dce 592 } else {
8e9394ce 593 device_lock(dev);
91f8d063
AS
594 usb_unbind_interface(dev);
595 dev->driver = NULL;
8e9394ce 596 device_unlock(dev);
36e56a34 597 }
36e56a34 598}
782e70c6 599EXPORT_SYMBOL_GPL(usb_driver_release_interface);
36e56a34 600
733260ff 601/* returns 0 if no match, 1 if match */
bb417020 602int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
733260ff 603{
733260ff
GKH
604 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
605 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
606 return 0;
607
608 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
609 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
610 return 0;
611
612 /* No need to test id->bcdDevice_lo != 0, since 0 is never
613 greater than any unsigned number. */
614 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
615 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
616 return 0;
617
618 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
619 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
620 return 0;
621
622 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
623 (id->bDeviceClass != dev->descriptor.bDeviceClass))
624 return 0;
625
626 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
2c044a48 627 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
733260ff
GKH
628 return 0;
629
630 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
631 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
632 return 0;
633
bb417020
GKH
634 return 1;
635}
636
637/* returns 0 if no match, 1 if match */
80da2e0d
LP
638int usb_match_one_id_intf(struct usb_device *dev,
639 struct usb_host_interface *intf,
640 const struct usb_device_id *id)
bb417020 641{
81df2d59 642 /* The interface class, subclass, protocol and number should never be
93c8bf45
AS
643 * checked for a match if the device class is Vendor Specific,
644 * unless the match record specifies the Vendor ID. */
645 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
646 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
647 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
648 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
81df2d59
BM
649 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
650 USB_DEVICE_ID_MATCH_INT_NUMBER)))
93c8bf45
AS
651 return 0;
652
733260ff
GKH
653 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
654 (id->bInterfaceClass != intf->desc.bInterfaceClass))
655 return 0;
656
657 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
658 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
659 return 0;
660
661 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
662 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
663 return 0;
664
81df2d59
BM
665 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
666 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
667 return 0;
668
733260ff
GKH
669 return 1;
670}
80da2e0d
LP
671
672/* returns 0 if no match, 1 if match */
673int usb_match_one_id(struct usb_interface *interface,
674 const struct usb_device_id *id)
675{
676 struct usb_host_interface *intf;
677 struct usb_device *dev;
678
679 /* proc_connectinfo in devio.c may call us with id == NULL. */
680 if (id == NULL)
681 return 0;
682
683 intf = interface->cur_altsetting;
684 dev = interface_to_usbdev(interface);
685
686 if (!usb_match_device(dev, id))
687 return 0;
688
689 return usb_match_one_id_intf(dev, intf, id);
690}
93bacefc
GKH
691EXPORT_SYMBOL_GPL(usb_match_one_id);
692
ddae41be
GKH
693/**
694 * usb_match_id - find first usb_device_id matching device or interface
695 * @interface: the interface of interest
696 * @id: array of usb_device_id structures, terminated by zero entry
697 *
698 * usb_match_id searches an array of usb_device_id's and returns
699 * the first one matching the device or interface, or null.
700 * This is used when binding (or rebinding) a driver to an interface.
701 * Most USB device drivers will use this indirectly, through the usb core,
702 * but some layered driver frameworks use it directly.
703 * These device tables are exported with MODULE_DEVICE_TABLE, through
704 * modutils, to support the driver loading functionality of USB hotplugging.
705 *
626f090c
YB
706 * Return: The first matching usb_device_id, or %NULL.
707 *
ddae41be
GKH
708 * What Matches:
709 *
710 * The "match_flags" element in a usb_device_id controls which
711 * members are used. If the corresponding bit is set, the
712 * value in the device_id must match its corresponding member
713 * in the device or interface descriptor, or else the device_id
714 * does not match.
715 *
716 * "driver_info" is normally used only by device drivers,
717 * but you can create a wildcard "matches anything" usb_device_id
718 * as a driver's "modules.usbmap" entry if you provide an id with
719 * only a nonzero "driver_info" field. If you do this, the USB device
720 * driver's probe() routine should use additional intelligence to
721 * decide whether to bind to the specified interface.
722 *
723 * What Makes Good usb_device_id Tables:
724 *
725 * The match algorithm is very simple, so that intelligence in
726 * driver selection must come from smart driver id records.
727 * Unless you have good reasons to use another selection policy,
728 * provide match elements only in related groups, and order match
729 * specifiers from specific to general. Use the macros provided
730 * for that purpose if you can.
731 *
732 * The most specific match specifiers use device descriptor
733 * data. These are commonly used with product-specific matches;
734 * the USB_DEVICE macro lets you provide vendor and product IDs,
735 * and you can also match against ranges of product revisions.
736 * These are widely used for devices with application or vendor
737 * specific bDeviceClass values.
738 *
739 * Matches based on device class/subclass/protocol specifications
740 * are slightly more general; use the USB_DEVICE_INFO macro, or
741 * its siblings. These are used with single-function devices
742 * where bDeviceClass doesn't specify that each interface has
743 * its own class.
744 *
745 * Matches based on interface class/subclass/protocol are the
746 * most general; they let drivers bind to any interface on a
747 * multiple-function device. Use the USB_INTERFACE_INFO
748 * macro, or its siblings, to match class-per-interface style
93c8bf45
AS
749 * devices (as recorded in bInterfaceClass).
750 *
751 * Note that an entry created by USB_INTERFACE_INFO won't match
752 * any interface if the device class is set to Vendor-Specific.
753 * This is deliberate; according to the USB spec the meanings of
754 * the interface class/subclass/protocol for these devices are also
755 * vendor-specific, and hence matching against a standard product
756 * class wouldn't work anyway. If you really want to use an
757 * interface-based match for such a device, create a match record
758 * that also specifies the vendor ID. (Unforunately there isn't a
759 * standard macro for creating records like this.)
ddae41be
GKH
760 *
761 * Within those groups, remember that not all combinations are
762 * meaningful. For example, don't give a product version range
763 * without vendor and product IDs; or specify a protocol without
764 * its associated class and subclass.
765 */
766const struct usb_device_id *usb_match_id(struct usb_interface *interface,
767 const struct usb_device_id *id)
768{
ddae41be
GKH
769 /* proc_connectinfo in devio.c may call us with id == NULL. */
770 if (id == NULL)
771 return NULL;
772
ddae41be
GKH
773 /* It is important to check that id->driver_info is nonzero,
774 since an entry that is all zeroes except for a nonzero
775 id->driver_info is the way to create an entry that
776 indicates that the driver want to examine every
777 device and interface. */
de6f92b9
GKH
778 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
779 id->bInterfaceClass || id->driver_info; id++) {
733260ff
GKH
780 if (usb_match_one_id(interface, id))
781 return id;
ddae41be
GKH
782 }
783
784 return NULL;
785}
782e70c6 786EXPORT_SYMBOL_GPL(usb_match_id);
ddae41be 787
8bb22d2b 788static int usb_device_match(struct device *dev, struct device_driver *drv)
ddae41be 789{
8bb54ab5
AS
790 /* devices and interfaces are handled separately */
791 if (is_usb_device(dev)) {
ddae41be 792
8bb54ab5
AS
793 /* interface drivers never match devices */
794 if (!is_usb_device_driver(drv))
795 return 0;
ddae41be 796
8bb54ab5 797 /* TODO: Add real matching code */
ddae41be
GKH
798 return 1;
799
55129666 800 } else if (is_usb_interface(dev)) {
8bb54ab5
AS
801 struct usb_interface *intf;
802 struct usb_driver *usb_drv;
803 const struct usb_device_id *id;
804
805 /* device drivers never match interfaces */
806 if (is_usb_device_driver(drv))
807 return 0;
808
809 intf = to_usb_interface(dev);
810 usb_drv = to_usb_driver(drv);
811
812 id = usb_match_id(intf, usb_drv->id_table);
813 if (id)
814 return 1;
815
816 id = usb_match_dynamic_id(intf, usb_drv);
817 if (id)
818 return 1;
819 }
820
ddae41be
GKH
821 return 0;
822}
823
7eff2e7a 824static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
36e56a34 825{
36e56a34 826 struct usb_device *usb_dev;
36e56a34 827
55129666 828 if (is_usb_device(dev)) {
782da727 829 usb_dev = to_usb_device(dev);
55129666 830 } else if (is_usb_interface(dev)) {
9f8b17e6 831 struct usb_interface *intf = to_usb_interface(dev);
55129666 832
8bb54ab5 833 usb_dev = interface_to_usbdev(intf);
55129666
KS
834 } else {
835 return 0;
8bb54ab5 836 }
36e56a34
AS
837
838 if (usb_dev->devnum < 0) {
cceffe93 839 /* driver is often null here; dev_dbg() would oops */
7071a3ce 840 pr_debug("usb %s: already deleted?\n", dev_name(dev));
36e56a34
AS
841 return -ENODEV;
842 }
843 if (!usb_dev->bus) {
7071a3ce 844 pr_debug("usb %s: bus removed?\n", dev_name(dev));
36e56a34
AS
845 return -ENODEV;
846 }
847
36e56a34 848 /* per-device configurations are common */
7eff2e7a 849 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
36e56a34
AS
850 le16_to_cpu(usb_dev->descriptor.idVendor),
851 le16_to_cpu(usb_dev->descriptor.idProduct),
852 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
853 return -ENOMEM;
854
855 /* class-based driver binding models */
7eff2e7a 856 if (add_uevent_var(env, "TYPE=%d/%d/%d",
36e56a34
AS
857 usb_dev->descriptor.bDeviceClass,
858 usb_dev->descriptor.bDeviceSubClass,
859 usb_dev->descriptor.bDeviceProtocol))
860 return -ENOMEM;
861
36e56a34
AS
862 return 0;
863}
864
ddae41be 865/**
8bb54ab5
AS
866 * usb_register_device_driver - register a USB device (not interface) driver
867 * @new_udriver: USB operations for the device driver
2143acc6 868 * @owner: module owner of this driver.
ddae41be 869 *
8bb54ab5
AS
870 * Registers a USB device driver with the USB core. The list of
871 * unattached devices will be rescanned whenever a new driver is
872 * added, allowing the new driver to attach to any recognized devices.
626f090c
YB
873 *
874 * Return: A negative error code on failure and 0 on success.
8bb54ab5
AS
875 */
876int usb_register_device_driver(struct usb_device_driver *new_udriver,
877 struct module *owner)
878{
879 int retval = 0;
880
881 if (usb_disabled())
882 return -ENODEV;
883
884 new_udriver->drvwrap.for_devices = 1;
9f9af82f 885 new_udriver->drvwrap.driver.name = new_udriver->name;
8bb54ab5
AS
886 new_udriver->drvwrap.driver.bus = &usb_bus_type;
887 new_udriver->drvwrap.driver.probe = usb_probe_device;
888 new_udriver->drvwrap.driver.remove = usb_unbind_device;
889 new_udriver->drvwrap.driver.owner = owner;
890
891 retval = driver_register(&new_udriver->drvwrap.driver);
892
fb28d58b 893 if (!retval)
8bb54ab5
AS
894 pr_info("%s: registered new device driver %s\n",
895 usbcore_name, new_udriver->name);
fb28d58b 896 else
8bb54ab5
AS
897 printk(KERN_ERR "%s: error %d registering device "
898 " driver %s\n",
899 usbcore_name, retval, new_udriver->name);
8bb54ab5
AS
900
901 return retval;
902}
903EXPORT_SYMBOL_GPL(usb_register_device_driver);
904
905/**
906 * usb_deregister_device_driver - unregister a USB device (not interface) driver
907 * @udriver: USB operations of the device driver to unregister
908 * Context: must be able to sleep
909 *
910 * Unlinks the specified driver from the internal USB driver list.
911 */
912void usb_deregister_device_driver(struct usb_device_driver *udriver)
913{
914 pr_info("%s: deregistering device driver %s\n",
915 usbcore_name, udriver->name);
916
917 driver_unregister(&udriver->drvwrap.driver);
8bb54ab5
AS
918}
919EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
920
921/**
922 * usb_register_driver - register a USB interface driver
923 * @new_driver: USB operations for the interface driver
924 * @owner: module owner of this driver.
892705a1 925 * @mod_name: module name string
8bb54ab5
AS
926 *
927 * Registers a USB interface driver with the USB core. The list of
928 * unattached interfaces will be rescanned whenever a new driver is
929 * added, allowing the new driver to attach to any recognized interfaces.
626f090c
YB
930 *
931 * Return: A negative error code on failure and 0 on success.
ddae41be
GKH
932 *
933 * NOTE: if you want your driver to use the USB major number, you must call
934 * usb_register_dev() to enable that functionality. This function no longer
935 * takes care of that.
936 */
80f745fb
GKH
937int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
938 const char *mod_name)
ddae41be
GKH
939{
940 int retval = 0;
941
942 if (usb_disabled())
943 return -ENODEV;
944
8bb54ab5 945 new_driver->drvwrap.for_devices = 0;
9f9af82f 946 new_driver->drvwrap.driver.name = new_driver->name;
8bb54ab5
AS
947 new_driver->drvwrap.driver.bus = &usb_bus_type;
948 new_driver->drvwrap.driver.probe = usb_probe_interface;
949 new_driver->drvwrap.driver.remove = usb_unbind_interface;
950 new_driver->drvwrap.driver.owner = owner;
80f745fb 951 new_driver->drvwrap.driver.mod_name = mod_name;
733260ff
GKH
952 spin_lock_init(&new_driver->dynids.lock);
953 INIT_LIST_HEAD(&new_driver->dynids.list);
ddae41be 954
8bb54ab5 955 retval = driver_register(&new_driver->drvwrap.driver);
0c7a2b72
CR
956 if (retval)
957 goto out;
ddae41be 958
ed283e9f 959 retval = usb_create_newid_files(new_driver);
0c7a2b72
CR
960 if (retval)
961 goto out_newid;
962
0c7a2b72 963 pr_info("%s: registered new interface driver %s\n",
ddae41be 964 usbcore_name, new_driver->name);
ddae41be 965
0c7a2b72 966out:
ddae41be 967 return retval;
0c7a2b72 968
0c7a2b72
CR
969out_newid:
970 driver_unregister(&new_driver->drvwrap.driver);
971
972 printk(KERN_ERR "%s: error %d registering interface "
973 " driver %s\n",
974 usbcore_name, retval, new_driver->name);
975 goto out;
ddae41be 976}
782e70c6 977EXPORT_SYMBOL_GPL(usb_register_driver);
ddae41be
GKH
978
979/**
8bb54ab5
AS
980 * usb_deregister - unregister a USB interface driver
981 * @driver: USB operations of the interface driver to unregister
ddae41be
GKH
982 * Context: must be able to sleep
983 *
984 * Unlinks the specified driver from the internal USB driver list.
985 *
986 * NOTE: If you called usb_register_dev(), you still need to call
987 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
988 * this * call will no longer do it for you.
989 */
990void usb_deregister(struct usb_driver *driver)
991{
8bb54ab5
AS
992 pr_info("%s: deregistering interface driver %s\n",
993 usbcore_name, driver->name);
ddae41be 994
ed283e9f 995 usb_remove_newid_files(driver);
8bb54ab5 996 driver_unregister(&driver->drvwrap.driver);
ed283e9f 997 usb_free_dynids(driver);
ddae41be 998}
782e70c6 999EXPORT_SYMBOL_GPL(usb_deregister);
36e56a34 1000
78d9a487
AS
1001/* Forced unbinding of a USB interface driver, either because
1002 * it doesn't support pre_reset/post_reset/reset_resume or
1003 * because it doesn't support suspend/resume.
1004 *
6aec044c 1005 * The caller must hold @intf's device's lock, but not @intf's lock.
78d9a487
AS
1006 */
1007void usb_forced_unbind_intf(struct usb_interface *intf)
1008{
1009 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1010
1011 dev_dbg(&intf->dev, "forced unbind\n");
1012 usb_driver_release_interface(driver, intf);
1013
1014 /* Mark the interface for later rebinding */
1015 intf->needs_binding = 1;
1016}
1017
6aec044c
AS
1018/*
1019 * Unbind drivers for @udev's marked interfaces. These interfaces have
1020 * the needs_binding flag set, for example by usb_resume_interface().
1021 *
1022 * The caller must hold @udev's device lock.
1023 */
1024static void unbind_marked_interfaces(struct usb_device *udev)
1025{
1026 struct usb_host_config *config;
1027 int i;
1028 struct usb_interface *intf;
1029
1030 config = udev->actconfig;
1031 if (config) {
1032 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1033 intf = config->interface[i];
1034 if (intf->dev.driver && intf->needs_binding)
1035 usb_forced_unbind_intf(intf);
1036 }
1037 }
1038}
1039
78d9a487
AS
1040/* Delayed forced unbinding of a USB interface driver and scan
1041 * for rebinding.
1042 *
6aec044c 1043 * The caller must hold @intf's device's lock, but not @intf's lock.
78d9a487 1044 *
5096aedc
AS
1045 * Note: Rebinds will be skipped if a system sleep transition is in
1046 * progress and the PM "complete" callback hasn't occurred yet.
78d9a487 1047 */
6aec044c 1048static void usb_rebind_intf(struct usb_interface *intf)
78d9a487
AS
1049{
1050 int rc;
1051
1052 /* Delayed unbind of an existing driver */
1493138a
ON
1053 if (intf->dev.driver)
1054 usb_forced_unbind_intf(intf);
78d9a487
AS
1055
1056 /* Try to rebind the interface */
f76b168b 1057 if (!intf->dev.power.is_prepared) {
5096aedc
AS
1058 intf->needs_binding = 0;
1059 rc = device_attach(&intf->dev);
1060 if (rc < 0)
1061 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1062 }
78d9a487
AS
1063}
1064
6aec044c
AS
1065/*
1066 * Rebind drivers to @udev's marked interfaces. These interfaces have
1067 * the needs_binding flag set.
78d9a487
AS
1068 *
1069 * The caller must hold @udev's device lock.
78d9a487 1070 */
6aec044c 1071static void rebind_marked_interfaces(struct usb_device *udev)
78d9a487
AS
1072{
1073 struct usb_host_config *config;
1074 int i;
1075 struct usb_interface *intf;
78d9a487
AS
1076
1077 config = udev->actconfig;
1078 if (config) {
1079 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1080 intf = config->interface[i];
6aec044c
AS
1081 if (intf->needs_binding)
1082 usb_rebind_intf(intf);
78d9a487
AS
1083 }
1084 }
1085}
1086
6aec044c
AS
1087/*
1088 * Unbind all of @udev's marked interfaces and then rebind all of them.
1089 * This ordering is necessary because some drivers claim several interfaces
1090 * when they are first probed.
1493138a
ON
1091 *
1092 * The caller must hold @udev's device lock.
1093 */
6aec044c 1094void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev)
1493138a 1095{
6aec044c
AS
1096 unbind_marked_interfaces(udev);
1097 rebind_marked_interfaces(udev);
1493138a
ON
1098}
1099
6aec044c
AS
1100#ifdef CONFIG_PM
1101
1102/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1103 * There is no check for reset_resume here because it can be determined
1104 * only during resume whether reset_resume is needed.
1105 *
1106 * The caller must hold @udev's device lock.
1107 */
1108static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
1493138a
ON
1109{
1110 struct usb_host_config *config;
1111 int i;
1112 struct usb_interface *intf;
6aec044c 1113 struct usb_driver *drv;
1493138a
ON
1114
1115 config = udev->actconfig;
1116 if (config) {
1117 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1118 intf = config->interface[i];
6aec044c
AS
1119
1120 if (intf->dev.driver) {
1121 drv = to_usb_driver(intf->dev.driver);
1122 if (!drv->suspend || !drv->resume)
1123 usb_forced_unbind_intf(intf);
1124 }
1493138a
ON
1125 }
1126 }
1127}
1128
d5ec1686 1129static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
36e56a34 1130{
782da727 1131 struct usb_device_driver *udriver;
2bf4086d 1132 int status = 0;
36e56a34 1133
114b368c
AS
1134 if (udev->state == USB_STATE_NOTATTACHED ||
1135 udev->state == USB_STATE_SUSPENDED)
1136 goto done;
1137
b6f6436d
AS
1138 /* For devices that don't have a driver, we do a generic suspend. */
1139 if (udev->dev.driver)
1140 udriver = to_usb_device_driver(udev->dev.driver);
1141 else {
645daaab 1142 udev->do_remote_wakeup = 0;
b6f6436d 1143 udriver = &usb_generic_driver;
1c5df7e7 1144 }
2bf4086d
AS
1145 status = udriver->suspend(udev, msg);
1146
20dfdad7 1147 done:
441b62c1 1148 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
2bf4086d 1149 return status;
1cc8a25d
AS
1150}
1151
65bfd296 1152static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1cc8a25d
AS
1153{
1154 struct usb_device_driver *udriver;
2bf4086d 1155 int status = 0;
36e56a34 1156
0458d5b4
AS
1157 if (udev->state == USB_STATE_NOTATTACHED)
1158 goto done;
1cc8a25d 1159
1c5df7e7
AS
1160 /* Can't resume it if it doesn't have a driver. */
1161 if (udev->dev.driver == NULL) {
1162 status = -ENOTCONN;
2bf4086d 1163 goto done;
1c5df7e7
AS
1164 }
1165
6d19c009
AS
1166 /* Non-root devices on a full/low-speed bus must wait for their
1167 * companion high-speed root hub, in case a handoff is needed.
1168 */
5b1b0b81 1169 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
6d19c009
AS
1170 device_pm_wait_for_dev(&udev->dev,
1171 &udev->bus->hs_companion->root_hub->dev);
1172
6bc6cff5
AS
1173 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1174 udev->reset_resume = 1;
1175
1cc8a25d 1176 udriver = to_usb_device_driver(udev->dev.driver);
65bfd296 1177 status = udriver->resume(udev, msg);
2bf4086d 1178
20dfdad7 1179 done:
441b62c1 1180 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
2bf4086d 1181 return status;
1cc8a25d
AS
1182}
1183
65605ae8
AS
1184static int usb_suspend_interface(struct usb_device *udev,
1185 struct usb_interface *intf, pm_message_t msg)
1cc8a25d
AS
1186{
1187 struct usb_driver *driver;
2bf4086d 1188 int status = 0;
1cc8a25d 1189
9bbdf1e0
AS
1190 if (udev->state == USB_STATE_NOTATTACHED ||
1191 intf->condition == USB_INTERFACE_UNBOUND)
2bf4086d 1192 goto done;
114b368c 1193 driver = to_usb_driver(intf->dev.driver);
36e56a34 1194
e78832cd
ON
1195 /* at this time we know the driver supports suspend */
1196 status = driver->suspend(intf, msg);
1197 if (status && !PMSG_IS_AUTO(msg))
1198 dev_err(&intf->dev, "suspend error %d\n", status);
2bf4086d 1199
20dfdad7 1200 done:
441b62c1 1201 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
36e56a34
AS
1202 return status;
1203}
1204
65605ae8 1205static int usb_resume_interface(struct usb_device *udev,
65bfd296 1206 struct usb_interface *intf, pm_message_t msg, int reset_resume)
36e56a34 1207{
1cc8a25d 1208 struct usb_driver *driver;
2bf4086d 1209 int status = 0;
36e56a34 1210
9bbdf1e0 1211 if (udev->state == USB_STATE_NOTATTACHED)
2bf4086d 1212 goto done;
36e56a34 1213
645daaab
AS
1214 /* Don't let autoresume interfere with unbinding */
1215 if (intf->condition == USB_INTERFACE_UNBINDING)
1216 goto done;
1217
1c5df7e7 1218 /* Can't resume it if it doesn't have a driver. */
55151d7d
AS
1219 if (intf->condition == USB_INTERFACE_UNBOUND) {
1220
1221 /* Carry out a deferred switch to altsetting 0 */
f76b168b 1222 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
55151d7d
AS
1223 usb_set_interface(udev, intf->altsetting[0].
1224 desc.bInterfaceNumber, 0);
1225 intf->needs_altsetting0 = 0;
1226 }
78d9a487 1227 goto done;
55151d7d 1228 }
78d9a487
AS
1229
1230 /* Don't resume if the interface is marked for rebinding */
1231 if (intf->needs_binding)
2bf4086d 1232 goto done;
1cc8a25d 1233 driver = to_usb_driver(intf->dev.driver);
36e56a34 1234
f07600cf
AS
1235 if (reset_resume) {
1236 if (driver->reset_resume) {
1237 status = driver->reset_resume(intf);
1238 if (status)
1239 dev_err(&intf->dev, "%s error %d\n",
1240 "reset_resume", status);
1241 } else {
78d9a487 1242 intf->needs_binding = 1;
0a56b4fa
AS
1243 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1244 driver->name);
f07600cf
AS
1245 }
1246 } else {
e78832cd
ON
1247 status = driver->resume(intf);
1248 if (status)
1249 dev_err(&intf->dev, "resume error %d\n", status);
f07600cf 1250 }
2bf4086d
AS
1251
1252done:
441b62c1 1253 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
f07600cf 1254
78d9a487 1255 /* Later we will unbind the driver and/or reprobe, if necessary */
2bf4086d 1256 return status;
36e56a34
AS
1257}
1258
645daaab
AS
1259/**
1260 * usb_suspend_both - suspend a USB device and its interfaces
1261 * @udev: the usb_device to suspend
1262 * @msg: Power Management message describing this state transition
1263 *
1264 * This is the central routine for suspending USB devices. It calls the
1265 * suspend methods for all the interface drivers in @udev and then calls
303f0847
ML
1266 * the suspend method for @udev itself. When the routine is called in
1267 * autosuspend, if an error occurs at any stage, all the interfaces
1268 * which were suspended are resumed so that they remain in the same
1269 * state as the device, but when called from system sleep, all error
1270 * from suspend methods of interfaces and the non-root-hub device itself
1271 * are simply ignored, so all suspended interfaces are only resumed
1272 * to the device's state when @udev is root-hub and its suspend method
1273 * returns failure.
645daaab 1274 *
9bbdf1e0
AS
1275 * Autosuspend requests originating from a child device or an interface
1276 * driver may be made without the protection of @udev's device lock, but
1277 * all other suspend calls will hold the lock. Usbcore will insure that
1278 * method calls do not arrive during bind, unbind, or reset operations.
1279 * However drivers must be prepared to handle suspend calls arriving at
1280 * unpredictable times.
645daaab
AS
1281 *
1282 * This routine can run only in process context.
626f090c
YB
1283 *
1284 * Return: 0 if the suspend succeeded.
645daaab 1285 */
718efa64 1286static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
a8e7c565
AS
1287{
1288 int status = 0;
571dc79d 1289 int i = 0, n = 0;
a8e7c565 1290 struct usb_interface *intf;
645daaab 1291
1941044a
AS
1292 if (udev->state == USB_STATE_NOTATTACHED ||
1293 udev->state == USB_STATE_SUSPENDED)
1294 goto done;
a8e7c565 1295
645daaab 1296 /* Suspend all the interfaces and then udev itself */
a8e7c565 1297 if (udev->actconfig) {
571dc79d
AS
1298 n = udev->actconfig->desc.bNumInterfaces;
1299 for (i = n - 1; i >= 0; --i) {
a8e7c565 1300 intf = udev->actconfig->interface[i];
65605ae8 1301 status = usb_suspend_interface(udev, intf, msg);
0af212ba
AS
1302
1303 /* Ignore errors during system sleep transitions */
5b1b0b81 1304 if (!PMSG_IS_AUTO(msg))
0af212ba 1305 status = 0;
a8e7c565
AS
1306 if (status != 0)
1307 break;
1308 }
1309 }
0af212ba 1310 if (status == 0) {
d5ec1686 1311 status = usb_suspend_device(udev, msg);
a8e7c565 1312
cd4376e2
AS
1313 /*
1314 * Ignore errors from non-root-hub devices during
1315 * system sleep transitions. For the most part,
1316 * these devices should go to low power anyway when
1317 * the entire bus is suspended.
1318 */
1319 if (udev->parent && !PMSG_IS_AUTO(msg))
0af212ba
AS
1320 status = 0;
1321 }
1322
a8e7c565
AS
1323 /* If the suspend failed, resume interfaces that did get suspended */
1324 if (status != 0) {
505bdbc7
CG
1325 if (udev->actconfig) {
1326 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1327 while (++i < n) {
1328 intf = udev->actconfig->interface[i];
1329 usb_resume_interface(udev, intf, msg, 0);
1330 }
a8e7c565 1331 }
645daaab 1332
9bbdf1e0
AS
1333 /* If the suspend succeeded then prevent any more URB submissions
1334 * and flush any outstanding URBs.
6840d255 1335 */
ef7f6c70 1336 } else {
6840d255
AS
1337 udev->can_submit = 0;
1338 for (i = 0; i < 16; ++i) {
1339 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1340 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1341 }
ef7f6c70 1342 }
645daaab 1343
1941044a 1344 done:
441b62c1 1345 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
a8e7c565
AS
1346 return status;
1347}
1348
645daaab
AS
1349/**
1350 * usb_resume_both - resume a USB device and its interfaces
1351 * @udev: the usb_device to resume
65bfd296 1352 * @msg: Power Management message describing this state transition
645daaab
AS
1353 *
1354 * This is the central routine for resuming USB devices. It calls the
1355 * the resume method for @udev and then calls the resume methods for all
1356 * the interface drivers in @udev.
1357 *
9bbdf1e0
AS
1358 * Autoresume requests originating from a child device or an interface
1359 * driver may be made without the protection of @udev's device lock, but
1360 * all other resume calls will hold the lock. Usbcore will insure that
1361 * method calls do not arrive during bind, unbind, or reset operations.
1362 * However drivers must be prepared to handle resume calls arriving at
1363 * unpredictable times.
645daaab
AS
1364 *
1365 * This routine can run only in process context.
626f090c
YB
1366 *
1367 * Return: 0 on success.
645daaab 1368 */
65bfd296 1369static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
a8e7c565 1370{
645daaab 1371 int status = 0;
a8e7c565
AS
1372 int i;
1373 struct usb_interface *intf;
645daaab 1374
1941044a
AS
1375 if (udev->state == USB_STATE_NOTATTACHED) {
1376 status = -ENODEV;
1377 goto done;
1378 }
6840d255 1379 udev->can_submit = 1;
a8e7c565 1380
9bbdf1e0
AS
1381 /* Resume the device */
1382 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
65bfd296 1383 status = usb_resume_device(udev, msg);
114b368c 1384
9bbdf1e0 1385 /* Resume the interfaces */
a8e7c565
AS
1386 if (status == 0 && udev->actconfig) {
1387 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1388 intf = udev->actconfig->interface[i];
65bfd296
AS
1389 usb_resume_interface(udev, intf, msg,
1390 udev->reset_resume);
a8e7c565
AS
1391 }
1392 }
c08512c7 1393 usb_mark_last_busy(udev);
645daaab 1394
1941044a 1395 done:
441b62c1 1396 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
70a1c9e0
AS
1397 if (!status)
1398 udev->reset_resume = 0;
645daaab
AS
1399 return status;
1400}
1401
5f677f1d
AS
1402static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1403{
48826626 1404 int w;
5f677f1d
AS
1405
1406 /* Remote wakeup is needed only when we actually go to sleep.
1407 * For things like FREEZE and QUIESCE, if the device is already
1408 * autosuspended then its current wakeup setting is okay.
1409 */
1410 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1411 if (udev->state != USB_STATE_SUSPENDED)
1412 udev->do_remote_wakeup = 0;
1413 return;
1414 }
1415
48826626 1416 /* Enable remote wakeup if it is allowed, even if no interface drivers
5f677f1d
AS
1417 * actually want it.
1418 */
48826626 1419 w = device_may_wakeup(&udev->dev);
5f677f1d
AS
1420
1421 /* If the device is autosuspended with the wrong wakeup setting,
1422 * autoresume now so the setting can be changed.
1423 */
1424 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1425 pm_runtime_resume(&udev->dev);
1426 udev->do_remote_wakeup = w;
1427}
1428
9bbdf1e0 1429/* The device lock is held by the PM core */
0c590e23
AS
1430int usb_suspend(struct device *dev, pm_message_t msg)
1431{
9bbdf1e0 1432 struct usb_device *udev = to_usb_device(dev);
0c590e23 1433
1493138a
ON
1434 unbind_no_pm_drivers_interfaces(udev);
1435
1436 /* From now on we are sure all drivers support suspend/resume
1437 * but not necessarily reset_resume()
1438 * so we may still need to unbind and rebind upon resume
1439 */
5f677f1d 1440 choose_wakeup(udev, msg);
9bbdf1e0 1441 return usb_suspend_both(udev, msg);
0c590e23
AS
1442}
1443
9bbdf1e0 1444/* The device lock is held by the PM core */
98d9a82e 1445int usb_resume_complete(struct device *dev)
0c590e23 1446{
98d9a82e 1447 struct usb_device *udev = to_usb_device(dev);
0c590e23 1448
1493138a
ON
1449 /* For PM complete calls, all we do is rebind interfaces
1450 * whose needs_binding flag is set
1451 */
98d9a82e 1452 if (udev->state != USB_STATE_NOTATTACHED)
6aec044c 1453 rebind_marked_interfaces(udev);
98d9a82e
ON
1454 return 0;
1455}
0c590e23 1456
9bbdf1e0 1457/* The device lock is held by the PM core */
0c590e23
AS
1458int usb_resume(struct device *dev, pm_message_t msg)
1459{
9bbdf1e0 1460 struct usb_device *udev = to_usb_device(dev);
0c590e23
AS
1461 int status;
1462
98d9a82e 1463 /* For all calls, take the device back to full power and
9bbdf1e0 1464 * tell the PM core in case it was autosuspended previously.
1493138a
ON
1465 * Unbind the interfaces that will need rebinding later,
1466 * because they fail to support reset_resume.
1467 * (This can't be done in usb_resume_interface()
98d9a82e 1468 * above because it doesn't own the right set of locks.)
0c590e23 1469 */
98d9a82e
ON
1470 status = usb_resume_both(udev, msg);
1471 if (status == 0) {
1472 pm_runtime_disable(dev);
1473 pm_runtime_set_active(dev);
1474 pm_runtime_enable(dev);
6aec044c 1475 unbind_marked_interfaces(udev);
9bbdf1e0 1476 }
0c590e23
AS
1477
1478 /* Avoid PM error messages for devices disconnected while suspended
1479 * as we'll display regular disconnect messages just a bit later.
1480 */
7491f133 1481 if (status == -ENODEV || status == -ESHUTDOWN)
9bbdf1e0 1482 status = 0;
0c590e23
AS
1483 return status;
1484}
1485
088f7fec
AS
1486/**
1487 * usb_enable_autosuspend - allow a USB device to be autosuspended
1488 * @udev: the USB device which may be autosuspended
1489 *
1490 * This routine allows @udev to be autosuspended. An autosuspend won't
1491 * take place until the autosuspend_delay has elapsed and all the other
1492 * necessary conditions are satisfied.
1493 *
1494 * The caller must hold @udev's device lock.
1495 */
9e18c821 1496void usb_enable_autosuspend(struct usb_device *udev)
088f7fec 1497{
9e18c821 1498 pm_runtime_allow(&udev->dev);
088f7fec
AS
1499}
1500EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1501
1502/**
1503 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1504 * @udev: the USB device which may not be autosuspended
1505 *
1506 * This routine prevents @udev from being autosuspended and wakes it up
1507 * if it is already autosuspended.
1508 *
1509 * The caller must hold @udev's device lock.
1510 */
9e18c821 1511void usb_disable_autosuspend(struct usb_device *udev)
088f7fec 1512{
9e18c821 1513 pm_runtime_forbid(&udev->dev);
088f7fec
AS
1514}
1515EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1516
645daaab
AS
1517/**
1518 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
701f35af 1519 * @udev: the usb_device to autosuspend
645daaab
AS
1520 *
1521 * This routine should be called when a core subsystem is finished using
1522 * @udev and wants to allow it to autosuspend. Examples would be when
1523 * @udev's device file in usbfs is closed or after a configuration change.
1524 *
9bbdf1e0
AS
1525 * @udev's usage counter is decremented; if it drops to 0 and all the
1526 * interfaces are inactive then a delayed autosuspend will be attempted.
1527 * The attempt may fail (see autosuspend_check()).
645daaab 1528 *
62e299e6 1529 * The caller must hold @udev's device lock.
645daaab
AS
1530 *
1531 * This routine can run only in process context.
1532 */
94fcda1f 1533void usb_autosuspend_device(struct usb_device *udev)
645daaab 1534{
94fcda1f
AS
1535 int status;
1536
6ddf27cd 1537 usb_mark_last_busy(udev);
fcc4a01e 1538 status = pm_runtime_put_sync_autosuspend(&udev->dev);
9bbdf1e0
AS
1539 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1540 __func__, atomic_read(&udev->dev.power.usage_count),
1541 status);
645daaab
AS
1542}
1543
1544/**
1545 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
701f35af 1546 * @udev: the usb_device to autoresume
645daaab
AS
1547 *
1548 * This routine should be called when a core subsystem wants to use @udev
94fcda1f 1549 * and needs to guarantee that it is not suspended. No autosuspend will
9bbdf1e0
AS
1550 * occur until usb_autosuspend_device() is called. (Note that this will
1551 * not prevent suspend events originating in the PM core.) Examples would
1552 * be when @udev's device file in usbfs is opened or when a remote-wakeup
94fcda1f 1553 * request is received.
645daaab 1554 *
94fcda1f
AS
1555 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1556 * However if the autoresume fails then the usage counter is re-decremented.
645daaab 1557 *
62e299e6 1558 * The caller must hold @udev's device lock.
645daaab
AS
1559 *
1560 * This routine can run only in process context.
626f090c
YB
1561 *
1562 * Return: 0 on success. A negative error code otherwise.
645daaab 1563 */
94fcda1f 1564int usb_autoresume_device(struct usb_device *udev)
645daaab
AS
1565{
1566 int status;
1567
9bbdf1e0
AS
1568 status = pm_runtime_get_sync(&udev->dev);
1569 if (status < 0)
1570 pm_runtime_put_sync(&udev->dev);
1571 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1572 __func__, atomic_read(&udev->dev.power.usage_count),
1573 status);
1574 if (status > 0)
1575 status = 0;
af4f7606
AS
1576 return status;
1577}
1578
645daaab
AS
1579/**
1580 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
701f35af 1581 * @intf: the usb_interface whose counter should be decremented
645daaab
AS
1582 *
1583 * This routine should be called by an interface driver when it is
1584 * finished using @intf and wants to allow it to autosuspend. A typical
1585 * example would be a character-device driver when its device file is
1586 * closed.
1587 *
1588 * The routine decrements @intf's usage counter. When the counter reaches
9bbdf1e0
AS
1589 * 0, a delayed autosuspend request for @intf's device is attempted. The
1590 * attempt may fail (see autosuspend_check()).
645daaab 1591 *
645daaab
AS
1592 * This routine can run only in process context.
1593 */
1594void usb_autopm_put_interface(struct usb_interface *intf)
1595{
9bbdf1e0
AS
1596 struct usb_device *udev = interface_to_usbdev(intf);
1597 int status;
645daaab 1598
6ddf27cd 1599 usb_mark_last_busy(udev);
9bbdf1e0
AS
1600 atomic_dec(&intf->pm_usage_cnt);
1601 status = pm_runtime_put_sync(&intf->dev);
1602 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1603 __func__, atomic_read(&intf->dev.power.usage_count),
1604 status);
645daaab
AS
1605}
1606EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1607
9ac39f28
AS
1608/**
1609 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1610 * @intf: the usb_interface whose counter should be decremented
1611 *
9bbdf1e0
AS
1612 * This routine does much the same thing as usb_autopm_put_interface():
1613 * It decrements @intf's usage counter and schedules a delayed
1614 * autosuspend request if the counter is <= 0. The difference is that it
1615 * does not perform any synchronization; callers should hold a private
1616 * lock and handle all synchronization issues themselves.
9ac39f28
AS
1617 *
1618 * Typically a driver would call this routine during an URB's completion
1619 * handler, if no more URBs were pending.
1620 *
1621 * This routine can run in atomic context.
1622 */
1623void usb_autopm_put_interface_async(struct usb_interface *intf)
1624{
1625 struct usb_device *udev = interface_to_usbdev(intf);
fcc4a01e 1626 int status;
9ac39f28 1627
6ddf27cd 1628 usb_mark_last_busy(udev);
9bbdf1e0 1629 atomic_dec(&intf->pm_usage_cnt);
fcc4a01e 1630 status = pm_runtime_put(&intf->dev);
9bbdf1e0
AS
1631 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1632 __func__, atomic_read(&intf->dev.power.usage_count),
1633 status);
9ac39f28
AS
1634}
1635EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1636
9bbdf1e0
AS
1637/**
1638 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1639 * @intf: the usb_interface whose counter should be decremented
1640 *
1641 * This routine decrements @intf's usage counter but does not carry out an
1642 * autosuspend.
1643 *
1644 * This routine can run in atomic context.
1645 */
1646void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1647{
1648 struct usb_device *udev = interface_to_usbdev(intf);
1649
6ddf27cd 1650 usb_mark_last_busy(udev);
9bbdf1e0
AS
1651 atomic_dec(&intf->pm_usage_cnt);
1652 pm_runtime_put_noidle(&intf->dev);
1653}
1654EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1655
645daaab
AS
1656/**
1657 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
701f35af 1658 * @intf: the usb_interface whose counter should be incremented
645daaab
AS
1659 *
1660 * This routine should be called by an interface driver when it wants to
1661 * use @intf and needs to guarantee that it is not suspended. In addition,
1662 * the routine prevents @intf from being autosuspended subsequently. (Note
1663 * that this will not prevent suspend events originating in the PM core.)
1664 * This prevention will persist until usb_autopm_put_interface() is called
1665 * or @intf is unbound. A typical example would be a character-device
1666 * driver when its device file is opened.
1667 *
9bbdf1e0
AS
1668 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1669 * However if the autoresume fails then the counter is re-decremented.
645daaab
AS
1670 *
1671 * This routine can run only in process context.
626f090c
YB
1672 *
1673 * Return: 0 on success.
645daaab
AS
1674 */
1675int usb_autopm_get_interface(struct usb_interface *intf)
1676{
af4f7606 1677 int status;
645daaab 1678
9bbdf1e0
AS
1679 status = pm_runtime_get_sync(&intf->dev);
1680 if (status < 0)
1681 pm_runtime_put_sync(&intf->dev);
1682 else
1683 atomic_inc(&intf->pm_usage_cnt);
1684 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1685 __func__, atomic_read(&intf->dev.power.usage_count),
1686 status);
1687 if (status > 0)
1688 status = 0;
a8e7c565
AS
1689 return status;
1690}
645daaab
AS
1691EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1692
9ac39f28
AS
1693/**
1694 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1695 * @intf: the usb_interface whose counter should be incremented
1696 *
1697 * This routine does much the same thing as
9bbdf1e0
AS
1698 * usb_autopm_get_interface(): It increments @intf's usage counter and
1699 * queues an autoresume request if the device is suspended. The
1700 * differences are that it does not perform any synchronization (callers
1701 * should hold a private lock and handle all synchronization issues
1702 * themselves), and it does not autoresume the device directly (it only
1703 * queues a request). After a successful call, the device may not yet be
1704 * resumed.
9ac39f28
AS
1705 *
1706 * This routine can run in atomic context.
626f090c
YB
1707 *
1708 * Return: 0 on success. A negative error code otherwise.
9ac39f28
AS
1709 */
1710int usb_autopm_get_interface_async(struct usb_interface *intf)
1711{
63defa73 1712 int status;
9bbdf1e0 1713
63defa73 1714 status = pm_runtime_get(&intf->dev);
9bbdf1e0
AS
1715 if (status < 0 && status != -EINPROGRESS)
1716 pm_runtime_put_noidle(&intf->dev);
1717 else
ccf5b801 1718 atomic_inc(&intf->pm_usage_cnt);
9bbdf1e0
AS
1719 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1720 __func__, atomic_read(&intf->dev.power.usage_count),
1721 status);
c5a48592 1722 if (status > 0 || status == -EINPROGRESS)
9bbdf1e0 1723 status = 0;
9ac39f28
AS
1724 return status;
1725}
1726EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1727
9bbdf1e0
AS
1728/**
1729 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1730 * @intf: the usb_interface whose counter should be incremented
1731 *
1732 * This routine increments @intf's usage counter but does not carry out an
1733 * autoresume.
1734 *
1735 * This routine can run in atomic context.
1736 */
1737void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1738{
1739 struct usb_device *udev = interface_to_usbdev(intf);
1740
6ddf27cd 1741 usb_mark_last_busy(udev);
9bbdf1e0
AS
1742 atomic_inc(&intf->pm_usage_cnt);
1743 pm_runtime_get_noresume(&intf->dev);
1744}
1745EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1746
1747/* Internal routine to check whether we may autosuspend a device. */
1748static int autosuspend_check(struct usb_device *udev)
1749{
7560d32e 1750 int w, i;
9bbdf1e0 1751 struct usb_interface *intf;
9bbdf1e0
AS
1752
1753 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1754 * any interface drivers require remote wakeup but it isn't available.
1755 */
7560d32e 1756 w = 0;
9bbdf1e0
AS
1757 if (udev->actconfig) {
1758 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1759 intf = udev->actconfig->interface[i];
1760
1761 /* We don't need to check interfaces that are
1762 * disabled for runtime PM. Either they are unbound
1763 * or else their drivers don't support autosuspend
1764 * and so they are permanently active.
1765 */
1766 if (intf->dev.power.disable_depth)
1767 continue;
1768 if (atomic_read(&intf->dev.power.usage_count) > 0)
1769 return -EBUSY;
7560d32e 1770 w |= intf->needs_remote_wakeup;
9bbdf1e0
AS
1771
1772 /* Don't allow autosuspend if the device will need
1773 * a reset-resume and any of its interface drivers
1774 * doesn't include support or needs remote wakeup.
1775 */
1776 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1777 struct usb_driver *driver;
1778
1779 driver = to_usb_driver(intf->dev.driver);
1780 if (!driver->reset_resume ||
1781 intf->needs_remote_wakeup)
1782 return -EOPNOTSUPP;
1783 }
1784 }
1785 }
7560d32e
AS
1786 if (w && !device_can_wakeup(&udev->dev)) {
1787 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1788 return -EOPNOTSUPP;
1789 }
074f9dd5
AS
1790
1791 /*
1792 * If the device is a direct child of the root hub and the HCD
1793 * doesn't handle wakeup requests, don't allow autosuspend when
1794 * wakeup is needed.
1795 */
1796 if (w && udev->parent == udev->bus->root_hub &&
1797 bus_to_hcd(udev->bus)->cant_recv_wakeups) {
1798 dev_dbg(&udev->dev, "HCD doesn't handle wakeup requests\n");
1799 return -EOPNOTSUPP;
1800 }
1801
7560d32e 1802 udev->do_remote_wakeup = w;
9bbdf1e0
AS
1803 return 0;
1804}
1805
e1620d59 1806int usb_runtime_suspend(struct device *dev)
9bbdf1e0 1807{
63defa73
ML
1808 struct usb_device *udev = to_usb_device(dev);
1809 int status;
718efa64 1810
9bbdf1e0
AS
1811 /* A USB device can be suspended if it passes the various autosuspend
1812 * checks. Runtime suspend for a USB device means suspending all the
1813 * interfaces and then the device itself.
1814 */
63defa73
ML
1815 if (autosuspend_check(udev) != 0)
1816 return -EAGAIN;
9bbdf1e0 1817
63defa73 1818 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
b2c0a863
AS
1819
1820 /* Allow a retry if autosuspend failed temporarily */
1821 if (status == -EAGAIN || status == -EBUSY)
1822 usb_mark_last_busy(udev);
1823
8ef42ddd
AS
1824 /*
1825 * The PM core reacts badly unless the return code is 0,
1826 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1827 * (except for root hubs, because they don't suspend through
1828 * an upstream port like other USB devices).
db7c7c0a 1829 */
8ef42ddd 1830 if (status != 0 && udev->parent)
db7c7c0a 1831 return -EBUSY;
9bbdf1e0
AS
1832 return status;
1833}
1834
e1620d59 1835int usb_runtime_resume(struct device *dev)
9bbdf1e0 1836{
63defa73
ML
1837 struct usb_device *udev = to_usb_device(dev);
1838 int status;
1839
9bbdf1e0
AS
1840 /* Runtime resume for a USB device means resuming both the device
1841 * and all its interfaces.
1842 */
63defa73 1843 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
63defa73 1844 return status;
9bbdf1e0
AS
1845}
1846
e1620d59 1847int usb_runtime_idle(struct device *dev)
9bbdf1e0 1848{
63defa73
ML
1849 struct usb_device *udev = to_usb_device(dev);
1850
9bbdf1e0 1851 /* An idle USB device can be suspended if it passes the various
63defa73 1852 * autosuspend checks.
9bbdf1e0 1853 */
63defa73 1854 if (autosuspend_check(udev) == 0)
fcc4a01e 1855 pm_runtime_autosuspend(dev);
45f0a85c
RW
1856 /* Tell the core not to suspend it, though. */
1857 return -EBUSY;
9bbdf1e0
AS
1858}
1859
65580b43
AX
1860int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1861{
1862 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1863 int ret = -EPERM;
1864
de68bab4
SS
1865 if (enable && !udev->usb2_hw_lpm_allowed)
1866 return 0;
1867
65580b43
AX
1868 if (hcd->driver->set_usb2_hw_lpm) {
1869 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1870 if (!ret)
1871 udev->usb2_hw_lpm_enabled = enable;
1872 }
1873
1874 return ret;
1875}
1876
ceb6c9c8 1877#endif /* CONFIG_PM */
a8e7c565 1878
36e56a34
AS
1879struct bus_type usb_bus_type = {
1880 .name = "usb",
1881 .match = usb_device_match,
1882 .uevent = usb_uevent,
36e56a34 1883};