input-polldev.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_polled_device_work(struct work_struct *work)
  47. {
  48. struct input_polled_dev *dev =
  49. container_of(work, struct input_polled_dev, work.work);
  50. unsigned long delay;
  51. dev->poll(dev);
  52. delay = msecs_to_jiffies(dev->poll_interval);
  53. if (delay >= HZ)
  54. delay = round_jiffies_relative(delay);
  55. queue_delayed_work(polldev_wq, &dev->work, delay);
  56. }
  57. static int input_open_polled_device(struct input_dev *input)
  58. {
  59. struct input_polled_dev *dev = input_get_drvdata(input);
  60. int error;
  61. error = input_polldev_start_workqueue();
  62. if (error)
  63. return error;
  64. if (dev->flush)
  65. dev->flush(dev);
  66. queue_delayed_work(polldev_wq, &dev->work,
  67. msecs_to_jiffies(dev->poll_interval));
  68. return 0;
  69. }
  70. static void input_close_polled_device(struct input_dev *input)
  71. {
  72. struct input_polled_dev *dev = input_get_drvdata(input);
  73. cancel_delayed_work_sync(&dev->work);
  74. input_polldev_stop_workqueue();
  75. }
  76. /**
  77. * input_allocate_polled_device - allocated memory polled device
  78. *
  79. * The function allocates memory for a polled device and also
  80. * for an input device associated with this polled device.
  81. */
  82. struct input_polled_dev *input_allocate_polled_device(void)
  83. {
  84. struct input_polled_dev *dev;
  85. dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
  86. if (!dev)
  87. return NULL;
  88. dev->input = input_allocate_device();
  89. if (!dev->input) {
  90. kfree(dev);
  91. return NULL;
  92. }
  93. return dev;
  94. }
  95. EXPORT_SYMBOL(input_allocate_polled_device);
  96. /**
  97. * input_free_polled_device - free memory allocated for polled device
  98. * @dev: device to free
  99. *
  100. * The function frees memory allocated for polling device and drops
  101. * reference to the associated input device (if present).
  102. */
  103. void input_free_polled_device(struct input_polled_dev *dev)
  104. {
  105. if (dev) {
  106. input_free_device(dev->input);
  107. kfree(dev);
  108. }
  109. }
  110. EXPORT_SYMBOL(input_free_polled_device);
  111. /**
  112. * input_register_polled_device - register polled device
  113. * @dev: device to register
  114. *
  115. * The function registers previously initialized polled input device
  116. * with input layer. The device should be allocated with call to
  117. * input_allocate_polled_device(). Callers should also set up poll()
  118. * method and set up capabilities (id, name, phys, bits) of the
  119. * corresponing input_dev structure.
  120. */
  121. int input_register_polled_device(struct input_polled_dev *dev)
  122. {
  123. struct input_dev *input = dev->input;
  124. input_set_drvdata(input, dev);
  125. INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
  126. if (!dev->poll_interval)
  127. dev->poll_interval = 500;
  128. input->open = input_open_polled_device;
  129. input->close = input_close_polled_device;
  130. return input_register_device(input);
  131. }
  132. EXPORT_SYMBOL(input_register_polled_device);
  133. /**
  134. * input_unregister_polled_device - unregister polled device
  135. * @dev: device to unregister
  136. *
  137. * The function unregisters previously registered polled input
  138. * device from input layer. Polling is stopped and device is
  139. * ready to be freed with call to input_free_polled_device().
  140. * Callers should not attempt to access dev->input pointer
  141. * after calling this function.
  142. */
  143. void input_unregister_polled_device(struct input_polled_dev *dev)
  144. {
  145. input_unregister_device(dev->input);
  146. dev->input = NULL;
  147. }
  148. EXPORT_SYMBOL(input_unregister_polled_device);