ir-rc6-decoder.c 6.6 KB

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