ir-raw.c 9.3 KB

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