generic.c 6.9 KB

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