driver.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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, 1);
  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, 1);
  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, 1);
  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, 1);
  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. {
  638. int retval = 0;
  639. if (usb_disabled())
  640. return -ENODEV;
  641. new_driver->drvwrap.for_devices = 0;
  642. new_driver->drvwrap.driver.name = (char *) new_driver->name;
  643. new_driver->drvwrap.driver.bus = &usb_bus_type;
  644. new_driver->drvwrap.driver.probe = usb_probe_interface;
  645. new_driver->drvwrap.driver.remove = usb_unbind_interface;
  646. new_driver->drvwrap.driver.owner = owner;
  647. spin_lock_init(&new_driver->dynids.lock);
  648. INIT_LIST_HEAD(&new_driver->dynids.list);
  649. retval = driver_register(&new_driver->drvwrap.driver);
  650. if (!retval) {
  651. pr_info("%s: registered new interface driver %s\n",
  652. usbcore_name, new_driver->name);
  653. usbfs_update_special();
  654. usb_create_newid_file(new_driver);
  655. } else {
  656. printk(KERN_ERR "%s: error %d registering interface "
  657. " driver %s\n",
  658. usbcore_name, retval, new_driver->name);
  659. }
  660. return retval;
  661. }
  662. EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
  663. /**
  664. * usb_deregister - unregister a USB interface driver
  665. * @driver: USB operations of the interface driver to unregister
  666. * Context: must be able to sleep
  667. *
  668. * Unlinks the specified driver from the internal USB driver list.
  669. *
  670. * NOTE: If you called usb_register_dev(), you still need to call
  671. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  672. * this * call will no longer do it for you.
  673. */
  674. void usb_deregister(struct usb_driver *driver)
  675. {
  676. pr_info("%s: deregistering interface driver %s\n",
  677. usbcore_name, driver->name);
  678. usb_remove_newid_file(driver);
  679. usb_free_dynids(driver);
  680. driver_unregister(&driver->drvwrap.driver);
  681. usbfs_update_special();
  682. }
  683. EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);
  684. #ifdef CONFIG_PM
  685. /* Caller has locked udev's pm_mutex */
  686. static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
  687. {
  688. struct usb_device_driver *udriver;
  689. int status = 0;
  690. if (udev->state == USB_STATE_NOTATTACHED ||
  691. udev->state == USB_STATE_SUSPENDED)
  692. goto done;
  693. /* For devices that don't have a driver, we do a standard suspend. */
  694. if (udev->dev.driver == NULL) {
  695. udev->do_remote_wakeup = 0;
  696. status = usb_port_suspend(udev);
  697. goto done;
  698. }
  699. udriver = to_usb_device_driver(udev->dev.driver);
  700. status = udriver->suspend(udev, msg);
  701. done:
  702. // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  703. if (status == 0)
  704. udev->dev.power.power_state.event = msg.event;
  705. return status;
  706. }
  707. /* Caller has locked udev's pm_mutex */
  708. static int usb_resume_device(struct usb_device *udev)
  709. {
  710. struct usb_device_driver *udriver;
  711. int status = 0;
  712. if (udev->state == USB_STATE_NOTATTACHED ||
  713. udev->state != USB_STATE_SUSPENDED)
  714. goto done;
  715. /* Can't resume it if it doesn't have a driver. */
  716. if (udev->dev.driver == NULL) {
  717. status = -ENOTCONN;
  718. goto done;
  719. }
  720. udriver = to_usb_device_driver(udev->dev.driver);
  721. status = udriver->resume(udev);
  722. done:
  723. // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  724. if (status == 0)
  725. udev->dev.power.power_state.event = PM_EVENT_ON;
  726. return status;
  727. }
  728. /* Caller has locked intf's usb_device's pm mutex */
  729. static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg)
  730. {
  731. struct usb_driver *driver;
  732. int status = 0;
  733. /* with no hardware, USB interfaces only use FREEZE and ON states */
  734. if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
  735. !is_active(intf))
  736. goto done;
  737. if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */
  738. goto done;
  739. driver = to_usb_driver(intf->dev.driver);
  740. if (driver->suspend && driver->resume) {
  741. status = driver->suspend(intf, msg);
  742. if (status == 0)
  743. mark_quiesced(intf);
  744. else if (!interface_to_usbdev(intf)->auto_pm)
  745. dev_err(&intf->dev, "%s error %d\n",
  746. "suspend", status);
  747. } else {
  748. // FIXME else if there's no suspend method, disconnect...
  749. // Not possible if auto_pm is set...
  750. dev_warn(&intf->dev, "no suspend for driver %s?\n",
  751. driver->name);
  752. mark_quiesced(intf);
  753. }
  754. done:
  755. // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
  756. if (status == 0)
  757. intf->dev.power.power_state.event = msg.event;
  758. return status;
  759. }
  760. /* Caller has locked intf's usb_device's pm_mutex */
  761. static int usb_resume_interface(struct usb_interface *intf)
  762. {
  763. struct usb_driver *driver;
  764. int status = 0;
  765. if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED ||
  766. is_active(intf))
  767. goto done;
  768. /* Don't let autoresume interfere with unbinding */
  769. if (intf->condition == USB_INTERFACE_UNBINDING)
  770. goto done;
  771. /* Can't resume it if it doesn't have a driver. */
  772. if (intf->condition == USB_INTERFACE_UNBOUND) {
  773. status = -ENOTCONN;
  774. goto done;
  775. }
  776. driver = to_usb_driver(intf->dev.driver);
  777. if (driver->resume) {
  778. status = driver->resume(intf);
  779. if (status)
  780. dev_err(&intf->dev, "%s error %d\n",
  781. "resume", status);
  782. else
  783. mark_active(intf);
  784. } else {
  785. dev_warn(&intf->dev, "no resume for driver %s?\n",
  786. driver->name);
  787. mark_active(intf);
  788. }
  789. done:
  790. // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status);
  791. if (status == 0)
  792. intf->dev.power.power_state.event = PM_EVENT_ON;
  793. return status;
  794. }
  795. /* Internal routine to check whether we may autosuspend a device. */
  796. static int autosuspend_check(struct usb_device *udev)
  797. {
  798. int i;
  799. struct usb_interface *intf;
  800. /* For autosuspend, fail fast if anything is in use.
  801. * Also fail if any interfaces require remote wakeup but it
  802. * isn't available. */
  803. udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
  804. if (udev->pm_usage_cnt > 0)
  805. return -EBUSY;
  806. if (udev->actconfig) {
  807. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  808. intf = udev->actconfig->interface[i];
  809. if (!is_active(intf))
  810. continue;
  811. if (intf->pm_usage_cnt > 0)
  812. return -EBUSY;
  813. if (intf->needs_remote_wakeup &&
  814. !udev->do_remote_wakeup) {
  815. dev_dbg(&udev->dev, "remote wakeup needed "
  816. "for autosuspend\n");
  817. return -EOPNOTSUPP;
  818. }
  819. }
  820. }
  821. return 0;
  822. }
  823. /**
  824. * usb_suspend_both - suspend a USB device and its interfaces
  825. * @udev: the usb_device to suspend
  826. * @msg: Power Management message describing this state transition
  827. *
  828. * This is the central routine for suspending USB devices. It calls the
  829. * suspend methods for all the interface drivers in @udev and then calls
  830. * the suspend method for @udev itself. If an error occurs at any stage,
  831. * all the interfaces which were suspended are resumed so that they remain
  832. * in the same state as the device.
  833. *
  834. * If an autosuspend is in progress (@udev->auto_pm is set), the routine
  835. * checks first to make sure that neither the device itself or any of its
  836. * active interfaces is in use (pm_usage_cnt is greater than 0). If they
  837. * are, the autosuspend fails.
  838. *
  839. * If the suspend succeeds, the routine recursively queues an autosuspend
  840. * request for @udev's parent device, thereby propagating the change up
  841. * the device tree. If all of the parent's children are now suspended,
  842. * the parent will autosuspend in turn.
  843. *
  844. * The suspend method calls are subject to mutual exclusion under control
  845. * of @udev's pm_mutex. Many of these calls are also under the protection
  846. * of @udev's device lock (including all requests originating outside the
  847. * USB subsystem), but autosuspend requests generated by a child device or
  848. * interface driver may not be. Usbcore will insure that the method calls
  849. * do not arrive during bind, unbind, or reset operations. However, drivers
  850. * must be prepared to handle suspend calls arriving at unpredictable times.
  851. * The only way to block such calls is to do an autoresume (preventing
  852. * autosuspends) while holding @udev's device lock (preventing outside
  853. * suspends).
  854. *
  855. * The caller must hold @udev->pm_mutex.
  856. *
  857. * This routine can run only in process context.
  858. */
  859. int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
  860. {
  861. int status = 0;
  862. int i = 0;
  863. struct usb_interface *intf;
  864. struct usb_device *parent = udev->parent;
  865. cancel_delayed_work(&udev->autosuspend);
  866. if (udev->state == USB_STATE_NOTATTACHED)
  867. return 0;
  868. if (udev->state == USB_STATE_SUSPENDED)
  869. return 0;
  870. udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
  871. if (udev->auto_pm) {
  872. status = autosuspend_check(udev);
  873. if (status < 0)
  874. return status;
  875. }
  876. /* Suspend all the interfaces and then udev itself */
  877. if (udev->actconfig) {
  878. for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
  879. intf = udev->actconfig->interface[i];
  880. status = usb_suspend_interface(intf, msg);
  881. if (status != 0)
  882. break;
  883. }
  884. }
  885. if (status == 0)
  886. status = usb_suspend_device(udev, msg);
  887. /* If the suspend failed, resume interfaces that did get suspended */
  888. if (status != 0) {
  889. while (--i >= 0) {
  890. intf = udev->actconfig->interface[i];
  891. usb_resume_interface(intf);
  892. }
  893. /* If the suspend succeeded, propagate it up the tree */
  894. } else if (parent)
  895. usb_autosuspend_device(parent, 1);
  896. // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  897. return status;
  898. }
  899. /**
  900. * usb_resume_both - resume a USB device and its interfaces
  901. * @udev: the usb_device to resume
  902. *
  903. * This is the central routine for resuming USB devices. It calls the
  904. * the resume method for @udev and then calls the resume methods for all
  905. * the interface drivers in @udev.
  906. *
  907. * Before starting the resume, the routine calls itself recursively for
  908. * the parent device of @udev, thereby propagating the change up the device
  909. * tree and assuring that @udev will be able to resume. If the parent is
  910. * unable to resume successfully, the routine fails.
  911. *
  912. * The resume method calls are subject to mutual exclusion under control
  913. * of @udev's pm_mutex. Many of these calls are also under the protection
  914. * of @udev's device lock (including all requests originating outside the
  915. * USB subsystem), but autoresume requests generated by a child device or
  916. * interface driver may not be. Usbcore will insure that the method calls
  917. * do not arrive during bind, unbind, or reset operations. However, drivers
  918. * must be prepared to handle resume calls arriving at unpredictable times.
  919. * The only way to block such calls is to do an autoresume (preventing
  920. * other autoresumes) while holding @udev's device lock (preventing outside
  921. * resumes).
  922. *
  923. * The caller must hold @udev->pm_mutex.
  924. *
  925. * This routine can run only in process context.
  926. */
  927. int usb_resume_both(struct usb_device *udev)
  928. {
  929. int status = 0;
  930. int i;
  931. struct usb_interface *intf;
  932. struct usb_device *parent = udev->parent;
  933. cancel_delayed_work(&udev->autosuspend);
  934. if (udev->state == USB_STATE_NOTATTACHED)
  935. return -ENODEV;
  936. /* Propagate the resume up the tree, if necessary */
  937. if (udev->state == USB_STATE_SUSPENDED) {
  938. if (parent) {
  939. status = usb_autoresume_device(parent, 1);
  940. if (status == 0) {
  941. status = usb_resume_device(udev);
  942. if (status) {
  943. usb_autosuspend_device(parent, 1);
  944. /* It's possible usb_resume_device()
  945. * failed after the port was
  946. * unsuspended, causing udev to be
  947. * logically disconnected. We don't
  948. * want usb_disconnect() to autosuspend
  949. * the parent again, so tell it that
  950. * udev disconnected while still
  951. * suspended. */
  952. if (udev->state ==
  953. USB_STATE_NOTATTACHED)
  954. udev->discon_suspended = 1;
  955. }
  956. }
  957. } else {
  958. /* We can't progagate beyond the USB subsystem,
  959. * so if a root hub's controller is suspended
  960. * then we're stuck. */
  961. if (udev->dev.parent->power.power_state.event !=
  962. PM_EVENT_ON)
  963. status = -EHOSTUNREACH;
  964. else
  965. status = usb_resume_device(udev);
  966. }
  967. } else {
  968. /* Needed only for setting udev->dev.power.power_state.event
  969. * and for possible debugging message. */
  970. status = usb_resume_device(udev);
  971. }
  972. if (status == 0 && udev->actconfig) {
  973. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  974. intf = udev->actconfig->interface[i];
  975. usb_resume_interface(intf);
  976. }
  977. }
  978. // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  979. return status;
  980. }
  981. #ifdef CONFIG_USB_SUSPEND
  982. /**
  983. * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
  984. * @udev: the usb_device to autosuspend
  985. * @dec_usage_cnt: flag to decrement @udev's PM-usage counter
  986. *
  987. * This routine should be called when a core subsystem is finished using
  988. * @udev and wants to allow it to autosuspend. Examples would be when
  989. * @udev's device file in usbfs is closed or after a configuration change.
  990. *
  991. * @dec_usage_cnt should be 1 if the subsystem previously incremented
  992. * @udev's usage counter (such as by passing 1 to usb_autoresume_device);
  993. * otherwise it should be 0.
  994. *
  995. * If the usage counter for @udev or any of its active interfaces is greater
  996. * than 0, the autosuspend request will not be queued. (If an interface
  997. * driver does not support autosuspend then its usage counter is permanently
  998. * positive.) Likewise, if an interface driver requires remote-wakeup
  999. * capability during autosuspend but remote wakeup is disabled, the
  1000. * autosuspend will fail.
  1001. *
  1002. * Often the caller will hold @udev's device lock, but this is not
  1003. * necessary.
  1004. *
  1005. * This routine can run only in process context.
  1006. */
  1007. void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt)
  1008. {
  1009. usb_pm_lock(udev);
  1010. udev->pm_usage_cnt -= dec_usage_cnt;
  1011. if (autosuspend_check(udev) == 0)
  1012. queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
  1013. USB_AUTOSUSPEND_DELAY);
  1014. usb_pm_unlock(udev);
  1015. // dev_dbg(&udev->dev, "%s: cnt %d\n",
  1016. // __FUNCTION__, udev->pm_usage_cnt);
  1017. }
  1018. /**
  1019. * usb_autoresume_device - immediately autoresume a USB device and its interfaces
  1020. * @udev: the usb_device to autoresume
  1021. * @inc_usage_cnt: flag to increment @udev's PM-usage counter
  1022. *
  1023. * This routine should be called when a core subsystem wants to use @udev
  1024. * and needs to guarantee that it is not suspended. In addition, the
  1025. * caller can prevent @udev from being autosuspended subsequently. (Note
  1026. * that this will not prevent suspend events originating in the PM core.)
  1027. * Examples would be when @udev's device file in usbfs is opened (autosuspend
  1028. * should be prevented until the file is closed) or when a remote-wakeup
  1029. * request is received (later autosuspends should not be prevented).
  1030. *
  1031. * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent
  1032. * autosuspends. This prevention will persist until the usage counter is
  1033. * decremented again (such as by passing 1 to usb_autosuspend_device).
  1034. * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged.
  1035. * Regardless, if the autoresume fails then the usage counter is not
  1036. * incremented.
  1037. *
  1038. * Often the caller will hold @udev's device lock, but this is not
  1039. * necessary (and attempting it might cause deadlock).
  1040. *
  1041. * This routine can run only in process context.
  1042. */
  1043. int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt)
  1044. {
  1045. int status;
  1046. usb_pm_lock(udev);
  1047. udev->pm_usage_cnt += inc_usage_cnt;
  1048. udev->auto_pm = 1;
  1049. status = usb_resume_both(udev);
  1050. if (status != 0)
  1051. udev->pm_usage_cnt -= inc_usage_cnt;
  1052. usb_pm_unlock(udev);
  1053. // dev_dbg(&udev->dev, "%s: status %d cnt %d\n",
  1054. // __FUNCTION__, status, udev->pm_usage_cnt);
  1055. return status;
  1056. }
  1057. /* Internal routine to adjust an interface's usage counter and change
  1058. * its device's autosuspend state.
  1059. */
  1060. static int usb_autopm_do_interface(struct usb_interface *intf,
  1061. int inc_usage_cnt)
  1062. {
  1063. struct usb_device *udev = interface_to_usbdev(intf);
  1064. int status = 0;
  1065. usb_pm_lock(udev);
  1066. if (intf->condition == USB_INTERFACE_UNBOUND)
  1067. status = -ENODEV;
  1068. else {
  1069. intf->pm_usage_cnt += inc_usage_cnt;
  1070. if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
  1071. udev->auto_pm = 1;
  1072. status = usb_resume_both(udev);
  1073. if (status != 0)
  1074. intf->pm_usage_cnt -= inc_usage_cnt;
  1075. } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0)
  1076. queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
  1077. USB_AUTOSUSPEND_DELAY);
  1078. }
  1079. usb_pm_unlock(udev);
  1080. return status;
  1081. }
  1082. /**
  1083. * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
  1084. * @intf: the usb_interface whose counter should be decremented
  1085. *
  1086. * This routine should be called by an interface driver when it is
  1087. * finished using @intf and wants to allow it to autosuspend. A typical
  1088. * example would be a character-device driver when its device file is
  1089. * closed.
  1090. *
  1091. * The routine decrements @intf's usage counter. When the counter reaches
  1092. * 0, a delayed autosuspend request for @intf's device is queued. When
  1093. * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
  1094. * the other usage counters for the sibling interfaces and @intf's
  1095. * usb_device, the device and all its interfaces will be autosuspended.
  1096. *
  1097. * Note that @intf->pm_usage_cnt is owned by the interface driver. The
  1098. * core will not change its value other than the increment and decrement
  1099. * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
  1100. * may use this simple counter-oriented discipline or may set the value
  1101. * any way it likes.
  1102. *
  1103. * If the driver has set @intf->needs_remote_wakeup then autosuspend will
  1104. * take place only if the device's remote-wakeup facility is enabled.
  1105. *
  1106. * Suspend method calls queued by this routine can arrive at any time
  1107. * while @intf is resumed and its usage counter is equal to 0. They are
  1108. * not protected by the usb_device's lock but only by its pm_mutex.
  1109. * Drivers must provide their own synchronization.
  1110. *
  1111. * This routine can run only in process context.
  1112. */
  1113. void usb_autopm_put_interface(struct usb_interface *intf)
  1114. {
  1115. int status;
  1116. status = usb_autopm_do_interface(intf, -1);
  1117. // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
  1118. // __FUNCTION__, status, intf->pm_usage_cnt);
  1119. }
  1120. EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
  1121. /**
  1122. * usb_autopm_get_interface - increment a USB interface's PM-usage counter
  1123. * @intf: the usb_interface whose counter should be incremented
  1124. *
  1125. * This routine should be called by an interface driver when it wants to
  1126. * use @intf and needs to guarantee that it is not suspended. In addition,
  1127. * the routine prevents @intf from being autosuspended subsequently. (Note
  1128. * that this will not prevent suspend events originating in the PM core.)
  1129. * This prevention will persist until usb_autopm_put_interface() is called
  1130. * or @intf is unbound. A typical example would be a character-device
  1131. * driver when its device file is opened.
  1132. *
  1133. * The routine increments @intf's usage counter. So long as the counter
  1134. * is greater than 0, autosuspend will not be allowed for @intf or its
  1135. * usb_device. When the driver is finished using @intf it should call
  1136. * usb_autopm_put_interface() to decrement the usage counter and queue
  1137. * a delayed autosuspend request (if the counter is <= 0).
  1138. *
  1139. * Note that @intf->pm_usage_cnt is owned by the interface driver. The
  1140. * core will not change its value other than the increment and decrement
  1141. * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
  1142. * may use this simple counter-oriented discipline or may set the value
  1143. * any way it likes.
  1144. *
  1145. * Resume method calls generated by this routine can arrive at any time
  1146. * while @intf is suspended. They are not protected by the usb_device's
  1147. * lock but only by its pm_mutex. Drivers must provide their own
  1148. * synchronization.
  1149. *
  1150. * This routine can run only in process context.
  1151. */
  1152. int usb_autopm_get_interface(struct usb_interface *intf)
  1153. {
  1154. int status;
  1155. status = usb_autopm_do_interface(intf, 1);
  1156. // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
  1157. // __FUNCTION__, status, intf->pm_usage_cnt);
  1158. return status;
  1159. }
  1160. EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
  1161. /**
  1162. * usb_autopm_set_interface - set a USB interface's autosuspend state
  1163. * @intf: the usb_interface whose state should be set
  1164. *
  1165. * This routine sets the autosuspend state of @intf's device according
  1166. * to @intf's usage counter, which the caller must have set previously.
  1167. * If the counter is <= 0, the device is autosuspended (if it isn't
  1168. * already suspended and if nothing else prevents the autosuspend). If
  1169. * the counter is > 0, the device is autoresumed (if it isn't already
  1170. * awake).
  1171. */
  1172. int usb_autopm_set_interface(struct usb_interface *intf)
  1173. {
  1174. int status;
  1175. status = usb_autopm_do_interface(intf, 0);
  1176. // dev_dbg(&intf->dev, "%s: status %d cnt %d\n",
  1177. // __FUNCTION__, status, intf->pm_usage_cnt);
  1178. return status;
  1179. }
  1180. EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
  1181. #endif /* CONFIG_USB_SUSPEND */
  1182. static int usb_suspend(struct device *dev, pm_message_t message)
  1183. {
  1184. int status;
  1185. if (is_usb_device(dev)) {
  1186. struct usb_device *udev = to_usb_device(dev);
  1187. usb_pm_lock(udev);
  1188. udev->auto_pm = 0;
  1189. status = usb_suspend_both(udev, message);
  1190. usb_pm_unlock(udev);
  1191. } else
  1192. status = 0;
  1193. return status;
  1194. }
  1195. static int usb_resume(struct device *dev)
  1196. {
  1197. int status;
  1198. if (is_usb_device(dev)) {
  1199. struct usb_device *udev = to_usb_device(dev);
  1200. usb_pm_lock(udev);
  1201. udev->auto_pm = 0;
  1202. status = usb_resume_both(udev);
  1203. usb_pm_unlock(udev);
  1204. /* Rebind drivers that had no suspend method? */
  1205. } else
  1206. status = 0;
  1207. return status;
  1208. }
  1209. #endif /* CONFIG_PM */
  1210. struct bus_type usb_bus_type = {
  1211. .name = "usb",
  1212. .match = usb_device_match,
  1213. .uevent = usb_uevent,
  1214. #ifdef CONFIG_PM
  1215. .suspend = usb_suspend,
  1216. .resume = usb_resume,
  1217. #endif
  1218. };