ir-raw.c 9.5 KB

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