driver.c 55 KB

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