driver.c 45 KB

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