driver.c 53 KB

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