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