driver.c 49 KB

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