ir-xmp-decoder.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* ir-xmp-decoder.c - handle XMP IR Pulse/Space protocol
  2. *
  3. * Copyright (C) 2014 by Marcel Mol
  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. * - Based on info from http://www.hifi-remote.com
  15. * - Ignore Toggle=9 frames
  16. * - Ignore XMP-1 XMP-2 difference, always store 16 bit OBC
  17. */
  18. #include <linux/bitrev.h>
  19. #include <linux/module.h>
  20. #include "rc-core-priv.h"
  21. #define XMP_UNIT 136000 /* ns */
  22. #define XMP_LEADER 210000 /* ns */
  23. #define XMP_NIBBLE_PREFIX 760000 /* ns */
  24. #define XMP_HALFFRAME_SPACE 13800000 /* ns */
  25. #define XMP_TRAILER_SPACE 20000000 /* should be 80ms but not all dureation supliers can go that high */
  26. enum xmp_state {
  27. STATE_INACTIVE,
  28. STATE_LEADER_PULSE,
  29. STATE_NIBBLE_SPACE,
  30. };
  31. /**
  32. * ir_xmp_decode() - Decode one XMP pulse or space
  33. * @dev: the struct rc_dev descriptor of the device
  34. * @ev: the struct ir_raw_event descriptor of the pulse/space
  35. *
  36. * This function returns -EINVAL if the pulse violates the state machine
  37. */
  38. static int ir_xmp_decode(struct rc_dev *dev, struct ir_raw_event ev)
  39. {
  40. struct xmp_dec *data = &dev->raw->xmp;
  41. if (!is_timing_event(ev)) {
  42. if (ev.reset)
  43. data->state = STATE_INACTIVE;
  44. return 0;
  45. }
  46. dev_dbg(&dev->dev, "XMP decode started at state %d %d (%uus %s)\n",
  47. data->state, data->count, TO_US(ev.duration), TO_STR(ev.pulse));
  48. switch (data->state) {
  49. case STATE_INACTIVE:
  50. if (!ev.pulse)
  51. break;
  52. if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2)) {
  53. data->count = 0;
  54. data->state = STATE_NIBBLE_SPACE;
  55. }
  56. return 0;
  57. case STATE_LEADER_PULSE:
  58. if (!ev.pulse)
  59. break;
  60. if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2))
  61. data->state = STATE_NIBBLE_SPACE;
  62. return 0;
  63. case STATE_NIBBLE_SPACE:
  64. if (ev.pulse)
  65. break;
  66. if (geq_margin(ev.duration, XMP_TRAILER_SPACE, XMP_NIBBLE_PREFIX)) {
  67. int divider, i;
  68. u8 addr, subaddr, subaddr2, toggle, oem, obc1, obc2, sum1, sum2;
  69. u32 *n;
  70. u32 scancode;
  71. if (data->count != 16) {
  72. dev_dbg(&dev->dev, "received TRAILER period at index %d: %u\n",
  73. data->count, ev.duration);
  74. data->state = STATE_INACTIVE;
  75. return -EINVAL;
  76. }
  77. n = data->durations;
  78. /*
  79. * the 4th nibble should be 15 so base the divider on this
  80. * to transform durations into nibbles. Substract 2000 from
  81. * the divider to compensate for fluctuations in the signal
  82. */
  83. divider = (n[3] - XMP_NIBBLE_PREFIX) / 15 - 2000;
  84. if (divider < 50) {
  85. dev_dbg(&dev->dev, "divider to small %d.\n",
  86. divider);
  87. data->state = STATE_INACTIVE;
  88. return -EINVAL;
  89. }
  90. /* convert to nibbles and do some sanity checks */
  91. for (i = 0; i < 16; i++)
  92. n[i] = (n[i] - XMP_NIBBLE_PREFIX) / divider;
  93. sum1 = (15 + n[0] + n[1] + n[2] + n[3] +
  94. n[4] + n[5] + n[6] + n[7]) % 16;
  95. sum2 = (15 + n[8] + n[9] + n[10] + n[11] +
  96. n[12] + n[13] + n[14] + n[15]) % 16;
  97. if (sum1 != 15 || sum2 != 15) {
  98. dev_dbg(&dev->dev, "checksum errors sum1=0x%X sum2=0x%X\n",
  99. sum1, sum2);
  100. data->state = STATE_INACTIVE;
  101. return -EINVAL;
  102. }
  103. subaddr = n[0] << 4 | n[2];
  104. subaddr2 = n[8] << 4 | n[11];
  105. oem = n[4] << 4 | n[5];
  106. addr = n[6] << 4 | n[7];
  107. toggle = n[10];
  108. obc1 = n[12] << 4 | n[13];
  109. obc2 = n[14] << 4 | n[15];
  110. if (subaddr != subaddr2) {
  111. dev_dbg(&dev->dev, "subaddress nibbles mismatch 0x%02X != 0x%02X\n",
  112. subaddr, subaddr2);
  113. data->state = STATE_INACTIVE;
  114. return -EINVAL;
  115. }
  116. if (oem != 0x44)
  117. dev_dbg(&dev->dev, "Warning: OEM nibbles 0x%02X. Expected 0x44\n",
  118. oem);
  119. scancode = addr << 24 | subaddr << 16 |
  120. obc1 << 8 | obc2;
  121. dev_dbg(&dev->dev, "XMP scancode 0x%06x\n", scancode);
  122. if (toggle == 0) {
  123. rc_keydown(dev, RC_PROTO_XMP, scancode, 0);
  124. } else {
  125. rc_repeat(dev);
  126. dev_dbg(&dev->dev, "Repeat last key\n");
  127. }
  128. data->state = STATE_INACTIVE;
  129. return 0;
  130. } else if (geq_margin(ev.duration, XMP_HALFFRAME_SPACE, XMP_NIBBLE_PREFIX)) {
  131. /* Expect 8 or 16 nibble pulses. 16 in case of 'final' frame */
  132. if (data->count == 16) {
  133. dev_dbg(&dev->dev, "received half frame pulse at index %d. Probably a final frame key-up event: %u\n",
  134. data->count, ev.duration);
  135. /*
  136. * TODO: for now go back to half frame position
  137. * so trailer can be found and key press
  138. * can be handled.
  139. */
  140. data->count = 8;
  141. }
  142. else if (data->count != 8)
  143. dev_dbg(&dev->dev, "received half frame pulse at index %d: %u\n",
  144. data->count, ev.duration);
  145. data->state = STATE_LEADER_PULSE;
  146. return 0;
  147. } else if (geq_margin(ev.duration, XMP_NIBBLE_PREFIX, XMP_UNIT)) {
  148. /* store nibble raw data, decode after trailer */
  149. if (data->count == 16) {
  150. dev_dbg(&dev->dev, "to many pulses (%d) ignoring: %u\n",
  151. data->count, ev.duration);
  152. data->state = STATE_INACTIVE;
  153. return -EINVAL;
  154. }
  155. data->durations[data->count] = ev.duration;
  156. data->count++;
  157. data->state = STATE_LEADER_PULSE;
  158. return 0;
  159. }
  160. break;
  161. }
  162. dev_dbg(&dev->dev, "XMP decode failed at count %d state %d (%uus %s)\n",
  163. data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  164. data->state = STATE_INACTIVE;
  165. return -EINVAL;
  166. }
  167. static struct ir_raw_handler xmp_handler = {
  168. .protocols = RC_PROTO_BIT_XMP,
  169. .decode = ir_xmp_decode,
  170. .min_timeout = XMP_TRAILER_SPACE,
  171. };
  172. static int __init ir_xmp_decode_init(void)
  173. {
  174. ir_raw_handler_register(&xmp_handler);
  175. printk(KERN_INFO "IR XMP protocol handler initialized\n");
  176. return 0;
  177. }
  178. static void __exit ir_xmp_decode_exit(void)
  179. {
  180. ir_raw_handler_unregister(&xmp_handler);
  181. }
  182. module_init(ir_xmp_decode_init);
  183. module_exit(ir_xmp_decode_exit);
  184. MODULE_LICENSE("GPL");
  185. MODULE_AUTHOR("Marcel Mol <marcel@mesa.nl>");
  186. MODULE_AUTHOR("MESA Consulting (http://www.mesa.nl)");
  187. MODULE_DESCRIPTION("XMP IR protocol decoder");