driver.c 55 KB

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