driver.c 49 KB

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