ir-raw-event.c 9.9 KB

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