ir-raw-event.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/kthread.h>
  15. #include <linux/mutex.h>
  16. #include <linux/sched.h>
  17. #include <linux/freezer.h>
  18. #include "ir-core-priv.h"
  19. /* Define the max number of pulse/space transitions to buffer */
  20. #define MAX_IR_EVENT_SIZE 512
  21. /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
  22. static LIST_HEAD(ir_raw_client_list);
  23. /* Used to handle IR raw handler extensions */
  24. static DEFINE_MUTEX(ir_raw_handler_lock);
  25. static LIST_HEAD(ir_raw_handler_list);
  26. static u64 available_protocols;
  27. #ifdef MODULE
  28. /* Used to load the decoders */
  29. static struct work_struct wq_load;
  30. #endif
  31. static int ir_raw_event_thread(void *data)
  32. {
  33. struct ir_raw_event ev;
  34. struct ir_raw_handler *handler;
  35. struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
  36. while (!kthread_should_stop()) {
  37. try_to_freeze();
  38. mutex_lock(&ir_raw_handler_lock);
  39. while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev)) {
  40. list_for_each_entry(handler, &ir_raw_handler_list, list)
  41. handler->decode(raw->input_dev, ev);
  42. raw->prev_ev = ev;
  43. }
  44. mutex_unlock(&ir_raw_handler_lock);
  45. set_current_state(TASK_INTERRUPTIBLE);
  46. schedule();
  47. }
  48. return 0;
  49. }
  50. /**
  51. * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
  52. * @input_dev: the struct input_dev device descriptor
  53. * @ev: the struct ir_raw_event descriptor of the pulse/space
  54. *
  55. * This routine (which may be called from an interrupt context) stores a
  56. * pulse/space duration for the raw ir decoding state machines. Pulses are
  57. * signalled as positive values and spaces as negative values. A zero value
  58. * will reset the decoding state machines.
  59. */
  60. int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
  61. {
  62. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  63. if (!ir->raw)
  64. return -EINVAL;
  65. IR_dprintk(2, "sample: (05%dus %s)\n",
  66. TO_US(ev->duration), TO_STR(ev->pulse));
  67. if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
  68. return -ENOMEM;
  69. return 0;
  70. }
  71. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  72. /**
  73. * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
  74. * @input_dev: the struct input_dev device descriptor
  75. * @type: the type of the event that has occurred
  76. *
  77. * This routine (which may be called from an interrupt context) is used to
  78. * store the beginning of an ir pulse or space (or the start/end of ir
  79. * reception) for the raw ir decoding state machines. This is used by
  80. * hardware which does not provide durations directly but only interrupts
  81. * (or similar events) on state change.
  82. */
  83. int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
  84. {
  85. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  86. ktime_t now;
  87. s64 delta; /* ns */
  88. struct ir_raw_event ev;
  89. int rc = 0;
  90. if (!ir->raw)
  91. return -EINVAL;
  92. now = ktime_get();
  93. delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
  94. /* Check for a long duration since last event or if we're
  95. * being called for the first time, note that delta can't
  96. * possibly be negative.
  97. */
  98. ev.duration = 0;
  99. if (delta > IR_MAX_DURATION || !ir->raw->last_type)
  100. type |= IR_START_EVENT;
  101. else
  102. ev.duration = delta;
  103. if (type & IR_START_EVENT)
  104. ir_raw_event_reset(input_dev);
  105. else if (ir->raw->last_type & IR_SPACE) {
  106. ev.pulse = false;
  107. rc = ir_raw_event_store(input_dev, &ev);
  108. } else if (ir->raw->last_type & IR_PULSE) {
  109. ev.pulse = true;
  110. rc = ir_raw_event_store(input_dev, &ev);
  111. } else
  112. return 0;
  113. ir->raw->last_event = now;
  114. ir->raw->last_type = type;
  115. return rc;
  116. }
  117. EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
  118. /**
  119. * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
  120. * @input_dev: the struct input_dev device descriptor
  121. * @type: the type of the event that has occurred
  122. *
  123. * This routine (which may be called from an interrupt context) works
  124. * in similiar manner to ir_raw_event_store_edge.
  125. * This routine is intended for devices with limited internal buffer
  126. * It automerges samples of same type, and handles timeouts
  127. */
  128. int ir_raw_event_store_with_filter(struct input_dev *input_dev,
  129. struct ir_raw_event *ev)
  130. {
  131. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  132. struct ir_raw_event_ctrl *raw = ir->raw;
  133. if (!raw || !ir->props)
  134. return -EINVAL;
  135. /* Ignore spaces in idle mode */
  136. if (ir->idle && !ev->pulse)
  137. return 0;
  138. else if (ir->idle)
  139. ir_raw_event_set_idle(input_dev, 0);
  140. if (!raw->this_ev.duration) {
  141. raw->this_ev = *ev;
  142. } else if (ev->pulse == raw->this_ev.pulse) {
  143. raw->this_ev.duration += ev->duration;
  144. } else {
  145. ir_raw_event_store(input_dev, &raw->this_ev);
  146. raw->this_ev = *ev;
  147. }
  148. /* Enter idle mode if nessesary */
  149. if (!ev->pulse && ir->props->timeout &&
  150. raw->this_ev.duration >= ir->props->timeout)
  151. ir_raw_event_set_idle(input_dev, 1);
  152. return 0;
  153. }
  154. EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
  155. void ir_raw_event_set_idle(struct input_dev *input_dev, int idle)
  156. {
  157. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  158. struct ir_raw_event_ctrl *raw = ir->raw;
  159. ktime_t now;
  160. u64 delta;
  161. if (!ir->props)
  162. return;
  163. if (!ir->raw)
  164. goto out;
  165. if (idle) {
  166. IR_dprintk(2, "enter idle mode\n");
  167. raw->last_event = ktime_get();
  168. } else {
  169. IR_dprintk(2, "exit idle mode\n");
  170. now = ktime_get();
  171. delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
  172. WARN_ON(raw->this_ev.pulse);
  173. raw->this_ev.duration =
  174. min(raw->this_ev.duration + delta,
  175. (u64)IR_MAX_DURATION);
  176. ir_raw_event_store(input_dev, &raw->this_ev);
  177. if (raw->this_ev.duration == IR_MAX_DURATION)
  178. ir_raw_event_reset(input_dev);
  179. raw->this_ev.duration = 0;
  180. }
  181. out:
  182. if (ir->props->s_idle)
  183. ir->props->s_idle(ir->props->priv, idle);
  184. ir->idle = idle;
  185. }
  186. EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
  187. /**
  188. * ir_raw_event_handle() - schedules the decoding of stored ir data
  189. * @input_dev: the struct input_dev device descriptor
  190. *
  191. * This routine will signal the workqueue to start decoding stored ir data.
  192. */
  193. void ir_raw_event_handle(struct input_dev *input_dev)
  194. {
  195. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  196. if (!ir->raw)
  197. return;
  198. wake_up_process(ir->raw->thread);
  199. }
  200. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  201. /* used internally by the sysfs interface */
  202. u64
  203. ir_raw_get_allowed_protocols()
  204. {
  205. u64 protocols;
  206. mutex_lock(&ir_raw_handler_lock);
  207. protocols = available_protocols;
  208. mutex_unlock(&ir_raw_handler_lock);
  209. return protocols;
  210. }
  211. /*
  212. * Used to (un)register raw event clients
  213. */
  214. int ir_raw_event_register(struct input_dev *input_dev)
  215. {
  216. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  217. int rc;
  218. struct ir_raw_handler *handler;
  219. ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
  220. if (!ir->raw)
  221. return -ENOMEM;
  222. ir->raw->input_dev = input_dev;
  223. ir->raw->enabled_protocols = ~0;
  224. rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
  225. GFP_KERNEL);
  226. if (rc < 0) {
  227. kfree(ir->raw);
  228. ir->raw = NULL;
  229. return rc;
  230. }
  231. ir->raw->thread = kthread_run(ir_raw_event_thread, ir->raw,
  232. "rc%u", (unsigned int)ir->devno);
  233. if (IS_ERR(ir->raw->thread)) {
  234. kfree(ir->raw);
  235. ir->raw = NULL;
  236. return PTR_ERR(ir->raw->thread);
  237. }
  238. mutex_lock(&ir_raw_handler_lock);
  239. list_add_tail(&ir->raw->list, &ir_raw_client_list);
  240. list_for_each_entry(handler, &ir_raw_handler_list, list)
  241. if (handler->raw_register)
  242. handler->raw_register(ir->raw->input_dev);
  243. mutex_unlock(&ir_raw_handler_lock);
  244. return 0;
  245. }
  246. void ir_raw_event_unregister(struct input_dev *input_dev)
  247. {
  248. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  249. struct ir_raw_handler *handler;
  250. if (!ir->raw)
  251. return;
  252. kthread_stop(ir->raw->thread);
  253. mutex_lock(&ir_raw_handler_lock);
  254. list_del(&ir->raw->list);
  255. list_for_each_entry(handler, &ir_raw_handler_list, list)
  256. if (handler->raw_unregister)
  257. handler->raw_unregister(ir->raw->input_dev);
  258. mutex_unlock(&ir_raw_handler_lock);
  259. kfifo_free(&ir->raw->kfifo);
  260. kfree(ir->raw);
  261. ir->raw = NULL;
  262. }
  263. /*
  264. * Extension interface - used to register the IR decoders
  265. */
  266. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  267. {
  268. struct ir_raw_event_ctrl *raw;
  269. mutex_lock(&ir_raw_handler_lock);
  270. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  271. if (ir_raw_handler->raw_register)
  272. list_for_each_entry(raw, &ir_raw_client_list, list)
  273. ir_raw_handler->raw_register(raw->input_dev);
  274. available_protocols |= ir_raw_handler->protocols;
  275. mutex_unlock(&ir_raw_handler_lock);
  276. return 0;
  277. }
  278. EXPORT_SYMBOL(ir_raw_handler_register);
  279. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  280. {
  281. struct ir_raw_event_ctrl *raw;
  282. mutex_lock(&ir_raw_handler_lock);
  283. list_del(&ir_raw_handler->list);
  284. if (ir_raw_handler->raw_unregister)
  285. list_for_each_entry(raw, &ir_raw_client_list, list)
  286. ir_raw_handler->raw_unregister(raw->input_dev);
  287. available_protocols &= ~ir_raw_handler->protocols;
  288. mutex_unlock(&ir_raw_handler_lock);
  289. }
  290. EXPORT_SYMBOL(ir_raw_handler_unregister);
  291. #ifdef MODULE
  292. static void init_decoders(struct work_struct *work)
  293. {
  294. /* Load the decoder modules */
  295. load_nec_decode();
  296. load_rc5_decode();
  297. load_rc6_decode();
  298. load_jvc_decode();
  299. load_sony_decode();
  300. load_lirc_codec();
  301. /* If needed, we may later add some init code. In this case,
  302. it is needed to change the CONFIG_MODULE test at ir-core.h
  303. */
  304. }
  305. #endif
  306. void ir_raw_init(void)
  307. {
  308. #ifdef MODULE
  309. INIT_WORK(&wq_load, init_decoders);
  310. schedule_work(&wq_load);
  311. #endif
  312. }