driver.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/device.h>
  24. #include <linux/usb.h>
  25. #include "hcd.h"
  26. #include "usb.h"
  27. static int usb_match_one_id(struct usb_interface *interface,
  28. const struct usb_device_id *id);
  29. struct usb_dynid {
  30. struct list_head node;
  31. struct usb_device_id id;
  32. };
  33. static int generic_probe(struct device *dev)
  34. {
  35. return 0;
  36. }
  37. static int generic_remove(struct device *dev)
  38. {
  39. struct usb_device *udev = to_usb_device(dev);
  40. /* if this is only an unbind, not a physical disconnect, then
  41. * unconfigure the device */
  42. if (udev->state == USB_STATE_CONFIGURED)
  43. usb_set_configuration(udev, 0);
  44. /* in case the call failed or the device was suspended */
  45. if (udev->state >= USB_STATE_CONFIGURED)
  46. usb_disable_device(udev, 0);
  47. return 0;
  48. }
  49. struct device_driver usb_generic_driver = {
  50. .owner = THIS_MODULE,
  51. .name = "usb",
  52. .bus = &usb_bus_type,
  53. .probe = generic_probe,
  54. .remove = generic_remove,
  55. };
  56. /* Fun hack to determine if the struct device is a
  57. * usb device or a usb interface. */
  58. int usb_generic_driver_data;
  59. #ifdef CONFIG_HOTPLUG
  60. /*
  61. * Adds a new dynamic USBdevice ID to this driver,
  62. * and cause the driver to probe for all devices again.
  63. */
  64. static ssize_t store_new_id(struct device_driver *driver,
  65. const char *buf, size_t count)
  66. {
  67. struct usb_driver *usb_drv = to_usb_driver(driver);
  68. struct usb_dynid *dynid;
  69. u32 idVendor = 0;
  70. u32 idProduct = 0;
  71. int fields = 0;
  72. fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
  73. if (fields < 2)
  74. return -EINVAL;
  75. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  76. if (!dynid)
  77. return -ENOMEM;
  78. INIT_LIST_HEAD(&dynid->node);
  79. dynid->id.idVendor = idVendor;
  80. dynid->id.idProduct = idProduct;
  81. dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  82. spin_lock(&usb_drv->dynids.lock);
  83. list_add_tail(&usb_drv->dynids.list, &dynid->node);
  84. spin_unlock(&usb_drv->dynids.lock);
  85. if (get_driver(driver)) {
  86. driver_attach(driver);
  87. put_driver(driver);
  88. }
  89. return count;
  90. }
  91. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  92. static int usb_create_newid_file(struct usb_driver *usb_drv)
  93. {
  94. int error = 0;
  95. if (usb_drv->no_dynamic_id)
  96. goto exit;
  97. if (usb_drv->probe != NULL)
  98. error = sysfs_create_file(&usb_drv->driver.kobj,
  99. &driver_attr_new_id.attr);
  100. exit:
  101. return error;
  102. }
  103. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  104. {
  105. if (usb_drv->no_dynamic_id)
  106. return;
  107. if (usb_drv->probe != NULL)
  108. sysfs_remove_file(&usb_drv->driver.kobj,
  109. &driver_attr_new_id.attr);
  110. }
  111. static void usb_free_dynids(struct usb_driver *usb_drv)
  112. {
  113. struct usb_dynid *dynid, *n;
  114. spin_lock(&usb_drv->dynids.lock);
  115. list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
  116. list_del(&dynid->node);
  117. kfree(dynid);
  118. }
  119. spin_unlock(&usb_drv->dynids.lock);
  120. }
  121. #else
  122. static inline int usb_create_newid_file(struct usb_driver *usb_drv)
  123. {
  124. return 0;
  125. }
  126. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  127. {
  128. }
  129. static inline void usb_free_dynids(struct usb_driver *usb_drv)
  130. {
  131. }
  132. #endif
  133. static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
  134. struct usb_driver *drv)
  135. {
  136. struct usb_dynid *dynid;
  137. spin_lock(&drv->dynids.lock);
  138. list_for_each_entry(dynid, &drv->dynids.list, node) {
  139. if (usb_match_one_id(intf, &dynid->id)) {
  140. spin_unlock(&drv->dynids.lock);
  141. return &dynid->id;
  142. }
  143. }
  144. spin_unlock(&drv->dynids.lock);
  145. return NULL;
  146. }
  147. /* called from driver core with usb_bus_type.subsys writelock */
  148. static int usb_probe_interface(struct device *dev)
  149. {
  150. struct usb_interface * intf = to_usb_interface(dev);
  151. struct usb_driver * driver = to_usb_driver(dev->driver);
  152. const struct usb_device_id *id;
  153. int error = -ENODEV;
  154. dev_dbg(dev, "%s\n", __FUNCTION__);
  155. if (!driver->probe)
  156. return error;
  157. /* FIXME we'd much prefer to just resume it ... */
  158. if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
  159. return -EHOSTUNREACH;
  160. id = usb_match_id(intf, driver->id_table);
  161. if (!id)
  162. id = usb_match_dynamic_id(intf, driver);
  163. if (id) {
  164. dev_dbg(dev, "%s - got id\n", __FUNCTION__);
  165. /* Interface "power state" doesn't correspond to any hardware
  166. * state whatsoever. We use it to record when it's bound to
  167. * a driver that may start I/0: it's not frozen/quiesced.
  168. */
  169. mark_active(intf);
  170. intf->condition = USB_INTERFACE_BINDING;
  171. error = driver->probe(intf, id);
  172. if (error) {
  173. mark_quiesced(intf);
  174. intf->condition = USB_INTERFACE_UNBOUND;
  175. } else
  176. intf->condition = USB_INTERFACE_BOUND;
  177. }
  178. return error;
  179. }
  180. /* called from driver core with usb_bus_type.subsys writelock */
  181. static int usb_unbind_interface(struct device *dev)
  182. {
  183. struct usb_interface *intf = to_usb_interface(dev);
  184. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  185. intf->condition = USB_INTERFACE_UNBINDING;
  186. /* release all urbs for this interface */
  187. usb_disable_interface(interface_to_usbdev(intf), intf);
  188. if (driver && driver->disconnect)
  189. driver->disconnect(intf);
  190. /* reset other interface state */
  191. usb_set_interface(interface_to_usbdev(intf),
  192. intf->altsetting[0].desc.bInterfaceNumber,
  193. 0);
  194. usb_set_intfdata(intf, NULL);
  195. intf->condition = USB_INTERFACE_UNBOUND;
  196. mark_quiesced(intf);
  197. return 0;
  198. }
  199. /* returns 0 if no match, 1 if match */
  200. static int usb_match_one_id(struct usb_interface *interface,
  201. const struct usb_device_id *id)
  202. {
  203. struct usb_host_interface *intf;
  204. struct usb_device *dev;
  205. /* proc_connectinfo in devio.c may call us with id == NULL. */
  206. if (id == NULL)
  207. return 0;
  208. intf = interface->cur_altsetting;
  209. dev = interface_to_usbdev(interface);
  210. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  211. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  212. return 0;
  213. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  214. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  215. return 0;
  216. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  217. greater than any unsigned number. */
  218. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  219. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  220. return 0;
  221. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  222. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  223. return 0;
  224. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  225. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  226. return 0;
  227. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  228. (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
  229. return 0;
  230. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  231. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  232. return 0;
  233. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  234. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  235. return 0;
  236. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  237. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  238. return 0;
  239. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  240. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  241. return 0;
  242. return 1;
  243. }
  244. /**
  245. * usb_match_id - find first usb_device_id matching device or interface
  246. * @interface: the interface of interest
  247. * @id: array of usb_device_id structures, terminated by zero entry
  248. *
  249. * usb_match_id searches an array of usb_device_id's and returns
  250. * the first one matching the device or interface, or null.
  251. * This is used when binding (or rebinding) a driver to an interface.
  252. * Most USB device drivers will use this indirectly, through the usb core,
  253. * but some layered driver frameworks use it directly.
  254. * These device tables are exported with MODULE_DEVICE_TABLE, through
  255. * modutils, to support the driver loading functionality of USB hotplugging.
  256. *
  257. * What Matches:
  258. *
  259. * The "match_flags" element in a usb_device_id controls which
  260. * members are used. If the corresponding bit is set, the
  261. * value in the device_id must match its corresponding member
  262. * in the device or interface descriptor, or else the device_id
  263. * does not match.
  264. *
  265. * "driver_info" is normally used only by device drivers,
  266. * but you can create a wildcard "matches anything" usb_device_id
  267. * as a driver's "modules.usbmap" entry if you provide an id with
  268. * only a nonzero "driver_info" field. If you do this, the USB device
  269. * driver's probe() routine should use additional intelligence to
  270. * decide whether to bind to the specified interface.
  271. *
  272. * What Makes Good usb_device_id Tables:
  273. *
  274. * The match algorithm is very simple, so that intelligence in
  275. * driver selection must come from smart driver id records.
  276. * Unless you have good reasons to use another selection policy,
  277. * provide match elements only in related groups, and order match
  278. * specifiers from specific to general. Use the macros provided
  279. * for that purpose if you can.
  280. *
  281. * The most specific match specifiers use device descriptor
  282. * data. These are commonly used with product-specific matches;
  283. * the USB_DEVICE macro lets you provide vendor and product IDs,
  284. * and you can also match against ranges of product revisions.
  285. * These are widely used for devices with application or vendor
  286. * specific bDeviceClass values.
  287. *
  288. * Matches based on device class/subclass/protocol specifications
  289. * are slightly more general; use the USB_DEVICE_INFO macro, or
  290. * its siblings. These are used with single-function devices
  291. * where bDeviceClass doesn't specify that each interface has
  292. * its own class.
  293. *
  294. * Matches based on interface class/subclass/protocol are the
  295. * most general; they let drivers bind to any interface on a
  296. * multiple-function device. Use the USB_INTERFACE_INFO
  297. * macro, or its siblings, to match class-per-interface style
  298. * devices (as recorded in bDeviceClass).
  299. *
  300. * Within those groups, remember that not all combinations are
  301. * meaningful. For example, don't give a product version range
  302. * without vendor and product IDs; or specify a protocol without
  303. * its associated class and subclass.
  304. */
  305. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  306. const struct usb_device_id *id)
  307. {
  308. /* proc_connectinfo in devio.c may call us with id == NULL. */
  309. if (id == NULL)
  310. return NULL;
  311. /* It is important to check that id->driver_info is nonzero,
  312. since an entry that is all zeroes except for a nonzero
  313. id->driver_info is the way to create an entry that
  314. indicates that the driver want to examine every
  315. device and interface. */
  316. for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
  317. id->driver_info; id++) {
  318. if (usb_match_one_id(interface, id))
  319. return id;
  320. }
  321. return NULL;
  322. }
  323. EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
  324. int usb_device_match(struct device *dev, struct device_driver *drv)
  325. {
  326. struct usb_interface *intf;
  327. struct usb_driver *usb_drv;
  328. const struct usb_device_id *id;
  329. /* check for generic driver, which we don't match any device with */
  330. if (drv == &usb_generic_driver)
  331. return 0;
  332. intf = to_usb_interface(dev);
  333. usb_drv = to_usb_driver(drv);
  334. id = usb_match_id(intf, usb_drv->id_table);
  335. if (id)
  336. return 1;
  337. id = usb_match_dynamic_id(intf, usb_drv);
  338. if (id)
  339. return 1;
  340. return 0;
  341. }
  342. /**
  343. * usb_register_driver - register a USB driver
  344. * @new_driver: USB operations for the driver
  345. * @owner: module owner of this driver.
  346. *
  347. * Registers a USB driver with the USB core. The list of unattached
  348. * interfaces will be rescanned whenever a new driver is added, allowing
  349. * the new driver to attach to any recognized devices.
  350. * Returns a negative error code on failure and 0 on success.
  351. *
  352. * NOTE: if you want your driver to use the USB major number, you must call
  353. * usb_register_dev() to enable that functionality. This function no longer
  354. * takes care of that.
  355. */
  356. int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
  357. {
  358. int retval = 0;
  359. if (usb_disabled())
  360. return -ENODEV;
  361. new_driver->driver.name = (char *)new_driver->name;
  362. new_driver->driver.bus = &usb_bus_type;
  363. new_driver->driver.probe = usb_probe_interface;
  364. new_driver->driver.remove = usb_unbind_interface;
  365. new_driver->driver.owner = owner;
  366. spin_lock_init(&new_driver->dynids.lock);
  367. INIT_LIST_HEAD(&new_driver->dynids.list);
  368. retval = driver_register(&new_driver->driver);
  369. if (!retval) {
  370. pr_info("%s: registered new driver %s\n",
  371. usbcore_name, new_driver->name);
  372. usbfs_update_special();
  373. usb_create_newid_file(new_driver);
  374. } else {
  375. printk(KERN_ERR "%s: error %d registering driver %s\n",
  376. usbcore_name, retval, new_driver->name);
  377. }
  378. return retval;
  379. }
  380. EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
  381. /**
  382. * usb_deregister - unregister a USB driver
  383. * @driver: USB operations of the driver to unregister
  384. * Context: must be able to sleep
  385. *
  386. * Unlinks the specified driver from the internal USB driver list.
  387. *
  388. * NOTE: If you called usb_register_dev(), you still need to call
  389. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  390. * this * call will no longer do it for you.
  391. */
  392. void usb_deregister(struct usb_driver *driver)
  393. {
  394. pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
  395. usb_remove_newid_file(driver);
  396. usb_free_dynids(driver);
  397. driver_unregister(&driver->driver);
  398. usbfs_update_special();
  399. }
  400. EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);