driver.c 52 KB

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