driver.c 47 KB

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