driver.c 14 KB

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