ir-raw-event.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <linux/workqueue.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/sched.h>
  17. #include "ir-core-priv.h"
  18. /* Define the max number of pulse/space transitions to buffer */
  19. #define MAX_IR_EVENT_SIZE 512
  20. /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
  21. static LIST_HEAD(ir_raw_client_list);
  22. /* Used to handle IR raw handler extensions */
  23. static DEFINE_SPINLOCK(ir_raw_handler_lock);
  24. static LIST_HEAD(ir_raw_handler_list);
  25. static u64 available_protocols;
  26. #ifdef MODULE
  27. /* Used to load the decoders */
  28. static struct work_struct wq_load;
  29. #endif
  30. static void ir_raw_event_work(struct work_struct *work)
  31. {
  32. struct ir_raw_event ev;
  33. struct ir_raw_handler *handler;
  34. struct ir_raw_event_ctrl *raw =
  35. container_of(work, struct ir_raw_event_ctrl, rx_work);
  36. while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev)) {
  37. spin_lock(&ir_raw_handler_lock);
  38. list_for_each_entry(handler, &ir_raw_handler_list, list)
  39. handler->decode(raw->input_dev, ev);
  40. spin_unlock(&ir_raw_handler_lock);
  41. raw->prev_ev = ev;
  42. }
  43. }
  44. /**
  45. * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
  46. * @input_dev: the struct input_dev device descriptor
  47. * @ev: the struct ir_raw_event descriptor of the pulse/space
  48. *
  49. * This routine (which may be called from an interrupt context) stores a
  50. * pulse/space duration for the raw ir decoding state machines. Pulses are
  51. * signalled as positive values and spaces as negative values. A zero value
  52. * will reset the decoding state machines.
  53. */
  54. int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
  55. {
  56. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  57. if (!ir->raw)
  58. return -EINVAL;
  59. IR_dprintk(2, "sample: (05%dus %s)\n",
  60. TO_US(ev->duration), TO_STR(ev->pulse));
  61. if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
  62. return -ENOMEM;
  63. return 0;
  64. }
  65. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  66. /**
  67. * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
  68. * @input_dev: the struct input_dev device descriptor
  69. * @type: the type of the event that has occurred
  70. *
  71. * This routine (which may be called from an interrupt context) is used to
  72. * store the beginning of an ir pulse or space (or the start/end of ir
  73. * reception) for the raw ir decoding state machines. This is used by
  74. * hardware which does not provide durations directly but only interrupts
  75. * (or similar events) on state change.
  76. */
  77. int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
  78. {
  79. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  80. ktime_t now;
  81. s64 delta; /* ns */
  82. struct ir_raw_event ev;
  83. int rc = 0;
  84. if (!ir->raw)
  85. return -EINVAL;
  86. now = ktime_get();
  87. delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
  88. /* Check for a long duration since last event or if we're
  89. * being called for the first time, note that delta can't
  90. * possibly be negative.
  91. */
  92. ev.duration = 0;
  93. if (delta > IR_MAX_DURATION || !ir->raw->last_type)
  94. type |= IR_START_EVENT;
  95. else
  96. ev.duration = delta;
  97. if (type & IR_START_EVENT)
  98. ir_raw_event_reset(input_dev);
  99. else if (ir->raw->last_type & IR_SPACE) {
  100. ev.pulse = false;
  101. rc = ir_raw_event_store(input_dev, &ev);
  102. } else if (ir->raw->last_type & IR_PULSE) {
  103. ev.pulse = true;
  104. rc = ir_raw_event_store(input_dev, &ev);
  105. } else
  106. return 0;
  107. ir->raw->last_event = now;
  108. ir->raw->last_type = type;
  109. return rc;
  110. }
  111. EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
  112. /**
  113. * ir_raw_event_handle() - schedules the decoding of stored ir data
  114. * @input_dev: the struct input_dev device descriptor
  115. *
  116. * This routine will signal the workqueue to start decoding stored ir data.
  117. */
  118. void ir_raw_event_handle(struct input_dev *input_dev)
  119. {
  120. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  121. if (!ir->raw)
  122. return;
  123. schedule_work(&ir->raw->rx_work);
  124. }
  125. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  126. /* used internally by the sysfs interface */
  127. u64
  128. ir_raw_get_allowed_protocols()
  129. {
  130. u64 protocols;
  131. spin_lock(&ir_raw_handler_lock);
  132. protocols = available_protocols;
  133. spin_unlock(&ir_raw_handler_lock);
  134. return protocols;
  135. }
  136. /*
  137. * Used to (un)register raw event clients
  138. */
  139. int ir_raw_event_register(struct input_dev *input_dev)
  140. {
  141. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  142. int rc;
  143. struct ir_raw_handler *handler;
  144. ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
  145. if (!ir->raw)
  146. return -ENOMEM;
  147. ir->raw->input_dev = input_dev;
  148. INIT_WORK(&ir->raw->rx_work, ir_raw_event_work);
  149. ir->raw->enabled_protocols = ~0;
  150. rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
  151. GFP_KERNEL);
  152. if (rc < 0) {
  153. kfree(ir->raw);
  154. ir->raw = NULL;
  155. return rc;
  156. }
  157. spin_lock(&ir_raw_handler_lock);
  158. list_add_tail(&ir->raw->list, &ir_raw_client_list);
  159. list_for_each_entry(handler, &ir_raw_handler_list, list)
  160. if (handler->raw_register)
  161. handler->raw_register(ir->raw->input_dev);
  162. spin_unlock(&ir_raw_handler_lock);
  163. return 0;
  164. }
  165. void ir_raw_event_unregister(struct input_dev *input_dev)
  166. {
  167. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  168. struct ir_raw_handler *handler;
  169. if (!ir->raw)
  170. return;
  171. cancel_work_sync(&ir->raw->rx_work);
  172. spin_lock(&ir_raw_handler_lock);
  173. list_del(&ir->raw->list);
  174. list_for_each_entry(handler, &ir_raw_handler_list, list)
  175. if (handler->raw_unregister)
  176. handler->raw_unregister(ir->raw->input_dev);
  177. spin_unlock(&ir_raw_handler_lock);
  178. kfifo_free(&ir->raw->kfifo);
  179. kfree(ir->raw);
  180. ir->raw = NULL;
  181. }
  182. /*
  183. * Extension interface - used to register the IR decoders
  184. */
  185. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  186. {
  187. struct ir_raw_event_ctrl *raw;
  188. spin_lock(&ir_raw_handler_lock);
  189. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  190. if (ir_raw_handler->raw_register)
  191. list_for_each_entry(raw, &ir_raw_client_list, list)
  192. ir_raw_handler->raw_register(raw->input_dev);
  193. available_protocols |= ir_raw_handler->protocols;
  194. spin_unlock(&ir_raw_handler_lock);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(ir_raw_handler_register);
  198. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  199. {
  200. struct ir_raw_event_ctrl *raw;
  201. spin_lock(&ir_raw_handler_lock);
  202. list_del(&ir_raw_handler->list);
  203. if (ir_raw_handler->raw_unregister)
  204. list_for_each_entry(raw, &ir_raw_client_list, list)
  205. ir_raw_handler->raw_unregister(raw->input_dev);
  206. available_protocols &= ~ir_raw_handler->protocols;
  207. spin_unlock(&ir_raw_handler_lock);
  208. }
  209. EXPORT_SYMBOL(ir_raw_handler_unregister);
  210. #ifdef MODULE
  211. static void init_decoders(struct work_struct *work)
  212. {
  213. /* Load the decoder modules */
  214. load_nec_decode();
  215. load_rc5_decode();
  216. load_rc6_decode();
  217. load_jvc_decode();
  218. load_sony_decode();
  219. load_lirc_codec();
  220. /* If needed, we may later add some init code. In this case,
  221. it is needed to change the CONFIG_MODULE test at ir-core.h
  222. */
  223. }
  224. #endif
  225. void ir_raw_init(void)
  226. {
  227. #ifdef MODULE
  228. INIT_WORK(&wq_load, init_decoders);
  229. schedule_work(&wq_load);
  230. #endif
  231. }