ir-sharp-decoder.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* ir-sharp-decoder.c - handle Sharp IR Pulse/Space protocol
  2. *
  3. * Copyright (C) 2013-2014 Imagination Technologies Ltd.
  4. *
  5. * Based on NEC decoder:
  6. * Copyright (C) 2010 by Mauro Carvalho Chehab
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/bitrev.h>
  18. #include <linux/module.h>
  19. #include "rc-core-priv.h"
  20. #define SHARP_NBITS 15
  21. #define SHARP_UNIT 40000 /* ns */
  22. #define SHARP_BIT_PULSE (8 * SHARP_UNIT) /* 320us */
  23. #define SHARP_BIT_0_PERIOD (25 * SHARP_UNIT) /* 1ms (680us space) */
  24. #define SHARP_BIT_1_PERIOD (50 * SHARP_UNIT) /* 2ms (1680ms space) */
  25. #define SHARP_ECHO_SPACE (1000 * SHARP_UNIT) /* 40 ms */
  26. #define SHARP_TRAILER_SPACE (125 * SHARP_UNIT) /* 5 ms (even longer) */
  27. enum sharp_state {
  28. STATE_INACTIVE,
  29. STATE_BIT_PULSE,
  30. STATE_BIT_SPACE,
  31. STATE_TRAILER_PULSE,
  32. STATE_ECHO_SPACE,
  33. STATE_TRAILER_SPACE,
  34. };
  35. /**
  36. * ir_sharp_decode() - Decode one Sharp 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_sharp_decode(struct rc_dev *dev, struct ir_raw_event ev)
  43. {
  44. struct sharp_dec *data = &dev->raw->sharp;
  45. u32 msg, echo, address, command, scancode;
  46. if (!is_timing_event(ev)) {
  47. if (ev.reset)
  48. data->state = STATE_INACTIVE;
  49. return 0;
  50. }
  51. dev_dbg(&dev->dev, "Sharp decode started at state %d (%uus %s)\n",
  52. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  53. switch (data->state) {
  54. case STATE_INACTIVE:
  55. if (!ev.pulse)
  56. break;
  57. if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
  58. SHARP_BIT_PULSE / 2))
  59. break;
  60. data->count = 0;
  61. data->pulse_len = ev.duration;
  62. data->state = STATE_BIT_SPACE;
  63. return 0;
  64. case STATE_BIT_PULSE:
  65. if (!ev.pulse)
  66. break;
  67. if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
  68. SHARP_BIT_PULSE / 2))
  69. break;
  70. data->pulse_len = ev.duration;
  71. data->state = STATE_BIT_SPACE;
  72. return 0;
  73. case STATE_BIT_SPACE:
  74. if (ev.pulse)
  75. break;
  76. data->bits <<= 1;
  77. if (eq_margin(data->pulse_len + ev.duration, SHARP_BIT_1_PERIOD,
  78. SHARP_BIT_PULSE * 2))
  79. data->bits |= 1;
  80. else if (!eq_margin(data->pulse_len + ev.duration,
  81. SHARP_BIT_0_PERIOD, SHARP_BIT_PULSE * 2))
  82. break;
  83. data->count++;
  84. if (data->count == SHARP_NBITS ||
  85. data->count == SHARP_NBITS * 2)
  86. data->state = STATE_TRAILER_PULSE;
  87. else
  88. data->state = STATE_BIT_PULSE;
  89. return 0;
  90. case STATE_TRAILER_PULSE:
  91. if (!ev.pulse)
  92. break;
  93. if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
  94. SHARP_BIT_PULSE / 2))
  95. break;
  96. if (data->count == SHARP_NBITS) {
  97. /* exp,chk bits should be 1,0 */
  98. if ((data->bits & 0x3) != 0x2 &&
  99. /* DENON variant, both chk bits 0 */
  100. (data->bits & 0x3) != 0x0)
  101. break;
  102. data->state = STATE_ECHO_SPACE;
  103. } else {
  104. data->state = STATE_TRAILER_SPACE;
  105. }
  106. return 0;
  107. case STATE_ECHO_SPACE:
  108. if (ev.pulse)
  109. break;
  110. if (!eq_margin(ev.duration, SHARP_ECHO_SPACE,
  111. SHARP_ECHO_SPACE / 4))
  112. break;
  113. data->state = STATE_BIT_PULSE;
  114. return 0;
  115. case STATE_TRAILER_SPACE:
  116. if (ev.pulse)
  117. break;
  118. if (!geq_margin(ev.duration, SHARP_TRAILER_SPACE,
  119. SHARP_BIT_PULSE / 2))
  120. break;
  121. /* Validate - command, ext, chk should be inverted in 2nd */
  122. msg = (data->bits >> 15) & 0x7fff;
  123. echo = data->bits & 0x7fff;
  124. if ((msg ^ echo) != 0x3ff) {
  125. dev_dbg(&dev->dev,
  126. "Sharp checksum error: received 0x%04x, 0x%04x\n",
  127. msg, echo);
  128. break;
  129. }
  130. address = bitrev8((msg >> 7) & 0xf8);
  131. command = bitrev8((msg >> 2) & 0xff);
  132. scancode = address << 8 | command;
  133. dev_dbg(&dev->dev, "Sharp scancode 0x%04x\n", scancode);
  134. rc_keydown(dev, RC_PROTO_SHARP, scancode, 0);
  135. data->state = STATE_INACTIVE;
  136. return 0;
  137. }
  138. dev_dbg(&dev->dev, "Sharp decode failed at count %d state %d (%uus %s)\n",
  139. data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  140. data->state = STATE_INACTIVE;
  141. return -EINVAL;
  142. }
  143. static const struct ir_raw_timings_pd ir_sharp_timings = {
  144. .header_pulse = 0,
  145. .header_space = 0,
  146. .bit_pulse = SHARP_BIT_PULSE,
  147. .bit_space[0] = SHARP_BIT_0_PERIOD,
  148. .bit_space[1] = SHARP_BIT_1_PERIOD,
  149. .trailer_pulse = SHARP_BIT_PULSE,
  150. .trailer_space = SHARP_ECHO_SPACE,
  151. .msb_first = 1,
  152. };
  153. /**
  154. * ir_sharp_encode() - Encode a scancode as a stream of raw events
  155. *
  156. * @protocol: protocol to encode
  157. * @scancode: scancode to encode
  158. * @events: array of raw ir events to write into
  159. * @max: maximum size of @events
  160. *
  161. * Returns: The number of events written.
  162. * -ENOBUFS if there isn't enough space in the array to fit the
  163. * encoding. In this case all @max events will have been written.
  164. */
  165. static int ir_sharp_encode(enum rc_proto protocol, u32 scancode,
  166. struct ir_raw_event *events, unsigned int max)
  167. {
  168. struct ir_raw_event *e = events;
  169. int ret;
  170. u32 raw;
  171. raw = (((bitrev8(scancode >> 8) >> 3) << 8) & 0x1f00) |
  172. bitrev8(scancode);
  173. ret = ir_raw_gen_pd(&e, max, &ir_sharp_timings, SHARP_NBITS,
  174. (raw << 2) | 2);
  175. if (ret < 0)
  176. return ret;
  177. max -= ret;
  178. raw = (((bitrev8(scancode >> 8) >> 3) << 8) & 0x1f00) |
  179. bitrev8(~scancode);
  180. ret = ir_raw_gen_pd(&e, max, &ir_sharp_timings, SHARP_NBITS,
  181. (raw << 2) | 1);
  182. if (ret < 0)
  183. return ret;
  184. return e - events;
  185. }
  186. static struct ir_raw_handler sharp_handler = {
  187. .protocols = RC_PROTO_BIT_SHARP,
  188. .decode = ir_sharp_decode,
  189. .encode = ir_sharp_encode,
  190. .carrier = 38000,
  191. .min_timeout = SHARP_ECHO_SPACE + SHARP_ECHO_SPACE / 4,
  192. };
  193. static int __init ir_sharp_decode_init(void)
  194. {
  195. ir_raw_handler_register(&sharp_handler);
  196. pr_info("IR Sharp protocol handler initialized\n");
  197. return 0;
  198. }
  199. static void __exit ir_sharp_decode_exit(void)
  200. {
  201. ir_raw_handler_unregister(&sharp_handler);
  202. }
  203. module_init(ir_sharp_decode_init);
  204. module_exit(ir_sharp_decode_exit);
  205. MODULE_LICENSE("GPL");
  206. MODULE_AUTHOR("James Hogan <jhogan@kernel.org>");
  207. MODULE_DESCRIPTION("Sharp IR protocol decoder");