driver.c 53 KB

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