driver.c 55 KB

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