driver.c 50 KB

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