input-polldev.c 7.2 KB

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