driver.c 52 KB

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