ir-xmp-decoder.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. * @duration: 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. IR_dprintk(2, "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. IR_dprintk(2, "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. IR_dprintk(2, "divider to small %d.\n", divider);
  86. data->state = STATE_INACTIVE;
  87. return -EINVAL;
  88. }
  89. /* convert to nibbles and do some sanity checks */
  90. for (i = 0; i < 16; i++)
  91. n[i] = (n[i] - XMP_NIBBLE_PREFIX) / divider;
  92. sum1 = (15 + n[0] + n[1] + n[2] + n[3] +
  93. n[4] + n[5] + n[6] + n[7]) % 16;
  94. sum2 = (15 + n[8] + n[9] + n[10] + n[11] +
  95. n[12] + n[13] + n[14] + n[15]) % 16;
  96. if (sum1 != 15 || sum2 != 15) {
  97. IR_dprintk(2, "checksum errors sum1=0x%X sum2=0x%X\n",
  98. sum1, sum2);
  99. data->state = STATE_INACTIVE;
  100. return -EINVAL;
  101. }
  102. subaddr = n[0] << 4 | n[2];
  103. subaddr2 = n[8] << 4 | n[11];
  104. oem = n[4] << 4 | n[5];
  105. addr = n[6] << 4 | n[7];
  106. toggle = n[10];
  107. obc1 = n[12] << 4 | n[13];
  108. obc2 = n[14] << 4 | n[15];
  109. if (subaddr != subaddr2) {
  110. IR_dprintk(2, "subaddress nibbles mismatch 0x%02X != 0x%02X\n",
  111. subaddr, subaddr2);
  112. data->state = STATE_INACTIVE;
  113. return -EINVAL;
  114. }
  115. if (oem != 0x44)
  116. IR_dprintk(1, "Warning: OEM nibbles 0x%02X. Expected 0x44\n",
  117. oem);
  118. scancode = addr << 24 | subaddr << 16 |
  119. obc1 << 8 | obc2;
  120. IR_dprintk(1, "XMP scancode 0x%06x\n", scancode);
  121. if (toggle == 0) {
  122. rc_keydown(dev, RC_TYPE_XMP, scancode, 0);
  123. } else {
  124. rc_repeat(dev);
  125. IR_dprintk(1, "Repeat last key\n");
  126. }
  127. data->state = STATE_INACTIVE;
  128. return 0;
  129. } else if (geq_margin(ev.duration, XMP_HALFFRAME_SPACE, XMP_NIBBLE_PREFIX)) {
  130. /* Expect 8 or 16 nibble pulses. 16 in case of 'final' frame */
  131. if (data->count == 16) {
  132. IR_dprintk(2, "received half frame pulse at index %d. Probably a final frame key-up event: %u\n",
  133. data->count, ev.duration);
  134. /*
  135. * TODO: for now go back to half frame position
  136. * so trailer can be found and key press
  137. * can be handled.
  138. */
  139. data->count = 8;
  140. }
  141. else if (data->count != 8)
  142. IR_dprintk(2, "received half frame pulse at index %d: %u\n",
  143. data->count, ev.duration);
  144. data->state = STATE_LEADER_PULSE;
  145. return 0;
  146. } else if (geq_margin(ev.duration, XMP_NIBBLE_PREFIX, XMP_UNIT)) {
  147. /* store nibble raw data, decode after trailer */
  148. if (data->count == 16) {
  149. IR_dprintk(2, "to many pulses (%d) ignoring: %u\n",
  150. data->count, ev.duration);
  151. data->state = STATE_INACTIVE;
  152. return -EINVAL;
  153. }
  154. data->durations[data->count] = ev.duration;
  155. data->count++;
  156. data->state = STATE_LEADER_PULSE;
  157. return 0;
  158. }
  159. break;
  160. }
  161. IR_dprintk(1, "XMP decode failed at count %d state %d (%uus %s)\n",
  162. data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  163. data->state = STATE_INACTIVE;
  164. return -EINVAL;
  165. }
  166. static struct ir_raw_handler xmp_handler = {
  167. .protocols = RC_BIT_XMP,
  168. .decode = ir_xmp_decode,
  169. };
  170. static int __init ir_xmp_decode_init(void)
  171. {
  172. ir_raw_handler_register(&xmp_handler);
  173. printk(KERN_INFO "IR XMP protocol handler initialized\n");
  174. return 0;
  175. }
  176. static void __exit ir_xmp_decode_exit(void)
  177. {
  178. ir_raw_handler_unregister(&xmp_handler);
  179. }
  180. module_init(ir_xmp_decode_init);
  181. module_exit(ir_xmp_decode_exit);
  182. MODULE_LICENSE("GPL");
  183. MODULE_AUTHOR("Marcel Mol <marcel@mesa.nl>");
  184. MODULE_AUTHOR("MESA Consulting (http://www.mesa.nl)");
  185. MODULE_DESCRIPTION("XMP IR protocol decoder");