ir-raw-event.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 DEFINE_SPINLOCK(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. void ir_raw_event_unregister(struct input_dev *input_dev)
  74. {
  75. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  76. if (!ir->raw)
  77. return;
  78. RUN_DECODER(raw_unregister, input_dev);
  79. kfifo_free(&ir->raw->kfifo);
  80. kfree(ir->raw);
  81. ir->raw = NULL;
  82. }
  83. int ir_raw_event_store(struct input_dev *input_dev, enum raw_event_type type)
  84. {
  85. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  86. struct timespec ts;
  87. struct ir_raw_event event;
  88. int rc;
  89. if (!ir->raw)
  90. return -EINVAL;
  91. event.type = type;
  92. event.delta.tv_sec = 0;
  93. event.delta.tv_nsec = 0;
  94. ktime_get_ts(&ts);
  95. if (timespec_equal(&ir->raw->last_event, &event.delta))
  96. event.type |= IR_START_EVENT;
  97. else
  98. event.delta = timespec_sub(ts, ir->raw->last_event);
  99. memcpy(&ir->raw->last_event, &ts, sizeof(ts));
  100. if (event.delta.tv_sec) {
  101. event.type |= IR_START_EVENT;
  102. event.delta.tv_sec = 0;
  103. event.delta.tv_nsec = 0;
  104. }
  105. kfifo_in(&ir->raw->kfifo, &event, sizeof(event));
  106. return rc;
  107. }
  108. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  109. int ir_raw_event_handle(struct input_dev *input_dev)
  110. {
  111. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  112. int rc;
  113. struct ir_raw_event ev;
  114. int len, i;
  115. /*
  116. * Store the events into a temporary buffer. This allows calling more than
  117. * one decoder to deal with the received data
  118. */
  119. len = kfifo_len(&ir->raw->kfifo) / sizeof(ev);
  120. if (!len)
  121. return 0;
  122. for (i = 0; i < len; i++) {
  123. rc = kfifo_out(&ir->raw->kfifo, &ev, sizeof(ev));
  124. if (rc != sizeof(ev)) {
  125. IR_dprintk(1, "overflow error: received %d instead of %zd\n",
  126. rc, sizeof(ev));
  127. return -EINVAL;
  128. }
  129. IR_dprintk(2, "event type %d, time before event: %07luus\n",
  130. ev.type, (ev.delta.tv_nsec + 500) / 1000);
  131. rc = RUN_DECODER(decode, input_dev, &ev);
  132. }
  133. /*
  134. * Call all ir decoders. This allows decoding the same event with
  135. * more than one protocol handler.
  136. */
  137. return rc;
  138. }
  139. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  140. /*
  141. * Extension interface - used to register the IR decoders
  142. */
  143. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  144. {
  145. spin_lock(&ir_raw_handler_lock);
  146. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  147. spin_unlock(&ir_raw_handler_lock);
  148. return 0;
  149. }
  150. EXPORT_SYMBOL(ir_raw_handler_register);
  151. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  152. {
  153. spin_lock(&ir_raw_handler_lock);
  154. list_del(&ir_raw_handler->list);
  155. spin_unlock(&ir_raw_handler_lock);
  156. }
  157. EXPORT_SYMBOL(ir_raw_handler_unregister);
  158. static void init_decoders(struct work_struct *work)
  159. {
  160. /* Load the decoder modules */
  161. load_nec_decode();
  162. load_rc5_decode();
  163. /* If needed, we may later add some init code. In this case,
  164. it is needed to change the CONFIG_MODULE test at ir-core.h
  165. */
  166. }
  167. void ir_raw_init(void)
  168. {
  169. #ifdef MODULE
  170. INIT_WORK(&wq_load, init_decoders);
  171. schedule_work(&wq_load);
  172. #endif
  173. }