input-polldev.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 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. /**
  111. * input_allocate_polled_device - allocate memory for polled device
  112. *
  113. * The function allocates memory for a polled device and also
  114. * for an input device associated with this polled device.
  115. */
  116. struct input_polled_dev *input_allocate_polled_device(void)
  117. {
  118. struct input_polled_dev *dev;
  119. dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
  120. if (!dev)
  121. return NULL;
  122. dev->input = input_allocate_device();
  123. if (!dev->input) {
  124. kfree(dev);
  125. return NULL;
  126. }
  127. return dev;
  128. }
  129. EXPORT_SYMBOL(input_allocate_polled_device);
  130. /**
  131. * input_free_polled_device - free memory allocated for polled device
  132. * @dev: device to free
  133. *
  134. * The function frees memory allocated for polling device and drops
  135. * reference to the associated input device.
  136. */
  137. void input_free_polled_device(struct input_polled_dev *dev)
  138. {
  139. if (dev) {
  140. input_free_device(dev->input);
  141. kfree(dev);
  142. }
  143. }
  144. EXPORT_SYMBOL(input_free_polled_device);
  145. /**
  146. * input_register_polled_device - register polled device
  147. * @dev: device to register
  148. *
  149. * The function registers previously initialized polled input device
  150. * with input layer. The device should be allocated with call to
  151. * input_allocate_polled_device(). Callers should also set up poll()
  152. * method and set up capabilities (id, name, phys, bits) of the
  153. * corresponding input_dev structure.
  154. */
  155. int input_register_polled_device(struct input_polled_dev *dev)
  156. {
  157. struct input_dev *input = dev->input;
  158. int error;
  159. input_set_drvdata(input, dev);
  160. INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
  161. if (!dev->poll_interval)
  162. dev->poll_interval = 500;
  163. if (!dev->poll_interval_max)
  164. dev->poll_interval_max = dev->poll_interval;
  165. input->open = input_open_polled_device;
  166. input->close = input_close_polled_device;
  167. error = input_register_device(input);
  168. if (error)
  169. return error;
  170. error = sysfs_create_group(&input->dev.kobj,
  171. &input_polldev_attribute_group);
  172. if (error) {
  173. input_unregister_device(input);
  174. return error;
  175. }
  176. /*
  177. * Take extra reference to the underlying input device so
  178. * that it survives call to input_unregister_polled_device()
  179. * and is deleted only after input_free_polled_device()
  180. * has been invoked. This is needed to ease task of freeing
  181. * sparse keymaps.
  182. */
  183. input_get_device(input);
  184. return 0;
  185. }
  186. EXPORT_SYMBOL(input_register_polled_device);
  187. /**
  188. * input_unregister_polled_device - unregister polled device
  189. * @dev: device to unregister
  190. *
  191. * The function unregisters previously registered polled input
  192. * device from input layer. Polling is stopped and device is
  193. * ready to be freed with call to input_free_polled_device().
  194. */
  195. void input_unregister_polled_device(struct input_polled_dev *dev)
  196. {
  197. sysfs_remove_group(&dev->input->dev.kobj,
  198. &input_polldev_attribute_group);
  199. input_unregister_device(dev->input);
  200. }
  201. EXPORT_SYMBOL(input_unregister_polled_device);