driver.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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. /* Non-root devices on a full/low-speed bus must wait for their
  871. * companion high-speed root hub, in case a handoff is needed.
  872. */
  873. if (!(msg.event & PM_EVENT_AUTO) && udev->parent &&
  874. udev->bus->hs_companion)
  875. device_pm_wait_for_dev(&udev->dev,
  876. &udev->bus->hs_companion->root_hub->dev);
  877. if (udev->quirks & USB_QUIRK_RESET_RESUME)
  878. udev->reset_resume = 1;
  879. udriver = to_usb_device_driver(udev->dev.driver);
  880. status = udriver->resume(udev, msg);
  881. done:
  882. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  883. return status;
  884. }
  885. /* Caller has locked intf's usb_device's pm mutex */
  886. static int usb_suspend_interface(struct usb_device *udev,
  887. struct usb_interface *intf, pm_message_t msg)
  888. {
  889. struct usb_driver *driver;
  890. int status = 0;
  891. /* with no hardware, USB interfaces only use FREEZE and ON states */
  892. if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf))
  893. goto done;
  894. /* This can happen; see usb_driver_release_interface() */
  895. if (intf->condition == USB_INTERFACE_UNBOUND)
  896. goto done;
  897. driver = to_usb_driver(intf->dev.driver);
  898. if (driver->suspend) {
  899. status = driver->suspend(intf, msg);
  900. if (status == 0)
  901. mark_quiesced(intf);
  902. else if (!(msg.event & PM_EVENT_AUTO))
  903. dev_err(&intf->dev, "%s error %d\n",
  904. "suspend", status);
  905. } else {
  906. /* Later we will unbind the driver and reprobe */
  907. intf->needs_binding = 1;
  908. dev_warn(&intf->dev, "no %s for driver %s?\n",
  909. "suspend", driver->name);
  910. mark_quiesced(intf);
  911. }
  912. done:
  913. dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
  914. return status;
  915. }
  916. /* Caller has locked intf's usb_device's pm_mutex */
  917. static int usb_resume_interface(struct usb_device *udev,
  918. struct usb_interface *intf, pm_message_t msg, int reset_resume)
  919. {
  920. struct usb_driver *driver;
  921. int status = 0;
  922. if (udev->state == USB_STATE_NOTATTACHED || is_active(intf))
  923. goto done;
  924. /* Don't let autoresume interfere with unbinding */
  925. if (intf->condition == USB_INTERFACE_UNBINDING)
  926. goto done;
  927. /* Can't resume it if it doesn't have a driver. */
  928. if (intf->condition == USB_INTERFACE_UNBOUND) {
  929. /* Carry out a deferred switch to altsetting 0 */
  930. if (intf->needs_altsetting0 &&
  931. intf->dev.power.status == DPM_ON) {
  932. usb_set_interface(udev, intf->altsetting[0].
  933. desc.bInterfaceNumber, 0);
  934. intf->needs_altsetting0 = 0;
  935. }
  936. goto done;
  937. }
  938. /* Don't resume if the interface is marked for rebinding */
  939. if (intf->needs_binding)
  940. goto done;
  941. driver = to_usb_driver(intf->dev.driver);
  942. if (reset_resume) {
  943. if (driver->reset_resume) {
  944. status = driver->reset_resume(intf);
  945. if (status)
  946. dev_err(&intf->dev, "%s error %d\n",
  947. "reset_resume", status);
  948. } else {
  949. intf->needs_binding = 1;
  950. dev_warn(&intf->dev, "no %s for driver %s?\n",
  951. "reset_resume", driver->name);
  952. }
  953. } else {
  954. if (driver->resume) {
  955. status = driver->resume(intf);
  956. if (status)
  957. dev_err(&intf->dev, "%s error %d\n",
  958. "resume", status);
  959. } else {
  960. intf->needs_binding = 1;
  961. dev_warn(&intf->dev, "no %s for driver %s?\n",
  962. "resume", driver->name);
  963. }
  964. }
  965. done:
  966. dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
  967. if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
  968. mark_active(intf);
  969. /* Later we will unbind the driver and/or reprobe, if necessary */
  970. return status;
  971. }
  972. #ifdef CONFIG_USB_SUSPEND
  973. /* Internal routine to check whether we may autosuspend a device. */
  974. static int autosuspend_check(struct usb_device *udev, int reschedule)
  975. {
  976. int i;
  977. struct usb_interface *intf;
  978. unsigned long suspend_time, j;
  979. /* For autosuspend, fail fast if anything is in use or autosuspend
  980. * is disabled. Also fail if any interfaces require remote wakeup
  981. * but it isn't available.
  982. */
  983. if (udev->pm_usage_cnt > 0)
  984. return -EBUSY;
  985. if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
  986. return -EPERM;
  987. suspend_time = udev->last_busy + udev->autosuspend_delay;
  988. if (udev->actconfig) {
  989. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  990. intf = udev->actconfig->interface[i];
  991. if (!is_active(intf))
  992. continue;
  993. if (atomic_read(&intf->pm_usage_cnt) > 0)
  994. return -EBUSY;
  995. if (intf->needs_remote_wakeup &&
  996. !udev->do_remote_wakeup) {
  997. dev_dbg(&udev->dev, "remote wakeup needed "
  998. "for autosuspend\n");
  999. return -EOPNOTSUPP;
  1000. }
  1001. /* Don't allow autosuspend if the device will need
  1002. * a reset-resume and any of its interface drivers
  1003. * doesn't include support.
  1004. */
  1005. if (udev->quirks & USB_QUIRK_RESET_RESUME) {
  1006. struct usb_driver *driver;
  1007. driver = to_usb_driver(intf->dev.driver);
  1008. if (!driver->reset_resume ||
  1009. intf->needs_remote_wakeup)
  1010. return -EOPNOTSUPP;
  1011. }
  1012. }
  1013. }
  1014. /* If everything is okay but the device hasn't been idle for long
  1015. * enough, queue a delayed autosuspend request. If the device
  1016. * _has_ been idle for long enough and the reschedule flag is set,
  1017. * likewise queue a delayed (1 second) autosuspend request.
  1018. */
  1019. j = jiffies;
  1020. if (time_before(j, suspend_time))
  1021. reschedule = 1;
  1022. else
  1023. suspend_time = j + HZ;
  1024. if (reschedule) {
  1025. if (!timer_pending(&udev->autosuspend.timer)) {
  1026. queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
  1027. round_jiffies_up_relative(suspend_time - j));
  1028. }
  1029. return -EAGAIN;
  1030. }
  1031. return 0;
  1032. }
  1033. #else
  1034. static inline int autosuspend_check(struct usb_device *udev, int reschedule)
  1035. {
  1036. return 0;
  1037. }
  1038. #endif /* CONFIG_USB_SUSPEND */
  1039. /**
  1040. * usb_suspend_both - suspend a USB device and its interfaces
  1041. * @udev: the usb_device to suspend
  1042. * @msg: Power Management message describing this state transition
  1043. *
  1044. * This is the central routine for suspending USB devices. It calls the
  1045. * suspend methods for all the interface drivers in @udev and then calls
  1046. * the suspend method for @udev itself. If an error occurs at any stage,
  1047. * all the interfaces which were suspended are resumed so that they remain
  1048. * in the same state as the device.
  1049. *
  1050. * If an autosuspend is in progress the routine checks first to make sure
  1051. * that neither the device itself or any of its active interfaces is in use
  1052. * (pm_usage_cnt is greater than 0). If they are, the autosuspend fails.
  1053. *
  1054. * If the suspend succeeds, the routine recursively queues an autosuspend
  1055. * request for @udev's parent device, thereby propagating the change up
  1056. * the device tree. If all of the parent's children are now suspended,
  1057. * the parent will autosuspend in turn.
  1058. *
  1059. * The suspend method calls are subject to mutual exclusion under control
  1060. * of @udev's pm_mutex. Many of these calls are also under the protection
  1061. * of @udev's device lock (including all requests originating outside the
  1062. * USB subsystem), but autosuspend requests generated by a child device or
  1063. * interface driver may not be. Usbcore will insure that the method calls
  1064. * do not arrive during bind, unbind, or reset operations. However, drivers
  1065. * must be prepared to handle suspend calls arriving at unpredictable times.
  1066. * The only way to block such calls is to do an autoresume (preventing
  1067. * autosuspends) while holding @udev's device lock (preventing outside
  1068. * suspends).
  1069. *
  1070. * The caller must hold @udev->pm_mutex.
  1071. *
  1072. * This routine can run only in process context.
  1073. */
  1074. static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
  1075. {
  1076. int status = 0;
  1077. int i = 0;
  1078. struct usb_interface *intf;
  1079. struct usb_device *parent = udev->parent;
  1080. if (udev->state == USB_STATE_NOTATTACHED ||
  1081. udev->state == USB_STATE_SUSPENDED)
  1082. goto done;
  1083. udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
  1084. if (msg.event & PM_EVENT_AUTO) {
  1085. status = autosuspend_check(udev, 0);
  1086. if (status < 0)
  1087. goto done;
  1088. }
  1089. /* Suspend all the interfaces and then udev itself */
  1090. if (udev->actconfig) {
  1091. for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1092. intf = udev->actconfig->interface[i];
  1093. status = usb_suspend_interface(udev, intf, msg);
  1094. if (status != 0)
  1095. break;
  1096. }
  1097. }
  1098. if (status == 0)
  1099. status = usb_suspend_device(udev, msg);
  1100. /* If the suspend failed, resume interfaces that did get suspended */
  1101. if (status != 0) {
  1102. pm_message_t msg2;
  1103. msg2.event = msg.event ^ (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
  1104. while (--i >= 0) {
  1105. intf = udev->actconfig->interface[i];
  1106. usb_resume_interface(udev, intf, msg2, 0);
  1107. }
  1108. /* Try another autosuspend when the interfaces aren't busy */
  1109. if (msg.event & PM_EVENT_AUTO)
  1110. autosuspend_check(udev, status == -EBUSY);
  1111. /* If the suspend succeeded then prevent any more URB submissions,
  1112. * flush any outstanding URBs, and propagate the suspend up the tree.
  1113. */
  1114. } else {
  1115. cancel_delayed_work(&udev->autosuspend);
  1116. udev->can_submit = 0;
  1117. for (i = 0; i < 16; ++i) {
  1118. usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
  1119. usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
  1120. }
  1121. /* If this is just a FREEZE or a PRETHAW, udev might
  1122. * not really be suspended. Only true suspends get
  1123. * propagated up the device tree.
  1124. */
  1125. if (parent && udev->state == USB_STATE_SUSPENDED)
  1126. usb_autosuspend_device(parent);
  1127. }
  1128. done:
  1129. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1130. return status;
  1131. }
  1132. /**
  1133. * usb_resume_both - resume a USB device and its interfaces
  1134. * @udev: the usb_device to resume
  1135. * @msg: Power Management message describing this state transition
  1136. *
  1137. * This is the central routine for resuming USB devices. It calls the
  1138. * the resume method for @udev and then calls the resume methods for all
  1139. * the interface drivers in @udev.
  1140. *
  1141. * Before starting the resume, the routine calls itself recursively for
  1142. * the parent device of @udev, thereby propagating the change up the device
  1143. * tree and assuring that @udev will be able to resume. If the parent is
  1144. * unable to resume successfully, the routine fails.
  1145. *
  1146. * The resume method calls are subject to mutual exclusion under control
  1147. * of @udev's pm_mutex. Many of these calls are also under the protection
  1148. * of @udev's device lock (including all requests originating outside the
  1149. * USB subsystem), but autoresume requests generated by a child device or
  1150. * interface driver may not be. Usbcore will insure that the method calls
  1151. * do not arrive during bind, unbind, or reset operations. However, drivers
  1152. * must be prepared to handle resume calls arriving at unpredictable times.
  1153. * The only way to block such calls is to do an autoresume (preventing
  1154. * other autoresumes) while holding @udev's device lock (preventing outside
  1155. * resumes).
  1156. *
  1157. * The caller must hold @udev->pm_mutex.
  1158. *
  1159. * This routine can run only in process context.
  1160. */
  1161. static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
  1162. {
  1163. int status = 0;
  1164. int i;
  1165. struct usb_interface *intf;
  1166. struct usb_device *parent = udev->parent;
  1167. cancel_delayed_work(&udev->autosuspend);
  1168. if (udev->state == USB_STATE_NOTATTACHED) {
  1169. status = -ENODEV;
  1170. goto done;
  1171. }
  1172. udev->can_submit = 1;
  1173. /* Propagate the resume up the tree, if necessary */
  1174. if (udev->state == USB_STATE_SUSPENDED) {
  1175. if (parent) {
  1176. status = usb_autoresume_device(parent);
  1177. if (status == 0) {
  1178. status = usb_resume_device(udev, msg);
  1179. if (status || udev->state ==
  1180. USB_STATE_NOTATTACHED) {
  1181. usb_autosuspend_device(parent);
  1182. /* It's possible usb_resume_device()
  1183. * failed after the port was
  1184. * unsuspended, causing udev to be
  1185. * logically disconnected. We don't
  1186. * want usb_disconnect() to autosuspend
  1187. * the parent again, so tell it that
  1188. * udev disconnected while still
  1189. * suspended. */
  1190. if (udev->state ==
  1191. USB_STATE_NOTATTACHED)
  1192. udev->discon_suspended = 1;
  1193. }
  1194. }
  1195. } else {
  1196. /* We can't progagate beyond the USB subsystem,
  1197. * so if a root hub's controller is suspended
  1198. * then we're stuck. */
  1199. status = usb_resume_device(udev, msg);
  1200. }
  1201. } else if (udev->reset_resume)
  1202. status = usb_resume_device(udev, msg);
  1203. if (status == 0 && udev->actconfig) {
  1204. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1205. intf = udev->actconfig->interface[i];
  1206. usb_resume_interface(udev, intf, msg,
  1207. udev->reset_resume);
  1208. }
  1209. }
  1210. done:
  1211. dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
  1212. if (!status)
  1213. udev->reset_resume = 0;
  1214. return status;
  1215. }
  1216. #ifdef CONFIG_USB_SUSPEND
  1217. /* Internal routine to adjust a device's usage counter and change
  1218. * its autosuspend state.
  1219. */
  1220. static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
  1221. {
  1222. int status = 0;
  1223. usb_pm_lock(udev);
  1224. udev->pm_usage_cnt += inc_usage_cnt;
  1225. WARN_ON(udev->pm_usage_cnt < 0);
  1226. if (inc_usage_cnt)
  1227. udev->last_busy = jiffies;
  1228. if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
  1229. if (udev->state == USB_STATE_SUSPENDED)
  1230. status = usb_resume_both(udev, PMSG_AUTO_RESUME);
  1231. if (status != 0)
  1232. udev->pm_usage_cnt -= inc_usage_cnt;
  1233. else if (inc_usage_cnt)
  1234. udev->last_busy = jiffies;
  1235. } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
  1236. status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
  1237. }
  1238. usb_pm_unlock(udev);
  1239. return status;
  1240. }
  1241. /* usb_autosuspend_work - callback routine to autosuspend a USB device */
  1242. void usb_autosuspend_work(struct work_struct *work)
  1243. {
  1244. struct usb_device *udev =
  1245. container_of(work, struct usb_device, autosuspend.work);
  1246. usb_autopm_do_device(udev, 0);
  1247. }
  1248. /* usb_autoresume_work - callback routine to autoresume a USB device */
  1249. void usb_autoresume_work(struct work_struct *work)
  1250. {
  1251. struct usb_device *udev =
  1252. container_of(work, struct usb_device, autoresume);
  1253. /* Wake it up, let the drivers do their thing, and then put it
  1254. * back to sleep.
  1255. */
  1256. if (usb_autopm_do_device(udev, 1) == 0)
  1257. usb_autopm_do_device(udev, -1);
  1258. }
  1259. /**
  1260. * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
  1261. * @udev: the usb_device to autosuspend
  1262. *
  1263. * This routine should be called when a core subsystem is finished using
  1264. * @udev and wants to allow it to autosuspend. Examples would be when
  1265. * @udev's device file in usbfs is closed or after a configuration change.
  1266. *
  1267. * @udev's usage counter is decremented. If it or any of the usage counters
  1268. * for an active interface is greater than 0, no autosuspend request will be
  1269. * queued. (If an interface driver does not support autosuspend then its
  1270. * usage counter is permanently positive.) Furthermore, if an interface
  1271. * driver requires remote-wakeup capability during autosuspend but remote
  1272. * wakeup is disabled, the autosuspend will fail.
  1273. *
  1274. * Often the caller will hold @udev's device lock, but this is not
  1275. * necessary.
  1276. *
  1277. * This routine can run only in process context.
  1278. */
  1279. void usb_autosuspend_device(struct usb_device *udev)
  1280. {
  1281. int status;
  1282. status = usb_autopm_do_device(udev, -1);
  1283. dev_vdbg(&udev->dev, "%s: cnt %d\n",
  1284. __func__, udev->pm_usage_cnt);
  1285. }
  1286. /**
  1287. * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
  1288. * @udev: the usb_device to autosuspend
  1289. *
  1290. * This routine should be called when a core subsystem thinks @udev may
  1291. * be ready to autosuspend.
  1292. *
  1293. * @udev's usage counter left unchanged. If it or any of the usage counters
  1294. * for an active interface is greater than 0, or autosuspend is not allowed
  1295. * for any other reason, no autosuspend request will be queued.
  1296. *
  1297. * This routine can run only in process context.
  1298. */
  1299. void usb_try_autosuspend_device(struct usb_device *udev)
  1300. {
  1301. usb_autopm_do_device(udev, 0);
  1302. dev_vdbg(&udev->dev, "%s: cnt %d\n",
  1303. __func__, udev->pm_usage_cnt);
  1304. }
  1305. /**
  1306. * usb_autoresume_device - immediately autoresume a USB device and its interfaces
  1307. * @udev: the usb_device to autoresume
  1308. *
  1309. * This routine should be called when a core subsystem wants to use @udev
  1310. * and needs to guarantee that it is not suspended. No autosuspend will
  1311. * occur until usb_autosuspend_device is called. (Note that this will not
  1312. * prevent suspend events originating in the PM core.) Examples would be
  1313. * when @udev's device file in usbfs is opened or when a remote-wakeup
  1314. * request is received.
  1315. *
  1316. * @udev's usage counter is incremented to prevent subsequent autosuspends.
  1317. * However if the autoresume fails then the usage counter is re-decremented.
  1318. *
  1319. * Often the caller will hold @udev's device lock, but this is not
  1320. * necessary (and attempting it might cause deadlock).
  1321. *
  1322. * This routine can run only in process context.
  1323. */
  1324. int usb_autoresume_device(struct usb_device *udev)
  1325. {
  1326. int status;
  1327. status = usb_autopm_do_device(udev, 1);
  1328. dev_vdbg(&udev->dev, "%s: status %d cnt %d\n",
  1329. __func__, status, udev->pm_usage_cnt);
  1330. return status;
  1331. }
  1332. /* Internal routine to adjust an interface's usage counter and change
  1333. * its device's autosuspend state.
  1334. */
  1335. static int usb_autopm_do_interface(struct usb_interface *intf,
  1336. int inc_usage_cnt)
  1337. {
  1338. struct usb_device *udev = interface_to_usbdev(intf);
  1339. int status = 0;
  1340. usb_pm_lock(udev);
  1341. if (intf->condition == USB_INTERFACE_UNBOUND)
  1342. status = -ENODEV;
  1343. else {
  1344. atomic_add(inc_usage_cnt, &intf->pm_usage_cnt);
  1345. udev->last_busy = jiffies;
  1346. if (inc_usage_cnt >= 0 &&
  1347. atomic_read(&intf->pm_usage_cnt) > 0) {
  1348. if (udev->state == USB_STATE_SUSPENDED)
  1349. status = usb_resume_both(udev,
  1350. PMSG_AUTO_RESUME);
  1351. if (status != 0)
  1352. atomic_sub(inc_usage_cnt, &intf->pm_usage_cnt);
  1353. else
  1354. udev->last_busy = jiffies;
  1355. } else if (inc_usage_cnt <= 0 &&
  1356. atomic_read(&intf->pm_usage_cnt) <= 0) {
  1357. status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
  1358. }
  1359. }
  1360. usb_pm_unlock(udev);
  1361. return status;
  1362. }
  1363. /**
  1364. * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
  1365. * @intf: the usb_interface whose counter should be decremented
  1366. *
  1367. * This routine should be called by an interface driver when it is
  1368. * finished using @intf and wants to allow it to autosuspend. A typical
  1369. * example would be a character-device driver when its device file is
  1370. * closed.
  1371. *
  1372. * The routine decrements @intf's usage counter. When the counter reaches
  1373. * 0, a delayed autosuspend request for @intf's device is queued. When
  1374. * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
  1375. * the other usage counters for the sibling interfaces and @intf's
  1376. * usb_device, the device and all its interfaces will be autosuspended.
  1377. *
  1378. * Note that @intf->pm_usage_cnt is owned by the interface driver. The
  1379. * core will not change its value other than the increment and decrement
  1380. * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
  1381. * may use this simple counter-oriented discipline or may set the value
  1382. * any way it likes.
  1383. *
  1384. * If the driver has set @intf->needs_remote_wakeup then autosuspend will
  1385. * take place only if the device's remote-wakeup facility is enabled.
  1386. *
  1387. * Suspend method calls queued by this routine can arrive at any time
  1388. * while @intf is resumed and its usage counter is equal to 0. They are
  1389. * not protected by the usb_device's lock but only by its pm_mutex.
  1390. * Drivers must provide their own synchronization.
  1391. *
  1392. * This routine can run only in process context.
  1393. */
  1394. void usb_autopm_put_interface(struct usb_interface *intf)
  1395. {
  1396. int status;
  1397. status = usb_autopm_do_interface(intf, -1);
  1398. dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
  1399. __func__, status, atomic_read(&intf->pm_usage_cnt));
  1400. }
  1401. EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
  1402. /**
  1403. * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
  1404. * @intf: the usb_interface whose counter should be decremented
  1405. *
  1406. * This routine does essentially the same thing as
  1407. * usb_autopm_put_interface(): it decrements @intf's usage counter and
  1408. * queues a delayed autosuspend request if the counter is <= 0. The
  1409. * difference is that it does not acquire the device's pm_mutex;
  1410. * callers must handle all synchronization issues themselves.
  1411. *
  1412. * Typically a driver would call this routine during an URB's completion
  1413. * handler, if no more URBs were pending.
  1414. *
  1415. * This routine can run in atomic context.
  1416. */
  1417. void usb_autopm_put_interface_async(struct usb_interface *intf)
  1418. {
  1419. struct usb_device *udev = interface_to_usbdev(intf);
  1420. int status = 0;
  1421. if (intf->condition == USB_INTERFACE_UNBOUND) {
  1422. status = -ENODEV;
  1423. } else {
  1424. udev->last_busy = jiffies;
  1425. atomic_dec(&intf->pm_usage_cnt);
  1426. if (udev->autosuspend_disabled || udev->autosuspend_delay < 0)
  1427. status = -EPERM;
  1428. else if (atomic_read(&intf->pm_usage_cnt) <= 0 &&
  1429. !timer_pending(&udev->autosuspend.timer)) {
  1430. queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
  1431. round_jiffies_up_relative(
  1432. udev->autosuspend_delay));
  1433. }
  1434. }
  1435. dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
  1436. __func__, status, atomic_read(&intf->pm_usage_cnt));
  1437. }
  1438. EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
  1439. /**
  1440. * usb_autopm_get_interface - increment a USB interface's PM-usage counter
  1441. * @intf: the usb_interface whose counter should be incremented
  1442. *
  1443. * This routine should be called by an interface driver when it wants to
  1444. * use @intf and needs to guarantee that it is not suspended. In addition,
  1445. * the routine prevents @intf from being autosuspended subsequently. (Note
  1446. * that this will not prevent suspend events originating in the PM core.)
  1447. * This prevention will persist until usb_autopm_put_interface() is called
  1448. * or @intf is unbound. A typical example would be a character-device
  1449. * driver when its device file is opened.
  1450. *
  1451. *
  1452. * The routine increments @intf's usage counter. (However if the
  1453. * autoresume fails then the counter is re-decremented.) So long as the
  1454. * counter is greater than 0, autosuspend will not be allowed for @intf
  1455. * or its usb_device. When the driver is finished using @intf it should
  1456. * call usb_autopm_put_interface() to decrement the usage counter and
  1457. * queue a delayed autosuspend request (if the counter is <= 0).
  1458. *
  1459. *
  1460. * Note that @intf->pm_usage_cnt is owned by the interface driver. The
  1461. * core will not change its value other than the increment and decrement
  1462. * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
  1463. * may use this simple counter-oriented discipline or may set the value
  1464. * any way it likes.
  1465. *
  1466. * Resume method calls generated by this routine can arrive at any time
  1467. * while @intf is suspended. They are not protected by the usb_device's
  1468. * lock but only by its pm_mutex. Drivers must provide their own
  1469. * synchronization.
  1470. *
  1471. * This routine can run only in process context.
  1472. */
  1473. int usb_autopm_get_interface(struct usb_interface *intf)
  1474. {
  1475. int status;
  1476. status = usb_autopm_do_interface(intf, 1);
  1477. dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
  1478. __func__, status, atomic_read(&intf->pm_usage_cnt));
  1479. return status;
  1480. }
  1481. EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
  1482. /**
  1483. * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
  1484. * @intf: the usb_interface whose counter should be incremented
  1485. *
  1486. * This routine does much the same thing as
  1487. * usb_autopm_get_interface(): it increments @intf's usage counter and
  1488. * queues an autoresume request if the result is > 0. The differences
  1489. * are that it does not acquire the device's pm_mutex (callers must
  1490. * handle all synchronization issues themselves), and it does not
  1491. * autoresume the device directly (it only queues a request). After a
  1492. * successful call, the device will generally not yet be resumed.
  1493. *
  1494. * This routine can run in atomic context.
  1495. */
  1496. int usb_autopm_get_interface_async(struct usb_interface *intf)
  1497. {
  1498. struct usb_device *udev = interface_to_usbdev(intf);
  1499. int status = 0;
  1500. if (intf->condition == USB_INTERFACE_UNBOUND)
  1501. status = -ENODEV;
  1502. else {
  1503. atomic_inc(&intf->pm_usage_cnt);
  1504. if (atomic_read(&intf->pm_usage_cnt) > 0 &&
  1505. udev->state == USB_STATE_SUSPENDED)
  1506. queue_work(ksuspend_usb_wq, &udev->autoresume);
  1507. }
  1508. dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
  1509. __func__, status, atomic_read(&intf->pm_usage_cnt));
  1510. return status;
  1511. }
  1512. EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
  1513. #else
  1514. void usb_autosuspend_work(struct work_struct *work)
  1515. {}
  1516. void usb_autoresume_work(struct work_struct *work)
  1517. {}
  1518. #endif /* CONFIG_USB_SUSPEND */
  1519. /**
  1520. * usb_external_suspend_device - external suspend of a USB device and its interfaces
  1521. * @udev: the usb_device to suspend
  1522. * @msg: Power Management message describing this state transition
  1523. *
  1524. * This routine handles external suspend requests: ones not generated
  1525. * internally by a USB driver (autosuspend) but rather coming from the user
  1526. * (via sysfs) or the PM core (system sleep). The suspend will be carried
  1527. * out regardless of @udev's usage counter or those of its interfaces,
  1528. * and regardless of whether or not remote wakeup is enabled. Of course,
  1529. * interface drivers still have the option of failing the suspend (if
  1530. * there are unsuspended children, for example).
  1531. *
  1532. * The caller must hold @udev's device lock.
  1533. */
  1534. int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
  1535. {
  1536. int status;
  1537. do_unbind_rebind(udev, DO_UNBIND);
  1538. usb_pm_lock(udev);
  1539. status = usb_suspend_both(udev, msg);
  1540. usb_pm_unlock(udev);
  1541. return status;
  1542. }
  1543. /**
  1544. * usb_external_resume_device - external resume of a USB device and its interfaces
  1545. * @udev: the usb_device to resume
  1546. * @msg: Power Management message describing this state transition
  1547. *
  1548. * This routine handles external resume requests: ones not generated
  1549. * internally by a USB driver (autoresume) but rather coming from the user
  1550. * (via sysfs), the PM core (system resume), or the device itself (remote
  1551. * wakeup). @udev's usage counter is unaffected.
  1552. *
  1553. * The caller must hold @udev's device lock.
  1554. */
  1555. int usb_external_resume_device(struct usb_device *udev, pm_message_t msg)
  1556. {
  1557. int status;
  1558. usb_pm_lock(udev);
  1559. status = usb_resume_both(udev, msg);
  1560. udev->last_busy = jiffies;
  1561. usb_pm_unlock(udev);
  1562. if (status == 0)
  1563. do_unbind_rebind(udev, DO_REBIND);
  1564. /* Now that the device is awake, we can start trying to autosuspend
  1565. * it again. */
  1566. if (status == 0)
  1567. usb_try_autosuspend_device(udev);
  1568. return status;
  1569. }
  1570. int usb_suspend(struct device *dev, pm_message_t msg)
  1571. {
  1572. struct usb_device *udev;
  1573. udev = to_usb_device(dev);
  1574. /* If udev is already suspended, we can skip this suspend and
  1575. * we should also skip the upcoming system resume. High-speed
  1576. * root hubs are an exception; they need to resume whenever the
  1577. * system wakes up in order for USB-PERSIST port handover to work
  1578. * properly.
  1579. */
  1580. if (udev->state == USB_STATE_SUSPENDED) {
  1581. if (udev->parent || udev->speed != USB_SPEED_HIGH)
  1582. udev->skip_sys_resume = 1;
  1583. return 0;
  1584. }
  1585. udev->skip_sys_resume = 0;
  1586. return usb_external_suspend_device(udev, msg);
  1587. }
  1588. int usb_resume(struct device *dev, pm_message_t msg)
  1589. {
  1590. struct usb_device *udev;
  1591. int status;
  1592. udev = to_usb_device(dev);
  1593. /* If udev->skip_sys_resume is set then udev was already suspended
  1594. * when the system sleep started, so we don't want to resume it
  1595. * during this system wakeup.
  1596. */
  1597. if (udev->skip_sys_resume)
  1598. return 0;
  1599. status = usb_external_resume_device(udev, msg);
  1600. /* Avoid PM error messages for devices disconnected while suspended
  1601. * as we'll display regular disconnect messages just a bit later.
  1602. */
  1603. if (status == -ENODEV)
  1604. return 0;
  1605. return status;
  1606. }
  1607. #endif /* CONFIG_PM */
  1608. struct bus_type usb_bus_type = {
  1609. .name = "usb",
  1610. .match = usb_device_match,
  1611. .uevent = usb_uevent,
  1612. };