rc-ir-raw.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 "rc-core-priv.h"
  20. /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
  21. static LIST_HEAD(ir_raw_client_list);
  22. /* Used to handle IR raw handler extensions */
  23. static DEFINE_MUTEX(ir_raw_handler_lock);
  24. static LIST_HEAD(ir_raw_handler_list);
  25. static atomic64_t available_protocols = ATOMIC64_INIT(0);
  26. static int ir_raw_event_thread(void *data)
  27. {
  28. struct ir_raw_event ev;
  29. struct ir_raw_handler *handler;
  30. struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
  31. while (1) {
  32. mutex_lock(&ir_raw_handler_lock);
  33. while (kfifo_out(&raw->kfifo, &ev, 1)) {
  34. list_for_each_entry(handler, &ir_raw_handler_list, list)
  35. if (raw->dev->enabled_protocols &
  36. handler->protocols || !handler->protocols)
  37. handler->decode(raw->dev, ev);
  38. raw->prev_ev = ev;
  39. }
  40. mutex_unlock(&ir_raw_handler_lock);
  41. set_current_state(TASK_INTERRUPTIBLE);
  42. if (kthread_should_stop()) {
  43. __set_current_state(TASK_RUNNING);
  44. break;
  45. } else if (!kfifo_is_empty(&raw->kfifo))
  46. set_current_state(TASK_RUNNING);
  47. schedule();
  48. }
  49. return 0;
  50. }
  51. /**
  52. * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
  53. * @dev: the struct rc_dev device descriptor
  54. * @ev: the struct ir_raw_event descriptor of the pulse/space
  55. *
  56. * This routine (which may be called from an interrupt context) stores a
  57. * pulse/space duration for the raw ir decoding state machines. Pulses are
  58. * signalled as positive values and spaces as negative values. A zero value
  59. * will reset the decoding state machines.
  60. */
  61. int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev)
  62. {
  63. if (!dev->raw)
  64. return -EINVAL;
  65. IR_dprintk(2, "sample: (%05dus %s)\n",
  66. TO_US(ev->duration), TO_STR(ev->pulse));
  67. if (!kfifo_put(&dev->raw->kfifo, *ev)) {
  68. dev_err(&dev->dev, "IR event FIFO is full!\n");
  69. return -ENOSPC;
  70. }
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  74. /**
  75. * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
  76. * @dev: the struct rc_dev device descriptor
  77. * @type: the type of the event that has occurred
  78. *
  79. * This routine (which may be called from an interrupt context) is used to
  80. * store the beginning of an ir pulse or space (or the start/end of ir
  81. * reception) for the raw ir decoding state machines. This is used by
  82. * hardware which does not provide durations directly but only interrupts
  83. * (or similar events) on state change.
  84. */
  85. int ir_raw_event_store_edge(struct rc_dev *dev, enum raw_event_type type)
  86. {
  87. ktime_t now;
  88. s64 delta; /* ns */
  89. DEFINE_IR_RAW_EVENT(ev);
  90. int rc = 0;
  91. int delay;
  92. if (!dev->raw)
  93. return -EINVAL;
  94. now = ktime_get();
  95. delta = ktime_to_ns(ktime_sub(now, dev->raw->last_event));
  96. delay = MS_TO_NS(dev->input_dev->rep[REP_DELAY]);
  97. /* Check for a long duration since last event or if we're
  98. * being called for the first time, note that delta can't
  99. * possibly be negative.
  100. */
  101. if (delta > delay || !dev->raw->last_type)
  102. type |= IR_START_EVENT;
  103. else
  104. ev.duration = delta;
  105. if (type & IR_START_EVENT)
  106. ir_raw_event_reset(dev);
  107. else if (dev->raw->last_type & IR_SPACE) {
  108. ev.pulse = false;
  109. rc = ir_raw_event_store(dev, &ev);
  110. } else if (dev->raw->last_type & IR_PULSE) {
  111. ev.pulse = true;
  112. rc = ir_raw_event_store(dev, &ev);
  113. } else
  114. return 0;
  115. dev->raw->last_event = now;
  116. dev->raw->last_type = type;
  117. return rc;
  118. }
  119. EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
  120. /**
  121. * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
  122. * @dev: the struct rc_dev device descriptor
  123. * @type: the type of the event that has occurred
  124. *
  125. * This routine (which may be called from an interrupt context) works
  126. * in similar manner to ir_raw_event_store_edge.
  127. * This routine is intended for devices with limited internal buffer
  128. * It automerges samples of same type, and handles timeouts. Returns non-zero
  129. * if the event was added, and zero if the event was ignored due to idle
  130. * processing.
  131. */
  132. int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev)
  133. {
  134. if (!dev->raw)
  135. return -EINVAL;
  136. /* Ignore spaces in idle mode */
  137. if (dev->idle && !ev->pulse)
  138. return 0;
  139. else if (dev->idle)
  140. ir_raw_event_set_idle(dev, false);
  141. if (!dev->raw->this_ev.duration)
  142. dev->raw->this_ev = *ev;
  143. else if (ev->pulse == dev->raw->this_ev.pulse)
  144. dev->raw->this_ev.duration += ev->duration;
  145. else {
  146. ir_raw_event_store(dev, &dev->raw->this_ev);
  147. dev->raw->this_ev = *ev;
  148. }
  149. /* Enter idle mode if nessesary */
  150. if (!ev->pulse && dev->timeout &&
  151. dev->raw->this_ev.duration >= dev->timeout)
  152. ir_raw_event_set_idle(dev, true);
  153. return 1;
  154. }
  155. EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
  156. /**
  157. * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
  158. * @dev: the struct rc_dev device descriptor
  159. * @idle: whether the device is idle or not
  160. */
  161. void ir_raw_event_set_idle(struct rc_dev *dev, bool idle)
  162. {
  163. if (!dev->raw)
  164. return;
  165. IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave");
  166. if (idle) {
  167. dev->raw->this_ev.timeout = true;
  168. ir_raw_event_store(dev, &dev->raw->this_ev);
  169. init_ir_raw_event(&dev->raw->this_ev);
  170. }
  171. if (dev->s_idle)
  172. dev->s_idle(dev, idle);
  173. dev->idle = idle;
  174. }
  175. EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
  176. /**
  177. * ir_raw_event_handle() - schedules the decoding of stored ir data
  178. * @dev: the struct rc_dev device descriptor
  179. *
  180. * This routine will tell rc-core to start decoding stored ir data.
  181. */
  182. void ir_raw_event_handle(struct rc_dev *dev)
  183. {
  184. if (!dev->raw)
  185. return;
  186. wake_up_process(dev->raw->thread);
  187. }
  188. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  189. /* used internally by the sysfs interface */
  190. u64
  191. ir_raw_get_allowed_protocols(void)
  192. {
  193. return atomic64_read(&available_protocols);
  194. }
  195. static int change_protocol(struct rc_dev *dev, u64 *rc_type)
  196. {
  197. /* the caller will update dev->enabled_protocols */
  198. return 0;
  199. }
  200. static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols)
  201. {
  202. mutex_lock(&dev->lock);
  203. dev->enabled_protocols &= ~protocols;
  204. mutex_unlock(&dev->lock);
  205. }
  206. /**
  207. * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation.
  208. * @ev: Pointer to pointer to next free event. *@ev is incremented for
  209. * each raw event filled.
  210. * @max: Maximum number of raw events to fill.
  211. * @timings: Manchester modulation timings.
  212. * @n: Number of bits of data.
  213. * @data: Data bits to encode.
  214. *
  215. * Encodes the @n least significant bits of @data using Manchester (bi-phase)
  216. * modulation with the timing characteristics described by @timings, writing up
  217. * to @max raw IR events using the *@ev pointer.
  218. *
  219. * Returns: 0 on success.
  220. * -ENOBUFS if there isn't enough space in the array to fit the
  221. * full encoded data. In this case all @max events will have been
  222. * written.
  223. */
  224. int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
  225. const struct ir_raw_timings_manchester *timings,
  226. unsigned int n, u64 data)
  227. {
  228. bool need_pulse;
  229. u64 i;
  230. int ret = -ENOBUFS;
  231. i = BIT_ULL(n - 1);
  232. if (timings->leader) {
  233. if (!max--)
  234. return ret;
  235. if (timings->pulse_space_start) {
  236. init_ir_raw_event_duration((*ev)++, 1, timings->leader);
  237. if (!max--)
  238. return ret;
  239. init_ir_raw_event_duration((*ev), 0, timings->leader);
  240. } else {
  241. init_ir_raw_event_duration((*ev), 1, timings->leader);
  242. }
  243. i >>= 1;
  244. } else {
  245. /* continue existing signal */
  246. --(*ev);
  247. }
  248. /* from here on *ev will point to the last event rather than the next */
  249. while (n && i > 0) {
  250. need_pulse = !(data & i);
  251. if (timings->invert)
  252. need_pulse = !need_pulse;
  253. if (need_pulse == !!(*ev)->pulse) {
  254. (*ev)->duration += timings->clock;
  255. } else {
  256. if (!max--)
  257. goto nobufs;
  258. init_ir_raw_event_duration(++(*ev), need_pulse,
  259. timings->clock);
  260. }
  261. if (!max--)
  262. goto nobufs;
  263. init_ir_raw_event_duration(++(*ev), !need_pulse,
  264. timings->clock);
  265. i >>= 1;
  266. }
  267. if (timings->trailer_space) {
  268. if (!(*ev)->pulse)
  269. (*ev)->duration += timings->trailer_space;
  270. else if (!max--)
  271. goto nobufs;
  272. else
  273. init_ir_raw_event_duration(++(*ev), 0,
  274. timings->trailer_space);
  275. }
  276. ret = 0;
  277. nobufs:
  278. /* point to the next event rather than last event before returning */
  279. ++(*ev);
  280. return ret;
  281. }
  282. EXPORT_SYMBOL(ir_raw_gen_manchester);
  283. /**
  284. * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation.
  285. * @ev: Pointer to pointer to next free event. *@ev is incremented for
  286. * each raw event filled.
  287. * @max: Maximum number of raw events to fill.
  288. * @timings: Pulse distance modulation timings.
  289. * @n: Number of bits of data.
  290. * @data: Data bits to encode.
  291. *
  292. * Encodes the @n least significant bits of @data using pulse-distance
  293. * modulation with the timing characteristics described by @timings, writing up
  294. * to @max raw IR events using the *@ev pointer.
  295. *
  296. * Returns: 0 on success.
  297. * -ENOBUFS if there isn't enough space in the array to fit the
  298. * full encoded data. In this case all @max events will have been
  299. * written.
  300. */
  301. int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
  302. const struct ir_raw_timings_pd *timings,
  303. unsigned int n, u64 data)
  304. {
  305. int i;
  306. int ret;
  307. unsigned int space;
  308. if (timings->header_pulse) {
  309. ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse,
  310. timings->header_space);
  311. if (ret)
  312. return ret;
  313. }
  314. if (timings->msb_first) {
  315. for (i = n - 1; i >= 0; --i) {
  316. space = timings->bit_space[(data >> i) & 1];
  317. ret = ir_raw_gen_pulse_space(ev, &max,
  318. timings->bit_pulse,
  319. space);
  320. if (ret)
  321. return ret;
  322. }
  323. } else {
  324. for (i = 0; i < n; ++i, data >>= 1) {
  325. space = timings->bit_space[data & 1];
  326. ret = ir_raw_gen_pulse_space(ev, &max,
  327. timings->bit_pulse,
  328. space);
  329. if (ret)
  330. return ret;
  331. }
  332. }
  333. ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse,
  334. timings->trailer_space);
  335. return ret;
  336. }
  337. EXPORT_SYMBOL(ir_raw_gen_pd);
  338. /**
  339. * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation.
  340. * @ev: Pointer to pointer to next free event. *@ev is incremented for
  341. * each raw event filled.
  342. * @max: Maximum number of raw events to fill.
  343. * @timings: Pulse distance modulation timings.
  344. * @n: Number of bits of data.
  345. * @data: Data bits to encode.
  346. *
  347. * Encodes the @n least significant bits of @data using space-distance
  348. * modulation with the timing characteristics described by @timings, writing up
  349. * to @max raw IR events using the *@ev pointer.
  350. *
  351. * Returns: 0 on success.
  352. * -ENOBUFS if there isn't enough space in the array to fit the
  353. * full encoded data. In this case all @max events will have been
  354. * written.
  355. */
  356. int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
  357. const struct ir_raw_timings_pl *timings,
  358. unsigned int n, u64 data)
  359. {
  360. int i;
  361. int ret = -ENOBUFS;
  362. unsigned int pulse;
  363. if (!max--)
  364. return ret;
  365. init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse);
  366. if (timings->msb_first) {
  367. for (i = n - 1; i >= 0; --i) {
  368. if (!max--)
  369. return ret;
  370. init_ir_raw_event_duration((*ev)++, 0,
  371. timings->bit_space);
  372. if (!max--)
  373. return ret;
  374. pulse = timings->bit_pulse[(data >> i) & 1];
  375. init_ir_raw_event_duration((*ev)++, 1, pulse);
  376. }
  377. } else {
  378. for (i = 0; i < n; ++i, data >>= 1) {
  379. if (!max--)
  380. return ret;
  381. init_ir_raw_event_duration((*ev)++, 0,
  382. timings->bit_space);
  383. if (!max--)
  384. return ret;
  385. pulse = timings->bit_pulse[data & 1];
  386. init_ir_raw_event_duration((*ev)++, 1, pulse);
  387. }
  388. }
  389. if (!max--)
  390. return ret;
  391. init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space);
  392. return 0;
  393. }
  394. EXPORT_SYMBOL(ir_raw_gen_pl);
  395. /**
  396. * ir_raw_encode_scancode() - Encode a scancode as raw events
  397. *
  398. * @protocol: protocol
  399. * @scancode: scancode filter describing a single scancode
  400. * @events: array of raw events to write into
  401. * @max: max number of raw events
  402. *
  403. * Attempts to encode the scancode as raw events.
  404. *
  405. * Returns: The number of events written.
  406. * -ENOBUFS if there isn't enough space in the array to fit the
  407. * encoding. In this case all @max events will have been written.
  408. * -EINVAL if the scancode is ambiguous or invalid, or if no
  409. * compatible encoder was found.
  410. */
  411. int ir_raw_encode_scancode(enum rc_type protocol, u32 scancode,
  412. struct ir_raw_event *events, unsigned int max)
  413. {
  414. struct ir_raw_handler *handler;
  415. int ret = -EINVAL;
  416. u64 mask = 1ULL << protocol;
  417. mutex_lock(&ir_raw_handler_lock);
  418. list_for_each_entry(handler, &ir_raw_handler_list, list) {
  419. if (handler->protocols & mask && handler->encode) {
  420. ret = handler->encode(protocol, scancode, events, max);
  421. if (ret >= 0 || ret == -ENOBUFS)
  422. break;
  423. }
  424. }
  425. mutex_unlock(&ir_raw_handler_lock);
  426. return ret;
  427. }
  428. EXPORT_SYMBOL(ir_raw_encode_scancode);
  429. /*
  430. * Used to (un)register raw event clients
  431. */
  432. int ir_raw_event_register(struct rc_dev *dev)
  433. {
  434. int rc;
  435. struct ir_raw_handler *handler;
  436. if (!dev)
  437. return -EINVAL;
  438. dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL);
  439. if (!dev->raw)
  440. return -ENOMEM;
  441. dev->raw->dev = dev;
  442. dev->change_protocol = change_protocol;
  443. INIT_KFIFO(dev->raw->kfifo);
  444. /*
  445. * raw transmitters do not need any event registration
  446. * because the event is coming from userspace
  447. */
  448. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  449. dev->raw->thread = kthread_run(ir_raw_event_thread, dev->raw,
  450. "rc%u", dev->minor);
  451. if (IS_ERR(dev->raw->thread)) {
  452. rc = PTR_ERR(dev->raw->thread);
  453. goto out;
  454. }
  455. }
  456. mutex_lock(&ir_raw_handler_lock);
  457. list_add_tail(&dev->raw->list, &ir_raw_client_list);
  458. list_for_each_entry(handler, &ir_raw_handler_list, list)
  459. if (handler->raw_register)
  460. handler->raw_register(dev);
  461. mutex_unlock(&ir_raw_handler_lock);
  462. return 0;
  463. out:
  464. kfree(dev->raw);
  465. dev->raw = NULL;
  466. return rc;
  467. }
  468. void ir_raw_event_unregister(struct rc_dev *dev)
  469. {
  470. struct ir_raw_handler *handler;
  471. if (!dev || !dev->raw)
  472. return;
  473. kthread_stop(dev->raw->thread);
  474. mutex_lock(&ir_raw_handler_lock);
  475. list_del(&dev->raw->list);
  476. list_for_each_entry(handler, &ir_raw_handler_list, list)
  477. if (handler->raw_unregister)
  478. handler->raw_unregister(dev);
  479. mutex_unlock(&ir_raw_handler_lock);
  480. kfree(dev->raw);
  481. dev->raw = NULL;
  482. }
  483. /*
  484. * Extension interface - used to register the IR decoders
  485. */
  486. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  487. {
  488. struct ir_raw_event_ctrl *raw;
  489. mutex_lock(&ir_raw_handler_lock);
  490. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  491. if (ir_raw_handler->raw_register)
  492. list_for_each_entry(raw, &ir_raw_client_list, list)
  493. ir_raw_handler->raw_register(raw->dev);
  494. atomic64_or(ir_raw_handler->protocols, &available_protocols);
  495. mutex_unlock(&ir_raw_handler_lock);
  496. return 0;
  497. }
  498. EXPORT_SYMBOL(ir_raw_handler_register);
  499. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  500. {
  501. struct ir_raw_event_ctrl *raw;
  502. u64 protocols = ir_raw_handler->protocols;
  503. mutex_lock(&ir_raw_handler_lock);
  504. list_del(&ir_raw_handler->list);
  505. list_for_each_entry(raw, &ir_raw_client_list, list) {
  506. ir_raw_disable_protocols(raw->dev, protocols);
  507. if (ir_raw_handler->raw_unregister)
  508. ir_raw_handler->raw_unregister(raw->dev);
  509. }
  510. atomic64_andnot(protocols, &available_protocols);
  511. mutex_unlock(&ir_raw_handler_lock);
  512. }
  513. EXPORT_SYMBOL(ir_raw_handler_unregister);