driver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * drivers/usb/driver.c - most of the driver model stuff for usb
  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. * NOTE! This is not actually a driver at all, rather this is
  19. * just a collection of helper routines that implement the
  20. * generic USB things that the real drivers can use..
  21. *
  22. */
  23. #include <linux/config.h>
  24. #include <linux/device.h>
  25. #include <linux/usb.h>
  26. #include "hcd.h"
  27. #include "usb.h"
  28. static int generic_probe(struct device *dev)
  29. {
  30. return 0;
  31. }
  32. static int generic_remove(struct device *dev)
  33. {
  34. struct usb_device *udev = to_usb_device(dev);
  35. /* if this is only an unbind, not a physical disconnect, then
  36. * unconfigure the device */
  37. if (udev->state == USB_STATE_CONFIGURED)
  38. usb_set_configuration(udev, 0);
  39. /* in case the call failed or the device was suspended */
  40. if (udev->state >= USB_STATE_CONFIGURED)
  41. usb_disable_device(udev, 0);
  42. return 0;
  43. }
  44. struct device_driver usb_generic_driver = {
  45. .owner = THIS_MODULE,
  46. .name = "usb",
  47. .bus = &usb_bus_type,
  48. .probe = generic_probe,
  49. .remove = generic_remove,
  50. };
  51. /* Fun hack to determine if the struct device is a
  52. * usb device or a usb interface. */
  53. int usb_generic_driver_data;
  54. /* called from driver core with usb_bus_type.subsys writelock */
  55. static int usb_probe_interface(struct device *dev)
  56. {
  57. struct usb_interface * intf = to_usb_interface(dev);
  58. struct usb_driver * driver = to_usb_driver(dev->driver);
  59. const struct usb_device_id *id;
  60. int error = -ENODEV;
  61. dev_dbg(dev, "%s\n", __FUNCTION__);
  62. if (!driver->probe)
  63. return error;
  64. /* FIXME we'd much prefer to just resume it ... */
  65. if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
  66. return -EHOSTUNREACH;
  67. id = usb_match_id(intf, driver->id_table);
  68. if (id) {
  69. dev_dbg(dev, "%s - got id\n", __FUNCTION__);
  70. /* Interface "power state" doesn't correspond to any hardware
  71. * state whatsoever. We use it to record when it's bound to
  72. * a driver that may start I/0: it's not frozen/quiesced.
  73. */
  74. mark_active(intf);
  75. intf->condition = USB_INTERFACE_BINDING;
  76. error = driver->probe(intf, id);
  77. if (error) {
  78. mark_quiesced(intf);
  79. intf->condition = USB_INTERFACE_UNBOUND;
  80. } else
  81. intf->condition = USB_INTERFACE_BOUND;
  82. }
  83. return error;
  84. }
  85. /* called from driver core with usb_bus_type.subsys writelock */
  86. static int usb_unbind_interface(struct device *dev)
  87. {
  88. struct usb_interface *intf = to_usb_interface(dev);
  89. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  90. intf->condition = USB_INTERFACE_UNBINDING;
  91. /* release all urbs for this interface */
  92. usb_disable_interface(interface_to_usbdev(intf), intf);
  93. if (driver && driver->disconnect)
  94. driver->disconnect(intf);
  95. /* reset other interface state */
  96. usb_set_interface(interface_to_usbdev(intf),
  97. intf->altsetting[0].desc.bInterfaceNumber,
  98. 0);
  99. usb_set_intfdata(intf, NULL);
  100. intf->condition = USB_INTERFACE_UNBOUND;
  101. mark_quiesced(intf);
  102. return 0;
  103. }
  104. /**
  105. * usb_match_id - find first usb_device_id matching device or interface
  106. * @interface: the interface of interest
  107. * @id: array of usb_device_id structures, terminated by zero entry
  108. *
  109. * usb_match_id searches an array of usb_device_id's and returns
  110. * the first one matching the device or interface, or null.
  111. * This is used when binding (or rebinding) a driver to an interface.
  112. * Most USB device drivers will use this indirectly, through the usb core,
  113. * but some layered driver frameworks use it directly.
  114. * These device tables are exported with MODULE_DEVICE_TABLE, through
  115. * modutils, to support the driver loading functionality of USB hotplugging.
  116. *
  117. * What Matches:
  118. *
  119. * The "match_flags" element in a usb_device_id controls which
  120. * members are used. If the corresponding bit is set, the
  121. * value in the device_id must match its corresponding member
  122. * in the device or interface descriptor, or else the device_id
  123. * does not match.
  124. *
  125. * "driver_info" is normally used only by device drivers,
  126. * but you can create a wildcard "matches anything" usb_device_id
  127. * as a driver's "modules.usbmap" entry if you provide an id with
  128. * only a nonzero "driver_info" field. If you do this, the USB device
  129. * driver's probe() routine should use additional intelligence to
  130. * decide whether to bind to the specified interface.
  131. *
  132. * What Makes Good usb_device_id Tables:
  133. *
  134. * The match algorithm is very simple, so that intelligence in
  135. * driver selection must come from smart driver id records.
  136. * Unless you have good reasons to use another selection policy,
  137. * provide match elements only in related groups, and order match
  138. * specifiers from specific to general. Use the macros provided
  139. * for that purpose if you can.
  140. *
  141. * The most specific match specifiers use device descriptor
  142. * data. These are commonly used with product-specific matches;
  143. * the USB_DEVICE macro lets you provide vendor and product IDs,
  144. * and you can also match against ranges of product revisions.
  145. * These are widely used for devices with application or vendor
  146. * specific bDeviceClass values.
  147. *
  148. * Matches based on device class/subclass/protocol specifications
  149. * are slightly more general; use the USB_DEVICE_INFO macro, or
  150. * its siblings. These are used with single-function devices
  151. * where bDeviceClass doesn't specify that each interface has
  152. * its own class.
  153. *
  154. * Matches based on interface class/subclass/protocol are the
  155. * most general; they let drivers bind to any interface on a
  156. * multiple-function device. Use the USB_INTERFACE_INFO
  157. * macro, or its siblings, to match class-per-interface style
  158. * devices (as recorded in bDeviceClass).
  159. *
  160. * Within those groups, remember that not all combinations are
  161. * meaningful. For example, don't give a product version range
  162. * without vendor and product IDs; or specify a protocol without
  163. * its associated class and subclass.
  164. */
  165. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  166. const struct usb_device_id *id)
  167. {
  168. struct usb_host_interface *intf;
  169. struct usb_device *dev;
  170. /* proc_connectinfo in devio.c may call us with id == NULL. */
  171. if (id == NULL)
  172. return NULL;
  173. intf = interface->cur_altsetting;
  174. dev = interface_to_usbdev(interface);
  175. /* It is important to check that id->driver_info is nonzero,
  176. since an entry that is all zeroes except for a nonzero
  177. id->driver_info is the way to create an entry that
  178. indicates that the driver want to examine every
  179. device and interface. */
  180. for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
  181. id->driver_info; id++) {
  182. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  183. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  184. continue;
  185. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  186. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  187. continue;
  188. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  189. greater than any unsigned number. */
  190. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  191. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  192. continue;
  193. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  194. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  195. continue;
  196. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  197. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  198. continue;
  199. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  200. (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
  201. continue;
  202. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  203. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  204. continue;
  205. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  206. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  207. continue;
  208. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  209. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  210. continue;
  211. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  212. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  213. continue;
  214. return id;
  215. }
  216. return NULL;
  217. }
  218. EXPORT_SYMBOL_GPL(usb_match_id);
  219. int usb_device_match(struct device *dev, struct device_driver *drv)
  220. {
  221. struct usb_interface *intf;
  222. struct usb_driver *usb_drv;
  223. const struct usb_device_id *id;
  224. /* check for generic driver, which we don't match any device with */
  225. if (drv == &usb_generic_driver)
  226. return 0;
  227. intf = to_usb_interface(dev);
  228. usb_drv = to_usb_driver(drv);
  229. id = usb_match_id(intf, usb_drv->id_table);
  230. if (id)
  231. return 1;
  232. return 0;
  233. }
  234. /**
  235. * usb_register - register a USB driver
  236. * @new_driver: USB operations for the driver
  237. *
  238. * Registers a USB driver with the USB core. The list of unattached
  239. * interfaces will be rescanned whenever a new driver is added, allowing
  240. * the new driver to attach to any recognized devices.
  241. * Returns a negative error code on failure and 0 on success.
  242. *
  243. * NOTE: if you want your driver to use the USB major number, you must call
  244. * usb_register_dev() to enable that functionality. This function no longer
  245. * takes care of that.
  246. */
  247. int usb_register(struct usb_driver *new_driver)
  248. {
  249. int retval = 0;
  250. if (usb_disabled())
  251. return -ENODEV;
  252. new_driver->driver.name = (char *)new_driver->name;
  253. new_driver->driver.bus = &usb_bus_type;
  254. new_driver->driver.probe = usb_probe_interface;
  255. new_driver->driver.remove = usb_unbind_interface;
  256. new_driver->driver.owner = new_driver->owner;
  257. usb_lock_all_devices();
  258. retval = driver_register(&new_driver->driver);
  259. usb_unlock_all_devices();
  260. if (!retval) {
  261. pr_info("%s: registered new driver %s\n",
  262. usbcore_name, new_driver->name);
  263. usbfs_update_special();
  264. } else {
  265. printk(KERN_ERR "%s: error %d registering driver %s\n",
  266. usbcore_name, retval, new_driver->name);
  267. }
  268. return retval;
  269. }
  270. EXPORT_SYMBOL_GPL(usb_register);
  271. /**
  272. * usb_deregister - unregister a USB driver
  273. * @driver: USB operations of the driver to unregister
  274. * Context: must be able to sleep
  275. *
  276. * Unlinks the specified driver from the internal USB driver list.
  277. *
  278. * NOTE: If you called usb_register_dev(), you still need to call
  279. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  280. * this * call will no longer do it for you.
  281. */
  282. void usb_deregister(struct usb_driver *driver)
  283. {
  284. pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
  285. usb_lock_all_devices();
  286. driver_unregister(&driver->driver);
  287. usb_unlock_all_devices();
  288. usbfs_update_special();
  289. }
  290. EXPORT_SYMBOL_GPL(usb_deregister);