ir-raw.c 9.3 KB

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