driver.c 40 KB

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