xref: /linux/drivers/usb/core/generic.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * drivers/usb/core/generic.c - generic driver for USB devices (not interfaces)
4  *
5  * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
6  *
7  * based on drivers/usb/usb.c which had the following copyrights:
8  *	(C) Copyright Linus Torvalds 1999
9  *	(C) Copyright Johannes Erdfelt 1999-2001
10  *	(C) Copyright Andreas Gal 1999
11  *	(C) Copyright Gregory P. Smith 1999
12  *	(C) Copyright Deti Fliegl 1999 (new USB architecture)
13  *	(C) Copyright Randy Dunlap 2000
14  *	(C) Copyright David Brownell 2000-2004
15  *	(C) Copyright Yggdrasil Computing, Inc. 2000
16  *		(usb_device_id matching changes by Adam J. Richter)
17  *	(C) Copyright Greg Kroah-Hartman 2002-2003
18  *
19  * Released under the GPLv2 only.
20  */
21 
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
24 #include <uapi/linux/usb/audio.h>
25 #include "usb.h"
26 
27 static inline const char *plural(int n)
28 {
29 	return (n == 1 ? "" : "s");
30 }
31 
32 static int is_rndis(struct usb_interface_descriptor *desc)
33 {
34 	return desc->bInterfaceClass == USB_CLASS_COMM
35 		&& desc->bInterfaceSubClass == 2
36 		&& desc->bInterfaceProtocol == 0xff;
37 }
38 
39 static int is_activesync(struct usb_interface_descriptor *desc)
40 {
41 	return desc->bInterfaceClass == USB_CLASS_MISC
42 		&& desc->bInterfaceSubClass == 1
43 		&& desc->bInterfaceProtocol == 1;
44 }
45 
46 static bool is_audio(struct usb_interface_descriptor *desc)
47 {
48 	return desc->bInterfaceClass == USB_CLASS_AUDIO;
49 }
50 
51 static bool is_uac3_config(struct usb_interface_descriptor *desc)
52 {
53 	return desc->bInterfaceProtocol == UAC_VERSION_3;
54 }
55 
56 int usb_choose_configuration(struct usb_device *udev)
57 {
58 	int i;
59 	int num_configs;
60 	int insufficient_power = 0;
61 	struct usb_host_config *c, *best;
62 
63 	if (usb_device_is_owned(udev))
64 		return 0;
65 
66 	best = NULL;
67 	c = udev->config;
68 	num_configs = udev->descriptor.bNumConfigurations;
69 	for (i = 0; i < num_configs; (i++, c++)) {
70 		struct usb_interface_descriptor	*desc = NULL;
71 
72 		/* It's possible that a config has no interfaces! */
73 		if (c->desc.bNumInterfaces > 0)
74 			desc = &c->intf_cache[0]->altsetting->desc;
75 
76 		/*
77 		 * HP's USB bus-powered keyboard has only one configuration
78 		 * and it claims to be self-powered; other devices may have
79 		 * similar errors in their descriptors.  If the next test
80 		 * were allowed to execute, such configurations would always
81 		 * be rejected and the devices would not work as expected.
82 		 * In the meantime, we run the risk of selecting a config
83 		 * that requires external power at a time when that power
84 		 * isn't available.  It seems to be the lesser of two evils.
85 		 *
86 		 * Bugzilla #6448 reports a device that appears to crash
87 		 * when it receives a GET_DEVICE_STATUS request!  We don't
88 		 * have any other way to tell whether a device is self-powered,
89 		 * but since we don't use that information anywhere but here,
90 		 * the call has been removed.
91 		 *
92 		 * Maybe the GET_DEVICE_STATUS call and the test below can
93 		 * be reinstated when device firmwares become more reliable.
94 		 * Don't hold your breath.
95 		 */
96 #if 0
97 		/* Rule out self-powered configs for a bus-powered device */
98 		if (bus_powered && (c->desc.bmAttributes &
99 					USB_CONFIG_ATT_SELFPOWER))
100 			continue;
101 #endif
102 
103 		/*
104 		 * The next test may not be as effective as it should be.
105 		 * Some hubs have errors in their descriptor, claiming
106 		 * to be self-powered when they are really bus-powered.
107 		 * We will overestimate the amount of current such hubs
108 		 * make available for each port.
109 		 *
110 		 * This is a fairly benign sort of failure.  It won't
111 		 * cause us to reject configurations that we should have
112 		 * accepted.
113 		 */
114 
115 		/* Rule out configs that draw too much bus current */
116 		if (usb_get_max_power(udev, c) > udev->bus_mA) {
117 			insufficient_power++;
118 			continue;
119 		}
120 
121 		/*
122 		 * Select first configuration as default for audio so that
123 		 * devices that don't comply with UAC3 protocol are supported.
124 		 * But, still iterate through other configurations and
125 		 * select UAC3 compliant config if present.
126 		 */
127 		if (desc && is_audio(desc)) {
128 			/* Always prefer the first found UAC3 config */
129 			if (is_uac3_config(desc)) {
130 				best = c;
131 				break;
132 			}
133 
134 			/* If there is no UAC3 config, prefer the first config */
135 			else if (i == 0)
136 				best = c;
137 
138 			/* Unconditional continue, because the rest of the code
139 			 * in the loop is irrelevant for audio devices, and
140 			 * because it can reassign best, which for audio devices
141 			 * we don't want.
142 			 */
143 			continue;
144 		}
145 
146 		/* When the first config's first interface is one of Microsoft's
147 		 * pet nonstandard Ethernet-over-USB protocols, ignore it unless
148 		 * this kernel has enabled the necessary host side driver.
149 		 * But: Don't ignore it if it's the only config.
150 		 */
151 		if (i == 0 && num_configs > 1 && desc &&
152 				(is_rndis(desc) || is_activesync(desc))) {
153 #if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
154 			continue;
155 #else
156 			best = c;
157 #endif
158 		}
159 
160 		/* From the remaining configs, choose the first one whose
161 		 * first interface is for a non-vendor-specific class.
162 		 * Reason: Linux is more likely to have a class driver
163 		 * than a vendor-specific driver. */
164 		else if (udev->descriptor.bDeviceClass !=
165 						USB_CLASS_VENDOR_SPEC &&
166 				(desc && desc->bInterfaceClass !=
167 						USB_CLASS_VENDOR_SPEC)) {
168 			best = c;
169 			break;
170 		}
171 
172 		/* If all the remaining configs are vendor-specific,
173 		 * choose the first one. */
174 		else if (!best)
175 			best = c;
176 	}
177 
178 	if (insufficient_power > 0)
179 		dev_info(&udev->dev, "rejected %d configuration%s "
180 			"due to insufficient available bus power\n",
181 			insufficient_power, plural(insufficient_power));
182 
183 	if (best) {
184 		i = best->desc.bConfigurationValue;
185 		dev_dbg(&udev->dev,
186 			"configuration #%d chosen from %d choice%s\n",
187 			i, num_configs, plural(num_configs));
188 	} else {
189 		i = -1;
190 		dev_warn(&udev->dev,
191 			"no configuration chosen from %d choice%s\n",
192 			num_configs, plural(num_configs));
193 	}
194 	return i;
195 }
196 EXPORT_SYMBOL_GPL(usb_choose_configuration);
197 
198 static int __check_for_non_generic_match(struct device_driver *drv, void *data)
199 {
200 	struct usb_device *udev = data;
201 	struct usb_device_driver *udrv;
202 
203 	if (!is_usb_device_driver(drv))
204 		return 0;
205 	udrv = to_usb_device_driver(drv);
206 	if (udrv == &usb_generic_driver)
207 		return 0;
208 	return usb_driver_applicable(udev, udrv);
209 }
210 
211 static bool usb_generic_driver_match(struct usb_device *udev)
212 {
213 	if (udev->use_generic_driver)
214 		return true;
215 
216 	/*
217 	 * If any other driver wants the device, leave the device to this other
218 	 * driver.
219 	 */
220 	if (bus_for_each_drv(&usb_bus_type, NULL, udev, __check_for_non_generic_match))
221 		return false;
222 
223 	return true;
224 }
225 
226 int usb_generic_driver_probe(struct usb_device *udev)
227 {
228 	int err, c;
229 
230 	/* Choose and set the configuration.  This registers the interfaces
231 	 * with the driver core and lets interface drivers bind to them.
232 	 */
233 	if (udev->authorized == 0)
234 		dev_err(&udev->dev, "Device is not authorized for usage\n");
235 	else {
236 		c = usb_choose_configuration(udev);
237 		if (c >= 0) {
238 			err = usb_set_configuration(udev, c);
239 			if (err && err != -ENODEV) {
240 				dev_err(&udev->dev, "can't set config #%d, error %d\n",
241 					c, err);
242 				/* This need not be fatal.  The user can try to
243 				 * set other configurations. */
244 			}
245 		}
246 	}
247 	/* USB device state == configured ... usable */
248 	usb_notify_add_device(udev);
249 
250 	return 0;
251 }
252 
253 void usb_generic_driver_disconnect(struct usb_device *udev)
254 {
255 	usb_notify_remove_device(udev);
256 
257 	/* if this is only an unbind, not a physical disconnect, then
258 	 * unconfigure the device */
259 	if (udev->actconfig)
260 		usb_set_configuration(udev, -1);
261 }
262 
263 #ifdef	CONFIG_PM
264 
265 int usb_generic_driver_suspend(struct usb_device *udev, pm_message_t msg)
266 {
267 	int rc;
268 
269 	/* Normal USB devices suspend through their upstream port.
270 	 * Root hubs don't have upstream ports to suspend,
271 	 * so we have to shut down their downstream HC-to-USB
272 	 * interfaces manually by doing a bus (or "global") suspend.
273 	 */
274 	if (!udev->parent)
275 		rc = hcd_bus_suspend(udev, msg);
276 
277 	/*
278 	 * Non-root USB2 devices don't need to do anything for FREEZE
279 	 * or PRETHAW. USB3 devices don't support global suspend and
280 	 * needs to be selectively suspended.
281 	 */
282 	else if ((msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_PRETHAW)
283 		 && (udev->speed < USB_SPEED_SUPER))
284 		rc = 0;
285 	else
286 		rc = usb_port_suspend(udev, msg);
287 
288 	if (rc == 0)
289 		usbfs_notify_suspend(udev);
290 	return rc;
291 }
292 
293 int usb_generic_driver_resume(struct usb_device *udev, pm_message_t msg)
294 {
295 	int rc;
296 
297 	/* Normal USB devices resume/reset through their upstream port.
298 	 * Root hubs don't have upstream ports to resume or reset,
299 	 * so we have to start up their downstream HC-to-USB
300 	 * interfaces manually by doing a bus (or "global") resume.
301 	 */
302 	if (!udev->parent)
303 		rc = hcd_bus_resume(udev, msg);
304 	else
305 		rc = usb_port_resume(udev, msg);
306 
307 	if (rc == 0)
308 		usbfs_notify_resume(udev);
309 	return rc;
310 }
311 
312 #endif	/* CONFIG_PM */
313 
314 struct usb_device_driver usb_generic_driver = {
315 	.name =	"usb",
316 	.match = usb_generic_driver_match,
317 	.probe = usb_generic_driver_probe,
318 	.disconnect = usb_generic_driver_disconnect,
319 #ifdef	CONFIG_PM
320 	.suspend = usb_generic_driver_suspend,
321 	.resume = usb_generic_driver_resume,
322 #endif
323 	.supports_autosuspend = 1,
324 };
325