input-polldev.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 long interval;
  68. if (strict_strtoul(buf, 0, &interval))
  69. return -EINVAL;
  70. if (interval < polldev->poll_interval_min)
  71. return -EINVAL;
  72. if (interval > polldev->poll_interval_max)
  73. return -EINVAL;
  74. mutex_lock(&input->mutex);
  75. polldev->poll_interval = interval;
  76. if (input->users) {
  77. cancel_delayed_work_sync(&polldev->work);
  78. if (polldev->poll_interval > 0)
  79. input_polldev_queue_work(polldev);
  80. }
  81. mutex_unlock(&input->mutex);
  82. return count;
  83. }
  84. static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
  85. input_polldev_set_poll);
  86. static ssize_t input_polldev_get_max(struct device *dev,
  87. struct device_attribute *attr, char *buf)
  88. {
  89. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  90. return sprintf(buf, "%d\n", polldev->poll_interval_max);
  91. }
  92. static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
  93. static ssize_t input_polldev_get_min(struct device *dev,
  94. struct device_attribute *attr, char *buf)
  95. {
  96. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  97. return sprintf(buf, "%d\n", polldev->poll_interval_min);
  98. }
  99. static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
  100. static struct attribute *sysfs_attrs[] = {
  101. &dev_attr_poll.attr,
  102. &dev_attr_max.attr,
  103. &dev_attr_min.attr,
  104. NULL
  105. };
  106. static struct attribute_group input_polldev_attribute_group = {
  107. .attrs = sysfs_attrs
  108. };
  109. /**
  110. * input_allocate_polled_device - allocate memory for polled device
  111. *
  112. * The function allocates memory for a polled device and also
  113. * for an input device associated with this polled device.
  114. */
  115. struct input_polled_dev *input_allocate_polled_device(void)
  116. {
  117. struct input_polled_dev *dev;
  118. dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
  119. if (!dev)
  120. return NULL;
  121. dev->input = input_allocate_device();
  122. if (!dev->input) {
  123. kfree(dev);
  124. return NULL;
  125. }
  126. return dev;
  127. }
  128. EXPORT_SYMBOL(input_allocate_polled_device);
  129. /**
  130. * input_free_polled_device - free memory allocated for polled device
  131. * @dev: device to free
  132. *
  133. * The function frees memory allocated for polling device and drops
  134. * reference to the associated input device.
  135. */
  136. void input_free_polled_device(struct input_polled_dev *dev)
  137. {
  138. if (dev) {
  139. input_free_device(dev->input);
  140. kfree(dev);
  141. }
  142. }
  143. EXPORT_SYMBOL(input_free_polled_device);
  144. /**
  145. * input_register_polled_device - register polled device
  146. * @dev: device to register
  147. *
  148. * The function registers previously initialized polled input device
  149. * with input layer. The device should be allocated with call to
  150. * input_allocate_polled_device(). Callers should also set up poll()
  151. * method and set up capabilities (id, name, phys, bits) of the
  152. * corresponding input_dev structure.
  153. */
  154. int input_register_polled_device(struct input_polled_dev *dev)
  155. {
  156. struct input_dev *input = dev->input;
  157. int error;
  158. input_set_drvdata(input, dev);
  159. INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
  160. if (!dev->poll_interval)
  161. dev->poll_interval = 500;
  162. if (!dev->poll_interval_max)
  163. dev->poll_interval_max = dev->poll_interval;
  164. input->open = input_open_polled_device;
  165. input->close = input_close_polled_device;
  166. error = input_register_device(input);
  167. if (error)
  168. return error;
  169. error = sysfs_create_group(&input->dev.kobj,
  170. &input_polldev_attribute_group);
  171. if (error) {
  172. input_unregister_device(input);
  173. return error;
  174. }
  175. /*
  176. * Take extra reference to the underlying input device so
  177. * that it survives call to input_unregister_polled_device()
  178. * and is deleted only after input_free_polled_device()
  179. * has been invoked. This is needed to ease task of freeing
  180. * sparse keymaps.
  181. */
  182. input_get_device(input);
  183. return 0;
  184. }
  185. EXPORT_SYMBOL(input_register_polled_device);
  186. /**
  187. * input_unregister_polled_device - unregister polled device
  188. * @dev: device to unregister
  189. *
  190. * The function unregisters previously registered polled input
  191. * device from input layer. Polling is stopped and device is
  192. * ready to be freed with call to input_free_polled_device().
  193. */
  194. void input_unregister_polled_device(struct input_polled_dev *dev)
  195. {
  196. sysfs_remove_group(&dev->input->dev.kobj,
  197. &input_polldev_attribute_group);
  198. input_unregister_device(dev->input);
  199. }
  200. EXPORT_SYMBOL(input_unregister_polled_device);