driver.c 52 KB

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