xref: /OK3568_Linux_fs/kernel/drivers/usb/core/driver.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * drivers/usb/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  * @priv: 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  * Few drivers should need to use this routine, since the most natural
531  * way to bind to an interface is to return the private data from
532  * the driver's probe() method.
533  *
534  * Callers must own the device lock, so driver probe() entries don't need
535  * extra locking, but other call contexts may need to explicitly claim that
536  * lock.
537  *
538  * Return: 0 on success.
539  */
usb_driver_claim_interface(struct usb_driver * driver,struct usb_interface * iface,void * priv)540 int usb_driver_claim_interface(struct usb_driver *driver,
541 				struct usb_interface *iface, void *priv)
542 {
543 	struct device *dev;
544 	int retval = 0;
545 
546 	if (!iface)
547 		return -ENODEV;
548 
549 	dev = &iface->dev;
550 	if (dev->driver)
551 		return -EBUSY;
552 
553 	/* reject claim if interface is not authorized */
554 	if (!iface->authorized)
555 		return -ENODEV;
556 
557 	dev->driver = &driver->drvwrap.driver;
558 	usb_set_intfdata(iface, priv);
559 	iface->needs_binding = 0;
560 
561 	iface->condition = USB_INTERFACE_BOUND;
562 
563 	/* Claimed interfaces are initially inactive (suspended) and
564 	 * runtime-PM-enabled, but only if the driver has autosuspend
565 	 * support.  Otherwise they are marked active, to prevent the
566 	 * device from being autosuspended, but left disabled.  In either
567 	 * case they are sensitive to their children's power states.
568 	 */
569 	pm_suspend_ignore_children(dev, false);
570 	if (driver->supports_autosuspend)
571 		pm_runtime_enable(dev);
572 	else
573 		pm_runtime_set_active(dev);
574 
575 	/* if interface was already added, bind now; else let
576 	 * the future device_add() bind it, bypassing probe()
577 	 */
578 	if (device_is_registered(dev))
579 		retval = device_bind_driver(dev);
580 
581 	if (retval) {
582 		dev->driver = NULL;
583 		usb_set_intfdata(iface, NULL);
584 		iface->needs_remote_wakeup = 0;
585 		iface->condition = USB_INTERFACE_UNBOUND;
586 
587 		/*
588 		 * Unbound interfaces are always runtime-PM-disabled
589 		 * and runtime-PM-suspended
590 		 */
591 		if (driver->supports_autosuspend)
592 			pm_runtime_disable(dev);
593 		pm_runtime_set_suspended(dev);
594 	}
595 
596 	return retval;
597 }
598 EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
599 
600 /**
601  * usb_driver_release_interface - unbind a driver from an interface
602  * @driver: the driver to be unbound
603  * @iface: the interface from which it will be unbound
604  *
605  * This can be used by drivers to release an interface without waiting
606  * for their disconnect() methods to be called.  In typical cases this
607  * also causes the driver disconnect() method to be called.
608  *
609  * This call is synchronous, and may not be used in an interrupt context.
610  * Callers must own the device lock, so driver disconnect() entries don't
611  * need extra locking, but other call contexts may need to explicitly claim
612  * that lock.
613  */
usb_driver_release_interface(struct usb_driver * driver,struct usb_interface * iface)614 void usb_driver_release_interface(struct usb_driver *driver,
615 					struct usb_interface *iface)
616 {
617 	struct device *dev = &iface->dev;
618 
619 	/* this should never happen, don't release something that's not ours */
620 	if (!dev->driver || dev->driver != &driver->drvwrap.driver)
621 		return;
622 
623 	/* don't release from within disconnect() */
624 	if (iface->condition != USB_INTERFACE_BOUND)
625 		return;
626 	iface->condition = USB_INTERFACE_UNBINDING;
627 
628 	/* Release via the driver core only if the interface
629 	 * has already been registered
630 	 */
631 	if (device_is_registered(dev)) {
632 		device_release_driver(dev);
633 	} else {
634 		device_lock(dev);
635 		usb_unbind_interface(dev);
636 		dev->driver = NULL;
637 		device_unlock(dev);
638 	}
639 }
640 EXPORT_SYMBOL_GPL(usb_driver_release_interface);
641 
642 /* returns 0 if no match, 1 if match */
usb_match_device(struct usb_device * dev,const struct usb_device_id * id)643 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
644 {
645 	if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
646 	    id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
647 		return 0;
648 
649 	if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
650 	    id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
651 		return 0;
652 
653 	/* No need to test id->bcdDevice_lo != 0, since 0 is never
654 	   greater than any unsigned number. */
655 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
656 	    (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
657 		return 0;
658 
659 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
660 	    (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
661 		return 0;
662 
663 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
664 	    (id->bDeviceClass != dev->descriptor.bDeviceClass))
665 		return 0;
666 
667 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
668 	    (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
669 		return 0;
670 
671 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
672 	    (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
673 		return 0;
674 
675 	return 1;
676 }
677 
678 /* 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)679 int usb_match_one_id_intf(struct usb_device *dev,
680 			  struct usb_host_interface *intf,
681 			  const struct usb_device_id *id)
682 {
683 	/* The interface class, subclass, protocol and number should never be
684 	 * checked for a match if the device class is Vendor Specific,
685 	 * unless the match record specifies the Vendor ID. */
686 	if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
687 			!(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
688 			(id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
689 				USB_DEVICE_ID_MATCH_INT_SUBCLASS |
690 				USB_DEVICE_ID_MATCH_INT_PROTOCOL |
691 				USB_DEVICE_ID_MATCH_INT_NUMBER)))
692 		return 0;
693 
694 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
695 	    (id->bInterfaceClass != intf->desc.bInterfaceClass))
696 		return 0;
697 
698 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
699 	    (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
700 		return 0;
701 
702 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
703 	    (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
704 		return 0;
705 
706 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
707 	    (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
708 		return 0;
709 
710 	return 1;
711 }
712 
713 /* returns 0 if no match, 1 if match */
usb_match_one_id(struct usb_interface * interface,const struct usb_device_id * id)714 int usb_match_one_id(struct usb_interface *interface,
715 		     const struct usb_device_id *id)
716 {
717 	struct usb_host_interface *intf;
718 	struct usb_device *dev;
719 
720 	/* proc_connectinfo in devio.c may call us with id == NULL. */
721 	if (id == NULL)
722 		return 0;
723 
724 	intf = interface->cur_altsetting;
725 	dev = interface_to_usbdev(interface);
726 
727 	if (!usb_match_device(dev, id))
728 		return 0;
729 
730 	return usb_match_one_id_intf(dev, intf, id);
731 }
732 EXPORT_SYMBOL_GPL(usb_match_one_id);
733 
734 /**
735  * usb_match_id - find first usb_device_id matching device or interface
736  * @interface: the interface of interest
737  * @id: array of usb_device_id structures, terminated by zero entry
738  *
739  * usb_match_id searches an array of usb_device_id's and returns
740  * the first one matching the device or interface, or null.
741  * This is used when binding (or rebinding) a driver to an interface.
742  * Most USB device drivers will use this indirectly, through the usb core,
743  * but some layered driver frameworks use it directly.
744  * These device tables are exported with MODULE_DEVICE_TABLE, through
745  * modutils, to support the driver loading functionality of USB hotplugging.
746  *
747  * Return: The first matching usb_device_id, or %NULL.
748  *
749  * What Matches:
750  *
751  * The "match_flags" element in a usb_device_id controls which
752  * members are used.  If the corresponding bit is set, the
753  * value in the device_id must match its corresponding member
754  * in the device or interface descriptor, or else the device_id
755  * does not match.
756  *
757  * "driver_info" is normally used only by device drivers,
758  * but you can create a wildcard "matches anything" usb_device_id
759  * as a driver's "modules.usbmap" entry if you provide an id with
760  * only a nonzero "driver_info" field.  If you do this, the USB device
761  * driver's probe() routine should use additional intelligence to
762  * decide whether to bind to the specified interface.
763  *
764  * What Makes Good usb_device_id Tables:
765  *
766  * The match algorithm is very simple, so that intelligence in
767  * driver selection must come from smart driver id records.
768  * Unless you have good reasons to use another selection policy,
769  * provide match elements only in related groups, and order match
770  * specifiers from specific to general.  Use the macros provided
771  * for that purpose if you can.
772  *
773  * The most specific match specifiers use device descriptor
774  * data.  These are commonly used with product-specific matches;
775  * the USB_DEVICE macro lets you provide vendor and product IDs,
776  * and you can also match against ranges of product revisions.
777  * These are widely used for devices with application or vendor
778  * specific bDeviceClass values.
779  *
780  * Matches based on device class/subclass/protocol specifications
781  * are slightly more general; use the USB_DEVICE_INFO macro, or
782  * its siblings.  These are used with single-function devices
783  * where bDeviceClass doesn't specify that each interface has
784  * its own class.
785  *
786  * Matches based on interface class/subclass/protocol are the
787  * most general; they let drivers bind to any interface on a
788  * multiple-function device.  Use the USB_INTERFACE_INFO
789  * macro, or its siblings, to match class-per-interface style
790  * devices (as recorded in bInterfaceClass).
791  *
792  * Note that an entry created by USB_INTERFACE_INFO won't match
793  * any interface if the device class is set to Vendor-Specific.
794  * This is deliberate; according to the USB spec the meanings of
795  * the interface class/subclass/protocol for these devices are also
796  * vendor-specific, and hence matching against a standard product
797  * class wouldn't work anyway.  If you really want to use an
798  * interface-based match for such a device, create a match record
799  * that also specifies the vendor ID.  (Unforunately there isn't a
800  * standard macro for creating records like this.)
801  *
802  * Within those groups, remember that not all combinations are
803  * meaningful.  For example, don't give a product version range
804  * without vendor and product IDs; or specify a protocol without
805  * its associated class and subclass.
806  */
usb_match_id(struct usb_interface * interface,const struct usb_device_id * id)807 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
808 					 const struct usb_device_id *id)
809 {
810 	/* proc_connectinfo in devio.c may call us with id == NULL. */
811 	if (id == NULL)
812 		return NULL;
813 
814 	/* It is important to check that id->driver_info is nonzero,
815 	   since an entry that is all zeroes except for a nonzero
816 	   id->driver_info is the way to create an entry that
817 	   indicates that the driver want to examine every
818 	   device and interface. */
819 	for (; id->idVendor || id->idProduct || id->bDeviceClass ||
820 	       id->bInterfaceClass || id->driver_info; id++) {
821 		if (usb_match_one_id(interface, id))
822 			return id;
823 	}
824 
825 	return NULL;
826 }
827 EXPORT_SYMBOL_GPL(usb_match_id);
828 
usb_device_match_id(struct usb_device * udev,const struct usb_device_id * id)829 const struct usb_device_id *usb_device_match_id(struct usb_device *udev,
830 				const struct usb_device_id *id)
831 {
832 	if (!id)
833 		return NULL;
834 
835 	for (; id->idVendor || id->idProduct ; id++) {
836 		if (usb_match_device(udev, id))
837 			return id;
838 	}
839 
840 	return NULL;
841 }
842 
usb_driver_applicable(struct usb_device * udev,struct usb_device_driver * udrv)843 bool usb_driver_applicable(struct usb_device *udev,
844 			   struct usb_device_driver *udrv)
845 {
846 	if (udrv->id_table && udrv->match)
847 		return usb_device_match_id(udev, udrv->id_table) != NULL &&
848 		       udrv->match(udev);
849 
850 	if (udrv->id_table)
851 		return usb_device_match_id(udev, udrv->id_table) != NULL;
852 
853 	if (udrv->match)
854 		return udrv->match(udev);
855 
856 	return false;
857 }
858 
usb_device_match(struct device * dev,struct device_driver * drv)859 static int usb_device_match(struct device *dev, struct device_driver *drv)
860 {
861 	/* devices and interfaces are handled separately */
862 	if (is_usb_device(dev)) {
863 		struct usb_device *udev;
864 		struct usb_device_driver *udrv;
865 
866 		/* interface drivers never match devices */
867 		if (!is_usb_device_driver(drv))
868 			return 0;
869 
870 		udev = to_usb_device(dev);
871 		udrv = to_usb_device_driver(drv);
872 
873 		/* If the device driver under consideration does not have a
874 		 * id_table or a match function, then let the driver's probe
875 		 * function decide.
876 		 */
877 		if (!udrv->id_table && !udrv->match)
878 			return 1;
879 
880 		return usb_driver_applicable(udev, udrv);
881 
882 	} else if (is_usb_interface(dev)) {
883 		struct usb_interface *intf;
884 		struct usb_driver *usb_drv;
885 		const struct usb_device_id *id;
886 
887 		/* device drivers never match interfaces */
888 		if (is_usb_device_driver(drv))
889 			return 0;
890 
891 		intf = to_usb_interface(dev);
892 		usb_drv = to_usb_driver(drv);
893 
894 		id = usb_match_id(intf, usb_drv->id_table);
895 		if (id)
896 			return 1;
897 
898 		id = usb_match_dynamic_id(intf, usb_drv);
899 		if (id)
900 			return 1;
901 	}
902 
903 	return 0;
904 }
905 
usb_uevent(struct device * dev,struct kobj_uevent_env * env)906 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
907 {
908 	struct usb_device *usb_dev;
909 
910 	if (is_usb_device(dev)) {
911 		usb_dev = to_usb_device(dev);
912 	} else if (is_usb_interface(dev)) {
913 		struct usb_interface *intf = to_usb_interface(dev);
914 
915 		usb_dev = interface_to_usbdev(intf);
916 	} else {
917 		return 0;
918 	}
919 
920 	if (usb_dev->devnum < 0) {
921 		/* driver is often null here; dev_dbg() would oops */
922 		pr_debug("usb %s: already deleted?\n", dev_name(dev));
923 		return -ENODEV;
924 	}
925 	if (!usb_dev->bus) {
926 		pr_debug("usb %s: bus removed?\n", dev_name(dev));
927 		return -ENODEV;
928 	}
929 
930 	/* per-device configurations are common */
931 	if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
932 			   le16_to_cpu(usb_dev->descriptor.idVendor),
933 			   le16_to_cpu(usb_dev->descriptor.idProduct),
934 			   le16_to_cpu(usb_dev->descriptor.bcdDevice)))
935 		return -ENOMEM;
936 
937 	/* class-based driver binding models */
938 	if (add_uevent_var(env, "TYPE=%d/%d/%d",
939 			   usb_dev->descriptor.bDeviceClass,
940 			   usb_dev->descriptor.bDeviceSubClass,
941 			   usb_dev->descriptor.bDeviceProtocol))
942 		return -ENOMEM;
943 
944 	return 0;
945 }
946 
__usb_bus_reprobe_drivers(struct device * dev,void * data)947 static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
948 {
949 	struct usb_device_driver *new_udriver = data;
950 	struct usb_device *udev;
951 	int ret;
952 
953 	/* Don't reprobe if current driver isn't usb_generic_driver */
954 	if (dev->driver != &usb_generic_driver.drvwrap.driver)
955 		return 0;
956 
957 	udev = to_usb_device(dev);
958 	if (!usb_driver_applicable(udev, new_udriver))
959 		return 0;
960 
961 	ret = device_reprobe(dev);
962 	if (ret && ret != -EPROBE_DEFER)
963 		dev_err(dev, "Failed to reprobe device (error %d)\n", ret);
964 
965 	return 0;
966 }
967 
968 /**
969  * usb_register_device_driver - register a USB device (not interface) driver
970  * @new_udriver: USB operations for the device driver
971  * @owner: module owner of this driver.
972  *
973  * Registers a USB device driver with the USB core.  The list of
974  * unattached devices will be rescanned whenever a new driver is
975  * added, allowing the new driver to attach to any recognized devices.
976  *
977  * Return: A negative error code on failure and 0 on success.
978  */
usb_register_device_driver(struct usb_device_driver * new_udriver,struct module * owner)979 int usb_register_device_driver(struct usb_device_driver *new_udriver,
980 		struct module *owner)
981 {
982 	int retval = 0;
983 
984 	if (usb_disabled())
985 		return -ENODEV;
986 
987 	new_udriver->drvwrap.for_devices = 1;
988 	new_udriver->drvwrap.driver.name = new_udriver->name;
989 	new_udriver->drvwrap.driver.bus = &usb_bus_type;
990 	new_udriver->drvwrap.driver.probe = usb_probe_device;
991 	new_udriver->drvwrap.driver.remove = usb_unbind_device;
992 	new_udriver->drvwrap.driver.owner = owner;
993 	new_udriver->drvwrap.driver.dev_groups = new_udriver->dev_groups;
994 
995 	retval = driver_register(&new_udriver->drvwrap.driver);
996 
997 	if (!retval) {
998 		pr_info("%s: registered new device driver %s\n",
999 			usbcore_name, new_udriver->name);
1000 		/*
1001 		 * Check whether any device could be better served with
1002 		 * this new driver
1003 		 */
1004 		bus_for_each_dev(&usb_bus_type, NULL, new_udriver,
1005 				 __usb_bus_reprobe_drivers);
1006 	} else {
1007 		pr_err("%s: error %d registering device driver %s\n",
1008 			usbcore_name, retval, new_udriver->name);
1009 	}
1010 
1011 	return retval;
1012 }
1013 EXPORT_SYMBOL_GPL(usb_register_device_driver);
1014 
1015 /**
1016  * usb_deregister_device_driver - unregister a USB device (not interface) driver
1017  * @udriver: USB operations of the device driver to unregister
1018  * Context: must be able to sleep
1019  *
1020  * Unlinks the specified driver from the internal USB driver list.
1021  */
usb_deregister_device_driver(struct usb_device_driver * udriver)1022 void usb_deregister_device_driver(struct usb_device_driver *udriver)
1023 {
1024 	pr_info("%s: deregistering device driver %s\n",
1025 			usbcore_name, udriver->name);
1026 
1027 	driver_unregister(&udriver->drvwrap.driver);
1028 }
1029 EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
1030 
1031 /**
1032  * usb_register_driver - register a USB interface driver
1033  * @new_driver: USB operations for the interface driver
1034  * @owner: module owner of this driver.
1035  * @mod_name: module name string
1036  *
1037  * Registers a USB interface driver with the USB core.  The list of
1038  * unattached interfaces will be rescanned whenever a new driver is
1039  * added, allowing the new driver to attach to any recognized interfaces.
1040  *
1041  * Return: A negative error code on failure and 0 on success.
1042  *
1043  * NOTE: if you want your driver to use the USB major number, you must call
1044  * usb_register_dev() to enable that functionality.  This function no longer
1045  * takes care of that.
1046  */
usb_register_driver(struct usb_driver * new_driver,struct module * owner,const char * mod_name)1047 int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
1048 			const char *mod_name)
1049 {
1050 	int retval = 0;
1051 
1052 	if (usb_disabled())
1053 		return -ENODEV;
1054 
1055 	new_driver->drvwrap.for_devices = 0;
1056 	new_driver->drvwrap.driver.name = new_driver->name;
1057 	new_driver->drvwrap.driver.bus = &usb_bus_type;
1058 	new_driver->drvwrap.driver.probe = usb_probe_interface;
1059 	new_driver->drvwrap.driver.remove = usb_unbind_interface;
1060 	new_driver->drvwrap.driver.owner = owner;
1061 	new_driver->drvwrap.driver.mod_name = mod_name;
1062 	new_driver->drvwrap.driver.dev_groups = new_driver->dev_groups;
1063 	spin_lock_init(&new_driver->dynids.lock);
1064 	INIT_LIST_HEAD(&new_driver->dynids.list);
1065 
1066 	retval = driver_register(&new_driver->drvwrap.driver);
1067 	if (retval)
1068 		goto out;
1069 
1070 	retval = usb_create_newid_files(new_driver);
1071 	if (retval)
1072 		goto out_newid;
1073 
1074 	pr_info("%s: registered new interface driver %s\n",
1075 			usbcore_name, new_driver->name);
1076 
1077 out:
1078 	return retval;
1079 
1080 out_newid:
1081 	driver_unregister(&new_driver->drvwrap.driver);
1082 
1083 	pr_err("%s: error %d registering interface driver %s\n",
1084 		usbcore_name, retval, new_driver->name);
1085 	goto out;
1086 }
1087 EXPORT_SYMBOL_GPL(usb_register_driver);
1088 
1089 /**
1090  * usb_deregister - unregister a USB interface driver
1091  * @driver: USB operations of the interface driver to unregister
1092  * Context: must be able to sleep
1093  *
1094  * Unlinks the specified driver from the internal USB driver list.
1095  *
1096  * NOTE: If you called usb_register_dev(), you still need to call
1097  * usb_deregister_dev() to clean up your driver's allocated minor numbers,
1098  * this * call will no longer do it for you.
1099  */
usb_deregister(struct usb_driver * driver)1100 void usb_deregister(struct usb_driver *driver)
1101 {
1102 	pr_info("%s: deregistering interface driver %s\n",
1103 			usbcore_name, driver->name);
1104 
1105 	usb_remove_newid_files(driver);
1106 	driver_unregister(&driver->drvwrap.driver);
1107 	usb_free_dynids(driver);
1108 }
1109 EXPORT_SYMBOL_GPL(usb_deregister);
1110 
1111 /* Forced unbinding of a USB interface driver, either because
1112  * it doesn't support pre_reset/post_reset/reset_resume or
1113  * because it doesn't support suspend/resume.
1114  *
1115  * The caller must hold @intf's device's lock, but not @intf's lock.
1116  */
usb_forced_unbind_intf(struct usb_interface * intf)1117 void usb_forced_unbind_intf(struct usb_interface *intf)
1118 {
1119 	struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1120 
1121 	dev_dbg(&intf->dev, "forced unbind\n");
1122 	usb_driver_release_interface(driver, intf);
1123 
1124 	/* Mark the interface for later rebinding */
1125 	intf->needs_binding = 1;
1126 }
1127 
1128 /*
1129  * Unbind drivers for @udev's marked interfaces.  These interfaces have
1130  * the needs_binding flag set, for example by usb_resume_interface().
1131  *
1132  * The caller must hold @udev's device lock.
1133  */
unbind_marked_interfaces(struct usb_device * udev)1134 static void unbind_marked_interfaces(struct usb_device *udev)
1135 {
1136 	struct usb_host_config	*config;
1137 	int			i;
1138 	struct usb_interface	*intf;
1139 
1140 	config = udev->actconfig;
1141 	if (config) {
1142 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1143 			intf = config->interface[i];
1144 			if (intf->dev.driver && intf->needs_binding)
1145 				usb_forced_unbind_intf(intf);
1146 		}
1147 	}
1148 }
1149 
1150 /* Delayed forced unbinding of a USB interface driver and scan
1151  * for rebinding.
1152  *
1153  * The caller must hold @intf's device's lock, but not @intf's lock.
1154  *
1155  * Note: Rebinds will be skipped if a system sleep transition is in
1156  * progress and the PM "complete" callback hasn't occurred yet.
1157  */
usb_rebind_intf(struct usb_interface * intf)1158 static void usb_rebind_intf(struct usb_interface *intf)
1159 {
1160 	int rc;
1161 
1162 	/* Delayed unbind of an existing driver */
1163 	if (intf->dev.driver)
1164 		usb_forced_unbind_intf(intf);
1165 
1166 	/* Try to rebind the interface */
1167 	if (!intf->dev.power.is_prepared) {
1168 		intf->needs_binding = 0;
1169 		rc = device_attach(&intf->dev);
1170 		if (rc < 0 && rc != -EPROBE_DEFER)
1171 			dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1172 	}
1173 }
1174 
1175 /*
1176  * Rebind drivers to @udev's marked interfaces.  These interfaces have
1177  * the needs_binding flag set.
1178  *
1179  * The caller must hold @udev's device lock.
1180  */
rebind_marked_interfaces(struct usb_device * udev)1181 static void rebind_marked_interfaces(struct usb_device *udev)
1182 {
1183 	struct usb_host_config	*config;
1184 	int			i;
1185 	struct usb_interface	*intf;
1186 
1187 	config = udev->actconfig;
1188 	if (config) {
1189 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1190 			intf = config->interface[i];
1191 			if (intf->needs_binding)
1192 				usb_rebind_intf(intf);
1193 		}
1194 	}
1195 }
1196 
1197 /*
1198  * Unbind all of @udev's marked interfaces and then rebind all of them.
1199  * This ordering is necessary because some drivers claim several interfaces
1200  * when they are first probed.
1201  *
1202  * The caller must hold @udev's device lock.
1203  */
usb_unbind_and_rebind_marked_interfaces(struct usb_device * udev)1204 void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev)
1205 {
1206 	unbind_marked_interfaces(udev);
1207 	rebind_marked_interfaces(udev);
1208 }
1209 
1210 #ifdef CONFIG_PM
1211 
1212 /* Unbind drivers for @udev's interfaces that don't support suspend/resume
1213  * There is no check for reset_resume here because it can be determined
1214  * only during resume whether reset_resume is needed.
1215  *
1216  * The caller must hold @udev's device lock.
1217  */
unbind_no_pm_drivers_interfaces(struct usb_device * udev)1218 static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
1219 {
1220 	struct usb_host_config	*config;
1221 	int			i;
1222 	struct usb_interface	*intf;
1223 	struct usb_driver	*drv;
1224 
1225 	config = udev->actconfig;
1226 	if (config) {
1227 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1228 			intf = config->interface[i];
1229 
1230 			if (intf->dev.driver) {
1231 				drv = to_usb_driver(intf->dev.driver);
1232 				if (!drv->suspend || !drv->resume)
1233 					usb_forced_unbind_intf(intf);
1234 			}
1235 		}
1236 	}
1237 }
1238 
usb_suspend_device(struct usb_device * udev,pm_message_t msg)1239 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1240 {
1241 	struct usb_device_driver	*udriver;
1242 	int				status = 0;
1243 
1244 	if (udev->state == USB_STATE_NOTATTACHED ||
1245 			udev->state == USB_STATE_SUSPENDED)
1246 		goto done;
1247 
1248 	/* For devices that don't have a driver, we do a generic suspend. */
1249 	if (udev->dev.driver)
1250 		udriver = to_usb_device_driver(udev->dev.driver);
1251 	else {
1252 		udev->do_remote_wakeup = 0;
1253 		udriver = &usb_generic_driver;
1254 	}
1255 	if (udriver->suspend)
1256 		status = udriver->suspend(udev, msg);
1257 	if (status == 0 && udriver->generic_subclass)
1258 		status = usb_generic_driver_suspend(udev, msg);
1259 
1260  done:
1261 	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1262 	return status;
1263 }
1264 
usb_resume_device(struct usb_device * udev,pm_message_t msg)1265 static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1266 {
1267 	struct usb_device_driver	*udriver;
1268 	int				status = 0;
1269 
1270 	if (udev->state == USB_STATE_NOTATTACHED)
1271 		goto done;
1272 
1273 	/* Can't resume it if it doesn't have a driver. */
1274 	if (udev->dev.driver == NULL) {
1275 		status = -ENOTCONN;
1276 		goto done;
1277 	}
1278 
1279 	/* Non-root devices on a full/low-speed bus must wait for their
1280 	 * companion high-speed root hub, in case a handoff is needed.
1281 	 */
1282 	if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
1283 		device_pm_wait_for_dev(&udev->dev,
1284 				&udev->bus->hs_companion->root_hub->dev);
1285 
1286 	if (udev->quirks & USB_QUIRK_RESET_RESUME)
1287 		udev->reset_resume = 1;
1288 
1289 	udriver = to_usb_device_driver(udev->dev.driver);
1290 	if (udriver->generic_subclass)
1291 		status = usb_generic_driver_resume(udev, msg);
1292 	if (status == 0 && udriver->resume)
1293 		status = udriver->resume(udev, msg);
1294 
1295  done:
1296 	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1297 	return status;
1298 }
1299 
usb_suspend_interface(struct usb_device * udev,struct usb_interface * intf,pm_message_t msg)1300 static int usb_suspend_interface(struct usb_device *udev,
1301 		struct usb_interface *intf, pm_message_t msg)
1302 {
1303 	struct usb_driver	*driver;
1304 	int			status = 0;
1305 
1306 	if (udev->state == USB_STATE_NOTATTACHED ||
1307 			intf->condition == USB_INTERFACE_UNBOUND)
1308 		goto done;
1309 	driver = to_usb_driver(intf->dev.driver);
1310 
1311 	/* at this time we know the driver supports suspend */
1312 	status = driver->suspend(intf, msg);
1313 	if (status && !PMSG_IS_AUTO(msg))
1314 		dev_err(&intf->dev, "suspend error %d\n", status);
1315 
1316  done:
1317 	dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1318 	return status;
1319 }
1320 
usb_resume_interface(struct usb_device * udev,struct usb_interface * intf,pm_message_t msg,int reset_resume)1321 static int usb_resume_interface(struct usb_device *udev,
1322 		struct usb_interface *intf, pm_message_t msg, int reset_resume)
1323 {
1324 	struct usb_driver	*driver;
1325 	int			status = 0;
1326 
1327 	if (udev->state == USB_STATE_NOTATTACHED)
1328 		goto done;
1329 
1330 	/* Don't let autoresume interfere with unbinding */
1331 	if (intf->condition == USB_INTERFACE_UNBINDING)
1332 		goto done;
1333 
1334 	/* Can't resume it if it doesn't have a driver. */
1335 	if (intf->condition == USB_INTERFACE_UNBOUND) {
1336 
1337 		/* Carry out a deferred switch to altsetting 0 */
1338 		if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
1339 			usb_set_interface(udev, intf->altsetting[0].
1340 					desc.bInterfaceNumber, 0);
1341 			intf->needs_altsetting0 = 0;
1342 		}
1343 		goto done;
1344 	}
1345 
1346 	/* Don't resume if the interface is marked for rebinding */
1347 	if (intf->needs_binding)
1348 		goto done;
1349 	driver = to_usb_driver(intf->dev.driver);
1350 
1351 	if (reset_resume) {
1352 		if (driver->reset_resume) {
1353 			status = driver->reset_resume(intf);
1354 			if (status)
1355 				dev_err(&intf->dev, "%s error %d\n",
1356 						"reset_resume", status);
1357 		} else {
1358 			intf->needs_binding = 1;
1359 			dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1360 					driver->name);
1361 		}
1362 	} else {
1363 		status = driver->resume(intf);
1364 		if (status)
1365 			dev_err(&intf->dev, "resume error %d\n", status);
1366 	}
1367 
1368 done:
1369 	dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1370 
1371 	/* Later we will unbind the driver and/or reprobe, if necessary */
1372 	return status;
1373 }
1374 
1375 /**
1376  * usb_suspend_both - suspend a USB device and its interfaces
1377  * @udev: the usb_device to suspend
1378  * @msg: Power Management message describing this state transition
1379  *
1380  * This is the central routine for suspending USB devices.  It calls the
1381  * suspend methods for all the interface drivers in @udev and then calls
1382  * the suspend method for @udev itself.  When the routine is called in
1383  * autosuspend, if an error occurs at any stage, all the interfaces
1384  * which were suspended are resumed so that they remain in the same
1385  * state as the device, but when called from system sleep, all error
1386  * from suspend methods of interfaces and the non-root-hub device itself
1387  * are simply ignored, so all suspended interfaces are only resumed
1388  * to the device's state when @udev is root-hub and its suspend method
1389  * returns failure.
1390  *
1391  * Autosuspend requests originating from a child device or an interface
1392  * driver may be made without the protection of @udev's device lock, but
1393  * all other suspend calls will hold the lock.  Usbcore will insure that
1394  * method calls do not arrive during bind, unbind, or reset operations.
1395  * However drivers must be prepared to handle suspend calls arriving at
1396  * unpredictable times.
1397  *
1398  * This routine can run only in process context.
1399  *
1400  * Return: 0 if the suspend succeeded.
1401  */
usb_suspend_both(struct usb_device * udev,pm_message_t msg)1402 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1403 {
1404 	int			status = 0;
1405 	int			i = 0, n = 0;
1406 	struct usb_interface	*intf;
1407 	int			bypass = 0;
1408 
1409 	if (udev->state == USB_STATE_NOTATTACHED ||
1410 			udev->state == USB_STATE_SUSPENDED)
1411 		goto done;
1412 
1413 	trace_android_vh_usb_dev_suspend(udev, msg, &bypass);
1414 	if (bypass)
1415 		goto done;
1416 
1417 	/* Suspend all the interfaces and then udev itself */
1418 	if (udev->actconfig) {
1419 		n = udev->actconfig->desc.bNumInterfaces;
1420 		for (i = n - 1; i >= 0; --i) {
1421 			intf = udev->actconfig->interface[i];
1422 			status = usb_suspend_interface(udev, intf, msg);
1423 
1424 			/* Ignore errors during system sleep transitions */
1425 			if (!PMSG_IS_AUTO(msg))
1426 				status = 0;
1427 			if (status != 0)
1428 				break;
1429 		}
1430 	}
1431 	if (status == 0) {
1432 		status = usb_suspend_device(udev, msg);
1433 
1434 		/*
1435 		 * Ignore errors from non-root-hub devices during
1436 		 * system sleep transitions.  For the most part,
1437 		 * these devices should go to low power anyway when
1438 		 * the entire bus is suspended.
1439 		 */
1440 		if (udev->parent && !PMSG_IS_AUTO(msg))
1441 			status = 0;
1442 
1443 		/*
1444 		 * If the device is inaccessible, don't try to resume
1445 		 * suspended interfaces and just return the error.
1446 		 */
1447 		if (status && status != -EBUSY) {
1448 			int err;
1449 			u16 devstat;
1450 
1451 			err = usb_get_std_status(udev, USB_RECIP_DEVICE, 0,
1452 						 &devstat);
1453 			if (err) {
1454 				dev_err(&udev->dev,
1455 					"Failed to suspend device, error %d\n",
1456 					status);
1457 				goto done;
1458 			}
1459 		}
1460 	}
1461 
1462 	/* If the suspend failed, resume interfaces that did get suspended */
1463 	if (status != 0) {
1464 		if (udev->actconfig) {
1465 			msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1466 			while (++i < n) {
1467 				intf = udev->actconfig->interface[i];
1468 				usb_resume_interface(udev, intf, msg, 0);
1469 			}
1470 		}
1471 
1472 	/* If the suspend succeeded then prevent any more URB submissions
1473 	 * and flush any outstanding URBs.
1474 	 */
1475 	} else {
1476 		udev->can_submit = 0;
1477 		for (i = 0; i < 16; ++i) {
1478 			usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1479 			usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1480 		}
1481 	}
1482 
1483  done:
1484 	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1485 	return status;
1486 }
1487 
1488 /**
1489  * usb_resume_both - resume a USB device and its interfaces
1490  * @udev: the usb_device to resume
1491  * @msg: Power Management message describing this state transition
1492  *
1493  * This is the central routine for resuming USB devices.  It calls the
1494  * the resume method for @udev and then calls the resume methods for all
1495  * the interface drivers in @udev.
1496  *
1497  * Autoresume requests originating from a child device or an interface
1498  * driver may be made without the protection of @udev's device lock, but
1499  * all other resume calls will hold the lock.  Usbcore will insure that
1500  * method calls do not arrive during bind, unbind, or reset operations.
1501  * However drivers must be prepared to handle resume calls arriving at
1502  * unpredictable times.
1503  *
1504  * This routine can run only in process context.
1505  *
1506  * Return: 0 on success.
1507  */
usb_resume_both(struct usb_device * udev,pm_message_t msg)1508 static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1509 {
1510 	int			status = 0;
1511 	int			i;
1512 	struct usb_interface	*intf;
1513 	int			bypass = 0;
1514 
1515 	if (udev->state == USB_STATE_NOTATTACHED) {
1516 		status = -ENODEV;
1517 		goto done;
1518 	}
1519 
1520 	trace_android_vh_usb_dev_resume(udev, msg, &bypass);
1521 	if (bypass)
1522 		goto done;
1523 
1524 	udev->can_submit = 1;
1525 
1526 	/* Resume the device */
1527 	if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1528 		status = usb_resume_device(udev, msg);
1529 
1530 	/* Resume the interfaces */
1531 	if (status == 0 && udev->actconfig) {
1532 		for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1533 			intf = udev->actconfig->interface[i];
1534 			usb_resume_interface(udev, intf, msg,
1535 					udev->reset_resume);
1536 		}
1537 	}
1538 	usb_mark_last_busy(udev);
1539 
1540  done:
1541 	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1542 	if (!status)
1543 		udev->reset_resume = 0;
1544 	return status;
1545 }
1546 
choose_wakeup(struct usb_device * udev,pm_message_t msg)1547 static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1548 {
1549 	int	w;
1550 
1551 	/* Remote wakeup is needed only when we actually go to sleep.
1552 	 * For things like FREEZE and QUIESCE, if the device is already
1553 	 * autosuspended then its current wakeup setting is okay.
1554 	 */
1555 	if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1556 		if (udev->state != USB_STATE_SUSPENDED)
1557 			udev->do_remote_wakeup = 0;
1558 		return;
1559 	}
1560 
1561 	/* Enable remote wakeup if it is allowed, even if no interface drivers
1562 	 * actually want it.
1563 	 */
1564 	w = device_may_wakeup(&udev->dev);
1565 
1566 	/* If the device is autosuspended with the wrong wakeup setting,
1567 	 * autoresume now so the setting can be changed.
1568 	 */
1569 	if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1570 		pm_runtime_resume(&udev->dev);
1571 	udev->do_remote_wakeup = w;
1572 }
1573 
1574 /* The device lock is held by the PM core */
usb_suspend(struct device * dev,pm_message_t msg)1575 int usb_suspend(struct device *dev, pm_message_t msg)
1576 {
1577 	struct usb_device	*udev = to_usb_device(dev);
1578 	int r;
1579 
1580 	unbind_no_pm_drivers_interfaces(udev);
1581 
1582 	/* From now on we are sure all drivers support suspend/resume
1583 	 * but not necessarily reset_resume()
1584 	 * so we may still need to unbind and rebind upon resume
1585 	 */
1586 	choose_wakeup(udev, msg);
1587 	r = usb_suspend_both(udev, msg);
1588 	if (r)
1589 		return r;
1590 
1591 	if (udev->quirks & USB_QUIRK_DISCONNECT_SUSPEND)
1592 		usb_port_disable(udev);
1593 
1594 	return 0;
1595 }
1596 
1597 /* The device lock is held by the PM core */
usb_resume_complete(struct device * dev)1598 int usb_resume_complete(struct device *dev)
1599 {
1600 	struct usb_device *udev = to_usb_device(dev);
1601 
1602 	/* For PM complete calls, all we do is rebind interfaces
1603 	 * whose needs_binding flag is set
1604 	 */
1605 	if (udev->state != USB_STATE_NOTATTACHED)
1606 		rebind_marked_interfaces(udev);
1607 	return 0;
1608 }
1609 
1610 /* The device lock is held by the PM core */
usb_resume(struct device * dev,pm_message_t msg)1611 int usb_resume(struct device *dev, pm_message_t msg)
1612 {
1613 	struct usb_device	*udev = to_usb_device(dev);
1614 	int			status;
1615 
1616 	/* For all calls, take the device back to full power and
1617 	 * tell the PM core in case it was autosuspended previously.
1618 	 * Unbind the interfaces that will need rebinding later,
1619 	 * because they fail to support reset_resume.
1620 	 * (This can't be done in usb_resume_interface()
1621 	 * above because it doesn't own the right set of locks.)
1622 	 */
1623 	status = usb_resume_both(udev, msg);
1624 	if (status == 0) {
1625 		pm_runtime_disable(dev);
1626 		pm_runtime_set_active(dev);
1627 		pm_runtime_enable(dev);
1628 		unbind_marked_interfaces(udev);
1629 	}
1630 
1631 	/* Avoid PM error messages for devices disconnected while suspended
1632 	 * as we'll display regular disconnect messages just a bit later.
1633 	 */
1634 	if (status == -ENODEV || status == -ESHUTDOWN)
1635 		status = 0;
1636 	return status;
1637 }
1638 
1639 /**
1640  * usb_enable_autosuspend - allow a USB device to be autosuspended
1641  * @udev: the USB device which may be autosuspended
1642  *
1643  * This routine allows @udev to be autosuspended.  An autosuspend won't
1644  * take place until the autosuspend_delay has elapsed and all the other
1645  * necessary conditions are satisfied.
1646  *
1647  * The caller must hold @udev's device lock.
1648  */
usb_enable_autosuspend(struct usb_device * udev)1649 void usb_enable_autosuspend(struct usb_device *udev)
1650 {
1651 	pm_runtime_allow(&udev->dev);
1652 }
1653 EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1654 
1655 /**
1656  * usb_disable_autosuspend - prevent a USB device from being autosuspended
1657  * @udev: the USB device which may not be autosuspended
1658  *
1659  * This routine prevents @udev from being autosuspended and wakes it up
1660  * if it is already autosuspended.
1661  *
1662  * The caller must hold @udev's device lock.
1663  */
usb_disable_autosuspend(struct usb_device * udev)1664 void usb_disable_autosuspend(struct usb_device *udev)
1665 {
1666 	pm_runtime_forbid(&udev->dev);
1667 }
1668 EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1669 
1670 /**
1671  * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1672  * @udev: the usb_device to autosuspend
1673  *
1674  * This routine should be called when a core subsystem is finished using
1675  * @udev and wants to allow it to autosuspend.  Examples would be when
1676  * @udev's device file in usbfs is closed or after a configuration change.
1677  *
1678  * @udev's usage counter is decremented; if it drops to 0 and all the
1679  * interfaces are inactive then a delayed autosuspend will be attempted.
1680  * The attempt may fail (see autosuspend_check()).
1681  *
1682  * The caller must hold @udev's device lock.
1683  *
1684  * This routine can run only in process context.
1685  */
usb_autosuspend_device(struct usb_device * udev)1686 void usb_autosuspend_device(struct usb_device *udev)
1687 {
1688 	int	status;
1689 
1690 	usb_mark_last_busy(udev);
1691 	status = pm_runtime_put_sync_autosuspend(&udev->dev);
1692 	dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1693 			__func__, atomic_read(&udev->dev.power.usage_count),
1694 			status);
1695 }
1696 
1697 /**
1698  * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1699  * @udev: the usb_device to autoresume
1700  *
1701  * This routine should be called when a core subsystem wants to use @udev
1702  * and needs to guarantee that it is not suspended.  No autosuspend will
1703  * occur until usb_autosuspend_device() is called.  (Note that this will
1704  * not prevent suspend events originating in the PM core.)  Examples would
1705  * be when @udev's device file in usbfs is opened or when a remote-wakeup
1706  * request is received.
1707  *
1708  * @udev's usage counter is incremented to prevent subsequent autosuspends.
1709  * However if the autoresume fails then the usage counter is re-decremented.
1710  *
1711  * The caller must hold @udev's device lock.
1712  *
1713  * This routine can run only in process context.
1714  *
1715  * Return: 0 on success. A negative error code otherwise.
1716  */
usb_autoresume_device(struct usb_device * udev)1717 int usb_autoresume_device(struct usb_device *udev)
1718 {
1719 	int	status;
1720 
1721 	status = pm_runtime_get_sync(&udev->dev);
1722 	if (status < 0)
1723 		pm_runtime_put_sync(&udev->dev);
1724 	dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1725 			__func__, atomic_read(&udev->dev.power.usage_count),
1726 			status);
1727 	if (status > 0)
1728 		status = 0;
1729 	return status;
1730 }
1731 
1732 /**
1733  * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1734  * @intf: the usb_interface whose counter should be decremented
1735  *
1736  * This routine should be called by an interface driver when it is
1737  * finished using @intf and wants to allow it to autosuspend.  A typical
1738  * example would be a character-device driver when its device file is
1739  * closed.
1740  *
1741  * The routine decrements @intf's usage counter.  When the counter reaches
1742  * 0, a delayed autosuspend request for @intf's device is attempted.  The
1743  * attempt may fail (see autosuspend_check()).
1744  *
1745  * This routine can run only in process context.
1746  */
usb_autopm_put_interface(struct usb_interface * intf)1747 void usb_autopm_put_interface(struct usb_interface *intf)
1748 {
1749 	struct usb_device	*udev = interface_to_usbdev(intf);
1750 	int			status;
1751 
1752 	usb_mark_last_busy(udev);
1753 	status = pm_runtime_put_sync(&intf->dev);
1754 	dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1755 			__func__, atomic_read(&intf->dev.power.usage_count),
1756 			status);
1757 }
1758 EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1759 
1760 /**
1761  * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1762  * @intf: the usb_interface whose counter should be decremented
1763  *
1764  * This routine does much the same thing as usb_autopm_put_interface():
1765  * It decrements @intf's usage counter and schedules a delayed
1766  * autosuspend request if the counter is <= 0.  The difference is that it
1767  * does not perform any synchronization; callers should hold a private
1768  * lock and handle all synchronization issues themselves.
1769  *
1770  * Typically a driver would call this routine during an URB's completion
1771  * handler, if no more URBs were pending.
1772  *
1773  * This routine can run in atomic context.
1774  */
usb_autopm_put_interface_async(struct usb_interface * intf)1775 void usb_autopm_put_interface_async(struct usb_interface *intf)
1776 {
1777 	struct usb_device	*udev = interface_to_usbdev(intf);
1778 	int			status;
1779 
1780 	usb_mark_last_busy(udev);
1781 	status = pm_runtime_put(&intf->dev);
1782 	dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1783 			__func__, atomic_read(&intf->dev.power.usage_count),
1784 			status);
1785 }
1786 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1787 
1788 /**
1789  * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1790  * @intf: the usb_interface whose counter should be decremented
1791  *
1792  * This routine decrements @intf's usage counter but does not carry out an
1793  * autosuspend.
1794  *
1795  * This routine can run in atomic context.
1796  */
usb_autopm_put_interface_no_suspend(struct usb_interface * intf)1797 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1798 {
1799 	struct usb_device	*udev = interface_to_usbdev(intf);
1800 
1801 	usb_mark_last_busy(udev);
1802 	pm_runtime_put_noidle(&intf->dev);
1803 }
1804 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1805 
1806 /**
1807  * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1808  * @intf: the usb_interface whose counter should be incremented
1809  *
1810  * This routine should be called by an interface driver when it wants to
1811  * use @intf and needs to guarantee that it is not suspended.  In addition,
1812  * the routine prevents @intf from being autosuspended subsequently.  (Note
1813  * that this will not prevent suspend events originating in the PM core.)
1814  * This prevention will persist until usb_autopm_put_interface() is called
1815  * or @intf is unbound.  A typical example would be a character-device
1816  * driver when its device file is opened.
1817  *
1818  * @intf's usage counter is incremented to prevent subsequent autosuspends.
1819  * However if the autoresume fails then the counter is re-decremented.
1820  *
1821  * This routine can run only in process context.
1822  *
1823  * Return: 0 on success.
1824  */
usb_autopm_get_interface(struct usb_interface * intf)1825 int usb_autopm_get_interface(struct usb_interface *intf)
1826 {
1827 	int	status;
1828 
1829 	status = pm_runtime_get_sync(&intf->dev);
1830 	if (status < 0)
1831 		pm_runtime_put_sync(&intf->dev);
1832 	dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1833 			__func__, atomic_read(&intf->dev.power.usage_count),
1834 			status);
1835 	if (status > 0)
1836 		status = 0;
1837 	return status;
1838 }
1839 EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1840 
1841 /**
1842  * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1843  * @intf: the usb_interface whose counter should be incremented
1844  *
1845  * This routine does much the same thing as
1846  * usb_autopm_get_interface(): It increments @intf's usage counter and
1847  * queues an autoresume request if the device is suspended.  The
1848  * differences are that it does not perform any synchronization (callers
1849  * should hold a private lock and handle all synchronization issues
1850  * themselves), and it does not autoresume the device directly (it only
1851  * queues a request).  After a successful call, the device may not yet be
1852  * resumed.
1853  *
1854  * This routine can run in atomic context.
1855  *
1856  * Return: 0 on success. A negative error code otherwise.
1857  */
usb_autopm_get_interface_async(struct usb_interface * intf)1858 int usb_autopm_get_interface_async(struct usb_interface *intf)
1859 {
1860 	int	status;
1861 
1862 	status = pm_runtime_get(&intf->dev);
1863 	if (status < 0 && status != -EINPROGRESS)
1864 		pm_runtime_put_noidle(&intf->dev);
1865 	dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1866 			__func__, atomic_read(&intf->dev.power.usage_count),
1867 			status);
1868 	if (status > 0 || status == -EINPROGRESS)
1869 		status = 0;
1870 	return status;
1871 }
1872 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1873 
1874 /**
1875  * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1876  * @intf: the usb_interface whose counter should be incremented
1877  *
1878  * This routine increments @intf's usage counter but does not carry out an
1879  * autoresume.
1880  *
1881  * This routine can run in atomic context.
1882  */
usb_autopm_get_interface_no_resume(struct usb_interface * intf)1883 void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1884 {
1885 	struct usb_device	*udev = interface_to_usbdev(intf);
1886 
1887 	usb_mark_last_busy(udev);
1888 	pm_runtime_get_noresume(&intf->dev);
1889 }
1890 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1891 
1892 /* Internal routine to check whether we may autosuspend a device. */
autosuspend_check(struct usb_device * udev)1893 static int autosuspend_check(struct usb_device *udev)
1894 {
1895 	int			w, i;
1896 	struct usb_interface	*intf;
1897 
1898 	if (udev->state == USB_STATE_NOTATTACHED)
1899 		return -ENODEV;
1900 
1901 	/* Fail if autosuspend is disabled, or any interfaces are in use, or
1902 	 * any interface drivers require remote wakeup but it isn't available.
1903 	 */
1904 	w = 0;
1905 	if (udev->actconfig) {
1906 		for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1907 			intf = udev->actconfig->interface[i];
1908 
1909 			/* We don't need to check interfaces that are
1910 			 * disabled for runtime PM.  Either they are unbound
1911 			 * or else their drivers don't support autosuspend
1912 			 * and so they are permanently active.
1913 			 */
1914 			if (intf->dev.power.disable_depth)
1915 				continue;
1916 			if (atomic_read(&intf->dev.power.usage_count) > 0)
1917 				return -EBUSY;
1918 			w |= intf->needs_remote_wakeup;
1919 
1920 			/* Don't allow autosuspend if the device will need
1921 			 * a reset-resume and any of its interface drivers
1922 			 * doesn't include support or needs remote wakeup.
1923 			 */
1924 			if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1925 				struct usb_driver *driver;
1926 
1927 				driver = to_usb_driver(intf->dev.driver);
1928 				if (!driver->reset_resume ||
1929 						intf->needs_remote_wakeup)
1930 					return -EOPNOTSUPP;
1931 			}
1932 		}
1933 	}
1934 	if (w && !device_can_wakeup(&udev->dev)) {
1935 		dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1936 		return -EOPNOTSUPP;
1937 	}
1938 
1939 	/*
1940 	 * If the device is a direct child of the root hub and the HCD
1941 	 * doesn't handle wakeup requests, don't allow autosuspend when
1942 	 * wakeup is needed.
1943 	 */
1944 	if (w && udev->parent == udev->bus->root_hub &&
1945 			bus_to_hcd(udev->bus)->cant_recv_wakeups) {
1946 		dev_dbg(&udev->dev, "HCD doesn't handle wakeup requests\n");
1947 		return -EOPNOTSUPP;
1948 	}
1949 
1950 	udev->do_remote_wakeup = w;
1951 	return 0;
1952 }
1953 
usb_runtime_suspend(struct device * dev)1954 int usb_runtime_suspend(struct device *dev)
1955 {
1956 	struct usb_device	*udev = to_usb_device(dev);
1957 	int			status;
1958 
1959 	/* A USB device can be suspended if it passes the various autosuspend
1960 	 * checks.  Runtime suspend for a USB device means suspending all the
1961 	 * interfaces and then the device itself.
1962 	 */
1963 	if (autosuspend_check(udev) != 0)
1964 		return -EAGAIN;
1965 
1966 	status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1967 
1968 	/* Allow a retry if autosuspend failed temporarily */
1969 	if (status == -EAGAIN || status == -EBUSY)
1970 		usb_mark_last_busy(udev);
1971 
1972 	/*
1973 	 * The PM core reacts badly unless the return code is 0,
1974 	 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1975 	 * (except for root hubs, because they don't suspend through
1976 	 * an upstream port like other USB devices).
1977 	 */
1978 	if (status != 0 && udev->parent)
1979 		return -EBUSY;
1980 	return status;
1981 }
1982 
usb_runtime_resume(struct device * dev)1983 int usb_runtime_resume(struct device *dev)
1984 {
1985 	struct usb_device	*udev = to_usb_device(dev);
1986 	int			status;
1987 
1988 	/* Runtime resume for a USB device means resuming both the device
1989 	 * and all its interfaces.
1990 	 */
1991 	status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1992 	return status;
1993 }
1994 
usb_runtime_idle(struct device * dev)1995 int usb_runtime_idle(struct device *dev)
1996 {
1997 	struct usb_device	*udev = to_usb_device(dev);
1998 
1999 	/* An idle USB device can be suspended if it passes the various
2000 	 * autosuspend checks.
2001 	 */
2002 	if (autosuspend_check(udev) == 0)
2003 		pm_runtime_autosuspend(dev);
2004 	/* Tell the core not to suspend it, though. */
2005 	return -EBUSY;
2006 }
2007 
usb_set_usb2_hardware_lpm(struct usb_device * udev,int enable)2008 static int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
2009 {
2010 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2011 	int ret = -EPERM;
2012 
2013 	if (hcd->driver->set_usb2_hw_lpm) {
2014 		ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
2015 		if (!ret)
2016 			udev->usb2_hw_lpm_enabled = enable;
2017 	}
2018 
2019 	return ret;
2020 }
2021 
usb_enable_usb2_hardware_lpm(struct usb_device * udev)2022 int usb_enable_usb2_hardware_lpm(struct usb_device *udev)
2023 {
2024 	if (!udev->usb2_hw_lpm_capable ||
2025 	    !udev->usb2_hw_lpm_allowed ||
2026 	    udev->usb2_hw_lpm_enabled)
2027 		return 0;
2028 
2029 	return usb_set_usb2_hardware_lpm(udev, 1);
2030 }
2031 
usb_disable_usb2_hardware_lpm(struct usb_device * udev)2032 int usb_disable_usb2_hardware_lpm(struct usb_device *udev)
2033 {
2034 	if (!udev->usb2_hw_lpm_enabled)
2035 		return 0;
2036 
2037 	return usb_set_usb2_hardware_lpm(udev, 0);
2038 }
2039 
2040 #endif /* CONFIG_PM */
2041 
2042 struct bus_type usb_bus_type = {
2043 	.name =		"usb",
2044 	.match =	usb_device_match,
2045 	.uevent =	usb_uevent,
2046 	.need_parent_lock =	true,
2047 };
2048