driver.c 54 KB

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