driver.c 53 KB

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