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