generic.c 6.1 KB

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