ir-raw.c 9.3 KB

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