driver.c 52 KB

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