ir-jvc-decoder.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* ir-jvc-decoder.c - handle JVC IR Pulse/Space protocol
  3. *
  4. * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
  5. */
  6. #include <linux/bitrev.h>
  7. #include <linux/module.h>
  8. #include "rc-core-priv.h"
  9. #define JVC_NBITS 16 /* dev(8) + func(8) */
  10. #define JVC_UNIT 525 /* us */
  11. #define JVC_HEADER_PULSE (16 * JVC_UNIT) /* lack of header -> repeat */
  12. #define JVC_HEADER_SPACE (8 * JVC_UNIT)
  13. #define JVC_BIT_PULSE (1 * JVC_UNIT)
  14. #define JVC_BIT_0_SPACE (1 * JVC_UNIT)
  15. #define JVC_BIT_1_SPACE (3 * JVC_UNIT)
  16. #define JVC_TRAILER_PULSE (1 * JVC_UNIT)
  17. #define JVC_TRAILER_SPACE (35 * JVC_UNIT)
  18. enum jvc_state {
  19. STATE_INACTIVE,
  20. STATE_HEADER_SPACE,
  21. STATE_BIT_PULSE,
  22. STATE_BIT_SPACE,
  23. STATE_TRAILER_PULSE,
  24. STATE_TRAILER_SPACE,
  25. STATE_CHECK_REPEAT,
  26. };
  27. /**
  28. * ir_jvc_decode() - Decode one JVC pulse or space
  29. * @dev: the struct rc_dev descriptor of the device
  30. * @ev: the struct ir_raw_event descriptor of the pulse/space
  31. *
  32. * This function returns -EINVAL if the pulse violates the state machine
  33. */
  34. static int ir_jvc_decode(struct rc_dev *dev, struct ir_raw_event ev)
  35. {
  36. struct jvc_dec *data = &dev->raw->jvc;
  37. if (!is_timing_event(ev)) {
  38. if (ev.reset)
  39. data->state = STATE_INACTIVE;
  40. return 0;
  41. }
  42. if (!geq_margin(ev.duration, JVC_UNIT, JVC_UNIT / 2))
  43. goto out;
  44. dev_dbg(&dev->dev, "JVC decode started at state %d (%uus %s)\n",
  45. data->state, ev.duration, TO_STR(ev.pulse));
  46. again:
  47. switch (data->state) {
  48. case STATE_INACTIVE:
  49. if (!ev.pulse)
  50. break;
  51. if (!eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
  52. break;
  53. data->count = 0;
  54. data->first = true;
  55. data->toggle = !data->toggle;
  56. data->state = STATE_HEADER_SPACE;
  57. return 0;
  58. case STATE_HEADER_SPACE:
  59. if (ev.pulse)
  60. break;
  61. if (!eq_margin(ev.duration, JVC_HEADER_SPACE, JVC_UNIT / 2))
  62. break;
  63. data->state = STATE_BIT_PULSE;
  64. return 0;
  65. case STATE_BIT_PULSE:
  66. if (!ev.pulse)
  67. break;
  68. if (!eq_margin(ev.duration, JVC_BIT_PULSE, JVC_UNIT / 2))
  69. break;
  70. data->state = STATE_BIT_SPACE;
  71. return 0;
  72. case STATE_BIT_SPACE:
  73. if (ev.pulse)
  74. break;
  75. data->bits <<= 1;
  76. if (eq_margin(ev.duration, JVC_BIT_1_SPACE, JVC_UNIT / 2)) {
  77. data->bits |= 1;
  78. decrease_duration(&ev, JVC_BIT_1_SPACE);
  79. } else if (eq_margin(ev.duration, JVC_BIT_0_SPACE, JVC_UNIT / 2))
  80. decrease_duration(&ev, JVC_BIT_0_SPACE);
  81. else
  82. break;
  83. data->count++;
  84. if (data->count == JVC_NBITS)
  85. data->state = STATE_TRAILER_PULSE;
  86. else
  87. data->state = STATE_BIT_PULSE;
  88. return 0;
  89. case STATE_TRAILER_PULSE:
  90. if (!ev.pulse)
  91. break;
  92. if (!eq_margin(ev.duration, JVC_TRAILER_PULSE, JVC_UNIT / 2))
  93. break;
  94. data->state = STATE_TRAILER_SPACE;
  95. return 0;
  96. case STATE_TRAILER_SPACE:
  97. if (ev.pulse)
  98. break;
  99. if (!geq_margin(ev.duration, JVC_TRAILER_SPACE, JVC_UNIT / 2))
  100. break;
  101. if (data->first) {
  102. u32 scancode;
  103. scancode = (bitrev8((data->bits >> 8) & 0xff) << 8) |
  104. (bitrev8((data->bits >> 0) & 0xff) << 0);
  105. dev_dbg(&dev->dev, "JVC scancode 0x%04x\n", scancode);
  106. rc_keydown(dev, RC_PROTO_JVC, scancode, data->toggle);
  107. data->first = false;
  108. data->old_bits = data->bits;
  109. } else if (data->bits == data->old_bits) {
  110. dev_dbg(&dev->dev, "JVC repeat\n");
  111. rc_repeat(dev);
  112. } else {
  113. dev_dbg(&dev->dev, "JVC invalid repeat msg\n");
  114. break;
  115. }
  116. data->count = 0;
  117. data->state = STATE_CHECK_REPEAT;
  118. return 0;
  119. case STATE_CHECK_REPEAT:
  120. if (!ev.pulse)
  121. break;
  122. if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
  123. data->state = STATE_INACTIVE;
  124. else
  125. data->state = STATE_BIT_PULSE;
  126. goto again;
  127. }
  128. out:
  129. dev_dbg(&dev->dev, "JVC decode failed at state %d (%uus %s)\n",
  130. data->state, ev.duration, TO_STR(ev.pulse));
  131. data->state = STATE_INACTIVE;
  132. return -EINVAL;
  133. }
  134. static const struct ir_raw_timings_pd ir_jvc_timings = {
  135. .header_pulse = JVC_HEADER_PULSE,
  136. .header_space = JVC_HEADER_SPACE,
  137. .bit_pulse = JVC_BIT_PULSE,
  138. .bit_space[0] = JVC_BIT_0_SPACE,
  139. .bit_space[1] = JVC_BIT_1_SPACE,
  140. .trailer_pulse = JVC_TRAILER_PULSE,
  141. .trailer_space = JVC_TRAILER_SPACE,
  142. .msb_first = 1,
  143. };
  144. /**
  145. * ir_jvc_encode() - Encode a scancode as a stream of raw events
  146. *
  147. * @protocol: protocol to encode
  148. * @scancode: scancode to encode
  149. * @events: array of raw ir events to write into
  150. * @max: maximum size of @events
  151. *
  152. * Returns: The number of events written.
  153. * -ENOBUFS if there isn't enough space in the array to fit the
  154. * encoding. In this case all @max events will have been written.
  155. */
  156. static int ir_jvc_encode(enum rc_proto protocol, u32 scancode,
  157. struct ir_raw_event *events, unsigned int max)
  158. {
  159. struct ir_raw_event *e = events;
  160. int ret;
  161. u32 raw = (bitrev8((scancode >> 8) & 0xff) << 8) |
  162. (bitrev8((scancode >> 0) & 0xff) << 0);
  163. ret = ir_raw_gen_pd(&e, max, &ir_jvc_timings, JVC_NBITS, raw);
  164. if (ret < 0)
  165. return ret;
  166. return e - events;
  167. }
  168. static struct ir_raw_handler jvc_handler = {
  169. .protocols = RC_PROTO_BIT_JVC,
  170. .decode = ir_jvc_decode,
  171. .encode = ir_jvc_encode,
  172. .carrier = 38000,
  173. .min_timeout = JVC_TRAILER_SPACE,
  174. };
  175. static int __init ir_jvc_decode_init(void)
  176. {
  177. ir_raw_handler_register(&jvc_handler);
  178. printk(KERN_INFO "IR JVC protocol handler initialized\n");
  179. return 0;
  180. }
  181. static void __exit ir_jvc_decode_exit(void)
  182. {
  183. ir_raw_handler_unregister(&jvc_handler);
  184. }
  185. module_init(ir_jvc_decode_init);
  186. module_exit(ir_jvc_decode_exit);
  187. MODULE_LICENSE("GPL");
  188. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  189. MODULE_DESCRIPTION("JVC IR protocol decoder");