driver.c 54 KB

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