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