driver.c 43 KB

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