input-polldev.c 4.3 KB

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