driver.c 55 KB

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