driver.c 40 KB

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