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