ir-raw-event.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include <linux/sched.h>
  18. /* Define the max number of pulse/space transitions to buffer */
  19. #define MAX_IR_EVENT_SIZE 512
  20. /* Used to handle IR raw handler extensions */
  21. static LIST_HEAD(ir_raw_handler_list);
  22. static DEFINE_SPINLOCK(ir_raw_handler_lock);
  23. /**
  24. * RUN_DECODER() - runs an operation on all IR decoders
  25. * @ops: IR raw handler operation to be called
  26. * @arg: arguments to be passed to the callback
  27. *
  28. * Calls ir_raw_handler::ops for all registered IR handlers. It prevents
  29. * new decode addition/removal while running, by locking ir_raw_handler_lock
  30. * mutex. If an error occurs, it stops the ops. Otherwise, it returns a sum
  31. * of the return codes.
  32. */
  33. #define RUN_DECODER(ops, ...) ({ \
  34. struct ir_raw_handler *_ir_raw_handler; \
  35. int _sumrc = 0, _rc; \
  36. spin_lock(&ir_raw_handler_lock); \
  37. list_for_each_entry(_ir_raw_handler, &ir_raw_handler_list, list) { \
  38. if (_ir_raw_handler->ops) { \
  39. _rc = _ir_raw_handler->ops(__VA_ARGS__); \
  40. if (_rc < 0) \
  41. break; \
  42. _sumrc += _rc; \
  43. } \
  44. } \
  45. spin_unlock(&ir_raw_handler_lock); \
  46. _sumrc; \
  47. })
  48. /* Used to load the decoders */
  49. static struct work_struct wq_load;
  50. static void ir_raw_event_work(struct work_struct *work)
  51. {
  52. s64 d;
  53. struct ir_raw_event_ctrl *raw =
  54. container_of(work, struct ir_raw_event_ctrl, rx_work);
  55. while (kfifo_out(&raw->kfifo, &d, sizeof(d)) == sizeof(d))
  56. RUN_DECODER(decode, raw->input_dev, d);
  57. }
  58. int ir_raw_event_register(struct input_dev *input_dev)
  59. {
  60. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  61. int rc;
  62. ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
  63. if (!ir->raw)
  64. return -ENOMEM;
  65. ir->raw->input_dev = input_dev;
  66. INIT_WORK(&ir->raw->rx_work, ir_raw_event_work);
  67. rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
  68. GFP_KERNEL);
  69. if (rc < 0) {
  70. kfree(ir->raw);
  71. ir->raw = NULL;
  72. return rc;
  73. }
  74. rc = RUN_DECODER(raw_register, input_dev);
  75. if (rc < 0) {
  76. kfifo_free(&ir->raw->kfifo);
  77. kfree(ir->raw);
  78. ir->raw = NULL;
  79. return rc;
  80. }
  81. return rc;
  82. }
  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. cancel_work_sync(&ir->raw->rx_work);
  89. RUN_DECODER(raw_unregister, input_dev);
  90. kfifo_free(&ir->raw->kfifo);
  91. kfree(ir->raw);
  92. ir->raw = NULL;
  93. }
  94. /**
  95. * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
  96. * @input_dev: the struct input_dev device descriptor
  97. * @duration: duration of the pulse or space in ns
  98. *
  99. * This routine (which may be called from an interrupt context) stores a
  100. * pulse/space duration for the raw ir decoding state machines. Pulses are
  101. * signalled as positive values and spaces as negative values. A zero value
  102. * will reset the decoding state machines.
  103. */
  104. int ir_raw_event_store(struct input_dev *input_dev, s64 duration)
  105. {
  106. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  107. if (!ir->raw)
  108. return -EINVAL;
  109. if (kfifo_in(&ir->raw->kfifo, &duration, sizeof(duration)) != sizeof(duration))
  110. return -ENOMEM;
  111. return 0;
  112. }
  113. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  114. /**
  115. * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
  116. * @input_dev: the struct input_dev device descriptor
  117. * @type: the type of the event that has occurred
  118. *
  119. * This routine (which may be called from an interrupt context) is used to
  120. * store the beginning of an ir pulse or space (or the start/end of ir
  121. * reception) for the raw ir decoding state machines. This is used by
  122. * hardware which does not provide durations directly but only interrupts
  123. * (or similar events) on state change.
  124. */
  125. int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
  126. {
  127. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  128. ktime_t now;
  129. s64 delta; /* ns */
  130. int rc = 0;
  131. if (!ir->raw)
  132. return -EINVAL;
  133. now = ktime_get();
  134. delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
  135. /* Check for a long duration since last event or if we're
  136. * being called for the first time, note that delta can't
  137. * possibly be negative.
  138. */
  139. if (delta > NSEC_PER_SEC || !ir->raw->last_type)
  140. type |= IR_START_EVENT;
  141. if (type & IR_START_EVENT)
  142. ir_raw_event_reset(input_dev);
  143. else if (ir->raw->last_type & IR_SPACE)
  144. rc = ir_raw_event_store(input_dev, -delta);
  145. else if (ir->raw->last_type & IR_PULSE)
  146. rc = ir_raw_event_store(input_dev, delta);
  147. else
  148. return 0;
  149. ir->raw->last_event = now;
  150. ir->raw->last_type = type;
  151. return rc;
  152. }
  153. EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
  154. /**
  155. * ir_raw_event_handle() - schedules the decoding of stored ir data
  156. * @input_dev: the struct input_dev device descriptor
  157. *
  158. * This routine will signal the workqueue to start decoding stored ir data.
  159. */
  160. void ir_raw_event_handle(struct input_dev *input_dev)
  161. {
  162. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  163. if (!ir->raw)
  164. return;
  165. schedule_work(&ir->raw->rx_work);
  166. }
  167. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  168. /*
  169. * Extension interface - used to register the IR decoders
  170. */
  171. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  172. {
  173. spin_lock(&ir_raw_handler_lock);
  174. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  175. spin_unlock(&ir_raw_handler_lock);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(ir_raw_handler_register);
  179. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  180. {
  181. spin_lock(&ir_raw_handler_lock);
  182. list_del(&ir_raw_handler->list);
  183. spin_unlock(&ir_raw_handler_lock);
  184. }
  185. EXPORT_SYMBOL(ir_raw_handler_unregister);
  186. static void init_decoders(struct work_struct *work)
  187. {
  188. /* Load the decoder modules */
  189. load_nec_decode();
  190. load_rc5_decode();
  191. /* If needed, we may later add some init code. In this case,
  192. it is needed to change the CONFIG_MODULE test at ir-core.h
  193. */
  194. }
  195. void ir_raw_init(void)
  196. {
  197. #ifdef MODULE
  198. INIT_WORK(&wq_load, init_decoders);
  199. schedule_work(&wq_load);
  200. #endif
  201. }