driver.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. * matching, probing, releasing, suspending and resuming for
  21. * real drivers.
  22. *
  23. */
  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. #ifdef CONFIG_HOTPLUG
  35. /*
  36. * Adds a new dynamic USBdevice ID to this driver,
  37. * and cause the driver to probe for all devices again.
  38. */
  39. static ssize_t store_new_id(struct device_driver *driver,
  40. const char *buf, size_t count)
  41. {
  42. struct usb_driver *usb_drv = to_usb_driver(driver);
  43. struct usb_dynid *dynid;
  44. u32 idVendor = 0;
  45. u32 idProduct = 0;
  46. int fields = 0;
  47. fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
  48. if (fields < 2)
  49. return -EINVAL;
  50. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  51. if (!dynid)
  52. return -ENOMEM;
  53. INIT_LIST_HEAD(&dynid->node);
  54. dynid->id.idVendor = idVendor;
  55. dynid->id.idProduct = idProduct;
  56. dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  57. spin_lock(&usb_drv->dynids.lock);
  58. list_add_tail(&usb_drv->dynids.list, &dynid->node);
  59. spin_unlock(&usb_drv->dynids.lock);
  60. if (get_driver(driver)) {
  61. driver_attach(driver);
  62. put_driver(driver);
  63. }
  64. return count;
  65. }
  66. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  67. static int usb_create_newid_file(struct usb_driver *usb_drv)
  68. {
  69. int error = 0;
  70. if (usb_drv->no_dynamic_id)
  71. goto exit;
  72. if (usb_drv->probe != NULL)
  73. error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
  74. &driver_attr_new_id.attr);
  75. exit:
  76. return error;
  77. }
  78. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  79. {
  80. if (usb_drv->no_dynamic_id)
  81. return;
  82. if (usb_drv->probe != NULL)
  83. sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
  84. &driver_attr_new_id.attr);
  85. }
  86. static void usb_free_dynids(struct usb_driver *usb_drv)
  87. {
  88. struct usb_dynid *dynid, *n;
  89. spin_lock(&usb_drv->dynids.lock);
  90. list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
  91. list_del(&dynid->node);
  92. kfree(dynid);
  93. }
  94. spin_unlock(&usb_drv->dynids.lock);
  95. }
  96. #else
  97. static inline int usb_create_newid_file(struct usb_driver *usb_drv)
  98. {
  99. return 0;
  100. }
  101. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  102. {
  103. }
  104. static inline void usb_free_dynids(struct usb_driver *usb_drv)
  105. {
  106. }
  107. #endif
  108. static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
  109. struct usb_driver *drv)
  110. {
  111. struct usb_dynid *dynid;
  112. spin_lock(&drv->dynids.lock);
  113. list_for_each_entry(dynid, &drv->dynids.list, node) {
  114. if (usb_match_one_id(intf, &dynid->id)) {
  115. spin_unlock(&drv->dynids.lock);
  116. return &dynid->id;
  117. }
  118. }
  119. spin_unlock(&drv->dynids.lock);
  120. return NULL;
  121. }
  122. /* called from driver core with dev locked */
  123. static int usb_probe_device(struct device *dev)
  124. {
  125. struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  126. struct usb_device *udev;
  127. int error = -ENODEV;
  128. dev_dbg(dev, "%s\n", __FUNCTION__);
  129. if (!is_usb_device(dev)) /* Sanity check */
  130. return error;
  131. udev = to_usb_device(dev);
  132. /* FIXME: resume a suspended device */
  133. if (udev->state == USB_STATE_SUSPENDED)
  134. return -EHOSTUNREACH;
  135. /* TODO: Add real matching code */
  136. error = udriver->probe(udev);
  137. return error;
  138. }
  139. /* called from driver core with dev locked */
  140. static int usb_unbind_device(struct device *dev)
  141. {
  142. struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  143. udriver->disconnect(to_usb_device(dev));
  144. return 0;
  145. }
  146. /* called from driver core with dev locked */
  147. static int usb_probe_interface(struct device *dev)
  148. {
  149. struct usb_driver *driver = to_usb_driver(dev->driver);
  150. struct usb_interface *intf;
  151. const struct usb_device_id *id;
  152. int error = -ENODEV;
  153. dev_dbg(dev, "%s\n", __FUNCTION__);
  154. if (is_usb_device(dev)) /* Sanity check */
  155. return error;
  156. intf = to_usb_interface(dev);
  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 dev locked */
  181. static int usb_unbind_interface(struct device *dev)
  182. {
  183. struct usb_driver *driver = to_usb_driver(dev->driver);
  184. struct usb_interface *intf = to_usb_interface(dev);
  185. intf->condition = USB_INTERFACE_UNBINDING;
  186. /* release all urbs for this interface */
  187. usb_disable_interface(interface_to_usbdev(intf), intf);
  188. driver->disconnect(intf);
  189. /* reset other interface state */
  190. usb_set_interface(interface_to_usbdev(intf),
  191. intf->altsetting[0].desc.bInterfaceNumber,
  192. 0);
  193. usb_set_intfdata(intf, NULL);
  194. intf->condition = USB_INTERFACE_UNBOUND;
  195. mark_quiesced(intf);
  196. return 0;
  197. }
  198. /**
  199. * usb_driver_claim_interface - bind a driver to an interface
  200. * @driver: the driver to be bound
  201. * @iface: the interface to which it will be bound; must be in the
  202. * usb device's active configuration
  203. * @priv: driver data associated with that interface
  204. *
  205. * This is used by usb device drivers that need to claim more than one
  206. * interface on a device when probing (audio and acm are current examples).
  207. * No device driver should directly modify internal usb_interface or
  208. * usb_device structure members.
  209. *
  210. * Few drivers should need to use this routine, since the most natural
  211. * way to bind to an interface is to return the private data from
  212. * the driver's probe() method.
  213. *
  214. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  215. * writelock. So driver probe() entries don't need extra locking,
  216. * but other call contexts may need to explicitly claim those locks.
  217. */
  218. int usb_driver_claim_interface(struct usb_driver *driver,
  219. struct usb_interface *iface, void* priv)
  220. {
  221. struct device *dev = &iface->dev;
  222. if (dev->driver)
  223. return -EBUSY;
  224. dev->driver = &driver->drvwrap.driver;
  225. usb_set_intfdata(iface, priv);
  226. iface->condition = USB_INTERFACE_BOUND;
  227. mark_active(iface);
  228. /* if interface was already added, bind now; else let
  229. * the future device_add() bind it, bypassing probe()
  230. */
  231. if (device_is_registered(dev))
  232. device_bind_driver(dev);
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(usb_driver_claim_interface);
  236. /**
  237. * usb_driver_release_interface - unbind a driver from an interface
  238. * @driver: the driver to be unbound
  239. * @iface: the interface from which it will be unbound
  240. *
  241. * This can be used by drivers to release an interface without waiting
  242. * for their disconnect() methods to be called. In typical cases this
  243. * also causes the driver disconnect() method to be called.
  244. *
  245. * This call is synchronous, and may not be used in an interrupt context.
  246. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  247. * writelock. So driver disconnect() entries don't need extra locking,
  248. * but other call contexts may need to explicitly claim those locks.
  249. */
  250. void usb_driver_release_interface(struct usb_driver *driver,
  251. struct usb_interface *iface)
  252. {
  253. struct device *dev = &iface->dev;
  254. /* this should never happen, don't release something that's not ours */
  255. if (!dev->driver || dev->driver != &driver->drvwrap.driver)
  256. return;
  257. /* don't release from within disconnect() */
  258. if (iface->condition != USB_INTERFACE_BOUND)
  259. return;
  260. /* don't release if the interface hasn't been added yet */
  261. if (device_is_registered(dev)) {
  262. iface->condition = USB_INTERFACE_UNBINDING;
  263. device_release_driver(dev);
  264. }
  265. dev->driver = NULL;
  266. usb_set_intfdata(iface, NULL);
  267. iface->condition = USB_INTERFACE_UNBOUND;
  268. mark_quiesced(iface);
  269. }
  270. EXPORT_SYMBOL(usb_driver_release_interface);
  271. /* returns 0 if no match, 1 if match */
  272. static int usb_match_one_id(struct usb_interface *interface,
  273. const struct usb_device_id *id)
  274. {
  275. struct usb_host_interface *intf;
  276. struct usb_device *dev;
  277. /* proc_connectinfo in devio.c may call us with id == NULL. */
  278. if (id == NULL)
  279. return 0;
  280. intf = interface->cur_altsetting;
  281. dev = interface_to_usbdev(interface);
  282. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  283. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  284. return 0;
  285. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  286. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  287. return 0;
  288. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  289. greater than any unsigned number. */
  290. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  291. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  292. return 0;
  293. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  294. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  295. return 0;
  296. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  297. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  298. return 0;
  299. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  300. (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
  301. return 0;
  302. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  303. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  304. return 0;
  305. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  306. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  307. return 0;
  308. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  309. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  310. return 0;
  311. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  312. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  313. return 0;
  314. return 1;
  315. }
  316. /**
  317. * usb_match_id - find first usb_device_id matching device or interface
  318. * @interface: the interface of interest
  319. * @id: array of usb_device_id structures, terminated by zero entry
  320. *
  321. * usb_match_id searches an array of usb_device_id's and returns
  322. * the first one matching the device or interface, or null.
  323. * This is used when binding (or rebinding) a driver to an interface.
  324. * Most USB device drivers will use this indirectly, through the usb core,
  325. * but some layered driver frameworks use it directly.
  326. * These device tables are exported with MODULE_DEVICE_TABLE, through
  327. * modutils, to support the driver loading functionality of USB hotplugging.
  328. *
  329. * What Matches:
  330. *
  331. * The "match_flags" element in a usb_device_id controls which
  332. * members are used. If the corresponding bit is set, the
  333. * value in the device_id must match its corresponding member
  334. * in the device or interface descriptor, or else the device_id
  335. * does not match.
  336. *
  337. * "driver_info" is normally used only by device drivers,
  338. * but you can create a wildcard "matches anything" usb_device_id
  339. * as a driver's "modules.usbmap" entry if you provide an id with
  340. * only a nonzero "driver_info" field. If you do this, the USB device
  341. * driver's probe() routine should use additional intelligence to
  342. * decide whether to bind to the specified interface.
  343. *
  344. * What Makes Good usb_device_id Tables:
  345. *
  346. * The match algorithm is very simple, so that intelligence in
  347. * driver selection must come from smart driver id records.
  348. * Unless you have good reasons to use another selection policy,
  349. * provide match elements only in related groups, and order match
  350. * specifiers from specific to general. Use the macros provided
  351. * for that purpose if you can.
  352. *
  353. * The most specific match specifiers use device descriptor
  354. * data. These are commonly used with product-specific matches;
  355. * the USB_DEVICE macro lets you provide vendor and product IDs,
  356. * and you can also match against ranges of product revisions.
  357. * These are widely used for devices with application or vendor
  358. * specific bDeviceClass values.
  359. *
  360. * Matches based on device class/subclass/protocol specifications
  361. * are slightly more general; use the USB_DEVICE_INFO macro, or
  362. * its siblings. These are used with single-function devices
  363. * where bDeviceClass doesn't specify that each interface has
  364. * its own class.
  365. *
  366. * Matches based on interface class/subclass/protocol are the
  367. * most general; they let drivers bind to any interface on a
  368. * multiple-function device. Use the USB_INTERFACE_INFO
  369. * macro, or its siblings, to match class-per-interface style
  370. * devices (as recorded in bDeviceClass).
  371. *
  372. * Within those groups, remember that not all combinations are
  373. * meaningful. For example, don't give a product version range
  374. * without vendor and product IDs; or specify a protocol without
  375. * its associated class and subclass.
  376. */
  377. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  378. const struct usb_device_id *id)
  379. {
  380. /* proc_connectinfo in devio.c may call us with id == NULL. */
  381. if (id == NULL)
  382. return NULL;
  383. /* It is important to check that id->driver_info is nonzero,
  384. since an entry that is all zeroes except for a nonzero
  385. id->driver_info is the way to create an entry that
  386. indicates that the driver want to examine every
  387. device and interface. */
  388. for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
  389. id->driver_info; id++) {
  390. if (usb_match_one_id(interface, id))
  391. return id;
  392. }
  393. return NULL;
  394. }
  395. EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
  396. int usb_device_match(struct device *dev, struct device_driver *drv)
  397. {
  398. /* devices and interfaces are handled separately */
  399. if (is_usb_device(dev)) {
  400. /* interface drivers never match devices */
  401. if (!is_usb_device_driver(drv))
  402. return 0;
  403. /* TODO: Add real matching code */
  404. return 1;
  405. } else {
  406. struct usb_interface *intf;
  407. struct usb_driver *usb_drv;
  408. const struct usb_device_id *id;
  409. /* device drivers never match interfaces */
  410. if (is_usb_device_driver(drv))
  411. return 0;
  412. intf = to_usb_interface(dev);
  413. usb_drv = to_usb_driver(drv);
  414. id = usb_match_id(intf, usb_drv->id_table);
  415. if (id)
  416. return 1;
  417. id = usb_match_dynamic_id(intf, usb_drv);
  418. if (id)
  419. return 1;
  420. }
  421. return 0;
  422. }
  423. #ifdef CONFIG_HOTPLUG
  424. /*
  425. * This sends an uevent to userspace, typically helping to load driver
  426. * or other modules, configure the device, and more. Drivers can provide
  427. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  428. *
  429. * We're called either from khubd (the typical case) or from root hub
  430. * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
  431. * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
  432. * device (and this configuration!) are still present.
  433. */
  434. static int usb_uevent(struct device *dev, char **envp, int num_envp,
  435. char *buffer, int buffer_size)
  436. {
  437. struct usb_interface *intf;
  438. struct usb_device *usb_dev;
  439. struct usb_host_interface *alt;
  440. int i = 0;
  441. int length = 0;
  442. if (!dev)
  443. return -ENODEV;
  444. /* driver is often null here; dev_dbg() would oops */
  445. pr_debug ("usb %s: uevent\n", dev->bus_id);
  446. if (is_usb_device(dev)) {
  447. usb_dev = to_usb_device(dev);
  448. alt = NULL;
  449. } else {
  450. intf = to_usb_interface(dev);
  451. usb_dev = interface_to_usbdev(intf);
  452. alt = intf->cur_altsetting;
  453. }
  454. if (usb_dev->devnum < 0) {
  455. pr_debug ("usb %s: already deleted?\n", dev->bus_id);
  456. return -ENODEV;
  457. }
  458. if (!usb_dev->bus) {
  459. pr_debug ("usb %s: bus removed?\n", dev->bus_id);
  460. return -ENODEV;
  461. }
  462. #ifdef CONFIG_USB_DEVICEFS
  463. /* If this is available, userspace programs can directly read
  464. * all the device descriptors we don't tell them about. Or
  465. * even act as usermode drivers.
  466. *
  467. * FIXME reduce hardwired intelligence here
  468. */
  469. if (add_uevent_var(envp, num_envp, &i,
  470. buffer, buffer_size, &length,
  471. "DEVICE=/proc/bus/usb/%03d/%03d",
  472. usb_dev->bus->busnum, usb_dev->devnum))
  473. return -ENOMEM;
  474. #endif
  475. /* per-device configurations are common */
  476. if (add_uevent_var(envp, num_envp, &i,
  477. buffer, buffer_size, &length,
  478. "PRODUCT=%x/%x/%x",
  479. le16_to_cpu(usb_dev->descriptor.idVendor),
  480. le16_to_cpu(usb_dev->descriptor.idProduct),
  481. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  482. return -ENOMEM;
  483. /* class-based driver binding models */
  484. if (add_uevent_var(envp, num_envp, &i,
  485. buffer, buffer_size, &length,
  486. "TYPE=%d/%d/%d",
  487. usb_dev->descriptor.bDeviceClass,
  488. usb_dev->descriptor.bDeviceSubClass,
  489. usb_dev->descriptor.bDeviceProtocol))
  490. return -ENOMEM;
  491. if (!is_usb_device(dev)) {
  492. if (add_uevent_var(envp, num_envp, &i,
  493. buffer, buffer_size, &length,
  494. "INTERFACE=%d/%d/%d",
  495. alt->desc.bInterfaceClass,
  496. alt->desc.bInterfaceSubClass,
  497. alt->desc.bInterfaceProtocol))
  498. return -ENOMEM;
  499. if (add_uevent_var(envp, num_envp, &i,
  500. buffer, buffer_size, &length,
  501. "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
  502. le16_to_cpu(usb_dev->descriptor.idVendor),
  503. le16_to_cpu(usb_dev->descriptor.idProduct),
  504. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  505. usb_dev->descriptor.bDeviceClass,
  506. usb_dev->descriptor.bDeviceSubClass,
  507. usb_dev->descriptor.bDeviceProtocol,
  508. alt->desc.bInterfaceClass,
  509. alt->desc.bInterfaceSubClass,
  510. alt->desc.bInterfaceProtocol))
  511. return -ENOMEM;
  512. }
  513. envp[i] = NULL;
  514. return 0;
  515. }
  516. #else
  517. static int usb_uevent(struct device *dev, char **envp,
  518. int num_envp, char *buffer, int buffer_size)
  519. {
  520. return -ENODEV;
  521. }
  522. #endif /* CONFIG_HOTPLUG */
  523. /**
  524. * usb_register_device_driver - register a USB device (not interface) driver
  525. * @new_udriver: USB operations for the device driver
  526. * @owner: module owner of this driver.
  527. *
  528. * Registers a USB device driver with the USB core. The list of
  529. * unattached devices will be rescanned whenever a new driver is
  530. * added, allowing the new driver to attach to any recognized devices.
  531. * Returns a negative error code on failure and 0 on success.
  532. */
  533. int usb_register_device_driver(struct usb_device_driver *new_udriver,
  534. struct module *owner)
  535. {
  536. int retval = 0;
  537. if (usb_disabled())
  538. return -ENODEV;
  539. new_udriver->drvwrap.for_devices = 1;
  540. new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
  541. new_udriver->drvwrap.driver.bus = &usb_bus_type;
  542. new_udriver->drvwrap.driver.probe = usb_probe_device;
  543. new_udriver->drvwrap.driver.remove = usb_unbind_device;
  544. new_udriver->drvwrap.driver.owner = owner;
  545. retval = driver_register(&new_udriver->drvwrap.driver);
  546. if (!retval) {
  547. pr_info("%s: registered new device driver %s\n",
  548. usbcore_name, new_udriver->name);
  549. usbfs_update_special();
  550. } else {
  551. printk(KERN_ERR "%s: error %d registering device "
  552. " driver %s\n",
  553. usbcore_name, retval, new_udriver->name);
  554. }
  555. return retval;
  556. }
  557. EXPORT_SYMBOL_GPL(usb_register_device_driver);
  558. /**
  559. * usb_deregister_device_driver - unregister a USB device (not interface) driver
  560. * @udriver: USB operations of the device driver to unregister
  561. * Context: must be able to sleep
  562. *
  563. * Unlinks the specified driver from the internal USB driver list.
  564. */
  565. void usb_deregister_device_driver(struct usb_device_driver *udriver)
  566. {
  567. pr_info("%s: deregistering device driver %s\n",
  568. usbcore_name, udriver->name);
  569. driver_unregister(&udriver->drvwrap.driver);
  570. usbfs_update_special();
  571. }
  572. EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
  573. /**
  574. * usb_register_driver - register a USB interface driver
  575. * @new_driver: USB operations for the interface driver
  576. * @owner: module owner of this driver.
  577. *
  578. * Registers a USB interface driver with the USB core. The list of
  579. * unattached interfaces will be rescanned whenever a new driver is
  580. * added, allowing the new driver to attach to any recognized interfaces.
  581. * Returns a negative error code on failure and 0 on success.
  582. *
  583. * NOTE: if you want your driver to use the USB major number, you must call
  584. * usb_register_dev() to enable that functionality. This function no longer
  585. * takes care of that.
  586. */
  587. int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
  588. {
  589. int retval = 0;
  590. if (usb_disabled())
  591. return -ENODEV;
  592. new_driver->drvwrap.for_devices = 0;
  593. new_driver->drvwrap.driver.name = (char *) new_driver->name;
  594. new_driver->drvwrap.driver.bus = &usb_bus_type;
  595. new_driver->drvwrap.driver.probe = usb_probe_interface;
  596. new_driver->drvwrap.driver.remove = usb_unbind_interface;
  597. new_driver->drvwrap.driver.owner = owner;
  598. spin_lock_init(&new_driver->dynids.lock);
  599. INIT_LIST_HEAD(&new_driver->dynids.list);
  600. retval = driver_register(&new_driver->drvwrap.driver);
  601. if (!retval) {
  602. pr_info("%s: registered new interface driver %s\n",
  603. usbcore_name, new_driver->name);
  604. usbfs_update_special();
  605. usb_create_newid_file(new_driver);
  606. } else {
  607. printk(KERN_ERR "%s: error %d registering interface "
  608. " driver %s\n",
  609. usbcore_name, retval, new_driver->name);
  610. }
  611. return retval;
  612. }
  613. EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
  614. /**
  615. * usb_deregister - unregister a USB interface driver
  616. * @driver: USB operations of the interface driver to unregister
  617. * Context: must be able to sleep
  618. *
  619. * Unlinks the specified driver from the internal USB driver list.
  620. *
  621. * NOTE: If you called usb_register_dev(), you still need to call
  622. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  623. * this * call will no longer do it for you.
  624. */
  625. void usb_deregister(struct usb_driver *driver)
  626. {
  627. pr_info("%s: deregistering interface driver %s\n",
  628. usbcore_name, driver->name);
  629. usb_remove_newid_file(driver);
  630. usb_free_dynids(driver);
  631. driver_unregister(&driver->drvwrap.driver);
  632. usbfs_update_special();
  633. }
  634. EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
  635. #ifdef CONFIG_PM
  636. /* Caller has locked udev */
  637. static int suspend_device(struct usb_device *udev, pm_message_t msg)
  638. {
  639. struct usb_device_driver *udriver;
  640. int status = 0;
  641. if (udev->state == USB_STATE_NOTATTACHED ||
  642. udev->state == USB_STATE_SUSPENDED)
  643. goto done;
  644. /* For devices that don't have a driver, we do a standard suspend. */
  645. if (udev->dev.driver == NULL) {
  646. status = usb_port_suspend(udev);
  647. goto done;
  648. }
  649. udriver = to_usb_device_driver(udev->dev.driver);
  650. status = udriver->suspend(udev, msg);
  651. done:
  652. if (status == 0)
  653. udev->dev.power.power_state.event = msg.event;
  654. return status;
  655. }
  656. /* Caller has locked udev */
  657. static int resume_device(struct usb_device *udev)
  658. {
  659. struct usb_device_driver *udriver;
  660. int status = 0;
  661. if (udev->state == USB_STATE_NOTATTACHED ||
  662. udev->state != USB_STATE_SUSPENDED)
  663. goto done;
  664. /* Can't resume it if it doesn't have a driver. */
  665. if (udev->dev.driver == NULL) {
  666. status = -ENOTCONN;
  667. goto done;
  668. }
  669. udriver = to_usb_device_driver(udev->dev.driver);
  670. status = udriver->resume(udev);
  671. done:
  672. if (status == 0)
  673. udev->dev.power.power_state.event = PM_EVENT_ON;
  674. return status;
  675. }
  676. /* Caller has locked intf's usb_device */
  677. static int suspend_interface(struct usb_interface *intf, pm_message_t msg)
  678. {
  679. struct usb_driver *driver;
  680. int status = 0;
  681. /* with no hardware, USB interfaces only use FREEZE and ON states */
  682. if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
  683. !is_active(intf))
  684. goto done;
  685. if (intf->dev.driver == NULL) /* This can't happen */
  686. goto done;
  687. driver = to_usb_driver(intf->dev.driver);
  688. if (driver->suspend && driver->resume) {
  689. status = driver->suspend(intf, msg);
  690. if (status)
  691. dev_err(&intf->dev, "%s error %d\n",
  692. "suspend", status);
  693. else
  694. mark_quiesced(intf);
  695. } else {
  696. // FIXME else if there's no suspend method, disconnect...
  697. dev_warn(&intf->dev, "no suspend for driver %s?\n",
  698. driver->name);
  699. mark_quiesced(intf);
  700. }
  701. done:
  702. if (status == 0)
  703. intf->dev.power.power_state.event = msg.event;
  704. return status;
  705. }
  706. /* Caller has locked intf's usb_device */
  707. static int resume_interface(struct usb_interface *intf)
  708. {
  709. struct usb_driver *driver;
  710. int status = 0;
  711. if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
  712. is_active(intf))
  713. goto done;
  714. /* Can't resume it if it doesn't have a driver. */
  715. if (intf->dev.driver == NULL) {
  716. status = -ENOTCONN;
  717. goto done;
  718. }
  719. driver = to_usb_driver(intf->dev.driver);
  720. if (driver->resume) {
  721. status = driver->resume(intf);
  722. if (status)
  723. dev_err(&intf->dev, "%s error %d\n",
  724. "resume", status);
  725. else
  726. mark_active(intf);
  727. } else {
  728. dev_warn(&intf->dev, "no resume for driver %s?\n",
  729. driver->name);
  730. mark_active(intf);
  731. }
  732. done:
  733. if (status == 0)
  734. intf->dev.power.power_state.event = PM_EVENT_ON;
  735. return status;
  736. }
  737. /* Caller has locked udev */
  738. int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
  739. {
  740. int status = 0;
  741. int i = 0;
  742. struct usb_interface *intf;
  743. if (udev->actconfig) {
  744. for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
  745. intf = udev->actconfig->interface[i];
  746. status = suspend_interface(intf, msg);
  747. if (status != 0)
  748. break;
  749. }
  750. }
  751. if (status == 0)
  752. status = suspend_device(udev, msg);
  753. /* If the suspend failed, resume interfaces that did get suspended */
  754. if (status != 0) {
  755. while (--i >= 0) {
  756. intf = udev->actconfig->interface[i];
  757. resume_interface(intf);
  758. }
  759. }
  760. return status;
  761. }
  762. /* Caller has locked udev */
  763. int usb_resume_both(struct usb_device *udev)
  764. {
  765. int status;
  766. int i;
  767. struct usb_interface *intf;
  768. /* Can't resume if the parent is suspended */
  769. if (udev->parent && udev->parent->state == USB_STATE_SUSPENDED) {
  770. dev_warn(&udev->dev, "can't resume; parent is suspended\n");
  771. return -EHOSTUNREACH;
  772. }
  773. status = resume_device(udev);
  774. if (status == 0 && udev->actconfig) {
  775. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  776. intf = udev->actconfig->interface[i];
  777. resume_interface(intf);
  778. }
  779. }
  780. return status;
  781. }
  782. static int usb_suspend(struct device *dev, pm_message_t message)
  783. {
  784. int status;
  785. if (is_usb_device(dev))
  786. status = usb_suspend_both(to_usb_device(dev), message);
  787. else
  788. status = 0;
  789. return status;
  790. }
  791. static int usb_resume(struct device *dev)
  792. {
  793. int status;
  794. if (is_usb_device(dev)) {
  795. status = usb_resume_both(to_usb_device(dev));
  796. /* Rebind drivers that had no suspend method? */
  797. } else
  798. status = 0;
  799. return status;
  800. }
  801. #endif /* CONFIG_PM */
  802. struct bus_type usb_bus_type = {
  803. .name = "usb",
  804. .match = usb_device_match,
  805. .uevent = usb_uevent,
  806. #ifdef CONFIG_PM
  807. .suspend = usb_suspend,
  808. .resume = usb_resume,
  809. #endif
  810. };