input-polldev.c 9.1 KB

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