driver.c 43 KB

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