ir-jvc-decoder.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 (!(dev->enabled_protocols & RC_BIT_JVC))
  46. return 0;
  47. if (!is_timing_event(ev)) {
  48. if (ev.reset)
  49. data->state = STATE_INACTIVE;
  50. return 0;
  51. }
  52. if (!geq_margin(ev.duration, JVC_UNIT, JVC_UNIT / 2))
  53. goto out;
  54. IR_dprintk(2, "JVC decode started at state %d (%uus %s)\n",
  55. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  56. again:
  57. switch (data->state) {
  58. case STATE_INACTIVE:
  59. if (!ev.pulse)
  60. break;
  61. if (!eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
  62. break;
  63. data->count = 0;
  64. data->first = true;
  65. data->toggle = !data->toggle;
  66. data->state = STATE_HEADER_SPACE;
  67. return 0;
  68. case STATE_HEADER_SPACE:
  69. if (ev.pulse)
  70. break;
  71. if (!eq_margin(ev.duration, JVC_HEADER_SPACE, JVC_UNIT / 2))
  72. break;
  73. data->state = STATE_BIT_PULSE;
  74. return 0;
  75. case STATE_BIT_PULSE:
  76. if (!ev.pulse)
  77. break;
  78. if (!eq_margin(ev.duration, JVC_BIT_PULSE, JVC_UNIT / 2))
  79. break;
  80. data->state = STATE_BIT_SPACE;
  81. return 0;
  82. case STATE_BIT_SPACE:
  83. if (ev.pulse)
  84. break;
  85. data->bits <<= 1;
  86. if (eq_margin(ev.duration, JVC_BIT_1_SPACE, JVC_UNIT / 2)) {
  87. data->bits |= 1;
  88. decrease_duration(&ev, JVC_BIT_1_SPACE);
  89. } else if (eq_margin(ev.duration, JVC_BIT_0_SPACE, JVC_UNIT / 2))
  90. decrease_duration(&ev, JVC_BIT_0_SPACE);
  91. else
  92. break;
  93. data->count++;
  94. if (data->count == JVC_NBITS)
  95. data->state = STATE_TRAILER_PULSE;
  96. else
  97. data->state = STATE_BIT_PULSE;
  98. return 0;
  99. case STATE_TRAILER_PULSE:
  100. if (!ev.pulse)
  101. break;
  102. if (!eq_margin(ev.duration, JVC_TRAILER_PULSE, JVC_UNIT / 2))
  103. break;
  104. data->state = STATE_TRAILER_SPACE;
  105. return 0;
  106. case STATE_TRAILER_SPACE:
  107. if (ev.pulse)
  108. break;
  109. if (!geq_margin(ev.duration, JVC_TRAILER_SPACE, JVC_UNIT / 2))
  110. break;
  111. if (data->first) {
  112. u32 scancode;
  113. scancode = (bitrev8((data->bits >> 8) & 0xff) << 8) |
  114. (bitrev8((data->bits >> 0) & 0xff) << 0);
  115. IR_dprintk(1, "JVC scancode 0x%04x\n", scancode);
  116. rc_keydown(dev, RC_TYPE_JVC, scancode, data->toggle);
  117. data->first = false;
  118. data->old_bits = data->bits;
  119. } else if (data->bits == data->old_bits) {
  120. IR_dprintk(1, "JVC repeat\n");
  121. rc_repeat(dev);
  122. } else {
  123. IR_dprintk(1, "JVC invalid repeat msg\n");
  124. break;
  125. }
  126. data->count = 0;
  127. data->state = STATE_CHECK_REPEAT;
  128. return 0;
  129. case STATE_CHECK_REPEAT:
  130. if (!ev.pulse)
  131. break;
  132. if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
  133. data->state = STATE_INACTIVE;
  134. else
  135. data->state = STATE_BIT_PULSE;
  136. goto again;
  137. }
  138. out:
  139. IR_dprintk(1, "JVC decode failed at state %d (%uus %s)\n",
  140. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  141. data->state = STATE_INACTIVE;
  142. return -EINVAL;
  143. }
  144. static struct ir_raw_handler jvc_handler = {
  145. .protocols = RC_BIT_JVC,
  146. .decode = ir_jvc_decode,
  147. };
  148. static int __init ir_jvc_decode_init(void)
  149. {
  150. ir_raw_handler_register(&jvc_handler);
  151. printk(KERN_INFO "IR JVC protocol handler initialized\n");
  152. return 0;
  153. }
  154. static void __exit ir_jvc_decode_exit(void)
  155. {
  156. ir_raw_handler_unregister(&jvc_handler);
  157. }
  158. module_init(ir_jvc_decode_init);
  159. module_exit(ir_jvc_decode_exit);
  160. MODULE_LICENSE("GPL");
  161. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  162. MODULE_DESCRIPTION("JVC IR protocol decoder");