ir-imon-decoder.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // ir-imon-decoder.c - handle iMon protocol
  3. //
  4. // Copyright (C) 2018 by Sean Young <sean@mess.org>
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/module.h>
  7. #include "rc-core-priv.h"
  8. #define IMON_UNIT 416 /* us */
  9. #define IMON_BITS 30
  10. #define IMON_CHKBITS (BIT(30) | BIT(25) | BIT(24) | BIT(22) | \
  11. BIT(21) | BIT(20) | BIT(19) | BIT(18) | \
  12. BIT(17) | BIT(16) | BIT(14) | BIT(13) | \
  13. BIT(12) | BIT(11) | BIT(10) | BIT(9))
  14. /*
  15. * This protocol has 30 bits. The format is one IMON_UNIT header pulse,
  16. * followed by 30 bits. Each bit is one IMON_UNIT check field, and then
  17. * one IMON_UNIT field with the actual bit (1=space, 0=pulse).
  18. * The check field is always space for some bits, for others it is pulse if
  19. * both the preceding and current bit are zero, else space. IMON_CHKBITS
  20. * defines which bits are of type check.
  21. *
  22. * There is no way to distinguish an incomplete message from one where
  23. * the lower bits are all set, iow. the last pulse is for the lowest
  24. * bit which is 0.
  25. */
  26. enum imon_state {
  27. STATE_INACTIVE,
  28. STATE_BIT_CHK,
  29. STATE_BIT_START,
  30. STATE_FINISHED,
  31. STATE_ERROR,
  32. };
  33. static void ir_imon_decode_scancode(struct rc_dev *dev)
  34. {
  35. struct imon_dec *imon = &dev->raw->imon;
  36. /* Keyboard/Mouse toggle */
  37. if (imon->bits == 0x299115b7)
  38. imon->stick_keyboard = !imon->stick_keyboard;
  39. if ((imon->bits & 0xfc0000ff) == 0x680000b7) {
  40. int rel_x, rel_y;
  41. u8 buf;
  42. buf = imon->bits >> 16;
  43. rel_x = (buf & 0x08) | (buf & 0x10) >> 2 |
  44. (buf & 0x20) >> 4 | (buf & 0x40) >> 6;
  45. if (imon->bits & 0x02000000)
  46. rel_x |= ~0x0f;
  47. buf = imon->bits >> 8;
  48. rel_y = (buf & 0x08) | (buf & 0x10) >> 2 |
  49. (buf & 0x20) >> 4 | (buf & 0x40) >> 6;
  50. if (imon->bits & 0x01000000)
  51. rel_y |= ~0x0f;
  52. if (rel_x && rel_y && imon->stick_keyboard) {
  53. if (abs(rel_y) > abs(rel_x))
  54. imon->bits = rel_y > 0 ?
  55. 0x289515b7 : /* KEY_DOWN */
  56. 0x2aa515b7; /* KEY_UP */
  57. else
  58. imon->bits = rel_x > 0 ?
  59. 0x2ba515b7 : /* KEY_RIGHT */
  60. 0x29a515b7; /* KEY_LEFT */
  61. }
  62. if (!imon->stick_keyboard) {
  63. input_report_rel(dev->input_dev, REL_X, rel_x);
  64. input_report_rel(dev->input_dev, REL_Y, rel_y);
  65. input_report_key(dev->input_dev, BTN_LEFT,
  66. (imon->bits & 0x00010000) != 0);
  67. input_report_key(dev->input_dev, BTN_RIGHT,
  68. (imon->bits & 0x00040000) != 0);
  69. }
  70. }
  71. rc_keydown(dev, RC_PROTO_IMON, imon->bits, 0);
  72. }
  73. /**
  74. * ir_imon_decode() - Decode one iMON pulse or space
  75. * @dev: the struct rc_dev descriptor of the device
  76. * @ev: the struct ir_raw_event descriptor of the pulse/space
  77. *
  78. * This function returns -EINVAL if the pulse violates the state machine
  79. */
  80. static int ir_imon_decode(struct rc_dev *dev, struct ir_raw_event ev)
  81. {
  82. struct imon_dec *data = &dev->raw->imon;
  83. if (!is_timing_event(ev)) {
  84. if (ev.reset)
  85. data->state = STATE_INACTIVE;
  86. return 0;
  87. }
  88. dev_dbg(&dev->dev,
  89. "iMON decode started at state %d bitno %d (%uus %s)\n",
  90. data->state, data->count, ev.duration, TO_STR(ev.pulse));
  91. /*
  92. * Since iMON protocol is a series of bits, if at any point
  93. * we encounter an error, make sure that any remaining bits
  94. * aren't parsed as a scancode made up of less bits.
  95. *
  96. * Note that if the stick is held, then the remote repeats
  97. * the scancode with about 12ms between them. So, make sure
  98. * we have at least 10ms of space after an error. That way,
  99. * we're at a new scancode.
  100. */
  101. if (data->state == STATE_ERROR) {
  102. if (!ev.pulse && ev.duration > MS_TO_US(10))
  103. data->state = STATE_INACTIVE;
  104. return 0;
  105. }
  106. for (;;) {
  107. if (!geq_margin(ev.duration, IMON_UNIT, IMON_UNIT / 2))
  108. return 0;
  109. decrease_duration(&ev, IMON_UNIT);
  110. switch (data->state) {
  111. case STATE_INACTIVE:
  112. if (ev.pulse) {
  113. data->state = STATE_BIT_CHK;
  114. data->bits = 0;
  115. data->count = IMON_BITS;
  116. }
  117. break;
  118. case STATE_BIT_CHK:
  119. if (IMON_CHKBITS & BIT(data->count))
  120. data->last_chk = ev.pulse;
  121. else if (ev.pulse)
  122. goto err_out;
  123. data->state = STATE_BIT_START;
  124. break;
  125. case STATE_BIT_START:
  126. data->bits <<= 1;
  127. if (!ev.pulse)
  128. data->bits |= 1;
  129. if (IMON_CHKBITS & BIT(data->count)) {
  130. if (data->last_chk != !(data->bits & 3))
  131. goto err_out;
  132. }
  133. if (!data->count--)
  134. data->state = STATE_FINISHED;
  135. else
  136. data->state = STATE_BIT_CHK;
  137. break;
  138. case STATE_FINISHED:
  139. if (ev.pulse)
  140. goto err_out;
  141. ir_imon_decode_scancode(dev);
  142. data->state = STATE_INACTIVE;
  143. break;
  144. }
  145. }
  146. err_out:
  147. dev_dbg(&dev->dev,
  148. "iMON decode failed at state %d bitno %d (%uus %s)\n",
  149. data->state, data->count, ev.duration, TO_STR(ev.pulse));
  150. data->state = STATE_ERROR;
  151. return -EINVAL;
  152. }
  153. /**
  154. * ir_imon_encode() - Encode a scancode as a stream of raw events
  155. *
  156. * @protocol: protocol to encode
  157. * @scancode: scancode to encode
  158. * @events: array of raw ir events to write into
  159. * @max: maximum size of @events
  160. *
  161. * Returns: The number of events written.
  162. * -ENOBUFS if there isn't enough space in the array to fit the
  163. * encoding. In this case all @max events will have been written.
  164. */
  165. static int ir_imon_encode(enum rc_proto protocol, u32 scancode,
  166. struct ir_raw_event *events, unsigned int max)
  167. {
  168. struct ir_raw_event *e = events;
  169. int i, pulse;
  170. if (!max--)
  171. return -ENOBUFS;
  172. init_ir_raw_event_duration(e, 1, IMON_UNIT);
  173. for (i = IMON_BITS; i >= 0; i--) {
  174. if (BIT(i) & IMON_CHKBITS)
  175. pulse = !(scancode & (BIT(i) | BIT(i + 1)));
  176. else
  177. pulse = 0;
  178. if (pulse == e->pulse) {
  179. e->duration += IMON_UNIT;
  180. } else {
  181. if (!max--)
  182. return -ENOBUFS;
  183. init_ir_raw_event_duration(++e, pulse, IMON_UNIT);
  184. }
  185. pulse = !(scancode & BIT(i));
  186. if (pulse == e->pulse) {
  187. e->duration += IMON_UNIT;
  188. } else {
  189. if (!max--)
  190. return -ENOBUFS;
  191. init_ir_raw_event_duration(++e, pulse, IMON_UNIT);
  192. }
  193. }
  194. if (e->pulse)
  195. e++;
  196. return e - events;
  197. }
  198. static int ir_imon_register(struct rc_dev *dev)
  199. {
  200. struct imon_dec *imon = &dev->raw->imon;
  201. imon->stick_keyboard = false;
  202. return 0;
  203. }
  204. static struct ir_raw_handler imon_handler = {
  205. .protocols = RC_PROTO_BIT_IMON,
  206. .decode = ir_imon_decode,
  207. .encode = ir_imon_encode,
  208. .carrier = 38000,
  209. .raw_register = ir_imon_register,
  210. .min_timeout = IMON_UNIT * IMON_BITS * 2,
  211. };
  212. static int __init ir_imon_decode_init(void)
  213. {
  214. ir_raw_handler_register(&imon_handler);
  215. pr_info("IR iMON protocol handler initialized\n");
  216. return 0;
  217. }
  218. static void __exit ir_imon_decode_exit(void)
  219. {
  220. ir_raw_handler_unregister(&imon_handler);
  221. }
  222. module_init(ir_imon_decode_init);
  223. module_exit(ir_imon_decode_exit);
  224. MODULE_LICENSE("GPL");
  225. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  226. MODULE_DESCRIPTION("iMON IR protocol decoder");