ir-jvc-decoder.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* ir-jvc-decoder.c - handle JVC IR Pulse/Space protocol
  2. *
  3. * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
  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. #include <linux/bitrev.h>
  15. #include <linux/module.h>
  16. #include "rc-core-priv.h"
  17. #define JVC_NBITS 16 /* dev(8) + func(8) */
  18. #define JVC_UNIT 525000 /* ns */
  19. #define JVC_HEADER_PULSE (16 * JVC_UNIT) /* lack of header -> repeat */
  20. #define JVC_HEADER_SPACE (8 * JVC_UNIT)
  21. #define JVC_BIT_PULSE (1 * JVC_UNIT)
  22. #define JVC_BIT_0_SPACE (1 * JVC_UNIT)
  23. #define JVC_BIT_1_SPACE (3 * JVC_UNIT)
  24. #define JVC_TRAILER_PULSE (1 * JVC_UNIT)
  25. #define JVC_TRAILER_SPACE (35 * JVC_UNIT)
  26. enum jvc_state {
  27. STATE_INACTIVE,
  28. STATE_HEADER_SPACE,
  29. STATE_BIT_PULSE,
  30. STATE_BIT_SPACE,
  31. STATE_TRAILER_PULSE,
  32. STATE_TRAILER_SPACE,
  33. STATE_CHECK_REPEAT,
  34. };
  35. /**
  36. * ir_jvc_decode() - Decode one JVC pulse or space
  37. * @dev: the struct rc_dev descriptor of the device
  38. * @duration: the struct ir_raw_event descriptor of the pulse/space
  39. *
  40. * This function returns -EINVAL if the pulse violates the state machine
  41. */
  42. static int ir_jvc_decode(struct rc_dev *dev, struct ir_raw_event ev)
  43. {
  44. struct jvc_dec *data = &dev->raw->jvc;
  45. if (!is_timing_event(ev)) {
  46. if (ev.reset)
  47. data->state = STATE_INACTIVE;
  48. return 0;
  49. }
  50. if (!geq_margin(ev.duration, JVC_UNIT, JVC_UNIT / 2))
  51. goto out;
  52. IR_dprintk(2, "JVC decode started at state %d (%uus %s)\n",
  53. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  54. again:
  55. switch (data->state) {
  56. case STATE_INACTIVE:
  57. if (!ev.pulse)
  58. break;
  59. if (!eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
  60. break;
  61. data->count = 0;
  62. data->first = true;
  63. data->toggle = !data->toggle;
  64. data->state = STATE_HEADER_SPACE;
  65. return 0;
  66. case STATE_HEADER_SPACE:
  67. if (ev.pulse)
  68. break;
  69. if (!eq_margin(ev.duration, JVC_HEADER_SPACE, JVC_UNIT / 2))
  70. break;
  71. data->state = STATE_BIT_PULSE;
  72. return 0;
  73. case STATE_BIT_PULSE:
  74. if (!ev.pulse)
  75. break;
  76. if (!eq_margin(ev.duration, JVC_BIT_PULSE, JVC_UNIT / 2))
  77. break;
  78. data->state = STATE_BIT_SPACE;
  79. return 0;
  80. case STATE_BIT_SPACE:
  81. if (ev.pulse)
  82. break;
  83. data->bits <<= 1;
  84. if (eq_margin(ev.duration, JVC_BIT_1_SPACE, JVC_UNIT / 2)) {
  85. data->bits |= 1;
  86. decrease_duration(&ev, JVC_BIT_1_SPACE);
  87. } else if (eq_margin(ev.duration, JVC_BIT_0_SPACE, JVC_UNIT / 2))
  88. decrease_duration(&ev, JVC_BIT_0_SPACE);
  89. else
  90. break;
  91. data->count++;
  92. if (data->count == JVC_NBITS)
  93. data->state = STATE_TRAILER_PULSE;
  94. else
  95. data->state = STATE_BIT_PULSE;
  96. return 0;
  97. case STATE_TRAILER_PULSE:
  98. if (!ev.pulse)
  99. break;
  100. if (!eq_margin(ev.duration, JVC_TRAILER_PULSE, JVC_UNIT / 2))
  101. break;
  102. data->state = STATE_TRAILER_SPACE;
  103. return 0;
  104. case STATE_TRAILER_SPACE:
  105. if (ev.pulse)
  106. break;
  107. if (!geq_margin(ev.duration, JVC_TRAILER_SPACE, JVC_UNIT / 2))
  108. break;
  109. if (data->first) {
  110. u32 scancode;
  111. scancode = (bitrev8((data->bits >> 8) & 0xff) << 8) |
  112. (bitrev8((data->bits >> 0) & 0xff) << 0);
  113. IR_dprintk(1, "JVC scancode 0x%04x\n", scancode);
  114. rc_keydown(dev, RC_TYPE_JVC, scancode, data->toggle);
  115. data->first = false;
  116. data->old_bits = data->bits;
  117. } else if (data->bits == data->old_bits) {
  118. IR_dprintk(1, "JVC repeat\n");
  119. rc_repeat(dev);
  120. } else {
  121. IR_dprintk(1, "JVC invalid repeat msg\n");
  122. break;
  123. }
  124. data->count = 0;
  125. data->state = STATE_CHECK_REPEAT;
  126. return 0;
  127. case STATE_CHECK_REPEAT:
  128. if (!ev.pulse)
  129. break;
  130. if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
  131. data->state = STATE_INACTIVE;
  132. else
  133. data->state = STATE_BIT_PULSE;
  134. goto again;
  135. }
  136. out:
  137. IR_dprintk(1, "JVC decode failed at state %d (%uus %s)\n",
  138. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  139. data->state = STATE_INACTIVE;
  140. return -EINVAL;
  141. }
  142. static struct ir_raw_handler jvc_handler = {
  143. .protocols = RC_BIT_JVC,
  144. .decode = ir_jvc_decode,
  145. };
  146. static int __init ir_jvc_decode_init(void)
  147. {
  148. ir_raw_handler_register(&jvc_handler);
  149. printk(KERN_INFO "IR JVC protocol handler initialized\n");
  150. return 0;
  151. }
  152. static void __exit ir_jvc_decode_exit(void)
  153. {
  154. ir_raw_handler_unregister(&jvc_handler);
  155. }
  156. module_init(ir_jvc_decode_init);
  157. module_exit(ir_jvc_decode_exit);
  158. MODULE_LICENSE("GPL");
  159. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  160. MODULE_DESCRIPTION("JVC IR protocol decoder");