driver.c 53 KB

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