driver.c 52 KB

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