driver.c 43 KB

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