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