ir-rc6-decoder.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* ir-rc6-decoder.c - A decoder for the RC6 IR 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 "rc-core-priv.h"
  15. #include <linux/module.h>
  16. /*
  17. * This decoder currently supports:
  18. * RC6-0-16 (standard toggle bit in header)
  19. * RC6-6A-20 (no toggle bit)
  20. * RC6-6A-24 (no toggle bit)
  21. * RC6-6A-32 (MCE version with toggle bit in body)
  22. */
  23. #define RC6_UNIT 444444 /* nanosecs */
  24. #define RC6_HEADER_NBITS 4 /* not including toggle bit */
  25. #define RC6_0_NBITS 16
  26. #define RC6_6A_32_NBITS 32
  27. #define RC6_6A_NBITS 128 /* Variable 8..128 */
  28. #define RC6_PREFIX_PULSE (6 * RC6_UNIT)
  29. #define RC6_PREFIX_SPACE (2 * RC6_UNIT)
  30. #define RC6_BIT_START (1 * RC6_UNIT)
  31. #define RC6_BIT_END (1 * RC6_UNIT)
  32. #define RC6_TOGGLE_START (2 * RC6_UNIT)
  33. #define RC6_TOGGLE_END (2 * RC6_UNIT)
  34. #define RC6_SUFFIX_SPACE (6 * RC6_UNIT)
  35. #define RC6_MODE_MASK 0x07 /* for the header bits */
  36. #define RC6_STARTBIT_MASK 0x08 /* for the header bits */
  37. #define RC6_6A_MCE_TOGGLE_MASK 0x8000 /* for the body bits */
  38. #define RC6_6A_LCC_MASK 0xffff0000 /* RC6-6A-32 long customer code mask */
  39. #define RC6_6A_MCE_CC 0x800f0000 /* MCE customer code */
  40. #ifndef CHAR_BIT
  41. #define CHAR_BIT 8 /* Normally in <limits.h> */
  42. #endif
  43. enum rc6_mode {
  44. RC6_MODE_0,
  45. RC6_MODE_6A,
  46. RC6_MODE_UNKNOWN,
  47. };
  48. enum rc6_state {
  49. STATE_INACTIVE,
  50. STATE_PREFIX_SPACE,
  51. STATE_HEADER_BIT_START,
  52. STATE_HEADER_BIT_END,
  53. STATE_TOGGLE_START,
  54. STATE_TOGGLE_END,
  55. STATE_BODY_BIT_START,
  56. STATE_BODY_BIT_END,
  57. STATE_FINISHED,
  58. };
  59. static enum rc6_mode rc6_mode(struct rc6_dec *data)
  60. {
  61. switch (data->header & RC6_MODE_MASK) {
  62. case 0:
  63. return RC6_MODE_0;
  64. case 6:
  65. if (!data->toggle)
  66. return RC6_MODE_6A;
  67. /* fall through */
  68. default:
  69. return RC6_MODE_UNKNOWN;
  70. }
  71. }
  72. /**
  73. * ir_rc6_decode() - Decode one RC6 pulse or space
  74. * @dev: the struct rc_dev descriptor of the device
  75. * @ev: the struct ir_raw_event descriptor of the pulse/space
  76. *
  77. * This function returns -EINVAL if the pulse violates the state machine
  78. */
  79. static int ir_rc6_decode(struct rc_dev *dev, struct ir_raw_event ev)
  80. {
  81. struct rc6_dec *data = &dev->raw->rc6;
  82. u32 scancode;
  83. u8 toggle;
  84. if (!(dev->raw->enabled_protocols &
  85. (RC_BIT_RC6_0 | RC_BIT_RC6_6A_20 | RC_BIT_RC6_6A_24 |
  86. RC_BIT_RC6_6A_32 | RC_BIT_RC6_MCE)))
  87. return 0;
  88. if (!is_timing_event(ev)) {
  89. if (ev.reset)
  90. data->state = STATE_INACTIVE;
  91. return 0;
  92. }
  93. if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  94. goto out;
  95. again:
  96. IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n",
  97. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  98. if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  99. return 0;
  100. switch (data->state) {
  101. case STATE_INACTIVE:
  102. if (!ev.pulse)
  103. break;
  104. /* Note: larger margin on first pulse since each RC6_UNIT
  105. is quite short and some hardware takes some time to
  106. adjust to the signal */
  107. if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
  108. break;
  109. data->state = STATE_PREFIX_SPACE;
  110. data->count = 0;
  111. return 0;
  112. case STATE_PREFIX_SPACE:
  113. if (ev.pulse)
  114. break;
  115. if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
  116. break;
  117. data->state = STATE_HEADER_BIT_START;
  118. data->header = 0;
  119. return 0;
  120. case STATE_HEADER_BIT_START:
  121. if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
  122. break;
  123. data->header <<= 1;
  124. if (ev.pulse)
  125. data->header |= 1;
  126. data->count++;
  127. data->state = STATE_HEADER_BIT_END;
  128. return 0;
  129. case STATE_HEADER_BIT_END:
  130. if (!is_transition(&ev, &dev->raw->prev_ev))
  131. break;
  132. if (data->count == RC6_HEADER_NBITS)
  133. data->state = STATE_TOGGLE_START;
  134. else
  135. data->state = STATE_HEADER_BIT_START;
  136. decrease_duration(&ev, RC6_BIT_END);
  137. goto again;
  138. case STATE_TOGGLE_START:
  139. if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
  140. break;
  141. data->toggle = ev.pulse;
  142. data->state = STATE_TOGGLE_END;
  143. return 0;
  144. case STATE_TOGGLE_END:
  145. if (!is_transition(&ev, &dev->raw->prev_ev) ||
  146. !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2))
  147. break;
  148. if (!(data->header & RC6_STARTBIT_MASK)) {
  149. IR_dprintk(1, "RC6 invalid start bit\n");
  150. break;
  151. }
  152. data->state = STATE_BODY_BIT_START;
  153. decrease_duration(&ev, RC6_TOGGLE_END);
  154. data->count = 0;
  155. data->body = 0;
  156. switch (rc6_mode(data)) {
  157. case RC6_MODE_0:
  158. data->wanted_bits = RC6_0_NBITS;
  159. break;
  160. case RC6_MODE_6A:
  161. data->wanted_bits = RC6_6A_NBITS;
  162. break;
  163. default:
  164. IR_dprintk(1, "RC6 unknown mode\n");
  165. goto out;
  166. }
  167. goto again;
  168. case STATE_BODY_BIT_START:
  169. if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) {
  170. /* Discard LSB's that won't fit in data->body */
  171. if (data->count++ < CHAR_BIT * sizeof data->body) {
  172. data->body <<= 1;
  173. if (ev.pulse)
  174. data->body |= 1;
  175. }
  176. data->state = STATE_BODY_BIT_END;
  177. return 0;
  178. } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse &&
  179. geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) {
  180. data->state = STATE_FINISHED;
  181. goto again;
  182. }
  183. break;
  184. case STATE_BODY_BIT_END:
  185. if (!is_transition(&ev, &dev->raw->prev_ev))
  186. break;
  187. if (data->count == data->wanted_bits)
  188. data->state = STATE_FINISHED;
  189. else
  190. data->state = STATE_BODY_BIT_START;
  191. decrease_duration(&ev, RC6_BIT_END);
  192. goto again;
  193. case STATE_FINISHED:
  194. if (ev.pulse)
  195. break;
  196. switch (rc6_mode(data)) {
  197. case RC6_MODE_0:
  198. scancode = data->body;
  199. toggle = data->toggle;
  200. IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n",
  201. scancode, toggle);
  202. break;
  203. case RC6_MODE_6A:
  204. if (data->count > CHAR_BIT * sizeof data->body) {
  205. IR_dprintk(1, "RC6 too many (%u) data bits\n",
  206. data->count);
  207. goto out;
  208. }
  209. scancode = data->body;
  210. if (data->count == RC6_6A_32_NBITS &&
  211. (scancode & RC6_6A_LCC_MASK) == RC6_6A_MCE_CC) {
  212. /* MCE RC */
  213. toggle = (scancode & RC6_6A_MCE_TOGGLE_MASK) ? 1 : 0;
  214. scancode &= ~RC6_6A_MCE_TOGGLE_MASK;
  215. } else {
  216. toggle = 0;
  217. }
  218. IR_dprintk(1, "RC6(6A) scancode 0x%08x (toggle: %u)\n",
  219. scancode, toggle);
  220. break;
  221. default:
  222. IR_dprintk(1, "RC6 unknown mode\n");
  223. goto out;
  224. }
  225. rc_keydown(dev, scancode, toggle);
  226. data->state = STATE_INACTIVE;
  227. return 0;
  228. }
  229. out:
  230. IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n",
  231. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  232. data->state = STATE_INACTIVE;
  233. return -EINVAL;
  234. }
  235. static struct ir_raw_handler rc6_handler = {
  236. .protocols = RC_BIT_RC6_0 | RC_BIT_RC6_6A_20 |
  237. RC_BIT_RC6_6A_24 | RC_BIT_RC6_6A_32 |
  238. RC_BIT_RC6_MCE,
  239. .decode = ir_rc6_decode,
  240. };
  241. static int __init ir_rc6_decode_init(void)
  242. {
  243. ir_raw_handler_register(&rc6_handler);
  244. printk(KERN_INFO "IR RC6 protocol handler initialized\n");
  245. return 0;
  246. }
  247. static void __exit ir_rc6_decode_exit(void)
  248. {
  249. ir_raw_handler_unregister(&rc6_handler);
  250. }
  251. module_init(ir_rc6_decode_init);
  252. module_exit(ir_rc6_decode_exit);
  253. MODULE_LICENSE("GPL");
  254. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  255. MODULE_DESCRIPTION("RC6 IR protocol decoder");