input-polldev.c 6.2 KB

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