driver.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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. /* Terminate all URBs for this interface unless the driver
  352. * supports "soft" unbinding.
  353. */
  354. if (!driver->soft_unbind)
  355. usb_disable_interface(udev, intf, false);
  356. driver->disconnect(intf);
  357. usb_cancel_queued_reset(intf);
  358. /* Free streams */
  359. for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  360. ep = &intf->cur_altsetting->endpoint[i];
  361. if (ep->streams == 0)
  362. continue;
  363. if (j == 0) {
  364. eps = kmalloc(USB_MAXENDPOINTS * sizeof(void *),
  365. GFP_KERNEL);
  366. if (!eps) {
  367. dev_warn(dev, "oom, leaking streams\n");
  368. break;
  369. }
  370. }
  371. eps[j++] = ep;
  372. }
  373. if (j) {
  374. usb_free_streams(intf, eps, j, GFP_KERNEL);
  375. kfree(eps);
  376. }
  377. /* Reset other interface state.
  378. * We cannot do a Set-Interface if the device is suspended or
  379. * if it is prepared for a system sleep (since installing a new
  380. * altsetting means creating new endpoint device entries).
  381. * When either of these happens, defer the Set-Interface.
  382. */
  383. if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
  384. /* Already in altsetting 0 so skip Set-Interface.
  385. * Just re-enable it without affecting the endpoint toggles.
  386. */
  387. usb_enable_interface(udev, intf, false);
  388. } else if (!error && !intf->dev.power.is_prepared) {
  389. r = usb_set_interface(udev, intf->altsetting[0].
  390. desc.bInterfaceNumber, 0);
  391. if (r < 0)
  392. intf->needs_altsetting0 = 1;
  393. } else {
  394. intf->needs_altsetting0 = 1;
  395. }
  396. usb_set_intfdata(intf, NULL);
  397. intf->condition = USB_INTERFACE_UNBOUND;
  398. intf->needs_remote_wakeup = 0;
  399. /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
  400. if (!lpm_disable_error)
  401. usb_unlocked_enable_lpm(udev);
  402. /* Unbound interfaces are always runtime-PM-disabled and -suspended */
  403. if (driver->supports_autosuspend)
  404. pm_runtime_disable(dev);
  405. pm_runtime_set_suspended(dev);
  406. /* Undo any residual pm_autopm_get_interface_* calls */
  407. for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
  408. usb_autopm_put_interface_no_suspend(intf);
  409. atomic_set(&intf->pm_usage_cnt, 0);
  410. if (!error)
  411. usb_autosuspend_device(udev);
  412. return 0;
  413. }
  414. /**
  415. * usb_driver_claim_interface - bind a driver to an interface
  416. * @driver: the driver to be bound
  417. * @iface: the interface to which it will be bound; must be in the
  418. * usb device's active configuration
  419. * @priv: driver data associated with that interface
  420. *
  421. * This is used by usb device drivers that need to claim more than one
  422. * interface on a device when probing (audio and acm are current examples).
  423. * No device driver should directly modify internal usb_interface or
  424. * usb_device structure members.
  425. *
  426. * Few drivers should need to use this routine, since the most natural
  427. * way to bind to an interface is to return the private data from
  428. * the driver's probe() method.
  429. *
  430. * Callers must own the device lock, so driver probe() entries don't need
  431. * extra locking, but other call contexts may need to explicitly claim that
  432. * lock.
  433. *
  434. * Return: 0 on success.
  435. */
  436. int usb_driver_claim_interface(struct usb_driver *driver,
  437. struct usb_interface *iface, void *priv)
  438. {
  439. struct device *dev = &iface->dev;
  440. struct usb_device *udev;
  441. int retval = 0;
  442. int lpm_disable_error;
  443. if (dev->driver)
  444. return -EBUSY;
  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. /* Disable LPM until this driver is bound. */
  451. lpm_disable_error = usb_unlocked_disable_lpm(udev);
  452. if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
  453. dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
  454. __func__, driver->name);
  455. return -ENOMEM;
  456. }
  457. /* Claimed interfaces are initially inactive (suspended) and
  458. * runtime-PM-enabled, but only if the driver has autosuspend
  459. * support. Otherwise they are marked active, to prevent the
  460. * device from being autosuspended, but left disabled. In either
  461. * case they are sensitive to their children's power states.
  462. */
  463. pm_suspend_ignore_children(dev, false);
  464. if (driver->supports_autosuspend)
  465. pm_runtime_enable(dev);
  466. else
  467. pm_runtime_set_active(dev);
  468. /* if interface was already added, bind now; else let
  469. * the future device_add() bind it, bypassing probe()
  470. */
  471. if (device_is_registered(dev))
  472. retval = device_bind_driver(dev);
  473. /* Attempt to re-enable USB3 LPM, if the disable was successful. */
  474. if (!lpm_disable_error)
  475. usb_unlocked_enable_lpm(udev);
  476. return retval;
  477. }
  478. EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
  479. /**
  480. * usb_driver_release_interface - unbind a driver from an interface
  481. * @driver: the driver to be unbound
  482. * @iface: the interface from which it will be unbound
  483. *
  484. * This can be used by drivers to release an interface without waiting
  485. * for their disconnect() methods to be called. In typical cases this
  486. * also causes the driver disconnect() method to be called.
  487. *
  488. * This call is synchronous, and may not be used in an interrupt context.
  489. * Callers must own the device lock, so driver disconnect() entries don't
  490. * need extra locking, but other call contexts may need to explicitly claim
  491. * that lock.
  492. */
  493. void usb_driver_release_interface(struct usb_driver *driver,
  494. struct usb_interface *iface)
  495. {
  496. struct device *dev = &iface->dev;
  497. /* this should never happen, don't release something that's not ours */
  498. if (!dev->driver || dev->driver != &driver->drvwrap.driver)
  499. return;
  500. /* don't release from within disconnect() */
  501. if (iface->condition != USB_INTERFACE_BOUND)
  502. return;
  503. iface->condition = USB_INTERFACE_UNBINDING;
  504. /* Release via the driver core only if the interface
  505. * has already been registered
  506. */
  507. if (device_is_registered(dev)) {
  508. device_release_driver(dev);
  509. } else {
  510. device_lock(dev);
  511. usb_unbind_interface(dev);
  512. dev->driver = NULL;
  513. device_unlock(dev);
  514. }
  515. }
  516. EXPORT_SYMBOL_GPL(usb_driver_release_interface);
  517. /* returns 0 if no match, 1 if match */
  518. int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
  519. {
  520. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  521. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  522. return 0;
  523. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  524. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  525. return 0;
  526. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  527. greater than any unsigned number. */
  528. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  529. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  530. return 0;
  531. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  532. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  533. return 0;
  534. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  535. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  536. return 0;
  537. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  538. (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  539. return 0;
  540. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  541. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  542. return 0;
  543. return 1;
  544. }
  545. /* returns 0 if no match, 1 if match */
  546. int usb_match_one_id_intf(struct usb_device *dev,
  547. struct usb_host_interface *intf,
  548. const struct usb_device_id *id)
  549. {
  550. /* The interface class, subclass, protocol and number should never be
  551. * checked for a match if the device class is Vendor Specific,
  552. * unless the match record specifies the Vendor ID. */
  553. if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
  554. !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  555. (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
  556. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  557. USB_DEVICE_ID_MATCH_INT_PROTOCOL |
  558. USB_DEVICE_ID_MATCH_INT_NUMBER)))
  559. return 0;
  560. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  561. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  562. return 0;
  563. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  564. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  565. return 0;
  566. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  567. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  568. return 0;
  569. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
  570. (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
  571. return 0;
  572. return 1;
  573. }
  574. /* returns 0 if no match, 1 if match */
  575. int usb_match_one_id(struct usb_interface *interface,
  576. const struct usb_device_id *id)
  577. {
  578. struct usb_host_interface *intf;
  579. struct usb_device *dev;
  580. /* proc_connectinfo in devio.c may call us with id == NULL. */
  581. if (id == NULL)
  582. return 0;
  583. intf = interface->cur_altsetting;
  584. dev = interface_to_usbdev(interface);
  585. if (!usb_match_device(dev, id))
  586. return 0;
  587. return usb_match_one_id_intf(dev, intf, id);
  588. }
  589. EXPORT_SYMBOL_GPL(usb_match_one_id);
  590. /**
  591. * usb_match_id - find first usb_device_id matching device or interface
  592. * @interface: the interface of interest
  593. * @id: array of usb_device_id structures, terminated by zero entry
  594. *
  595. * usb_match_id searches an array of usb_device_id's and returns
  596. * the first one matching the device or interface, or null.
  597. * This is used when binding (or rebinding) a driver to an interface.
  598. * Most USB device drivers will use this indirectly, through the usb core,
  599. * but some layered driver frameworks use it directly.
  600. * These device tables are exported with MODULE_DEVICE_TABLE, through
  601. * modutils, to support the driver loading functionality of USB hotplugging.
  602. *
  603. * Return: The first matching usb_device_id, or %NULL.
  604. *
  605. * What Matches:
  606. *
  607. * The "match_flags" element in a usb_device_id controls which
  608. * members are used. If the corresponding bit is set, the
  609. * value in the device_id must match its corresponding member
  610. * in the device or interface descriptor, or else the device_id
  611. * does not match.
  612. *
  613. * "driver_info" is normally used only by device drivers,
  614. * but you can create a wildcard "matches anything" usb_device_id
  615. * as a driver's "modules.usbmap" entry if you provide an id with
  616. * only a nonzero "driver_info" field. If you do this, the USB device
  617. * driver's probe() routine should use additional intelligence to
  618. * decide whether to bind to the specified interface.
  619. *
  620. * What Makes Good usb_device_id Tables:
  621. *
  622. * The match algorithm is very simple, so that intelligence in
  623. * driver selection must come from smart driver id records.
  624. * Unless you have good reasons to use another selection policy,
  625. * provide match elements only in related groups, and order match
  626. * specifiers from specific to general. Use the macros provided
  627. * for that purpose if you can.
  628. *
  629. * The most specific match specifiers use device descriptor
  630. * data. These are commonly used with product-specific matches;
  631. * the USB_DEVICE macro lets you provide vendor and product IDs,
  632. * and you can also match against ranges of product revisions.
  633. * These are widely used for devices with application or vendor
  634. * specific bDeviceClass values.
  635. *
  636. * Matches based on device class/subclass/protocol specifications
  637. * are slightly more general; use the USB_DEVICE_INFO macro, or
  638. * its siblings. These are used with single-function devices
  639. * where bDeviceClass doesn't specify that each interface has
  640. * its own class.
  641. *
  642. * Matches based on interface class/subclass/protocol are the
  643. * most general; they let drivers bind to any interface on a
  644. * multiple-function device. Use the USB_INTERFACE_INFO
  645. * macro, or its siblings, to match class-per-interface style
  646. * devices (as recorded in bInterfaceClass).
  647. *
  648. * Note that an entry created by USB_INTERFACE_INFO won't match
  649. * any interface if the device class is set to Vendor-Specific.
  650. * This is deliberate; according to the USB spec the meanings of
  651. * the interface class/subclass/protocol for these devices are also
  652. * vendor-specific, and hence matching against a standard product
  653. * class wouldn't work anyway. If you really want to use an
  654. * interface-based match for such a device, create a match record
  655. * that also specifies the vendor ID. (Unforunately there isn't a
  656. * standard macro for creating records like this.)
  657. *
  658. * Within those groups, remember that not all combinations are
  659. * meaningful. For example, don't give a product version range
  660. * without vendor and product IDs; or specify a protocol without
  661. * its associated class and subclass.
  662. */
  663. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  664. const struct usb_device_id *id)
  665. {
  666. /* proc_connectinfo in devio.c may call us with id == NULL. */
  667. if (id == NULL)
  668. return NULL;
  669. /* It is important to check that id->driver_info is nonzero,
  670. since an entry that is all zeroes except for a nonzero
  671. id->driver_info is the way to create an entry that
  672. indicates that the driver want to examine every
  673. device and interface. */
  674. for (; id->idVendor || id->idProduct || id->bDeviceClass ||
  675. id->bInterfaceClass || id->driver_info; id++) {
  676. if (usb_match_one_id(interface, id))
  677. return id;
  678. }
  679. return NULL;
  680. }
  681. EXPORT_SYMBOL_GPL(usb_match_id);
  682. static int usb_device_match(struct device *dev, struct device_driver *drv)
  683. {
  684. /* devices and interfaces are handled separately */
  685. if (is_usb_device(dev)) {
  686. /* interface drivers never match devices */
  687. if (!is_usb_device_driver(drv))
  688. return 0;
  689. /* TODO: Add real matching code */
  690. return 1;
  691. } else if (is_usb_interface(dev)) {
  692. struct usb_interface *intf;
  693. struct usb_driver *usb_drv;
  694. const struct usb_device_id *id;
  695. /* device drivers never match interfaces */
  696. if (is_usb_device_driver(drv))
  697. return 0;
  698. intf = to_usb_interface(dev);
  699. usb_drv = to_usb_driver(drv);
  700. id = usb_match_id(intf, usb_drv->id_table);
  701. if (id)
  702. return 1;
  703. id = usb_match_dynamic_id(intf, usb_drv);
  704. if (id)
  705. return 1;
  706. }
  707. return 0;
  708. }
  709. static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
  710. {
  711. struct usb_device *usb_dev;
  712. if (is_usb_device(dev)) {
  713. usb_dev = to_usb_device(dev);
  714. } else if (is_usb_interface(dev)) {
  715. struct usb_interface *intf = to_usb_interface(dev);
  716. usb_dev = interface_to_usbdev(intf);
  717. } else {
  718. return 0;
  719. }
  720. if (usb_dev->devnum < 0) {
  721. /* driver is often null here; dev_dbg() would oops */
  722. pr_debug("usb %s: already deleted?\n", dev_name(dev));
  723. return -ENODEV;
  724. }
  725. if (!usb_dev->bus) {
  726. pr_debug("usb %s: bus removed?\n", dev_name(dev));
  727. return -ENODEV;
  728. }
  729. /* per-device configurations are common */
  730. if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
  731. le16_to_cpu(usb_dev->descriptor.idVendor),
  732. le16_to_cpu(usb_dev->descriptor.idProduct),
  733. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  734. return -ENOMEM;
  735. /* class-based driver binding models */
  736. if (add_uevent_var(env, "TYPE=%d/%d/%d",
  737. usb_dev->descriptor.bDeviceClass,
  738. usb_dev->descriptor.bDeviceSubClass,
  739. usb_dev->descriptor.bDeviceProtocol))
  740. return -ENOMEM;
  741. return 0;
  742. }
  743. /**
  744. * usb_register_device_driver - register a USB device (not interface) driver
  745. * @new_udriver: USB operations for the device driver
  746. * @owner: module owner of this driver.
  747. *
  748. * Registers a USB device driver with the USB core. The list of
  749. * unattached devices will be rescanned whenever a new driver is
  750. * added, allowing the new driver to attach to any recognized devices.
  751. *
  752. * Return: A negative error code on failure and 0 on success.
  753. */
  754. int usb_register_device_driver(struct usb_device_driver *new_udriver,
  755. struct module *owner)
  756. {
  757. int retval = 0;
  758. if (usb_disabled())
  759. return -ENODEV;
  760. new_udriver->drvwrap.for_devices = 1;
  761. new_udriver->drvwrap.driver.name = new_udriver->name;
  762. new_udriver->drvwrap.driver.bus = &usb_bus_type;
  763. new_udriver->drvwrap.driver.probe = usb_probe_device;
  764. new_udriver->drvwrap.driver.remove = usb_unbind_device;
  765. new_udriver->drvwrap.driver.owner = owner;
  766. retval = driver_register(&new_udriver->drvwrap.driver);
  767. if (!retval)
  768. pr_info("%s: registered new device driver %s\n",
  769. usbcore_name, new_udriver->name);
  770. else
  771. printk(KERN_ERR "%s: error %d registering device "
  772. " driver %s\n",
  773. usbcore_name, retval, new_udriver->name);
  774. return retval;
  775. }
  776. EXPORT_SYMBOL_GPL(usb_register_device_driver);
  777. /**
  778. * usb_deregister_device_driver - unregister a USB device (not interface) driver
  779. * @udriver: USB operations of the device driver to unregister
  780. * Context: must be able to sleep
  781. *
  782. * Unlinks the specified driver from the internal USB driver list.
  783. */
  784. void usb_deregister_device_driver(struct usb_device_driver *udriver)
  785. {
  786. pr_info("%s: deregistering device driver %s\n",
  787. usbcore_name, udriver->name);
  788. driver_unregister(&udriver->drvwrap.driver);
  789. }
  790. EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
  791. /**
  792. * usb_register_driver - register a USB interface driver
  793. * @new_driver: USB operations for the interface driver
  794. * @owner: module owner of this driver.
  795. * @mod_name: module name string
  796. *
  797. * Registers a USB interface driver with the USB core. The list of
  798. * unattached interfaces will be rescanned whenever a new driver is
  799. * added, allowing the new driver to attach to any recognized interfaces.
  800. *
  801. * Return: A negative error code on failure and 0 on success.
  802. *
  803. * NOTE: if you want your driver to use the USB major number, you must call
  804. * usb_register_dev() to enable that functionality. This function no longer
  805. * takes care of that.
  806. */
  807. int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
  808. const char *mod_name)
  809. {
  810. int retval = 0;
  811. if (usb_disabled())
  812. return -ENODEV;
  813. new_driver->drvwrap.for_devices = 0;
  814. new_driver->drvwrap.driver.name = new_driver->name;
  815. new_driver->drvwrap.driver.bus = &usb_bus_type;
  816. new_driver->drvwrap.driver.probe = usb_probe_interface;
  817. new_driver->drvwrap.driver.remove = usb_unbind_interface;
  818. new_driver->drvwrap.driver.owner = owner;
  819. new_driver->drvwrap.driver.mod_name = mod_name;
  820. spin_lock_init(&new_driver->dynids.lock);
  821. INIT_LIST_HEAD(&new_driver->dynids.list);
  822. retval = driver_register(&new_driver->drvwrap.driver);
  823. if (retval)
  824. goto out;
  825. retval = usb_create_newid_files(new_driver);
  826. if (retval)
  827. goto out_newid;
  828. pr_info("%s: registered new interface driver %s\n",
  829. usbcore_name, new_driver->name);
  830. out:
  831. return retval;
  832. out_newid:
  833. driver_unregister(&new_driver->drvwrap.driver);
  834. printk(KERN_ERR "%s: error %d registering interface "
  835. " driver %s\n",
  836. usbcore_name, retval, new_driver->name);
  837. goto out;
  838. }
  839. EXPORT_SYMBOL_GPL(usb_register_driver);
  840. /**
  841. * usb_deregister - unregister a USB interface driver
  842. * @driver: USB operations of the interface driver to unregister
  843. * Context: must be able to sleep
  844. *
  845. * Unlinks the specified driver from the internal USB driver list.
  846. *
  847. * NOTE: If you called usb_register_dev(), you still need to call
  848. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  849. * this * call will no longer do it for you.
  850. */
  851. void usb_deregister(struct usb_driver *driver)
  852. {
  853. pr_info("%s: deregistering interface driver %s\n",
  854. usbcore_name, driver->name);
  855. usb_remove_newid_files(driver);
  856. driver_unregister(&driver->drvwrap.driver);
  857. usb_free_dynids(driver);
  858. }
  859. EXPORT_SYMBOL_GPL(usb_deregister);
  860. /* Forced unbinding of a USB interface driver, either because
  861. * it doesn't support pre_reset/post_reset/reset_resume or
  862. * because it doesn't support suspend/resume.
  863. *
  864. * The caller must hold @intf's device's lock, but not @intf's lock.
  865. */
  866. void usb_forced_unbind_intf(struct usb_interface *intf)
  867. {
  868. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  869. dev_dbg(&intf->dev, "forced unbind\n");
  870. usb_driver_release_interface(driver, intf);
  871. /* Mark the interface for later rebinding */
  872. intf->needs_binding = 1;
  873. }
  874. /*
  875. * Unbind drivers for @udev's marked interfaces. These interfaces have
  876. * the needs_binding flag set, for example by usb_resume_interface().
  877. *
  878. * The caller must hold @udev's device lock.
  879. */
  880. static void unbind_marked_interfaces(struct usb_device *udev)
  881. {
  882. struct usb_host_config *config;
  883. int i;
  884. struct usb_interface *intf;
  885. config = udev->actconfig;
  886. if (config) {
  887. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  888. intf = config->interface[i];
  889. if (intf->dev.driver && intf->needs_binding)
  890. usb_forced_unbind_intf(intf);
  891. }
  892. }
  893. }
  894. /* Delayed forced unbinding of a USB interface driver and scan
  895. * for rebinding.
  896. *
  897. * The caller must hold @intf's device's lock, but not @intf's lock.
  898. *
  899. * Note: Rebinds will be skipped if a system sleep transition is in
  900. * progress and the PM "complete" callback hasn't occurred yet.
  901. */
  902. static void usb_rebind_intf(struct usb_interface *intf)
  903. {
  904. int rc;
  905. /* Delayed unbind of an existing driver */
  906. if (intf->dev.driver)
  907. usb_forced_unbind_intf(intf);
  908. /* Try to rebind the interface */
  909. if (!intf->dev.power.is_prepared) {
  910. intf->needs_binding = 0;
  911. rc = device_attach(&intf->dev);
  912. if (rc < 0)
  913. dev_warn(&intf->dev, "rebind failed: %d\n", rc);
  914. }
  915. }
  916. /*
  917. * Rebind drivers to @udev's marked interfaces. These interfaces have
  918. * the needs_binding flag set.
  919. *
  920. * The caller must hold @udev's device lock.
  921. */
  922. static void rebind_marked_interfaces(struct usb_device *udev)
  923. {
  924. struct usb_host_config *config;
  925. int i;
  926. struct usb_interface *intf;
  927. config = udev->actconfig;
  928. if (config) {
  929. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  930. intf = config->interface[i];
  931. if (intf->needs_binding)
  932. usb_rebind_intf(intf);
  933. }
  934. }
  935. }
  936. /*
  937. * Unbind all of @udev's marked interfaces and then rebind all of them.
  938. * This ordering is necessary because some drivers claim several interfaces
  939. * when they are first probed.
  940. *
  941. * The caller must hold @udev's device lock.
  942. */
  943. void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev)
  944. {
  945. unbind_marked_interfaces(udev);
  946. rebind_marked_interfaces(udev);
  947. }
  948. #ifdef CONFIG_PM
  949. /* Unbind drivers for @udev's interfaces that don't support suspend/resume
  950. * There is no check for reset_resume here because it can be determined
  951. * only during resume whether reset_resume is needed.
  952. *
  953. * The caller must hold @udev's device lock.
  954. */
  955. static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
  956. {
  957. struct usb_host_config *config;
  958. int i;
  959. struct usb_interface *intf;
  960. struct usb_driver *drv;
  961. config = udev->actconfig;
  962. if (config) {
  963. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  964. intf = config->interface[i];
  965. if (intf->dev.driver) {
  966. drv = to_usb_driver(intf->dev.driver);
  967. if (!drv->suspend || !drv->resume)
  968. usb_forced_unbind_intf(intf);
  969. }
  970. }
  971. }
  972. }
  973. static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
  974. {
  975. struct usb_device_driver *udriver;
  976. int status = 0;
  977. if (udev->state == USB_STATE_NOTATTACHED ||
  978. udev->state == USB_STATE_SUSPENDED)
  979. goto done;
  980. /* For devices that don't have a driver, we do a generic suspend. */
  981. if (udev->dev.driver)
  982. udriver = to_usb_device_driver(udev->dev.driver);
  983. else {
  984. udev->do_remote_wakeup = 0;
  985. udriver = &usb_generic_driver;
  986. }
  987. status = udriver->suspend(udev, msg);
  988. done:
  989. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  990. return status;
  991. }
  992. static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
  993. {
  994. struct usb_device_driver *udriver;
  995. int status = 0;
  996. if (udev->state == USB_STATE_NOTATTACHED)
  997. goto done;
  998. /* Can't resume it if it doesn't have a driver. */
  999. if (udev->dev.driver == NULL) {
  1000. status = -ENOTCONN;
  1001. goto done;
  1002. }
  1003. /* Non-root devices on a full/low-speed bus must wait for their
  1004. * companion high-speed root hub, in case a handoff is needed.
  1005. */
  1006. if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
  1007. device_pm_wait_for_dev(&udev->dev,
  1008. &udev->bus->hs_companion->root_hub->dev);
  1009. if (udev->quirks & USB_QUIRK_RESET_RESUME)
  1010. udev->reset_resume = 1;
  1011. udriver = to_usb_device_driver(udev->dev.driver);
  1012. status = udriver->resume(udev, msg);
  1013. done:
  1014. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1015. return status;
  1016. }
  1017. static int usb_suspend_interface(struct usb_device *udev,
  1018. struct usb_interface *intf, pm_message_t msg)
  1019. {
  1020. struct usb_driver *driver;
  1021. int status = 0;
  1022. if (udev->state == USB_STATE_NOTATTACHED ||
  1023. intf->condition == USB_INTERFACE_UNBOUND)
  1024. goto done;
  1025. driver = to_usb_driver(intf->dev.driver);
  1026. /* at this time we know the driver supports suspend */
  1027. status = driver->suspend(intf, msg);
  1028. if (status && !PMSG_IS_AUTO(msg))
  1029. dev_err(&intf->dev, "suspend error %d\n", status);
  1030. done:
  1031. dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
  1032. return status;
  1033. }
  1034. static int usb_resume_interface(struct usb_device *udev,
  1035. struct usb_interface *intf, pm_message_t msg, int reset_resume)
  1036. {
  1037. struct usb_driver *driver;
  1038. int status = 0;
  1039. if (udev->state == USB_STATE_NOTATTACHED)
  1040. goto done;
  1041. /* Don't let autoresume interfere with unbinding */
  1042. if (intf->condition == USB_INTERFACE_UNBINDING)
  1043. goto done;
  1044. /* Can't resume it if it doesn't have a driver. */
  1045. if (intf->condition == USB_INTERFACE_UNBOUND) {
  1046. /* Carry out a deferred switch to altsetting 0 */
  1047. if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
  1048. usb_set_interface(udev, intf->altsetting[0].
  1049. desc.bInterfaceNumber, 0);
  1050. intf->needs_altsetting0 = 0;
  1051. }
  1052. goto done;
  1053. }
  1054. /* Don't resume if the interface is marked for rebinding */
  1055. if (intf->needs_binding)
  1056. goto done;
  1057. driver = to_usb_driver(intf->dev.driver);
  1058. if (reset_resume) {
  1059. if (driver->reset_resume) {
  1060. status = driver->reset_resume(intf);
  1061. if (status)
  1062. dev_err(&intf->dev, "%s error %d\n",
  1063. "reset_resume", status);
  1064. } else {
  1065. intf->needs_binding = 1;
  1066. dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
  1067. driver->name);
  1068. }
  1069. } else {
  1070. status = driver->resume(intf);
  1071. if (status)
  1072. dev_err(&intf->dev, "resume error %d\n", status);
  1073. }
  1074. done:
  1075. dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
  1076. /* Later we will unbind the driver and/or reprobe, if necessary */
  1077. return status;
  1078. }
  1079. /**
  1080. * usb_suspend_both - suspend a USB device and its interfaces
  1081. * @udev: the usb_device to suspend
  1082. * @msg: Power Management message describing this state transition
  1083. *
  1084. * This is the central routine for suspending USB devices. It calls the
  1085. * suspend methods for all the interface drivers in @udev and then calls
  1086. * the suspend method for @udev itself. When the routine is called in
  1087. * autosuspend, if an error occurs at any stage, all the interfaces
  1088. * which were suspended are resumed so that they remain in the same
  1089. * state as the device, but when called from system sleep, all error
  1090. * from suspend methods of interfaces and the non-root-hub device itself
  1091. * are simply ignored, so all suspended interfaces are only resumed
  1092. * to the device's state when @udev is root-hub and its suspend method
  1093. * returns failure.
  1094. *
  1095. * Autosuspend requests originating from a child device or an interface
  1096. * driver may be made without the protection of @udev's device lock, but
  1097. * all other suspend calls will hold the lock. Usbcore will insure that
  1098. * method calls do not arrive during bind, unbind, or reset operations.
  1099. * However drivers must be prepared to handle suspend calls arriving at
  1100. * unpredictable times.
  1101. *
  1102. * This routine can run only in process context.
  1103. *
  1104. * Return: 0 if the suspend succeeded.
  1105. */
  1106. static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
  1107. {
  1108. int status = 0;
  1109. int i = 0, n = 0;
  1110. struct usb_interface *intf;
  1111. if (udev->state == USB_STATE_NOTATTACHED ||
  1112. udev->state == USB_STATE_SUSPENDED)
  1113. goto done;
  1114. /* Suspend all the interfaces and then udev itself */
  1115. if (udev->actconfig) {
  1116. n = udev->actconfig->desc.bNumInterfaces;
  1117. for (i = n - 1; i >= 0; --i) {
  1118. intf = udev->actconfig->interface[i];
  1119. status = usb_suspend_interface(udev, intf, msg);
  1120. /* Ignore errors during system sleep transitions */
  1121. if (!PMSG_IS_AUTO(msg))
  1122. status = 0;
  1123. if (status != 0)
  1124. break;
  1125. }
  1126. }
  1127. if (status == 0) {
  1128. status = usb_suspend_device(udev, msg);
  1129. /*
  1130. * Ignore errors from non-root-hub devices during
  1131. * system sleep transitions. For the most part,
  1132. * these devices should go to low power anyway when
  1133. * the entire bus is suspended.
  1134. */
  1135. if (udev->parent && !PMSG_IS_AUTO(msg))
  1136. status = 0;
  1137. }
  1138. /* If the suspend failed, resume interfaces that did get suspended */
  1139. if (status != 0) {
  1140. if (udev->actconfig) {
  1141. msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
  1142. while (++i < n) {
  1143. intf = udev->actconfig->interface[i];
  1144. usb_resume_interface(udev, intf, msg, 0);
  1145. }
  1146. }
  1147. /* If the suspend succeeded then prevent any more URB submissions
  1148. * and flush any outstanding URBs.
  1149. */
  1150. } else {
  1151. udev->can_submit = 0;
  1152. for (i = 0; i < 16; ++i) {
  1153. usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
  1154. usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
  1155. }
  1156. }
  1157. done:
  1158. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1159. return status;
  1160. }
  1161. /**
  1162. * usb_resume_both - resume a USB device and its interfaces
  1163. * @udev: the usb_device to resume
  1164. * @msg: Power Management message describing this state transition
  1165. *
  1166. * This is the central routine for resuming USB devices. It calls the
  1167. * the resume method for @udev and then calls the resume methods for all
  1168. * the interface drivers in @udev.
  1169. *
  1170. * Autoresume requests originating from a child device or an interface
  1171. * driver may be made without the protection of @udev's device lock, but
  1172. * all other resume calls will hold the lock. Usbcore will insure that
  1173. * method calls do not arrive during bind, unbind, or reset operations.
  1174. * However drivers must be prepared to handle resume calls arriving at
  1175. * unpredictable times.
  1176. *
  1177. * This routine can run only in process context.
  1178. *
  1179. * Return: 0 on success.
  1180. */
  1181. static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
  1182. {
  1183. int status = 0;
  1184. int i;
  1185. struct usb_interface *intf;
  1186. if (udev->state == USB_STATE_NOTATTACHED) {
  1187. status = -ENODEV;
  1188. goto done;
  1189. }
  1190. udev->can_submit = 1;
  1191. /* Resume the device */
  1192. if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
  1193. status = usb_resume_device(udev, msg);
  1194. /* Resume the interfaces */
  1195. if (status == 0 && udev->actconfig) {
  1196. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1197. intf = udev->actconfig->interface[i];
  1198. usb_resume_interface(udev, intf, msg,
  1199. udev->reset_resume);
  1200. }
  1201. }
  1202. usb_mark_last_busy(udev);
  1203. done:
  1204. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1205. if (!status)
  1206. udev->reset_resume = 0;
  1207. return status;
  1208. }
  1209. static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
  1210. {
  1211. int w;
  1212. /* Remote wakeup is needed only when we actually go to sleep.
  1213. * For things like FREEZE and QUIESCE, if the device is already
  1214. * autosuspended then its current wakeup setting is okay.
  1215. */
  1216. if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
  1217. if (udev->state != USB_STATE_SUSPENDED)
  1218. udev->do_remote_wakeup = 0;
  1219. return;
  1220. }
  1221. /* Enable remote wakeup if it is allowed, even if no interface drivers
  1222. * actually want it.
  1223. */
  1224. w = device_may_wakeup(&udev->dev);
  1225. /* If the device is autosuspended with the wrong wakeup setting,
  1226. * autoresume now so the setting can be changed.
  1227. */
  1228. if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
  1229. pm_runtime_resume(&udev->dev);
  1230. udev->do_remote_wakeup = w;
  1231. }
  1232. /* The device lock is held by the PM core */
  1233. int usb_suspend(struct device *dev, pm_message_t msg)
  1234. {
  1235. struct usb_device *udev = to_usb_device(dev);
  1236. unbind_no_pm_drivers_interfaces(udev);
  1237. /* From now on we are sure all drivers support suspend/resume
  1238. * but not necessarily reset_resume()
  1239. * so we may still need to unbind and rebind upon resume
  1240. */
  1241. choose_wakeup(udev, msg);
  1242. return usb_suspend_both(udev, msg);
  1243. }
  1244. /* The device lock is held by the PM core */
  1245. int usb_resume_complete(struct device *dev)
  1246. {
  1247. struct usb_device *udev = to_usb_device(dev);
  1248. /* For PM complete calls, all we do is rebind interfaces
  1249. * whose needs_binding flag is set
  1250. */
  1251. if (udev->state != USB_STATE_NOTATTACHED)
  1252. rebind_marked_interfaces(udev);
  1253. return 0;
  1254. }
  1255. /* The device lock is held by the PM core */
  1256. int usb_resume(struct device *dev, pm_message_t msg)
  1257. {
  1258. struct usb_device *udev = to_usb_device(dev);
  1259. int status;
  1260. /* For all calls, take the device back to full power and
  1261. * tell the PM core in case it was autosuspended previously.
  1262. * Unbind the interfaces that will need rebinding later,
  1263. * because they fail to support reset_resume.
  1264. * (This can't be done in usb_resume_interface()
  1265. * above because it doesn't own the right set of locks.)
  1266. */
  1267. status = usb_resume_both(udev, msg);
  1268. if (status == 0) {
  1269. pm_runtime_disable(dev);
  1270. pm_runtime_set_active(dev);
  1271. pm_runtime_enable(dev);
  1272. unbind_marked_interfaces(udev);
  1273. }
  1274. /* Avoid PM error messages for devices disconnected while suspended
  1275. * as we'll display regular disconnect messages just a bit later.
  1276. */
  1277. if (status == -ENODEV || status == -ESHUTDOWN)
  1278. status = 0;
  1279. return status;
  1280. }
  1281. #endif /* CONFIG_PM */
  1282. #ifdef CONFIG_PM_RUNTIME
  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. udev->do_remote_wakeup = w;
  1566. return 0;
  1567. }
  1568. int usb_runtime_suspend(struct device *dev)
  1569. {
  1570. struct usb_device *udev = to_usb_device(dev);
  1571. int status;
  1572. /* A USB device can be suspended if it passes the various autosuspend
  1573. * checks. Runtime suspend for a USB device means suspending all the
  1574. * interfaces and then the device itself.
  1575. */
  1576. if (autosuspend_check(udev) != 0)
  1577. return -EAGAIN;
  1578. status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
  1579. /* Allow a retry if autosuspend failed temporarily */
  1580. if (status == -EAGAIN || status == -EBUSY)
  1581. usb_mark_last_busy(udev);
  1582. /*
  1583. * The PM core reacts badly unless the return code is 0,
  1584. * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
  1585. * (except for root hubs, because they don't suspend through
  1586. * an upstream port like other USB devices).
  1587. */
  1588. if (status != 0 && udev->parent)
  1589. return -EBUSY;
  1590. return status;
  1591. }
  1592. int usb_runtime_resume(struct device *dev)
  1593. {
  1594. struct usb_device *udev = to_usb_device(dev);
  1595. int status;
  1596. /* Runtime resume for a USB device means resuming both the device
  1597. * and all its interfaces.
  1598. */
  1599. status = usb_resume_both(udev, PMSG_AUTO_RESUME);
  1600. return status;
  1601. }
  1602. int usb_runtime_idle(struct device *dev)
  1603. {
  1604. struct usb_device *udev = to_usb_device(dev);
  1605. /* An idle USB device can be suspended if it passes the various
  1606. * autosuspend checks.
  1607. */
  1608. if (autosuspend_check(udev) == 0)
  1609. pm_runtime_autosuspend(dev);
  1610. /* Tell the core not to suspend it, though. */
  1611. return -EBUSY;
  1612. }
  1613. int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
  1614. {
  1615. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1616. int ret = -EPERM;
  1617. if (enable && !udev->usb2_hw_lpm_allowed)
  1618. return 0;
  1619. if (hcd->driver->set_usb2_hw_lpm) {
  1620. ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
  1621. if (!ret)
  1622. udev->usb2_hw_lpm_enabled = enable;
  1623. }
  1624. return ret;
  1625. }
  1626. #endif /* CONFIG_PM_RUNTIME */
  1627. struct bus_type usb_bus_type = {
  1628. .name = "usb",
  1629. .match = usb_device_match,
  1630. .uevent = usb_uevent,
  1631. };