ir-raw-event.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. static void ir_keyup_timer(unsigned long data)
  50. {
  51. struct input_dev *input_dev = (struct input_dev *)data;
  52. ir_keyup(input_dev);
  53. }
  54. int ir_raw_event_register(struct input_dev *input_dev)
  55. {
  56. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  57. int rc, size;
  58. ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
  59. if (!ir->raw)
  60. return -ENOMEM;
  61. size = sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE * 2;
  62. size = roundup_pow_of_two(size);
  63. init_timer(&ir->raw->timer_keyup);
  64. ir->raw->timer_keyup.function = ir_keyup_timer;
  65. ir->raw->timer_keyup.data = (unsigned long)input_dev;
  66. set_bit(EV_REP, input_dev->evbit);
  67. rc = kfifo_alloc(&ir->raw->kfifo, size, GFP_KERNEL);
  68. if (rc < 0) {
  69. kfree(ir->raw);
  70. ir->raw = NULL;
  71. return rc;
  72. }
  73. rc = RUN_DECODER(raw_register, input_dev);
  74. if (rc < 0) {
  75. kfifo_free(&ir->raw->kfifo);
  76. kfree(ir->raw);
  77. ir->raw = NULL;
  78. return rc;
  79. }
  80. return rc;
  81. }
  82. EXPORT_SYMBOL_GPL(ir_raw_event_register);
  83. void ir_raw_event_unregister(struct input_dev *input_dev)
  84. {
  85. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  86. if (!ir->raw)
  87. return;
  88. del_timer_sync(&ir->raw->timer_keyup);
  89. RUN_DECODER(raw_unregister, input_dev);
  90. kfifo_free(&ir->raw->kfifo);
  91. kfree(ir->raw);
  92. ir->raw = NULL;
  93. }
  94. EXPORT_SYMBOL_GPL(ir_raw_event_unregister);
  95. int ir_raw_event_store(struct input_dev *input_dev, enum raw_event_type type)
  96. {
  97. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  98. struct timespec ts;
  99. struct ir_raw_event event;
  100. int rc;
  101. if (!ir->raw)
  102. return -EINVAL;
  103. event.type = type;
  104. event.delta.tv_sec = 0;
  105. event.delta.tv_nsec = 0;
  106. ktime_get_ts(&ts);
  107. if (timespec_equal(&ir->raw->last_event, &event.delta))
  108. event.type |= IR_START_EVENT;
  109. else
  110. event.delta = timespec_sub(ts, ir->raw->last_event);
  111. memcpy(&ir->raw->last_event, &ts, sizeof(ts));
  112. if (event.delta.tv_sec) {
  113. event.type |= IR_START_EVENT;
  114. event.delta.tv_sec = 0;
  115. event.delta.tv_nsec = 0;
  116. }
  117. kfifo_in(&ir->raw->kfifo, &event, sizeof(event));
  118. return rc;
  119. }
  120. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  121. int ir_raw_event_handle(struct input_dev *input_dev)
  122. {
  123. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  124. int rc;
  125. struct ir_raw_event *evs;
  126. int len, i;
  127. /*
  128. * Store the events into a temporary buffer. This allows calling more than
  129. * one decoder to deal with the received data
  130. */
  131. len = kfifo_len(&ir->raw->kfifo) / sizeof(*evs);
  132. if (!len)
  133. return 0;
  134. evs = kmalloc(len * sizeof(*evs), GFP_ATOMIC);
  135. for (i = 0; i < len; i++) {
  136. rc = kfifo_out(&ir->raw->kfifo, &evs[i], sizeof(*evs));
  137. if (rc != sizeof(*evs)) {
  138. IR_dprintk(1, "overflow error: received %d instead of %zd\n",
  139. rc, sizeof(*evs));
  140. return -EINVAL;
  141. }
  142. IR_dprintk(2, "event type %d, time before event: %07luus\n",
  143. evs[i].type, (evs[i].delta.tv_nsec + 500) / 1000);
  144. }
  145. /*
  146. * Call all ir decoders. This allows decoding the same event with
  147. * more than one protocol handler. It returns the number of keystrokes
  148. * sent to the event interface
  149. */
  150. rc = RUN_DECODER(decode, input_dev, evs, len);
  151. kfree(evs);
  152. return rc;
  153. }
  154. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  155. /*
  156. * Extension interface - used to register the IR decoders
  157. */
  158. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  159. {
  160. spin_lock(&ir_raw_handler_lock);
  161. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  162. spin_unlock(&ir_raw_handler_lock);
  163. return 0;
  164. }
  165. EXPORT_SYMBOL(ir_raw_handler_register);
  166. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  167. {
  168. spin_lock(&ir_raw_handler_lock);
  169. list_del(&ir_raw_handler->list);
  170. spin_unlock(&ir_raw_handler_lock);
  171. }
  172. EXPORT_SYMBOL(ir_raw_handler_unregister);
  173. static void init_decoders(struct work_struct *work)
  174. {
  175. /* Load the decoder modules */
  176. load_nec_decode();
  177. /* If needed, we may later add some init code. In this case,
  178. it is needed to change the CONFIG_MODULE test at ir-core.h
  179. */
  180. }
  181. void ir_raw_init(void)
  182. {
  183. spin_lock_init(&ir_raw_handler_lock);
  184. #ifdef MODULE
  185. INIT_WORK(&wq_load, init_decoders);
  186. schedule_work(&wq_load);
  187. #endif
  188. }