driver.c 47 KB

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