generic.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /* When the first config's first interface is one of Microsoft's
  108. * pet nonstandard Ethernet-over-USB protocols, ignore it unless
  109. * this kernel has enabled the necessary host side driver.
  110. * But: Don't ignore it if it's the only config.
  111. */
  112. if (i == 0 && num_configs > 1 && desc &&
  113. (is_rndis(desc) || is_activesync(desc))) {
  114. #if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
  115. continue;
  116. #else
  117. best = c;
  118. #endif
  119. }
  120. /*
  121. * Select first configuration as default for audio so that
  122. * devices that don't comply with UAC3 protocol are supported.
  123. * But, still iterate through other configurations and
  124. * select UAC3 compliant config if present.
  125. */
  126. if (i == 0 && num_configs > 1 && desc && is_audio(desc)) {
  127. best = c;
  128. continue;
  129. }
  130. if (i > 0 && desc && is_audio(desc) && is_uac3_config(desc)) {
  131. best = c;
  132. break;
  133. }
  134. /* From the remaining configs, choose the first one whose
  135. * first interface is for a non-vendor-specific class.
  136. * Reason: Linux is more likely to have a class driver
  137. * than a vendor-specific driver. */
  138. else if (udev->descriptor.bDeviceClass !=
  139. USB_CLASS_VENDOR_SPEC &&
  140. (desc && desc->bInterfaceClass !=
  141. USB_CLASS_VENDOR_SPEC)) {
  142. best = c;
  143. break;
  144. }
  145. /* If all the remaining configs are vendor-specific,
  146. * choose the first one. */
  147. else if (!best)
  148. best = c;
  149. }
  150. if (insufficient_power > 0)
  151. dev_info(&udev->dev, "rejected %d configuration%s "
  152. "due to insufficient available bus power\n",
  153. insufficient_power, plural(insufficient_power));
  154. if (best) {
  155. i = best->desc.bConfigurationValue;
  156. dev_dbg(&udev->dev,
  157. "configuration #%d chosen from %d choice%s\n",
  158. i, num_configs, plural(num_configs));
  159. } else {
  160. i = -1;
  161. dev_warn(&udev->dev,
  162. "no configuration chosen from %d choice%s\n",
  163. num_configs, plural(num_configs));
  164. }
  165. return i;
  166. }
  167. EXPORT_SYMBOL_GPL(usb_choose_configuration);
  168. static int generic_probe(struct usb_device *udev)
  169. {
  170. int err, c;
  171. /* Choose and set the configuration. This registers the interfaces
  172. * with the driver core and lets interface drivers bind to them.
  173. */
  174. if (udev->authorized == 0)
  175. dev_err(&udev->dev, "Device is not authorized for usage\n");
  176. else {
  177. c = usb_choose_configuration(udev);
  178. if (c >= 0) {
  179. err = usb_set_configuration(udev, c);
  180. if (err && err != -ENODEV) {
  181. dev_err(&udev->dev, "can't set config #%d, error %d\n",
  182. c, err);
  183. /* This need not be fatal. The user can try to
  184. * set other configurations. */
  185. }
  186. }
  187. }
  188. /* USB device state == configured ... usable */
  189. usb_notify_add_device(udev);
  190. return 0;
  191. }
  192. static void generic_disconnect(struct usb_device *udev)
  193. {
  194. usb_notify_remove_device(udev);
  195. /* if this is only an unbind, not a physical disconnect, then
  196. * unconfigure the device */
  197. if (udev->actconfig)
  198. usb_set_configuration(udev, -1);
  199. }
  200. #ifdef CONFIG_PM
  201. static int generic_suspend(struct usb_device *udev, pm_message_t msg)
  202. {
  203. int rc;
  204. /* Normal USB devices suspend through their upstream port.
  205. * Root hubs don't have upstream ports to suspend,
  206. * so we have to shut down their downstream HC-to-USB
  207. * interfaces manually by doing a bus (or "global") suspend.
  208. */
  209. if (!udev->parent)
  210. rc = hcd_bus_suspend(udev, msg);
  211. /*
  212. * Non-root USB2 devices don't need to do anything for FREEZE
  213. * or PRETHAW. USB3 devices don't support global suspend and
  214. * needs to be selectively suspended.
  215. */
  216. else if ((msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_PRETHAW)
  217. && (udev->speed < USB_SPEED_SUPER))
  218. rc = 0;
  219. else
  220. rc = usb_port_suspend(udev, msg);
  221. return rc;
  222. }
  223. static int generic_resume(struct usb_device *udev, pm_message_t msg)
  224. {
  225. int rc;
  226. /* Normal USB devices resume/reset through their upstream port.
  227. * Root hubs don't have upstream ports to resume or reset,
  228. * so we have to start up their downstream HC-to-USB
  229. * interfaces manually by doing a bus (or "global") resume.
  230. */
  231. if (!udev->parent)
  232. rc = hcd_bus_resume(udev, msg);
  233. else
  234. rc = usb_port_resume(udev, msg);
  235. return rc;
  236. }
  237. #endif /* CONFIG_PM */
  238. struct usb_device_driver usb_generic_driver = {
  239. .name = "usb",
  240. .probe = generic_probe,
  241. .disconnect = generic_disconnect,
  242. #ifdef CONFIG_PM
  243. .suspend = generic_suspend,
  244. .resume = generic_resume,
  245. #endif
  246. .supports_autosuspend = 1,
  247. };