generic.c 5.7 KB

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