ir-jvc-decoder.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * @ev: 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. dev_dbg(&dev->dev, "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. dev_dbg(&dev->dev, "JVC scancode 0x%04x\n", scancode);
  114. rc_keydown(dev, RC_PROTO_JVC, scancode, data->toggle);
  115. data->first = false;
  116. data->old_bits = data->bits;
  117. } else if (data->bits == data->old_bits) {
  118. dev_dbg(&dev->dev, "JVC repeat\n");
  119. rc_repeat(dev);
  120. } else {
  121. dev_dbg(&dev->dev, "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. dev_dbg(&dev->dev, "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 const struct ir_raw_timings_pd ir_jvc_timings = {
  143. .header_pulse = JVC_HEADER_PULSE,
  144. .header_space = JVC_HEADER_SPACE,
  145. .bit_pulse = JVC_BIT_PULSE,
  146. .bit_space[0] = JVC_BIT_0_SPACE,
  147. .bit_space[1] = JVC_BIT_1_SPACE,
  148. .trailer_pulse = JVC_TRAILER_PULSE,
  149. .trailer_space = JVC_TRAILER_SPACE,
  150. .msb_first = 1,
  151. };
  152. /**
  153. * ir_jvc_encode() - Encode a scancode as a stream of raw events
  154. *
  155. * @protocol: protocol to encode
  156. * @scancode: scancode to encode
  157. * @events: array of raw ir events to write into
  158. * @max: maximum size of @events
  159. *
  160. * Returns: The number of events written.
  161. * -ENOBUFS if there isn't enough space in the array to fit the
  162. * encoding. In this case all @max events will have been written.
  163. */
  164. static int ir_jvc_encode(enum rc_proto protocol, u32 scancode,
  165. struct ir_raw_event *events, unsigned int max)
  166. {
  167. struct ir_raw_event *e = events;
  168. int ret;
  169. u32 raw = (bitrev8((scancode >> 8) & 0xff) << 8) |
  170. (bitrev8((scancode >> 0) & 0xff) << 0);
  171. ret = ir_raw_gen_pd(&e, max, &ir_jvc_timings, JVC_NBITS, raw);
  172. if (ret < 0)
  173. return ret;
  174. return e - events;
  175. }
  176. static struct ir_raw_handler jvc_handler = {
  177. .protocols = RC_PROTO_BIT_JVC,
  178. .decode = ir_jvc_decode,
  179. .encode = ir_jvc_encode,
  180. .carrier = 38000,
  181. .min_timeout = JVC_TRAILER_SPACE,
  182. };
  183. static int __init ir_jvc_decode_init(void)
  184. {
  185. ir_raw_handler_register(&jvc_handler);
  186. printk(KERN_INFO "IR JVC protocol handler initialized\n");
  187. return 0;
  188. }
  189. static void __exit ir_jvc_decode_exit(void)
  190. {
  191. ir_raw_handler_unregister(&jvc_handler);
  192. }
  193. module_init(ir_jvc_decode_init);
  194. module_exit(ir_jvc_decode_exit);
  195. MODULE_LICENSE("GPL");
  196. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  197. MODULE_DESCRIPTION("JVC IR protocol decoder");