ir-rc6-decoder.c 7.6 KB

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