driver.c 46 KB

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