driver.c 42 KB

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