driver.c 51 KB

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