driver.c 54 KB

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