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