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