generic.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * drivers/usb/generic.c - generic driver for USB devices (not interfaces)
  3. *
  4. * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
  5. *
  6. * based on drivers/usb/usb.c which had the following copyrights:
  7. * (C) Copyright Linus Torvalds 1999
  8. * (C) Copyright Johannes Erdfelt 1999-2001
  9. * (C) Copyright Andreas Gal 1999
  10. * (C) Copyright Gregory P. Smith 1999
  11. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  12. * (C) Copyright Randy Dunlap 2000
  13. * (C) Copyright David Brownell 2000-2004
  14. * (C) Copyright Yggdrasil Computing, Inc. 2000
  15. * (usb_device_id matching changes by Adam J. Richter)
  16. * (C) Copyright Greg Kroah-Hartman 2002-2003
  17. *
  18. * Released under the GPLv2 only.
  19. * SPDX-License-Identifier: GPL-2.0
  20. */
  21. #include <linux/usb.h>
  22. #include <linux/usb/hcd.h>
  23. #include "usb.h"
  24. static inline const char *plural(int n)
  25. {
  26. return (n == 1 ? "" : "s");
  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. static int is_activesync(struct usb_interface_descriptor *desc)
  35. {
  36. return desc->bInterfaceClass == USB_CLASS_MISC
  37. && desc->bInterfaceSubClass == 1
  38. && desc->bInterfaceProtocol == 1;
  39. }
  40. int usb_choose_configuration(struct usb_device *udev)
  41. {
  42. int i;
  43. int num_configs;
  44. int insufficient_power = 0;
  45. struct usb_host_config *c, *best;
  46. if (usb_device_is_owned(udev))
  47. return 0;
  48. best = NULL;
  49. c = udev->config;
  50. num_configs = udev->descriptor.bNumConfigurations;
  51. for (i = 0; i < num_configs; (i++, c++)) {
  52. struct usb_interface_descriptor *desc = NULL;
  53. /* It's possible that a config has no interfaces! */
  54. if (c->desc.bNumInterfaces > 0)
  55. desc = &c->intf_cache[0]->altsetting->desc;
  56. /*
  57. * HP's USB bus-powered keyboard has only one configuration
  58. * and it claims to be self-powered; other devices may have
  59. * similar errors in their descriptors. If the next test
  60. * were allowed to execute, such configurations would always
  61. * be rejected and the devices would not work as expected.
  62. * In the meantime, we run the risk of selecting a config
  63. * that requires external power at a time when that power
  64. * isn't available. It seems to be the lesser of two evils.
  65. *
  66. * Bugzilla #6448 reports a device that appears to crash
  67. * when it receives a GET_DEVICE_STATUS request! We don't
  68. * have any other way to tell whether a device is self-powered,
  69. * but since we don't use that information anywhere but here,
  70. * the call has been removed.
  71. *
  72. * Maybe the GET_DEVICE_STATUS call and the test below can
  73. * be reinstated when device firmwares become more reliable.
  74. * Don't hold your breath.
  75. */
  76. #if 0
  77. /* Rule out self-powered configs for a bus-powered device */
  78. if (bus_powered && (c->desc.bmAttributes &
  79. USB_CONFIG_ATT_SELFPOWER))
  80. continue;
  81. #endif
  82. /*
  83. * The next test may not be as effective as it should be.
  84. * Some hubs have errors in their descriptor, claiming
  85. * to be self-powered when they are really bus-powered.
  86. * We will overestimate the amount of current such hubs
  87. * make available for each port.
  88. *
  89. * This is a fairly benign sort of failure. It won't
  90. * cause us to reject configurations that we should have
  91. * accepted.
  92. */
  93. /* Rule out configs that draw too much bus current */
  94. if (usb_get_max_power(udev, c) > udev->bus_mA) {
  95. insufficient_power++;
  96. continue;
  97. }
  98. /* When the first config's first interface is one of Microsoft's
  99. * pet nonstandard Ethernet-over-USB protocols, ignore it unless
  100. * this kernel has enabled the necessary host side driver.
  101. * But: Don't ignore it if it's the only config.
  102. */
  103. if (i == 0 && num_configs > 1 && desc &&
  104. (is_rndis(desc) || is_activesync(desc))) {
  105. #if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
  106. continue;
  107. #else
  108. best = c;
  109. #endif
  110. }
  111. /* From the remaining configs, choose the first one whose
  112. * first interface is for a non-vendor-specific class.
  113. * Reason: Linux is more likely to have a class driver
  114. * than a vendor-specific driver. */
  115. else if (udev->descriptor.bDeviceClass !=
  116. USB_CLASS_VENDOR_SPEC &&
  117. (desc && desc->bInterfaceClass !=
  118. USB_CLASS_VENDOR_SPEC)) {
  119. best = c;
  120. break;
  121. }
  122. /* If all the remaining configs are vendor-specific,
  123. * choose the first one. */
  124. else if (!best)
  125. best = c;
  126. }
  127. if (insufficient_power > 0)
  128. dev_info(&udev->dev, "rejected %d configuration%s "
  129. "due to insufficient available bus power\n",
  130. insufficient_power, plural(insufficient_power));
  131. if (best) {
  132. i = best->desc.bConfigurationValue;
  133. dev_dbg(&udev->dev,
  134. "configuration #%d chosen from %d choice%s\n",
  135. i, num_configs, plural(num_configs));
  136. } else {
  137. i = -1;
  138. dev_warn(&udev->dev,
  139. "no configuration chosen from %d choice%s\n",
  140. num_configs, plural(num_configs));
  141. }
  142. return i;
  143. }
  144. EXPORT_SYMBOL_GPL(usb_choose_configuration);
  145. static int generic_probe(struct usb_device *udev)
  146. {
  147. int err, c;
  148. /* Choose and set the configuration. This registers the interfaces
  149. * with the driver core and lets interface drivers bind to them.
  150. */
  151. if (udev->authorized == 0)
  152. dev_err(&udev->dev, "Device is not authorized for usage\n");
  153. else {
  154. c = usb_choose_configuration(udev);
  155. if (c >= 0) {
  156. err = usb_set_configuration(udev, c);
  157. if (err && err != -ENODEV) {
  158. dev_err(&udev->dev, "can't set config #%d, error %d\n",
  159. c, err);
  160. /* This need not be fatal. The user can try to
  161. * set other configurations. */
  162. }
  163. }
  164. }
  165. /* USB device state == configured ... usable */
  166. usb_notify_add_device(udev);
  167. return 0;
  168. }
  169. static void generic_disconnect(struct usb_device *udev)
  170. {
  171. usb_notify_remove_device(udev);
  172. /* if this is only an unbind, not a physical disconnect, then
  173. * unconfigure the device */
  174. if (udev->actconfig)
  175. usb_set_configuration(udev, -1);
  176. }
  177. #ifdef CONFIG_PM
  178. static int generic_suspend(struct usb_device *udev, pm_message_t msg)
  179. {
  180. int rc;
  181. /* Normal USB devices suspend through their upstream port.
  182. * Root hubs don't have upstream ports to suspend,
  183. * so we have to shut down their downstream HC-to-USB
  184. * interfaces manually by doing a bus (or "global") suspend.
  185. */
  186. if (!udev->parent)
  187. rc = hcd_bus_suspend(udev, msg);
  188. /* Non-root devices don't need to do anything for FREEZE or PRETHAW */
  189. else if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_PRETHAW)
  190. rc = 0;
  191. else
  192. rc = usb_port_suspend(udev, msg);
  193. return rc;
  194. }
  195. static int generic_resume(struct usb_device *udev, pm_message_t msg)
  196. {
  197. int rc;
  198. /* Normal USB devices resume/reset through their upstream port.
  199. * Root hubs don't have upstream ports to resume or reset,
  200. * so we have to start up their downstream HC-to-USB
  201. * interfaces manually by doing a bus (or "global") resume.
  202. */
  203. if (!udev->parent)
  204. rc = hcd_bus_resume(udev, msg);
  205. else
  206. rc = usb_port_resume(udev, msg);
  207. return rc;
  208. }
  209. #endif /* CONFIG_PM */
  210. struct usb_device_driver usb_generic_driver = {
  211. .name = "usb",
  212. .probe = generic_probe,
  213. .disconnect = generic_disconnect,
  214. #ifdef CONFIG_PM
  215. .suspend = generic_suspend,
  216. .resume = generic_resume,
  217. #endif
  218. .supports_autosuspend = 1,
  219. };