generic.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "usb.h"
  21. #include "hcd.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. */
  98. if (i == 0 && desc && (is_rndis(desc) || is_activesync(desc))) {
  99. #if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
  100. continue;
  101. #else
  102. best = c;
  103. #endif
  104. }
  105. /* From the remaining configs, choose the first one whose
  106. * first interface is for a non-vendor-specific class.
  107. * Reason: Linux is more likely to have a class driver
  108. * than a vendor-specific driver. */
  109. else if (udev->descriptor.bDeviceClass !=
  110. USB_CLASS_VENDOR_SPEC &&
  111. (!desc || desc->bInterfaceClass !=
  112. USB_CLASS_VENDOR_SPEC)) {
  113. best = c;
  114. break;
  115. }
  116. /* If all the remaining configs are vendor-specific,
  117. * choose the first one. */
  118. else if (!best)
  119. best = c;
  120. }
  121. if (insufficient_power > 0)
  122. dev_info(&udev->dev, "rejected %d configuration%s "
  123. "due to insufficient available bus power\n",
  124. insufficient_power, plural(insufficient_power));
  125. if (best) {
  126. i = best->desc.bConfigurationValue;
  127. dev_info(&udev->dev,
  128. "configuration #%d chosen from %d choice%s\n",
  129. i, num_configs, plural(num_configs));
  130. } else {
  131. i = -1;
  132. dev_warn(&udev->dev,
  133. "no configuration chosen from %d choice%s\n",
  134. num_configs, plural(num_configs));
  135. }
  136. return i;
  137. }
  138. static int generic_probe(struct usb_device *udev)
  139. {
  140. int err, c;
  141. /* Choose and set the configuration. This registers the interfaces
  142. * with the driver core and lets interface drivers bind to them.
  143. */
  144. if (udev->authorized == 0)
  145. dev_err(&udev->dev, "Device is not authorized for usage\n");
  146. else {
  147. c = usb_choose_configuration(udev);
  148. if (c >= 0) {
  149. err = usb_set_configuration(udev, c);
  150. if (err) {
  151. dev_err(&udev->dev, "can't set config #%d, error %d\n",
  152. c, err);
  153. /* This need not be fatal. The user can try to
  154. * set other configurations. */
  155. }
  156. }
  157. }
  158. /* USB device state == configured ... usable */
  159. usb_notify_add_device(udev);
  160. return 0;
  161. }
  162. static void generic_disconnect(struct usb_device *udev)
  163. {
  164. usb_notify_remove_device(udev);
  165. /* if this is only an unbind, not a physical disconnect, then
  166. * unconfigure the device */
  167. if (udev->actconfig)
  168. usb_set_configuration(udev, -1);
  169. }
  170. #ifdef CONFIG_PM
  171. static int generic_suspend(struct usb_device *udev, pm_message_t msg)
  172. {
  173. int rc;
  174. /* Normal USB devices suspend through their upstream port.
  175. * Root hubs don't have upstream ports to suspend,
  176. * so we have to shut down their downstream HC-to-USB
  177. * interfaces manually by doing a bus (or "global") suspend.
  178. */
  179. if (!udev->parent)
  180. rc = hcd_bus_suspend(udev, msg);
  181. /* Non-root devices don't need to do anything for FREEZE or PRETHAW */
  182. else if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_PRETHAW)
  183. rc = 0;
  184. else
  185. rc = usb_port_suspend(udev, msg);
  186. return rc;
  187. }
  188. static int generic_resume(struct usb_device *udev, pm_message_t msg)
  189. {
  190. int rc;
  191. /* Normal USB devices resume/reset through their upstream port.
  192. * Root hubs don't have upstream ports to resume or reset,
  193. * so we have to start up their downstream HC-to-USB
  194. * interfaces manually by doing a bus (or "global") resume.
  195. */
  196. if (!udev->parent)
  197. rc = hcd_bus_resume(udev, msg);
  198. else
  199. rc = usb_port_resume(udev, msg);
  200. return rc;
  201. }
  202. #endif /* CONFIG_PM */
  203. struct usb_device_driver usb_generic_driver = {
  204. .name = "usb",
  205. .probe = generic_probe,
  206. .disconnect = generic_disconnect,
  207. #ifdef CONFIG_PM
  208. .suspend = generic_suspend,
  209. .resume = generic_resume,
  210. #endif
  211. .supports_autosuspend = 1,
  212. };