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. * @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 (!(dev->enabled_protocols & RC_BIT_XMP))
  42. return 0;
  43. if (!is_timing_event(ev)) {
  44. if (ev.reset)
  45. data->state = STATE_INACTIVE;
  46. return 0;
  47. }
  48. IR_dprintk(2, "XMP decode started at state %d %d (%uus %s)\n",
  49. data->state, data->count, TO_US(ev.duration), TO_STR(ev.pulse));
  50. switch (data->state) {
  51. case STATE_INACTIVE:
  52. if (!ev.pulse)
  53. break;
  54. if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2)) {
  55. data->count = 0;
  56. data->state = STATE_NIBBLE_SPACE;
  57. }
  58. return 0;
  59. case STATE_LEADER_PULSE:
  60. if (!ev.pulse)
  61. break;
  62. if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2))
  63. data->state = STATE_NIBBLE_SPACE;
  64. return 0;
  65. case STATE_NIBBLE_SPACE:
  66. if (ev.pulse)
  67. break;
  68. if (geq_margin(ev.duration, XMP_TRAILER_SPACE, XMP_NIBBLE_PREFIX)) {
  69. int divider, i;
  70. u8 addr, subaddr, subaddr2, toggle, oem, obc1, obc2, sum1, sum2;
  71. u32 *n;
  72. u32 scancode;
  73. if (data->count != 16) {
  74. IR_dprintk(2, "received TRAILER period at index %d: %u\n",
  75. data->count, ev.duration);
  76. data->state = STATE_INACTIVE;
  77. return -EINVAL;
  78. }
  79. n = data->durations;
  80. /*
  81. * the 4th nibble should be 15 so base the divider on this
  82. * to transform durations into nibbles. Substract 2000 from
  83. * the divider to compensate for fluctuations in the signal
  84. */
  85. divider = (n[3] - XMP_NIBBLE_PREFIX) / 15 - 2000;
  86. if (divider < 50) {
  87. IR_dprintk(2, "divider to small %d.\n", divider);
  88. data->state = STATE_INACTIVE;
  89. return -EINVAL;
  90. }
  91. /* convert to nibbles and do some sanity checks */
  92. for (i = 0; i < 16; i++)
  93. n[i] = (n[i] - XMP_NIBBLE_PREFIX) / divider;
  94. sum1 = (15 + n[0] + n[1] + n[2] + n[3] +
  95. n[4] + n[5] + n[6] + n[7]) % 16;
  96. sum2 = (15 + n[8] + n[9] + n[10] + n[11] +
  97. n[12] + n[13] + n[14] + n[15]) % 16;
  98. if (sum1 != 15 || sum2 != 15) {
  99. IR_dprintk(2, "checksum errors sum1=0x%X sum2=0x%X\n",
  100. sum1, sum2);
  101. data->state = STATE_INACTIVE;
  102. return -EINVAL;
  103. }
  104. subaddr = n[0] << 4 | n[2];
  105. subaddr2 = n[8] << 4 | n[11];
  106. oem = n[4] << 4 | n[5];
  107. addr = n[6] << 4 | n[7];
  108. toggle = n[10];
  109. obc1 = n[12] << 4 | n[13];
  110. obc2 = n[14] << 4 | n[15];
  111. if (subaddr != subaddr2) {
  112. IR_dprintk(2, "subaddress nibbles mismatch 0x%02X != 0x%02X\n",
  113. subaddr, subaddr2);
  114. data->state = STATE_INACTIVE;
  115. return -EINVAL;
  116. }
  117. if (oem != 0x44)
  118. IR_dprintk(1, "Warning: OEM nibbles 0x%02X. Expected 0x44\n",
  119. oem);
  120. scancode = addr << 24 | subaddr << 16 |
  121. obc1 << 8 | obc2;
  122. IR_dprintk(1, "XMP scancode 0x%06x\n", scancode);
  123. if (toggle == 0) {
  124. rc_keydown(dev, RC_TYPE_XMP, scancode, 0);
  125. } else {
  126. rc_repeat(dev);
  127. IR_dprintk(1, "Repeat last key\n");
  128. }
  129. data->state = STATE_INACTIVE;
  130. return 0;
  131. } else if (geq_margin(ev.duration, XMP_HALFFRAME_SPACE, XMP_NIBBLE_PREFIX)) {
  132. /* Expect 8 or 16 nibble pulses. 16 in case of 'final' frame */
  133. if (data->count == 16) {
  134. IR_dprintk(2, "received half frame pulse at index %d. Probably a final frame key-up event: %u\n",
  135. data->count, ev.duration);
  136. /*
  137. * TODO: for now go back to half frame position
  138. * so trailer can be found and key press
  139. * can be handled.
  140. */
  141. data->count = 8;
  142. }
  143. else if (data->count != 8)
  144. IR_dprintk(2, "received half frame pulse at index %d: %u\n",
  145. data->count, ev.duration);
  146. data->state = STATE_LEADER_PULSE;
  147. return 0;
  148. } else if (geq_margin(ev.duration, XMP_NIBBLE_PREFIX, XMP_UNIT)) {
  149. /* store nibble raw data, decode after trailer */
  150. if (data->count == 16) {
  151. IR_dprintk(2, "to many pulses (%d) ignoring: %u\n",
  152. data->count, ev.duration);
  153. data->state = STATE_INACTIVE;
  154. return -EINVAL;
  155. }
  156. data->durations[data->count] = ev.duration;
  157. data->count++;
  158. data->state = STATE_LEADER_PULSE;
  159. return 0;
  160. }
  161. break;
  162. }
  163. IR_dprintk(1, "XMP decode failed at count %d state %d (%uus %s)\n",
  164. data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  165. data->state = STATE_INACTIVE;
  166. return -EINVAL;
  167. }
  168. static struct ir_raw_handler xmp_handler = {
  169. .protocols = RC_BIT_XMP,
  170. .decode = ir_xmp_decode,
  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");