driver.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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. 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. if (udev->dev.parent->power.power_state.event !=
  1025. PM_EVENT_ON)
  1026. status = -EHOSTUNREACH;
  1027. else
  1028. status = usb_resume_device(udev);
  1029. }
  1030. } else {
  1031. /* Needed for setting udev->dev.power.power_state.event,
  1032. * for possible debugging message, and for reset_resume. */
  1033. status = usb_resume_device(udev);
  1034. }
  1035. if (status == 0 && udev->actconfig) {
  1036. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  1037. intf = udev->actconfig->interface[i];
  1038. usb_resume_interface(intf, udev->reset_resume);
  1039. }
  1040. }
  1041. done:
  1042. dev_vdbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status);
  1043. udev->reset_resume = 0;
  1044. return status;
  1045. }
  1046. #ifdef CONFIG_USB_SUSPEND
  1047. /* Internal routine to adjust a device's usage counter and change
  1048. * its autosuspend state.
  1049. */
  1050. static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
  1051. {
  1052. int status = 0;
  1053. usb_pm_lock(udev);
  1054. udev->auto_pm = 1;
  1055. udev->pm_usage_cnt += inc_usage_cnt;
  1056. WARN_ON(udev->pm_usage_cnt < 0);
  1057. if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
  1058. if (udev->state == USB_STATE_SUSPENDED)
  1059. status = usb_resume_both(udev);
  1060. if (status != 0)
  1061. udev->pm_usage_cnt -= inc_usage_cnt;
  1062. else if (inc_usage_cnt)
  1063. udev->last_busy = jiffies;
  1064. } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
  1065. if (inc_usage_cnt)
  1066. udev->last_busy = jiffies;
  1067. status = usb_suspend_both(udev, PMSG_SUSPEND);
  1068. }
  1069. usb_pm_unlock(udev);
  1070. return status;
  1071. }
  1072. /* usb_autosuspend_work - callback routine to autosuspend a USB device */
  1073. void usb_autosuspend_work(struct work_struct *work)
  1074. {
  1075. struct usb_device *udev =
  1076. container_of(work, struct usb_device, autosuspend.work);
  1077. usb_autopm_do_device(udev, 0);
  1078. }
  1079. /**
  1080. * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
  1081. * @udev: the usb_device to autosuspend
  1082. *
  1083. * This routine should be called when a core subsystem is finished using
  1084. * @udev and wants to allow it to autosuspend. Examples would be when
  1085. * @udev's device file in usbfs is closed or after a configuration change.
  1086. *
  1087. * @udev's usage counter is decremented. If it or any of the usage counters
  1088. * for an active interface is greater than 0, no autosuspend request will be
  1089. * queued. (If an interface driver does not support autosuspend then its
  1090. * usage counter is permanently positive.) Furthermore, if an interface
  1091. * driver requires remote-wakeup capability during autosuspend but remote
  1092. * wakeup is disabled, the autosuspend will fail.
  1093. *
  1094. * Often the caller will hold @udev's device lock, but this is not
  1095. * necessary.
  1096. *
  1097. * This routine can run only in process context.
  1098. */
  1099. void usb_autosuspend_device(struct usb_device *udev)
  1100. {
  1101. int status;
  1102. status = usb_autopm_do_device(udev, -1);
  1103. dev_vdbg(&udev->dev, "%s: cnt %d\n",
  1104. __FUNCTION__, udev->pm_usage_cnt);
  1105. }
  1106. /**
  1107. * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
  1108. * @udev: the usb_device to autosuspend
  1109. *
  1110. * This routine should be called when a core subsystem thinks @udev may
  1111. * be ready to autosuspend.
  1112. *
  1113. * @udev's usage counter left unchanged. If it or any of the usage counters
  1114. * for an active interface is greater than 0, or autosuspend is not allowed
  1115. * for any other reason, no autosuspend request will be queued.
  1116. *
  1117. * This routine can run only in process context.
  1118. */
  1119. void usb_try_autosuspend_device(struct usb_device *udev)
  1120. {
  1121. usb_autopm_do_device(udev, 0);
  1122. dev_vdbg(&udev->dev, "%s: cnt %d\n",
  1123. __FUNCTION__, udev->pm_usage_cnt);
  1124. }
  1125. /**
  1126. * usb_autoresume_device - immediately autoresume a USB device and its interfaces
  1127. * @udev: the usb_device to autoresume
  1128. *
  1129. * This routine should be called when a core subsystem wants to use @udev
  1130. * and needs to guarantee that it is not suspended. No autosuspend will
  1131. * occur until usb_autosuspend_device is called. (Note that this will not
  1132. * prevent suspend events originating in the PM core.) Examples would be
  1133. * when @udev's device file in usbfs is opened or when a remote-wakeup
  1134. * request is received.
  1135. *
  1136. * @udev's usage counter is incremented to prevent subsequent autosuspends.
  1137. * However if the autoresume fails then the usage counter is re-decremented.
  1138. *
  1139. * Often the caller will hold @udev's device lock, but this is not
  1140. * necessary (and attempting it might cause deadlock).
  1141. *
  1142. * This routine can run only in process context.
  1143. */
  1144. int usb_autoresume_device(struct usb_device *udev)
  1145. {
  1146. int status;
  1147. status = usb_autopm_do_device(udev, 1);
  1148. dev_vdbg(&udev->dev, "%s: status %d cnt %d\n",
  1149. __FUNCTION__, status, udev->pm_usage_cnt);
  1150. return status;
  1151. }
  1152. /* Internal routine to adjust an interface's usage counter and change
  1153. * its device's autosuspend state.
  1154. */
  1155. static int usb_autopm_do_interface(struct usb_interface *intf,
  1156. int inc_usage_cnt)
  1157. {
  1158. struct usb_device *udev = interface_to_usbdev(intf);
  1159. int status = 0;
  1160. usb_pm_lock(udev);
  1161. if (intf->condition == USB_INTERFACE_UNBOUND)
  1162. status = -ENODEV;
  1163. else {
  1164. udev->auto_pm = 1;
  1165. intf->pm_usage_cnt += inc_usage_cnt;
  1166. if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
  1167. if (udev->state == USB_STATE_SUSPENDED)
  1168. status = usb_resume_both(udev);
  1169. if (status != 0)
  1170. intf->pm_usage_cnt -= inc_usage_cnt;
  1171. else if (inc_usage_cnt)
  1172. udev->last_busy = jiffies;
  1173. } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) {
  1174. if (inc_usage_cnt)
  1175. udev->last_busy = jiffies;
  1176. status = usb_suspend_both(udev, PMSG_SUSPEND);
  1177. }
  1178. }
  1179. usb_pm_unlock(udev);
  1180. return status;
  1181. }
  1182. /**
  1183. * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
  1184. * @intf: the usb_interface whose counter should be decremented
  1185. *
  1186. * This routine should be called by an interface driver when it is
  1187. * finished using @intf and wants to allow it to autosuspend. A typical
  1188. * example would be a character-device driver when its device file is
  1189. * closed.
  1190. *
  1191. * The routine decrements @intf's usage counter. When the counter reaches
  1192. * 0, a delayed autosuspend request for @intf's device is queued. When
  1193. * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
  1194. * the other usage counters for the sibling interfaces and @intf's
  1195. * usb_device, the device and all its interfaces will be autosuspended.
  1196. *
  1197. * Note that @intf->pm_usage_cnt is owned by the interface driver. The
  1198. * core will not change its value other than the increment and decrement
  1199. * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
  1200. * may use this simple counter-oriented discipline or may set the value
  1201. * any way it likes.
  1202. *
  1203. * If the driver has set @intf->needs_remote_wakeup then autosuspend will
  1204. * take place only if the device's remote-wakeup facility is enabled.
  1205. *
  1206. * Suspend method calls queued by this routine can arrive at any time
  1207. * while @intf is resumed and its usage counter is equal to 0. They are
  1208. * not protected by the usb_device's lock but only by its pm_mutex.
  1209. * Drivers must provide their own synchronization.
  1210. *
  1211. * This routine can run only in process context.
  1212. */
  1213. void usb_autopm_put_interface(struct usb_interface *intf)
  1214. {
  1215. int status;
  1216. status = usb_autopm_do_interface(intf, -1);
  1217. dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
  1218. __FUNCTION__, status, intf->pm_usage_cnt);
  1219. }
  1220. EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
  1221. /**
  1222. * usb_autopm_get_interface - increment a USB interface's PM-usage counter
  1223. * @intf: the usb_interface whose counter should be incremented
  1224. *
  1225. * This routine should be called by an interface driver when it wants to
  1226. * use @intf and needs to guarantee that it is not suspended. In addition,
  1227. * the routine prevents @intf from being autosuspended subsequently. (Note
  1228. * that this will not prevent suspend events originating in the PM core.)
  1229. * This prevention will persist until usb_autopm_put_interface() is called
  1230. * or @intf is unbound. A typical example would be a character-device
  1231. * driver when its device file is opened.
  1232. *
  1233. *
  1234. * The routine increments @intf's usage counter. (However if the
  1235. * autoresume fails then the counter is re-decremented.) So long as the
  1236. * counter is greater than 0, autosuspend will not be allowed for @intf
  1237. * or its usb_device. When the driver is finished using @intf it should
  1238. * call usb_autopm_put_interface() to decrement the usage counter and
  1239. * queue a delayed autosuspend request (if the counter is <= 0).
  1240. *
  1241. *
  1242. * Note that @intf->pm_usage_cnt is owned by the interface driver. The
  1243. * core will not change its value other than the increment and decrement
  1244. * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
  1245. * may use this simple counter-oriented discipline or may set the value
  1246. * any way it likes.
  1247. *
  1248. * Resume method calls generated by this routine can arrive at any time
  1249. * while @intf is suspended. They are not protected by the usb_device's
  1250. * lock but only by its pm_mutex. Drivers must provide their own
  1251. * synchronization.
  1252. *
  1253. * This routine can run only in process context.
  1254. */
  1255. int usb_autopm_get_interface(struct usb_interface *intf)
  1256. {
  1257. int status;
  1258. status = usb_autopm_do_interface(intf, 1);
  1259. dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
  1260. __FUNCTION__, status, intf->pm_usage_cnt);
  1261. return status;
  1262. }
  1263. EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
  1264. /**
  1265. * usb_autopm_set_interface - set a USB interface's autosuspend state
  1266. * @intf: the usb_interface whose state should be set
  1267. *
  1268. * This routine sets the autosuspend state of @intf's device according
  1269. * to @intf's usage counter, which the caller must have set previously.
  1270. * If the counter is <= 0, the device is autosuspended (if it isn't
  1271. * already suspended and if nothing else prevents the autosuspend). If
  1272. * the counter is > 0, the device is autoresumed (if it isn't already
  1273. * awake).
  1274. */
  1275. int usb_autopm_set_interface(struct usb_interface *intf)
  1276. {
  1277. int status;
  1278. status = usb_autopm_do_interface(intf, 0);
  1279. dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
  1280. __FUNCTION__, status, intf->pm_usage_cnt);
  1281. return status;
  1282. }
  1283. EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
  1284. #else
  1285. void usb_autosuspend_work(struct work_struct *work)
  1286. {}
  1287. #endif /* CONFIG_USB_SUSPEND */
  1288. /**
  1289. * usb_external_suspend_device - external suspend of a USB device and its interfaces
  1290. * @udev: the usb_device to suspend
  1291. * @msg: Power Management message describing this state transition
  1292. *
  1293. * This routine handles external suspend requests: ones not generated
  1294. * internally by a USB driver (autosuspend) but rather coming from the user
  1295. * (via sysfs) or the PM core (system sleep). The suspend will be carried
  1296. * out regardless of @udev's usage counter or those of its interfaces,
  1297. * and regardless of whether or not remote wakeup is enabled. Of course,
  1298. * interface drivers still have the option of failing the suspend (if
  1299. * there are unsuspended children, for example).
  1300. *
  1301. * The caller must hold @udev's device lock.
  1302. */
  1303. int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
  1304. {
  1305. int status;
  1306. usb_pm_lock(udev);
  1307. udev->auto_pm = 0;
  1308. status = usb_suspend_both(udev, msg);
  1309. usb_pm_unlock(udev);
  1310. return status;
  1311. }
  1312. /**
  1313. * usb_external_resume_device - external resume of a USB device and its interfaces
  1314. * @udev: the usb_device to resume
  1315. *
  1316. * This routine handles external resume requests: ones not generated
  1317. * internally by a USB driver (autoresume) but rather coming from the user
  1318. * (via sysfs), the PM core (system resume), or the device itself (remote
  1319. * wakeup). @udev's usage counter is unaffected.
  1320. *
  1321. * The caller must hold @udev's device lock.
  1322. */
  1323. int usb_external_resume_device(struct usb_device *udev)
  1324. {
  1325. int status;
  1326. usb_pm_lock(udev);
  1327. udev->auto_pm = 0;
  1328. status = usb_resume_both(udev);
  1329. udev->last_busy = jiffies;
  1330. usb_pm_unlock(udev);
  1331. /* Now that the device is awake, we can start trying to autosuspend
  1332. * it again. */
  1333. if (status == 0)
  1334. usb_try_autosuspend_device(udev);
  1335. return status;
  1336. }
  1337. static int usb_suspend(struct device *dev, pm_message_t message)
  1338. {
  1339. if (!is_usb_device(dev)) /* Ignore PM for interfaces */
  1340. return 0;
  1341. return usb_external_suspend_device(to_usb_device(dev), message);
  1342. }
  1343. static int usb_resume(struct device *dev)
  1344. {
  1345. struct usb_device *udev;
  1346. if (!is_usb_device(dev)) /* Ignore PM for interfaces */
  1347. return 0;
  1348. udev = to_usb_device(dev);
  1349. /* If autoresume is disabled then we also want to prevent resume
  1350. * during system wakeup. However, a "persistent-device" reset-resume
  1351. * after power loss counts as a wakeup event. So allow a
  1352. * reset-resume to occur if remote wakeup is enabled. */
  1353. if (udev->autoresume_disabled) {
  1354. if (!(udev->reset_resume && udev->do_remote_wakeup))
  1355. return -EPERM;
  1356. }
  1357. return usb_external_resume_device(udev);
  1358. }
  1359. #else
  1360. #define usb_suspend NULL
  1361. #define usb_resume NULL
  1362. #endif /* CONFIG_PM */
  1363. struct bus_type usb_bus_type = {
  1364. .name = "usb",
  1365. .match = usb_device_match,
  1366. .uevent = usb_uevent,
  1367. .suspend = usb_suspend,
  1368. .resume = usb_resume,
  1369. };