input-polldev.c 9.3 KB

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