generic.c 9.3 KB

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