driver.c 50 KB

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