generic.c 8.9 KB

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