driver.c 59 KB

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