generic.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/config.h>
  20. #include <linux/usb.h>
  21. #include "usb.h"
  22. static inline const char *plural(int n)
  23. {
  24. return (n == 1 ? "" : "s");
  25. }
  26. static int choose_configuration(struct usb_device *udev)
  27. {
  28. int i;
  29. int num_configs;
  30. int insufficient_power = 0;
  31. struct usb_host_config *c, *best;
  32. best = NULL;
  33. c = udev->config;
  34. num_configs = udev->descriptor.bNumConfigurations;
  35. for (i = 0; i < num_configs; (i++, c++)) {
  36. struct usb_interface_descriptor *desc = NULL;
  37. /* It's possible that a config has no interfaces! */
  38. if (c->desc.bNumInterfaces > 0)
  39. desc = &c->intf_cache[0]->altsetting->desc;
  40. /*
  41. * HP's USB bus-powered keyboard has only one configuration
  42. * and it claims to be self-powered; other devices may have
  43. * similar errors in their descriptors. If the next test
  44. * were allowed to execute, such configurations would always
  45. * be rejected and the devices would not work as expected.
  46. * In the meantime, we run the risk of selecting a config
  47. * that requires external power at a time when that power
  48. * isn't available. It seems to be the lesser of two evils.
  49. *
  50. * Bugzilla #6448 reports a device that appears to crash
  51. * when it receives a GET_DEVICE_STATUS request! We don't
  52. * have any other way to tell whether a device is self-powered,
  53. * but since we don't use that information anywhere but here,
  54. * the call has been removed.
  55. *
  56. * Maybe the GET_DEVICE_STATUS call and the test below can
  57. * be reinstated when device firmwares become more reliable.
  58. * Don't hold your breath.
  59. */
  60. #if 0
  61. /* Rule out self-powered configs for a bus-powered device */
  62. if (bus_powered && (c->desc.bmAttributes &
  63. USB_CONFIG_ATT_SELFPOWER))
  64. continue;
  65. #endif
  66. /*
  67. * The next test may not be as effective as it should be.
  68. * Some hubs have errors in their descriptor, claiming
  69. * to be self-powered when they are really bus-powered.
  70. * We will overestimate the amount of current such hubs
  71. * make available for each port.
  72. *
  73. * This is a fairly benign sort of failure. It won't
  74. * cause us to reject configurations that we should have
  75. * accepted.
  76. */
  77. /* Rule out configs that draw too much bus current */
  78. if (c->desc.bMaxPower * 2 > udev->bus_mA) {
  79. insufficient_power++;
  80. continue;
  81. }
  82. /* If the first config's first interface is COMM/2/0xff
  83. * (MSFT RNDIS), rule it out unless Linux has host-side
  84. * RNDIS support. */
  85. if (i == 0 && desc
  86. && desc->bInterfaceClass == USB_CLASS_COMM
  87. && desc->bInterfaceSubClass == 2
  88. && desc->bInterfaceProtocol == 0xff) {
  89. #ifndef CONFIG_USB_NET_RNDIS_HOST
  90. continue;
  91. #else
  92. best = c;
  93. #endif
  94. }
  95. /* From the remaining configs, choose the first one whose
  96. * first interface is for a non-vendor-specific class.
  97. * Reason: Linux is more likely to have a class driver
  98. * than a vendor-specific driver. */
  99. else if (udev->descriptor.bDeviceClass !=
  100. USB_CLASS_VENDOR_SPEC &&
  101. (!desc || desc->bInterfaceClass !=
  102. USB_CLASS_VENDOR_SPEC)) {
  103. best = c;
  104. break;
  105. }
  106. /* If all the remaining configs are vendor-specific,
  107. * choose the first one. */
  108. else if (!best)
  109. best = c;
  110. }
  111. if (insufficient_power > 0)
  112. dev_info(&udev->dev, "rejected %d configuration%s "
  113. "due to insufficient available bus power\n",
  114. insufficient_power, plural(insufficient_power));
  115. if (best) {
  116. i = best->desc.bConfigurationValue;
  117. dev_info(&udev->dev,
  118. "configuration #%d chosen from %d choice%s\n",
  119. i, num_configs, plural(num_configs));
  120. } else {
  121. i = -1;
  122. dev_warn(&udev->dev,
  123. "no configuration chosen from %d choice%s\n",
  124. num_configs, plural(num_configs));
  125. }
  126. return i;
  127. }
  128. static int generic_probe(struct usb_device *udev)
  129. {
  130. int err, c;
  131. /* put device-specific files into sysfs */
  132. usb_create_sysfs_dev_files(udev);
  133. /* Choose and set the configuration. This registers the interfaces
  134. * with the driver core and lets interface drivers bind to them.
  135. */
  136. c = choose_configuration(udev);
  137. if (c >= 0) {
  138. err = usb_set_configuration(udev, c);
  139. if (err) {
  140. dev_err(&udev->dev, "can't set config #%d, error %d\n",
  141. c, err);
  142. /* This need not be fatal. The user can try to
  143. * set other configurations. */
  144. }
  145. }
  146. /* USB device state == configured ... usable */
  147. usb_notify_add_device(udev);
  148. return 0;
  149. }
  150. static void generic_disconnect(struct usb_device *udev)
  151. {
  152. usb_notify_remove_device(udev);
  153. /* if this is only an unbind, not a physical disconnect, then
  154. * unconfigure the device */
  155. if (udev->state == USB_STATE_CONFIGURED)
  156. usb_set_configuration(udev, 0);
  157. usb_remove_sysfs_dev_files(udev);
  158. /* in case the call failed or the device was suspended */
  159. if (udev->state >= USB_STATE_CONFIGURED)
  160. usb_disable_device(udev, 0);
  161. }
  162. #ifdef CONFIG_PM
  163. static int generic_suspend(struct usb_device *udev, pm_message_t msg)
  164. {
  165. /* USB devices enter SUSPEND state through their hubs, but can be
  166. * marked for FREEZE as soon as their children are already idled.
  167. * But those semantics are useless, so we equate the two (sigh).
  168. */
  169. return usb_port_suspend(udev);
  170. }
  171. static int generic_resume(struct usb_device *udev)
  172. {
  173. return usb_port_resume(udev);
  174. }
  175. #endif /* CONFIG_PM */
  176. struct usb_device_driver usb_generic_driver = {
  177. .name = "usb",
  178. .probe = generic_probe,
  179. .disconnect = generic_disconnect,
  180. #ifdef CONFIG_PM
  181. .suspend = generic_suspend,
  182. .resume = generic_resume,
  183. #endif
  184. };