driver.c 50 KB

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