rc-ir-raw.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. /* 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 DEFINE_MUTEX(available_protocols_lock);
  27. static u64 available_protocols;
  28. static int ir_raw_event_thread(void *data)
  29. {
  30. struct ir_raw_event ev;
  31. struct ir_raw_handler *handler;
  32. struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
  33. while (!kthread_should_stop()) {
  34. spin_lock_irq(&raw->lock);
  35. if (!kfifo_len(&raw->kfifo)) {
  36. set_current_state(TASK_INTERRUPTIBLE);
  37. if (kthread_should_stop())
  38. set_current_state(TASK_RUNNING);
  39. spin_unlock_irq(&raw->lock);
  40. schedule();
  41. continue;
  42. }
  43. if(!kfifo_out(&raw->kfifo, &ev, 1))
  44. dev_err(&raw->dev->dev, "IR event FIFO is empty!\n");
  45. spin_unlock_irq(&raw->lock);
  46. mutex_lock(&ir_raw_handler_lock);
  47. list_for_each_entry(handler, &ir_raw_handler_list, list)
  48. if (raw->dev->enabled_protocols & handler->protocols ||
  49. !handler->protocols)
  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_put(&dev->raw->kfifo, *ev)) {
  73. dev_err(&dev->dev, "IR event FIFO is full!\n");
  74. return -ENOSPC;
  75. }
  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. Returns non-zero
  134. * if the event was added, and zero if the event was ignored due to idle
  135. * processing.
  136. */
  137. int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev)
  138. {
  139. if (!dev->raw)
  140. return -EINVAL;
  141. /* Ignore spaces in idle mode */
  142. if (dev->idle && !ev->pulse)
  143. return 0;
  144. else if (dev->idle)
  145. ir_raw_event_set_idle(dev, false);
  146. if (!dev->raw->this_ev.duration)
  147. dev->raw->this_ev = *ev;
  148. else if (ev->pulse == dev->raw->this_ev.pulse)
  149. dev->raw->this_ev.duration += ev->duration;
  150. else {
  151. ir_raw_event_store(dev, &dev->raw->this_ev);
  152. dev->raw->this_ev = *ev;
  153. }
  154. /* Enter idle mode if nessesary */
  155. if (!ev->pulse && dev->timeout &&
  156. dev->raw->this_ev.duration >= dev->timeout)
  157. ir_raw_event_set_idle(dev, true);
  158. return 1;
  159. }
  160. EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
  161. /**
  162. * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
  163. * @dev: the struct rc_dev device descriptor
  164. * @idle: whether the device is idle or not
  165. */
  166. void ir_raw_event_set_idle(struct rc_dev *dev, bool idle)
  167. {
  168. if (!dev->raw)
  169. return;
  170. IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave");
  171. if (idle) {
  172. dev->raw->this_ev.timeout = true;
  173. ir_raw_event_store(dev, &dev->raw->this_ev);
  174. init_ir_raw_event(&dev->raw->this_ev);
  175. }
  176. if (dev->s_idle)
  177. dev->s_idle(dev, idle);
  178. dev->idle = idle;
  179. }
  180. EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
  181. /**
  182. * ir_raw_event_handle() - schedules the decoding of stored ir data
  183. * @dev: the struct rc_dev device descriptor
  184. *
  185. * This routine will tell rc-core to start decoding stored ir data.
  186. */
  187. void ir_raw_event_handle(struct rc_dev *dev)
  188. {
  189. unsigned long flags;
  190. if (!dev->raw)
  191. return;
  192. spin_lock_irqsave(&dev->raw->lock, flags);
  193. wake_up_process(dev->raw->thread);
  194. spin_unlock_irqrestore(&dev->raw->lock, flags);
  195. }
  196. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  197. /* used internally by the sysfs interface */
  198. u64
  199. ir_raw_get_allowed_protocols(void)
  200. {
  201. u64 protocols;
  202. mutex_lock(&available_protocols_lock);
  203. protocols = available_protocols;
  204. mutex_unlock(&available_protocols_lock);
  205. return protocols;
  206. }
  207. static int change_protocol(struct rc_dev *dev, u64 *rc_type)
  208. {
  209. /* the caller will update dev->enabled_protocols */
  210. return 0;
  211. }
  212. static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols)
  213. {
  214. mutex_lock(&dev->lock);
  215. dev->enabled_protocols &= ~protocols;
  216. dev->enabled_wakeup_protocols &= ~protocols;
  217. mutex_unlock(&dev->lock);
  218. }
  219. /*
  220. * Used to (un)register raw event clients
  221. */
  222. int ir_raw_event_register(struct rc_dev *dev)
  223. {
  224. int rc;
  225. struct ir_raw_handler *handler;
  226. if (!dev)
  227. return -EINVAL;
  228. dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL);
  229. if (!dev->raw)
  230. return -ENOMEM;
  231. dev->raw->dev = dev;
  232. dev->change_protocol = change_protocol;
  233. INIT_KFIFO(dev->raw->kfifo);
  234. spin_lock_init(&dev->raw->lock);
  235. dev->raw->thread = kthread_run(ir_raw_event_thread, dev->raw,
  236. "rc%u", dev->minor);
  237. if (IS_ERR(dev->raw->thread)) {
  238. rc = PTR_ERR(dev->raw->thread);
  239. goto out;
  240. }
  241. mutex_lock(&ir_raw_handler_lock);
  242. list_add_tail(&dev->raw->list, &ir_raw_client_list);
  243. list_for_each_entry(handler, &ir_raw_handler_list, list)
  244. if (handler->raw_register)
  245. handler->raw_register(dev);
  246. mutex_unlock(&ir_raw_handler_lock);
  247. return 0;
  248. out:
  249. kfree(dev->raw);
  250. dev->raw = NULL;
  251. return rc;
  252. }
  253. void ir_raw_event_unregister(struct rc_dev *dev)
  254. {
  255. struct ir_raw_handler *handler;
  256. if (!dev || !dev->raw)
  257. return;
  258. kthread_stop(dev->raw->thread);
  259. mutex_lock(&ir_raw_handler_lock);
  260. list_del(&dev->raw->list);
  261. list_for_each_entry(handler, &ir_raw_handler_list, list)
  262. if (handler->raw_unregister)
  263. handler->raw_unregister(dev);
  264. mutex_unlock(&ir_raw_handler_lock);
  265. kfree(dev->raw);
  266. dev->raw = NULL;
  267. }
  268. /*
  269. * Extension interface - used to register the IR decoders
  270. */
  271. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  272. {
  273. struct ir_raw_event_ctrl *raw;
  274. mutex_lock(&ir_raw_handler_lock);
  275. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  276. if (ir_raw_handler->raw_register)
  277. list_for_each_entry(raw, &ir_raw_client_list, list)
  278. ir_raw_handler->raw_register(raw->dev);
  279. mutex_lock(&available_protocols_lock);
  280. available_protocols |= ir_raw_handler->protocols;
  281. mutex_unlock(&available_protocols_lock);
  282. mutex_unlock(&ir_raw_handler_lock);
  283. return 0;
  284. }
  285. EXPORT_SYMBOL(ir_raw_handler_register);
  286. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  287. {
  288. struct ir_raw_event_ctrl *raw;
  289. u64 protocols = ir_raw_handler->protocols;
  290. mutex_lock(&ir_raw_handler_lock);
  291. list_del(&ir_raw_handler->list);
  292. list_for_each_entry(raw, &ir_raw_client_list, list) {
  293. ir_raw_disable_protocols(raw->dev, protocols);
  294. if (ir_raw_handler->raw_unregister)
  295. ir_raw_handler->raw_unregister(raw->dev);
  296. }
  297. mutex_lock(&available_protocols_lock);
  298. available_protocols &= ~protocols;
  299. mutex_unlock(&available_protocols_lock);
  300. mutex_unlock(&ir_raw_handler_lock);
  301. }
  302. EXPORT_SYMBOL(ir_raw_handler_unregister);