ir-imon-decoder.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 415662 /* ns */
  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. struct lirc_scancode lsc = {
  64. .scancode = imon->bits,
  65. .rc_proto = RC_PROTO_IMON,
  66. };
  67. ir_lirc_scancode_event(dev, &lsc);
  68. input_event(imon->idev, EV_MSC, MSC_SCAN, imon->bits);
  69. input_report_rel(imon->idev, REL_X, rel_x);
  70. input_report_rel(imon->idev, REL_Y, rel_y);
  71. input_report_key(imon->idev, BTN_LEFT,
  72. (imon->bits & 0x00010000) != 0);
  73. input_report_key(imon->idev, BTN_RIGHT,
  74. (imon->bits & 0x00040000) != 0);
  75. input_sync(imon->idev);
  76. return;
  77. }
  78. }
  79. rc_keydown(dev, RC_PROTO_IMON, imon->bits, 0);
  80. }
  81. /**
  82. * ir_imon_decode() - Decode one iMON pulse or space
  83. * @dev: the struct rc_dev descriptor of the device
  84. * @ev: the struct ir_raw_event descriptor of the pulse/space
  85. *
  86. * This function returns -EINVAL if the pulse violates the state machine
  87. */
  88. static int ir_imon_decode(struct rc_dev *dev, struct ir_raw_event ev)
  89. {
  90. struct imon_dec *data = &dev->raw->imon;
  91. if (!is_timing_event(ev)) {
  92. if (ev.reset)
  93. data->state = STATE_INACTIVE;
  94. return 0;
  95. }
  96. dev_dbg(&dev->dev,
  97. "iMON decode started at state %d bitno %d (%uus %s)\n",
  98. data->state, data->count, TO_US(ev.duration),
  99. TO_STR(ev.pulse));
  100. /*
  101. * Since iMON protocol is a series of bits, if at any point
  102. * we encounter an error, make sure that any remaining bits
  103. * aren't parsed as a scancode made up of less bits.
  104. *
  105. * Note that if the stick is held, then the remote repeats
  106. * the scancode with about 12ms between them. So, make sure
  107. * we have at least 10ms of space after an error. That way,
  108. * we're at a new scancode.
  109. */
  110. if (data->state == STATE_ERROR) {
  111. if (!ev.pulse && ev.duration > MS_TO_NS(10))
  112. data->state = STATE_INACTIVE;
  113. return 0;
  114. }
  115. for (;;) {
  116. if (!geq_margin(ev.duration, IMON_UNIT, IMON_UNIT / 2))
  117. return 0;
  118. decrease_duration(&ev, IMON_UNIT);
  119. switch (data->state) {
  120. case STATE_INACTIVE:
  121. if (ev.pulse) {
  122. data->state = STATE_BIT_CHK;
  123. data->bits = 0;
  124. data->count = IMON_BITS;
  125. }
  126. break;
  127. case STATE_BIT_CHK:
  128. if (IMON_CHKBITS & BIT(data->count))
  129. data->last_chk = ev.pulse;
  130. else if (ev.pulse)
  131. goto err_out;
  132. data->state = STATE_BIT_START;
  133. break;
  134. case STATE_BIT_START:
  135. data->bits <<= 1;
  136. if (!ev.pulse)
  137. data->bits |= 1;
  138. if (IMON_CHKBITS & BIT(data->count)) {
  139. if (data->last_chk != !(data->bits & 3))
  140. goto err_out;
  141. }
  142. if (!data->count--)
  143. data->state = STATE_FINISHED;
  144. else
  145. data->state = STATE_BIT_CHK;
  146. break;
  147. case STATE_FINISHED:
  148. if (ev.pulse)
  149. goto err_out;
  150. ir_imon_decode_scancode(dev);
  151. data->state = STATE_INACTIVE;
  152. break;
  153. }
  154. }
  155. err_out:
  156. dev_dbg(&dev->dev,
  157. "iMON decode failed at state %d bitno %d (%uus %s)\n",
  158. data->state, data->count, TO_US(ev.duration),
  159. TO_STR(ev.pulse));
  160. data->state = STATE_ERROR;
  161. return -EINVAL;
  162. }
  163. /**
  164. * ir_imon_encode() - Encode a scancode as a stream of raw events
  165. *
  166. * @protocol: protocol to encode
  167. * @scancode: scancode to encode
  168. * @events: array of raw ir events to write into
  169. * @max: maximum size of @events
  170. *
  171. * Returns: The number of events written.
  172. * -ENOBUFS if there isn't enough space in the array to fit the
  173. * encoding. In this case all @max events will have been written.
  174. */
  175. static int ir_imon_encode(enum rc_proto protocol, u32 scancode,
  176. struct ir_raw_event *events, unsigned int max)
  177. {
  178. struct ir_raw_event *e = events;
  179. int i, pulse;
  180. if (!max--)
  181. return -ENOBUFS;
  182. init_ir_raw_event_duration(e, 1, IMON_UNIT);
  183. for (i = IMON_BITS; i >= 0; i--) {
  184. if (BIT(i) & IMON_CHKBITS)
  185. pulse = !(scancode & (BIT(i) | BIT(i + 1)));
  186. else
  187. pulse = 0;
  188. if (pulse == e->pulse) {
  189. e->duration += IMON_UNIT;
  190. } else {
  191. if (!max--)
  192. return -ENOBUFS;
  193. init_ir_raw_event_duration(++e, pulse, IMON_UNIT);
  194. }
  195. pulse = !(scancode & BIT(i));
  196. if (pulse == e->pulse) {
  197. e->duration += IMON_UNIT;
  198. } else {
  199. if (!max--)
  200. return -ENOBUFS;
  201. init_ir_raw_event_duration(++e, pulse, IMON_UNIT);
  202. }
  203. }
  204. if (e->pulse)
  205. e++;
  206. return e - events;
  207. }
  208. static int ir_imon_register(struct rc_dev *dev)
  209. {
  210. struct input_dev *idev;
  211. struct imon_dec *imon = &dev->raw->imon;
  212. int ret;
  213. idev = input_allocate_device();
  214. if (!idev)
  215. return -ENOMEM;
  216. snprintf(imon->name, sizeof(imon->name),
  217. "iMON PAD Stick (%s)", dev->device_name);
  218. idev->name = imon->name;
  219. idev->phys = dev->input_phys;
  220. /* Mouse bits */
  221. set_bit(EV_REL, idev->evbit);
  222. set_bit(EV_KEY, idev->evbit);
  223. set_bit(REL_X, idev->relbit);
  224. set_bit(REL_Y, idev->relbit);
  225. set_bit(BTN_LEFT, idev->keybit);
  226. set_bit(BTN_RIGHT, idev->keybit);
  227. /* Report scancodes too */
  228. set_bit(EV_MSC, idev->evbit);
  229. set_bit(MSC_SCAN, idev->mscbit);
  230. input_set_drvdata(idev, imon);
  231. ret = input_register_device(idev);
  232. if (ret < 0) {
  233. input_free_device(idev);
  234. return -EIO;
  235. }
  236. imon->idev = idev;
  237. imon->stick_keyboard = false;
  238. return 0;
  239. }
  240. static int ir_imon_unregister(struct rc_dev *dev)
  241. {
  242. struct imon_dec *imon = &dev->raw->imon;
  243. input_unregister_device(imon->idev);
  244. imon->idev = NULL;
  245. return 0;
  246. }
  247. static struct ir_raw_handler imon_handler = {
  248. .protocols = RC_PROTO_BIT_IMON,
  249. .decode = ir_imon_decode,
  250. .encode = ir_imon_encode,
  251. .carrier = 38000,
  252. .raw_register = ir_imon_register,
  253. .raw_unregister = ir_imon_unregister,
  254. .min_timeout = IMON_UNIT * IMON_BITS * 2,
  255. };
  256. static int __init ir_imon_decode_init(void)
  257. {
  258. ir_raw_handler_register(&imon_handler);
  259. pr_info("IR iMON protocol handler initialized\n");
  260. return 0;
  261. }
  262. static void __exit ir_imon_decode_exit(void)
  263. {
  264. ir_raw_handler_unregister(&imon_handler);
  265. }
  266. module_init(ir_imon_decode_init);
  267. module_exit(ir_imon_decode_exit);
  268. MODULE_LICENSE("GPL");
  269. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  270. MODULE_DESCRIPTION("iMON IR protocol decoder");