ir-rc6-decoder.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 & RC_TYPE_RC6))
  85. return 0;
  86. if (!is_timing_event(ev)) {
  87. if (ev.reset)
  88. data->state = STATE_INACTIVE;
  89. return 0;
  90. }
  91. if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  92. goto out;
  93. again:
  94. IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n",
  95. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  96. if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  97. return 0;
  98. switch (data->state) {
  99. case STATE_INACTIVE:
  100. if (!ev.pulse)
  101. break;
  102. /* Note: larger margin on first pulse since each RC6_UNIT
  103. is quite short and some hardware takes some time to
  104. adjust to the signal */
  105. if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
  106. break;
  107. data->state = STATE_PREFIX_SPACE;
  108. data->count = 0;
  109. return 0;
  110. case STATE_PREFIX_SPACE:
  111. if (ev.pulse)
  112. break;
  113. if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
  114. break;
  115. data->state = STATE_HEADER_BIT_START;
  116. data->header = 0;
  117. return 0;
  118. case STATE_HEADER_BIT_START:
  119. if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
  120. break;
  121. data->header <<= 1;
  122. if (ev.pulse)
  123. data->header |= 1;
  124. data->count++;
  125. data->state = STATE_HEADER_BIT_END;
  126. return 0;
  127. case STATE_HEADER_BIT_END:
  128. if (!is_transition(&ev, &dev->raw->prev_ev))
  129. break;
  130. if (data->count == RC6_HEADER_NBITS)
  131. data->state = STATE_TOGGLE_START;
  132. else
  133. data->state = STATE_HEADER_BIT_START;
  134. decrease_duration(&ev, RC6_BIT_END);
  135. goto again;
  136. case STATE_TOGGLE_START:
  137. if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
  138. break;
  139. data->toggle = ev.pulse;
  140. data->state = STATE_TOGGLE_END;
  141. return 0;
  142. case STATE_TOGGLE_END:
  143. if (!is_transition(&ev, &dev->raw->prev_ev) ||
  144. !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2))
  145. break;
  146. if (!(data->header & RC6_STARTBIT_MASK)) {
  147. IR_dprintk(1, "RC6 invalid start bit\n");
  148. break;
  149. }
  150. data->state = STATE_BODY_BIT_START;
  151. decrease_duration(&ev, RC6_TOGGLE_END);
  152. data->count = 0;
  153. data->body = 0;
  154. switch (rc6_mode(data)) {
  155. case RC6_MODE_0:
  156. data->wanted_bits = RC6_0_NBITS;
  157. break;
  158. case RC6_MODE_6A:
  159. data->wanted_bits = RC6_6A_NBITS;
  160. break;
  161. default:
  162. IR_dprintk(1, "RC6 unknown mode\n");
  163. goto out;
  164. }
  165. goto again;
  166. case STATE_BODY_BIT_START:
  167. if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) {
  168. /* Discard LSB's that won't fit in data->body */
  169. if (data->count++ < CHAR_BIT * sizeof data->body) {
  170. data->body <<= 1;
  171. if (ev.pulse)
  172. data->body |= 1;
  173. }
  174. data->state = STATE_BODY_BIT_END;
  175. return 0;
  176. } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse &&
  177. geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) {
  178. data->state = STATE_FINISHED;
  179. goto again;
  180. }
  181. break;
  182. case STATE_BODY_BIT_END:
  183. if (!is_transition(&ev, &dev->raw->prev_ev))
  184. break;
  185. if (data->count == data->wanted_bits)
  186. data->state = STATE_FINISHED;
  187. else
  188. data->state = STATE_BODY_BIT_START;
  189. decrease_duration(&ev, RC6_BIT_END);
  190. goto again;
  191. case STATE_FINISHED:
  192. if (ev.pulse)
  193. break;
  194. switch (rc6_mode(data)) {
  195. case RC6_MODE_0:
  196. scancode = data->body;
  197. toggle = data->toggle;
  198. IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n",
  199. scancode, toggle);
  200. break;
  201. case RC6_MODE_6A:
  202. if (data->count > CHAR_BIT * sizeof data->body) {
  203. IR_dprintk(1, "RC6 too many (%u) data bits\n",
  204. data->count);
  205. goto out;
  206. }
  207. scancode = data->body;
  208. if (data->count == RC6_6A_32_NBITS &&
  209. (scancode & RC6_6A_LCC_MASK) == RC6_6A_MCE_CC) {
  210. /* MCE RC */
  211. toggle = (scancode & RC6_6A_MCE_TOGGLE_MASK) ? 1 : 0;
  212. scancode &= ~RC6_6A_MCE_TOGGLE_MASK;
  213. } else {
  214. toggle = 0;
  215. }
  216. IR_dprintk(1, "RC6(6A) scancode 0x%08x (toggle: %u)\n",
  217. scancode, toggle);
  218. break;
  219. default:
  220. IR_dprintk(1, "RC6 unknown mode\n");
  221. goto out;
  222. }
  223. rc_keydown(dev, scancode, toggle);
  224. data->state = STATE_INACTIVE;
  225. return 0;
  226. }
  227. out:
  228. IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n",
  229. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  230. data->state = STATE_INACTIVE;
  231. return -EINVAL;
  232. }
  233. static struct ir_raw_handler rc6_handler = {
  234. .protocols = RC_TYPE_RC6,
  235. .decode = ir_rc6_decode,
  236. };
  237. static int __init ir_rc6_decode_init(void)
  238. {
  239. ir_raw_handler_register(&rc6_handler);
  240. printk(KERN_INFO "IR RC6 protocol handler initialized\n");
  241. return 0;
  242. }
  243. static void __exit ir_rc6_decode_exit(void)
  244. {
  245. ir_raw_handler_unregister(&rc6_handler);
  246. }
  247. module_init(ir_rc6_decode_init);
  248. module_exit(ir_rc6_decode_exit);
  249. MODULE_LICENSE("GPL");
  250. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  251. MODULE_DESCRIPTION("RC6 IR protocol decoder");