rc-ir-raw.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. if (raw->dev->enabled_protocols & handler->protocols ||
  51. !handler->protocols)
  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. 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(&ir_raw_handler_lock);
  203. protocols = available_protocols;
  204. mutex_unlock(&ir_raw_handler_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. rc = kfifo_alloc(&dev->raw->kfifo,
  234. sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE,
  235. GFP_KERNEL);
  236. if (rc < 0)
  237. goto out;
  238. spin_lock_init(&dev->raw->lock);
  239. dev->raw->thread = kthread_run(ir_raw_event_thread, dev->raw,
  240. "rc%u", dev->minor);
  241. if (IS_ERR(dev->raw->thread)) {
  242. rc = PTR_ERR(dev->raw->thread);
  243. goto out;
  244. }
  245. mutex_lock(&ir_raw_handler_lock);
  246. list_add_tail(&dev->raw->list, &ir_raw_client_list);
  247. list_for_each_entry(handler, &ir_raw_handler_list, list)
  248. if (handler->raw_register)
  249. handler->raw_register(dev);
  250. mutex_unlock(&ir_raw_handler_lock);
  251. return 0;
  252. out:
  253. kfree(dev->raw);
  254. dev->raw = NULL;
  255. return rc;
  256. }
  257. void ir_raw_event_unregister(struct rc_dev *dev)
  258. {
  259. struct ir_raw_handler *handler;
  260. if (!dev || !dev->raw)
  261. return;
  262. kthread_stop(dev->raw->thread);
  263. mutex_lock(&ir_raw_handler_lock);
  264. list_del(&dev->raw->list);
  265. list_for_each_entry(handler, &ir_raw_handler_list, list)
  266. if (handler->raw_unregister)
  267. handler->raw_unregister(dev);
  268. mutex_unlock(&ir_raw_handler_lock);
  269. kfifo_free(&dev->raw->kfifo);
  270. kfree(dev->raw);
  271. dev->raw = NULL;
  272. }
  273. /*
  274. * Extension interface - used to register the IR decoders
  275. */
  276. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  277. {
  278. struct ir_raw_event_ctrl *raw;
  279. mutex_lock(&ir_raw_handler_lock);
  280. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  281. if (ir_raw_handler->raw_register)
  282. list_for_each_entry(raw, &ir_raw_client_list, list)
  283. ir_raw_handler->raw_register(raw->dev);
  284. available_protocols |= ir_raw_handler->protocols;
  285. mutex_unlock(&ir_raw_handler_lock);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL(ir_raw_handler_register);
  289. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  290. {
  291. struct ir_raw_event_ctrl *raw;
  292. u64 protocols = ir_raw_handler->protocols;
  293. mutex_lock(&ir_raw_handler_lock);
  294. list_del(&ir_raw_handler->list);
  295. list_for_each_entry(raw, &ir_raw_client_list, list) {
  296. ir_raw_disable_protocols(raw->dev, protocols);
  297. if (ir_raw_handler->raw_unregister)
  298. ir_raw_handler->raw_unregister(raw->dev);
  299. }
  300. available_protocols &= ~protocols;
  301. mutex_unlock(&ir_raw_handler_lock);
  302. }
  303. EXPORT_SYMBOL(ir_raw_handler_unregister);
  304. void ir_raw_init(void)
  305. {
  306. /* Load the decoder modules */
  307. load_lirc_codec();
  308. /* If needed, we may later add some init code. In this case,
  309. it is needed to change the CONFIG_MODULE test at rc-core.h
  310. */
  311. }