driver.c 47 KB

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