generic.c 7.1 KB

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