input-polldev.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. /*
  80. * Clean up work struct to remove references to the workqueue.
  81. * It may be destroyed by the next call. This causes problems
  82. * at next device open-close in case of poll_interval == 0.
  83. */
  84. INIT_DELAYED_WORK(&dev->work, dev->work.work.func);
  85. input_polldev_stop_workqueue();
  86. if (dev->close)
  87. dev->close(dev);
  88. }
  89. /* SYSFS interface */
  90. static ssize_t input_polldev_get_poll(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);
  95. }
  96. static ssize_t input_polldev_set_poll(struct device *dev,
  97. struct device_attribute *attr, const char *buf,
  98. size_t count)
  99. {
  100. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  101. struct input_dev *input = polldev->input;
  102. unsigned long interval;
  103. if (strict_strtoul(buf, 0, &interval))
  104. return -EINVAL;
  105. if (interval < polldev->poll_interval_min)
  106. return -EINVAL;
  107. if (interval > polldev->poll_interval_max)
  108. return -EINVAL;
  109. mutex_lock(&input->mutex);
  110. polldev->poll_interval = interval;
  111. if (input->users) {
  112. cancel_delayed_work_sync(&polldev->work);
  113. if (polldev->poll_interval > 0)
  114. input_polldev_queue_work(polldev);
  115. }
  116. mutex_unlock(&input->mutex);
  117. return count;
  118. }
  119. static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
  120. input_polldev_set_poll);
  121. static ssize_t input_polldev_get_max(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  125. return sprintf(buf, "%d\n", polldev->poll_interval_max);
  126. }
  127. static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
  128. static ssize_t input_polldev_get_min(struct device *dev,
  129. struct device_attribute *attr, char *buf)
  130. {
  131. struct input_polled_dev *polldev = dev_get_drvdata(dev);
  132. return sprintf(buf, "%d\n", polldev->poll_interval_min);
  133. }
  134. static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
  135. static struct attribute *sysfs_attrs[] = {
  136. &dev_attr_poll.attr,
  137. &dev_attr_max.attr,
  138. &dev_attr_min.attr,
  139. NULL
  140. };
  141. static struct attribute_group input_polldev_attribute_group = {
  142. .attrs = sysfs_attrs
  143. };
  144. /**
  145. * input_allocate_polled_device - allocated memory polled device
  146. *
  147. * The function allocates memory for a polled device and also
  148. * for an input device associated with this polled device.
  149. */
  150. struct input_polled_dev *input_allocate_polled_device(void)
  151. {
  152. struct input_polled_dev *dev;
  153. dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
  154. if (!dev)
  155. return NULL;
  156. dev->input = input_allocate_device();
  157. if (!dev->input) {
  158. kfree(dev);
  159. return NULL;
  160. }
  161. return dev;
  162. }
  163. EXPORT_SYMBOL(input_allocate_polled_device);
  164. /**
  165. * input_free_polled_device - free memory allocated for polled device
  166. * @dev: device to free
  167. *
  168. * The function frees memory allocated for polling device and drops
  169. * reference to the associated input device.
  170. */
  171. void input_free_polled_device(struct input_polled_dev *dev)
  172. {
  173. if (dev) {
  174. input_free_device(dev->input);
  175. kfree(dev);
  176. }
  177. }
  178. EXPORT_SYMBOL(input_free_polled_device);
  179. /**
  180. * input_register_polled_device - register polled device
  181. * @dev: device to register
  182. *
  183. * The function registers previously initialized polled input device
  184. * with input layer. The device should be allocated with call to
  185. * input_allocate_polled_device(). Callers should also set up poll()
  186. * method and set up capabilities (id, name, phys, bits) of the
  187. * corresponing input_dev structure.
  188. */
  189. int input_register_polled_device(struct input_polled_dev *dev)
  190. {
  191. struct input_dev *input = dev->input;
  192. int error;
  193. input_set_drvdata(input, dev);
  194. INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
  195. if (!dev->poll_interval)
  196. dev->poll_interval = 500;
  197. if (!dev->poll_interval_max)
  198. dev->poll_interval_max = dev->poll_interval;
  199. input->open = input_open_polled_device;
  200. input->close = input_close_polled_device;
  201. error = input_register_device(input);
  202. if (error)
  203. return error;
  204. error = sysfs_create_group(&input->dev.kobj,
  205. &input_polldev_attribute_group);
  206. if (error) {
  207. input_unregister_device(input);
  208. return error;
  209. }
  210. /*
  211. * Take extra reference to the underlying input device so
  212. * that it survives call to input_unregister_polled_device()
  213. * and is deleted only after input_free_polled_device()
  214. * has been invoked. This is needed to ease task of freeing
  215. * sparse keymaps.
  216. */
  217. input_get_device(input);
  218. return 0;
  219. }
  220. EXPORT_SYMBOL(input_register_polled_device);
  221. /**
  222. * input_unregister_polled_device - unregister polled device
  223. * @dev: device to unregister
  224. *
  225. * The function unregisters previously registered polled input
  226. * device from input layer. Polling is stopped and device is
  227. * ready to be freed with call to input_free_polled_device().
  228. */
  229. void input_unregister_polled_device(struct input_polled_dev *dev)
  230. {
  231. sysfs_remove_group(&dev->input->dev.kobj,
  232. &input_polldev_attribute_group);
  233. input_unregister_device(dev->input);
  234. }
  235. EXPORT_SYMBOL(input_unregister_polled_device);