driver.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  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/slab.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/quirks.h>
  28. #include <linux/usb/hcd.h>
  29. #include <linux/pm_runtime.h>
  30. #include "usb.h"
  31. #ifdef CONFIG_HOTPLUG
  32. /*
  33. * Adds a new dynamic USBdevice ID to this driver,
  34. * and cause the driver to probe for all devices again.
  35. */
  36. ssize_t usb_store_new_id(struct usb_dynids *dynids,
  37. struct device_driver *driver,
  38. const char *buf, size_t count)
  39. {
  40. struct usb_dynid *dynid;
  41. u32 idVendor = 0;
  42. u32 idProduct = 0;
  43. int fields = 0;
  44. int retval = 0;
  45. fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
  46. if (fields < 2)
  47. return -EINVAL;
  48. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  49. if (!dynid)
  50. return -ENOMEM;
  51. INIT_LIST_HEAD(&dynid->node);
  52. dynid->id.idVendor = idVendor;
  53. dynid->id.idProduct = idProduct;
  54. dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  55. spin_lock(&dynids->lock);
  56. list_add_tail(&dynid->node, &dynids->list);
  57. spin_unlock(&dynids->lock);
  58. if (get_driver(driver)) {
  59. retval = driver_attach(driver);
  60. put_driver(driver);
  61. }
  62. if (retval)
  63. return retval;
  64. return count;
  65. }
  66. EXPORT_SYMBOL_GPL(usb_store_new_id);
  67. static ssize_t store_new_id(struct device_driver *driver,
  68. const char *buf, size_t count)
  69. {
  70. struct usb_driver *usb_drv = to_usb_driver(driver);
  71. return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
  72. }
  73. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  74. /**
  75. * store_remove_id - remove a USB device ID from this driver
  76. * @driver: target device driver
  77. * @buf: buffer for scanning device ID data
  78. * @count: input size
  79. *
  80. * Removes a dynamic usb device ID from this driver.
  81. */
  82. static ssize_t
  83. store_remove_id(struct device_driver *driver, const char *buf, size_t count)
  84. {
  85. struct usb_dynid *dynid, *n;
  86. struct usb_driver *usb_driver = to_usb_driver(driver);
  87. u32 idVendor = 0;
  88. u32 idProduct = 0;
  89. int fields = 0;
  90. int retval = 0;
  91. fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
  92. if (fields < 2)
  93. return -EINVAL;
  94. spin_lock(&usb_driver->dynids.lock);
  95. list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
  96. struct usb_device_id *id = &dynid->id;
  97. if ((id->idVendor == idVendor) &&
  98. (id->idProduct == idProduct)) {
  99. list_del(&dynid->node);
  100. kfree(dynid);
  101. retval = 0;
  102. break;
  103. }
  104. }
  105. spin_unlock(&usb_driver->dynids.lock);
  106. if (retval)
  107. return retval;
  108. return count;
  109. }
  110. static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
  111. static int usb_create_newid_file(struct usb_driver *usb_drv)
  112. {
  113. int error = 0;
  114. if (usb_drv->no_dynamic_id)
  115. goto exit;
  116. if (usb_drv->probe != NULL)
  117. error = driver_create_file(&usb_drv->drvwrap.driver,
  118. &driver_attr_new_id);
  119. exit:
  120. return error;
  121. }
  122. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  123. {
  124. if (usb_drv->no_dynamic_id)
  125. return;
  126. if (usb_drv->probe != NULL)
  127. driver_remove_file(&usb_drv->drvwrap.driver,
  128. &driver_attr_new_id);
  129. }
  130. static int
  131. usb_create_removeid_file(struct usb_driver *drv)
  132. {
  133. int error = 0;
  134. if (drv->probe != NULL)
  135. error = driver_create_file(&drv->drvwrap.driver,
  136. &driver_attr_remove_id);
  137. return error;
  138. }
  139. static void usb_remove_removeid_file(struct usb_driver *drv)
  140. {
  141. driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
  142. }
  143. static void usb_free_dynids(struct usb_driver *usb_drv)
  144. {
  145. struct usb_dynid *dynid, *n;
  146. spin_lock(&usb_drv->dynids.lock);
  147. list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
  148. list_del(&dynid->node);
  149. kfree(dynid);
  150. }
  151. spin_unlock(&usb_drv->dynids.lock);
  152. }
  153. #else
  154. static inline int usb_create_newid_file(struct usb_driver *usb_drv)
  155. {
  156. return 0;
  157. }
  158. static void usb_remove_newid_file(struct usb_driver *usb_drv)
  159. {
  160. }
  161. static int
  162. usb_create_removeid_file(struct usb_driver *drv)
  163. {
  164. return 0;
  165. }
  166. static void usb_remove_removeid_file(struct usb_driver *drv)
  167. {
  168. }
  169. static inline void usb_free_dynids(struct usb_driver *usb_drv)
  170. {
  171. }
  172. #endif
  173. static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
  174. struct usb_driver *drv)
  175. {
  176. struct usb_dynid *dynid;
  177. spin_lock(&drv->dynids.lock);
  178. list_for_each_entry(dynid, &drv->dynids.list, node) {
  179. if (usb_match_one_id(intf, &dynid->id)) {
  180. spin_unlock(&drv->dynids.lock);
  181. return &dynid->id;
  182. }
  183. }
  184. spin_unlock(&drv->dynids.lock);
  185. return NULL;
  186. }
  187. /* called from driver core with dev locked */
  188. static int usb_probe_device(struct device *dev)
  189. {
  190. struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  191. struct usb_device *udev = to_usb_device(dev);
  192. int error = 0;
  193. dev_dbg(dev, "%s\n", __func__);
  194. /* TODO: Add real matching code */
  195. /* The device should always appear to be in use
  196. * unless the driver suports autosuspend.
  197. */
  198. if (!udriver->supports_autosuspend)
  199. error = usb_autoresume_device(udev);
  200. if (!error)
  201. error = udriver->probe(udev);
  202. return error;
  203. }
  204. /* called from driver core with dev locked */
  205. static int usb_unbind_device(struct device *dev)
  206. {
  207. struct usb_device *udev = to_usb_device(dev);
  208. struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
  209. udriver->disconnect(udev);
  210. if (!udriver->supports_autosuspend)
  211. usb_autosuspend_device(udev);
  212. return 0;
  213. }
  214. /*
  215. * Cancel any pending scheduled resets
  216. *
  217. * [see usb_queue_reset_device()]
  218. *
  219. * Called after unconfiguring / when releasing interfaces. See
  220. * comments in __usb_queue_reset_device() regarding
  221. * udev->reset_running.
  222. */
  223. static void usb_cancel_queued_reset(struct usb_interface *iface)
  224. {
  225. if (iface->reset_running == 0)
  226. cancel_work_sync(&iface->reset_ws);
  227. }
  228. /* called from driver core with dev locked */
  229. static int usb_probe_interface(struct device *dev)
  230. {
  231. struct usb_driver *driver = to_usb_driver(dev->driver);
  232. struct usb_interface *intf = to_usb_interface(dev);
  233. struct usb_device *udev = interface_to_usbdev(intf);
  234. const struct usb_device_id *id;
  235. int error = -ENODEV;
  236. dev_dbg(dev, "%s\n", __func__);
  237. intf->needs_binding = 0;
  238. if (usb_device_is_owned(udev))
  239. return error;
  240. if (udev->authorized == 0) {
  241. dev_err(&intf->dev, "Device is not authorized for usage\n");
  242. return error;
  243. }
  244. id = usb_match_id(intf, driver->id_table);
  245. if (!id)
  246. id = usb_match_dynamic_id(intf, driver);
  247. if (!id)
  248. return error;
  249. dev_dbg(dev, "%s - got id\n", __func__);
  250. error = usb_autoresume_device(udev);
  251. if (error)
  252. return error;
  253. intf->condition = USB_INTERFACE_BINDING;
  254. /* Probed interfaces are initially active. They are
  255. * runtime-PM-enabled only if the driver has autosuspend support.
  256. * They are sensitive to their children's power states.
  257. */
  258. pm_runtime_set_active(dev);
  259. pm_suspend_ignore_children(dev, false);
  260. if (driver->supports_autosuspend)
  261. pm_runtime_enable(dev);
  262. /* Carry out a deferred switch to altsetting 0 */
  263. if (intf->needs_altsetting0) {
  264. error = usb_set_interface(udev, intf->altsetting[0].
  265. desc.bInterfaceNumber, 0);
  266. if (error < 0)
  267. goto err;
  268. intf->needs_altsetting0 = 0;
  269. }
  270. error = driver->probe(intf, id);
  271. if (error)
  272. goto err;
  273. intf->condition = USB_INTERFACE_BOUND;
  274. usb_autosuspend_device(udev);
  275. return error;
  276. err:
  277. intf->needs_remote_wakeup = 0;
  278. intf->condition = USB_INTERFACE_UNBOUND;
  279. usb_cancel_queued_reset(intf);
  280. /* Unbound interfaces are always runtime-PM-disabled and -suspended */
  281. if (driver->supports_autosuspend)
  282. pm_runtime_disable(dev);
  283. pm_runtime_set_suspended(dev);
  284. usb_autosuspend_device(udev);
  285. return error;
  286. }
  287. /* called from driver core with dev locked */
  288. static int usb_unbind_interface(struct device *dev)
  289. {
  290. struct usb_driver *driver = to_usb_driver(dev->driver);
  291. struct usb_interface *intf = to_usb_interface(dev);
  292. struct usb_device *udev;
  293. int error, r;
  294. intf->condition = USB_INTERFACE_UNBINDING;
  295. /* Autoresume for set_interface call below */
  296. udev = interface_to_usbdev(intf);
  297. error = usb_autoresume_device(udev);
  298. /* Terminate all URBs for this interface unless the driver
  299. * supports "soft" unbinding.
  300. */
  301. if (!driver->soft_unbind)
  302. usb_disable_interface(udev, intf, false);
  303. driver->disconnect(intf);
  304. usb_cancel_queued_reset(intf);
  305. /* Reset other interface state.
  306. * We cannot do a Set-Interface if the device is suspended or
  307. * if it is prepared for a system sleep (since installing a new
  308. * altsetting means creating new endpoint device entries).
  309. * When either of these happens, defer the Set-Interface.
  310. */
  311. if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
  312. /* Already in altsetting 0 so skip Set-Interface.
  313. * Just re-enable it without affecting the endpoint toggles.
  314. */
  315. usb_enable_interface(udev, intf, false);
  316. } else if (!error && intf->dev.power.status == DPM_ON) {
  317. r = usb_set_interface(udev, intf->altsetting[0].
  318. desc.bInterfaceNumber, 0);
  319. if (r < 0)
  320. intf->needs_altsetting0 = 1;
  321. } else {
  322. intf->needs_altsetting0 = 1;
  323. }
  324. usb_set_intfdata(intf, NULL);
  325. intf->condition = USB_INTERFACE_UNBOUND;
  326. intf->needs_remote_wakeup = 0;
  327. /* Unbound interfaces are always runtime-PM-disabled and -suspended */
  328. if (driver->supports_autosuspend)
  329. pm_runtime_disable(dev);
  330. pm_runtime_set_suspended(dev);
  331. /* Undo any residual pm_autopm_get_interface_* calls */
  332. for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
  333. usb_autopm_put_interface_no_suspend(intf);
  334. atomic_set(&intf->pm_usage_cnt, 0);
  335. if (!error)
  336. usb_autosuspend_device(udev);
  337. return 0;
  338. }
  339. /**
  340. * usb_driver_claim_interface - bind a driver to an interface
  341. * @driver: the driver to be bound
  342. * @iface: the interface to which it will be bound; must be in the
  343. * usb device's active configuration
  344. * @priv: driver data associated with that interface
  345. *
  346. * This is used by usb device drivers that need to claim more than one
  347. * interface on a device when probing (audio and acm are current examples).
  348. * No device driver should directly modify internal usb_interface or
  349. * usb_device structure members.
  350. *
  351. * Few drivers should need to use this routine, since the most natural
  352. * way to bind to an interface is to return the private data from
  353. * the driver's probe() method.
  354. *
  355. * Callers must own the device lock, so driver probe() entries don't need
  356. * extra locking, but other call contexts may need to explicitly claim that
  357. * lock.
  358. */
  359. int usb_driver_claim_interface(struct usb_driver *driver,
  360. struct usb_interface *iface, void *priv)
  361. {
  362. struct device *dev = &iface->dev;
  363. int retval = 0;
  364. if (dev->driver)
  365. return -EBUSY;
  366. dev->driver = &driver->drvwrap.driver;
  367. usb_set_intfdata(iface, priv);
  368. iface->needs_binding = 0;
  369. iface->condition = USB_INTERFACE_BOUND;
  370. /* Claimed interfaces are initially inactive (suspended) and
  371. * runtime-PM-enabled, but only if the driver has autosuspend
  372. * support. Otherwise they are marked active, to prevent the
  373. * device from being autosuspended, but left disabled. In either
  374. * case they are sensitive to their children's power states.
  375. */
  376. pm_suspend_ignore_children(dev, false);
  377. if (driver->supports_autosuspend)
  378. pm_runtime_enable(dev);
  379. else
  380. pm_runtime_set_active(dev);
  381. /* if interface was already added, bind now; else let
  382. * the future device_add() bind it, bypassing probe()
  383. */
  384. if (device_is_registered(dev))
  385. retval = device_bind_driver(dev);
  386. return retval;
  387. }
  388. EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
  389. /**
  390. * usb_driver_release_interface - unbind a driver from an interface
  391. * @driver: the driver to be unbound
  392. * @iface: the interface from which it will be unbound
  393. *
  394. * This can be used by drivers to release an interface without waiting
  395. * for their disconnect() methods to be called. In typical cases this
  396. * also causes the driver disconnect() method to be called.
  397. *
  398. * This call is synchronous, and may not be used in an interrupt context.
  399. * Callers must own the device lock, so driver disconnect() entries don't
  400. * need extra locking, but other call contexts may need to explicitly claim
  401. * that lock.
  402. */
  403. void usb_driver_release_interface(struct usb_driver *driver,
  404. struct usb_interface *iface)
  405. {
  406. struct device *dev = &iface->dev;
  407. /* this should never happen, don't release something that's not ours */
  408. if (!dev->driver || dev->driver != &driver->drvwrap.driver)
  409. return;
  410. /* don't release from within disconnect() */
  411. if (iface->condition != USB_INTERFACE_BOUND)
  412. return;
  413. iface->condition = USB_INTERFACE_UNBINDING;
  414. /* Release via the driver core only if the interface
  415. * has already been registered
  416. */
  417. if (device_is_registered(dev)) {
  418. device_release_driver(dev);
  419. } else {
  420. device_lock(dev);
  421. usb_unbind_interface(dev);
  422. dev->driver = NULL;
  423. device_unlock(dev);
  424. }
  425. }
  426. EXPORT_SYMBOL_GPL(usb_driver_release_interface);
  427. /* returns 0 if no match, 1 if match */
  428. int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
  429. {
  430. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  431. id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  432. return 0;
  433. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  434. id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  435. return 0;
  436. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  437. greater than any unsigned number. */
  438. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  439. (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  440. return 0;
  441. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  442. (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  443. return 0;
  444. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  445. (id->bDeviceClass != dev->descriptor.bDeviceClass))
  446. return 0;
  447. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  448. (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  449. return 0;
  450. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  451. (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  452. return 0;
  453. return 1;
  454. }
  455. /* returns 0 if no match, 1 if match */
  456. int usb_match_one_id(struct usb_interface *interface,
  457. const struct usb_device_id *id)
  458. {
  459. struct usb_host_interface *intf;
  460. struct usb_device *dev;
  461. /* proc_connectinfo in devio.c may call us with id == NULL. */
  462. if (id == NULL)
  463. return 0;
  464. intf = interface->cur_altsetting;
  465. dev = interface_to_usbdev(interface);
  466. if (!usb_match_device(dev, id))
  467. return 0;
  468. /* The interface class, subclass, and protocol should never be
  469. * checked for a match if the device class is Vendor Specific,
  470. * unless the match record specifies the Vendor ID. */
  471. if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
  472. !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  473. (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
  474. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  475. USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
  476. return 0;
  477. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  478. (id->bInterfaceClass != intf->desc.bInterfaceClass))
  479. return 0;
  480. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  481. (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
  482. return 0;
  483. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  484. (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
  485. return 0;
  486. return 1;
  487. }
  488. EXPORT_SYMBOL_GPL(usb_match_one_id);
  489. /**
  490. * usb_match_id - find first usb_device_id matching device or interface
  491. * @interface: the interface of interest
  492. * @id: array of usb_device_id structures, terminated by zero entry
  493. *
  494. * usb_match_id searches an array of usb_device_id's and returns
  495. * the first one matching the device or interface, or null.
  496. * This is used when binding (or rebinding) a driver to an interface.
  497. * Most USB device drivers will use this indirectly, through the usb core,
  498. * but some layered driver frameworks use it directly.
  499. * These device tables are exported with MODULE_DEVICE_TABLE, through
  500. * modutils, to support the driver loading functionality of USB hotplugging.
  501. *
  502. * What Matches:
  503. *
  504. * The "match_flags" element in a usb_device_id controls which
  505. * members are used. If the corresponding bit is set, the
  506. * value in the device_id must match its corresponding member
  507. * in the device or interface descriptor, or else the device_id
  508. * does not match.
  509. *
  510. * "driver_info" is normally used only by device drivers,
  511. * but you can create a wildcard "matches anything" usb_device_id
  512. * as a driver's "modules.usbmap" entry if you provide an id with
  513. * only a nonzero "driver_info" field. If you do this, the USB device
  514. * driver's probe() routine should use additional intelligence to
  515. * decide whether to bind to the specified interface.
  516. *
  517. * What Makes Good usb_device_id Tables:
  518. *
  519. * The match algorithm is very simple, so that intelligence in
  520. * driver selection must come from smart driver id records.
  521. * Unless you have good reasons to use another selection policy,
  522. * provide match elements only in related groups, and order match
  523. * specifiers from specific to general. Use the macros provided
  524. * for that purpose if you can.
  525. *
  526. * The most specific match specifiers use device descriptor
  527. * data. These are commonly used with product-specific matches;
  528. * the USB_DEVICE macro lets you provide vendor and product IDs,
  529. * and you can also match against ranges of product revisions.
  530. * These are widely used for devices with application or vendor
  531. * specific bDeviceClass values.
  532. *
  533. * Matches based on device class/subclass/protocol specifications
  534. * are slightly more general; use the USB_DEVICE_INFO macro, or
  535. * its siblings. These are used with single-function devices
  536. * where bDeviceClass doesn't specify that each interface has
  537. * its own class.
  538. *
  539. * Matches based on interface class/subclass/protocol are the
  540. * most general; they let drivers bind to any interface on a
  541. * multiple-function device. Use the USB_INTERFACE_INFO
  542. * macro, or its siblings, to match class-per-interface style
  543. * devices (as recorded in bInterfaceClass).
  544. *
  545. * Note that an entry created by USB_INTERFACE_INFO won't match
  546. * any interface if the device class is set to Vendor-Specific.
  547. * This is deliberate; according to the USB spec the meanings of
  548. * the interface class/subclass/protocol for these devices are also
  549. * vendor-specific, and hence matching against a standard product
  550. * class wouldn't work anyway. If you really want to use an
  551. * interface-based match for such a device, create a match record
  552. * that also specifies the vendor ID. (Unforunately there isn't a
  553. * standard macro for creating records like this.)
  554. *
  555. * Within those groups, remember that not all combinations are
  556. * meaningful. For example, don't give a product version range
  557. * without vendor and product IDs; or specify a protocol without
  558. * its associated class and subclass.
  559. */
  560. const struct usb_device_id *usb_match_id(struct usb_interface *interface,
  561. const struct usb_device_id *id)
  562. {
  563. /* proc_connectinfo in devio.c may call us with id == NULL. */
  564. if (id == NULL)
  565. return NULL;
  566. /* It is important to check that id->driver_info is nonzero,
  567. since an entry that is all zeroes except for a nonzero
  568. id->driver_info is the way to create an entry that
  569. indicates that the driver want to examine every
  570. device and interface. */
  571. for (; id->idVendor || id->idProduct || id->bDeviceClass ||
  572. id->bInterfaceClass || id->driver_info; id++) {
  573. if (usb_match_one_id(interface, id))
  574. return id;
  575. }
  576. return NULL;
  577. }
  578. EXPORT_SYMBOL_GPL(usb_match_id);
  579. static int usb_device_match(struct device *dev, struct device_driver *drv)
  580. {
  581. /* devices and interfaces are handled separately */
  582. if (is_usb_device(dev)) {
  583. /* interface drivers never match devices */
  584. if (!is_usb_device_driver(drv))
  585. return 0;
  586. /* TODO: Add real matching code */
  587. return 1;
  588. } else if (is_usb_interface(dev)) {
  589. struct usb_interface *intf;
  590. struct usb_driver *usb_drv;
  591. const struct usb_device_id *id;
  592. /* device drivers never match interfaces */
  593. if (is_usb_device_driver(drv))
  594. return 0;
  595. intf = to_usb_interface(dev);
  596. usb_drv = to_usb_driver(drv);
  597. id = usb_match_id(intf, usb_drv->id_table);
  598. if (id)
  599. return 1;
  600. id = usb_match_dynamic_id(intf, usb_drv);
  601. if (id)
  602. return 1;
  603. }
  604. return 0;
  605. }
  606. #ifdef CONFIG_HOTPLUG
  607. static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
  608. {
  609. struct usb_device *usb_dev;
  610. if (is_usb_device(dev)) {
  611. usb_dev = to_usb_device(dev);
  612. } else if (is_usb_interface(dev)) {
  613. struct usb_interface *intf = to_usb_interface(dev);
  614. usb_dev = interface_to_usbdev(intf);
  615. } else {
  616. return 0;
  617. }
  618. if (usb_dev->devnum < 0) {
  619. /* driver is often null here; dev_dbg() would oops */
  620. pr_debug("usb %s: already deleted?\n", dev_name(dev));
  621. return -ENODEV;
  622. }
  623. if (!usb_dev->bus) {
  624. pr_debug("usb %s: bus removed?\n", dev_name(dev));
  625. return -ENODEV;
  626. }
  627. #ifdef CONFIG_USB_DEVICEFS
  628. /* If this is available, userspace programs can directly read
  629. * all the device descriptors we don't tell them about. Or
  630. * act as usermode drivers.
  631. */
  632. if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
  633. usb_dev->bus->busnum, usb_dev->devnum))
  634. return -ENOMEM;
  635. #endif
  636. /* per-device configurations are common */
  637. if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
  638. le16_to_cpu(usb_dev->descriptor.idVendor),
  639. le16_to_cpu(usb_dev->descriptor.idProduct),
  640. le16_to_cpu(usb_dev->descriptor.bcdDevice)))
  641. return -ENOMEM;
  642. /* class-based driver binding models */
  643. if (add_uevent_var(env, "TYPE=%d/%d/%d",
  644. usb_dev->descriptor.bDeviceClass,
  645. usb_dev->descriptor.bDeviceSubClass,
  646. usb_dev->descriptor.bDeviceProtocol))
  647. return -ENOMEM;
  648. return 0;
  649. }
  650. #else
  651. static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
  652. {
  653. return -ENODEV;
  654. }
  655. #endif /* CONFIG_HOTPLUG */
  656. /**
  657. * usb_register_device_driver - register a USB device (not interface) driver
  658. * @new_udriver: USB operations for the device driver
  659. * @owner: module owner of this driver.
  660. *
  661. * Registers a USB device driver with the USB core. The list of
  662. * unattached devices will be rescanned whenever a new driver is
  663. * added, allowing the new driver to attach to any recognized devices.
  664. * Returns a negative error code on failure and 0 on success.
  665. */
  666. int usb_register_device_driver(struct usb_device_driver *new_udriver,
  667. struct module *owner)
  668. {
  669. int retval = 0;
  670. if (usb_disabled())
  671. return -ENODEV;
  672. new_udriver->drvwrap.for_devices = 1;
  673. new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
  674. new_udriver->drvwrap.driver.bus = &usb_bus_type;
  675. new_udriver->drvwrap.driver.probe = usb_probe_device;
  676. new_udriver->drvwrap.driver.remove = usb_unbind_device;
  677. new_udriver->drvwrap.driver.owner = owner;
  678. retval = driver_register(&new_udriver->drvwrap.driver);
  679. if (!retval) {
  680. pr_info("%s: registered new device driver %s\n",
  681. usbcore_name, new_udriver->name);
  682. usbfs_update_special();
  683. } else {
  684. printk(KERN_ERR "%s: error %d registering device "
  685. " driver %s\n",
  686. usbcore_name, retval, new_udriver->name);
  687. }
  688. return retval;
  689. }
  690. EXPORT_SYMBOL_GPL(usb_register_device_driver);
  691. /**
  692. * usb_deregister_device_driver - unregister a USB device (not interface) driver
  693. * @udriver: USB operations of the device driver to unregister
  694. * Context: must be able to sleep
  695. *
  696. * Unlinks the specified driver from the internal USB driver list.
  697. */
  698. void usb_deregister_device_driver(struct usb_device_driver *udriver)
  699. {
  700. pr_info("%s: deregistering device driver %s\n",
  701. usbcore_name, udriver->name);
  702. driver_unregister(&udriver->drvwrap.driver);
  703. usbfs_update_special();
  704. }
  705. EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
  706. /**
  707. * usb_register_driver - register a USB interface driver
  708. * @new_driver: USB operations for the interface driver
  709. * @owner: module owner of this driver.
  710. * @mod_name: module name string
  711. *
  712. * Registers a USB interface driver with the USB core. The list of
  713. * unattached interfaces will be rescanned whenever a new driver is
  714. * added, allowing the new driver to attach to any recognized interfaces.
  715. * Returns a negative error code on failure and 0 on success.
  716. *
  717. * NOTE: if you want your driver to use the USB major number, you must call
  718. * usb_register_dev() to enable that functionality. This function no longer
  719. * takes care of that.
  720. */
  721. int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
  722. const char *mod_name)
  723. {
  724. int retval = 0;
  725. if (usb_disabled())
  726. return -ENODEV;
  727. new_driver->drvwrap.for_devices = 0;
  728. new_driver->drvwrap.driver.name = (char *) new_driver->name;
  729. new_driver->drvwrap.driver.bus = &usb_bus_type;
  730. new_driver->drvwrap.driver.probe = usb_probe_interface;
  731. new_driver->drvwrap.driver.remove = usb_unbind_interface;
  732. new_driver->drvwrap.driver.owner = owner;
  733. new_driver->drvwrap.driver.mod_name = mod_name;
  734. spin_lock_init(&new_driver->dynids.lock);
  735. INIT_LIST_HEAD(&new_driver->dynids.list);
  736. retval = driver_register(&new_driver->drvwrap.driver);
  737. if (retval)
  738. goto out;
  739. usbfs_update_special();
  740. retval = usb_create_newid_file(new_driver);
  741. if (retval)
  742. goto out_newid;
  743. retval = usb_create_removeid_file(new_driver);
  744. if (retval)
  745. goto out_removeid;
  746. pr_info("%s: registered new interface driver %s\n",
  747. usbcore_name, new_driver->name);
  748. out:
  749. return retval;
  750. out_removeid:
  751. usb_remove_newid_file(new_driver);
  752. out_newid:
  753. driver_unregister(&new_driver->drvwrap.driver);
  754. printk(KERN_ERR "%s: error %d registering interface "
  755. " driver %s\n",
  756. usbcore_name, retval, new_driver->name);
  757. goto out;
  758. }
  759. EXPORT_SYMBOL_GPL(usb_register_driver);
  760. /**
  761. * usb_deregister - unregister a USB interface driver
  762. * @driver: USB operations of the interface driver to unregister
  763. * Context: must be able to sleep
  764. *
  765. * Unlinks the specified driver from the internal USB driver list.
  766. *
  767. * NOTE: If you called usb_register_dev(), you still need to call
  768. * usb_deregister_dev() to clean up your driver's allocated minor numbers,
  769. * this * call will no longer do it for you.
  770. */
  771. void usb_deregister(struct usb_driver *driver)
  772. {
  773. pr_info("%s: deregistering interface driver %s\n",
  774. usbcore_name, driver->name);
  775. usb_remove_removeid_file(driver);
  776. usb_remove_newid_file(driver);
  777. usb_free_dynids(driver);
  778. driver_unregister(&driver->drvwrap.driver);
  779. usbfs_update_special();
  780. }
  781. EXPORT_SYMBOL_GPL(usb_deregister);
  782. /* Forced unbinding of a USB interface driver, either because
  783. * it doesn't support pre_reset/post_reset/reset_resume or
  784. * because it doesn't support suspend/resume.
  785. *
  786. * The caller must hold @intf's device's lock, but not its pm_mutex
  787. * and not @intf->dev.sem.
  788. */
  789. void usb_forced_unbind_intf(struct usb_interface *intf)
  790. {
  791. struct usb_driver *driver = to_usb_driver(intf->dev.driver);
  792. dev_dbg(&intf->dev, "forced unbind\n");
  793. usb_driver_release_interface(driver, intf);
  794. /* Mark the interface for later rebinding */
  795. intf->needs_binding = 1;
  796. }
  797. /* Delayed forced unbinding of a USB interface driver and scan
  798. * for rebinding.
  799. *
  800. * The caller must hold @intf's device's lock, but not its pm_mutex
  801. * and not @intf->dev.sem.
  802. *
  803. * Note: Rebinds will be skipped if a system sleep transition is in
  804. * progress and the PM "complete" callback hasn't occurred yet.
  805. */
  806. void usb_rebind_intf(struct usb_interface *intf)
  807. {
  808. int rc;
  809. /* Delayed unbind of an existing driver */
  810. if (intf->dev.driver) {
  811. struct usb_driver *driver =
  812. to_usb_driver(intf->dev.driver);
  813. dev_dbg(&intf->dev, "forced unbind\n");
  814. usb_driver_release_interface(driver, intf);
  815. }
  816. /* Try to rebind the interface */
  817. if (intf->dev.power.status == DPM_ON) {
  818. intf->needs_binding = 0;
  819. rc = device_attach(&intf->dev);
  820. if (rc < 0)
  821. dev_warn(&intf->dev, "rebind failed: %d\n", rc);
  822. }
  823. }
  824. #ifdef CONFIG_PM
  825. #define DO_UNBIND 0
  826. #define DO_REBIND 1
  827. /* Unbind drivers for @udev's interfaces that don't support suspend/resume,
  828. * or rebind interfaces that have been unbound, according to @action.
  829. *
  830. * The caller must hold @udev's device lock.
  831. */
  832. static void do_unbind_rebind(struct usb_device *udev, int action)
  833. {
  834. struct usb_host_config *config;
  835. int i;
  836. struct usb_interface *intf;
  837. struct usb_driver *drv;
  838. config = udev->actconfig;
  839. if (config) {
  840. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  841. intf = config->interface[i];
  842. switch (action) {
  843. case DO_UNBIND:
  844. if (intf->dev.driver) {
  845. drv = to_usb_driver(intf->dev.driver);
  846. if (!drv->suspend || !drv->resume)
  847. usb_forced_unbind_intf(intf);
  848. }
  849. break;
  850. case DO_REBIND:
  851. if (intf->needs_binding)
  852. usb_rebind_intf(intf);
  853. break;
  854. }
  855. }
  856. }
  857. }
  858. static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
  859. {
  860. struct usb_device_driver *udriver;
  861. int status = 0;
  862. if (udev->state == USB_STATE_NOTATTACHED ||
  863. udev->state == USB_STATE_SUSPENDED)
  864. goto done;
  865. /* For devices that don't have a driver, we do a generic suspend. */
  866. if (udev->dev.driver)
  867. udriver = to_usb_device_driver(udev->dev.driver);
  868. else {
  869. udev->do_remote_wakeup = 0;
  870. udriver = &usb_generic_driver;
  871. }
  872. status = udriver->suspend(udev, msg);
  873. done:
  874. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  875. return status;
  876. }
  877. static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
  878. {
  879. struct usb_device_driver *udriver;
  880. int status = 0;
  881. if (udev->state == USB_STATE_NOTATTACHED)
  882. goto done;
  883. /* Can't resume it if it doesn't have a driver. */
  884. if (udev->dev.driver == NULL) {
  885. status = -ENOTCONN;
  886. goto done;
  887. }
  888. /* Non-root devices on a full/low-speed bus must wait for their
  889. * companion high-speed root hub, in case a handoff is needed.
  890. */
  891. if (!(msg.event & PM_EVENT_AUTO) && udev->parent &&
  892. udev->bus->hs_companion)
  893. device_pm_wait_for_dev(&udev->dev,
  894. &udev->bus->hs_companion->root_hub->dev);
  895. if (udev->quirks & USB_QUIRK_RESET_RESUME)
  896. udev->reset_resume = 1;
  897. udriver = to_usb_device_driver(udev->dev.driver);
  898. status = udriver->resume(udev, msg);
  899. done:
  900. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  901. return status;
  902. }
  903. static int usb_suspend_interface(struct usb_device *udev,
  904. struct usb_interface *intf, pm_message_t msg)
  905. {
  906. struct usb_driver *driver;
  907. int status = 0;
  908. if (udev->state == USB_STATE_NOTATTACHED ||
  909. intf->condition == USB_INTERFACE_UNBOUND)
  910. goto done;
  911. driver = to_usb_driver(intf->dev.driver);
  912. if (driver->suspend) {
  913. status = driver->suspend(intf, msg);
  914. if (status && !(msg.event & PM_EVENT_AUTO))
  915. dev_err(&intf->dev, "%s error %d\n",
  916. "suspend", status);
  917. } else {
  918. /* Later we will unbind the driver and reprobe */
  919. intf->needs_binding = 1;
  920. dev_warn(&intf->dev, "no %s for driver %s?\n",
  921. "suspend", driver->name);
  922. }
  923. done:
  924. dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
  925. return status;
  926. }
  927. static int usb_resume_interface(struct usb_device *udev,
  928. struct usb_interface *intf, pm_message_t msg, int reset_resume)
  929. {
  930. struct usb_driver *driver;
  931. int status = 0;
  932. if (udev->state == USB_STATE_NOTATTACHED)
  933. goto done;
  934. /* Don't let autoresume interfere with unbinding */
  935. if (intf->condition == USB_INTERFACE_UNBINDING)
  936. goto done;
  937. /* Can't resume it if it doesn't have a driver. */
  938. if (intf->condition == USB_INTERFACE_UNBOUND) {
  939. /* Carry out a deferred switch to altsetting 0 */
  940. if (intf->needs_altsetting0 &&
  941. intf->dev.power.status == DPM_ON) {
  942. usb_set_interface(udev, intf->altsetting[0].
  943. desc.bInterfaceNumber, 0);
  944. intf->needs_altsetting0 = 0;
  945. }
  946. goto done;
  947. }
  948. /* Don't resume if the interface is marked for rebinding */
  949. if (intf->needs_binding)
  950. goto done;
  951. driver = to_usb_driver(intf->dev.driver);
  952. if (reset_resume) {
  953. if (driver->reset_resume) {
  954. status = driver->reset_resume(intf);
  955. if (status)
  956. dev_err(&intf->dev, "%s error %d\n",
  957. "reset_resume", status);
  958. } else {
  959. intf->needs_binding = 1;
  960. dev_warn(&intf->dev, "no %s for driver %s?\n",
  961. "reset_resume", driver->name);
  962. }
  963. } else {
  964. if (driver->resume) {
  965. status = driver->resume(intf);
  966. if (status)
  967. dev_err(&intf->dev, "%s error %d\n",
  968. "resume", status);
  969. } else {
  970. intf->needs_binding = 1;
  971. dev_warn(&intf->dev, "no %s for driver %s?\n",
  972. "resume", driver->name);
  973. }
  974. }
  975. done:
  976. dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
  977. /* Later we will unbind the driver and/or reprobe, if necessary */
  978. return status;
  979. }
  980. /**
  981. * usb_suspend_both - suspend a USB device and its interfaces
  982. * @udev: the usb_device to suspend
  983. * @msg: Power Management message describing this state transition
  984. *
  985. * This is the central routine for suspending USB devices. It calls the
  986. * suspend methods for all the interface drivers in @udev and then calls
  987. * the suspend method for @udev itself. If an error occurs at any stage,
  988. * all the interfaces which were suspended are resumed so that they remain
  989. * in the same state as the device.
  990. *
  991. * Autosuspend requests originating from a child device or an interface
  992. * driver may be made without the protection of @udev's device lock, but
  993. * all other suspend calls will hold the lock. Usbcore will insure that
  994. * method calls do not arrive during bind, unbind, or reset operations.
  995. * However drivers must be prepared to handle suspend calls arriving at
  996. * unpredictable times.
  997. *
  998. * This routine can run only in process context.
  999. */
  1000. static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
  1001. {
  1002. int status = 0;
  1003. int i = 0, n = 0;
  1004. struct usb_interface *intf;
  1005. if (udev->state == USB_STATE_NOTATTACHED ||
  1006. udev->state == USB_STATE_SUSPENDED)
  1007. goto done;
  1008. /* Suspend all the interfaces and then udev itself */
  1009. if (udev->actconfig) {
  1010. n = udev->actconfig->desc.bNumInterfaces;
  1011. for (i = n - 1; i >= 0; --i) {
  1012. intf = udev->actconfig->interface[i];
  1013. status = usb_suspend_interface(udev, intf, msg);
  1014. if (status != 0)
  1015. break;
  1016. }
  1017. }
  1018. if (status == 0)
  1019. status = usb_suspend_device(udev, msg);
  1020. /* If the suspend failed, resume interfaces that did get suspended */
  1021. if (status != 0) {
  1022. msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
  1023. while (++i < n) {
  1024. intf = udev->actconfig->interface[i];
  1025. usb_resume_interface(udev, intf, msg, 0);
  1026. }
  1027. /* If the suspend succeeded then prevent any more URB submissions
  1028. * and flush any outstanding URBs.
  1029. */
  1030. } else {
  1031. udev->can_submit = 0;
  1032. for (i = 0; i < 16; ++i) {
  1033. usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
  1034. usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
  1035. }
  1036. }
  1037. done:
  1038. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1039. return status;
  1040. }
  1041. /**
  1042. * usb_resume_both - resume a USB device and its interfaces
  1043. * @udev: the usb_device to resume
  1044. * @msg: Power Management message describing this state transition
  1045. *
  1046. * This is the central routine for resuming USB devices. It calls the
  1047. * the resume method for @udev and then calls the resume methods for all
  1048. * the interface drivers in @udev.
  1049. *
  1050. * Autoresume requests originating from a child device or an interface
  1051. * driver may be made without the protection of @udev's device lock, but
  1052. * all other resume calls will hold the lock. Usbcore will insure that
  1053. * method calls do not arrive during bind, unbind, or reset operations.
  1054. * However drivers must be prepared to handle resume calls arriving at
  1055. * unpredictable times.
  1056. *
  1057. * This routine can run only in process context.
  1058. */
  1059. static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
  1060. {
  1061. int status = 0;
  1062. int i;
  1063. struct usb_interface *intf;
  1064. if (udev->state == USB_STATE_NOTATTACHED) {
  1065. status = -ENODEV;
  1066. goto done;
  1067. }
  1068. udev->can_submit = 1;
  1069. /* Resume the device */
  1070. if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
  1071. status = usb_resume_device(udev, msg);
  1072. /* Resume the interfaces */
  1073. if (status == 0 && udev->actconfig) {
  1074. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1075. intf = udev->actconfig->interface[i];
  1076. usb_resume_interface(udev, intf, msg,
  1077. udev->reset_resume);
  1078. }
  1079. }
  1080. done:
  1081. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1082. if (!status)
  1083. udev->reset_resume = 0;
  1084. return status;
  1085. }
  1086. static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
  1087. {
  1088. int w, i;
  1089. struct usb_interface *intf;
  1090. /* Remote wakeup is needed only when we actually go to sleep.
  1091. * For things like FREEZE and QUIESCE, if the device is already
  1092. * autosuspended then its current wakeup setting is okay.
  1093. */
  1094. if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
  1095. if (udev->state != USB_STATE_SUSPENDED)
  1096. udev->do_remote_wakeup = 0;
  1097. return;
  1098. }
  1099. /* If remote wakeup is permitted, see whether any interface drivers
  1100. * actually want it.
  1101. */
  1102. w = 0;
  1103. if (device_may_wakeup(&udev->dev) && udev->actconfig) {
  1104. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1105. intf = udev->actconfig->interface[i];
  1106. w |= intf->needs_remote_wakeup;
  1107. }
  1108. }
  1109. /* If the device is autosuspended with the wrong wakeup setting,
  1110. * autoresume now so the setting can be changed.
  1111. */
  1112. if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
  1113. pm_runtime_resume(&udev->dev);
  1114. udev->do_remote_wakeup = w;
  1115. }
  1116. /* The device lock is held by the PM core */
  1117. int usb_suspend(struct device *dev, pm_message_t msg)
  1118. {
  1119. struct usb_device *udev = to_usb_device(dev);
  1120. do_unbind_rebind(udev, DO_UNBIND);
  1121. choose_wakeup(udev, msg);
  1122. return usb_suspend_both(udev, msg);
  1123. }
  1124. /* The device lock is held by the PM core */
  1125. int usb_resume(struct device *dev, pm_message_t msg)
  1126. {
  1127. struct usb_device *udev = to_usb_device(dev);
  1128. int status;
  1129. /* For PM complete calls, all we do is rebind interfaces */
  1130. if (msg.event == PM_EVENT_ON) {
  1131. if (udev->state != USB_STATE_NOTATTACHED)
  1132. do_unbind_rebind(udev, DO_REBIND);
  1133. status = 0;
  1134. /* For all other calls, take the device back to full power and
  1135. * tell the PM core in case it was autosuspended previously.
  1136. */
  1137. } else {
  1138. status = usb_resume_both(udev, msg);
  1139. if (status == 0) {
  1140. pm_runtime_disable(dev);
  1141. pm_runtime_set_active(dev);
  1142. pm_runtime_enable(dev);
  1143. udev->last_busy = jiffies;
  1144. }
  1145. }
  1146. /* Avoid PM error messages for devices disconnected while suspended
  1147. * as we'll display regular disconnect messages just a bit later.
  1148. */
  1149. if (status == -ENODEV)
  1150. status = 0;
  1151. return status;
  1152. }
  1153. #endif /* CONFIG_PM */
  1154. #ifdef CONFIG_USB_SUSPEND
  1155. /**
  1156. * usb_enable_autosuspend - allow a USB device to be autosuspended
  1157. * @udev: the USB device which may be autosuspended
  1158. *
  1159. * This routine allows @udev to be autosuspended. An autosuspend won't
  1160. * take place until the autosuspend_delay has elapsed and all the other
  1161. * necessary conditions are satisfied.
  1162. *
  1163. * The caller must hold @udev's device lock.
  1164. */
  1165. void usb_enable_autosuspend(struct usb_device *udev)
  1166. {
  1167. pm_runtime_allow(&udev->dev);
  1168. }
  1169. EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
  1170. /**
  1171. * usb_disable_autosuspend - prevent a USB device from being autosuspended
  1172. * @udev: the USB device which may not be autosuspended
  1173. *
  1174. * This routine prevents @udev from being autosuspended and wakes it up
  1175. * if it is already autosuspended.
  1176. *
  1177. * The caller must hold @udev's device lock.
  1178. */
  1179. void usb_disable_autosuspend(struct usb_device *udev)
  1180. {
  1181. pm_runtime_forbid(&udev->dev);
  1182. }
  1183. EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
  1184. /**
  1185. * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
  1186. * @udev: the usb_device to autosuspend
  1187. *
  1188. * This routine should be called when a core subsystem is finished using
  1189. * @udev and wants to allow it to autosuspend. Examples would be when
  1190. * @udev's device file in usbfs is closed or after a configuration change.
  1191. *
  1192. * @udev's usage counter is decremented; if it drops to 0 and all the
  1193. * interfaces are inactive then a delayed autosuspend will be attempted.
  1194. * The attempt may fail (see autosuspend_check()).
  1195. *
  1196. * The caller must hold @udev's device lock.
  1197. *
  1198. * This routine can run only in process context.
  1199. */
  1200. void usb_autosuspend_device(struct usb_device *udev)
  1201. {
  1202. int status;
  1203. udev->last_busy = jiffies;
  1204. status = pm_runtime_put_sync(&udev->dev);
  1205. dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
  1206. __func__, atomic_read(&udev->dev.power.usage_count),
  1207. status);
  1208. }
  1209. /**
  1210. * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
  1211. * @udev: the usb_device to autosuspend
  1212. *
  1213. * This routine should be called when a core subsystem thinks @udev may
  1214. * be ready to autosuspend.
  1215. *
  1216. * @udev's usage counter left unchanged. If it is 0 and all the interfaces
  1217. * are inactive then an autosuspend will be attempted. The attempt may
  1218. * fail or be delayed.
  1219. *
  1220. * The caller must hold @udev's device lock.
  1221. *
  1222. * This routine can run only in process context.
  1223. */
  1224. void usb_try_autosuspend_device(struct usb_device *udev)
  1225. {
  1226. int status;
  1227. status = pm_runtime_idle(&udev->dev);
  1228. dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
  1229. __func__, atomic_read(&udev->dev.power.usage_count),
  1230. status);
  1231. }
  1232. /**
  1233. * usb_autoresume_device - immediately autoresume a USB device and its interfaces
  1234. * @udev: the usb_device to autoresume
  1235. *
  1236. * This routine should be called when a core subsystem wants to use @udev
  1237. * and needs to guarantee that it is not suspended. No autosuspend will
  1238. * occur until usb_autosuspend_device() is called. (Note that this will
  1239. * not prevent suspend events originating in the PM core.) Examples would
  1240. * be when @udev's device file in usbfs is opened or when a remote-wakeup
  1241. * request is received.
  1242. *
  1243. * @udev's usage counter is incremented to prevent subsequent autosuspends.
  1244. * However if the autoresume fails then the usage counter is re-decremented.
  1245. *
  1246. * The caller must hold @udev's device lock.
  1247. *
  1248. * This routine can run only in process context.
  1249. */
  1250. int usb_autoresume_device(struct usb_device *udev)
  1251. {
  1252. int status;
  1253. status = pm_runtime_get_sync(&udev->dev);
  1254. if (status < 0)
  1255. pm_runtime_put_sync(&udev->dev);
  1256. dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
  1257. __func__, atomic_read(&udev->dev.power.usage_count),
  1258. status);
  1259. if (status > 0)
  1260. status = 0;
  1261. return status;
  1262. }
  1263. /**
  1264. * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
  1265. * @intf: the usb_interface whose counter should be decremented
  1266. *
  1267. * This routine should be called by an interface driver when it is
  1268. * finished using @intf and wants to allow it to autosuspend. A typical
  1269. * example would be a character-device driver when its device file is
  1270. * closed.
  1271. *
  1272. * The routine decrements @intf's usage counter. When the counter reaches
  1273. * 0, a delayed autosuspend request for @intf's device is attempted. The
  1274. * attempt may fail (see autosuspend_check()).
  1275. *
  1276. * This routine can run only in process context.
  1277. */
  1278. void usb_autopm_put_interface(struct usb_interface *intf)
  1279. {
  1280. struct usb_device *udev = interface_to_usbdev(intf);
  1281. int status;
  1282. udev->last_busy = jiffies;
  1283. atomic_dec(&intf->pm_usage_cnt);
  1284. status = pm_runtime_put_sync(&intf->dev);
  1285. dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
  1286. __func__, atomic_read(&intf->dev.power.usage_count),
  1287. status);
  1288. }
  1289. EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
  1290. /**
  1291. * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
  1292. * @intf: the usb_interface whose counter should be decremented
  1293. *
  1294. * This routine does much the same thing as usb_autopm_put_interface():
  1295. * It decrements @intf's usage counter and schedules a delayed
  1296. * autosuspend request if the counter is <= 0. The difference is that it
  1297. * does not perform any synchronization; callers should hold a private
  1298. * lock and handle all synchronization issues themselves.
  1299. *
  1300. * Typically a driver would call this routine during an URB's completion
  1301. * handler, if no more URBs were pending.
  1302. *
  1303. * This routine can run in atomic context.
  1304. */
  1305. void usb_autopm_put_interface_async(struct usb_interface *intf)
  1306. {
  1307. struct usb_device *udev = interface_to_usbdev(intf);
  1308. unsigned long last_busy;
  1309. int status = 0;
  1310. last_busy = udev->last_busy;
  1311. udev->last_busy = jiffies;
  1312. atomic_dec(&intf->pm_usage_cnt);
  1313. pm_runtime_put_noidle(&intf->dev);
  1314. if (udev->dev.power.runtime_auto) {
  1315. /* Optimization: Don't schedule a delayed autosuspend if
  1316. * the timer is already running and the expiration time
  1317. * wouldn't change.
  1318. *
  1319. * We have to use the interface's timer. Attempts to
  1320. * schedule a suspend for the device would fail because
  1321. * the interface is still active.
  1322. */
  1323. if (intf->dev.power.timer_expires == 0 ||
  1324. round_jiffies_up(last_busy) !=
  1325. round_jiffies_up(jiffies)) {
  1326. status = pm_schedule_suspend(&intf->dev,
  1327. jiffies_to_msecs(
  1328. round_jiffies_up_relative(
  1329. udev->autosuspend_delay)));
  1330. }
  1331. }
  1332. dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
  1333. __func__, atomic_read(&intf->dev.power.usage_count),
  1334. status);
  1335. }
  1336. EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
  1337. /**
  1338. * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
  1339. * @intf: the usb_interface whose counter should be decremented
  1340. *
  1341. * This routine decrements @intf's usage counter but does not carry out an
  1342. * autosuspend.
  1343. *
  1344. * This routine can run in atomic context.
  1345. */
  1346. void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
  1347. {
  1348. struct usb_device *udev = interface_to_usbdev(intf);
  1349. udev->last_busy = jiffies;
  1350. atomic_dec(&intf->pm_usage_cnt);
  1351. pm_runtime_put_noidle(&intf->dev);
  1352. }
  1353. EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
  1354. /**
  1355. * usb_autopm_get_interface - increment a USB interface's PM-usage counter
  1356. * @intf: the usb_interface whose counter should be incremented
  1357. *
  1358. * This routine should be called by an interface driver when it wants to
  1359. * use @intf and needs to guarantee that it is not suspended. In addition,
  1360. * the routine prevents @intf from being autosuspended subsequently. (Note
  1361. * that this will not prevent suspend events originating in the PM core.)
  1362. * This prevention will persist until usb_autopm_put_interface() is called
  1363. * or @intf is unbound. A typical example would be a character-device
  1364. * driver when its device file is opened.
  1365. *
  1366. * @intf's usage counter is incremented to prevent subsequent autosuspends.
  1367. * However if the autoresume fails then the counter is re-decremented.
  1368. *
  1369. * This routine can run only in process context.
  1370. */
  1371. int usb_autopm_get_interface(struct usb_interface *intf)
  1372. {
  1373. int status;
  1374. status = pm_runtime_get_sync(&intf->dev);
  1375. if (status < 0)
  1376. pm_runtime_put_sync(&intf->dev);
  1377. else
  1378. atomic_inc(&intf->pm_usage_cnt);
  1379. dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
  1380. __func__, atomic_read(&intf->dev.power.usage_count),
  1381. status);
  1382. if (status > 0)
  1383. status = 0;
  1384. return status;
  1385. }
  1386. EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
  1387. /**
  1388. * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
  1389. * @intf: the usb_interface whose counter should be incremented
  1390. *
  1391. * This routine does much the same thing as
  1392. * usb_autopm_get_interface(): It increments @intf's usage counter and
  1393. * queues an autoresume request if the device is suspended. The
  1394. * differences are that it does not perform any synchronization (callers
  1395. * should hold a private lock and handle all synchronization issues
  1396. * themselves), and it does not autoresume the device directly (it only
  1397. * queues a request). After a successful call, the device may not yet be
  1398. * resumed.
  1399. *
  1400. * This routine can run in atomic context.
  1401. */
  1402. int usb_autopm_get_interface_async(struct usb_interface *intf)
  1403. {
  1404. int status = 0;
  1405. enum rpm_status s;
  1406. /* Don't request a resume unless the interface is already suspending
  1407. * or suspended. Doing so would force a running suspend timer to be
  1408. * cancelled.
  1409. */
  1410. pm_runtime_get_noresume(&intf->dev);
  1411. s = ACCESS_ONCE(intf->dev.power.runtime_status);
  1412. if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
  1413. status = pm_request_resume(&intf->dev);
  1414. if (status < 0 && status != -EINPROGRESS)
  1415. pm_runtime_put_noidle(&intf->dev);
  1416. else
  1417. atomic_inc(&intf->pm_usage_cnt);
  1418. dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
  1419. __func__, atomic_read(&intf->dev.power.usage_count),
  1420. status);
  1421. if (status > 0)
  1422. status = 0;
  1423. return status;
  1424. }
  1425. EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
  1426. /**
  1427. * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
  1428. * @intf: the usb_interface whose counter should be incremented
  1429. *
  1430. * This routine increments @intf's usage counter but does not carry out an
  1431. * autoresume.
  1432. *
  1433. * This routine can run in atomic context.
  1434. */
  1435. void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
  1436. {
  1437. struct usb_device *udev = interface_to_usbdev(intf);
  1438. udev->last_busy = jiffies;
  1439. atomic_inc(&intf->pm_usage_cnt);
  1440. pm_runtime_get_noresume(&intf->dev);
  1441. }
  1442. EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
  1443. /* Internal routine to check whether we may autosuspend a device. */
  1444. static int autosuspend_check(struct usb_device *udev)
  1445. {
  1446. int w, i;
  1447. struct usb_interface *intf;
  1448. unsigned long suspend_time, j;
  1449. /* Fail if autosuspend is disabled, or any interfaces are in use, or
  1450. * any interface drivers require remote wakeup but it isn't available.
  1451. */
  1452. w = 0;
  1453. if (udev->actconfig) {
  1454. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1455. intf = udev->actconfig->interface[i];
  1456. /* We don't need to check interfaces that are
  1457. * disabled for runtime PM. Either they are unbound
  1458. * or else their drivers don't support autosuspend
  1459. * and so they are permanently active.
  1460. */
  1461. if (intf->dev.power.disable_depth)
  1462. continue;
  1463. if (atomic_read(&intf->dev.power.usage_count) > 0)
  1464. return -EBUSY;
  1465. w |= intf->needs_remote_wakeup;
  1466. /* Don't allow autosuspend if the device will need
  1467. * a reset-resume and any of its interface drivers
  1468. * doesn't include support or needs remote wakeup.
  1469. */
  1470. if (udev->quirks & USB_QUIRK_RESET_RESUME) {
  1471. struct usb_driver *driver;
  1472. driver = to_usb_driver(intf->dev.driver);
  1473. if (!driver->reset_resume ||
  1474. intf->needs_remote_wakeup)
  1475. return -EOPNOTSUPP;
  1476. }
  1477. }
  1478. }
  1479. if (w && !device_can_wakeup(&udev->dev)) {
  1480. dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
  1481. return -EOPNOTSUPP;
  1482. }
  1483. udev->do_remote_wakeup = w;
  1484. /* If everything is okay but the device hasn't been idle for long
  1485. * enough, queue a delayed autosuspend request.
  1486. */
  1487. j = ACCESS_ONCE(jiffies);
  1488. suspend_time = udev->last_busy + udev->autosuspend_delay;
  1489. if (time_before(j, suspend_time)) {
  1490. pm_schedule_suspend(&udev->dev, jiffies_to_msecs(
  1491. round_jiffies_up_relative(suspend_time - j)));
  1492. return -EAGAIN;
  1493. }
  1494. return 0;
  1495. }
  1496. static int usb_runtime_suspend(struct device *dev)
  1497. {
  1498. int status = 0;
  1499. /* A USB device can be suspended if it passes the various autosuspend
  1500. * checks. Runtime suspend for a USB device means suspending all the
  1501. * interfaces and then the device itself.
  1502. */
  1503. if (is_usb_device(dev)) {
  1504. struct usb_device *udev = to_usb_device(dev);
  1505. if (autosuspend_check(udev) != 0)
  1506. return -EAGAIN;
  1507. status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
  1508. /* If an interface fails the suspend, adjust the last_busy
  1509. * time so that we don't get another suspend attempt right
  1510. * away.
  1511. */
  1512. if (status) {
  1513. udev->last_busy = jiffies +
  1514. (udev->autosuspend_delay == 0 ?
  1515. HZ/2 : 0);
  1516. }
  1517. /* Prevent the parent from suspending immediately after */
  1518. else if (udev->parent) {
  1519. udev->parent->last_busy = jiffies;
  1520. }
  1521. }
  1522. /* Runtime suspend for a USB interface doesn't mean anything. */
  1523. return status;
  1524. }
  1525. static int usb_runtime_resume(struct device *dev)
  1526. {
  1527. /* Runtime resume for a USB device means resuming both the device
  1528. * and all its interfaces.
  1529. */
  1530. if (is_usb_device(dev)) {
  1531. struct usb_device *udev = to_usb_device(dev);
  1532. int status;
  1533. status = usb_resume_both(udev, PMSG_AUTO_RESUME);
  1534. udev->last_busy = jiffies;
  1535. return status;
  1536. }
  1537. /* Runtime resume for a USB interface doesn't mean anything. */
  1538. return 0;
  1539. }
  1540. static int usb_runtime_idle(struct device *dev)
  1541. {
  1542. /* An idle USB device can be suspended if it passes the various
  1543. * autosuspend checks. An idle interface can be suspended at
  1544. * any time.
  1545. */
  1546. if (is_usb_device(dev)) {
  1547. struct usb_device *udev = to_usb_device(dev);
  1548. if (autosuspend_check(udev) != 0)
  1549. return 0;
  1550. }
  1551. pm_runtime_suspend(dev);
  1552. return 0;
  1553. }
  1554. static struct dev_pm_ops usb_bus_pm_ops = {
  1555. .runtime_suspend = usb_runtime_suspend,
  1556. .runtime_resume = usb_runtime_resume,
  1557. .runtime_idle = usb_runtime_idle,
  1558. };
  1559. #else
  1560. #define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
  1561. #endif /* CONFIG_USB_SUSPEND */
  1562. struct bus_type usb_bus_type = {
  1563. .name = "usb",
  1564. .match = usb_device_match,
  1565. .uevent = usb_uevent,
  1566. .pm = &usb_bus_pm_ops,
  1567. };