ir-raw-event.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* ir-raw-event.c - handle IR Pulse/Space event
  2. *
  3. * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <media/ir-core.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/spinlock.h>
  17. /* Define the max number of bit transitions per IR keycode */
  18. #define MAX_IR_EVENT_SIZE 256
  19. /* Used to handle IR raw handler extensions */
  20. static LIST_HEAD(ir_raw_handler_list);
  21. static spinlock_t ir_raw_handler_lock;
  22. /**
  23. * RUN_DECODER() - runs an operation on all IR decoders
  24. * @ops: IR raw handler operation to be called
  25. * @arg: arguments to be passed to the callback
  26. *
  27. * Calls ir_raw_handler::ops for all registered IR handlers. It prevents
  28. * new decode addition/removal while running, by locking ir_raw_handler_lock
  29. * mutex. If an error occurs, it stops the ops. Otherwise, it returns a sum
  30. * of the return codes.
  31. */
  32. #define RUN_DECODER(ops, ...) ({ \
  33. struct ir_raw_handler *_ir_raw_handler; \
  34. int _sumrc = 0, _rc; \
  35. spin_lock(&ir_raw_handler_lock); \
  36. list_for_each_entry(_ir_raw_handler, &ir_raw_handler_list, list) { \
  37. if (_ir_raw_handler->ops) { \
  38. _rc = _ir_raw_handler->ops(__VA_ARGS__); \
  39. if (_rc < 0) \
  40. break; \
  41. _sumrc += _rc; \
  42. } \
  43. } \
  44. spin_unlock(&ir_raw_handler_lock); \
  45. _sumrc; \
  46. })
  47. /* Used to load the decoders */
  48. static struct work_struct wq_load;
  49. int ir_raw_event_register(struct input_dev *input_dev)
  50. {
  51. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  52. int rc, size;
  53. ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
  54. if (!ir->raw)
  55. return -ENOMEM;
  56. size = sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE * 2;
  57. size = roundup_pow_of_two(size);
  58. rc = kfifo_alloc(&ir->raw->kfifo, size, GFP_KERNEL);
  59. if (rc < 0) {
  60. kfree(ir->raw);
  61. ir->raw = NULL;
  62. return rc;
  63. }
  64. rc = RUN_DECODER(raw_register, input_dev);
  65. if (rc < 0) {
  66. kfifo_free(&ir->raw->kfifo);
  67. kfree(ir->raw);
  68. ir->raw = NULL;
  69. return rc;
  70. }
  71. return rc;
  72. }
  73. EXPORT_SYMBOL_GPL(ir_raw_event_register);
  74. void ir_raw_event_unregister(struct input_dev *input_dev)
  75. {
  76. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  77. if (!ir->raw)
  78. return;
  79. RUN_DECODER(raw_unregister, input_dev);
  80. kfifo_free(&ir->raw->kfifo);
  81. kfree(ir->raw);
  82. ir->raw = NULL;
  83. }
  84. EXPORT_SYMBOL_GPL(ir_raw_event_unregister);
  85. int ir_raw_event_store(struct input_dev *input_dev, enum raw_event_type type)
  86. {
  87. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  88. struct timespec ts;
  89. struct ir_raw_event event;
  90. int rc;
  91. if (!ir->raw)
  92. return -EINVAL;
  93. event.type = type;
  94. event.delta.tv_sec = 0;
  95. event.delta.tv_nsec = 0;
  96. ktime_get_ts(&ts);
  97. if (timespec_equal(&ir->raw->last_event, &event.delta))
  98. event.type |= IR_START_EVENT;
  99. else
  100. event.delta = timespec_sub(ts, ir->raw->last_event);
  101. memcpy(&ir->raw->last_event, &ts, sizeof(ts));
  102. if (event.delta.tv_sec) {
  103. event.type |= IR_START_EVENT;
  104. event.delta.tv_sec = 0;
  105. event.delta.tv_nsec = 0;
  106. }
  107. kfifo_in(&ir->raw->kfifo, &event, sizeof(event));
  108. return rc;
  109. }
  110. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  111. int ir_raw_event_handle(struct input_dev *input_dev)
  112. {
  113. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  114. int rc;
  115. struct ir_raw_event *evs;
  116. int len, i;
  117. /*
  118. * Store the events into a temporary buffer. This allows calling more than
  119. * one decoder to deal with the received data
  120. */
  121. len = kfifo_len(&ir->raw->kfifo) / sizeof(*evs);
  122. if (!len)
  123. return 0;
  124. evs = kmalloc(len * sizeof(*evs), GFP_ATOMIC);
  125. for (i = 0; i < len; i++) {
  126. rc = kfifo_out(&ir->raw->kfifo, &evs[i], sizeof(*evs));
  127. if (rc != sizeof(*evs)) {
  128. IR_dprintk(1, "overflow error: received %d instead of %zd\n",
  129. rc, sizeof(*evs));
  130. return -EINVAL;
  131. }
  132. IR_dprintk(2, "event type %d, time before event: %07luus\n",
  133. evs[i].type, (evs[i].delta.tv_nsec + 500) / 1000);
  134. }
  135. /*
  136. * Call all ir decoders. This allows decoding the same event with
  137. * more than one protocol handler. It returns the number of keystrokes
  138. * sent to the event interface
  139. */
  140. rc = RUN_DECODER(decode, input_dev, evs, len);
  141. kfree(evs);
  142. return rc;
  143. }
  144. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  145. /*
  146. * Extension interface - used to register the IR decoders
  147. */
  148. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  149. {
  150. spin_lock(&ir_raw_handler_lock);
  151. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  152. spin_unlock(&ir_raw_handler_lock);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL(ir_raw_handler_register);
  156. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  157. {
  158. spin_lock(&ir_raw_handler_lock);
  159. list_del(&ir_raw_handler->list);
  160. spin_unlock(&ir_raw_handler_lock);
  161. }
  162. EXPORT_SYMBOL(ir_raw_handler_unregister);
  163. static void init_decoders(struct work_struct *work)
  164. {
  165. /* Load the decoder modules */
  166. load_nec_decode();
  167. load_rc5_decode();
  168. /* If needed, we may later add some init code. In this case,
  169. it is needed to change the CONFIG_MODULE test at ir-core.h
  170. */
  171. }
  172. void ir_raw_init(void)
  173. {
  174. spin_lock_init(&ir_raw_handler_lock);
  175. #ifdef MODULE
  176. INIT_WORK(&wq_load, init_decoders);
  177. schedule_work(&wq_load);
  178. #endif
  179. }