input-polldev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Generic implementation of a polled input device
  3. * Copyright (c) 2007 Dmitry Torokhov
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/jiffies.h>
  11. #include <linux/slab.h>
  12. #include <linux/mutex.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/module.h>
  15. #include <linux/input-polldev.h>
  16. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  17. MODULE_DESCRIPTION("Generic implementation of a polled input device");
  18. MODULE_LICENSE("GPL v2");
  19. static void input_polldev_queue_work(struct input_polled_dev *dev)
  20. {
  21. unsigned long delay;
  22. delay = msecs_to_jiffies(dev->poll_interval);
  23. if (delay >= HZ)
  24. delay = round_jiffies_relative(delay);
  25. queue_delayed_work(system_freezable_wq, &dev->work, delay);
  26. }
  27. static void input_polled_device_work(struct work_struct *work)
  28. {
  29. struct input_polled_dev *dev =
  30. container_of(work, struct input_polled_dev, work.work);
  31. dev->poll(dev);
  32. input_polldev_queue_work(dev);
  33. }
  34. static int input_open_polled_device(struct input_dev *input)
  35. {
  36. struct input_polled_dev *dev = input_get_drvdata(input);
  37. if (dev->open)
  38. dev->open(dev);
  39. /* Only start polling if polling is enabled */
  40. if (dev->poll_interval > 0) {
  41. dev->poll(dev);
  42. input_polldev_queue_work(dev);
  43. }
  44. return 0;
  45. }
  46. static void input_close_polled_device(struct input_dev *input)
  47. {
  48. struct input_polled_dev *dev = input_get_drvdata(input);
  49. cancel_delayed_work_sync(&dev->work);
  50. if (dev->close)
  51. dev->close(dev);
  52. }
  53. /* SYSFS interface */
  54. static ssize_t input_polldev_get_poll(struct device *dev,
  55. struct device_attribute *attr, char *buf)
  56. {
  57. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  58. return sprintf(buf, "%d\n", polldev->poll_interval);
  59. }
  60. static ssize_t input_polldev_set_poll(struct device *dev,
  61. struct device_attribute *attr, const char *buf,
  62. size_t count)
  63. {
  64. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  65. struct input_dev *input = polldev->input;
  66. unsigned int interval;
  67. int err;
  68. err = kstrtouint(buf, 0, &interval);
  69. if (err)
  70. return err;
  71. if (interval < polldev->poll_interval_min)
  72. return -EINVAL;
  73. if (interval > polldev->poll_interval_max)
  74. return -EINVAL;
  75. mutex_lock(&input->mutex);
  76. polldev->poll_interval = interval;
  77. if (input->users) {
  78. cancel_delayed_work_sync(&polldev->work);
  79. if (polldev->poll_interval > 0)
  80. input_polldev_queue_work(polldev);
  81. }
  82. mutex_unlock(&input->mutex);
  83. return count;
  84. }
  85. static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
  86. input_polldev_set_poll);
  87. static ssize_t input_polldev_get_max(struct device *dev,
  88. struct device_attribute *attr, char *buf)
  89. {
  90. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  91. return sprintf(buf, "%d\n", polldev->poll_interval_max);
  92. }
  93. static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
  94. static ssize_t input_polldev_get_min(struct device *dev,
  95. struct device_attribute *attr, char *buf)
  96. {
  97. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  98. return sprintf(buf, "%d\n", polldev->poll_interval_min);
  99. }
  100. static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
  101. static struct attribute *sysfs_attrs[] = {
  102. &dev_attr_poll.attr,
  103. &dev_attr_max.attr,
  104. &dev_attr_min.attr,
  105. NULL
  106. };
  107. static struct attribute_group input_polldev_attribute_group = {
  108. .attrs = sysfs_attrs
  109. };
  110. static const struct attribute_group *input_polldev_attribute_groups[] = {
  111. &input_polldev_attribute_group,
  112. NULL
  113. };
  114. /**
  115. * input_allocate_polled_device - allocate memory for polled device
  116. *
  117. * The function allocates memory for a polled device and also
  118. * for an input device associated with this polled device.
  119. */
  120. struct input_polled_dev *input_allocate_polled_device(void)
  121. {
  122. struct input_polled_dev *dev;
  123. dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
  124. if (!dev)
  125. return NULL;
  126. dev->input = input_allocate_device();
  127. if (!dev->input) {
  128. kfree(dev);
  129. return NULL;
  130. }
  131. return dev;
  132. }
  133. EXPORT_SYMBOL(input_allocate_polled_device);
  134. struct input_polled_devres {
  135. struct input_polled_dev *polldev;
  136. };
  137. static int devm_input_polldev_match(struct device *dev, void *res, void *data)
  138. {
  139. struct input_polled_devres *devres = res;
  140. return devres->polldev == data;
  141. }
  142. static void devm_input_polldev_release(struct device *dev, void *res)
  143. {
  144. struct input_polled_devres *devres = res;
  145. struct input_polled_dev *polldev = devres->polldev;
  146. dev_dbg(dev, "%s: dropping reference/freeing %s\n",
  147. __func__, dev_name(&polldev->input->dev));
  148. input_put_device(polldev->input);
  149. kfree(polldev);
  150. }
  151. static void devm_input_polldev_unregister(struct device *dev, void *res)
  152. {
  153. struct input_polled_devres *devres = res;
  154. struct input_polled_dev *polldev = devres->polldev;
  155. dev_dbg(dev, "%s: unregistering device %s\n",
  156. __func__, dev_name(&polldev->input->dev));
  157. input_unregister_device(polldev->input);
  158. /*
  159. * Note that we are still holding extra reference to the input
  160. * device so it will stick around until devm_input_polldev_release()
  161. * is called.
  162. */
  163. }
  164. /**
  165. * devm_input_allocate_polled_device - allocate managed polled device
  166. * @dev: device owning the polled device being created
  167. *
  168. * Returns prepared &struct input_polled_dev or %NULL.
  169. *
  170. * Managed polled input devices do not need to be explicitly unregistered
  171. * or freed as it will be done automatically when owner device unbinds
  172. * from * its driver (or binding fails). Once such managed polled device
  173. * is allocated, it is ready to be set up and registered in the same
  174. * fashion as regular polled input devices (using
  175. * input_register_polled_device() function).
  176. *
  177. * If you want to manually unregister and free such managed polled devices,
  178. * it can be still done by calling input_unregister_polled_device() and
  179. * input_free_polled_device(), although it is rarely needed.
  180. *
  181. * NOTE: the owner device is set up as parent of input device and users
  182. * should not override it.
  183. */
  184. struct input_polled_dev *devm_input_allocate_polled_device(struct device *dev)
  185. {
  186. struct input_polled_dev *polldev;
  187. struct input_polled_devres *devres;
  188. devres = devres_alloc(devm_input_polldev_release, sizeof(*devres),
  189. GFP_KERNEL);
  190. if (!devres)
  191. return NULL;
  192. polldev = input_allocate_polled_device();
  193. if (!polldev) {
  194. devres_free(devres);
  195. return NULL;
  196. }
  197. polldev->input->dev.parent = dev;
  198. polldev->devres_managed = true;
  199. devres->polldev = polldev;
  200. devres_add(dev, devres);
  201. return polldev;
  202. }
  203. EXPORT_SYMBOL(devm_input_allocate_polled_device);
  204. /**
  205. * input_free_polled_device - free memory allocated for polled device
  206. * @dev: device to free
  207. *
  208. * The function frees memory allocated for polling device and drops
  209. * reference to the associated input device.
  210. */
  211. void input_free_polled_device(struct input_polled_dev *dev)
  212. {
  213. if (dev) {
  214. if (dev->devres_managed)
  215. WARN_ON(devres_destroy(dev->input->dev.parent,
  216. devm_input_polldev_release,
  217. devm_input_polldev_match,
  218. dev));
  219. input_put_device(dev->input);
  220. kfree(dev);
  221. }
  222. }
  223. EXPORT_SYMBOL(input_free_polled_device);
  224. /**
  225. * input_register_polled_device - register polled device
  226. * @dev: device to register
  227. *
  228. * The function registers previously initialized polled input device
  229. * with input layer. The device should be allocated with call to
  230. * input_allocate_polled_device(). Callers should also set up poll()
  231. * method and set up capabilities (id, name, phys, bits) of the
  232. * corresponding input_dev structure.
  233. */
  234. int input_register_polled_device(struct input_polled_dev *dev)
  235. {
  236. struct input_polled_devres *devres = NULL;
  237. struct input_dev *input = dev->input;
  238. int error;
  239. if (dev->devres_managed) {
  240. devres = devres_alloc(devm_input_polldev_unregister,
  241. sizeof(*devres), GFP_KERNEL);
  242. if (!devres)
  243. return -ENOMEM;
  244. devres->polldev = dev;
  245. }
  246. input_set_drvdata(input, dev);
  247. INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
  248. if (!dev->poll_interval)
  249. dev->poll_interval = 500;
  250. if (!dev->poll_interval_max)
  251. dev->poll_interval_max = dev->poll_interval;
  252. input->open = input_open_polled_device;
  253. input->close = input_close_polled_device;
  254. input->dev.groups = input_polldev_attribute_groups;
  255. error = input_register_device(input);
  256. if (error) {
  257. devres_free(devres);
  258. return error;
  259. }
  260. /*
  261. * Take extra reference to the underlying input device so
  262. * that it survives call to input_unregister_polled_device()
  263. * and is deleted only after input_free_polled_device()
  264. * has been invoked. This is needed to ease task of freeing
  265. * sparse keymaps.
  266. */
  267. input_get_device(input);
  268. if (dev->devres_managed) {
  269. dev_dbg(input->dev.parent, "%s: registering %s with devres.\n",
  270. __func__, dev_name(&input->dev));
  271. devres_add(input->dev.parent, devres);
  272. }
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(input_register_polled_device);
  276. /**
  277. * input_unregister_polled_device - unregister polled device
  278. * @dev: device to unregister
  279. *
  280. * The function unregisters previously registered polled input
  281. * device from input layer. Polling is stopped and device is
  282. * ready to be freed with call to input_free_polled_device().
  283. */
  284. void input_unregister_polled_device(struct input_polled_dev *dev)
  285. {
  286. if (dev->devres_managed)
  287. WARN_ON(devres_destroy(dev->input->dev.parent,
  288. devm_input_polldev_unregister,
  289. devm_input_polldev_match,
  290. dev));
  291. input_unregister_device(dev->input);
  292. }
  293. EXPORT_SYMBOL(input_unregister_polled_device);