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