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