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