driver.c 52 KB

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