input-polldev.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include <linux/jiffies.h>
  10. #include <linux/mutex.h>
  11. #include <linux/input-polldev.h>
  12. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  13. MODULE_DESCRIPTION("Generic implementation of a polled input device");
  14. MODULE_LICENSE("GPL v2");
  15. MODULE_VERSION("0.1");
  16. static DEFINE_MUTEX(polldev_mutex);
  17. static int polldev_users;
  18. static struct workqueue_struct *polldev_wq;
  19. static int input_polldev_start_workqueue(void)
  20. {
  21. int retval;
  22. retval = mutex_lock_interruptible(&polldev_mutex);
  23. if (retval)
  24. return retval;
  25. if (!polldev_users) {
  26. polldev_wq = create_singlethread_workqueue("ipolldevd");
  27. if (!polldev_wq) {
  28. printk(KERN_ERR "input-polldev: failed to create "
  29. "ipolldevd workqueue\n");
  30. retval = -ENOMEM;
  31. goto out;
  32. }
  33. }
  34. polldev_users++;
  35. out:
  36. mutex_unlock(&polldev_mutex);
  37. return retval;
  38. }
  39. static void input_polldev_stop_workqueue(void)
  40. {
  41. mutex_lock(&polldev_mutex);
  42. if (!--polldev_users)
  43. destroy_workqueue(polldev_wq);
  44. mutex_unlock(&polldev_mutex);
  45. }
  46. static void input_polldev_queue_work(struct input_polled_dev *dev)
  47. {
  48. unsigned long delay;
  49. delay = msecs_to_jiffies(dev->poll_interval);
  50. if (delay >= HZ)
  51. delay = round_jiffies_relative(delay);
  52. queue_delayed_work(polldev_wq, &dev->work, delay);
  53. }
  54. static void input_polled_device_work(struct work_struct *work)
  55. {
  56. struct input_polled_dev *dev =
  57. container_of(work, struct input_polled_dev, work.work);
  58. dev->poll(dev);
  59. input_polldev_queue_work(dev);
  60. }
  61. static int input_open_polled_device(struct input_dev *input)
  62. {
  63. struct input_polled_dev *dev = input_get_drvdata(input);
  64. int error;
  65. error = input_polldev_start_workqueue();
  66. if (error)
  67. return error;
  68. if (dev->open)
  69. dev->open(dev);
  70. /* Only start polling if polling is enabled */
  71. if (dev->poll_interval > 0)
  72. queue_delayed_work(polldev_wq, &dev->work, 0);
  73. return 0;
  74. }
  75. static void input_close_polled_device(struct input_dev *input)
  76. {
  77. struct input_polled_dev *dev = input_get_drvdata(input);
  78. cancel_delayed_work_sync(&dev->work);
  79. input_polldev_stop_workqueue();
  80. if (dev->close)
  81. dev->close(dev);
  82. }
  83. /* SYSFS interface */
  84. static ssize_t input_polldev_get_poll(struct device *dev,
  85. struct device_attribute *attr, char *buf)
  86. {
  87. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  88. return sprintf(buf, "%d\n", polldev->poll_interval);
  89. }
  90. static ssize_t input_polldev_set_poll(struct device *dev,
  91. struct device_attribute *attr, const char *buf,
  92. size_t count)
  93. {
  94. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  95. struct input_dev *input = polldev->input;
  96. unsigned long interval;
  97. if (strict_strtoul(buf, 0, &interval))
  98. return -EINVAL;
  99. if (interval < polldev->poll_interval_min)
  100. return -EINVAL;
  101. if (interval > polldev->poll_interval_max)
  102. return -EINVAL;
  103. mutex_lock(&input->mutex);
  104. polldev->poll_interval = interval;
  105. if (input->users) {
  106. cancel_delayed_work_sync(&polldev->work);
  107. if (polldev->poll_interval > 0)
  108. input_polldev_queue_work(polldev);
  109. }
  110. mutex_unlock(&input->mutex);
  111. return count;
  112. }
  113. static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
  114. input_polldev_set_poll);
  115. static ssize_t input_polldev_get_max(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  119. return sprintf(buf, "%d\n", polldev->poll_interval_max);
  120. }
  121. static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
  122. static ssize_t input_polldev_get_min(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  126. return sprintf(buf, "%d\n", polldev->poll_interval_min);
  127. }
  128. static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
  129. static struct attribute *sysfs_attrs[] = {
  130. &dev_attr_poll.attr,
  131. &dev_attr_max.attr,
  132. &dev_attr_min.attr,
  133. NULL
  134. };
  135. static struct attribute_group input_polldev_attribute_group = {
  136. .attrs = sysfs_attrs
  137. };
  138. /**
  139. * input_allocate_polled_device - allocated memory polled device
  140. *
  141. * The function allocates memory for a polled device and also
  142. * for an input device associated with this polled device.
  143. */
  144. struct input_polled_dev *input_allocate_polled_device(void)
  145. {
  146. struct input_polled_dev *dev;
  147. dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
  148. if (!dev)
  149. return NULL;
  150. dev->input = input_allocate_device();
  151. if (!dev->input) {
  152. kfree(dev);
  153. return NULL;
  154. }
  155. return dev;
  156. }
  157. EXPORT_SYMBOL(input_allocate_polled_device);
  158. /**
  159. * input_free_polled_device - free memory allocated for polled device
  160. * @dev: device to free
  161. *
  162. * The function frees memory allocated for polling device and drops
  163. * reference to the associated input device.
  164. */
  165. void input_free_polled_device(struct input_polled_dev *dev)
  166. {
  167. if (dev) {
  168. input_free_device(dev->input);
  169. kfree(dev);
  170. }
  171. }
  172. EXPORT_SYMBOL(input_free_polled_device);
  173. /**
  174. * input_register_polled_device - register polled device
  175. * @dev: device to register
  176. *
  177. * The function registers previously initialized polled input device
  178. * with input layer. The device should be allocated with call to
  179. * input_allocate_polled_device(). Callers should also set up poll()
  180. * method and set up capabilities (id, name, phys, bits) of the
  181. * corresponing input_dev structure.
  182. */
  183. int input_register_polled_device(struct input_polled_dev *dev)
  184. {
  185. struct input_dev *input = dev->input;
  186. int error;
  187. input_set_drvdata(input, dev);
  188. INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
  189. if (!dev->poll_interval)
  190. dev->poll_interval = 500;
  191. if (!dev->poll_interval_max)
  192. dev->poll_interval_max = dev->poll_interval;
  193. input->open = input_open_polled_device;
  194. input->close = input_close_polled_device;
  195. error = input_register_device(input);
  196. if (error)
  197. return error;
  198. error = sysfs_create_group(&input->dev.kobj,
  199. &input_polldev_attribute_group);
  200. if (error) {
  201. input_unregister_device(input);
  202. return error;
  203. }
  204. /*
  205. * Take extra reference to the underlying input device so
  206. * that it survives call to input_unregister_polled_device()
  207. * and is deleted only after input_free_polled_device()
  208. * has been invoked. This is needed to ease task of freeing
  209. * sparse keymaps.
  210. */
  211. input_get_device(input);
  212. return 0;
  213. }
  214. EXPORT_SYMBOL(input_register_polled_device);
  215. /**
  216. * input_unregister_polled_device - unregister polled device
  217. * @dev: device to unregister
  218. *
  219. * The function unregisters previously registered polled input
  220. * device from input layer. Polling is stopped and device is
  221. * ready to be freed with call to input_free_polled_device().
  222. */
  223. void input_unregister_polled_device(struct input_polled_dev *dev)
  224. {
  225. sysfs_remove_group(&dev->input->dev.kobj,
  226. &input_polldev_attribute_group);
  227. input_unregister_device(dev->input);
  228. }
  229. EXPORT_SYMBOL(input_unregister_polled_device);