driver.c 53 KB

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