driver.c 50 KB

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