driver.c 58 KB

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