driver.c 54 KB

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