driver.c 55 KB

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