driver.c 44 KB

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