driver.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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/slab.h>
  26. #include <linux/export.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/quirks.h>
  29. #include <linux/usb/hcd.h>
  30. #include "usb.h"
  31. /*
  32. * Adds a new dynamic USBdevice ID to this driver,
  33. * and cause the driver to probe for all devices again.
  34. */
  35. ssize_t usb_store_new_id(struct usb_dynids *dynids,
  36. struct device_driver *driver,
  37. const char *buf, size_t count)
  38. {
  39. struct usb_dynid *dynid;
  40. u32 idVendor = 0;
  41. u32 idProduct = 0;
  42. unsigned int bInterfaceClass = 0;
  43. int fields = 0;
  44. int retval = 0;
  45. fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct,
  46. &bInterfaceClass);
  47. if (fields < 2)
  48. return -EINVAL;
  49. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  50. if (!dynid)
  51. return -ENOMEM;
  52. INIT_LIST_HEAD(&dynid->node);
  53. dynid->id.idVendor = idVendor;
  54. dynid->id.idProduct = idProduct;
  55. dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  56. if (fields == 3) {
  57. dynid->id.bInterfaceClass = (u8)bInterfaceClass;
  58. dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
  59. }
  60. spin_lock(&dynids->lock);
  61. list_add_tail(&dynid->node, &dynids->list);
  62. spin_unlock(&dynids->lock);
  63. retval = driver_attach(driver);
  64. if (retval)
  65. return retval;
  66. return count;
  67. }
  68. EXPORT_SYMBOL_GPL(usb_store_new_id);
  69. ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
  70. {
  71. struct usb_dynid *dynid;
  72. size_t count = 0;
  73. list_for_each_entry(dynid, &dynids->list, node)
  74. if (dynid->id.bInterfaceClass != 0)
  75. count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
  76. dynid->id.idVendor, dynid->id.idProduct,
  77. dynid->id.bInterfaceClass);
  78. else
  79. count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
  80. dynid->id.idVendor, dynid->id.idProduct);
  81. return count;
  82. }
  83. EXPORT_SYMBOL_GPL(usb_show_dynids);
  84. static ssize_t show_dynids(struct device_driver *driver, char *buf)
  85. {
  86. struct usb_driver *usb_drv = to_usb_driver(driver);
  87. return usb_show_dynids(&usb_drv->dynids, buf);
  88. }
  89. static ssize_t store_new_id(struct device_driver *driver,
  90. const char *buf, size_t count)
  91. {
  92. struct usb_driver *usb_drv = to_usb_driver(driver);
  93. return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
  94. }
  95. static DRIVER_ATTR(new_id, S_IRUGO | S_IWUSR, show_dynids, store_new_id);
  96. /**
  97. * store_remove_id - remove a USB device ID from this driver
  98. * @driver: target device driver
  99. * @buf: buffer for scanning device ID data
  100. * @count: input size
  101. *
  102. * Removes a dynamic usb device ID from this driver.
  103. */
  104. static ssize_t
  105. store_remove_id(struct device_driver *driver, const char *buf, size_t count)
  106. {
  107. struct usb_dynid *dynid, *n;
  108. struct usb_driver *usb_driver = to_usb_driver(driver);
  109. u32 idVendor;
  110. u32 idProduct;
  111. int fields;
  112. fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
  113. if (fields < 2)
  114. return -EINVAL;
  115. spin_lock(&usb_driver->dynids.lock);
  116. list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
  117. struct usb_device_id *id = &dynid->id;
  118. if ((id->idVendor == idVendor) &&
  119. (id->idProduct == idProduct)) {
  120. list_del(&dynid->node);
  121. kfree(dynid);
  122. break;
  123. }
  124. }
  125. spin_unlock(&usb_driver->dynids.lock);
  126. return count;
  127. }
  128. static DRIVER_ATTR(remove_id, S_IRUGO | S_IWUSR, show_dynids, store_remove_id);
  129. static int usb_create_newid_files(struct usb_driver *usb_drv)
  130. {
  131. int error = 0;
  132. if (usb_drv->no_dynamic_id)
  133. goto exit;
  134. if (usb_drv->probe != NULL) {
  135. error = driver_create_file(&usb_drv->drvwrap.driver,
  136. &driver_attr_new_id);
  137. if (error == 0) {
  138. error = driver_create_file(&usb_drv->drvwrap.driver,
  139. &driver_attr_remove_id);
  140. if (error)
  141. driver_remove_file(&usb_drv->drvwrap.driver,
  142. &driver_attr_new_id);
  143. }
  144. }
  145. exit:
  146. return error;
  147. }
  148. static void usb_remove_newid_files(struct usb_driver *usb_drv)
  149. {
  150. if (usb_drv->no_dynamic_id)
  151. return;
  152. if (usb_drv->probe != NULL) {
  153. driver_remove_file(&usb_drv->drvwrap.driver,
  154. &driver_attr_remove_id);
  155. driver_remove_file(&usb_drv->drvwrap.driver,
  156. &driver_attr_new_id);
  157. }
  158. }
  159. static void usb_free_dynids(struct usb_driver *usb_drv)
  160. {
  161. struct usb_dynid *dynid, *n;
  162. spin_lock(&usb_drv->dynids.lock);
  163. list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
  164. list_del(&dynid->node);
  165. kfree(dynid);
  166. }
  167. spin_unlock(&usb_drv->dynids.lock);
  168. }
  169. static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
  170. struct usb_driver *drv)
  171. {
  172. struct usb_dynid *dynid;
  173. spin_lock(&drv->dynids.lock);
  174. list_for_each_entry(dynid, &drv->dynids.list, node) {
  175. if (usb_match_one_id(intf, &dynid->id)) {
  176. spin_unlock(&drv->dynids.lock);
  177. return &dynid->id;
  178. }
  179. }
  180. spin_unlock(&drv->dynids.lock);
  181. return NULL;
  182. }
  183. /* called from driver core with dev locked */
  184. static int usb_probe_device(struct device *dev)
  185. {
  186. struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  187. struct usb_device *udev = to_usb_device(dev);
  188. int error = 0;
  189. dev_dbg(dev, "%s\n", __func__);
  190. /* TODO: Add real matching code */
  191. /* The device should always appear to be in use
  192. * unless the driver supports autosuspend.
  193. */
  194. if (!udriver->supports_autosuspend)
  195. error = usb_autoresume_device(udev);
  196. if (!error)
  197. error = udriver->probe(udev);
  198. return error;
  199. }
  200. /* called from driver core with dev locked */
  201. static int usb_unbind_device(struct device *dev)
  202. {
  203. struct usb_device *udev = to_usb_device(dev);
  204. struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  205. udriver->disconnect(udev);
  206. if (!udriver->supports_autosuspend)
  207. usb_autosuspend_device(udev);
  208. return 0;
  209. }
  210. /*
  211. * Cancel any pending scheduled resets
  212. *
  213. * [see usb_queue_reset_device()]
  214. *
  215. * Called after unconfiguring / when releasing interfaces. See
  216. * comments in __usb_queue_reset_device() regarding
  217. * udev->reset_running.
  218. */
  219. static void usb_cancel_queued_reset(struct usb_interface *iface)
  220. {
  221. if (iface->reset_running == 0)
  222. cancel_work_sync(&iface->reset_ws);
  223. }
  224. /* called from driver core with dev locked */
  225. static int usb_probe_interface(struct device *dev)
  226. {
  227. struct usb_driver *driver = to_usb_driver(dev->driver);
  228. struct usb_interface *intf = to_usb_interface(dev);
  229. struct usb_device *udev = interface_to_usbdev(intf);
  230. const struct usb_device_id *id;
  231. int error = -ENODEV;
  232. int lpm_disable_error;
  233. dev_dbg(dev, "%s\n", __func__);
  234. intf->needs_binding = 0;
  235. if (usb_device_is_owned(udev))
  236. return error;
  237. if (udev->authorized == 0) {
  238. dev_err(&intf->dev, "Device is not authorized for usage\n");
  239. return error;
  240. }
  241. id = usb_match_id(intf, driver->id_table);
  242. if (!id)
  243. id = usb_match_dynamic_id(intf, driver);
  244. if (!id)
  245. return error;
  246. dev_dbg(dev, "%s - got id\n", __func__);
  247. error = usb_autoresume_device(udev);
  248. if (error)
  249. return error;
  250. intf->condition = USB_INTERFACE_BINDING;
  251. /* Probed interfaces are initially active. They are
  252. * runtime-PM-enabled only if the driver has autosuspend support.
  253. * They are sensitive to their children's power states.
  254. */
  255. pm_runtime_set_active(dev);
  256. pm_suspend_ignore_children(dev, false);
  257. if (driver->supports_autosuspend)
  258. pm_runtime_enable(dev);
  259. /* If the new driver doesn't allow hub-initiated LPM, and we can't
  260. * disable hub-initiated LPM, then fail the probe.
  261. *
  262. * Otherwise, leaving LPM enabled should be harmless, because the
  263. * endpoint intervals should remain the same, and the U1/U2 timeouts
  264. * should remain the same.
  265. *
  266. * If we need to install alt setting 0 before probe, or another alt
  267. * setting during probe, that should also be fine. usb_set_interface()
  268. * will attempt to disable LPM, and fail if it can't disable it.
  269. */
  270. lpm_disable_error = usb_unlocked_disable_lpm(udev);
  271. if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
  272. dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
  273. __func__, driver->name);
  274. error = lpm_disable_error;
  275. goto err;
  276. }
  277. /* Carry out a deferred switch to altsetting 0 */
  278. if (intf->needs_altsetting0) {
  279. error = usb_set_interface(udev, intf->altsetting[0].
  280. desc.bInterfaceNumber, 0);
  281. if (error < 0)
  282. goto err;
  283. intf->needs_altsetting0 = 0;
  284. }
  285. error = driver->probe(intf, id);
  286. if (error)
  287. goto err;
  288. intf->condition = USB_INTERFACE_BOUND;
  289. /* If the LPM disable succeeded, balance the ref counts. */
  290. if (!lpm_disable_error)
  291. usb_unlocked_enable_lpm(udev);
  292. usb_autosuspend_device(udev);
  293. return error;
  294. err:
  295. usb_set_intfdata(intf, NULL);
  296. intf->needs_remote_wakeup = 0;
  297. intf->condition = USB_INTERFACE_UNBOUND;
  298. usb_cancel_queued_reset(intf);
  299. /* If the LPM disable succeeded, balance the ref counts. */
  300. if (!lpm_disable_error)
  301. usb_unlocked_enable_lpm(udev);
  302. /* Unbound interfaces are always runtime-PM-disabled and -suspended */
  303. if (driver->supports_autosuspend)
  304. pm_runtime_disable(dev);
  305. pm_runtime_set_suspended(dev);
  306. usb_autosuspend_device(udev);
  307. return error;
  308. }
  309. /* called from driver core with dev locked */
  310. static int usb_unbind_interface(struct device *dev)
  311. {
  312. struct usb_driver *driver = to_usb_driver(dev->driver);
  313. struct usb_interface *intf = to_usb_interface(dev);
  314. struct usb_device *udev;
  315. int error, r, lpm_disable_error;
  316. intf->condition = USB_INTERFACE_UNBINDING;
  317. /* Autoresume for set_interface call below */
  318. udev = interface_to_usbdev(intf);
  319. error = usb_autoresume_device(udev);
  320. /* Hub-initiated LPM policy may change, so attempt to disable LPM until
  321. * the driver is unbound. If LPM isn't disabled, that's fine because it
  322. * wouldn't be enabled unless all the bound interfaces supported
  323. * hub-initiated LPM.
  324. */
  325. lpm_disable_error = usb_unlocked_disable_lpm(udev);
  326. /* Terminate all URBs for this interface unless the driver
  327. * supports "soft" unbinding.
  328. */
  329. if (!driver->soft_unbind)
  330. usb_disable_interface(udev, intf, false);
  331. driver->disconnect(intf);
  332. usb_cancel_queued_reset(intf);
  333. /* Reset other interface state.
  334. * We cannot do a Set-Interface if the device is suspended or
  335. * if it is prepared for a system sleep (since installing a new
  336. * altsetting means creating new endpoint device entries).
  337. * When either of these happens, defer the Set-Interface.
  338. */
  339. if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
  340. /* Already in altsetting 0 so skip Set-Interface.
  341. * Just re-enable it without affecting the endpoint toggles.
  342. */
  343. usb_enable_interface(udev, intf, false);
  344. } else if (!error && !intf->dev.power.is_prepared) {
  345. r = usb_set_interface(udev, intf->altsetting[0].
  346. desc.bInterfaceNumber, 0);
  347. if (r < 0)
  348. intf->needs_altsetting0 = 1;
  349. } else {
  350. intf->needs_altsetting0 = 1;
  351. }
  352. usb_set_intfdata(intf, NULL);
  353. intf->condition = USB_INTERFACE_UNBOUND;
  354. intf->needs_remote_wakeup = 0;
  355. /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
  356. if (!lpm_disable_error)
  357. usb_unlocked_enable_lpm(udev);
  358. /* Unbound interfaces are always runtime-PM-disabled and -suspended */
  359. if (driver->supports_autosuspend)
  360. pm_runtime_disable(dev);
  361. pm_runtime_set_suspended(dev);
  362. /* Undo any residual pm_autopm_get_interface_* calls */
  363. for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
  364. usb_autopm_put_interface_no_suspend(intf);
  365. atomic_set(&intf->pm_usage_cnt, 0);
  366. if (!error)
  367. usb_autosuspend_device(udev);
  368. return 0;
  369. }
  370. /**
  371. * usb_driver_claim_interface - bind a driver to an interface
  372. * @driver: the driver to be bound
  373. * @iface: the interface to which it will be bound; must be in the
  374. * usb device's active configuration
  375. * @priv: driver data associated with that interface
  376. *
  377. * This is used by usb device drivers that need to claim more than one
  378. * interface on a device when probing (audio and acm are current examples).
  379. * No device driver should directly modify internal usb_interface or
  380. * usb_device structure members.
  381. *
  382. * Few drivers should need to use this routine, since the most natural
  383. * way to bind to an interface is to return the private data from
  384. * the driver's probe() method.
  385. *
  386. * Callers must own the device lock, so driver probe() entries don't need
  387. * extra locking, but other call contexts may need to explicitly claim that
  388. * lock.
  389. */
  390. int usb_driver_claim_interface(struct usb_driver *driver,
  391. struct usb_interface *iface, void *priv)
  392. {
  393. struct device *dev = &iface->dev;
  394. struct usb_device *udev;
  395. int retval = 0;
  396. int lpm_disable_error;
  397. if (dev->driver)
  398. return -EBUSY;
  399. udev = interface_to_usbdev(iface);
  400. dev->driver = &driver->drvwrap.driver;
  401. usb_set_intfdata(iface, priv);
  402. iface->needs_binding = 0;
  403. iface->condition = USB_INTERFACE_BOUND;
  404. /* Disable LPM until this driver is bound. */
  405. lpm_disable_error = usb_unlocked_disable_lpm(udev);
  406. if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
  407. dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
  408. __func__, driver->name);
  409. return -ENOMEM;
  410. }
  411. /* Claimed interfaces are initially inactive (suspended) and
  412. * runtime-PM-enabled, but only if the driver has autosuspend
  413. * support. Otherwise they are marked active, to prevent the
  414. * device from being autosuspended, but left disabled. In either
  415. * case they are sensitive to their children's power states.
  416. */
  417. pm_suspend_ignore_children(dev, false);
  418. if (driver->supports_autosuspend)
  419. pm_runtime_enable(dev);
  420. else
  421. pm_runtime_set_active(dev);
  422. /* if interface was already added, bind now; else let
  423. * the future device_add() bind it, bypassing probe()
  424. */
  425. if (device_is_registered(dev))
  426. retval = device_bind_driver(dev);
  427. /* Attempt to re-enable USB3 LPM, if the disable was successful. */
  428. if (!lpm_disable_error)
  429. usb_unlocked_enable_lpm(udev);
  430. return retval;
  431. }
  432. EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
  433. /**
  434. * usb_driver_release_interface - unbind a driver from an interface
  435. * @driver: the driver to be unbound
  436. * @iface: the interface from which it will be unbound
  437. *
  438. * This can be used by drivers to release an interface without waiting
  439. * for their disconnect() methods to be called. In typical cases this
  440. * also causes the driver disconnect() method to be called.
  441. *
  442. * This call is synchronous, and may not be used in an interrupt context.
  443. * Callers must own the device lock, so driver disconnect() entries don't
  444. * need extra locking, but other call contexts may need to explicitly claim
  445. * that lock.
  446. */
  447. void usb_driver_release_interface(struct usb_driver *driver,
  448. struct usb_interface *iface)
  449. {
  450. struct device *dev = &iface->dev;
  451. /* this should never happen, don't release something that's not ours */
  452. if (!dev->driver || dev->driver != &driver->drvwrap.driver)
  453. return;
  454. /* don't release from within disconnect() */
  455. if (iface->condition != USB_INTERFACE_BOUND)
  456. return;
  457. iface->condition = USB_INTERFACE_UNBINDING;
  458. /* Release via the driver core only if the interface
  459. * has already been registered
  460. */
  461. if (device_is_registered(dev)) {
  462. device_release_driver(dev);
  463. } else {
  464. device_lock(dev);
  465. usb_unbind_interface(dev);
  466. dev->driver = NULL;
  467. device_unlock(dev);
  468. }
  469. }
  470. EXPORT_SYMBOL_GPL(usb_driver_release_interface);
  471. /* returns 0 if no match, 1 if match */
  472. int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
  473. {
  474. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  475. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  476. return 0;
  477. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  478. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  479. return 0;
  480. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  481. greater than any unsigned number. */
  482. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  483. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  484. return 0;
  485. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  486. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  487. return 0;
  488. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  489. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  490. return 0;
  491. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  492. (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  493. return 0;
  494. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  495. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  496. return 0;
  497. return 1;
  498. }
  499. /* returns 0 if no match, 1 if match */
  500. int usb_match_one_id_intf(struct usb_device *dev,
  501. struct usb_host_interface *intf,
  502. const struct usb_device_id *id)
  503. {
  504. /* The interface class, subclass, protocol and number should never be
  505. * checked for a match if the device class is Vendor Specific,
  506. * unless the match record specifies the Vendor ID. */
  507. if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
  508. !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  509. (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
  510. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  511. USB_DEVICE_ID_MATCH_INT_PROTOCOL |
  512. USB_DEVICE_ID_MATCH_INT_NUMBER)))
  513. return 0;
  514. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  515. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  516. return 0;
  517. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  518. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  519. return 0;
  520. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  521. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  522. return 0;
  523. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
  524. (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
  525. return 0;
  526. return 1;
  527. }
  528. /* returns 0 if no match, 1 if match */
  529. int usb_match_one_id(struct usb_interface *interface,
  530. const struct usb_device_id *id)
  531. {
  532. struct usb_host_interface *intf;
  533. struct usb_device *dev;
  534. /* proc_connectinfo in devio.c may call us with id == NULL. */
  535. if (id == NULL)
  536. return 0;
  537. intf = interface->cur_altsetting;
  538. dev = interface_to_usbdev(interface);
  539. if (!usb_match_device(dev, id))
  540. return 0;
  541. return usb_match_one_id_intf(dev, intf, id);
  542. }
  543. EXPORT_SYMBOL_GPL(usb_match_one_id);
  544. /**
  545. * usb_match_id - find first usb_device_id matching device or interface
  546. * @interface: the interface of interest
  547. * @id: array of usb_device_id structures, terminated by zero entry
  548. *
  549. * usb_match_id searches an array of usb_device_id's and returns
  550. * the first one matching the device or interface, or null.
  551. * This is used when binding (or rebinding) a driver to an interface.
  552. * Most USB device drivers will use this indirectly, through the usb core,
  553. * but some layered driver frameworks use it directly.
  554. * These device tables are exported with MODULE_DEVICE_TABLE, through
  555. * modutils, to support the driver loading functionality of USB hotplugging.
  556. *
  557. * What Matches:
  558. *
  559. * The "match_flags" element in a usb_device_id controls which
  560. * members are used. If the corresponding bit is set, the
  561. * value in the device_id must match its corresponding member
  562. * in the device or interface descriptor, or else the device_id
  563. * does not match.
  564. *
  565. * "driver_info" is normally used only by device drivers,
  566. * but you can create a wildcard "matches anything" usb_device_id
  567. * as a driver's "modules.usbmap" entry if you provide an id with
  568. * only a nonzero "driver_info" field. If you do this, the USB device
  569. * driver's probe() routine should use additional intelligence to
  570. * decide whether to bind to the specified interface.
  571. *
  572. * What Makes Good usb_device_id Tables:
  573. *
  574. * The match algorithm is very simple, so that intelligence in
  575. * driver selection must come from smart driver id records.
  576. * Unless you have good reasons to use another selection policy,
  577. * provide match elements only in related groups, and order match
  578. * specifiers from specific to general. Use the macros provided
  579. * for that purpose if you can.
  580. *
  581. * The most specific match specifiers use device descriptor
  582. * data. These are commonly used with product-specific matches;
  583. * the USB_DEVICE macro lets you provide vendor and product IDs,
  584. * and you can also match against ranges of product revisions.
  585. * These are widely used for devices with application or vendor
  586. * specific bDeviceClass values.
  587. *
  588. * Matches based on device class/subclass/protocol specifications
  589. * are slightly more general; use the USB_DEVICE_INFO macro, or
  590. * its siblings. These are used with single-function devices
  591. * where bDeviceClass doesn't specify that each interface has
  592. * its own class.
  593. *
  594. * Matches based on interface class/subclass/protocol are the
  595. * most general; they let drivers bind to any interface on a
  596. * multiple-function device. Use the USB_INTERFACE_INFO
  597. * macro, or its siblings, to match class-per-interface style
  598. * devices (as recorded in bInterfaceClass).
  599. *
  600. * Note that an entry created by USB_INTERFACE_INFO won't match
  601. * any interface if the device class is set to Vendor-Specific.
  602. * This is deliberate; according to the USB spec the meanings of
  603. * the interface class/subclass/protocol for these devices are also
  604. * vendor-specific, and hence matching against a standard product
  605. * class wouldn't work anyway. If you really want to use an
  606. * interface-based match for such a device, create a match record
  607. * that also specifies the vendor ID. (Unforunately there isn't a
  608. * standard macro for creating records like this.)
  609. *
  610. * Within those groups, remember that not all combinations are
  611. * meaningful. For example, don't give a product version range
  612. * without vendor and product IDs; or specify a protocol without
  613. * its associated class and subclass.
  614. */
  615. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  616. const struct usb_device_id *id)
  617. {
  618. /* proc_connectinfo in devio.c may call us with id == NULL. */
  619. if (id == NULL)
  620. return NULL;
  621. /* It is important to check that id->driver_info is nonzero,
  622. since an entry that is all zeroes except for a nonzero
  623. id->driver_info is the way to create an entry that
  624. indicates that the driver want to examine every
  625. device and interface. */
  626. for (; id->idVendor || id->idProduct || id->bDeviceClass ||
  627. id->bInterfaceClass || id->driver_info; id++) {
  628. if (usb_match_one_id(interface, id))
  629. return id;
  630. }
  631. return NULL;
  632. }
  633. EXPORT_SYMBOL_GPL(usb_match_id);
  634. static int usb_device_match(struct device *dev, struct device_driver *drv)
  635. {
  636. /* devices and interfaces are handled separately */
  637. if (is_usb_device(dev)) {
  638. /* interface drivers never match devices */
  639. if (!is_usb_device_driver(drv))
  640. return 0;
  641. /* TODO: Add real matching code */
  642. return 1;
  643. } else if (is_usb_interface(dev)) {
  644. struct usb_interface *intf;
  645. struct usb_driver *usb_drv;
  646. const struct usb_device_id *id;
  647. /* device drivers never match interfaces */
  648. if (is_usb_device_driver(drv))
  649. return 0;
  650. intf = to_usb_interface(dev);
  651. usb_drv = to_usb_driver(drv);
  652. id = usb_match_id(intf, usb_drv->id_table);
  653. if (id)
  654. return 1;
  655. id = usb_match_dynamic_id(intf, usb_drv);
  656. if (id)
  657. return 1;
  658. }
  659. return 0;
  660. }
  661. static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
  662. {
  663. struct usb_device *usb_dev;
  664. if (is_usb_device(dev)) {
  665. usb_dev = to_usb_device(dev);
  666. } else if (is_usb_interface(dev)) {
  667. struct usb_interface *intf = to_usb_interface(dev);
  668. usb_dev = interface_to_usbdev(intf);
  669. } else {
  670. return 0;
  671. }
  672. if (usb_dev->devnum < 0) {
  673. /* driver is often null here; dev_dbg() would oops */
  674. pr_debug("usb %s: already deleted?\n", dev_name(dev));
  675. return -ENODEV;
  676. }
  677. if (!usb_dev->bus) {
  678. pr_debug("usb %s: bus removed?\n", dev_name(dev));
  679. return -ENODEV;
  680. }
  681. /* per-device configurations are common */
  682. if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
  683. le16_to_cpu(usb_dev->descriptor.idVendor),
  684. le16_to_cpu(usb_dev->descriptor.idProduct),
  685. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  686. return -ENOMEM;
  687. /* class-based driver binding models */
  688. if (add_uevent_var(env, "TYPE=%d/%d/%d",
  689. usb_dev->descriptor.bDeviceClass,
  690. usb_dev->descriptor.bDeviceSubClass,
  691. usb_dev->descriptor.bDeviceProtocol))
  692. return -ENOMEM;
  693. return 0;
  694. }
  695. /**
  696. * usb_register_device_driver - register a USB device (not interface) driver
  697. * @new_udriver: USB operations for the device driver
  698. * @owner: module owner of this driver.
  699. *
  700. * Registers a USB device driver with the USB core. The list of
  701. * unattached devices will be rescanned whenever a new driver is
  702. * added, allowing the new driver to attach to any recognized devices.
  703. * Returns a negative error code on failure and 0 on success.
  704. */
  705. int usb_register_device_driver(struct usb_device_driver *new_udriver,
  706. struct module *owner)
  707. {
  708. int retval = 0;
  709. if (usb_disabled())
  710. return -ENODEV;
  711. new_udriver->drvwrap.for_devices = 1;
  712. new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
  713. new_udriver->drvwrap.driver.bus = &usb_bus_type;
  714. new_udriver->drvwrap.driver.probe = usb_probe_device;
  715. new_udriver->drvwrap.driver.remove = usb_unbind_device;
  716. new_udriver->drvwrap.driver.owner = owner;
  717. retval = driver_register(&new_udriver->drvwrap.driver);
  718. if (!retval)
  719. pr_info("%s: registered new device driver %s\n",
  720. usbcore_name, new_udriver->name);
  721. else
  722. printk(KERN_ERR "%s: error %d registering device "
  723. " driver %s\n",
  724. usbcore_name, retval, new_udriver->name);
  725. return retval;
  726. }
  727. EXPORT_SYMBOL_GPL(usb_register_device_driver);
  728. /**
  729. * usb_deregister_device_driver - unregister a USB device (not interface) driver
  730. * @udriver: USB operations of the device driver to unregister
  731. * Context: must be able to sleep
  732. *
  733. * Unlinks the specified driver from the internal USB driver list.
  734. */
  735. void usb_deregister_device_driver(struct usb_device_driver *udriver)
  736. {
  737. pr_info("%s: deregistering device driver %s\n",
  738. usbcore_name, udriver->name);
  739. driver_unregister(&udriver->drvwrap.driver);
  740. }
  741. EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
  742. /**
  743. * usb_register_driver - register a USB interface driver
  744. * @new_driver: USB operations for the interface driver
  745. * @owner: module owner of this driver.
  746. * @mod_name: module name string
  747. *
  748. * Registers a USB interface driver with the USB core. The list of
  749. * unattached interfaces will be rescanned whenever a new driver is
  750. * added, allowing the new driver to attach to any recognized interfaces.
  751. * Returns a negative error code on failure and 0 on success.
  752. *
  753. * NOTE: if you want your driver to use the USB major number, you must call
  754. * usb_register_dev() to enable that functionality. This function no longer
  755. * takes care of that.
  756. */
  757. int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
  758. const char *mod_name)
  759. {
  760. int retval = 0;
  761. if (usb_disabled())
  762. return -ENODEV;
  763. new_driver->drvwrap.for_devices = 0;
  764. new_driver->drvwrap.driver.name = (char *) new_driver->name;
  765. new_driver->drvwrap.driver.bus = &usb_bus_type;
  766. new_driver->drvwrap.driver.probe = usb_probe_interface;
  767. new_driver->drvwrap.driver.remove = usb_unbind_interface;
  768. new_driver->drvwrap.driver.owner = owner;
  769. new_driver->drvwrap.driver.mod_name = mod_name;
  770. spin_lock_init(&new_driver->dynids.lock);
  771. INIT_LIST_HEAD(&new_driver->dynids.list);
  772. retval = driver_register(&new_driver->drvwrap.driver);
  773. if (retval)
  774. goto out;
  775. retval = usb_create_newid_files(new_driver);
  776. if (retval)
  777. goto out_newid;
  778. pr_info("%s: registered new interface driver %s\n",
  779. usbcore_name, new_driver->name);
  780. out:
  781. return retval;
  782. out_newid:
  783. driver_unregister(&new_driver->drvwrap.driver);
  784. printk(KERN_ERR "%s: error %d registering interface "
  785. " driver %s\n",
  786. usbcore_name, retval, new_driver->name);
  787. goto out;
  788. }
  789. EXPORT_SYMBOL_GPL(usb_register_driver);
  790. /**
  791. * usb_deregister - unregister a USB interface driver
  792. * @driver: USB operations of the interface driver to unregister
  793. * Context: must be able to sleep
  794. *
  795. * Unlinks the specified driver from the internal USB driver list.
  796. *
  797. * NOTE: If you called usb_register_dev(), you still need to call
  798. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  799. * this * call will no longer do it for you.
  800. */
  801. void usb_deregister(struct usb_driver *driver)
  802. {
  803. pr_info("%s: deregistering interface driver %s\n",
  804. usbcore_name, driver->name);
  805. usb_remove_newid_files(driver);
  806. driver_unregister(&driver->drvwrap.driver);
  807. usb_free_dynids(driver);
  808. }
  809. EXPORT_SYMBOL_GPL(usb_deregister);
  810. /* Forced unbinding of a USB interface driver, either because
  811. * it doesn't support pre_reset/post_reset/reset_resume or
  812. * because it doesn't support suspend/resume.
  813. *
  814. * The caller must hold @intf's device's lock, but not its pm_mutex
  815. * and not @intf->dev.sem.
  816. */
  817. void usb_forced_unbind_intf(struct usb_interface *intf)
  818. {
  819. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  820. dev_dbg(&intf->dev, "forced unbind\n");
  821. usb_driver_release_interface(driver, intf);
  822. /* Mark the interface for later rebinding */
  823. intf->needs_binding = 1;
  824. }
  825. /* Delayed forced unbinding of a USB interface driver and scan
  826. * for rebinding.
  827. *
  828. * The caller must hold @intf's device's lock, but not its pm_mutex
  829. * and not @intf->dev.sem.
  830. *
  831. * Note: Rebinds will be skipped if a system sleep transition is in
  832. * progress and the PM "complete" callback hasn't occurred yet.
  833. */
  834. void usb_rebind_intf(struct usb_interface *intf)
  835. {
  836. int rc;
  837. /* Delayed unbind of an existing driver */
  838. if (intf->dev.driver)
  839. usb_forced_unbind_intf(intf);
  840. /* Try to rebind the interface */
  841. if (!intf->dev.power.is_prepared) {
  842. intf->needs_binding = 0;
  843. rc = device_attach(&intf->dev);
  844. if (rc < 0)
  845. dev_warn(&intf->dev, "rebind failed: %d\n", rc);
  846. }
  847. }
  848. #ifdef CONFIG_PM
  849. /* Unbind drivers for @udev's interfaces that don't support suspend/resume
  850. * There is no check for reset_resume here because it can be determined
  851. * only during resume whether reset_resume is needed.
  852. *
  853. * The caller must hold @udev's device lock.
  854. */
  855. static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
  856. {
  857. struct usb_host_config *config;
  858. int i;
  859. struct usb_interface *intf;
  860. struct usb_driver *drv;
  861. config = udev->actconfig;
  862. if (config) {
  863. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  864. intf = config->interface[i];
  865. if (intf->dev.driver) {
  866. drv = to_usb_driver(intf->dev.driver);
  867. if (!drv->suspend || !drv->resume)
  868. usb_forced_unbind_intf(intf);
  869. }
  870. }
  871. }
  872. }
  873. /* Unbind drivers for @udev's interfaces that failed to support reset-resume.
  874. * These interfaces have the needs_binding flag set by usb_resume_interface().
  875. *
  876. * The caller must hold @udev's device lock.
  877. */
  878. static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
  879. {
  880. struct usb_host_config *config;
  881. int i;
  882. struct usb_interface *intf;
  883. config = udev->actconfig;
  884. if (config) {
  885. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  886. intf = config->interface[i];
  887. if (intf->dev.driver && intf->needs_binding)
  888. usb_forced_unbind_intf(intf);
  889. }
  890. }
  891. }
  892. static void do_rebind_interfaces(struct usb_device *udev)
  893. {
  894. struct usb_host_config *config;
  895. int i;
  896. struct usb_interface *intf;
  897. config = udev->actconfig;
  898. if (config) {
  899. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  900. intf = config->interface[i];
  901. if (intf->needs_binding)
  902. usb_rebind_intf(intf);
  903. }
  904. }
  905. }
  906. static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
  907. {
  908. struct usb_device_driver *udriver;
  909. int status = 0;
  910. if (udev->state == USB_STATE_NOTATTACHED ||
  911. udev->state == USB_STATE_SUSPENDED)
  912. goto done;
  913. /* For devices that don't have a driver, we do a generic suspend. */
  914. if (udev->dev.driver)
  915. udriver = to_usb_device_driver(udev->dev.driver);
  916. else {
  917. udev->do_remote_wakeup = 0;
  918. udriver = &usb_generic_driver;
  919. }
  920. status = udriver->suspend(udev, msg);
  921. done:
  922. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  923. return status;
  924. }
  925. static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
  926. {
  927. struct usb_device_driver *udriver;
  928. int status = 0;
  929. if (udev->state == USB_STATE_NOTATTACHED)
  930. goto done;
  931. /* Can't resume it if it doesn't have a driver. */
  932. if (udev->dev.driver == NULL) {
  933. status = -ENOTCONN;
  934. goto done;
  935. }
  936. /* Non-root devices on a full/low-speed bus must wait for their
  937. * companion high-speed root hub, in case a handoff is needed.
  938. */
  939. if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
  940. device_pm_wait_for_dev(&udev->dev,
  941. &udev->bus->hs_companion->root_hub->dev);
  942. if (udev->quirks & USB_QUIRK_RESET_RESUME)
  943. udev->reset_resume = 1;
  944. udriver = to_usb_device_driver(udev->dev.driver);
  945. status = udriver->resume(udev, msg);
  946. done:
  947. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  948. return status;
  949. }
  950. static int usb_suspend_interface(struct usb_device *udev,
  951. struct usb_interface *intf, pm_message_t msg)
  952. {
  953. struct usb_driver *driver;
  954. int status = 0;
  955. if (udev->state == USB_STATE_NOTATTACHED ||
  956. intf->condition == USB_INTERFACE_UNBOUND)
  957. goto done;
  958. driver = to_usb_driver(intf->dev.driver);
  959. /* at this time we know the driver supports suspend */
  960. status = driver->suspend(intf, msg);
  961. if (status && !PMSG_IS_AUTO(msg))
  962. dev_err(&intf->dev, "suspend error %d\n", status);
  963. done:
  964. dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
  965. return status;
  966. }
  967. static int usb_resume_interface(struct usb_device *udev,
  968. struct usb_interface *intf, pm_message_t msg, int reset_resume)
  969. {
  970. struct usb_driver *driver;
  971. int status = 0;
  972. if (udev->state == USB_STATE_NOTATTACHED)
  973. goto done;
  974. /* Don't let autoresume interfere with unbinding */
  975. if (intf->condition == USB_INTERFACE_UNBINDING)
  976. goto done;
  977. /* Can't resume it if it doesn't have a driver. */
  978. if (intf->condition == USB_INTERFACE_UNBOUND) {
  979. /* Carry out a deferred switch to altsetting 0 */
  980. if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
  981. usb_set_interface(udev, intf->altsetting[0].
  982. desc.bInterfaceNumber, 0);
  983. intf->needs_altsetting0 = 0;
  984. }
  985. goto done;
  986. }
  987. /* Don't resume if the interface is marked for rebinding */
  988. if (intf->needs_binding)
  989. goto done;
  990. driver = to_usb_driver(intf->dev.driver);
  991. if (reset_resume) {
  992. if (driver->reset_resume) {
  993. status = driver->reset_resume(intf);
  994. if (status)
  995. dev_err(&intf->dev, "%s error %d\n",
  996. "reset_resume", status);
  997. } else {
  998. intf->needs_binding = 1;
  999. dev_warn(&intf->dev, "no %s for driver %s?\n",
  1000. "reset_resume", driver->name);
  1001. }
  1002. } else {
  1003. status = driver->resume(intf);
  1004. if (status)
  1005. dev_err(&intf->dev, "resume error %d\n", status);
  1006. }
  1007. done:
  1008. dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
  1009. /* Later we will unbind the driver and/or reprobe, if necessary */
  1010. return status;
  1011. }
  1012. /**
  1013. * usb_suspend_both - suspend a USB device and its interfaces
  1014. * @udev: the usb_device to suspend
  1015. * @msg: Power Management message describing this state transition
  1016. *
  1017. * This is the central routine for suspending USB devices. It calls the
  1018. * suspend methods for all the interface drivers in @udev and then calls
  1019. * the suspend method for @udev itself. When the routine is called in
  1020. * autosuspend, if an error occurs at any stage, all the interfaces
  1021. * which were suspended are resumed so that they remain in the same
  1022. * state as the device, but when called from system sleep, all error
  1023. * from suspend methods of interfaces and the non-root-hub device itself
  1024. * are simply ignored, so all suspended interfaces are only resumed
  1025. * to the device's state when @udev is root-hub and its suspend method
  1026. * returns failure.
  1027. *
  1028. * Autosuspend requests originating from a child device or an interface
  1029. * driver may be made without the protection of @udev's device lock, but
  1030. * all other suspend calls will hold the lock. Usbcore will insure that
  1031. * method calls do not arrive during bind, unbind, or reset operations.
  1032. * However drivers must be prepared to handle suspend calls arriving at
  1033. * unpredictable times.
  1034. *
  1035. * This routine can run only in process context.
  1036. */
  1037. static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
  1038. {
  1039. int status = 0;
  1040. int i = 0, n = 0;
  1041. struct usb_interface *intf;
  1042. if (udev->state == USB_STATE_NOTATTACHED ||
  1043. udev->state == USB_STATE_SUSPENDED)
  1044. goto done;
  1045. /* Suspend all the interfaces and then udev itself */
  1046. if (udev->actconfig) {
  1047. n = udev->actconfig->desc.bNumInterfaces;
  1048. for (i = n - 1; i >= 0; --i) {
  1049. intf = udev->actconfig->interface[i];
  1050. status = usb_suspend_interface(udev, intf, msg);
  1051. /* Ignore errors during system sleep transitions */
  1052. if (!PMSG_IS_AUTO(msg))
  1053. status = 0;
  1054. if (status != 0)
  1055. break;
  1056. }
  1057. }
  1058. if (status == 0) {
  1059. status = usb_suspend_device(udev, msg);
  1060. /*
  1061. * Ignore errors from non-root-hub devices during
  1062. * system sleep transitions. For the most part,
  1063. * these devices should go to low power anyway when
  1064. * the entire bus is suspended.
  1065. */
  1066. if (udev->parent && !PMSG_IS_AUTO(msg))
  1067. status = 0;
  1068. }
  1069. /* If the suspend failed, resume interfaces that did get suspended */
  1070. if (status != 0) {
  1071. if (udev->actconfig) {
  1072. msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
  1073. while (++i < n) {
  1074. intf = udev->actconfig->interface[i];
  1075. usb_resume_interface(udev, intf, msg, 0);
  1076. }
  1077. }
  1078. /* If the suspend succeeded then prevent any more URB submissions
  1079. * and flush any outstanding URBs.
  1080. */
  1081. } else {
  1082. udev->can_submit = 0;
  1083. for (i = 0; i < 16; ++i) {
  1084. usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
  1085. usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
  1086. }
  1087. }
  1088. done:
  1089. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1090. return status;
  1091. }
  1092. /**
  1093. * usb_resume_both - resume a USB device and its interfaces
  1094. * @udev: the usb_device to resume
  1095. * @msg: Power Management message describing this state transition
  1096. *
  1097. * This is the central routine for resuming USB devices. It calls the
  1098. * the resume method for @udev and then calls the resume methods for all
  1099. * the interface drivers in @udev.
  1100. *
  1101. * Autoresume requests originating from a child device or an interface
  1102. * driver may be made without the protection of @udev's device lock, but
  1103. * all other resume calls will hold the lock. Usbcore will insure that
  1104. * method calls do not arrive during bind, unbind, or reset operations.
  1105. * However drivers must be prepared to handle resume calls arriving at
  1106. * unpredictable times.
  1107. *
  1108. * This routine can run only in process context.
  1109. */
  1110. static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
  1111. {
  1112. int status = 0;
  1113. int i;
  1114. struct usb_interface *intf;
  1115. if (udev->state == USB_STATE_NOTATTACHED) {
  1116. status = -ENODEV;
  1117. goto done;
  1118. }
  1119. udev->can_submit = 1;
  1120. /* Resume the device */
  1121. if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
  1122. status = usb_resume_device(udev, msg);
  1123. /* Resume the interfaces */
  1124. if (status == 0 && udev->actconfig) {
  1125. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1126. intf = udev->actconfig->interface[i];
  1127. usb_resume_interface(udev, intf, msg,
  1128. udev->reset_resume);
  1129. }
  1130. }
  1131. usb_mark_last_busy(udev);
  1132. done:
  1133. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1134. if (!status)
  1135. udev->reset_resume = 0;
  1136. return status;
  1137. }
  1138. static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
  1139. {
  1140. int w;
  1141. /* Remote wakeup is needed only when we actually go to sleep.
  1142. * For things like FREEZE and QUIESCE, if the device is already
  1143. * autosuspended then its current wakeup setting is okay.
  1144. */
  1145. if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
  1146. if (udev->state != USB_STATE_SUSPENDED)
  1147. udev->do_remote_wakeup = 0;
  1148. return;
  1149. }
  1150. /* Enable remote wakeup if it is allowed, even if no interface drivers
  1151. * actually want it.
  1152. */
  1153. w = device_may_wakeup(&udev->dev);
  1154. /* If the device is autosuspended with the wrong wakeup setting,
  1155. * autoresume now so the setting can be changed.
  1156. */
  1157. if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
  1158. pm_runtime_resume(&udev->dev);
  1159. udev->do_remote_wakeup = w;
  1160. }
  1161. /* The device lock is held by the PM core */
  1162. int usb_suspend(struct device *dev, pm_message_t msg)
  1163. {
  1164. struct usb_device *udev = to_usb_device(dev);
  1165. unbind_no_pm_drivers_interfaces(udev);
  1166. /* From now on we are sure all drivers support suspend/resume
  1167. * but not necessarily reset_resume()
  1168. * so we may still need to unbind and rebind upon resume
  1169. */
  1170. choose_wakeup(udev, msg);
  1171. return usb_suspend_both(udev, msg);
  1172. }
  1173. /* The device lock is held by the PM core */
  1174. int usb_resume_complete(struct device *dev)
  1175. {
  1176. struct usb_device *udev = to_usb_device(dev);
  1177. /* For PM complete calls, all we do is rebind interfaces
  1178. * whose needs_binding flag is set
  1179. */
  1180. if (udev->state != USB_STATE_NOTATTACHED)
  1181. do_rebind_interfaces(udev);
  1182. return 0;
  1183. }
  1184. /* The device lock is held by the PM core */
  1185. int usb_resume(struct device *dev, pm_message_t msg)
  1186. {
  1187. struct usb_device *udev = to_usb_device(dev);
  1188. int status;
  1189. /* For all calls, take the device back to full power and
  1190. * tell the PM core in case it was autosuspended previously.
  1191. * Unbind the interfaces that will need rebinding later,
  1192. * because they fail to support reset_resume.
  1193. * (This can't be done in usb_resume_interface()
  1194. * above because it doesn't own the right set of locks.)
  1195. */
  1196. status = usb_resume_both(udev, msg);
  1197. if (status == 0) {
  1198. pm_runtime_disable(dev);
  1199. pm_runtime_set_active(dev);
  1200. pm_runtime_enable(dev);
  1201. unbind_no_reset_resume_drivers_interfaces(udev);
  1202. }
  1203. /* Avoid PM error messages for devices disconnected while suspended
  1204. * as we'll display regular disconnect messages just a bit later.
  1205. */
  1206. if (status == -ENODEV || status == -ESHUTDOWN)
  1207. status = 0;
  1208. return status;
  1209. }
  1210. #endif /* CONFIG_PM */
  1211. #ifdef CONFIG_PM_RUNTIME
  1212. /**
  1213. * usb_enable_autosuspend - allow a USB device to be autosuspended
  1214. * @udev: the USB device which may be autosuspended
  1215. *
  1216. * This routine allows @udev to be autosuspended. An autosuspend won't
  1217. * take place until the autosuspend_delay has elapsed and all the other
  1218. * necessary conditions are satisfied.
  1219. *
  1220. * The caller must hold @udev's device lock.
  1221. */
  1222. void usb_enable_autosuspend(struct usb_device *udev)
  1223. {
  1224. pm_runtime_allow(&udev->dev);
  1225. }
  1226. EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
  1227. /**
  1228. * usb_disable_autosuspend - prevent a USB device from being autosuspended
  1229. * @udev: the USB device which may not be autosuspended
  1230. *
  1231. * This routine prevents @udev from being autosuspended and wakes it up
  1232. * if it is already autosuspended.
  1233. *
  1234. * The caller must hold @udev's device lock.
  1235. */
  1236. void usb_disable_autosuspend(struct usb_device *udev)
  1237. {
  1238. pm_runtime_forbid(&udev->dev);
  1239. }
  1240. EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
  1241. /**
  1242. * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
  1243. * @udev: the usb_device to autosuspend
  1244. *
  1245. * This routine should be called when a core subsystem is finished using
  1246. * @udev and wants to allow it to autosuspend. Examples would be when
  1247. * @udev's device file in usbfs is closed or after a configuration change.
  1248. *
  1249. * @udev's usage counter is decremented; if it drops to 0 and all the
  1250. * interfaces are inactive then a delayed autosuspend will be attempted.
  1251. * The attempt may fail (see autosuspend_check()).
  1252. *
  1253. * The caller must hold @udev's device lock.
  1254. *
  1255. * This routine can run only in process context.
  1256. */
  1257. void usb_autosuspend_device(struct usb_device *udev)
  1258. {
  1259. int status;
  1260. usb_mark_last_busy(udev);
  1261. status = pm_runtime_put_sync_autosuspend(&udev->dev);
  1262. dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
  1263. __func__, atomic_read(&udev->dev.power.usage_count),
  1264. status);
  1265. }
  1266. /**
  1267. * usb_autoresume_device - immediately autoresume a USB device and its interfaces
  1268. * @udev: the usb_device to autoresume
  1269. *
  1270. * This routine should be called when a core subsystem wants to use @udev
  1271. * and needs to guarantee that it is not suspended. No autosuspend will
  1272. * occur until usb_autosuspend_device() is called. (Note that this will
  1273. * not prevent suspend events originating in the PM core.) Examples would
  1274. * be when @udev's device file in usbfs is opened or when a remote-wakeup
  1275. * request is received.
  1276. *
  1277. * @udev's usage counter is incremented to prevent subsequent autosuspends.
  1278. * However if the autoresume fails then the usage counter is re-decremented.
  1279. *
  1280. * The caller must hold @udev's device lock.
  1281. *
  1282. * This routine can run only in process context.
  1283. */
  1284. int usb_autoresume_device(struct usb_device *udev)
  1285. {
  1286. int status;
  1287. status = pm_runtime_get_sync(&udev->dev);
  1288. if (status < 0)
  1289. pm_runtime_put_sync(&udev->dev);
  1290. dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
  1291. __func__, atomic_read(&udev->dev.power.usage_count),
  1292. status);
  1293. if (status > 0)
  1294. status = 0;
  1295. return status;
  1296. }
  1297. /**
  1298. * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
  1299. * @intf: the usb_interface whose counter should be decremented
  1300. *
  1301. * This routine should be called by an interface driver when it is
  1302. * finished using @intf and wants to allow it to autosuspend. A typical
  1303. * example would be a character-device driver when its device file is
  1304. * closed.
  1305. *
  1306. * The routine decrements @intf's usage counter. When the counter reaches
  1307. * 0, a delayed autosuspend request for @intf's device is attempted. The
  1308. * attempt may fail (see autosuspend_check()).
  1309. *
  1310. * This routine can run only in process context.
  1311. */
  1312. void usb_autopm_put_interface(struct usb_interface *intf)
  1313. {
  1314. struct usb_device *udev = interface_to_usbdev(intf);
  1315. int status;
  1316. usb_mark_last_busy(udev);
  1317. atomic_dec(&intf->pm_usage_cnt);
  1318. status = pm_runtime_put_sync(&intf->dev);
  1319. dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
  1320. __func__, atomic_read(&intf->dev.power.usage_count),
  1321. status);
  1322. }
  1323. EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
  1324. /**
  1325. * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
  1326. * @intf: the usb_interface whose counter should be decremented
  1327. *
  1328. * This routine does much the same thing as usb_autopm_put_interface():
  1329. * It decrements @intf's usage counter and schedules a delayed
  1330. * autosuspend request if the counter is <= 0. The difference is that it
  1331. * does not perform any synchronization; callers should hold a private
  1332. * lock and handle all synchronization issues themselves.
  1333. *
  1334. * Typically a driver would call this routine during an URB's completion
  1335. * handler, if no more URBs were pending.
  1336. *
  1337. * This routine can run in atomic context.
  1338. */
  1339. void usb_autopm_put_interface_async(struct usb_interface *intf)
  1340. {
  1341. struct usb_device *udev = interface_to_usbdev(intf);
  1342. int status;
  1343. usb_mark_last_busy(udev);
  1344. atomic_dec(&intf->pm_usage_cnt);
  1345. status = pm_runtime_put(&intf->dev);
  1346. dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
  1347. __func__, atomic_read(&intf->dev.power.usage_count),
  1348. status);
  1349. }
  1350. EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
  1351. /**
  1352. * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
  1353. * @intf: the usb_interface whose counter should be decremented
  1354. *
  1355. * This routine decrements @intf's usage counter but does not carry out an
  1356. * autosuspend.
  1357. *
  1358. * This routine can run in atomic context.
  1359. */
  1360. void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
  1361. {
  1362. struct usb_device *udev = interface_to_usbdev(intf);
  1363. usb_mark_last_busy(udev);
  1364. atomic_dec(&intf->pm_usage_cnt);
  1365. pm_runtime_put_noidle(&intf->dev);
  1366. }
  1367. EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
  1368. /**
  1369. * usb_autopm_get_interface - increment a USB interface's PM-usage counter
  1370. * @intf: the usb_interface whose counter should be incremented
  1371. *
  1372. * This routine should be called by an interface driver when it wants to
  1373. * use @intf and needs to guarantee that it is not suspended. In addition,
  1374. * the routine prevents @intf from being autosuspended subsequently. (Note
  1375. * that this will not prevent suspend events originating in the PM core.)
  1376. * This prevention will persist until usb_autopm_put_interface() is called
  1377. * or @intf is unbound. A typical example would be a character-device
  1378. * driver when its device file is opened.
  1379. *
  1380. * @intf's usage counter is incremented to prevent subsequent autosuspends.
  1381. * However if the autoresume fails then the counter is re-decremented.
  1382. *
  1383. * This routine can run only in process context.
  1384. */
  1385. int usb_autopm_get_interface(struct usb_interface *intf)
  1386. {
  1387. int status;
  1388. status = pm_runtime_get_sync(&intf->dev);
  1389. if (status < 0)
  1390. pm_runtime_put_sync(&intf->dev);
  1391. else
  1392. atomic_inc(&intf->pm_usage_cnt);
  1393. dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
  1394. __func__, atomic_read(&intf->dev.power.usage_count),
  1395. status);
  1396. if (status > 0)
  1397. status = 0;
  1398. return status;
  1399. }
  1400. EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
  1401. /**
  1402. * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
  1403. * @intf: the usb_interface whose counter should be incremented
  1404. *
  1405. * This routine does much the same thing as
  1406. * usb_autopm_get_interface(): It increments @intf's usage counter and
  1407. * queues an autoresume request if the device is suspended. The
  1408. * differences are that it does not perform any synchronization (callers
  1409. * should hold a private lock and handle all synchronization issues
  1410. * themselves), and it does not autoresume the device directly (it only
  1411. * queues a request). After a successful call, the device may not yet be
  1412. * resumed.
  1413. *
  1414. * This routine can run in atomic context.
  1415. */
  1416. int usb_autopm_get_interface_async(struct usb_interface *intf)
  1417. {
  1418. int status;
  1419. status = pm_runtime_get(&intf->dev);
  1420. if (status < 0 && status != -EINPROGRESS)
  1421. pm_runtime_put_noidle(&intf->dev);
  1422. else
  1423. atomic_inc(&intf->pm_usage_cnt);
  1424. dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
  1425. __func__, atomic_read(&intf->dev.power.usage_count),
  1426. status);
  1427. if (status > 0 || status == -EINPROGRESS)
  1428. status = 0;
  1429. return status;
  1430. }
  1431. EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
  1432. /**
  1433. * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
  1434. * @intf: the usb_interface whose counter should be incremented
  1435. *
  1436. * This routine increments @intf's usage counter but does not carry out an
  1437. * autoresume.
  1438. *
  1439. * This routine can run in atomic context.
  1440. */
  1441. void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
  1442. {
  1443. struct usb_device *udev = interface_to_usbdev(intf);
  1444. usb_mark_last_busy(udev);
  1445. atomic_inc(&intf->pm_usage_cnt);
  1446. pm_runtime_get_noresume(&intf->dev);
  1447. }
  1448. EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
  1449. /* Internal routine to check whether we may autosuspend a device. */
  1450. static int autosuspend_check(struct usb_device *udev)
  1451. {
  1452. int w, i;
  1453. struct usb_interface *intf;
  1454. /* Fail if autosuspend is disabled, or any interfaces are in use, or
  1455. * any interface drivers require remote wakeup but it isn't available.
  1456. */
  1457. w = 0;
  1458. if (udev->actconfig) {
  1459. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1460. intf = udev->actconfig->interface[i];
  1461. /* We don't need to check interfaces that are
  1462. * disabled for runtime PM. Either they are unbound
  1463. * or else their drivers don't support autosuspend
  1464. * and so they are permanently active.
  1465. */
  1466. if (intf->dev.power.disable_depth)
  1467. continue;
  1468. if (atomic_read(&intf->dev.power.usage_count) > 0)
  1469. return -EBUSY;
  1470. w |= intf->needs_remote_wakeup;
  1471. /* Don't allow autosuspend if the device will need
  1472. * a reset-resume and any of its interface drivers
  1473. * doesn't include support or needs remote wakeup.
  1474. */
  1475. if (udev->quirks & USB_QUIRK_RESET_RESUME) {
  1476. struct usb_driver *driver;
  1477. driver = to_usb_driver(intf->dev.driver);
  1478. if (!driver->reset_resume ||
  1479. intf->needs_remote_wakeup)
  1480. return -EOPNOTSUPP;
  1481. }
  1482. }
  1483. }
  1484. if (w && !device_can_wakeup(&udev->dev)) {
  1485. dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
  1486. return -EOPNOTSUPP;
  1487. }
  1488. udev->do_remote_wakeup = w;
  1489. return 0;
  1490. }
  1491. int usb_runtime_suspend(struct device *dev)
  1492. {
  1493. struct usb_device *udev = to_usb_device(dev);
  1494. int status;
  1495. /* A USB device can be suspended if it passes the various autosuspend
  1496. * checks. Runtime suspend for a USB device means suspending all the
  1497. * interfaces and then the device itself.
  1498. */
  1499. if (autosuspend_check(udev) != 0)
  1500. return -EAGAIN;
  1501. status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
  1502. /* Allow a retry if autosuspend failed temporarily */
  1503. if (status == -EAGAIN || status == -EBUSY)
  1504. usb_mark_last_busy(udev);
  1505. /* The PM core reacts badly unless the return code is 0,
  1506. * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
  1507. */
  1508. if (status != 0)
  1509. return -EBUSY;
  1510. return status;
  1511. }
  1512. int usb_runtime_resume(struct device *dev)
  1513. {
  1514. struct usb_device *udev = to_usb_device(dev);
  1515. int status;
  1516. /* Runtime resume for a USB device means resuming both the device
  1517. * and all its interfaces.
  1518. */
  1519. status = usb_resume_both(udev, PMSG_AUTO_RESUME);
  1520. return status;
  1521. }
  1522. int usb_runtime_idle(struct device *dev)
  1523. {
  1524. struct usb_device *udev = to_usb_device(dev);
  1525. /* An idle USB device can be suspended if it passes the various
  1526. * autosuspend checks.
  1527. */
  1528. if (autosuspend_check(udev) == 0)
  1529. pm_runtime_autosuspend(dev);
  1530. /* Tell the core not to suspend it, though. */
  1531. return -EBUSY;
  1532. }
  1533. int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
  1534. {
  1535. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1536. int ret = -EPERM;
  1537. if (hcd->driver->set_usb2_hw_lpm) {
  1538. ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
  1539. if (!ret)
  1540. udev->usb2_hw_lpm_enabled = enable;
  1541. }
  1542. return ret;
  1543. }
  1544. #endif /* CONFIG_PM_RUNTIME */
  1545. struct bus_type usb_bus_type = {
  1546. .name = "usb",
  1547. .match = usb_device_match,
  1548. .uevent = usb_uevent,
  1549. };