driver.c 47 KB

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