ir-sanyo-decoder.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0
  2. // ir-sanyo-decoder.c - handle SANYO IR Pulse/Space protocol
  3. //
  4. // Copyright (C) 2011 by Mauro Carvalho Chehab
  5. //
  6. // This protocol uses the NEC protocol timings. However, data is formatted as:
  7. // 13 bits Custom Code
  8. // 13 bits NOT(Custom Code)
  9. // 8 bits Key data
  10. // 8 bits NOT(Key data)
  11. //
  12. // According with LIRC, this protocol is used on Sanyo, Aiwa and Chinon
  13. // Information for this protocol is available at the Sanyo LC7461 datasheet.
  14. #include <linux/module.h>
  15. #include <linux/bitrev.h>
  16. #include "rc-core-priv.h"
  17. #define SANYO_NBITS (13+13+8+8)
  18. #define SANYO_UNIT 563 /* us */
  19. #define SANYO_HEADER_PULSE (16 * SANYO_UNIT)
  20. #define SANYO_HEADER_SPACE (8 * SANYO_UNIT)
  21. #define SANYO_BIT_PULSE (1 * SANYO_UNIT)
  22. #define SANYO_BIT_0_SPACE (1 * SANYO_UNIT)
  23. #define SANYO_BIT_1_SPACE (3 * SANYO_UNIT)
  24. #define SANYO_REPEAT_SPACE (150 * SANYO_UNIT)
  25. #define SANYO_TRAILER_PULSE (1 * SANYO_UNIT)
  26. #define SANYO_TRAILER_SPACE (10 * SANYO_UNIT) /* in fact, 42 */
  27. enum sanyo_state {
  28. STATE_INACTIVE,
  29. STATE_HEADER_SPACE,
  30. STATE_BIT_PULSE,
  31. STATE_BIT_SPACE,
  32. STATE_TRAILER_PULSE,
  33. STATE_TRAILER_SPACE,
  34. };
  35. /**
  36. * ir_sanyo_decode() - Decode one SANYO 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_sanyo_decode(struct rc_dev *dev, struct ir_raw_event ev)
  43. {
  44. struct sanyo_dec *data = &dev->raw->sanyo;
  45. u32 scancode;
  46. u16 address;
  47. u8 command, not_command;
  48. if (!is_timing_event(ev)) {
  49. if (ev.reset) {
  50. dev_dbg(&dev->dev, "SANYO event reset received. reset to state 0\n");
  51. data->state = STATE_INACTIVE;
  52. }
  53. return 0;
  54. }
  55. dev_dbg(&dev->dev, "SANYO decode started at state %d (%uus %s)\n",
  56. data->state, ev.duration, TO_STR(ev.pulse));
  57. switch (data->state) {
  58. case STATE_INACTIVE:
  59. if (!ev.pulse)
  60. break;
  61. if (eq_margin(ev.duration, SANYO_HEADER_PULSE, SANYO_UNIT / 2)) {
  62. data->count = 0;
  63. data->state = STATE_HEADER_SPACE;
  64. return 0;
  65. }
  66. break;
  67. case STATE_HEADER_SPACE:
  68. if (ev.pulse)
  69. break;
  70. if (eq_margin(ev.duration, SANYO_HEADER_SPACE, SANYO_UNIT / 2)) {
  71. data->state = STATE_BIT_PULSE;
  72. return 0;
  73. }
  74. break;
  75. case STATE_BIT_PULSE:
  76. if (!ev.pulse)
  77. break;
  78. if (!eq_margin(ev.duration, SANYO_BIT_PULSE, SANYO_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. if (!data->count && geq_margin(ev.duration, SANYO_REPEAT_SPACE, SANYO_UNIT / 2)) {
  86. rc_repeat(dev);
  87. dev_dbg(&dev->dev, "SANYO repeat last key\n");
  88. data->state = STATE_INACTIVE;
  89. return 0;
  90. }
  91. data->bits <<= 1;
  92. if (eq_margin(ev.duration, SANYO_BIT_1_SPACE, SANYO_UNIT / 2))
  93. data->bits |= 1;
  94. else if (!eq_margin(ev.duration, SANYO_BIT_0_SPACE, SANYO_UNIT / 2))
  95. break;
  96. data->count++;
  97. if (data->count == SANYO_NBITS)
  98. data->state = STATE_TRAILER_PULSE;
  99. else
  100. data->state = STATE_BIT_PULSE;
  101. return 0;
  102. case STATE_TRAILER_PULSE:
  103. if (!ev.pulse)
  104. break;
  105. if (!eq_margin(ev.duration, SANYO_TRAILER_PULSE, SANYO_UNIT / 2))
  106. break;
  107. data->state = STATE_TRAILER_SPACE;
  108. return 0;
  109. case STATE_TRAILER_SPACE:
  110. if (ev.pulse)
  111. break;
  112. if (!geq_margin(ev.duration, SANYO_TRAILER_SPACE, SANYO_UNIT / 2))
  113. break;
  114. address = bitrev16((data->bits >> 29) & 0x1fff) >> 3;
  115. /* not_address = bitrev16((data->bits >> 16) & 0x1fff) >> 3; */
  116. command = bitrev8((data->bits >> 8) & 0xff);
  117. not_command = bitrev8((data->bits >> 0) & 0xff);
  118. if ((command ^ not_command) != 0xff) {
  119. dev_dbg(&dev->dev, "SANYO checksum error: received 0x%08llx\n",
  120. data->bits);
  121. data->state = STATE_INACTIVE;
  122. return 0;
  123. }
  124. scancode = address << 8 | command;
  125. dev_dbg(&dev->dev, "SANYO scancode: 0x%06x\n", scancode);
  126. rc_keydown(dev, RC_PROTO_SANYO, scancode, 0);
  127. data->state = STATE_INACTIVE;
  128. return 0;
  129. }
  130. dev_dbg(&dev->dev, "SANYO decode failed at count %d state %d (%uus %s)\n",
  131. data->count, data->state, ev.duration, TO_STR(ev.pulse));
  132. data->state = STATE_INACTIVE;
  133. return -EINVAL;
  134. }
  135. static const struct ir_raw_timings_pd ir_sanyo_timings = {
  136. .header_pulse = SANYO_HEADER_PULSE,
  137. .header_space = SANYO_HEADER_SPACE,
  138. .bit_pulse = SANYO_BIT_PULSE,
  139. .bit_space[0] = SANYO_BIT_0_SPACE,
  140. .bit_space[1] = SANYO_BIT_1_SPACE,
  141. .trailer_pulse = SANYO_TRAILER_PULSE,
  142. .trailer_space = SANYO_TRAILER_SPACE,
  143. .msb_first = 1,
  144. };
  145. /**
  146. * ir_sanyo_encode() - Encode a scancode as a stream of raw events
  147. *
  148. * @protocol: protocol to encode
  149. * @scancode: scancode to encode
  150. * @events: array of raw ir events to write into
  151. * @max: maximum size of @events
  152. *
  153. * Returns: The number of events written.
  154. * -ENOBUFS if there isn't enough space in the array to fit the
  155. * encoding. In this case all @max events will have been written.
  156. */
  157. static int ir_sanyo_encode(enum rc_proto protocol, u32 scancode,
  158. struct ir_raw_event *events, unsigned int max)
  159. {
  160. struct ir_raw_event *e = events;
  161. int ret;
  162. u64 raw;
  163. raw = ((u64)(bitrev16(scancode >> 8) & 0xfff8) << (8 + 8 + 13 - 3)) |
  164. ((u64)(bitrev16(~scancode >> 8) & 0xfff8) << (8 + 8 + 0 - 3)) |
  165. ((bitrev8(scancode) & 0xff) << 8) |
  166. (bitrev8(~scancode) & 0xff);
  167. ret = ir_raw_gen_pd(&e, max, &ir_sanyo_timings, SANYO_NBITS, raw);
  168. if (ret < 0)
  169. return ret;
  170. return e - events;
  171. }
  172. static struct ir_raw_handler sanyo_handler = {
  173. .protocols = RC_PROTO_BIT_SANYO,
  174. .decode = ir_sanyo_decode,
  175. .encode = ir_sanyo_encode,
  176. .carrier = 38000,
  177. .min_timeout = SANYO_TRAILER_SPACE,
  178. };
  179. static int __init ir_sanyo_decode_init(void)
  180. {
  181. ir_raw_handler_register(&sanyo_handler);
  182. printk(KERN_INFO "IR SANYO protocol handler initialized\n");
  183. return 0;
  184. }
  185. static void __exit ir_sanyo_decode_exit(void)
  186. {
  187. ir_raw_handler_unregister(&sanyo_handler);
  188. }
  189. module_init(ir_sanyo_decode_init);
  190. module_exit(ir_sanyo_decode_exit);
  191. MODULE_LICENSE("GPL v2");
  192. MODULE_AUTHOR("Mauro Carvalho Chehab");
  193. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  194. MODULE_DESCRIPTION("SANYO IR protocol decoder");