driver.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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 <linux/workqueue.h>
  27. #include "hcd.h"
  28. #include "usb.h"
  29. static int usb_match_one_id(struct usb_interface *interface,
  30. const struct usb_device_id *id);
  31. struct usb_dynid {
  32. struct list_head node;
  33. struct usb_device_id id;
  34. };
  35. #ifdef CONFIG_HOTPLUG
  36. /*
  37. * Adds a new dynamic USBdevice ID to this driver,
  38. * and cause the driver to probe for all devices again.
  39. */
  40. static ssize_t store_new_id(struct device_driver *driver,
  41. const char *buf, size_t count)
  42. {
  43. struct usb_driver *usb_drv = to_usb_driver(driver);
  44. struct usb_dynid *dynid;
  45. u32 idVendor = 0;
  46. u32 idProduct = 0;
  47. int fields = 0;
  48. int retval = 0;
  49. fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
  50. if (fields < 2)
  51. return -EINVAL;
  52. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  53. if (!dynid)
  54. return -ENOMEM;
  55. INIT_LIST_HEAD(&dynid->node);
  56. dynid->id.idVendor = idVendor;
  57. dynid->id.idProduct = idProduct;
  58. dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  59. spin_lock(&usb_drv->dynids.lock);
  60. list_add_tail(&usb_drv->dynids.list, &dynid->node);
  61. spin_unlock(&usb_drv->dynids.lock);
  62. if (get_driver(driver)) {
  63. retval = driver_attach(driver);
  64. put_driver(driver);
  65. }
  66. if (retval)
  67. return retval;
  68. return count;
  69. }
  70. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  71. static int usb_create_newid_file(struct usb_driver *usb_drv)
  72. {
  73. int error = 0;
  74. if (usb_drv->no_dynamic_id)
  75. goto exit;
  76. if (usb_drv->probe != NULL)
  77. error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
  78. &driver_attr_new_id.attr);
  79. exit:
  80. return error;
  81. }
  82. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  83. {
  84. if (usb_drv->no_dynamic_id)
  85. return;
  86. if (usb_drv->probe != NULL)
  87. sysfs_remove_file(&usb_drv->drvwrap.driver.kobj,
  88. &driver_attr_new_id.attr);
  89. }
  90. static void usb_free_dynids(struct usb_driver *usb_drv)
  91. {
  92. struct usb_dynid *dynid, *n;
  93. spin_lock(&usb_drv->dynids.lock);
  94. list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
  95. list_del(&dynid->node);
  96. kfree(dynid);
  97. }
  98. spin_unlock(&usb_drv->dynids.lock);
  99. }
  100. #else
  101. static inline int usb_create_newid_file(struct usb_driver *usb_drv)
  102. {
  103. return 0;
  104. }
  105. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  106. {
  107. }
  108. static inline void usb_free_dynids(struct usb_driver *usb_drv)
  109. {
  110. }
  111. #endif
  112. static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
  113. struct usb_driver *drv)
  114. {
  115. struct usb_dynid *dynid;
  116. spin_lock(&drv->dynids.lock);
  117. list_for_each_entry(dynid, &drv->dynids.list, node) {
  118. if (usb_match_one_id(intf, &dynid->id)) {
  119. spin_unlock(&drv->dynids.lock);
  120. return &dynid->id;
  121. }
  122. }
  123. spin_unlock(&drv->dynids.lock);
  124. return NULL;
  125. }
  126. /* called from driver core with dev locked */
  127. static int usb_probe_device(struct device *dev)
  128. {
  129. struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  130. struct usb_device *udev;
  131. int error = -ENODEV;
  132. dev_dbg(dev, "%s\n", __FUNCTION__);
  133. if (!is_usb_device(dev)) /* Sanity check */
  134. return error;
  135. udev = to_usb_device(dev);
  136. /* TODO: Add real matching code */
  137. /* The device should always appear to be in use
  138. * unless the driver suports autosuspend.
  139. */
  140. udev->pm_usage_cnt = !(udriver->supports_autosuspend);
  141. error = udriver->probe(udev);
  142. return error;
  143. }
  144. /* called from driver core with dev locked */
  145. static int usb_unbind_device(struct device *dev)
  146. {
  147. struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  148. udriver->disconnect(to_usb_device(dev));
  149. return 0;
  150. }
  151. /* called from driver core with dev locked */
  152. static int usb_probe_interface(struct device *dev)
  153. {
  154. struct usb_driver *driver = to_usb_driver(dev->driver);
  155. struct usb_interface *intf;
  156. struct usb_device *udev;
  157. const struct usb_device_id *id;
  158. int error = -ENODEV;
  159. dev_dbg(dev, "%s\n", __FUNCTION__);
  160. if (is_usb_device(dev)) /* Sanity check */
  161. return error;
  162. intf = to_usb_interface(dev);
  163. udev = interface_to_usbdev(intf);
  164. id = usb_match_id(intf, driver->id_table);
  165. if (!id)
  166. id = usb_match_dynamic_id(intf, driver);
  167. if (id) {
  168. dev_dbg(dev, "%s - got id\n", __FUNCTION__);
  169. error = usb_autoresume_device(udev, 1);
  170. if (error)
  171. return error;
  172. /* Interface "power state" doesn't correspond to any hardware
  173. * state whatsoever. We use it to record when it's bound to
  174. * a driver that may start I/0: it's not frozen/quiesced.
  175. */
  176. mark_active(intf);
  177. intf->condition = USB_INTERFACE_BINDING;
  178. /* The interface should always appear to be in use
  179. * unless the driver suports autosuspend.
  180. */
  181. intf->pm_usage_cnt = !(driver->supports_autosuspend);
  182. error = driver->probe(intf, id);
  183. if (error) {
  184. mark_quiesced(intf);
  185. intf->needs_remote_wakeup = 0;
  186. intf->condition = USB_INTERFACE_UNBOUND;
  187. } else
  188. intf->condition = USB_INTERFACE_BOUND;
  189. usb_autosuspend_device(udev, 1);
  190. }
  191. return error;
  192. }
  193. /* called from driver core with dev locked */
  194. static int usb_unbind_interface(struct device *dev)
  195. {
  196. struct usb_driver *driver = to_usb_driver(dev->driver);
  197. struct usb_interface *intf = to_usb_interface(dev);
  198. struct usb_device *udev;
  199. int error;
  200. intf->condition = USB_INTERFACE_UNBINDING;
  201. /* Autoresume for set_interface call below */
  202. udev = interface_to_usbdev(intf);
  203. error = usb_autoresume_device(udev, 1);
  204. /* release all urbs for this interface */
  205. usb_disable_interface(interface_to_usbdev(intf), intf);
  206. driver->disconnect(intf);
  207. /* reset other interface state */
  208. usb_set_interface(interface_to_usbdev(intf),
  209. intf->altsetting[0].desc.bInterfaceNumber,
  210. 0);
  211. usb_set_intfdata(intf, NULL);
  212. intf->condition = USB_INTERFACE_UNBOUND;
  213. mark_quiesced(intf);
  214. intf->needs_remote_wakeup = 0;
  215. if (!error)
  216. usb_autosuspend_device(udev, 1);
  217. return 0;
  218. }
  219. /**
  220. * usb_driver_claim_interface - bind a driver to an interface
  221. * @driver: the driver to be bound
  222. * @iface: the interface to which it will be bound; must be in the
  223. * usb device's active configuration
  224. * @priv: driver data associated with that interface
  225. *
  226. * This is used by usb device drivers that need to claim more than one
  227. * interface on a device when probing (audio and acm are current examples).
  228. * No device driver should directly modify internal usb_interface or
  229. * usb_device structure members.
  230. *
  231. * Few drivers should need to use this routine, since the most natural
  232. * way to bind to an interface is to return the private data from
  233. * the driver's probe() method.
  234. *
  235. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  236. * writelock. So driver probe() entries don't need extra locking,
  237. * but other call contexts may need to explicitly claim those locks.
  238. */
  239. int usb_driver_claim_interface(struct usb_driver *driver,
  240. struct usb_interface *iface, void* priv)
  241. {
  242. struct device *dev = &iface->dev;
  243. struct usb_device *udev = interface_to_usbdev(iface);
  244. int retval = 0;
  245. if (dev->driver)
  246. return -EBUSY;
  247. dev->driver = &driver->drvwrap.driver;
  248. usb_set_intfdata(iface, priv);
  249. usb_pm_lock(udev);
  250. iface->condition = USB_INTERFACE_BOUND;
  251. mark_active(iface);
  252. iface->pm_usage_cnt = !(driver->supports_autosuspend);
  253. usb_pm_unlock(udev);
  254. /* if interface was already added, bind now; else let
  255. * the future device_add() bind it, bypassing probe()
  256. */
  257. if (device_is_registered(dev))
  258. retval = device_bind_driver(dev);
  259. return retval;
  260. }
  261. EXPORT_SYMBOL(usb_driver_claim_interface);
  262. /**
  263. * usb_driver_release_interface - unbind a driver from an interface
  264. * @driver: the driver to be unbound
  265. * @iface: the interface from which it will be unbound
  266. *
  267. * This can be used by drivers to release an interface without waiting
  268. * for their disconnect() methods to be called. In typical cases this
  269. * also causes the driver disconnect() method to be called.
  270. *
  271. * This call is synchronous, and may not be used in an interrupt context.
  272. * Callers must own the device lock and the driver model's usb_bus_type.subsys
  273. * writelock. So driver disconnect() entries don't need extra locking,
  274. * but other call contexts may need to explicitly claim those locks.
  275. */
  276. void usb_driver_release_interface(struct usb_driver *driver,
  277. struct usb_interface *iface)
  278. {
  279. struct device *dev = &iface->dev;
  280. struct usb_device *udev = interface_to_usbdev(iface);
  281. /* this should never happen, don't release something that's not ours */
  282. if (!dev->driver || dev->driver != &driver->drvwrap.driver)
  283. return;
  284. /* don't release from within disconnect() */
  285. if (iface->condition != USB_INTERFACE_BOUND)
  286. return;
  287. /* don't release if the interface hasn't been added yet */
  288. if (device_is_registered(dev)) {
  289. iface->condition = USB_INTERFACE_UNBINDING;
  290. device_release_driver(dev);
  291. }
  292. dev->driver = NULL;
  293. usb_set_intfdata(iface, NULL);
  294. usb_pm_lock(udev);
  295. iface->condition = USB_INTERFACE_UNBOUND;
  296. mark_quiesced(iface);
  297. iface->needs_remote_wakeup = 0;
  298. usb_pm_unlock(udev);
  299. }
  300. EXPORT_SYMBOL(usb_driver_release_interface);
  301. /* returns 0 if no match, 1 if match */
  302. static int usb_match_one_id(struct usb_interface *interface,
  303. const struct usb_device_id *id)
  304. {
  305. struct usb_host_interface *intf;
  306. struct usb_device *dev;
  307. /* proc_connectinfo in devio.c may call us with id == NULL. */
  308. if (id == NULL)
  309. return 0;
  310. intf = interface->cur_altsetting;
  311. dev = interface_to_usbdev(interface);
  312. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  313. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  314. return 0;
  315. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  316. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  317. return 0;
  318. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  319. greater than any unsigned number. */
  320. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  321. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  322. return 0;
  323. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  324. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  325. return 0;
  326. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  327. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  328. return 0;
  329. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  330. (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
  331. return 0;
  332. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  333. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  334. return 0;
  335. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  336. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  337. return 0;
  338. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  339. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  340. return 0;
  341. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  342. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  343. return 0;
  344. return 1;
  345. }
  346. /**
  347. * usb_match_id - find first usb_device_id matching device or interface
  348. * @interface: the interface of interest
  349. * @id: array of usb_device_id structures, terminated by zero entry
  350. *
  351. * usb_match_id searches an array of usb_device_id's and returns
  352. * the first one matching the device or interface, or null.
  353. * This is used when binding (or rebinding) a driver to an interface.
  354. * Most USB device drivers will use this indirectly, through the usb core,
  355. * but some layered driver frameworks use it directly.
  356. * These device tables are exported with MODULE_DEVICE_TABLE, through
  357. * modutils, to support the driver loading functionality of USB hotplugging.
  358. *
  359. * What Matches:
  360. *
  361. * The "match_flags" element in a usb_device_id controls which
  362. * members are used. If the corresponding bit is set, the
  363. * value in the device_id must match its corresponding member
  364. * in the device or interface descriptor, or else the device_id
  365. * does not match.
  366. *
  367. * "driver_info" is normally used only by device drivers,
  368. * but you can create a wildcard "matches anything" usb_device_id
  369. * as a driver's "modules.usbmap" entry if you provide an id with
  370. * only a nonzero "driver_info" field. If you do this, the USB device
  371. * driver's probe() routine should use additional intelligence to
  372. * decide whether to bind to the specified interface.
  373. *
  374. * What Makes Good usb_device_id Tables:
  375. *
  376. * The match algorithm is very simple, so that intelligence in
  377. * driver selection must come from smart driver id records.
  378. * Unless you have good reasons to use another selection policy,
  379. * provide match elements only in related groups, and order match
  380. * specifiers from specific to general. Use the macros provided
  381. * for that purpose if you can.
  382. *
  383. * The most specific match specifiers use device descriptor
  384. * data. These are commonly used with product-specific matches;
  385. * the USB_DEVICE macro lets you provide vendor and product IDs,
  386. * and you can also match against ranges of product revisions.
  387. * These are widely used for devices with application or vendor
  388. * specific bDeviceClass values.
  389. *
  390. * Matches based on device class/subclass/protocol specifications
  391. * are slightly more general; use the USB_DEVICE_INFO macro, or
  392. * its siblings. These are used with single-function devices
  393. * where bDeviceClass doesn't specify that each interface has
  394. * its own class.
  395. *
  396. * Matches based on interface class/subclass/protocol are the
  397. * most general; they let drivers bind to any interface on a
  398. * multiple-function device. Use the USB_INTERFACE_INFO
  399. * macro, or its siblings, to match class-per-interface style
  400. * devices (as recorded in bDeviceClass).
  401. *
  402. * Within those groups, remember that not all combinations are
  403. * meaningful. For example, don't give a product version range
  404. * without vendor and product IDs; or specify a protocol without
  405. * its associated class and subclass.
  406. */
  407. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  408. const struct usb_device_id *id)
  409. {
  410. /* proc_connectinfo in devio.c may call us with id == NULL. */
  411. if (id == NULL)
  412. return NULL;
  413. /* It is important to check that id->driver_info is nonzero,
  414. since an entry that is all zeroes except for a nonzero
  415. id->driver_info is the way to create an entry that
  416. indicates that the driver want to examine every
  417. device and interface. */
  418. for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
  419. id->driver_info; id++) {
  420. if (usb_match_one_id(interface, id))
  421. return id;
  422. }
  423. return NULL;
  424. }
  425. EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
  426. int usb_device_match(struct device *dev, struct device_driver *drv)
  427. {
  428. /* devices and interfaces are handled separately */
  429. if (is_usb_device(dev)) {
  430. /* interface drivers never match devices */
  431. if (!is_usb_device_driver(drv))
  432. return 0;
  433. /* TODO: Add real matching code */
  434. return 1;
  435. } else {
  436. struct usb_interface *intf;
  437. struct usb_driver *usb_drv;
  438. const struct usb_device_id *id;
  439. /* device drivers never match interfaces */
  440. if (is_usb_device_driver(drv))
  441. return 0;
  442. intf = to_usb_interface(dev);
  443. usb_drv = to_usb_driver(drv);
  444. id = usb_match_id(intf, usb_drv->id_table);
  445. if (id)
  446. return 1;
  447. id = usb_match_dynamic_id(intf, usb_drv);
  448. if (id)
  449. return 1;
  450. }
  451. return 0;
  452. }
  453. #ifdef CONFIG_HOTPLUG
  454. /*
  455. * This sends an uevent to userspace, typically helping to load driver
  456. * or other modules, configure the device, and more. Drivers can provide
  457. * a MODULE_DEVICE_TABLE to help with module loading subtasks.
  458. *
  459. * We're called either from khubd (the typical case) or from root hub
  460. * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
  461. * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
  462. * device (and this configuration!) are still present.
  463. */
  464. static int usb_uevent(struct device *dev, char **envp, int num_envp,
  465. char *buffer, int buffer_size)
  466. {
  467. struct usb_interface *intf;
  468. struct usb_device *usb_dev;
  469. struct usb_host_interface *alt;
  470. int i = 0;
  471. int length = 0;
  472. if (!dev)
  473. return -ENODEV;
  474. /* driver is often null here; dev_dbg() would oops */
  475. pr_debug ("usb %s: uevent\n", dev->bus_id);
  476. if (is_usb_device(dev)) {
  477. usb_dev = to_usb_device(dev);
  478. alt = NULL;
  479. } else {
  480. intf = to_usb_interface(dev);
  481. usb_dev = interface_to_usbdev(intf);
  482. alt = intf->cur_altsetting;
  483. }
  484. if (usb_dev->devnum < 0) {
  485. pr_debug ("usb %s: already deleted?\n", dev->bus_id);
  486. return -ENODEV;
  487. }
  488. if (!usb_dev->bus) {
  489. pr_debug ("usb %s: bus removed?\n", dev->bus_id);
  490. return -ENODEV;
  491. }
  492. #ifdef CONFIG_USB_DEVICEFS
  493. /* If this is available, userspace programs can directly read
  494. * all the device descriptors we don't tell them about. Or
  495. * even act as usermode drivers.
  496. *
  497. * FIXME reduce hardwired intelligence here
  498. */
  499. if (add_uevent_var(envp, num_envp, &i,
  500. buffer, buffer_size, &length,
  501. "DEVICE=/proc/bus/usb/%03d/%03d",
  502. usb_dev->bus->busnum, usb_dev->devnum))
  503. return -ENOMEM;
  504. #endif
  505. /* per-device configurations are common */
  506. if (add_uevent_var(envp, num_envp, &i,
  507. buffer, buffer_size, &length,
  508. "PRODUCT=%x/%x/%x",
  509. le16_to_cpu(usb_dev->descriptor.idVendor),
  510. le16_to_cpu(usb_dev->descriptor.idProduct),
  511. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  512. return -ENOMEM;
  513. /* class-based driver binding models */
  514. if (add_uevent_var(envp, num_envp, &i,
  515. buffer, buffer_size, &length,
  516. "TYPE=%d/%d/%d",
  517. usb_dev->descriptor.bDeviceClass,
  518. usb_dev->descriptor.bDeviceSubClass,
  519. usb_dev->descriptor.bDeviceProtocol))
  520. return -ENOMEM;
  521. if (!is_usb_device(dev)) {
  522. if (add_uevent_var(envp, num_envp, &i,
  523. buffer, buffer_size, &length,
  524. "INTERFACE=%d/%d/%d",
  525. alt->desc.bInterfaceClass,
  526. alt->desc.bInterfaceSubClass,
  527. alt->desc.bInterfaceProtocol))
  528. return -ENOMEM;
  529. if (add_uevent_var(envp, num_envp, &i,
  530. buffer, buffer_size, &length,
  531. "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
  532. le16_to_cpu(usb_dev->descriptor.idVendor),
  533. le16_to_cpu(usb_dev->descriptor.idProduct),
  534. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  535. usb_dev->descriptor.bDeviceClass,
  536. usb_dev->descriptor.bDeviceSubClass,
  537. usb_dev->descriptor.bDeviceProtocol,
  538. alt->desc.bInterfaceClass,
  539. alt->desc.bInterfaceSubClass,
  540. alt->desc.bInterfaceProtocol))
  541. return -ENOMEM;
  542. }
  543. envp[i] = NULL;
  544. return 0;
  545. }
  546. #else
  547. static int usb_uevent(struct device *dev, char **envp,
  548. int num_envp, char *buffer, int buffer_size)
  549. {
  550. return -ENODEV;
  551. }
  552. #endif /* CONFIG_HOTPLUG */
  553. /**
  554. * usb_register_device_driver - register a USB device (not interface) driver
  555. * @new_udriver: USB operations for the device driver
  556. * @owner: module owner of this driver.
  557. *
  558. * Registers a USB device driver with the USB core. The list of
  559. * unattached devices will be rescanned whenever a new driver is
  560. * added, allowing the new driver to attach to any recognized devices.
  561. * Returns a negative error code on failure and 0 on success.
  562. */
  563. int usb_register_device_driver(struct usb_device_driver *new_udriver,
  564. struct module *owner)
  565. {
  566. int retval = 0;
  567. if (usb_disabled())
  568. return -ENODEV;
  569. new_udriver->drvwrap.for_devices = 1;
  570. new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
  571. new_udriver->drvwrap.driver.bus = &usb_bus_type;
  572. new_udriver->drvwrap.driver.probe = usb_probe_device;
  573. new_udriver->drvwrap.driver.remove = usb_unbind_device;
  574. new_udriver->drvwrap.driver.owner = owner;
  575. retval = driver_register(&new_udriver->drvwrap.driver);
  576. if (!retval) {
  577. pr_info("%s: registered new device driver %s\n",
  578. usbcore_name, new_udriver->name);
  579. usbfs_update_special();
  580. } else {
  581. printk(KERN_ERR "%s: error %d registering device "
  582. " driver %s\n",
  583. usbcore_name, retval, new_udriver->name);
  584. }
  585. return retval;
  586. }
  587. EXPORT_SYMBOL_GPL(usb_register_device_driver);
  588. /**
  589. * usb_deregister_device_driver - unregister a USB device (not interface) driver
  590. * @udriver: USB operations of the device driver to unregister
  591. * Context: must be able to sleep
  592. *
  593. * Unlinks the specified driver from the internal USB driver list.
  594. */
  595. void usb_deregister_device_driver(struct usb_device_driver *udriver)
  596. {
  597. pr_info("%s: deregistering device driver %s\n",
  598. usbcore_name, udriver->name);
  599. driver_unregister(&udriver->drvwrap.driver);
  600. usbfs_update_special();
  601. }
  602. EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
  603. /**
  604. * usb_register_driver - register a USB interface driver
  605. * @new_driver: USB operations for the interface driver
  606. * @owner: module owner of this driver.
  607. *
  608. * Registers a USB interface driver with the USB core. The list of
  609. * unattached interfaces will be rescanned whenever a new driver is
  610. * added, allowing the new driver to attach to any recognized interfaces.
  611. * Returns a negative error code on failure and 0 on success.
  612. *
  613. * NOTE: if you want your driver to use the USB major number, you must call
  614. * usb_register_dev() to enable that functionality. This function no longer
  615. * takes care of that.
  616. */
  617. int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
  618. {
  619. int retval = 0;
  620. if (usb_disabled())
  621. return -ENODEV;
  622. new_driver->drvwrap.for_devices = 0;
  623. new_driver->drvwrap.driver.name = (char *) new_driver->name;
  624. new_driver->drvwrap.driver.bus = &usb_bus_type;
  625. new_driver->drvwrap.driver.probe = usb_probe_interface;
  626. new_driver->drvwrap.driver.remove = usb_unbind_interface;
  627. new_driver->drvwrap.driver.owner = owner;
  628. spin_lock_init(&new_driver->dynids.lock);
  629. INIT_LIST_HEAD(&new_driver->dynids.list);
  630. retval = driver_register(&new_driver->drvwrap.driver);
  631. if (!retval) {
  632. pr_info("%s: registered new interface driver %s\n",
  633. usbcore_name, new_driver->name);
  634. usbfs_update_special();
  635. usb_create_newid_file(new_driver);
  636. } else {
  637. printk(KERN_ERR "%s: error %d registering interface "
  638. " driver %s\n",
  639. usbcore_name, retval, new_driver->name);
  640. }
  641. return retval;
  642. }
  643. EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
  644. /**
  645. * usb_deregister - unregister a USB interface driver
  646. * @driver: USB operations of the interface driver to unregister
  647. * Context: must be able to sleep
  648. *
  649. * Unlinks the specified driver from the internal USB driver list.
  650. *
  651. * NOTE: If you called usb_register_dev(), you still need to call
  652. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  653. * this * call will no longer do it for you.
  654. */
  655. void usb_deregister(struct usb_driver *driver)
  656. {
  657. pr_info("%s: deregistering interface driver %s\n",
  658. usbcore_name, driver->name);
  659. usb_remove_newid_file(driver);
  660. usb_free_dynids(driver);
  661. driver_unregister(&driver->drvwrap.driver);
  662. usbfs_update_special();
  663. }
  664. EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
  665. #ifdef CONFIG_PM
  666. /* Caller has locked udev's pm_mutex */
  667. static int suspend_device(struct usb_device *udev, pm_message_t msg)
  668. {
  669. struct usb_device_driver *udriver;
  670. int status = 0;
  671. if (udev->state == USB_STATE_NOTATTACHED ||
  672. udev->state == USB_STATE_SUSPENDED)
  673. goto done;
  674. /* For devices that don't have a driver, we do a standard suspend. */
  675. if (udev->dev.driver == NULL) {
  676. udev->do_remote_wakeup = 0;
  677. status = usb_port_suspend(udev);
  678. goto done;
  679. }
  680. udriver = to_usb_device_driver(udev->dev.driver);
  681. status = udriver->suspend(udev, msg);
  682. done:
  683. // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  684. if (status == 0)
  685. udev->dev.power.power_state.event = msg.event;
  686. return status;
  687. }
  688. /* Caller has locked udev's pm_mutex */
  689. static int resume_device(struct usb_device *udev)
  690. {
  691. struct usb_device_driver *udriver;
  692. int status = 0;
  693. if (udev->state == USB_STATE_NOTATTACHED ||
  694. udev->state != USB_STATE_SUSPENDED)
  695. goto done;
  696. /* Can't resume it if it doesn't have a driver. */
  697. if (udev->dev.driver == NULL) {
  698. status = -ENOTCONN;
  699. goto done;
  700. }
  701. udriver = to_usb_device_driver(udev->dev.driver);
  702. status = udriver->resume(udev);
  703. done:
  704. // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  705. if (status == 0)
  706. udev->dev.power.power_state.event = PM_EVENT_ON;
  707. return status;
  708. }
  709. /* Caller has locked intf's usb_device's pm mutex */
  710. static int suspend_interface(struct usb_interface *intf, pm_message_t msg)
  711. {
  712. struct usb_driver *driver;
  713. int status = 0;
  714. /* with no hardware, USB interfaces only use FREEZE and ON states */
  715. if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
  716. !is_active(intf))
  717. goto done;
  718. if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
  719. goto done;
  720. driver = to_usb_driver(intf->dev.driver);
  721. if (driver->suspend && driver->resume) {
  722. status = driver->suspend(intf, msg);
  723. if (status == 0)
  724. mark_quiesced(intf);
  725. else if (!interface_to_usbdev(intf)->auto_pm)
  726. dev_err(&intf->dev, "%s error %d\n",
  727. "suspend", status);
  728. } else {
  729. // FIXME else if there's no suspend method, disconnect...
  730. // Not possible if auto_pm is set...
  731. dev_warn(&intf->dev, "no suspend for driver %s?\n",
  732. driver->name);
  733. mark_quiesced(intf);
  734. }
  735. done:
  736. // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
  737. if (status == 0)
  738. intf->dev.power.power_state.event = msg.event;
  739. return status;
  740. }
  741. /* Caller has locked intf's usb_device's pm_mutex */
  742. static int resume_interface(struct usb_interface *intf)
  743. {
  744. struct usb_driver *driver;
  745. int status = 0;
  746. if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
  747. is_active(intf))
  748. goto done;
  749. /* Don't let autoresume interfere with unbinding */
  750. if (intf->condition == USB_INTERFACE_UNBINDING)
  751. goto done;
  752. /* Can't resume it if it doesn't have a driver. */
  753. if (intf->condition == USB_INTERFACE_UNBOUND) {
  754. status = -ENOTCONN;
  755. goto done;
  756. }
  757. driver = to_usb_driver(intf->dev.driver);
  758. if (driver->resume) {
  759. status = driver->resume(intf);
  760. if (status)
  761. dev_err(&intf->dev, "%s error %d\n",
  762. "resume", status);
  763. else
  764. mark_active(intf);
  765. } else {
  766. dev_warn(&intf->dev, "no resume for driver %s?\n",
  767. driver->name);
  768. mark_active(intf);
  769. }
  770. done:
  771. // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
  772. if (status == 0)
  773. intf->dev.power.power_state.event = PM_EVENT_ON;
  774. return status;
  775. }
  776. /**
  777. * usb_suspend_both - suspend a USB device and its interfaces
  778. * @udev: the usb_device to suspend
  779. * @msg: Power Management message describing this state transition
  780. *
  781. * This is the central routine for suspending USB devices. It calls the
  782. * suspend methods for all the interface drivers in @udev and then calls
  783. * the suspend method for @udev itself. If an error occurs at any stage,
  784. * all the interfaces which were suspended are resumed so that they remain
  785. * in the same state as the device.
  786. *
  787. * If an autosuspend is in progress (@udev->auto_pm is set), the routine
  788. * checks first to make sure that neither the device itself or any of its
  789. * active interfaces is in use (pm_usage_cnt is greater than 0). If they
  790. * are, the autosuspend fails.
  791. *
  792. * If the suspend succeeds, the routine recursively queues an autosuspend
  793. * request for @udev's parent device, thereby propagating the change up
  794. * the device tree. If all of the parent's children are now suspended,
  795. * the parent will autosuspend in turn.
  796. *
  797. * The suspend method calls are subject to mutual exclusion under control
  798. * of @udev's pm_mutex. Many of these calls are also under the protection
  799. * of @udev's device lock (including all requests originating outside the
  800. * USB subsystem), but autosuspend requests generated by a child device or
  801. * interface driver may not be. Usbcore will insure that the method calls
  802. * do not arrive during bind, unbind, or reset operations. However, drivers
  803. * must be prepared to handle suspend calls arriving at unpredictable times.
  804. * The only way to block such calls is to do an autoresume (preventing
  805. * autosuspends) while holding @udev's device lock (preventing outside
  806. * suspends).
  807. *
  808. * The caller must hold @udev->pm_mutex.
  809. *
  810. * This routine can run only in process context.
  811. */
  812. int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
  813. {
  814. int status = 0;
  815. int i = 0;
  816. struct usb_interface *intf;
  817. struct usb_device *parent = udev->parent;
  818. cancel_delayed_work(&udev->autosuspend);
  819. if (udev->state == USB_STATE_NOTATTACHED)
  820. return 0;
  821. if (udev->state == USB_STATE_SUSPENDED)
  822. return 0;
  823. udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
  824. /* For autosuspend, fail fast if anything is in use.
  825. * Also fail if any interfaces require remote wakeup but it
  826. * isn't available. */
  827. if (udev->auto_pm) {
  828. if (udev->pm_usage_cnt > 0)
  829. return -EBUSY;
  830. if (udev->actconfig) {
  831. for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
  832. intf = udev->actconfig->interface[i];
  833. if (!is_active(intf))
  834. continue;
  835. if (intf->pm_usage_cnt > 0)
  836. return -EBUSY;
  837. if (intf->needs_remote_wakeup &&
  838. !udev->do_remote_wakeup) {
  839. dev_dbg(&udev->dev,
  840. "remote wakeup needed for autosuspend\n");
  841. return -EOPNOTSUPP;
  842. }
  843. }
  844. i = 0;
  845. }
  846. }
  847. /* Suspend all the interfaces and then udev itself */
  848. if (udev->actconfig) {
  849. for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
  850. intf = udev->actconfig->interface[i];
  851. status = suspend_interface(intf, msg);
  852. if (status != 0)
  853. break;
  854. }
  855. }
  856. if (status == 0)
  857. status = suspend_device(udev, msg);
  858. /* If the suspend failed, resume interfaces that did get suspended */
  859. if (status != 0) {
  860. while (--i >= 0) {
  861. intf = udev->actconfig->interface[i];
  862. resume_interface(intf);
  863. }
  864. /* If the suspend succeeded, propagate it up the tree */
  865. } else if (parent)
  866. usb_autosuspend_device(parent, 0);
  867. // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  868. return status;
  869. }
  870. /**
  871. * usb_resume_both - resume a USB device and its interfaces
  872. * @udev: the usb_device to resume
  873. *
  874. * This is the central routine for resuming USB devices. It calls the
  875. * the resume method for @udev and then calls the resume methods for all
  876. * the interface drivers in @udev.
  877. *
  878. * Before starting the resume, the routine calls itself recursively for
  879. * the parent device of @udev, thereby propagating the change up the device
  880. * tree and assuring that @udev will be able to resume. If the parent is
  881. * unable to resume successfully, the routine fails.
  882. *
  883. * The resume method calls are subject to mutual exclusion under control
  884. * of @udev's pm_mutex. Many of these calls are also under the protection
  885. * of @udev's device lock (including all requests originating outside the
  886. * USB subsystem), but autoresume requests generated by a child device or
  887. * interface driver may not be. Usbcore will insure that the method calls
  888. * do not arrive during bind, unbind, or reset operations. However, drivers
  889. * must be prepared to handle resume calls arriving at unpredictable times.
  890. * The only way to block such calls is to do an autoresume (preventing
  891. * other autoresumes) while holding @udev's device lock (preventing outside
  892. * resumes).
  893. *
  894. * The caller must hold @udev->pm_mutex.
  895. *
  896. * This routine can run only in process context.
  897. */
  898. int usb_resume_both(struct usb_device *udev)
  899. {
  900. int status = 0;
  901. int i;
  902. struct usb_interface *intf;
  903. struct usb_device *parent = udev->parent;
  904. cancel_delayed_work(&udev->autosuspend);
  905. if (udev->state == USB_STATE_NOTATTACHED)
  906. return -ENODEV;
  907. /* Propagate the resume up the tree, if necessary */
  908. if (udev->state == USB_STATE_SUSPENDED) {
  909. if (parent) {
  910. usb_pm_lock(parent);
  911. parent->auto_pm = 1;
  912. status = usb_resume_both(parent);
  913. } else {
  914. /* We can't progagate beyond the USB subsystem,
  915. * so if a root hub's controller is suspended
  916. * then we're stuck. */
  917. if (udev->dev.parent->power.power_state.event !=
  918. PM_EVENT_ON)
  919. status = -EHOSTUNREACH;
  920. }
  921. if (status == 0)
  922. status = resume_device(udev);
  923. if (parent)
  924. usb_pm_unlock(parent);
  925. } else {
  926. /* Needed only for setting udev->dev.power.power_state.event
  927. * and for possible debugging message. */
  928. status = resume_device(udev);
  929. }
  930. /* Now the parent won't suspend until we are finished */
  931. if (status == 0 && udev->actconfig) {
  932. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  933. intf = udev->actconfig->interface[i];
  934. resume_interface(intf);
  935. }
  936. }
  937. // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  938. return status;
  939. }
  940. #ifdef CONFIG_USB_SUSPEND
  941. /**
  942. * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
  943. * @udev: the usb_device to autosuspend
  944. * @dec_usage_cnt: flag to decrement @udev's PM-usage counter
  945. *
  946. * This routine should be called when a core subsystem is finished using
  947. * @udev and wants to allow it to autosuspend. Examples would be when
  948. * @udev's device file in usbfs is closed or after a configuration change.
  949. *
  950. * @dec_usage_cnt should be 1 if the subsystem previously incremented
  951. * @udev's usage counter (such as by passing 1 to usb_autoresume_device);
  952. * otherwise it should be 0.
  953. *
  954. * If the usage counter for @udev or any of its active interfaces is greater
  955. * than 0, the autosuspend request will not be queued. (If an interface
  956. * driver does not support autosuspend then its usage counter is permanently
  957. * positive.) Likewise, if an interface driver requires remote-wakeup
  958. * capability during autosuspend but remote wakeup is disabled, the
  959. * autosuspend will fail.
  960. *
  961. * Often the caller will hold @udev's device lock, but this is not
  962. * necessary.
  963. *
  964. * This routine can run only in process context.
  965. */
  966. void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt)
  967. {
  968. usb_pm_lock(udev);
  969. udev->pm_usage_cnt -= dec_usage_cnt;
  970. if (udev->pm_usage_cnt <= 0)
  971. queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
  972. USB_AUTOSUSPEND_DELAY);
  973. usb_pm_unlock(udev);
  974. // dev_dbg(&udev->dev, "%s: cnt %d\n",
  975. // __FUNCTION__, udev->pm_usage_cnt);
  976. }
  977. /**
  978. * usb_autoresume_device - immediately autoresume a USB device and its interfaces
  979. * @udev: the usb_device to autoresume
  980. * @inc_usage_cnt: flag to increment @udev's PM-usage counter
  981. *
  982. * This routine should be called when a core subsystem wants to use @udev
  983. * and needs to guarantee that it is not suspended. In addition, the
  984. * caller can prevent @udev from being autosuspended subsequently. (Note
  985. * that this will not prevent suspend events originating in the PM core.)
  986. * Examples would be when @udev's device file in usbfs is opened (autosuspend
  987. * should be prevented until the file is closed) or when a remote-wakeup
  988. * request is received (later autosuspends should not be prevented).
  989. *
  990. * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent
  991. * autosuspends. This prevention will persist until the usage counter is
  992. * decremented again (such as by passing 1 to usb_autosuspend_device).
  993. * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged.
  994. * Regardless, if the autoresume fails then the usage counter is not
  995. * incremented.
  996. *
  997. * Often the caller will hold @udev's device lock, but this is not
  998. * necessary (and attempting it might cause deadlock).
  999. *
  1000. * This routine can run only in process context.
  1001. */
  1002. int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt)
  1003. {
  1004. int status;
  1005. usb_pm_lock(udev);
  1006. udev->pm_usage_cnt += inc_usage_cnt;
  1007. udev->auto_pm = 1;
  1008. status = usb_resume_both(udev);
  1009. if (status != 0)
  1010. udev->pm_usage_cnt -= inc_usage_cnt;
  1011. usb_pm_unlock(udev);
  1012. // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
  1013. // __FUNCTION__, status, udev->pm_usage_cnt);
  1014. return status;
  1015. }
  1016. /**
  1017. * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
  1018. * @intf: the usb_interface whose counter should be decremented
  1019. *
  1020. * This routine should be called by an interface driver when it is
  1021. * finished using @intf and wants to allow it to autosuspend. A typical
  1022. * example would be a character-device driver when its device file is
  1023. * closed.
  1024. *
  1025. * The routine decrements @intf's usage counter. When the counter reaches
  1026. * 0, a delayed autosuspend request for @intf's device is queued. When
  1027. * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
  1028. * the other usage counters for the sibling interfaces and @intf's
  1029. * usb_device, the device and all its interfaces will be autosuspended.
  1030. *
  1031. * Note that @intf->pm_usage_cnt is owned by the interface driver. The
  1032. * core will not change its value other than the increment and decrement
  1033. * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
  1034. * may use this simple counter-oriented discipline or may set the value
  1035. * any way it likes.
  1036. *
  1037. * If the driver has set @intf->needs_remote_wakeup then autosuspend will
  1038. * take place only if the device's remote-wakeup facility is enabled.
  1039. *
  1040. * Suspend method calls queued by this routine can arrive at any time
  1041. * while @intf is resumed and its usage counter is equal to 0. They are
  1042. * not protected by the usb_device's lock but only by its pm_mutex.
  1043. * Drivers must provide their own synchronization.
  1044. *
  1045. * This routine can run only in process context.
  1046. */
  1047. void usb_autopm_put_interface(struct usb_interface *intf)
  1048. {
  1049. struct usb_device *udev = interface_to_usbdev(intf);
  1050. usb_pm_lock(udev);
  1051. if (intf->condition != USB_INTERFACE_UNBOUND &&
  1052. --intf->pm_usage_cnt <= 0) {
  1053. queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
  1054. USB_AUTOSUSPEND_DELAY);
  1055. }
  1056. usb_pm_unlock(udev);
  1057. // dev_dbg(&intf->dev, "%s: cnt %d\n",
  1058. // __FUNCTION__, intf->pm_usage_cnt);
  1059. }
  1060. EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
  1061. /**
  1062. * usb_autopm_get_interface - increment a USB interface's PM-usage counter
  1063. * @intf: the usb_interface whose counter should be incremented
  1064. *
  1065. * This routine should be called by an interface driver when it wants to
  1066. * use @intf and needs to guarantee that it is not suspended. In addition,
  1067. * the routine prevents @intf from being autosuspended subsequently. (Note
  1068. * that this will not prevent suspend events originating in the PM core.)
  1069. * This prevention will persist until usb_autopm_put_interface() is called
  1070. * or @intf is unbound. A typical example would be a character-device
  1071. * driver when its device file is opened.
  1072. *
  1073. * The routine increments @intf's usage counter. So long as the counter
  1074. * is greater than 0, autosuspend will not be allowed for @intf or its
  1075. * usb_device. When the driver is finished using @intf it should call
  1076. * usb_autopm_put_interface() to decrement the usage counter and queue
  1077. * a delayed autosuspend request (if the counter is <= 0).
  1078. *
  1079. * Note that @intf->pm_usage_cnt is owned by the interface driver. The
  1080. * core will not change its value other than the increment and decrement
  1081. * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
  1082. * may use this simple counter-oriented discipline or may set the value
  1083. * any way it likes.
  1084. *
  1085. * Resume method calls generated by this routine can arrive at any time
  1086. * while @intf is suspended. They are not protected by the usb_device's
  1087. * lock but only by its pm_mutex. Drivers must provide their own
  1088. * synchronization.
  1089. *
  1090. * This routine can run only in process context.
  1091. */
  1092. int usb_autopm_get_interface(struct usb_interface *intf)
  1093. {
  1094. struct usb_device *udev = interface_to_usbdev(intf);
  1095. int status;
  1096. usb_pm_lock(udev);
  1097. if (intf->condition == USB_INTERFACE_UNBOUND)
  1098. status = -ENODEV;
  1099. else {
  1100. ++intf->pm_usage_cnt;
  1101. udev->auto_pm = 1;
  1102. status = usb_resume_both(udev);
  1103. if (status != 0)
  1104. --intf->pm_usage_cnt;
  1105. }
  1106. usb_pm_unlock(udev);
  1107. // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
  1108. // __FUNCTION__, status, intf->pm_usage_cnt);
  1109. return status;
  1110. }
  1111. EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
  1112. #endif /* CONFIG_USB_SUSPEND */
  1113. static int usb_suspend(struct device *dev, pm_message_t message)
  1114. {
  1115. int status;
  1116. if (is_usb_device(dev)) {
  1117. struct usb_device *udev = to_usb_device(dev);
  1118. usb_pm_lock(udev);
  1119. udev->auto_pm = 0;
  1120. status = usb_suspend_both(udev, message);
  1121. usb_pm_unlock(udev);
  1122. } else
  1123. status = 0;
  1124. return status;
  1125. }
  1126. static int usb_resume(struct device *dev)
  1127. {
  1128. int status;
  1129. if (is_usb_device(dev)) {
  1130. struct usb_device *udev = to_usb_device(dev);
  1131. usb_pm_lock(udev);
  1132. udev->auto_pm = 0;
  1133. status = usb_resume_both(udev);
  1134. usb_pm_unlock(udev);
  1135. /* Rebind drivers that had no suspend method? */
  1136. } else
  1137. status = 0;
  1138. return status;
  1139. }
  1140. #endif /* CONFIG_PM */
  1141. struct bus_type usb_bus_type = {
  1142. .name = "usb",
  1143. .match = usb_device_match,
  1144. .uevent = usb_uevent,
  1145. #ifdef CONFIG_PM
  1146. .suspend = usb_suspend,
  1147. .resume = usb_resume,
  1148. #endif
  1149. };