driver.c 49 KB

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