rc-ir-raw.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* rc-ir-raw.c - handle IR pulse/space events
  2. *
  3. * Copyright (C) 2010 by Mauro Carvalho Chehab
  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. static int ir_raw_event_thread(void *data)
  30. {
  31. struct ir_raw_event ev;
  32. struct ir_raw_handler *handler;
  33. struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
  34. int retval;
  35. while (!kthread_should_stop()) {
  36. spin_lock_irq(&raw->lock);
  37. retval = kfifo_len(&raw->kfifo);
  38. if (retval < sizeof(ev)) {
  39. set_current_state(TASK_INTERRUPTIBLE);
  40. if (kthread_should_stop())
  41. set_current_state(TASK_RUNNING);
  42. spin_unlock_irq(&raw->lock);
  43. schedule();
  44. continue;
  45. }
  46. retval = kfifo_out(&raw->kfifo, &ev, sizeof(ev));
  47. spin_unlock_irq(&raw->lock);
  48. mutex_lock(&ir_raw_handler_lock);
  49. list_for_each_entry(handler, &ir_raw_handler_list, list)
  50. handler->decode(raw->dev, ev);
  51. raw->prev_ev = ev;
  52. mutex_unlock(&ir_raw_handler_lock);
  53. }
  54. return 0;
  55. }
  56. /**
  57. * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
  58. * @dev: the struct rc_dev device descriptor
  59. * @ev: the struct ir_raw_event descriptor of the pulse/space
  60. *
  61. * This routine (which may be called from an interrupt context) stores a
  62. * pulse/space duration for the raw ir decoding state machines. Pulses are
  63. * signalled as positive values and spaces as negative values. A zero value
  64. * will reset the decoding state machines.
  65. */
  66. int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev)
  67. {
  68. if (!dev->raw)
  69. return -EINVAL;
  70. IR_dprintk(2, "sample: (%05dus %s)\n",
  71. TO_US(ev->duration), TO_STR(ev->pulse));
  72. if (kfifo_in(&dev->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
  73. return -ENOMEM;
  74. return 0;
  75. }
  76. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  77. /**
  78. * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
  79. * @dev: the struct rc_dev device descriptor
  80. * @type: the type of the event that has occurred
  81. *
  82. * This routine (which may be called from an interrupt context) is used to
  83. * store the beginning of an ir pulse or space (or the start/end of ir
  84. * reception) for the raw ir decoding state machines. This is used by
  85. * hardware which does not provide durations directly but only interrupts
  86. * (or similar events) on state change.
  87. */
  88. int ir_raw_event_store_edge(struct rc_dev *dev, enum raw_event_type type)
  89. {
  90. ktime_t now;
  91. s64 delta; /* ns */
  92. DEFINE_IR_RAW_EVENT(ev);
  93. int rc = 0;
  94. int delay;
  95. if (!dev->raw)
  96. return -EINVAL;
  97. now = ktime_get();
  98. delta = ktime_to_ns(ktime_sub(now, dev->raw->last_event));
  99. delay = MS_TO_NS(dev->input_dev->rep[REP_DELAY]);
  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 > delay || !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 similar 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. Returns non-zero
  132. * if the event was added, and zero if the event was ignored due to idle
  133. * processing.
  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 1;
  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. static int change_protocol(struct rc_dev *dev, u64 *rc_type)
  206. {
  207. /* the caller will update dev->enabled_protocols */
  208. return 0;
  209. }
  210. /*
  211. * Used to (un)register raw event clients
  212. */
  213. int ir_raw_event_register(struct rc_dev *dev)
  214. {
  215. int rc;
  216. struct ir_raw_handler *handler;
  217. if (!dev)
  218. return -EINVAL;
  219. dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL);
  220. if (!dev->raw)
  221. return -ENOMEM;
  222. dev->raw->dev = dev;
  223. dev->change_protocol = change_protocol;
  224. rc = kfifo_alloc(&dev->raw->kfifo,
  225. sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE,
  226. GFP_KERNEL);
  227. if (rc < 0)
  228. goto out;
  229. spin_lock_init(&dev->raw->lock);
  230. dev->raw->thread = kthread_run(ir_raw_event_thread, dev->raw,
  231. "rc%u", dev->minor);
  232. if (IS_ERR(dev->raw->thread)) {
  233. rc = PTR_ERR(dev->raw->thread);
  234. goto out;
  235. }
  236. mutex_lock(&ir_raw_handler_lock);
  237. list_add_tail(&dev->raw->list, &ir_raw_client_list);
  238. list_for_each_entry(handler, &ir_raw_handler_list, list)
  239. if (handler->raw_register)
  240. handler->raw_register(dev);
  241. mutex_unlock(&ir_raw_handler_lock);
  242. return 0;
  243. out:
  244. kfree(dev->raw);
  245. dev->raw = NULL;
  246. return rc;
  247. }
  248. void ir_raw_event_unregister(struct rc_dev *dev)
  249. {
  250. struct ir_raw_handler *handler;
  251. if (!dev || !dev->raw)
  252. return;
  253. kthread_stop(dev->raw->thread);
  254. mutex_lock(&ir_raw_handler_lock);
  255. list_del(&dev->raw->list);
  256. list_for_each_entry(handler, &ir_raw_handler_list, list)
  257. if (handler->raw_unregister)
  258. handler->raw_unregister(dev);
  259. mutex_unlock(&ir_raw_handler_lock);
  260. kfifo_free(&dev->raw->kfifo);
  261. kfree(dev->raw);
  262. dev->raw = NULL;
  263. }
  264. /*
  265. * Extension interface - used to register the IR decoders
  266. */
  267. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  268. {
  269. struct ir_raw_event_ctrl *raw;
  270. mutex_lock(&ir_raw_handler_lock);
  271. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  272. if (ir_raw_handler->raw_register)
  273. list_for_each_entry(raw, &ir_raw_client_list, list)
  274. ir_raw_handler->raw_register(raw->dev);
  275. available_protocols |= ir_raw_handler->protocols;
  276. mutex_unlock(&ir_raw_handler_lock);
  277. return 0;
  278. }
  279. EXPORT_SYMBOL(ir_raw_handler_register);
  280. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  281. {
  282. struct ir_raw_event_ctrl *raw;
  283. mutex_lock(&ir_raw_handler_lock);
  284. list_del(&ir_raw_handler->list);
  285. if (ir_raw_handler->raw_unregister)
  286. list_for_each_entry(raw, &ir_raw_client_list, list)
  287. ir_raw_handler->raw_unregister(raw->dev);
  288. available_protocols &= ~ir_raw_handler->protocols;
  289. mutex_unlock(&ir_raw_handler_lock);
  290. }
  291. EXPORT_SYMBOL(ir_raw_handler_unregister);
  292. void ir_raw_init(void)
  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_sharp_decode();
  302. load_mce_kbd_decode();
  303. load_lirc_codec();
  304. load_xmp_decode();
  305. /* If needed, we may later add some init code. In this case,
  306. it is needed to change the CONFIG_MODULE test at rc-core.h
  307. */
  308. }