xref: /openbmc/linux/drivers/usb/core/driver.c (revision 9d11b134)
1aa1f3bb5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ddae41beSGreg Kroah-Hartman /*
39899aa5bSWei Ming Chen  * drivers/usb/core/driver.c - most of the driver model stuff for usb
4ddae41beSGreg Kroah-Hartman  *
5ddae41beSGreg Kroah-Hartman  * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
6ddae41beSGreg Kroah-Hartman  *
7ddae41beSGreg Kroah-Hartman  * based on drivers/usb/usb.c which had the following copyrights:
8ddae41beSGreg Kroah-Hartman  *	(C) Copyright Linus Torvalds 1999
9ddae41beSGreg Kroah-Hartman  *	(C) Copyright Johannes Erdfelt 1999-2001
10ddae41beSGreg Kroah-Hartman  *	(C) Copyright Andreas Gal 1999
11ddae41beSGreg Kroah-Hartman  *	(C) Copyright Gregory P. Smith 1999
12ddae41beSGreg Kroah-Hartman  *	(C) Copyright Deti Fliegl 1999 (new USB architecture)
13ddae41beSGreg Kroah-Hartman  *	(C) Copyright Randy Dunlap 2000
14ddae41beSGreg Kroah-Hartman  *	(C) Copyright David Brownell 2000-2004
15ddae41beSGreg Kroah-Hartman  *	(C) Copyright Yggdrasil Computing, Inc. 2000
16ddae41beSGreg Kroah-Hartman  *		(usb_device_id matching changes by Adam J. Richter)
17ddae41beSGreg Kroah-Hartman  *	(C) Copyright Greg Kroah-Hartman 2002-2003
18ddae41beSGreg Kroah-Hartman  *
19b65fba3dSGreg Kroah-Hartman  * Released under the GPLv2 only.
20b65fba3dSGreg Kroah-Hartman  *
21ddae41beSGreg Kroah-Hartman  * NOTE! This is not actually a driver at all, rather this is
22ddae41beSGreg Kroah-Hartman  * just a collection of helper routines that implement the
2336e56a34SAlan Stern  * matching, probing, releasing, suspending and resuming for
2436e56a34SAlan Stern  * real drivers.
25ddae41beSGreg Kroah-Hartman  *
26ddae41beSGreg Kroah-Hartman  */
27ddae41beSGreg Kroah-Hartman 
28ddae41beSGreg Kroah-Hartman #include <linux/device.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30f940fcd8SPaul Gortmaker #include <linux/export.h>
31ddae41beSGreg Kroah-Hartman #include <linux/usb.h>
326bc6cff5SAlan Stern #include <linux/usb/quirks.h>
3327729aadSEric Lescouet #include <linux/usb/hcd.h>
3427729aadSEric Lescouet 
35ddae41beSGreg Kroah-Hartman #include "usb.h"
36ddae41beSGreg Kroah-Hartman 
3720dfdad7SAlan Stern 
38733260ffSGreg Kroah-Hartman /*
39733260ffSGreg Kroah-Hartman  * Adds a new dynamic USBdevice ID to this driver,
40733260ffSGreg Kroah-Hartman  * and cause the driver to probe for all devices again.
41733260ffSGreg Kroah-Hartman  */
usb_store_new_id(struct usb_dynids * dynids,const struct usb_device_id * id_table,struct device_driver * driver,const char * buf,size_t count)4293bacefcSGreg Kroah-Hartman ssize_t usb_store_new_id(struct usb_dynids *dynids,
432fc82c2dSWolfram Sang 			 const struct usb_device_id *id_table,
4493bacefcSGreg Kroah-Hartman 			 struct device_driver *driver,
45733260ffSGreg Kroah-Hartman 			 const char *buf, size_t count)
46733260ffSGreg Kroah-Hartman {
47733260ffSGreg Kroah-Hartman 	struct usb_dynid *dynid;
48733260ffSGreg Kroah-Hartman 	u32 idVendor = 0;
49733260ffSGreg Kroah-Hartman 	u32 idProduct = 0;
50ff231db8SJosua Dietze 	unsigned int bInterfaceClass = 0;
512fc82c2dSWolfram Sang 	u32 refVendor, refProduct;
52733260ffSGreg Kroah-Hartman 	int fields = 0;
531b21d5e1SGreg Kroah-Hartman 	int retval = 0;
54733260ffSGreg Kroah-Hartman 
552fc82c2dSWolfram Sang 	fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct,
562fc82c2dSWolfram Sang 			&bInterfaceClass, &refVendor, &refProduct);
57733260ffSGreg Kroah-Hartman 	if (fields < 2)
58733260ffSGreg Kroah-Hartman 		return -EINVAL;
59733260ffSGreg Kroah-Hartman 
60733260ffSGreg Kroah-Hartman 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
61733260ffSGreg Kroah-Hartman 	if (!dynid)
62733260ffSGreg Kroah-Hartman 		return -ENOMEM;
63733260ffSGreg Kroah-Hartman 
64733260ffSGreg Kroah-Hartman 	INIT_LIST_HEAD(&dynid->node);
65733260ffSGreg Kroah-Hartman 	dynid->id.idVendor = idVendor;
66733260ffSGreg Kroah-Hartman 	dynid->id.idProduct = idProduct;
67733260ffSGreg Kroah-Hartman 	dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
68c63fe8f6SWolfram Sang 	if (fields > 2 && bInterfaceClass) {
697f196cafSChristian Engelmayer 		if (bInterfaceClass > 255) {
707f196cafSChristian Engelmayer 			retval = -EINVAL;
717f196cafSChristian Engelmayer 			goto fail;
727f196cafSChristian Engelmayer 		}
73c63fe8f6SWolfram Sang 
74ff231db8SJosua Dietze 		dynid->id.bInterfaceClass = (u8)bInterfaceClass;
75ff231db8SJosua Dietze 		dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
76ff231db8SJosua Dietze 	}
77733260ffSGreg Kroah-Hartman 
782fc82c2dSWolfram Sang 	if (fields > 4) {
792fc82c2dSWolfram Sang 		const struct usb_device_id *id = id_table;
802fc82c2dSWolfram Sang 
817f196cafSChristian Engelmayer 		if (!id) {
827f196cafSChristian Engelmayer 			retval = -ENODEV;
837f196cafSChristian Engelmayer 			goto fail;
847f196cafSChristian Engelmayer 		}
851b9fb31fSWolfram Sang 
862fc82c2dSWolfram Sang 		for (; id->match_flags; id++)
8752a6966cSWolfram Sang 			if (id->idVendor == refVendor && id->idProduct == refProduct)
882fc82c2dSWolfram Sang 				break;
8952a6966cSWolfram Sang 
907f196cafSChristian Engelmayer 		if (id->match_flags) {
9152a6966cSWolfram Sang 			dynid->id.driver_info = id->driver_info;
927f196cafSChristian Engelmayer 		} else {
937f196cafSChristian Engelmayer 			retval = -ENODEV;
947f196cafSChristian Engelmayer 			goto fail;
957f196cafSChristian Engelmayer 		}
962fc82c2dSWolfram Sang 	}
972fc82c2dSWolfram Sang 
9893bacefcSGreg Kroah-Hartman 	spin_lock(&dynids->lock);
99e5dd0115SNathael Pajani 	list_add_tail(&dynid->node, &dynids->list);
10093bacefcSGreg Kroah-Hartman 	spin_unlock(&dynids->lock);
101733260ffSGreg Kroah-Hartman 
1021b21d5e1SGreg Kroah-Hartman 	retval = driver_attach(driver);
103733260ffSGreg Kroah-Hartman 
1041b21d5e1SGreg Kroah-Hartman 	if (retval)
1051b21d5e1SGreg Kroah-Hartman 		return retval;
106733260ffSGreg Kroah-Hartman 	return count;
1077f196cafSChristian Engelmayer 
1087f196cafSChristian Engelmayer fail:
1097f196cafSChristian Engelmayer 	kfree(dynid);
1107f196cafSChristian Engelmayer 	return retval;
111733260ffSGreg Kroah-Hartman }
11293bacefcSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_store_new_id);
11393bacefcSGreg Kroah-Hartman 
usb_show_dynids(struct usb_dynids * dynids,char * buf)114ef206f3fSBjørn Mork ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
115e6bbcef0SBjørn Mork {
116e6bbcef0SBjørn Mork 	struct usb_dynid *dynid;
117e6bbcef0SBjørn Mork 	size_t count = 0;
118e6bbcef0SBjørn Mork 
119ef206f3fSBjørn Mork 	list_for_each_entry(dynid, &dynids->list, node)
120e6bbcef0SBjørn Mork 		if (dynid->id.bInterfaceClass != 0)
121e6bbcef0SBjørn Mork 			count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
122e6bbcef0SBjørn Mork 					   dynid->id.idVendor, dynid->id.idProduct,
123e6bbcef0SBjørn Mork 					   dynid->id.bInterfaceClass);
124e6bbcef0SBjørn Mork 		else
125e6bbcef0SBjørn Mork 			count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
126e6bbcef0SBjørn Mork 					   dynid->id.idVendor, dynid->id.idProduct);
127e6bbcef0SBjørn Mork 	return count;
128e6bbcef0SBjørn Mork }
129ef206f3fSBjørn Mork EXPORT_SYMBOL_GPL(usb_show_dynids);
130ef206f3fSBjørn Mork 
new_id_show(struct device_driver * driver,char * buf)131598d0361SGreg Kroah-Hartman static ssize_t new_id_show(struct device_driver *driver, char *buf)
132ef206f3fSBjørn Mork {
133ef206f3fSBjørn Mork 	struct usb_driver *usb_drv = to_usb_driver(driver);
134ef206f3fSBjørn Mork 
135ef206f3fSBjørn Mork 	return usb_show_dynids(&usb_drv->dynids, buf);
136ef206f3fSBjørn Mork }
137e6bbcef0SBjørn Mork 
new_id_store(struct device_driver * driver,const char * buf,size_t count)138598d0361SGreg Kroah-Hartman static ssize_t new_id_store(struct device_driver *driver,
13993bacefcSGreg Kroah-Hartman 			    const char *buf, size_t count)
14093bacefcSGreg Kroah-Hartman {
14193bacefcSGreg Kroah-Hartman 	struct usb_driver *usb_drv = to_usb_driver(driver);
14293bacefcSGreg Kroah-Hartman 
1432fc82c2dSWolfram Sang 	return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
14493bacefcSGreg Kroah-Hartman }
145598d0361SGreg Kroah-Hartman static DRIVER_ATTR_RW(new_id);
146733260ffSGreg Kroah-Hartman 
147598d0361SGreg Kroah-Hartman /*
148598d0361SGreg Kroah-Hartman  * Remove a USB device ID from this driver
1490c7a2b72SCHENG Renquan  */
remove_id_store(struct device_driver * driver,const char * buf,size_t count)150598d0361SGreg Kroah-Hartman static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
151598d0361SGreg Kroah-Hartman 			       size_t count)
1520c7a2b72SCHENG Renquan {
1530c7a2b72SCHENG Renquan 	struct usb_dynid *dynid, *n;
1540c7a2b72SCHENG Renquan 	struct usb_driver *usb_driver = to_usb_driver(driver);
155ac08de32SAlan Cox 	u32 idVendor;
156ac08de32SAlan Cox 	u32 idProduct;
157ac08de32SAlan Cox 	int fields;
1580c7a2b72SCHENG Renquan 
1590c7a2b72SCHENG Renquan 	fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
1600c7a2b72SCHENG Renquan 	if (fields < 2)
1610c7a2b72SCHENG Renquan 		return -EINVAL;
1620c7a2b72SCHENG Renquan 
1630c7a2b72SCHENG Renquan 	spin_lock(&usb_driver->dynids.lock);
1640c7a2b72SCHENG Renquan 	list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
1650c7a2b72SCHENG Renquan 		struct usb_device_id *id = &dynid->id;
16679a02744SKris Borer 
1670c7a2b72SCHENG Renquan 		if ((id->idVendor == idVendor) &&
1680c7a2b72SCHENG Renquan 		    (id->idProduct == idProduct)) {
1690c7a2b72SCHENG Renquan 			list_del(&dynid->node);
1700c7a2b72SCHENG Renquan 			kfree(dynid);
1710c7a2b72SCHENG Renquan 			break;
1720c7a2b72SCHENG Renquan 		}
1730c7a2b72SCHENG Renquan 	}
1740c7a2b72SCHENG Renquan 	spin_unlock(&usb_driver->dynids.lock);
1750c7a2b72SCHENG Renquan 	return count;
1760c7a2b72SCHENG Renquan }
177598d0361SGreg Kroah-Hartman 
remove_id_show(struct device_driver * driver,char * buf)178598d0361SGreg Kroah-Hartman static ssize_t remove_id_show(struct device_driver *driver, char *buf)
179598d0361SGreg Kroah-Hartman {
180598d0361SGreg Kroah-Hartman 	return new_id_show(driver, buf);
181598d0361SGreg Kroah-Hartman }
182598d0361SGreg Kroah-Hartman static DRIVER_ATTR_RW(remove_id);
1830c7a2b72SCHENG Renquan 
usb_create_newid_files(struct usb_driver * usb_drv)184ed283e9fSAlan Stern static int usb_create_newid_files(struct usb_driver *usb_drv)
185733260ffSGreg Kroah-Hartman {
186733260ffSGreg Kroah-Hartman 	int error = 0;
187733260ffSGreg Kroah-Hartman 
188ba9dc657SGreg Kroah-Hartman 	if (usb_drv->no_dynamic_id)
189ba9dc657SGreg Kroah-Hartman 		goto exit;
190ba9dc657SGreg Kroah-Hartman 
191ed283e9fSAlan Stern 	if (usb_drv->probe != NULL) {
19215147ffdSGreg Kroah-Hartman 		error = driver_create_file(&usb_drv->drvwrap.driver,
19315147ffdSGreg Kroah-Hartman 					   &driver_attr_new_id);
194ed283e9fSAlan Stern 		if (error == 0) {
195ed283e9fSAlan Stern 			error = driver_create_file(&usb_drv->drvwrap.driver,
196ed283e9fSAlan Stern 					&driver_attr_remove_id);
197ed283e9fSAlan Stern 			if (error)
198ed283e9fSAlan Stern 				driver_remove_file(&usb_drv->drvwrap.driver,
199ed283e9fSAlan Stern 						&driver_attr_new_id);
200ed283e9fSAlan Stern 		}
201ed283e9fSAlan Stern 	}
202ba9dc657SGreg Kroah-Hartman exit:
203733260ffSGreg Kroah-Hartman 	return error;
204733260ffSGreg Kroah-Hartman }
205733260ffSGreg Kroah-Hartman 
usb_remove_newid_files(struct usb_driver * usb_drv)206ed283e9fSAlan Stern static void usb_remove_newid_files(struct usb_driver *usb_drv)
207ba9dc657SGreg Kroah-Hartman {
208ba9dc657SGreg Kroah-Hartman 	if (usb_drv->no_dynamic_id)
209ba9dc657SGreg Kroah-Hartman 		return;
210ba9dc657SGreg Kroah-Hartman 
211ed283e9fSAlan Stern 	if (usb_drv->probe != NULL) {
212ed283e9fSAlan Stern 		driver_remove_file(&usb_drv->drvwrap.driver,
213ed283e9fSAlan Stern 				&driver_attr_remove_id);
21415147ffdSGreg Kroah-Hartman 		driver_remove_file(&usb_drv->drvwrap.driver,
21515147ffdSGreg Kroah-Hartman 				   &driver_attr_new_id);
216ba9dc657SGreg Kroah-Hartman 	}
2170c7a2b72SCHENG Renquan }
2180c7a2b72SCHENG Renquan 
usb_free_dynids(struct usb_driver * usb_drv)219733260ffSGreg Kroah-Hartman static void usb_free_dynids(struct usb_driver *usb_drv)
220733260ffSGreg Kroah-Hartman {
221733260ffSGreg Kroah-Hartman 	struct usb_dynid *dynid, *n;
222733260ffSGreg Kroah-Hartman 
223733260ffSGreg Kroah-Hartman 	spin_lock(&usb_drv->dynids.lock);
224733260ffSGreg Kroah-Hartman 	list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
225733260ffSGreg Kroah-Hartman 		list_del(&dynid->node);
226733260ffSGreg Kroah-Hartman 		kfree(dynid);
227733260ffSGreg Kroah-Hartman 	}
228733260ffSGreg Kroah-Hartman 	spin_unlock(&usb_drv->dynids.lock);
229733260ffSGreg Kroah-Hartman }
230733260ffSGreg Kroah-Hartman 
usb_match_dynamic_id(struct usb_interface * intf,struct usb_driver * drv)231733260ffSGreg Kroah-Hartman static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
232733260ffSGreg Kroah-Hartman 							struct usb_driver *drv)
233733260ffSGreg Kroah-Hartman {
234733260ffSGreg Kroah-Hartman 	struct usb_dynid *dynid;
235733260ffSGreg Kroah-Hartman 
236733260ffSGreg Kroah-Hartman 	spin_lock(&drv->dynids.lock);
237733260ffSGreg Kroah-Hartman 	list_for_each_entry(dynid, &drv->dynids.list, node) {
238733260ffSGreg Kroah-Hartman 		if (usb_match_one_id(intf, &dynid->id)) {
239733260ffSGreg Kroah-Hartman 			spin_unlock(&drv->dynids.lock);
240733260ffSGreg Kroah-Hartman 			return &dynid->id;
241733260ffSGreg Kroah-Hartman 		}
242733260ffSGreg Kroah-Hartman 	}
243733260ffSGreg Kroah-Hartman 	spin_unlock(&drv->dynids.lock);
244733260ffSGreg Kroah-Hartman 	return NULL;
245733260ffSGreg Kroah-Hartman }
246733260ffSGreg Kroah-Hartman 
247733260ffSGreg Kroah-Hartman 
2488bb54ab5SAlan Stern /* called from driver core with dev locked */
usb_probe_device(struct device * dev)2498bb54ab5SAlan Stern static int usb_probe_device(struct device *dev)
2508bb54ab5SAlan Stern {
2518bb54ab5SAlan Stern 	struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
25255129666SKay Sievers 	struct usb_device *udev = to_usb_device(dev);
2539bbdf1e0SAlan Stern 	int error = 0;
2548bb54ab5SAlan Stern 
255441b62c1SHarvey Harrison 	dev_dbg(dev, "%s\n", __func__);
2568bb54ab5SAlan Stern 
2578bb54ab5SAlan Stern 	/* TODO: Add real matching code */
2588bb54ab5SAlan Stern 
259645daaabSAlan Stern 	/* The device should always appear to be in use
26002582e9bSMasanari Iida 	 * unless the driver supports autosuspend.
261645daaabSAlan Stern 	 */
2629bbdf1e0SAlan Stern 	if (!udriver->supports_autosuspend)
2639bbdf1e0SAlan Stern 		error = usb_autoresume_device(udev);
264c9d50337SBastien Nocera 	if (error)
265c9d50337SBastien Nocera 		return error;
266645daaabSAlan Stern 
267c9d50337SBastien Nocera 	if (udriver->generic_subclass)
268c9d50337SBastien Nocera 		error = usb_generic_driver_probe(udev);
269c9d50337SBastien Nocera 	if (error)
270c9d50337SBastien Nocera 		return error;
271c9d50337SBastien Nocera 
2723fce3960SM. Vefa Bicakci 	/* Probe the USB device with the driver in hand, but only
2733fce3960SM. Vefa Bicakci 	 * defer to a generic driver in case the current USB
2743fce3960SM. Vefa Bicakci 	 * device driver has an id_table or a match function; i.e.,
2753fce3960SM. Vefa Bicakci 	 * when the device driver was explicitly matched against
2763fce3960SM. Vefa Bicakci 	 * a device.
2773fce3960SM. Vefa Bicakci 	 *
2783fce3960SM. Vefa Bicakci 	 * If the device driver does not have either of these,
2793fce3960SM. Vefa Bicakci 	 * then we assume that it can bind to any device and is
2803fce3960SM. Vefa Bicakci 	 * not truly a more specialized/non-generic driver, so a
2813fce3960SM. Vefa Bicakci 	 * return value of -ENODEV should not force the device
2823fce3960SM. Vefa Bicakci 	 * to be handled by the generic USB driver, as there
2833fce3960SM. Vefa Bicakci 	 * can still be another, more specialized, device driver.
2843fce3960SM. Vefa Bicakci 	 *
2853fce3960SM. Vefa Bicakci 	 * This accommodates the usbip driver.
2863fce3960SM. Vefa Bicakci 	 *
2873fce3960SM. Vefa Bicakci 	 * TODO: What if, in the future, there are multiple
2883fce3960SM. Vefa Bicakci 	 * specialized USB device drivers for a particular device?
2893fce3960SM. Vefa Bicakci 	 * In such cases, there is a need to try all matching
2903fce3960SM. Vefa Bicakci 	 * specialised device drivers prior to setting the
2913fce3960SM. Vefa Bicakci 	 * use_generic_driver bit.
2923fce3960SM. Vefa Bicakci 	 */
2938bb54ab5SAlan Stern 	error = udriver->probe(udev);
2943fce3960SM. Vefa Bicakci 	if (error == -ENODEV && udriver != &usb_generic_driver &&
2953fce3960SM. Vefa Bicakci 	    (udriver->id_table || udriver->match)) {
29677419aa4SBastien Nocera 		udev->use_generic_driver = 1;
29777419aa4SBastien Nocera 		return -EPROBE_DEFER;
29877419aa4SBastien Nocera 	}
2998bb54ab5SAlan Stern 	return error;
3008bb54ab5SAlan Stern }
3018bb54ab5SAlan Stern 
3028bb54ab5SAlan Stern /* called from driver core with dev locked */
usb_unbind_device(struct device * dev)3038bb54ab5SAlan Stern static int usb_unbind_device(struct device *dev)
3048bb54ab5SAlan Stern {
3059bbdf1e0SAlan Stern 	struct usb_device *udev = to_usb_device(dev);
3068bb54ab5SAlan Stern 	struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
3078bb54ab5SAlan Stern 
308c9d50337SBastien Nocera 	if (udriver->disconnect)
3099bbdf1e0SAlan Stern 		udriver->disconnect(udev);
310c9d50337SBastien Nocera 	if (udriver->generic_subclass)
311c9d50337SBastien Nocera 		usb_generic_driver_disconnect(udev);
3129bbdf1e0SAlan Stern 	if (!udriver->supports_autosuspend)
3139bbdf1e0SAlan Stern 		usb_autosuspend_device(udev);
3148bb54ab5SAlan Stern 	return 0;
3158bb54ab5SAlan Stern }
3168bb54ab5SAlan Stern 
3178bb54ab5SAlan Stern /* called from driver core with dev locked */
usb_probe_interface(struct device * dev)318ddae41beSGreg Kroah-Hartman static int usb_probe_interface(struct device *dev)
319ddae41beSGreg Kroah-Hartman {
320ddae41beSGreg Kroah-Hartman 	struct usb_driver *driver = to_usb_driver(dev->driver);
32155129666SKay Sievers 	struct usb_interface *intf = to_usb_interface(dev);
32255129666SKay Sievers 	struct usb_device *udev = interface_to_usbdev(intf);
323ddae41beSGreg Kroah-Hartman 	const struct usb_device_id *id;
324ddae41beSGreg Kroah-Hartman 	int error = -ENODEV;
3256fb650d4SAlan Stern 	int lpm_disable_error = -ENODEV;
326ddae41beSGreg Kroah-Hartman 
327441b62c1SHarvey Harrison 	dev_dbg(dev, "%s\n", __func__);
328ddae41beSGreg Kroah-Hartman 
32978d9a487SAlan Stern 	intf->needs_binding = 0;
330ddae41beSGreg Kroah-Hartman 
3317cbe5dcaSAlan Stern 	if (usb_device_is_owned(udev))
3320f3dda9fSAlan Stern 		return error;
3337cbe5dcaSAlan Stern 
33472230abbSInaky Perez-Gonzalez 	if (udev->authorized == 0) {
33572230abbSInaky Perez-Gonzalez 		dev_err(&intf->dev, "Device is not authorized for usage\n");
3360f3dda9fSAlan Stern 		return error;
3378d1f8573SStefan Koch 	} else if (intf->authorized == 0) {
3388d1f8573SStefan Koch 		dev_err(&intf->dev, "Interface %d is not authorized for usage\n",
3398d1f8573SStefan Koch 				intf->altsetting->desc.bInterfaceNumber);
3408d1f8573SStefan Koch 		return error;
34172230abbSInaky Perez-Gonzalez 	}
34272230abbSInaky Perez-Gonzalez 
343733260ffSGreg Kroah-Hartman 	id = usb_match_dynamic_id(intf, driver);
3440f3dda9fSAlan Stern 	if (!id)
34531c6bf70SBjørn Mork 		id = usb_match_id(intf, driver->id_table);
34631c6bf70SBjørn Mork 	if (!id)
3470f3dda9fSAlan Stern 		return error;
3480f3dda9fSAlan Stern 
349441b62c1SHarvey Harrison 	dev_dbg(dev, "%s - got id\n", __func__);
350ddae41beSGreg Kroah-Hartman 
35194fcda1fSAlan Stern 	error = usb_autoresume_device(udev);
352645daaabSAlan Stern 	if (error)
353645daaabSAlan Stern 		return error;
354645daaabSAlan Stern 
355ddae41beSGreg Kroah-Hartman 	intf->condition = USB_INTERFACE_BINDING;
356645daaabSAlan Stern 
357571dc79dSAlan Stern 	/* Probed interfaces are initially active.  They are
3589bbdf1e0SAlan Stern 	 * runtime-PM-enabled only if the driver has autosuspend support.
3599bbdf1e0SAlan Stern 	 * They are sensitive to their children's power states.
360645daaabSAlan Stern 	 */
3619bbdf1e0SAlan Stern 	pm_runtime_set_active(dev);
3629bbdf1e0SAlan Stern 	pm_suspend_ignore_children(dev, false);
3639bbdf1e0SAlan Stern 	if (driver->supports_autosuspend)
3649bbdf1e0SAlan Stern 		pm_runtime_enable(dev);
365645daaabSAlan Stern 
3668306095fSSarah Sharp 	/* If the new driver doesn't allow hub-initiated LPM, and we can't
3678306095fSSarah Sharp 	 * disable hub-initiated LPM, then fail the probe.
3688306095fSSarah Sharp 	 *
3698306095fSSarah Sharp 	 * Otherwise, leaving LPM enabled should be harmless, because the
3708306095fSSarah Sharp 	 * endpoint intervals should remain the same, and the U1/U2 timeouts
3718306095fSSarah Sharp 	 * should remain the same.
3728306095fSSarah Sharp 	 *
3738306095fSSarah Sharp 	 * If we need to install alt setting 0 before probe, or another alt
3748306095fSSarah Sharp 	 * setting during probe, that should also be fine.  usb_set_interface()
3758306095fSSarah Sharp 	 * will attempt to disable LPM, and fail if it can't disable it.
3768306095fSSarah Sharp 	 */
3776fb650d4SAlan Stern 	if (driver->disable_hub_initiated_lpm) {
3788306095fSSarah Sharp 		lpm_disable_error = usb_unlocked_disable_lpm(udev);
3796fb650d4SAlan Stern 		if (lpm_disable_error) {
3801ccc417eSJoe Perches 			dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n",
3818306095fSSarah Sharp 				__func__, driver->name);
3828306095fSSarah Sharp 			error = lpm_disable_error;
3838306095fSSarah Sharp 			goto err;
3848306095fSSarah Sharp 		}
3856fb650d4SAlan Stern 	}
3868306095fSSarah Sharp 
38755151d7dSAlan Stern 	/* Carry out a deferred switch to altsetting 0 */
38855151d7dSAlan Stern 	if (intf->needs_altsetting0) {
3891e5ea5e3SOliver Neukum 		error = usb_set_interface(udev, intf->altsetting[0].
39055151d7dSAlan Stern 				desc.bInterfaceNumber, 0);
3911e5ea5e3SOliver Neukum 		if (error < 0)
3921e5ea5e3SOliver Neukum 			goto err;
39355151d7dSAlan Stern 		intf->needs_altsetting0 = 0;
39455151d7dSAlan Stern 	}
39555151d7dSAlan Stern 
396ddae41beSGreg Kroah-Hartman 	error = driver->probe(intf, id);
3971e5ea5e3SOliver Neukum 	if (error)
3981e5ea5e3SOliver Neukum 		goto err;
3991e5ea5e3SOliver Neukum 
4001e5ea5e3SOliver Neukum 	intf->condition = USB_INTERFACE_BOUND;
4018306095fSSarah Sharp 
4028306095fSSarah Sharp 	/* If the LPM disable succeeded, balance the ref counts. */
4038306095fSSarah Sharp 	if (!lpm_disable_error)
4048306095fSSarah Sharp 		usb_unlocked_enable_lpm(udev);
4058306095fSSarah Sharp 
4061e5ea5e3SOliver Neukum 	usb_autosuspend_device(udev);
4071e5ea5e3SOliver Neukum 	return error;
4081e5ea5e3SOliver Neukum 
4091e5ea5e3SOliver Neukum  err:
410e714fad0SHans de Goede 	usb_set_intfdata(intf, NULL);
411645daaabSAlan Stern 	intf->needs_remote_wakeup = 0;
412ddae41beSGreg Kroah-Hartman 	intf->condition = USB_INTERFACE_UNBOUND;
4139bbdf1e0SAlan Stern 
414d01f87c0SSarah Sharp 	/* If the LPM disable succeeded, balance the ref counts. */
415d01f87c0SSarah Sharp 	if (!lpm_disable_error)
416d01f87c0SSarah Sharp 		usb_unlocked_enable_lpm(udev);
417d01f87c0SSarah Sharp 
4189bbdf1e0SAlan Stern 	/* Unbound interfaces are always runtime-PM-disabled and -suspended */
41989842ae6SAlan Stern 	if (driver->supports_autosuspend)
4209bbdf1e0SAlan Stern 		pm_runtime_disable(dev);
4219bbdf1e0SAlan Stern 	pm_runtime_set_suspended(dev);
4229bbdf1e0SAlan Stern 
42394fcda1fSAlan Stern 	usb_autosuspend_device(udev);
424ddae41beSGreg Kroah-Hartman 	return error;
425ddae41beSGreg Kroah-Hartman }
426ddae41beSGreg Kroah-Hartman 
4278bb54ab5SAlan Stern /* called from driver core with dev locked */
usb_unbind_interface(struct device * dev)428ddae41beSGreg Kroah-Hartman static int usb_unbind_interface(struct device *dev)
429ddae41beSGreg Kroah-Hartman {
4308bb54ab5SAlan Stern 	struct usb_driver *driver = to_usb_driver(dev->driver);
431ddae41beSGreg Kroah-Hartman 	struct usb_interface *intf = to_usb_interface(dev);
4326343e8bfSHans de Goede 	struct usb_host_endpoint *ep, **eps = NULL;
433645daaabSAlan Stern 	struct usb_device *udev;
4346fb650d4SAlan Stern 	int i, j, error, r;
4356fb650d4SAlan Stern 	int lpm_disable_error = -ENODEV;
436ddae41beSGreg Kroah-Hartman 
437ddae41beSGreg Kroah-Hartman 	intf->condition = USB_INTERFACE_UNBINDING;
438ddae41beSGreg Kroah-Hartman 
439645daaabSAlan Stern 	/* Autoresume for set_interface call below */
440645daaabSAlan Stern 	udev = interface_to_usbdev(intf);
44194fcda1fSAlan Stern 	error = usb_autoresume_device(udev);
442645daaabSAlan Stern 
4436fb650d4SAlan Stern 	/* If hub-initiated LPM policy may change, attempt to disable LPM until
4448306095fSSarah Sharp 	 * the driver is unbound.  If LPM isn't disabled, that's fine because it
4458306095fSSarah Sharp 	 * wouldn't be enabled unless all the bound interfaces supported
4468306095fSSarah Sharp 	 * hub-initiated LPM.
4478306095fSSarah Sharp 	 */
4486fb650d4SAlan Stern 	if (driver->disable_hub_initiated_lpm)
4498306095fSSarah Sharp 		lpm_disable_error = usb_unlocked_disable_lpm(udev);
4508306095fSSarah Sharp 
4511299cff9SAlan Stern 	/*
4521299cff9SAlan Stern 	 * Terminate all URBs for this interface unless the driver
4531299cff9SAlan Stern 	 * supports "soft" unbinding and the device is still present.
4549da82bd4SAlan Stern 	 */
4551299cff9SAlan Stern 	if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
456ddeac4e7SAlan Stern 		usb_disable_interface(udev, intf, false);
457ddae41beSGreg Kroah-Hartman 
458ddae41beSGreg Kroah-Hartman 	driver->disconnect(intf);
459ddae41beSGreg Kroah-Hartman 
4606343e8bfSHans de Goede 	/* Free streams */
4616343e8bfSHans de Goede 	for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4626343e8bfSHans de Goede 		ep = &intf->cur_altsetting->endpoint[i];
4636343e8bfSHans de Goede 		if (ep->streams == 0)
4646343e8bfSHans de Goede 			continue;
4656343e8bfSHans de Goede 		if (j == 0) {
4669766f251SMuhammad Falak R Wani 			eps = kmalloc_array(USB_MAXENDPOINTS, sizeof(void *),
4676343e8bfSHans de Goede 				      GFP_KERNEL);
4689766f251SMuhammad Falak R Wani 			if (!eps)
4696343e8bfSHans de Goede 				break;
4706343e8bfSHans de Goede 		}
4716343e8bfSHans de Goede 		eps[j++] = ep;
4726343e8bfSHans de Goede 	}
4736343e8bfSHans de Goede 	if (j) {
4746343e8bfSHans de Goede 		usb_free_streams(intf, eps, j, GFP_KERNEL);
4756343e8bfSHans de Goede 		kfree(eps);
4766343e8bfSHans de Goede 	}
4776343e8bfSHans de Goede 
47855151d7dSAlan Stern 	/* Reset other interface state.
47955151d7dSAlan Stern 	 * We cannot do a Set-Interface if the device is suspended or
48055151d7dSAlan Stern 	 * if it is prepared for a system sleep (since installing a new
48155151d7dSAlan Stern 	 * altsetting means creating new endpoint device entries).
48255151d7dSAlan Stern 	 * When either of these happens, defer the Set-Interface.
48355151d7dSAlan Stern 	 */
4842caf7fcdSAlan Stern 	if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
4852caf7fcdSAlan Stern 		/* Already in altsetting 0 so skip Set-Interface.
4862caf7fcdSAlan Stern 		 * Just re-enable it without affecting the endpoint toggles.
4872caf7fcdSAlan Stern 		 */
4882caf7fcdSAlan Stern 		usb_enable_interface(udev, intf, false);
489f76b168bSAlan Stern 	} else if (!error && !intf->dev.power.is_prepared) {
4901e5ea5e3SOliver Neukum 		r = usb_set_interface(udev, intf->altsetting[0].
49155151d7dSAlan Stern 				desc.bInterfaceNumber, 0);
4921e5ea5e3SOliver Neukum 		if (r < 0)
49355151d7dSAlan Stern 			intf->needs_altsetting0 = 1;
4941e5ea5e3SOliver Neukum 	} else {
4951e5ea5e3SOliver Neukum 		intf->needs_altsetting0 = 1;
4961e5ea5e3SOliver Neukum 	}
497ddae41beSGreg Kroah-Hartman 	usb_set_intfdata(intf, NULL);
498645daaabSAlan Stern 
499ddae41beSGreg Kroah-Hartman 	intf->condition = USB_INTERFACE_UNBOUND;
500645daaabSAlan Stern 	intf->needs_remote_wakeup = 0;
501645daaabSAlan Stern 
5028306095fSSarah Sharp 	/* Attempt to re-enable USB3 LPM, if the disable succeeded. */
5038306095fSSarah Sharp 	if (!lpm_disable_error)
5048306095fSSarah Sharp 		usb_unlocked_enable_lpm(udev);
5058306095fSSarah Sharp 
5069bbdf1e0SAlan Stern 	/* Unbound interfaces are always runtime-PM-disabled and -suspended */
50789842ae6SAlan Stern 	if (driver->supports_autosuspend)
5089bbdf1e0SAlan Stern 		pm_runtime_disable(dev);
5099bbdf1e0SAlan Stern 	pm_runtime_set_suspended(dev);
5109bbdf1e0SAlan Stern 
511645daaabSAlan Stern 	if (!error)
51294fcda1fSAlan Stern 		usb_autosuspend_device(udev);
513ddae41beSGreg Kroah-Hartman 
514ddae41beSGreg Kroah-Hartman 	return 0;
515ddae41beSGreg Kroah-Hartman }
516ddae41beSGreg Kroah-Hartman 
51736e56a34SAlan Stern /**
51836e56a34SAlan Stern  * usb_driver_claim_interface - bind a driver to an interface
51936e56a34SAlan Stern  * @driver: the driver to be bound
52036e56a34SAlan Stern  * @iface: the interface to which it will be bound; must be in the
52136e56a34SAlan Stern  *	usb device's active configuration
522aaadc6aeSJohan Hovold  * @data: driver data associated with that interface
52336e56a34SAlan Stern  *
52436e56a34SAlan Stern  * This is used by usb device drivers that need to claim more than one
52536e56a34SAlan Stern  * interface on a device when probing (audio and acm are current examples).
52636e56a34SAlan Stern  * No device driver should directly modify internal usb_interface or
52736e56a34SAlan Stern  * usb_device structure members.
52836e56a34SAlan Stern  *
529341487a8SGreg Kroah-Hartman  * Callers must own the device lock, so driver probe() entries don't need
530341487a8SGreg Kroah-Hartman  * extra locking, but other call contexts may need to explicitly claim that
531341487a8SGreg Kroah-Hartman  * lock.
532626f090cSYacine Belkadi  *
533626f090cSYacine Belkadi  * Return: 0 on success.
53436e56a34SAlan Stern  */
usb_driver_claim_interface(struct usb_driver * driver,struct usb_interface * iface,void * data)53536e56a34SAlan Stern int usb_driver_claim_interface(struct usb_driver *driver,
536aaadc6aeSJohan Hovold 				struct usb_interface *iface, void *data)
53736e56a34SAlan Stern {
5380b818e39SOliver Neukum 	struct device *dev;
5391b21d5e1SGreg Kroah-Hartman 	int retval = 0;
54036e56a34SAlan Stern 
5410b818e39SOliver Neukum 	if (!iface)
5420b818e39SOliver Neukum 		return -ENODEV;
5430b818e39SOliver Neukum 
5440b818e39SOliver Neukum 	dev = &iface->dev;
54536e56a34SAlan Stern 	if (dev->driver)
54636e56a34SAlan Stern 		return -EBUSY;
54736e56a34SAlan Stern 
5488d1f8573SStefan Koch 	/* reject claim if interface is not authorized */
5498d1f8573SStefan Koch 	if (!iface->authorized)
5508d1f8573SStefan Koch 		return -ENODEV;
5518d1f8573SStefan Koch 
5528bb54ab5SAlan Stern 	dev->driver = &driver->drvwrap.driver;
553aaadc6aeSJohan Hovold 	usb_set_intfdata(iface, data);
55478d9a487SAlan Stern 	iface->needs_binding = 0;
555645daaabSAlan Stern 
55636e56a34SAlan Stern 	iface->condition = USB_INTERFACE_BOUND;
5579bbdf1e0SAlan Stern 
55889842ae6SAlan Stern 	/* Claimed interfaces are initially inactive (suspended) and
55989842ae6SAlan Stern 	 * runtime-PM-enabled, but only if the driver has autosuspend
56089842ae6SAlan Stern 	 * support.  Otherwise they are marked active, to prevent the
56189842ae6SAlan Stern 	 * device from being autosuspended, but left disabled.  In either
56289842ae6SAlan Stern 	 * case they are sensitive to their children's power states.
5639bbdf1e0SAlan Stern 	 */
5649bbdf1e0SAlan Stern 	pm_suspend_ignore_children(dev, false);
5659bbdf1e0SAlan Stern 	if (driver->supports_autosuspend)
5669bbdf1e0SAlan Stern 		pm_runtime_enable(dev);
56789842ae6SAlan Stern 	else
56889842ae6SAlan Stern 		pm_runtime_set_active(dev);
56936e56a34SAlan Stern 
57036e56a34SAlan Stern 	/* if interface was already added, bind now; else let
57136e56a34SAlan Stern 	 * the future device_add() bind it, bypassing probe()
57236e56a34SAlan Stern 	 */
57336e56a34SAlan Stern 	if (device_is_registered(dev))
5741b21d5e1SGreg Kroah-Hartman 		retval = device_bind_driver(dev);
57536e56a34SAlan Stern 
576bd729f9dSAlan Stern 	if (retval) {
577bd729f9dSAlan Stern 		dev->driver = NULL;
578bd729f9dSAlan Stern 		usb_set_intfdata(iface, NULL);
579bd729f9dSAlan Stern 		iface->needs_remote_wakeup = 0;
580bd729f9dSAlan Stern 		iface->condition = USB_INTERFACE_UNBOUND;
581bd729f9dSAlan Stern 
582bd729f9dSAlan Stern 		/*
583bd729f9dSAlan Stern 		 * Unbound interfaces are always runtime-PM-disabled
584bd729f9dSAlan Stern 		 * and runtime-PM-suspended
585bd729f9dSAlan Stern 		 */
586bd729f9dSAlan Stern 		if (driver->supports_autosuspend)
587bd729f9dSAlan Stern 			pm_runtime_disable(dev);
588bd729f9dSAlan Stern 		pm_runtime_set_suspended(dev);
589bd729f9dSAlan Stern 	}
590bd729f9dSAlan Stern 
5911b21d5e1SGreg Kroah-Hartman 	return retval;
59236e56a34SAlan Stern }
593782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
59436e56a34SAlan Stern 
59536e56a34SAlan Stern /**
59636e56a34SAlan Stern  * usb_driver_release_interface - unbind a driver from an interface
59736e56a34SAlan Stern  * @driver: the driver to be unbound
59836e56a34SAlan Stern  * @iface: the interface from which it will be unbound
59936e56a34SAlan Stern  *
60036e56a34SAlan Stern  * This can be used by drivers to release an interface without waiting
60136e56a34SAlan Stern  * for their disconnect() methods to be called.  In typical cases this
60236e56a34SAlan Stern  * also causes the driver disconnect() method to be called.
60336e56a34SAlan Stern  *
60436e56a34SAlan Stern  * This call is synchronous, and may not be used in an interrupt context.
605341487a8SGreg Kroah-Hartman  * Callers must own the device lock, so driver disconnect() entries don't
606341487a8SGreg Kroah-Hartman  * need extra locking, but other call contexts may need to explicitly claim
607341487a8SGreg Kroah-Hartman  * that lock.
60836e56a34SAlan Stern  */
usb_driver_release_interface(struct usb_driver * driver,struct usb_interface * iface)60936e56a34SAlan Stern void usb_driver_release_interface(struct usb_driver *driver,
61036e56a34SAlan Stern 					struct usb_interface *iface)
61136e56a34SAlan Stern {
61236e56a34SAlan Stern 	struct device *dev = &iface->dev;
61336e56a34SAlan Stern 
61436e56a34SAlan Stern 	/* this should never happen, don't release something that's not ours */
6158bb54ab5SAlan Stern 	if (!dev->driver || dev->driver != &driver->drvwrap.driver)
61636e56a34SAlan Stern 		return;
61736e56a34SAlan Stern 
61836e56a34SAlan Stern 	/* don't release from within disconnect() */
61936e56a34SAlan Stern 	if (iface->condition != USB_INTERFACE_BOUND)
62036e56a34SAlan Stern 		return;
62136e56a34SAlan Stern 	iface->condition = USB_INTERFACE_UNBINDING;
62291f8d063SAlan Stern 
62391f8d063SAlan Stern 	/* Release via the driver core only if the interface
62491f8d063SAlan Stern 	 * has already been registered
62591f8d063SAlan Stern 	 */
62691f8d063SAlan Stern 	if (device_is_registered(dev)) {
62736e56a34SAlan Stern 		device_release_driver(dev);
628dc023dceSInaky Perez-Gonzalez 	} else {
6298e9394ceSGreg Kroah-Hartman 		device_lock(dev);
63091f8d063SAlan Stern 		usb_unbind_interface(dev);
63136e56a34SAlan Stern 		dev->driver = NULL;
6328e9394ceSGreg Kroah-Hartman 		device_unlock(dev);
63391f8d063SAlan Stern 	}
63436e56a34SAlan Stern }
635782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_driver_release_interface);
63636e56a34SAlan Stern 
637733260ffSGreg Kroah-Hartman /* returns 0 if no match, 1 if match */
usb_match_device(struct usb_device * dev,const struct usb_device_id * id)638bb417020SGreg Kroah-Hartman int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
639733260ffSGreg Kroah-Hartman {
640733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
641733260ffSGreg Kroah-Hartman 	    id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
642733260ffSGreg Kroah-Hartman 		return 0;
643733260ffSGreg Kroah-Hartman 
644733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
645733260ffSGreg Kroah-Hartman 	    id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
646733260ffSGreg Kroah-Hartman 		return 0;
647733260ffSGreg Kroah-Hartman 
648733260ffSGreg Kroah-Hartman 	/* No need to test id->bcdDevice_lo != 0, since 0 is never
649733260ffSGreg Kroah-Hartman 	   greater than any unsigned number. */
650733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
651733260ffSGreg Kroah-Hartman 	    (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
652733260ffSGreg Kroah-Hartman 		return 0;
653733260ffSGreg Kroah-Hartman 
654733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
655733260ffSGreg Kroah-Hartman 	    (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
656733260ffSGreg Kroah-Hartman 		return 0;
657733260ffSGreg Kroah-Hartman 
658733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
659733260ffSGreg Kroah-Hartman 	    (id->bDeviceClass != dev->descriptor.bDeviceClass))
660733260ffSGreg Kroah-Hartman 		return 0;
661733260ffSGreg Kroah-Hartman 
662733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
663733260ffSGreg Kroah-Hartman 	    (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
664733260ffSGreg Kroah-Hartman 		return 0;
665733260ffSGreg Kroah-Hartman 
666733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
667733260ffSGreg Kroah-Hartman 	    (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
668733260ffSGreg Kroah-Hartman 		return 0;
669733260ffSGreg Kroah-Hartman 
670bb417020SGreg Kroah-Hartman 	return 1;
671bb417020SGreg Kroah-Hartman }
672bb417020SGreg Kroah-Hartman 
673bb417020SGreg Kroah-Hartman /* returns 0 if no match, 1 if match */
usb_match_one_id_intf(struct usb_device * dev,struct usb_host_interface * intf,const struct usb_device_id * id)67480da2e0dSLaurent Pinchart int usb_match_one_id_intf(struct usb_device *dev,
67580da2e0dSLaurent Pinchart 			  struct usb_host_interface *intf,
676bb417020SGreg Kroah-Hartman 			  const struct usb_device_id *id)
677bb417020SGreg Kroah-Hartman {
67881df2d59SBjørn Mork 	/* The interface class, subclass, protocol and number should never be
67993c8bf45SAlan Stern 	 * checked for a match if the device class is Vendor Specific,
68093c8bf45SAlan Stern 	 * unless the match record specifies the Vendor ID. */
68193c8bf45SAlan Stern 	if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
68293c8bf45SAlan Stern 			!(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
68393c8bf45SAlan Stern 			(id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
68493c8bf45SAlan Stern 				USB_DEVICE_ID_MATCH_INT_SUBCLASS |
68581df2d59SBjørn Mork 				USB_DEVICE_ID_MATCH_INT_PROTOCOL |
68681df2d59SBjørn Mork 				USB_DEVICE_ID_MATCH_INT_NUMBER)))
68793c8bf45SAlan Stern 		return 0;
68893c8bf45SAlan Stern 
689733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
690733260ffSGreg Kroah-Hartman 	    (id->bInterfaceClass != intf->desc.bInterfaceClass))
691733260ffSGreg Kroah-Hartman 		return 0;
692733260ffSGreg Kroah-Hartman 
693733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
694733260ffSGreg Kroah-Hartman 	    (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
695733260ffSGreg Kroah-Hartman 		return 0;
696733260ffSGreg Kroah-Hartman 
697733260ffSGreg Kroah-Hartman 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
698733260ffSGreg Kroah-Hartman 	    (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
699733260ffSGreg Kroah-Hartman 		return 0;
700733260ffSGreg Kroah-Hartman 
70181df2d59SBjørn Mork 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
70281df2d59SBjørn Mork 	    (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
70381df2d59SBjørn Mork 		return 0;
70481df2d59SBjørn Mork 
705733260ffSGreg Kroah-Hartman 	return 1;
706733260ffSGreg Kroah-Hartman }
70780da2e0dSLaurent Pinchart 
70880da2e0dSLaurent Pinchart /* returns 0 if no match, 1 if match */
usb_match_one_id(struct usb_interface * interface,const struct usb_device_id * id)70980da2e0dSLaurent Pinchart int usb_match_one_id(struct usb_interface *interface,
71080da2e0dSLaurent Pinchart 		     const struct usb_device_id *id)
71180da2e0dSLaurent Pinchart {
71280da2e0dSLaurent Pinchart 	struct usb_host_interface *intf;
71380da2e0dSLaurent Pinchart 	struct usb_device *dev;
71480da2e0dSLaurent Pinchart 
71580da2e0dSLaurent Pinchart 	/* proc_connectinfo in devio.c may call us with id == NULL. */
71680da2e0dSLaurent Pinchart 	if (id == NULL)
71780da2e0dSLaurent Pinchart 		return 0;
71880da2e0dSLaurent Pinchart 
71980da2e0dSLaurent Pinchart 	intf = interface->cur_altsetting;
72080da2e0dSLaurent Pinchart 	dev = interface_to_usbdev(interface);
72180da2e0dSLaurent Pinchart 
72280da2e0dSLaurent Pinchart 	if (!usb_match_device(dev, id))
72380da2e0dSLaurent Pinchart 		return 0;
72480da2e0dSLaurent Pinchart 
72580da2e0dSLaurent Pinchart 	return usb_match_one_id_intf(dev, intf, id);
72680da2e0dSLaurent Pinchart }
72793bacefcSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_match_one_id);
72893bacefcSGreg Kroah-Hartman 
729ddae41beSGreg Kroah-Hartman /**
730ddae41beSGreg Kroah-Hartman  * usb_match_id - find first usb_device_id matching device or interface
731ddae41beSGreg Kroah-Hartman  * @interface: the interface of interest
732ddae41beSGreg Kroah-Hartman  * @id: array of usb_device_id structures, terminated by zero entry
733ddae41beSGreg Kroah-Hartman  *
734ddae41beSGreg Kroah-Hartman  * usb_match_id searches an array of usb_device_id's and returns
735ddae41beSGreg Kroah-Hartman  * the first one matching the device or interface, or null.
736ddae41beSGreg Kroah-Hartman  * This is used when binding (or rebinding) a driver to an interface.
737ddae41beSGreg Kroah-Hartman  * Most USB device drivers will use this indirectly, through the usb core,
738ddae41beSGreg Kroah-Hartman  * but some layered driver frameworks use it directly.
739ddae41beSGreg Kroah-Hartman  * These device tables are exported with MODULE_DEVICE_TABLE, through
740ddae41beSGreg Kroah-Hartman  * modutils, to support the driver loading functionality of USB hotplugging.
741ddae41beSGreg Kroah-Hartman  *
742626f090cSYacine Belkadi  * Return: The first matching usb_device_id, or %NULL.
743626f090cSYacine Belkadi  *
744ddae41beSGreg Kroah-Hartman  * What Matches:
745ddae41beSGreg Kroah-Hartman  *
746ddae41beSGreg Kroah-Hartman  * The "match_flags" element in a usb_device_id controls which
747ddae41beSGreg Kroah-Hartman  * members are used.  If the corresponding bit is set, the
748ddae41beSGreg Kroah-Hartman  * value in the device_id must match its corresponding member
749ddae41beSGreg Kroah-Hartman  * in the device or interface descriptor, or else the device_id
750ddae41beSGreg Kroah-Hartman  * does not match.
751ddae41beSGreg Kroah-Hartman  *
752ddae41beSGreg Kroah-Hartman  * "driver_info" is normally used only by device drivers,
753ddae41beSGreg Kroah-Hartman  * but you can create a wildcard "matches anything" usb_device_id
754ddae41beSGreg Kroah-Hartman  * as a driver's "modules.usbmap" entry if you provide an id with
755ddae41beSGreg Kroah-Hartman  * only a nonzero "driver_info" field.  If you do this, the USB device
756ddae41beSGreg Kroah-Hartman  * driver's probe() routine should use additional intelligence to
757ddae41beSGreg Kroah-Hartman  * decide whether to bind to the specified interface.
758ddae41beSGreg Kroah-Hartman  *
759ddae41beSGreg Kroah-Hartman  * What Makes Good usb_device_id Tables:
760ddae41beSGreg Kroah-Hartman  *
761ddae41beSGreg Kroah-Hartman  * The match algorithm is very simple, so that intelligence in
762ddae41beSGreg Kroah-Hartman  * driver selection must come from smart driver id records.
763ddae41beSGreg Kroah-Hartman  * Unless you have good reasons to use another selection policy,
764ddae41beSGreg Kroah-Hartman  * provide match elements only in related groups, and order match
765ddae41beSGreg Kroah-Hartman  * specifiers from specific to general.  Use the macros provided
766ddae41beSGreg Kroah-Hartman  * for that purpose if you can.
767ddae41beSGreg Kroah-Hartman  *
768ddae41beSGreg Kroah-Hartman  * The most specific match specifiers use device descriptor
769ddae41beSGreg Kroah-Hartman  * data.  These are commonly used with product-specific matches;
770ddae41beSGreg Kroah-Hartman  * the USB_DEVICE macro lets you provide vendor and product IDs,
771ddae41beSGreg Kroah-Hartman  * and you can also match against ranges of product revisions.
772ddae41beSGreg Kroah-Hartman  * These are widely used for devices with application or vendor
773ddae41beSGreg Kroah-Hartman  * specific bDeviceClass values.
774ddae41beSGreg Kroah-Hartman  *
775ddae41beSGreg Kroah-Hartman  * Matches based on device class/subclass/protocol specifications
776ddae41beSGreg Kroah-Hartman  * are slightly more general; use the USB_DEVICE_INFO macro, or
777ddae41beSGreg Kroah-Hartman  * its siblings.  These are used with single-function devices
778ddae41beSGreg Kroah-Hartman  * where bDeviceClass doesn't specify that each interface has
779ddae41beSGreg Kroah-Hartman  * its own class.
780ddae41beSGreg Kroah-Hartman  *
781ddae41beSGreg Kroah-Hartman  * Matches based on interface class/subclass/protocol are the
782ddae41beSGreg Kroah-Hartman  * most general; they let drivers bind to any interface on a
783ddae41beSGreg Kroah-Hartman  * multiple-function device.  Use the USB_INTERFACE_INFO
784ddae41beSGreg Kroah-Hartman  * macro, or its siblings, to match class-per-interface style
78593c8bf45SAlan Stern  * devices (as recorded in bInterfaceClass).
78693c8bf45SAlan Stern  *
78793c8bf45SAlan Stern  * Note that an entry created by USB_INTERFACE_INFO won't match
78893c8bf45SAlan Stern  * any interface if the device class is set to Vendor-Specific.
78993c8bf45SAlan Stern  * This is deliberate; according to the USB spec the meanings of
79093c8bf45SAlan Stern  * the interface class/subclass/protocol for these devices are also
79193c8bf45SAlan Stern  * vendor-specific, and hence matching against a standard product
79293c8bf45SAlan Stern  * class wouldn't work anyway.  If you really want to use an
79393c8bf45SAlan Stern  * interface-based match for such a device, create a match record
79493c8bf45SAlan Stern  * that also specifies the vendor ID.  (Unforunately there isn't a
79593c8bf45SAlan Stern  * standard macro for creating records like this.)
796ddae41beSGreg Kroah-Hartman  *
797ddae41beSGreg Kroah-Hartman  * Within those groups, remember that not all combinations are
798ddae41beSGreg Kroah-Hartman  * meaningful.  For example, don't give a product version range
799ddae41beSGreg Kroah-Hartman  * without vendor and product IDs; or specify a protocol without
800ddae41beSGreg Kroah-Hartman  * its associated class and subclass.
801ddae41beSGreg Kroah-Hartman  */
usb_match_id(struct usb_interface * interface,const struct usb_device_id * id)802ddae41beSGreg Kroah-Hartman const struct usb_device_id *usb_match_id(struct usb_interface *interface,
803ddae41beSGreg Kroah-Hartman 					 const struct usb_device_id *id)
804ddae41beSGreg Kroah-Hartman {
805ddae41beSGreg Kroah-Hartman 	/* proc_connectinfo in devio.c may call us with id == NULL. */
806ddae41beSGreg Kroah-Hartman 	if (id == NULL)
807ddae41beSGreg Kroah-Hartman 		return NULL;
808ddae41beSGreg Kroah-Hartman 
809ddae41beSGreg Kroah-Hartman 	/* It is important to check that id->driver_info is nonzero,
810ddae41beSGreg Kroah-Hartman 	   since an entry that is all zeroes except for a nonzero
811ddae41beSGreg Kroah-Hartman 	   id->driver_info is the way to create an entry that
812ddae41beSGreg Kroah-Hartman 	   indicates that the driver want to examine every
813ddae41beSGreg Kroah-Hartman 	   device and interface. */
814de6f92b9SGreg Kroah-Hartman 	for (; id->idVendor || id->idProduct || id->bDeviceClass ||
815de6f92b9SGreg Kroah-Hartman 	       id->bInterfaceClass || id->driver_info; id++) {
816733260ffSGreg Kroah-Hartman 		if (usb_match_one_id(interface, id))
817ddae41beSGreg Kroah-Hartman 			return id;
818ddae41beSGreg Kroah-Hartman 	}
819ddae41beSGreg Kroah-Hartman 
820ddae41beSGreg Kroah-Hartman 	return NULL;
821ddae41beSGreg Kroah-Hartman }
822782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_match_id);
823ddae41beSGreg Kroah-Hartman 
usb_device_match_id(struct usb_device * udev,const struct usb_device_id * id)824aeebf2b5SBastien Nocera const struct usb_device_id *usb_device_match_id(struct usb_device *udev,
825aeebf2b5SBastien Nocera 				const struct usb_device_id *id)
826aeebf2b5SBastien Nocera {
827aeebf2b5SBastien Nocera 	if (!id)
828aeebf2b5SBastien Nocera 		return NULL;
829aeebf2b5SBastien Nocera 
830aeebf2b5SBastien Nocera 	for (; id->idVendor || id->idProduct ; id++) {
831aeebf2b5SBastien Nocera 		if (usb_match_device(udev, id))
832aeebf2b5SBastien Nocera 			return id;
833aeebf2b5SBastien Nocera 	}
834aeebf2b5SBastien Nocera 
835aeebf2b5SBastien Nocera 	return NULL;
836aeebf2b5SBastien Nocera }
837b1e9e7ebSRazvan Heghedus EXPORT_SYMBOL_GPL(usb_device_match_id);
838aeebf2b5SBastien Nocera 
usb_driver_applicable(struct usb_device * udev,struct usb_device_driver * udrv)8390942d59bSBastien Nocera bool usb_driver_applicable(struct usb_device *udev,
8400942d59bSBastien Nocera 			   struct usb_device_driver *udrv)
8410942d59bSBastien Nocera {
8420942d59bSBastien Nocera 	if (udrv->id_table && udrv->match)
8430942d59bSBastien Nocera 		return usb_device_match_id(udev, udrv->id_table) != NULL &&
8440942d59bSBastien Nocera 		       udrv->match(udev);
8450942d59bSBastien Nocera 
8460942d59bSBastien Nocera 	if (udrv->id_table)
8470942d59bSBastien Nocera 		return usb_device_match_id(udev, udrv->id_table) != NULL;
8480942d59bSBastien Nocera 
8490942d59bSBastien Nocera 	if (udrv->match)
8500942d59bSBastien Nocera 		return udrv->match(udev);
8510942d59bSBastien Nocera 
8520942d59bSBastien Nocera 	return false;
8530942d59bSBastien Nocera }
8540942d59bSBastien Nocera 
usb_device_match(struct device * dev,struct device_driver * drv)8558bb22d2bSAdrian Bunk static int usb_device_match(struct device *dev, struct device_driver *drv)
856ddae41beSGreg Kroah-Hartman {
8578bb54ab5SAlan Stern 	/* devices and interfaces are handled separately */
8588bb54ab5SAlan Stern 	if (is_usb_device(dev)) {
85988b7381aSBastien Nocera 		struct usb_device *udev;
86088b7381aSBastien Nocera 		struct usb_device_driver *udrv;
8618bb54ab5SAlan Stern 
8628bb54ab5SAlan Stern 		/* interface drivers never match devices */
8638bb54ab5SAlan Stern 		if (!is_usb_device_driver(drv))
8648bb54ab5SAlan Stern 			return 0;
8658bb54ab5SAlan Stern 
86688b7381aSBastien Nocera 		udev = to_usb_device(dev);
86788b7381aSBastien Nocera 		udrv = to_usb_device_driver(drv);
86888b7381aSBastien Nocera 
8693fce3960SM. Vefa Bicakci 		/* If the device driver under consideration does not have a
8703fce3960SM. Vefa Bicakci 		 * id_table or a match function, then let the driver's probe
8713fce3960SM. Vefa Bicakci 		 * function decide.
8723fce3960SM. Vefa Bicakci 		 */
8730942d59bSBastien Nocera 		if (!udrv->id_table && !udrv->match)
8743fce3960SM. Vefa Bicakci 			return 1;
8758bb54ab5SAlan Stern 
8760942d59bSBastien Nocera 		return usb_driver_applicable(udev, udrv);
8770942d59bSBastien Nocera 
87855129666SKay Sievers 	} else if (is_usb_interface(dev)) {
879ddae41beSGreg Kroah-Hartman 		struct usb_interface *intf;
880ddae41beSGreg Kroah-Hartman 		struct usb_driver *usb_drv;
881ddae41beSGreg Kroah-Hartman 		const struct usb_device_id *id;
882ddae41beSGreg Kroah-Hartman 
8838bb54ab5SAlan Stern 		/* device drivers never match interfaces */
8848bb54ab5SAlan Stern 		if (is_usb_device_driver(drv))
885ddae41beSGreg Kroah-Hartman 			return 0;
886ddae41beSGreg Kroah-Hartman 
887ddae41beSGreg Kroah-Hartman 		intf = to_usb_interface(dev);
888ddae41beSGreg Kroah-Hartman 		usb_drv = to_usb_driver(drv);
889ddae41beSGreg Kroah-Hartman 
890ddae41beSGreg Kroah-Hartman 		id = usb_match_id(intf, usb_drv->id_table);
891ddae41beSGreg Kroah-Hartman 		if (id)
892ddae41beSGreg Kroah-Hartman 			return 1;
893ddae41beSGreg Kroah-Hartman 
894733260ffSGreg Kroah-Hartman 		id = usb_match_dynamic_id(intf, usb_drv);
895733260ffSGreg Kroah-Hartman 		if (id)
896733260ffSGreg Kroah-Hartman 			return 1;
8978bb54ab5SAlan Stern 	}
8988bb54ab5SAlan Stern 
899ddae41beSGreg Kroah-Hartman 	return 0;
900ddae41beSGreg Kroah-Hartman }
901ddae41beSGreg Kroah-Hartman 
usb_uevent(const struct device * dev,struct kobj_uevent_env * env)9022a81ada3SGreg Kroah-Hartman static int usb_uevent(const struct device *dev, struct kobj_uevent_env *env)
90336e56a34SAlan Stern {
9042a81ada3SGreg Kroah-Hartman 	const struct usb_device *usb_dev;
90536e56a34SAlan Stern 
90655129666SKay Sievers 	if (is_usb_device(dev)) {
907782da727SAlan Stern 		usb_dev = to_usb_device(dev);
90855129666SKay Sievers 	} else if (is_usb_interface(dev)) {
9092a81ada3SGreg Kroah-Hartman 		const struct usb_interface *intf = to_usb_interface(dev);
91055129666SKay Sievers 
91136e56a34SAlan Stern 		usb_dev = interface_to_usbdev(intf);
91255129666SKay Sievers 	} else {
91355129666SKay Sievers 		return 0;
9148bb54ab5SAlan Stern 	}
91536e56a34SAlan Stern 
91636e56a34SAlan Stern 	if (usb_dev->devnum < 0) {
917cceffe93SAlan Stern 		/* driver is often null here; dev_dbg() would oops */
9187071a3ceSKay Sievers 		pr_debug("usb %s: already deleted?\n", dev_name(dev));
91936e56a34SAlan Stern 		return -ENODEV;
92036e56a34SAlan Stern 	}
92136e56a34SAlan Stern 	if (!usb_dev->bus) {
9227071a3ceSKay Sievers 		pr_debug("usb %s: bus removed?\n", dev_name(dev));
92336e56a34SAlan Stern 		return -ENODEV;
92436e56a34SAlan Stern 	}
92536e56a34SAlan Stern 
92636e56a34SAlan Stern 	/* per-device configurations are common */
9277eff2e7aSKay Sievers 	if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
92836e56a34SAlan Stern 			   le16_to_cpu(usb_dev->descriptor.idVendor),
92936e56a34SAlan Stern 			   le16_to_cpu(usb_dev->descriptor.idProduct),
93036e56a34SAlan Stern 			   le16_to_cpu(usb_dev->descriptor.bcdDevice)))
93136e56a34SAlan Stern 		return -ENOMEM;
93236e56a34SAlan Stern 
93336e56a34SAlan Stern 	/* class-based driver binding models */
9347eff2e7aSKay Sievers 	if (add_uevent_var(env, "TYPE=%d/%d/%d",
93536e56a34SAlan Stern 			   usb_dev->descriptor.bDeviceClass,
93636e56a34SAlan Stern 			   usb_dev->descriptor.bDeviceSubClass,
93736e56a34SAlan Stern 			   usb_dev->descriptor.bDeviceProtocol))
93836e56a34SAlan Stern 		return -ENOMEM;
93936e56a34SAlan Stern 
94036e56a34SAlan Stern 	return 0;
94136e56a34SAlan Stern }
94236e56a34SAlan Stern 
__usb_bus_reprobe_drivers(struct device * dev,void * data)943d5643d22SBastien Nocera static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
944d5643d22SBastien Nocera {
945d5643d22SBastien Nocera 	struct usb_device_driver *new_udriver = data;
946d5643d22SBastien Nocera 	struct usb_device *udev;
947d5643d22SBastien Nocera 	int ret;
948d5643d22SBastien Nocera 
9494df30e76SM. Vefa Bicakci 	/* Don't reprobe if current driver isn't usb_generic_driver */
9504df30e76SM. Vefa Bicakci 	if (dev->driver != &usb_generic_driver.drvwrap.driver)
951d5643d22SBastien Nocera 		return 0;
952d5643d22SBastien Nocera 
953d5643d22SBastien Nocera 	udev = to_usb_device(dev);
9540942d59bSBastien Nocera 	if (!usb_driver_applicable(udev, new_udriver))
955d5643d22SBastien Nocera 		return 0;
956d5643d22SBastien Nocera 
957d5643d22SBastien Nocera 	ret = device_reprobe(dev);
958d5643d22SBastien Nocera 	if (ret && ret != -EPROBE_DEFER)
959d5643d22SBastien Nocera 		dev_err(dev, "Failed to reprobe device (error %d)\n", ret);
960d5643d22SBastien Nocera 
961d5643d22SBastien Nocera 	return 0;
962d5643d22SBastien Nocera }
963d5643d22SBastien Nocera 
964ddae41beSGreg Kroah-Hartman /**
9658bb54ab5SAlan Stern  * usb_register_device_driver - register a USB device (not interface) driver
9668bb54ab5SAlan Stern  * @new_udriver: USB operations for the device driver
9672143acc6SGreg Kroah-Hartman  * @owner: module owner of this driver.
968ddae41beSGreg Kroah-Hartman  *
9698bb54ab5SAlan Stern  * Registers a USB device driver with the USB core.  The list of
9708bb54ab5SAlan Stern  * unattached devices will be rescanned whenever a new driver is
9718bb54ab5SAlan Stern  * added, allowing the new driver to attach to any recognized devices.
972626f090cSYacine Belkadi  *
973626f090cSYacine Belkadi  * Return: A negative error code on failure and 0 on success.
9748bb54ab5SAlan Stern  */
usb_register_device_driver(struct usb_device_driver * new_udriver,struct module * owner)9758bb54ab5SAlan Stern int usb_register_device_driver(struct usb_device_driver *new_udriver,
9768bb54ab5SAlan Stern 		struct module *owner)
9778bb54ab5SAlan Stern {
9788bb54ab5SAlan Stern 	int retval = 0;
9798bb54ab5SAlan Stern 
9808bb54ab5SAlan Stern 	if (usb_disabled())
9818bb54ab5SAlan Stern 		return -ENODEV;
9828bb54ab5SAlan Stern 
9838bb54ab5SAlan Stern 	new_udriver->drvwrap.for_devices = 1;
9849f9af82fSGeert Uytterhoeven 	new_udriver->drvwrap.driver.name = new_udriver->name;
9858bb54ab5SAlan Stern 	new_udriver->drvwrap.driver.bus = &usb_bus_type;
9868bb54ab5SAlan Stern 	new_udriver->drvwrap.driver.probe = usb_probe_device;
9878bb54ab5SAlan Stern 	new_udriver->drvwrap.driver.remove = usb_unbind_device;
9888bb54ab5SAlan Stern 	new_udriver->drvwrap.driver.owner = owner;
9897d9c1d2fSGreg Kroah-Hartman 	new_udriver->drvwrap.driver.dev_groups = new_udriver->dev_groups;
9908bb54ab5SAlan Stern 
9918bb54ab5SAlan Stern 	retval = driver_register(&new_udriver->drvwrap.driver);
9928bb54ab5SAlan Stern 
993d5643d22SBastien Nocera 	if (!retval) {
9948bb54ab5SAlan Stern 		pr_info("%s: registered new device driver %s\n",
9958bb54ab5SAlan Stern 			usbcore_name, new_udriver->name);
996d5643d22SBastien Nocera 		/*
997d5643d22SBastien Nocera 		 * Check whether any device could be better served with
998d5643d22SBastien Nocera 		 * this new driver
999d5643d22SBastien Nocera 		 */
1000d5643d22SBastien Nocera 		bus_for_each_dev(&usb_bus_type, NULL, new_udriver,
1001d5643d22SBastien Nocera 				 __usb_bus_reprobe_drivers);
1002d5643d22SBastien Nocera 	} else {
1003bb0634ecSSergey Shtylyov 		pr_err("%s: error %d registering device driver %s\n",
10048bb54ab5SAlan Stern 			usbcore_name, retval, new_udriver->name);
1005d5643d22SBastien Nocera 	}
10068bb54ab5SAlan Stern 
10078bb54ab5SAlan Stern 	return retval;
10088bb54ab5SAlan Stern }
10098bb54ab5SAlan Stern EXPORT_SYMBOL_GPL(usb_register_device_driver);
10108bb54ab5SAlan Stern 
10118bb54ab5SAlan Stern /**
10128bb54ab5SAlan Stern  * usb_deregister_device_driver - unregister a USB device (not interface) driver
10138bb54ab5SAlan Stern  * @udriver: USB operations of the device driver to unregister
10148bb54ab5SAlan Stern  * Context: must be able to sleep
10158bb54ab5SAlan Stern  *
10168bb54ab5SAlan Stern  * Unlinks the specified driver from the internal USB driver list.
10178bb54ab5SAlan Stern  */
usb_deregister_device_driver(struct usb_device_driver * udriver)10188bb54ab5SAlan Stern void usb_deregister_device_driver(struct usb_device_driver *udriver)
10198bb54ab5SAlan Stern {
10208bb54ab5SAlan Stern 	pr_info("%s: deregistering device driver %s\n",
10218bb54ab5SAlan Stern 			usbcore_name, udriver->name);
10228bb54ab5SAlan Stern 
10238bb54ab5SAlan Stern 	driver_unregister(&udriver->drvwrap.driver);
10248bb54ab5SAlan Stern }
10258bb54ab5SAlan Stern EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
10268bb54ab5SAlan Stern 
10278bb54ab5SAlan Stern /**
10288bb54ab5SAlan Stern  * usb_register_driver - register a USB interface driver
10298bb54ab5SAlan Stern  * @new_driver: USB operations for the interface driver
10308bb54ab5SAlan Stern  * @owner: module owner of this driver.
1031892705a1SRandy Dunlap  * @mod_name: module name string
10328bb54ab5SAlan Stern  *
10338bb54ab5SAlan Stern  * Registers a USB interface driver with the USB core.  The list of
10348bb54ab5SAlan Stern  * unattached interfaces will be rescanned whenever a new driver is
10358bb54ab5SAlan Stern  * added, allowing the new driver to attach to any recognized interfaces.
1036626f090cSYacine Belkadi  *
1037626f090cSYacine Belkadi  * Return: A negative error code on failure and 0 on success.
1038ddae41beSGreg Kroah-Hartman  *
1039ddae41beSGreg Kroah-Hartman  * NOTE: if you want your driver to use the USB major number, you must call
1040ddae41beSGreg Kroah-Hartman  * usb_register_dev() to enable that functionality.  This function no longer
1041ddae41beSGreg Kroah-Hartman  * takes care of that.
1042ddae41beSGreg Kroah-Hartman  */
usb_register_driver(struct usb_driver * new_driver,struct module * owner,const char * mod_name)104380f745fbSGreg Kroah-Hartman int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
104480f745fbSGreg Kroah-Hartman 			const char *mod_name)
1045ddae41beSGreg Kroah-Hartman {
1046ddae41beSGreg Kroah-Hartman 	int retval = 0;
1047ddae41beSGreg Kroah-Hartman 
1048ddae41beSGreg Kroah-Hartman 	if (usb_disabled())
1049ddae41beSGreg Kroah-Hartman 		return -ENODEV;
1050ddae41beSGreg Kroah-Hartman 
10518bb54ab5SAlan Stern 	new_driver->drvwrap.for_devices = 0;
10529f9af82fSGeert Uytterhoeven 	new_driver->drvwrap.driver.name = new_driver->name;
10538bb54ab5SAlan Stern 	new_driver->drvwrap.driver.bus = &usb_bus_type;
10548bb54ab5SAlan Stern 	new_driver->drvwrap.driver.probe = usb_probe_interface;
10558bb54ab5SAlan Stern 	new_driver->drvwrap.driver.remove = usb_unbind_interface;
10568bb54ab5SAlan Stern 	new_driver->drvwrap.driver.owner = owner;
105780f745fbSGreg Kroah-Hartman 	new_driver->drvwrap.driver.mod_name = mod_name;
1058b71b283eSGreg Kroah-Hartman 	new_driver->drvwrap.driver.dev_groups = new_driver->dev_groups;
1059733260ffSGreg Kroah-Hartman 	spin_lock_init(&new_driver->dynids.lock);
1060733260ffSGreg Kroah-Hartman 	INIT_LIST_HEAD(&new_driver->dynids.list);
1061ddae41beSGreg Kroah-Hartman 
10628bb54ab5SAlan Stern 	retval = driver_register(&new_driver->drvwrap.driver);
10630c7a2b72SCHENG Renquan 	if (retval)
10640c7a2b72SCHENG Renquan 		goto out;
1065ddae41beSGreg Kroah-Hartman 
1066ed283e9fSAlan Stern 	retval = usb_create_newid_files(new_driver);
10670c7a2b72SCHENG Renquan 	if (retval)
10680c7a2b72SCHENG Renquan 		goto out_newid;
10690c7a2b72SCHENG Renquan 
10708bb54ab5SAlan Stern 	pr_info("%s: registered new interface driver %s\n",
1071ddae41beSGreg Kroah-Hartman 			usbcore_name, new_driver->name);
10720c7a2b72SCHENG Renquan 
10730c7a2b72SCHENG Renquan out:
10740c7a2b72SCHENG Renquan 	return retval;
10750c7a2b72SCHENG Renquan 
10760c7a2b72SCHENG Renquan out_newid:
10770c7a2b72SCHENG Renquan 	driver_unregister(&new_driver->drvwrap.driver);
10780c7a2b72SCHENG Renquan 
1079bb0634ecSSergey Shtylyov 	pr_err("%s: error %d registering interface driver %s\n",
1080ddae41beSGreg Kroah-Hartman 		usbcore_name, retval, new_driver->name);
10810c7a2b72SCHENG Renquan 	goto out;
1082ddae41beSGreg Kroah-Hartman }
1083782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_register_driver);
1084ddae41beSGreg Kroah-Hartman 
1085ddae41beSGreg Kroah-Hartman /**
10868bb54ab5SAlan Stern  * usb_deregister - unregister a USB interface driver
10878bb54ab5SAlan Stern  * @driver: USB operations of the interface driver to unregister
1088ddae41beSGreg Kroah-Hartman  * Context: must be able to sleep
1089ddae41beSGreg Kroah-Hartman  *
1090ddae41beSGreg Kroah-Hartman  * Unlinks the specified driver from the internal USB driver list.
1091ddae41beSGreg Kroah-Hartman  *
1092ddae41beSGreg Kroah-Hartman  * NOTE: If you called usb_register_dev(), you still need to call
1093ddae41beSGreg Kroah-Hartman  * usb_deregister_dev() to clean up your driver's allocated minor numbers,
1094ddae41beSGreg Kroah-Hartman  * this * call will no longer do it for you.
1095ddae41beSGreg Kroah-Hartman  */
usb_deregister(struct usb_driver * driver)1096ddae41beSGreg Kroah-Hartman void usb_deregister(struct usb_driver *driver)
1097ddae41beSGreg Kroah-Hartman {
10988bb54ab5SAlan Stern 	pr_info("%s: deregistering interface driver %s\n",
10998bb54ab5SAlan Stern 			usbcore_name, driver->name);
1100ddae41beSGreg Kroah-Hartman 
1101ed283e9fSAlan Stern 	usb_remove_newid_files(driver);
11028bb54ab5SAlan Stern 	driver_unregister(&driver->drvwrap.driver);
1103ed283e9fSAlan Stern 	usb_free_dynids(driver);
1104ddae41beSGreg Kroah-Hartman }
1105782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_deregister);
110636e56a34SAlan Stern 
110778d9a487SAlan Stern /* Forced unbinding of a USB interface driver, either because
110878d9a487SAlan Stern  * it doesn't support pre_reset/post_reset/reset_resume or
110978d9a487SAlan Stern  * because it doesn't support suspend/resume.
111078d9a487SAlan Stern  *
11116aec044cSAlan Stern  * The caller must hold @intf's device's lock, but not @intf's lock.
111278d9a487SAlan Stern  */
usb_forced_unbind_intf(struct usb_interface * intf)111378d9a487SAlan Stern void usb_forced_unbind_intf(struct usb_interface *intf)
111478d9a487SAlan Stern {
111578d9a487SAlan Stern 	struct usb_driver *driver = to_usb_driver(intf->dev.driver);
111678d9a487SAlan Stern 
111778d9a487SAlan Stern 	dev_dbg(&intf->dev, "forced unbind\n");
111878d9a487SAlan Stern 	usb_driver_release_interface(driver, intf);
111978d9a487SAlan Stern 
112078d9a487SAlan Stern 	/* Mark the interface for later rebinding */
112178d9a487SAlan Stern 	intf->needs_binding = 1;
112278d9a487SAlan Stern }
112378d9a487SAlan Stern 
11246aec044cSAlan Stern /*
11256aec044cSAlan Stern  * Unbind drivers for @udev's marked interfaces.  These interfaces have
11266aec044cSAlan Stern  * the needs_binding flag set, for example by usb_resume_interface().
11276aec044cSAlan Stern  *
11286aec044cSAlan Stern  * The caller must hold @udev's device lock.
11296aec044cSAlan Stern  */
unbind_marked_interfaces(struct usb_device * udev)11306aec044cSAlan Stern static void unbind_marked_interfaces(struct usb_device *udev)
11316aec044cSAlan Stern {
11326aec044cSAlan Stern 	struct usb_host_config	*config;
11336aec044cSAlan Stern 	int			i;
11346aec044cSAlan Stern 	struct usb_interface	*intf;
11356aec044cSAlan Stern 
11366aec044cSAlan Stern 	config = udev->actconfig;
11376aec044cSAlan Stern 	if (config) {
11386aec044cSAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
11396aec044cSAlan Stern 			intf = config->interface[i];
11406aec044cSAlan Stern 			if (intf->dev.driver && intf->needs_binding)
11416aec044cSAlan Stern 				usb_forced_unbind_intf(intf);
11426aec044cSAlan Stern 		}
11436aec044cSAlan Stern 	}
11446aec044cSAlan Stern }
11456aec044cSAlan Stern 
114678d9a487SAlan Stern /* Delayed forced unbinding of a USB interface driver and scan
114778d9a487SAlan Stern  * for rebinding.
114878d9a487SAlan Stern  *
11496aec044cSAlan Stern  * The caller must hold @intf's device's lock, but not @intf's lock.
115078d9a487SAlan Stern  *
11515096aedcSAlan Stern  * Note: Rebinds will be skipped if a system sleep transition is in
11525096aedcSAlan Stern  * progress and the PM "complete" callback hasn't occurred yet.
115378d9a487SAlan Stern  */
usb_rebind_intf(struct usb_interface * intf)11546aec044cSAlan Stern static void usb_rebind_intf(struct usb_interface *intf)
115578d9a487SAlan Stern {
115678d9a487SAlan Stern 	int rc;
115778d9a487SAlan Stern 
115878d9a487SAlan Stern 	/* Delayed unbind of an existing driver */
11591493138aSOliver Neukum 	if (intf->dev.driver)
11601493138aSOliver Neukum 		usb_forced_unbind_intf(intf);
116178d9a487SAlan Stern 
116278d9a487SAlan Stern 	/* Try to rebind the interface */
1163f76b168bSAlan Stern 	if (!intf->dev.power.is_prepared) {
116478d9a487SAlan Stern 		intf->needs_binding = 0;
116578d9a487SAlan Stern 		rc = device_attach(&intf->dev);
1166c0f3ed87SMathias Nyman 		if (rc < 0 && rc != -EPROBE_DEFER)
116778d9a487SAlan Stern 			dev_warn(&intf->dev, "rebind failed: %d\n", rc);
116878d9a487SAlan Stern 	}
11695096aedcSAlan Stern }
117078d9a487SAlan Stern 
11716aec044cSAlan Stern /*
11726aec044cSAlan Stern  * Rebind drivers to @udev's marked interfaces.  These interfaces have
11736aec044cSAlan Stern  * the needs_binding flag set.
11746aec044cSAlan Stern  *
11756aec044cSAlan Stern  * The caller must hold @udev's device lock.
11766aec044cSAlan Stern  */
rebind_marked_interfaces(struct usb_device * udev)11776aec044cSAlan Stern static void rebind_marked_interfaces(struct usb_device *udev)
11786aec044cSAlan Stern {
11796aec044cSAlan Stern 	struct usb_host_config	*config;
11806aec044cSAlan Stern 	int			i;
11816aec044cSAlan Stern 	struct usb_interface	*intf;
11826aec044cSAlan Stern 
11836aec044cSAlan Stern 	config = udev->actconfig;
11846aec044cSAlan Stern 	if (config) {
11856aec044cSAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
11866aec044cSAlan Stern 			intf = config->interface[i];
11876aec044cSAlan Stern 			if (intf->needs_binding)
11886aec044cSAlan Stern 				usb_rebind_intf(intf);
11896aec044cSAlan Stern 		}
11906aec044cSAlan Stern 	}
11916aec044cSAlan Stern }
11926aec044cSAlan Stern 
11936aec044cSAlan Stern /*
11946aec044cSAlan Stern  * Unbind all of @udev's marked interfaces and then rebind all of them.
11956aec044cSAlan Stern  * This ordering is necessary because some drivers claim several interfaces
11966aec044cSAlan Stern  * when they are first probed.
11976aec044cSAlan Stern  *
11986aec044cSAlan Stern  * The caller must hold @udev's device lock.
11996aec044cSAlan Stern  */
usb_unbind_and_rebind_marked_interfaces(struct usb_device * udev)12006aec044cSAlan Stern void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev)
12016aec044cSAlan Stern {
12026aec044cSAlan Stern 	unbind_marked_interfaces(udev);
12036aec044cSAlan Stern 	rebind_marked_interfaces(udev);
12046aec044cSAlan Stern }
12056aec044cSAlan Stern 
12069ff78433SAlan Stern #ifdef CONFIG_PM
12079ff78433SAlan Stern 
12081493138aSOliver Neukum /* Unbind drivers for @udev's interfaces that don't support suspend/resume
12091493138aSOliver Neukum  * There is no check for reset_resume here because it can be determined
12101493138aSOliver Neukum  * only during resume whether reset_resume is needed.
121178d9a487SAlan Stern  *
121278d9a487SAlan Stern  * The caller must hold @udev's device lock.
121378d9a487SAlan Stern  */
unbind_no_pm_drivers_interfaces(struct usb_device * udev)12141493138aSOliver Neukum static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
121578d9a487SAlan Stern {
121678d9a487SAlan Stern 	struct usb_host_config	*config;
121778d9a487SAlan Stern 	int			i;
121878d9a487SAlan Stern 	struct usb_interface	*intf;
121978d9a487SAlan Stern 	struct usb_driver	*drv;
122078d9a487SAlan Stern 
122178d9a487SAlan Stern 	config = udev->actconfig;
122278d9a487SAlan Stern 	if (config) {
122378d9a487SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
122478d9a487SAlan Stern 			intf = config->interface[i];
12251493138aSOliver Neukum 
122678d9a487SAlan Stern 			if (intf->dev.driver) {
122778d9a487SAlan Stern 				drv = to_usb_driver(intf->dev.driver);
122878d9a487SAlan Stern 				if (!drv->suspend || !drv->resume)
122978d9a487SAlan Stern 					usb_forced_unbind_intf(intf);
123078d9a487SAlan Stern 			}
12311493138aSOliver Neukum 		}
12321493138aSOliver Neukum 	}
12331493138aSOliver Neukum }
12341493138aSOliver Neukum 
usb_suspend_device(struct usb_device * udev,pm_message_t msg)1235d5ec1686SStephen Hemminger static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
123636e56a34SAlan Stern {
1237782da727SAlan Stern 	struct usb_device_driver	*udriver;
12382bf4086dSAlan Stern 	int				status = 0;
12391cc8a25dSAlan Stern 
1240114b368cSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
1241114b368cSAlan Stern 			udev->state == USB_STATE_SUSPENDED)
1242114b368cSAlan Stern 		goto done;
1243114b368cSAlan Stern 
1244b6f6436dSAlan Stern 	/* For devices that don't have a driver, we do a generic suspend. */
1245b6f6436dSAlan Stern 	if (udev->dev.driver)
12461cc8a25dSAlan Stern 		udriver = to_usb_device_driver(udev->dev.driver);
1247b6f6436dSAlan Stern 	else {
1248b6f6436dSAlan Stern 		udev->do_remote_wakeup = 0;
1249b6f6436dSAlan Stern 		udriver = &usb_generic_driver;
1250b6f6436dSAlan Stern 	}
1251c9d50337SBastien Nocera 	if (udriver->suspend)
12522bf4086dSAlan Stern 		status = udriver->suspend(udev, msg);
1253c9d50337SBastien Nocera 	if (status == 0 && udriver->generic_subclass)
1254c9d50337SBastien Nocera 		status = usb_generic_driver_suspend(udev, msg);
12552bf4086dSAlan Stern 
12562bf4086dSAlan Stern  done:
1257441b62c1SHarvey Harrison 	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
12582bf4086dSAlan Stern 	return status;
12591cc8a25dSAlan Stern }
12601cc8a25dSAlan Stern 
usb_resume_device(struct usb_device * udev,pm_message_t msg)126165bfd296SAlan Stern static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
12621cc8a25dSAlan Stern {
12631cc8a25dSAlan Stern 	struct usb_device_driver	*udriver;
12642bf4086dSAlan Stern 	int				status = 0;
12651cc8a25dSAlan Stern 
12660458d5b4SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED)
12670458d5b4SAlan Stern 		goto done;
12681cc8a25dSAlan Stern 
12691c5df7e7SAlan Stern 	/* Can't resume it if it doesn't have a driver. */
12701c5df7e7SAlan Stern 	if (udev->dev.driver == NULL) {
12711c5df7e7SAlan Stern 		status = -ENOTCONN;
12722bf4086dSAlan Stern 		goto done;
12731c5df7e7SAlan Stern 	}
12741c5df7e7SAlan Stern 
12756d19c009SAlan Stern 	/* Non-root devices on a full/low-speed bus must wait for their
12766d19c009SAlan Stern 	 * companion high-speed root hub, in case a handoff is needed.
12776d19c009SAlan Stern 	 */
12785b1b0b81SAlan Stern 	if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
12796d19c009SAlan Stern 		device_pm_wait_for_dev(&udev->dev,
12806d19c009SAlan Stern 				&udev->bus->hs_companion->root_hub->dev);
12816d19c009SAlan Stern 
12826bc6cff5SAlan Stern 	if (udev->quirks & USB_QUIRK_RESET_RESUME)
12836bc6cff5SAlan Stern 		udev->reset_resume = 1;
12846bc6cff5SAlan Stern 
12851cc8a25dSAlan Stern 	udriver = to_usb_device_driver(udev->dev.driver);
1286c9d50337SBastien Nocera 	if (udriver->generic_subclass)
1287c9d50337SBastien Nocera 		status = usb_generic_driver_resume(udev, msg);
1288c9d50337SBastien Nocera 	if (status == 0 && udriver->resume)
128965bfd296SAlan Stern 		status = udriver->resume(udev, msg);
12902bf4086dSAlan Stern 
12912bf4086dSAlan Stern  done:
1292441b62c1SHarvey Harrison 	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
12932bf4086dSAlan Stern 	return status;
12941cc8a25dSAlan Stern }
12951cc8a25dSAlan Stern 
usb_suspend_interface(struct usb_device * udev,struct usb_interface * intf,pm_message_t msg)129665605ae8SAlan Stern static int usb_suspend_interface(struct usb_device *udev,
129765605ae8SAlan Stern 		struct usb_interface *intf, pm_message_t msg)
12981cc8a25dSAlan Stern {
129936e56a34SAlan Stern 	struct usb_driver	*driver;
13002bf4086dSAlan Stern 	int			status = 0;
130136e56a34SAlan Stern 
13029bbdf1e0SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
13039bbdf1e0SAlan Stern 			intf->condition == USB_INTERFACE_UNBOUND)
13042bf4086dSAlan Stern 		goto done;
13051cc8a25dSAlan Stern 	driver = to_usb_driver(intf->dev.driver);
130636e56a34SAlan Stern 
1307e78832cdSOliver Neukum 	/* at this time we know the driver supports suspend */
13081cc8a25dSAlan Stern 	status = driver->suspend(intf, msg);
13095b1b0b81SAlan Stern 	if (status && !PMSG_IS_AUTO(msg))
1310e78832cdSOliver Neukum 		dev_err(&intf->dev, "suspend error %d\n", status);
13112bf4086dSAlan Stern 
13122bf4086dSAlan Stern  done:
1313441b62c1SHarvey Harrison 	dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
131436e56a34SAlan Stern 	return status;
131536e56a34SAlan Stern }
131636e56a34SAlan Stern 
usb_resume_interface(struct usb_device * udev,struct usb_interface * intf,pm_message_t msg,int reset_resume)131765605ae8SAlan Stern static int usb_resume_interface(struct usb_device *udev,
131865bfd296SAlan Stern 		struct usb_interface *intf, pm_message_t msg, int reset_resume)
131936e56a34SAlan Stern {
132036e56a34SAlan Stern 	struct usb_driver	*driver;
13212bf4086dSAlan Stern 	int			status = 0;
132236e56a34SAlan Stern 
13239bbdf1e0SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED)
13242bf4086dSAlan Stern 		goto done;
132536e56a34SAlan Stern 
1326645daaabSAlan Stern 	/* Don't let autoresume interfere with unbinding */
1327645daaabSAlan Stern 	if (intf->condition == USB_INTERFACE_UNBINDING)
1328645daaabSAlan Stern 		goto done;
1329645daaabSAlan Stern 
13301c5df7e7SAlan Stern 	/* Can't resume it if it doesn't have a driver. */
133155151d7dSAlan Stern 	if (intf->condition == USB_INTERFACE_UNBOUND) {
133255151d7dSAlan Stern 
133355151d7dSAlan Stern 		/* Carry out a deferred switch to altsetting 0 */
1334f76b168bSAlan Stern 		if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
133555151d7dSAlan Stern 			usb_set_interface(udev, intf->altsetting[0].
133655151d7dSAlan Stern 					desc.bInterfaceNumber, 0);
133755151d7dSAlan Stern 			intf->needs_altsetting0 = 0;
133855151d7dSAlan Stern 		}
13392bf4086dSAlan Stern 		goto done;
134055151d7dSAlan Stern 	}
134178d9a487SAlan Stern 
134278d9a487SAlan Stern 	/* Don't resume if the interface is marked for rebinding */
134378d9a487SAlan Stern 	if (intf->needs_binding)
134478d9a487SAlan Stern 		goto done;
13451cc8a25dSAlan Stern 	driver = to_usb_driver(intf->dev.driver);
134636e56a34SAlan Stern 
1347f07600cfSAlan Stern 	if (reset_resume) {
1348f07600cfSAlan Stern 		if (driver->reset_resume) {
1349f07600cfSAlan Stern 			status = driver->reset_resume(intf);
1350f07600cfSAlan Stern 			if (status)
1351f07600cfSAlan Stern 				dev_err(&intf->dev, "%s error %d\n",
1352f07600cfSAlan Stern 						"reset_resume", status);
1353f07600cfSAlan Stern 		} else {
135478d9a487SAlan Stern 			intf->needs_binding = 1;
13550a56b4faSAlan Stern 			dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
13560a56b4faSAlan Stern 					driver->name);
1357f07600cfSAlan Stern 		}
1358f07600cfSAlan Stern 	} else {
135936e56a34SAlan Stern 		status = driver->resume(intf);
13602bf4086dSAlan Stern 		if (status)
1361e78832cdSOliver Neukum 			dev_err(&intf->dev, "resume error %d\n", status);
1362f07600cfSAlan Stern 	}
13632bf4086dSAlan Stern 
13642bf4086dSAlan Stern done:
1365441b62c1SHarvey Harrison 	dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1366f07600cfSAlan Stern 
136778d9a487SAlan Stern 	/* Later we will unbind the driver and/or reprobe, if necessary */
13682bf4086dSAlan Stern 	return status;
136936e56a34SAlan Stern }
137036e56a34SAlan Stern 
1371645daaabSAlan Stern /**
1372645daaabSAlan Stern  * usb_suspend_both - suspend a USB device and its interfaces
1373645daaabSAlan Stern  * @udev: the usb_device to suspend
1374645daaabSAlan Stern  * @msg: Power Management message describing this state transition
1375645daaabSAlan Stern  *
1376645daaabSAlan Stern  * This is the central routine for suspending USB devices.  It calls the
1377645daaabSAlan Stern  * suspend methods for all the interface drivers in @udev and then calls
1378303f0847SMing Lei  * the suspend method for @udev itself.  When the routine is called in
1379303f0847SMing Lei  * autosuspend, if an error occurs at any stage, all the interfaces
1380303f0847SMing Lei  * which were suspended are resumed so that they remain in the same
1381303f0847SMing Lei  * state as the device, but when called from system sleep, all error
1382303f0847SMing Lei  * from suspend methods of interfaces and the non-root-hub device itself
1383303f0847SMing Lei  * are simply ignored, so all suspended interfaces are only resumed
1384303f0847SMing Lei  * to the device's state when @udev is root-hub and its suspend method
1385303f0847SMing Lei  * returns failure.
1386645daaabSAlan Stern  *
13879bbdf1e0SAlan Stern  * Autosuspend requests originating from a child device or an interface
13889bbdf1e0SAlan Stern  * driver may be made without the protection of @udev's device lock, but
13899bbdf1e0SAlan Stern  * all other suspend calls will hold the lock.  Usbcore will insure that
13909bbdf1e0SAlan Stern  * method calls do not arrive during bind, unbind, or reset operations.
13919bbdf1e0SAlan Stern  * However drivers must be prepared to handle suspend calls arriving at
13929bbdf1e0SAlan Stern  * unpredictable times.
1393645daaabSAlan Stern  *
1394645daaabSAlan Stern  * This routine can run only in process context.
1395626f090cSYacine Belkadi  *
1396626f090cSYacine Belkadi  * Return: 0 if the suspend succeeded.
1397645daaabSAlan Stern  */
usb_suspend_both(struct usb_device * udev,pm_message_t msg)1398718efa64SAlan Stern static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1399a8e7c565SAlan Stern {
1400a8e7c565SAlan Stern 	int			status = 0;
1401571dc79dSAlan Stern 	int			i = 0, n = 0;
1402a8e7c565SAlan Stern 	struct usb_interface	*intf;
1403a8e7c565SAlan Stern 
14041941044aSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
14051941044aSAlan Stern 			udev->state == USB_STATE_SUSPENDED)
14061941044aSAlan Stern 		goto done;
1407645daaabSAlan Stern 
1408645daaabSAlan Stern 	/* Suspend all the interfaces and then udev itself */
1409a8e7c565SAlan Stern 	if (udev->actconfig) {
1410571dc79dSAlan Stern 		n = udev->actconfig->desc.bNumInterfaces;
1411571dc79dSAlan Stern 		for (i = n - 1; i >= 0; --i) {
1412a8e7c565SAlan Stern 			intf = udev->actconfig->interface[i];
141365605ae8SAlan Stern 			status = usb_suspend_interface(udev, intf, msg);
14140af212baSAlan Stern 
14150af212baSAlan Stern 			/* Ignore errors during system sleep transitions */
14165b1b0b81SAlan Stern 			if (!PMSG_IS_AUTO(msg))
14170af212baSAlan Stern 				status = 0;
1418a8e7c565SAlan Stern 			if (status != 0)
1419a8e7c565SAlan Stern 				break;
1420a8e7c565SAlan Stern 		}
1421a8e7c565SAlan Stern 	}
14220af212baSAlan Stern 	if (status == 0) {
1423d5ec1686SStephen Hemminger 		status = usb_suspend_device(udev, msg);
1424a8e7c565SAlan Stern 
1425cd4376e2SAlan Stern 		/*
1426cd4376e2SAlan Stern 		 * Ignore errors from non-root-hub devices during
1427cd4376e2SAlan Stern 		 * system sleep transitions.  For the most part,
1428cd4376e2SAlan Stern 		 * these devices should go to low power anyway when
1429cd4376e2SAlan Stern 		 * the entire bus is suspended.
1430cd4376e2SAlan Stern 		 */
1431cd4376e2SAlan Stern 		if (udev->parent && !PMSG_IS_AUTO(msg))
14320af212baSAlan Stern 			status = 0;
1433245b2eecSGuenter Roeck 
1434245b2eecSGuenter Roeck 		/*
1435245b2eecSGuenter Roeck 		 * If the device is inaccessible, don't try to resume
1436245b2eecSGuenter Roeck 		 * suspended interfaces and just return the error.
1437245b2eecSGuenter Roeck 		 */
1438245b2eecSGuenter Roeck 		if (status && status != -EBUSY) {
1439245b2eecSGuenter Roeck 			int err;
1440245b2eecSGuenter Roeck 			u16 devstat;
1441245b2eecSGuenter Roeck 
1442d9e1e148SFelipe Balbi 			err = usb_get_std_status(udev, USB_RECIP_DEVICE, 0,
1443245b2eecSGuenter Roeck 						 &devstat);
1444245b2eecSGuenter Roeck 			if (err) {
1445245b2eecSGuenter Roeck 				dev_err(&udev->dev,
1446245b2eecSGuenter Roeck 					"Failed to suspend device, error %d\n",
1447245b2eecSGuenter Roeck 					status);
1448245b2eecSGuenter Roeck 				goto done;
1449245b2eecSGuenter Roeck 			}
1450245b2eecSGuenter Roeck 		}
14510af212baSAlan Stern 	}
14520af212baSAlan Stern 
1453a8e7c565SAlan Stern 	/* If the suspend failed, resume interfaces that did get suspended */
1454a8e7c565SAlan Stern 	if (status != 0) {
1455505bdbc7SChen Gang 		if (udev->actconfig) {
14569bbdf1e0SAlan Stern 			msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1457571dc79dSAlan Stern 			while (++i < n) {
1458a8e7c565SAlan Stern 				intf = udev->actconfig->interface[i];
14599bbdf1e0SAlan Stern 				usb_resume_interface(udev, intf, msg, 0);
1460a8e7c565SAlan Stern 			}
1461505bdbc7SChen Gang 		}
1462645daaabSAlan Stern 
14639bbdf1e0SAlan Stern 	/* If the suspend succeeded then prevent any more URB submissions
14649bbdf1e0SAlan Stern 	 * and flush any outstanding URBs.
14656840d255SAlan Stern 	 */
1466ef7f6c70SAlan Stern 	} else {
14676840d255SAlan Stern 		udev->can_submit = 0;
14686840d255SAlan Stern 		for (i = 0; i < 16; ++i) {
14696840d255SAlan Stern 			usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
14706840d255SAlan Stern 			usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
14716840d255SAlan Stern 		}
1472ef7f6c70SAlan Stern 	}
1473645daaabSAlan Stern 
14741941044aSAlan Stern  done:
1475441b62c1SHarvey Harrison 	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
1476a8e7c565SAlan Stern 	return status;
1477a8e7c565SAlan Stern }
1478a8e7c565SAlan Stern 
1479645daaabSAlan Stern /**
1480645daaabSAlan Stern  * usb_resume_both - resume a USB device and its interfaces
1481645daaabSAlan Stern  * @udev: the usb_device to resume
148265bfd296SAlan Stern  * @msg: Power Management message describing this state transition
1483645daaabSAlan Stern  *
1484645daaabSAlan Stern  * This is the central routine for resuming USB devices.  It calls the
1485a7a9f4c0SJilin Yuan  * resume method for @udev and then calls the resume methods for all
1486645daaabSAlan Stern  * the interface drivers in @udev.
1487645daaabSAlan Stern  *
14889bbdf1e0SAlan Stern  * Autoresume requests originating from a child device or an interface
14899bbdf1e0SAlan Stern  * driver may be made without the protection of @udev's device lock, but
14909bbdf1e0SAlan Stern  * all other resume calls will hold the lock.  Usbcore will insure that
14919bbdf1e0SAlan Stern  * method calls do not arrive during bind, unbind, or reset operations.
14929bbdf1e0SAlan Stern  * However drivers must be prepared to handle resume calls arriving at
14939bbdf1e0SAlan Stern  * unpredictable times.
1494645daaabSAlan Stern  *
1495645daaabSAlan Stern  * This routine can run only in process context.
1496626f090cSYacine Belkadi  *
1497626f090cSYacine Belkadi  * Return: 0 on success.
1498645daaabSAlan Stern  */
usb_resume_both(struct usb_device * udev,pm_message_t msg)149965bfd296SAlan Stern static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1500a8e7c565SAlan Stern {
1501645daaabSAlan Stern 	int			status = 0;
1502a8e7c565SAlan Stern 	int			i;
1503a8e7c565SAlan Stern 	struct usb_interface	*intf;
1504a8e7c565SAlan Stern 
15051941044aSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED) {
15061941044aSAlan Stern 		status = -ENODEV;
15071941044aSAlan Stern 		goto done;
15081941044aSAlan Stern 	}
15096840d255SAlan Stern 	udev->can_submit = 1;
1510645daaabSAlan Stern 
15119bbdf1e0SAlan Stern 	/* Resume the device */
15129bbdf1e0SAlan Stern 	if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
151365bfd296SAlan Stern 		status = usb_resume_device(udev, msg);
1514114b368cSAlan Stern 
15159bbdf1e0SAlan Stern 	/* Resume the interfaces */
1516a8e7c565SAlan Stern 	if (status == 0 && udev->actconfig) {
1517a8e7c565SAlan Stern 		for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1518a8e7c565SAlan Stern 			intf = udev->actconfig->interface[i];
151965bfd296SAlan Stern 			usb_resume_interface(udev, intf, msg,
152065bfd296SAlan Stern 					udev->reset_resume);
1521a8e7c565SAlan Stern 		}
1522a8e7c565SAlan Stern 	}
1523c08512c7SAlan Stern 	usb_mark_last_busy(udev);
1524645daaabSAlan Stern 
15251941044aSAlan Stern  done:
1526441b62c1SHarvey Harrison 	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
152770a1c9e0SAlan Stern 	if (!status)
15280458d5b4SAlan Stern 		udev->reset_resume = 0;
1529a8e7c565SAlan Stern 	return status;
1530a8e7c565SAlan Stern }
1531a8e7c565SAlan Stern 
choose_wakeup(struct usb_device * udev,pm_message_t msg)15325f677f1dSAlan Stern static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
15335f677f1dSAlan Stern {
153448826626SAlan Stern 	int	w;
15355f677f1dSAlan Stern 
15369671d550SEvan Green 	/*
15379671d550SEvan Green 	 * For FREEZE/QUIESCE, disable remote wakeups so no interrupts get
15389671d550SEvan Green 	 * generated.
15395f677f1dSAlan Stern 	 */
15405f677f1dSAlan Stern 	if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
15419671d550SEvan Green 		w = 0;
15425f677f1dSAlan Stern 
15439671d550SEvan Green 	} else {
15449671d550SEvan Green 		/*
15459671d550SEvan Green 		 * Enable remote wakeup if it is allowed, even if no interface
15469671d550SEvan Green 		 * drivers actually want it.
15475f677f1dSAlan Stern 		 */
154848826626SAlan Stern 		w = device_may_wakeup(&udev->dev);
15499671d550SEvan Green 	}
15505f677f1dSAlan Stern 
15519671d550SEvan Green 	/*
15529671d550SEvan Green 	 * If the device is autosuspended with the wrong wakeup setting,
15535f677f1dSAlan Stern 	 * autoresume now so the setting can be changed.
15545f677f1dSAlan Stern 	 */
15555f677f1dSAlan Stern 	if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
15565f677f1dSAlan Stern 		pm_runtime_resume(&udev->dev);
15575f677f1dSAlan Stern 	udev->do_remote_wakeup = w;
15585f677f1dSAlan Stern }
15595f677f1dSAlan Stern 
15609bbdf1e0SAlan Stern /* The device lock is held by the PM core */
usb_suspend(struct device * dev,pm_message_t msg)15610c590e23SAlan Stern int usb_suspend(struct device *dev, pm_message_t msg)
15620c590e23SAlan Stern {
15639bbdf1e0SAlan Stern 	struct usb_device	*udev = to_usb_device(dev);
15648dd8d2c9SDaniel Drake 	int r;
15650c590e23SAlan Stern 
15661493138aSOliver Neukum 	unbind_no_pm_drivers_interfaces(udev);
15671493138aSOliver Neukum 
15681493138aSOliver Neukum 	/* From now on we are sure all drivers support suspend/resume
15691493138aSOliver Neukum 	 * but not necessarily reset_resume()
15701493138aSOliver Neukum 	 * so we may still need to unbind and rebind upon resume
15711493138aSOliver Neukum 	 */
15725f677f1dSAlan Stern 	choose_wakeup(udev, msg);
15738dd8d2c9SDaniel Drake 	r = usb_suspend_both(udev, msg);
15748dd8d2c9SDaniel Drake 	if (r)
15758dd8d2c9SDaniel Drake 		return r;
15768dd8d2c9SDaniel Drake 
15778dd8d2c9SDaniel Drake 	if (udev->quirks & USB_QUIRK_DISCONNECT_SUSPEND)
15788dd8d2c9SDaniel Drake 		usb_port_disable(udev);
15798dd8d2c9SDaniel Drake 
15808dd8d2c9SDaniel Drake 	return 0;
15810c590e23SAlan Stern }
15820c590e23SAlan Stern 
15839bbdf1e0SAlan Stern /* The device lock is held by the PM core */
usb_resume_complete(struct device * dev)158498d9a82eSOliver Neukum int usb_resume_complete(struct device *dev)
158598d9a82eSOliver Neukum {
158698d9a82eSOliver Neukum 	struct usb_device *udev = to_usb_device(dev);
158798d9a82eSOliver Neukum 
158898d9a82eSOliver Neukum 	/* For PM complete calls, all we do is rebind interfaces
158998d9a82eSOliver Neukum 	 * whose needs_binding flag is set
159098d9a82eSOliver Neukum 	 */
159198d9a82eSOliver Neukum 	if (udev->state != USB_STATE_NOTATTACHED)
15926aec044cSAlan Stern 		rebind_marked_interfaces(udev);
159398d9a82eSOliver Neukum 	return 0;
159498d9a82eSOliver Neukum }
159598d9a82eSOliver Neukum 
159698d9a82eSOliver Neukum /* The device lock is held by the PM core */
usb_resume(struct device * dev,pm_message_t msg)15970c590e23SAlan Stern int usb_resume(struct device *dev, pm_message_t msg)
15980c590e23SAlan Stern {
15999bbdf1e0SAlan Stern 	struct usb_device	*udev = to_usb_device(dev);
16000c590e23SAlan Stern 	int			status;
16010c590e23SAlan Stern 
160298d9a82eSOliver Neukum 	/* For all calls, take the device back to full power and
16039bbdf1e0SAlan Stern 	 * tell the PM core in case it was autosuspended previously.
16041493138aSOliver Neukum 	 * Unbind the interfaces that will need rebinding later,
16051493138aSOliver Neukum 	 * because they fail to support reset_resume.
16061493138aSOliver Neukum 	 * (This can't be done in usb_resume_interface()
16071493138aSOliver Neukum 	 * above because it doesn't own the right set of locks.)
16080c590e23SAlan Stern 	 */
16099bbdf1e0SAlan Stern 	status = usb_resume_both(udev, msg);
16109bbdf1e0SAlan Stern 	if (status == 0) {
16119bbdf1e0SAlan Stern 		pm_runtime_disable(dev);
16129bbdf1e0SAlan Stern 		pm_runtime_set_active(dev);
16139bbdf1e0SAlan Stern 		pm_runtime_enable(dev);
16146aec044cSAlan Stern 		unbind_marked_interfaces(udev);
16159bbdf1e0SAlan Stern 	}
16160c590e23SAlan Stern 
16170c590e23SAlan Stern 	/* Avoid PM error messages for devices disconnected while suspended
16180c590e23SAlan Stern 	 * as we'll display regular disconnect messages just a bit later.
16190c590e23SAlan Stern 	 */
16207491f133SPeter Chen 	if (status == -ENODEV || status == -ESHUTDOWN)
16219bbdf1e0SAlan Stern 		status = 0;
16220c590e23SAlan Stern 	return status;
16230c590e23SAlan Stern }
16240c590e23SAlan Stern 
1625088f7fecSAlan Stern /**
1626088f7fecSAlan Stern  * usb_enable_autosuspend - allow a USB device to be autosuspended
1627088f7fecSAlan Stern  * @udev: the USB device which may be autosuspended
1628088f7fecSAlan Stern  *
1629088f7fecSAlan Stern  * This routine allows @udev to be autosuspended.  An autosuspend won't
1630088f7fecSAlan Stern  * take place until the autosuspend_delay has elapsed and all the other
1631088f7fecSAlan Stern  * necessary conditions are satisfied.
1632088f7fecSAlan Stern  *
1633088f7fecSAlan Stern  * The caller must hold @udev's device lock.
1634088f7fecSAlan Stern  */
usb_enable_autosuspend(struct usb_device * udev)16359e18c821SAlan Stern void usb_enable_autosuspend(struct usb_device *udev)
1636088f7fecSAlan Stern {
16379e18c821SAlan Stern 	pm_runtime_allow(&udev->dev);
1638088f7fecSAlan Stern }
1639088f7fecSAlan Stern EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1640088f7fecSAlan Stern 
1641088f7fecSAlan Stern /**
1642088f7fecSAlan Stern  * usb_disable_autosuspend - prevent a USB device from being autosuspended
1643088f7fecSAlan Stern  * @udev: the USB device which may not be autosuspended
1644088f7fecSAlan Stern  *
1645088f7fecSAlan Stern  * This routine prevents @udev from being autosuspended and wakes it up
1646088f7fecSAlan Stern  * if it is already autosuspended.
1647088f7fecSAlan Stern  *
1648088f7fecSAlan Stern  * The caller must hold @udev's device lock.
1649088f7fecSAlan Stern  */
usb_disable_autosuspend(struct usb_device * udev)16509e18c821SAlan Stern void usb_disable_autosuspend(struct usb_device *udev)
1651088f7fecSAlan Stern {
16529e18c821SAlan Stern 	pm_runtime_forbid(&udev->dev);
1653088f7fecSAlan Stern }
1654088f7fecSAlan Stern EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1655088f7fecSAlan Stern 
1656645daaabSAlan Stern /**
1657645daaabSAlan Stern  * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1658701f35afSHenrik Kretzschmar  * @udev: the usb_device to autosuspend
1659645daaabSAlan Stern  *
1660645daaabSAlan Stern  * This routine should be called when a core subsystem is finished using
1661645daaabSAlan Stern  * @udev and wants to allow it to autosuspend.  Examples would be when
1662645daaabSAlan Stern  * @udev's device file in usbfs is closed or after a configuration change.
1663645daaabSAlan Stern  *
16649bbdf1e0SAlan Stern  * @udev's usage counter is decremented; if it drops to 0 and all the
16659bbdf1e0SAlan Stern  * interfaces are inactive then a delayed autosuspend will be attempted.
16669bbdf1e0SAlan Stern  * The attempt may fail (see autosuspend_check()).
1667645daaabSAlan Stern  *
166862e299e6SAlan Stern  * The caller must hold @udev's device lock.
1669645daaabSAlan Stern  *
1670645daaabSAlan Stern  * This routine can run only in process context.
1671645daaabSAlan Stern  */
usb_autosuspend_device(struct usb_device * udev)167294fcda1fSAlan Stern void usb_autosuspend_device(struct usb_device *udev)
1673645daaabSAlan Stern {
167494fcda1fSAlan Stern 	int	status;
167594fcda1fSAlan Stern 
16766ddf27cdSMing Lei 	usb_mark_last_busy(udev);
1677fcc4a01eSAlan Stern 	status = pm_runtime_put_sync_autosuspend(&udev->dev);
16789bbdf1e0SAlan Stern 	dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
16799bbdf1e0SAlan Stern 			__func__, atomic_read(&udev->dev.power.usage_count),
16809bbdf1e0SAlan Stern 			status);
168119c26239SAlan Stern }
168219c26239SAlan Stern 
168319c26239SAlan Stern /**
1684645daaabSAlan Stern  * usb_autoresume_device - immediately autoresume a USB device and its interfaces
1685701f35afSHenrik Kretzschmar  * @udev: the usb_device to autoresume
1686645daaabSAlan Stern  *
1687645daaabSAlan Stern  * This routine should be called when a core subsystem wants to use @udev
168894fcda1fSAlan Stern  * and needs to guarantee that it is not suspended.  No autosuspend will
16899bbdf1e0SAlan Stern  * occur until usb_autosuspend_device() is called.  (Note that this will
16909bbdf1e0SAlan Stern  * not prevent suspend events originating in the PM core.)  Examples would
16919bbdf1e0SAlan Stern  * be when @udev's device file in usbfs is opened or when a remote-wakeup
169294fcda1fSAlan Stern  * request is received.
1693645daaabSAlan Stern  *
169494fcda1fSAlan Stern  * @udev's usage counter is incremented to prevent subsequent autosuspends.
169594fcda1fSAlan Stern  * However if the autoresume fails then the usage counter is re-decremented.
1696645daaabSAlan Stern  *
169762e299e6SAlan Stern  * The caller must hold @udev's device lock.
1698645daaabSAlan Stern  *
1699645daaabSAlan Stern  * This routine can run only in process context.
1700626f090cSYacine Belkadi  *
1701626f090cSYacine Belkadi  * Return: 0 on success. A negative error code otherwise.
1702645daaabSAlan Stern  */
usb_autoresume_device(struct usb_device * udev)170394fcda1fSAlan Stern int usb_autoresume_device(struct usb_device *udev)
1704645daaabSAlan Stern {
1705645daaabSAlan Stern 	int	status;
1706645daaabSAlan Stern 
17079bbdf1e0SAlan Stern 	status = pm_runtime_get_sync(&udev->dev);
17089bbdf1e0SAlan Stern 	if (status < 0)
17099bbdf1e0SAlan Stern 		pm_runtime_put_sync(&udev->dev);
17109bbdf1e0SAlan Stern 	dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
17119bbdf1e0SAlan Stern 			__func__, atomic_read(&udev->dev.power.usage_count),
17129bbdf1e0SAlan Stern 			status);
17139bbdf1e0SAlan Stern 	if (status > 0)
17149bbdf1e0SAlan Stern 		status = 0;
1715af4f7606SAlan Stern 	return status;
1716af4f7606SAlan Stern }
1717af4f7606SAlan Stern 
1718645daaabSAlan Stern /**
1719645daaabSAlan Stern  * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
1720701f35afSHenrik Kretzschmar  * @intf: the usb_interface whose counter should be decremented
1721645daaabSAlan Stern  *
1722645daaabSAlan Stern  * This routine should be called by an interface driver when it is
1723645daaabSAlan Stern  * finished using @intf and wants to allow it to autosuspend.  A typical
1724645daaabSAlan Stern  * example would be a character-device driver when its device file is
1725645daaabSAlan Stern  * closed.
1726645daaabSAlan Stern  *
1727645daaabSAlan Stern  * The routine decrements @intf's usage counter.  When the counter reaches
17289bbdf1e0SAlan Stern  * 0, a delayed autosuspend request for @intf's device is attempted.  The
17299bbdf1e0SAlan Stern  * attempt may fail (see autosuspend_check()).
1730645daaabSAlan Stern  *
1731645daaabSAlan Stern  * This routine can run only in process context.
1732645daaabSAlan Stern  */
usb_autopm_put_interface(struct usb_interface * intf)1733645daaabSAlan Stern void usb_autopm_put_interface(struct usb_interface *intf)
1734645daaabSAlan Stern {
17359bbdf1e0SAlan Stern 	struct usb_device	*udev = interface_to_usbdev(intf);
1736af4f7606SAlan Stern 	int			status;
1737645daaabSAlan Stern 
17386ddf27cdSMing Lei 	usb_mark_last_busy(udev);
17399bbdf1e0SAlan Stern 	status = pm_runtime_put_sync(&intf->dev);
17409bbdf1e0SAlan Stern 	dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
17419bbdf1e0SAlan Stern 			__func__, atomic_read(&intf->dev.power.usage_count),
17429bbdf1e0SAlan Stern 			status);
1743645daaabSAlan Stern }
1744645daaabSAlan Stern EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1745645daaabSAlan Stern 
1746645daaabSAlan Stern /**
17479ac39f28SAlan Stern  * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
17489ac39f28SAlan Stern  * @intf: the usb_interface whose counter should be decremented
17499ac39f28SAlan Stern  *
17509bbdf1e0SAlan Stern  * This routine does much the same thing as usb_autopm_put_interface():
17519bbdf1e0SAlan Stern  * It decrements @intf's usage counter and schedules a delayed
17529bbdf1e0SAlan Stern  * autosuspend request if the counter is <= 0.  The difference is that it
17539bbdf1e0SAlan Stern  * does not perform any synchronization; callers should hold a private
17549bbdf1e0SAlan Stern  * lock and handle all synchronization issues themselves.
17559ac39f28SAlan Stern  *
17569ac39f28SAlan Stern  * Typically a driver would call this routine during an URB's completion
17579ac39f28SAlan Stern  * handler, if no more URBs were pending.
17589ac39f28SAlan Stern  *
17599ac39f28SAlan Stern  * This routine can run in atomic context.
17609ac39f28SAlan Stern  */
usb_autopm_put_interface_async(struct usb_interface * intf)17619ac39f28SAlan Stern void usb_autopm_put_interface_async(struct usb_interface *intf)
17629ac39f28SAlan Stern {
17639ac39f28SAlan Stern 	struct usb_device	*udev = interface_to_usbdev(intf);
1764fcc4a01eSAlan Stern 	int			status;
17659ac39f28SAlan Stern 
17666ddf27cdSMing Lei 	usb_mark_last_busy(udev);
1767fcc4a01eSAlan Stern 	status = pm_runtime_put(&intf->dev);
17689bbdf1e0SAlan Stern 	dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
17699bbdf1e0SAlan Stern 			__func__, atomic_read(&intf->dev.power.usage_count),
17709bbdf1e0SAlan Stern 			status);
17719ac39f28SAlan Stern }
17729ac39f28SAlan Stern EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
17739ac39f28SAlan Stern 
17749ac39f28SAlan Stern /**
17759bbdf1e0SAlan Stern  * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
17769bbdf1e0SAlan Stern  * @intf: the usb_interface whose counter should be decremented
17779bbdf1e0SAlan Stern  *
17789bbdf1e0SAlan Stern  * This routine decrements @intf's usage counter but does not carry out an
17799bbdf1e0SAlan Stern  * autosuspend.
17809bbdf1e0SAlan Stern  *
17819bbdf1e0SAlan Stern  * This routine can run in atomic context.
17829bbdf1e0SAlan Stern  */
usb_autopm_put_interface_no_suspend(struct usb_interface * intf)17839bbdf1e0SAlan Stern void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
17849bbdf1e0SAlan Stern {
17859bbdf1e0SAlan Stern 	struct usb_device	*udev = interface_to_usbdev(intf);
17869bbdf1e0SAlan Stern 
17876ddf27cdSMing Lei 	usb_mark_last_busy(udev);
17889bbdf1e0SAlan Stern 	pm_runtime_put_noidle(&intf->dev);
17899bbdf1e0SAlan Stern }
17909bbdf1e0SAlan Stern EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
17919bbdf1e0SAlan Stern 
17929bbdf1e0SAlan Stern /**
1793645daaabSAlan Stern  * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1794701f35afSHenrik Kretzschmar  * @intf: the usb_interface whose counter should be incremented
1795645daaabSAlan Stern  *
1796645daaabSAlan Stern  * This routine should be called by an interface driver when it wants to
1797645daaabSAlan Stern  * use @intf and needs to guarantee that it is not suspended.  In addition,
1798645daaabSAlan Stern  * the routine prevents @intf from being autosuspended subsequently.  (Note
1799645daaabSAlan Stern  * that this will not prevent suspend events originating in the PM core.)
1800645daaabSAlan Stern  * This prevention will persist until usb_autopm_put_interface() is called
1801645daaabSAlan Stern  * or @intf is unbound.  A typical example would be a character-device
1802645daaabSAlan Stern  * driver when its device file is opened.
1803645daaabSAlan Stern  *
18049bbdf1e0SAlan Stern  * @intf's usage counter is incremented to prevent subsequent autosuspends.
18059bbdf1e0SAlan Stern  * However if the autoresume fails then the counter is re-decremented.
1806645daaabSAlan Stern  *
1807645daaabSAlan Stern  * This routine can run only in process context.
1808626f090cSYacine Belkadi  *
1809626f090cSYacine Belkadi  * Return: 0 on success.
1810645daaabSAlan Stern  */
usb_autopm_get_interface(struct usb_interface * intf)1811645daaabSAlan Stern int usb_autopm_get_interface(struct usb_interface *intf)
1812645daaabSAlan Stern {
1813645daaabSAlan Stern 	int	status;
1814645daaabSAlan Stern 
18159bbdf1e0SAlan Stern 	status = pm_runtime_get_sync(&intf->dev);
18169bbdf1e0SAlan Stern 	if (status < 0)
18179bbdf1e0SAlan Stern 		pm_runtime_put_sync(&intf->dev);
18189bbdf1e0SAlan Stern 	dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
18199bbdf1e0SAlan Stern 			__func__, atomic_read(&intf->dev.power.usage_count),
18209bbdf1e0SAlan Stern 			status);
18219bbdf1e0SAlan Stern 	if (status > 0)
18229bbdf1e0SAlan Stern 		status = 0;
1823645daaabSAlan Stern 	return status;
1824645daaabSAlan Stern }
1825645daaabSAlan Stern EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1826645daaabSAlan Stern 
1827692a186cSAlan Stern /**
18289ac39f28SAlan Stern  * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
18299ac39f28SAlan Stern  * @intf: the usb_interface whose counter should be incremented
18309ac39f28SAlan Stern  *
18319ac39f28SAlan Stern  * This routine does much the same thing as
18329bbdf1e0SAlan Stern  * usb_autopm_get_interface(): It increments @intf's usage counter and
18339bbdf1e0SAlan Stern  * queues an autoresume request if the device is suspended.  The
18349bbdf1e0SAlan Stern  * differences are that it does not perform any synchronization (callers
18359bbdf1e0SAlan Stern  * should hold a private lock and handle all synchronization issues
18369bbdf1e0SAlan Stern  * themselves), and it does not autoresume the device directly (it only
18379bbdf1e0SAlan Stern  * queues a request).  After a successful call, the device may not yet be
18389bbdf1e0SAlan Stern  * resumed.
18399ac39f28SAlan Stern  *
18409ac39f28SAlan Stern  * This routine can run in atomic context.
1841626f090cSYacine Belkadi  *
1842626f090cSYacine Belkadi  * Return: 0 on success. A negative error code otherwise.
18439ac39f28SAlan Stern  */
usb_autopm_get_interface_async(struct usb_interface * intf)18449ac39f28SAlan Stern int usb_autopm_get_interface_async(struct usb_interface *intf)
18459ac39f28SAlan Stern {
184663defa73SMing Lei 	int	status;
18479ac39f28SAlan Stern 
184863defa73SMing Lei 	status = pm_runtime_get(&intf->dev);
18499bbdf1e0SAlan Stern 	if (status < 0 && status != -EINPROGRESS)
18509bbdf1e0SAlan Stern 		pm_runtime_put_noidle(&intf->dev);
18519bbdf1e0SAlan Stern 	dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
18529bbdf1e0SAlan Stern 			__func__, atomic_read(&intf->dev.power.usage_count),
18539bbdf1e0SAlan Stern 			status);
1854c5a48592SJim Wylder 	if (status > 0 || status == -EINPROGRESS)
18559bbdf1e0SAlan Stern 		status = 0;
18569ac39f28SAlan Stern 	return status;
18579ac39f28SAlan Stern }
18589ac39f28SAlan Stern EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
18599ac39f28SAlan Stern 
18609bbdf1e0SAlan Stern /**
18619bbdf1e0SAlan Stern  * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
18629bbdf1e0SAlan Stern  * @intf: the usb_interface whose counter should be incremented
18639bbdf1e0SAlan Stern  *
18649bbdf1e0SAlan Stern  * This routine increments @intf's usage counter but does not carry out an
18659bbdf1e0SAlan Stern  * autoresume.
18669bbdf1e0SAlan Stern  *
18679bbdf1e0SAlan Stern  * This routine can run in atomic context.
18689bbdf1e0SAlan Stern  */
usb_autopm_get_interface_no_resume(struct usb_interface * intf)18699bbdf1e0SAlan Stern void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
18709bbdf1e0SAlan Stern {
18719bbdf1e0SAlan Stern 	struct usb_device	*udev = interface_to_usbdev(intf);
18729bbdf1e0SAlan Stern 
18736ddf27cdSMing Lei 	usb_mark_last_busy(udev);
18749bbdf1e0SAlan Stern 	pm_runtime_get_noresume(&intf->dev);
18759bbdf1e0SAlan Stern }
18769bbdf1e0SAlan Stern EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
18779bbdf1e0SAlan Stern 
18789bbdf1e0SAlan Stern /* Internal routine to check whether we may autosuspend a device. */
autosuspend_check(struct usb_device * udev)18799bbdf1e0SAlan Stern static int autosuspend_check(struct usb_device *udev)
18809bbdf1e0SAlan Stern {
18817560d32eSAlan Stern 	int			w, i;
18829bbdf1e0SAlan Stern 	struct usb_interface	*intf;
18839bbdf1e0SAlan Stern 
1884f5cccf49SGuenter Roeck 	if (udev->state == USB_STATE_NOTATTACHED)
1885f5cccf49SGuenter Roeck 		return -ENODEV;
1886f5cccf49SGuenter Roeck 
18879bbdf1e0SAlan Stern 	/* Fail if autosuspend is disabled, or any interfaces are in use, or
18889bbdf1e0SAlan Stern 	 * any interface drivers require remote wakeup but it isn't available.
18899bbdf1e0SAlan Stern 	 */
18907560d32eSAlan Stern 	w = 0;
18919bbdf1e0SAlan Stern 	if (udev->actconfig) {
18929bbdf1e0SAlan Stern 		for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
18939bbdf1e0SAlan Stern 			intf = udev->actconfig->interface[i];
18949bbdf1e0SAlan Stern 
18959bbdf1e0SAlan Stern 			/* We don't need to check interfaces that are
18969bbdf1e0SAlan Stern 			 * disabled for runtime PM.  Either they are unbound
18979bbdf1e0SAlan Stern 			 * or else their drivers don't support autosuspend
18989bbdf1e0SAlan Stern 			 * and so they are permanently active.
18999bbdf1e0SAlan Stern 			 */
19009bbdf1e0SAlan Stern 			if (intf->dev.power.disable_depth)
19019bbdf1e0SAlan Stern 				continue;
19029bbdf1e0SAlan Stern 			if (atomic_read(&intf->dev.power.usage_count) > 0)
19039bbdf1e0SAlan Stern 				return -EBUSY;
19047560d32eSAlan Stern 			w |= intf->needs_remote_wakeup;
19059bbdf1e0SAlan Stern 
19069bbdf1e0SAlan Stern 			/* Don't allow autosuspend if the device will need
19079bbdf1e0SAlan Stern 			 * a reset-resume and any of its interface drivers
19089bbdf1e0SAlan Stern 			 * doesn't include support or needs remote wakeup.
19099bbdf1e0SAlan Stern 			 */
19109bbdf1e0SAlan Stern 			if (udev->quirks & USB_QUIRK_RESET_RESUME) {
19119bbdf1e0SAlan Stern 				struct usb_driver *driver;
19129bbdf1e0SAlan Stern 
19139bbdf1e0SAlan Stern 				driver = to_usb_driver(intf->dev.driver);
19149bbdf1e0SAlan Stern 				if (!driver->reset_resume ||
19159bbdf1e0SAlan Stern 						intf->needs_remote_wakeup)
19169bbdf1e0SAlan Stern 					return -EOPNOTSUPP;
19179bbdf1e0SAlan Stern 			}
19189bbdf1e0SAlan Stern 		}
19199bbdf1e0SAlan Stern 	}
19207560d32eSAlan Stern 	if (w && !device_can_wakeup(&udev->dev)) {
19217560d32eSAlan Stern 		dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
19227560d32eSAlan Stern 		return -EOPNOTSUPP;
19237560d32eSAlan Stern 	}
1924074f9dd5SAlan Stern 
1925074f9dd5SAlan Stern 	/*
1926074f9dd5SAlan Stern 	 * If the device is a direct child of the root hub and the HCD
1927074f9dd5SAlan Stern 	 * doesn't handle wakeup requests, don't allow autosuspend when
1928074f9dd5SAlan Stern 	 * wakeup is needed.
1929074f9dd5SAlan Stern 	 */
1930074f9dd5SAlan Stern 	if (w && udev->parent == udev->bus->root_hub &&
1931074f9dd5SAlan Stern 			bus_to_hcd(udev->bus)->cant_recv_wakeups) {
1932074f9dd5SAlan Stern 		dev_dbg(&udev->dev, "HCD doesn't handle wakeup requests\n");
1933074f9dd5SAlan Stern 		return -EOPNOTSUPP;
1934074f9dd5SAlan Stern 	}
1935074f9dd5SAlan Stern 
19367560d32eSAlan Stern 	udev->do_remote_wakeup = w;
19379bbdf1e0SAlan Stern 	return 0;
19389bbdf1e0SAlan Stern }
19399bbdf1e0SAlan Stern 
usb_runtime_suspend(struct device * dev)1940e1620d59SRafael J. Wysocki int usb_runtime_suspend(struct device *dev)
19419bbdf1e0SAlan Stern {
194263defa73SMing Lei 	struct usb_device	*udev = to_usb_device(dev);
194363defa73SMing Lei 	int			status;
19449bbdf1e0SAlan Stern 
19459bbdf1e0SAlan Stern 	/* A USB device can be suspended if it passes the various autosuspend
19469bbdf1e0SAlan Stern 	 * checks.  Runtime suspend for a USB device means suspending all the
19479bbdf1e0SAlan Stern 	 * interfaces and then the device itself.
19489bbdf1e0SAlan Stern 	 */
19499bbdf1e0SAlan Stern 	if (autosuspend_check(udev) != 0)
19509bbdf1e0SAlan Stern 		return -EAGAIN;
19519bbdf1e0SAlan Stern 
19529bbdf1e0SAlan Stern 	status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1953b2c0a863SAlan Stern 
1954b2c0a863SAlan Stern 	/* Allow a retry if autosuspend failed temporarily */
1955b2c0a863SAlan Stern 	if (status == -EAGAIN || status == -EBUSY)
1956b2c0a863SAlan Stern 		usb_mark_last_busy(udev);
1957b2c0a863SAlan Stern 
19588ef42dddSAlan Stern 	/*
19598ef42dddSAlan Stern 	 * The PM core reacts badly unless the return code is 0,
19608ef42dddSAlan Stern 	 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
19618ef42dddSAlan Stern 	 * (except for root hubs, because they don't suspend through
19628ef42dddSAlan Stern 	 * an upstream port like other USB devices).
1963db7c7c0aSSarah Sharp 	 */
19648ef42dddSAlan Stern 	if (status != 0 && udev->parent)
1965db7c7c0aSSarah Sharp 		return -EBUSY;
19669bbdf1e0SAlan Stern 	return status;
19679bbdf1e0SAlan Stern }
19689bbdf1e0SAlan Stern 
usb_runtime_resume(struct device * dev)1969e1620d59SRafael J. Wysocki int usb_runtime_resume(struct device *dev)
19709bbdf1e0SAlan Stern {
19719bbdf1e0SAlan Stern 	struct usb_device	*udev = to_usb_device(dev);
19729bbdf1e0SAlan Stern 	int			status;
19739bbdf1e0SAlan Stern 
197463defa73SMing Lei 	/* Runtime resume for a USB device means resuming both the device
197563defa73SMing Lei 	 * and all its interfaces.
197663defa73SMing Lei 	 */
19779bbdf1e0SAlan Stern 	status = usb_resume_both(udev, PMSG_AUTO_RESUME);
19789bbdf1e0SAlan Stern 	return status;
19799bbdf1e0SAlan Stern }
19809bbdf1e0SAlan Stern 
usb_runtime_idle(struct device * dev)1981e1620d59SRafael J. Wysocki int usb_runtime_idle(struct device *dev)
19829bbdf1e0SAlan Stern {
19839bbdf1e0SAlan Stern 	struct usb_device	*udev = to_usb_device(dev);
19849bbdf1e0SAlan Stern 
198563defa73SMing Lei 	/* An idle USB device can be suspended if it passes the various
198663defa73SMing Lei 	 * autosuspend checks.
198763defa73SMing Lei 	 */
198863defa73SMing Lei 	if (autosuspend_check(udev) == 0)
1989fcc4a01eSAlan Stern 		pm_runtime_autosuspend(dev);
199045f0a85cSRafael J. Wysocki 	/* Tell the core not to suspend it, though. */
199145f0a85cSRafael J. Wysocki 	return -EBUSY;
19929bbdf1e0SAlan Stern }
19939bbdf1e0SAlan Stern 
usb_set_usb2_hardware_lpm(struct usb_device * udev,int enable)19947529b257SKai-Heng Feng static int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
199565580b43SAndiry Xu {
199665580b43SAndiry Xu 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
199765580b43SAndiry Xu 	int ret = -EPERM;
199865580b43SAndiry Xu 
199965580b43SAndiry Xu 	if (hcd->driver->set_usb2_hw_lpm) {
200065580b43SAndiry Xu 		ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
200165580b43SAndiry Xu 		if (!ret)
200265580b43SAndiry Xu 			udev->usb2_hw_lpm_enabled = enable;
200365580b43SAndiry Xu 	}
200465580b43SAndiry Xu 
200565580b43SAndiry Xu 	return ret;
200665580b43SAndiry Xu }
200765580b43SAndiry Xu 
usb_enable_usb2_hardware_lpm(struct usb_device * udev)20087529b257SKai-Heng Feng int usb_enable_usb2_hardware_lpm(struct usb_device *udev)
20097529b257SKai-Heng Feng {
2010d7a6c0ceSKai-Heng Feng 	if (!udev->usb2_hw_lpm_capable ||
2011d7a6c0ceSKai-Heng Feng 	    !udev->usb2_hw_lpm_allowed ||
2012d7a6c0ceSKai-Heng Feng 	    udev->usb2_hw_lpm_enabled)
2013d7a6c0ceSKai-Heng Feng 		return 0;
2014d7a6c0ceSKai-Heng Feng 
20157529b257SKai-Heng Feng 	return usb_set_usb2_hardware_lpm(udev, 1);
20167529b257SKai-Heng Feng }
20177529b257SKai-Heng Feng 
usb_disable_usb2_hardware_lpm(struct usb_device * udev)20187529b257SKai-Heng Feng int usb_disable_usb2_hardware_lpm(struct usb_device *udev)
20197529b257SKai-Heng Feng {
2020d7a6c0ceSKai-Heng Feng 	if (!udev->usb2_hw_lpm_enabled)
2021d7a6c0ceSKai-Heng Feng 		return 0;
2022d7a6c0ceSKai-Heng Feng 
20237529b257SKai-Heng Feng 	return usb_set_usb2_hardware_lpm(udev, 0);
20247529b257SKai-Heng Feng }
20257529b257SKai-Heng Feng 
2026ceb6c9c8SRafael J. Wysocki #endif /* CONFIG_PM */
2027645daaabSAlan Stern 
2028*9d11b134SGreg Kroah-Hartman const struct bus_type usb_bus_type = {
202936e56a34SAlan Stern 	.name =		"usb",
203036e56a34SAlan Stern 	.match =	usb_device_match,
203136e56a34SAlan Stern 	.uevent =	usb_uevent,
20328c97a46aSMartin Liu 	.need_parent_lock =	true,
203336e56a34SAlan Stern };
2034