input-polldev.c 6.2 KB

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