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