driver.c 54 KB

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