ir-rc6-decoder.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #define RC6_6A_KATHREIN_CC 0x80460000 /* Kathrein RCU-676 customer code */
  41. #ifndef CHAR_BIT
  42. #define CHAR_BIT 8 /* Normally in <limits.h> */
  43. #endif
  44. enum rc6_mode {
  45. RC6_MODE_0,
  46. RC6_MODE_6A,
  47. RC6_MODE_UNKNOWN,
  48. };
  49. enum rc6_state {
  50. STATE_INACTIVE,
  51. STATE_PREFIX_SPACE,
  52. STATE_HEADER_BIT_START,
  53. STATE_HEADER_BIT_END,
  54. STATE_TOGGLE_START,
  55. STATE_TOGGLE_END,
  56. STATE_BODY_BIT_START,
  57. STATE_BODY_BIT_END,
  58. STATE_FINISHED,
  59. };
  60. static enum rc6_mode rc6_mode(struct rc6_dec *data)
  61. {
  62. switch (data->header & RC6_MODE_MASK) {
  63. case 0:
  64. return RC6_MODE_0;
  65. case 6:
  66. if (!data->toggle)
  67. return RC6_MODE_6A;
  68. /* fall through */
  69. default:
  70. return RC6_MODE_UNKNOWN;
  71. }
  72. }
  73. /**
  74. * ir_rc6_decode() - Decode one RC6 pulse or space
  75. * @dev: the struct rc_dev descriptor of the device
  76. * @ev: the struct ir_raw_event descriptor of the pulse/space
  77. *
  78. * This function returns -EINVAL if the pulse violates the state machine
  79. */
  80. static int ir_rc6_decode(struct rc_dev *dev, struct ir_raw_event ev)
  81. {
  82. struct rc6_dec *data = &dev->raw->rc6;
  83. u32 scancode;
  84. u8 toggle;
  85. enum rc_proto protocol;
  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. dev_dbg(&dev->dev, "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 (data->count == RC6_HEADER_NBITS)
  129. data->state = STATE_TOGGLE_START;
  130. else
  131. data->state = STATE_HEADER_BIT_START;
  132. decrease_duration(&ev, RC6_BIT_END);
  133. goto again;
  134. case STATE_TOGGLE_START:
  135. if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
  136. break;
  137. data->toggle = ev.pulse;
  138. data->state = STATE_TOGGLE_END;
  139. return 0;
  140. case STATE_TOGGLE_END:
  141. if (!(data->header & RC6_STARTBIT_MASK)) {
  142. dev_dbg(&dev->dev, "RC6 invalid start bit\n");
  143. break;
  144. }
  145. data->state = STATE_BODY_BIT_START;
  146. decrease_duration(&ev, RC6_TOGGLE_END);
  147. data->count = 0;
  148. data->body = 0;
  149. switch (rc6_mode(data)) {
  150. case RC6_MODE_0:
  151. data->wanted_bits = RC6_0_NBITS;
  152. break;
  153. case RC6_MODE_6A:
  154. data->wanted_bits = RC6_6A_NBITS;
  155. break;
  156. default:
  157. dev_dbg(&dev->dev, "RC6 unknown mode\n");
  158. goto out;
  159. }
  160. goto again;
  161. case STATE_BODY_BIT_START:
  162. if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) {
  163. /* Discard LSB's that won't fit in data->body */
  164. if (data->count++ < CHAR_BIT * sizeof data->body) {
  165. data->body <<= 1;
  166. if (ev.pulse)
  167. data->body |= 1;
  168. }
  169. data->state = STATE_BODY_BIT_END;
  170. return 0;
  171. } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse &&
  172. geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) {
  173. data->state = STATE_FINISHED;
  174. goto again;
  175. }
  176. break;
  177. case STATE_BODY_BIT_END:
  178. if (data->count == data->wanted_bits)
  179. data->state = STATE_FINISHED;
  180. else
  181. data->state = STATE_BODY_BIT_START;
  182. decrease_duration(&ev, RC6_BIT_END);
  183. goto again;
  184. case STATE_FINISHED:
  185. if (ev.pulse)
  186. break;
  187. switch (rc6_mode(data)) {
  188. case RC6_MODE_0:
  189. scancode = data->body;
  190. toggle = data->toggle;
  191. protocol = RC_PROTO_RC6_0;
  192. dev_dbg(&dev->dev, "RC6(0) scancode 0x%04x (toggle: %u)\n",
  193. scancode, toggle);
  194. break;
  195. case RC6_MODE_6A:
  196. if (data->count > CHAR_BIT * sizeof data->body) {
  197. dev_dbg(&dev->dev, "RC6 too many (%u) data bits\n",
  198. data->count);
  199. goto out;
  200. }
  201. scancode = data->body;
  202. switch (data->count) {
  203. case 20:
  204. protocol = RC_PROTO_RC6_6A_20;
  205. toggle = 0;
  206. break;
  207. case 24:
  208. protocol = RC_PROTO_RC6_6A_24;
  209. toggle = 0;
  210. break;
  211. case 32:
  212. switch (scancode & RC6_6A_LCC_MASK) {
  213. case RC6_6A_MCE_CC:
  214. case RC6_6A_KATHREIN_CC:
  215. protocol = RC_PROTO_RC6_MCE;
  216. toggle = !!(scancode & RC6_6A_MCE_TOGGLE_MASK);
  217. scancode &= ~RC6_6A_MCE_TOGGLE_MASK;
  218. break;
  219. default:
  220. protocol = RC_PROTO_RC6_6A_32;
  221. toggle = 0;
  222. break;
  223. }
  224. break;
  225. default:
  226. dev_dbg(&dev->dev, "RC6(6A) unsupported length\n");
  227. goto out;
  228. }
  229. dev_dbg(&dev->dev, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n",
  230. protocol, scancode, toggle);
  231. break;
  232. default:
  233. dev_dbg(&dev->dev, "RC6 unknown mode\n");
  234. goto out;
  235. }
  236. rc_keydown(dev, protocol, scancode, toggle);
  237. data->state = STATE_INACTIVE;
  238. return 0;
  239. }
  240. out:
  241. dev_dbg(&dev->dev, "RC6 decode failed at state %i (%uus %s)\n",
  242. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  243. data->state = STATE_INACTIVE;
  244. return -EINVAL;
  245. }
  246. static const struct ir_raw_timings_manchester ir_rc6_timings[4] = {
  247. {
  248. .leader_pulse = RC6_PREFIX_PULSE,
  249. .leader_space = RC6_PREFIX_SPACE,
  250. .clock = RC6_UNIT,
  251. .invert = 1,
  252. },
  253. {
  254. .clock = RC6_UNIT * 2,
  255. .invert = 1,
  256. },
  257. {
  258. .clock = RC6_UNIT,
  259. .invert = 1,
  260. .trailer_space = RC6_SUFFIX_SPACE,
  261. },
  262. };
  263. /**
  264. * ir_rc6_encode() - Encode a scancode as a stream of raw events
  265. *
  266. * @protocol: protocol to encode
  267. * @scancode: scancode to encode
  268. * @events: array of raw ir events to write into
  269. * @max: maximum size of @events
  270. *
  271. * Returns: The number of events written.
  272. * -ENOBUFS if there isn't enough space in the array to fit the
  273. * encoding. In this case all @max events will have been written.
  274. * -EINVAL if the scancode is ambiguous or invalid.
  275. */
  276. static int ir_rc6_encode(enum rc_proto protocol, u32 scancode,
  277. struct ir_raw_event *events, unsigned int max)
  278. {
  279. int ret;
  280. struct ir_raw_event *e = events;
  281. if (protocol == RC_PROTO_RC6_0) {
  282. /* Modulate the header (Start Bit & Mode-0) */
  283. ret = ir_raw_gen_manchester(&e, max - (e - events),
  284. &ir_rc6_timings[0],
  285. RC6_HEADER_NBITS, (1 << 3));
  286. if (ret < 0)
  287. return ret;
  288. /* Modulate Trailer Bit */
  289. ret = ir_raw_gen_manchester(&e, max - (e - events),
  290. &ir_rc6_timings[1], 1, 0);
  291. if (ret < 0)
  292. return ret;
  293. /* Modulate rest of the data */
  294. ret = ir_raw_gen_manchester(&e, max - (e - events),
  295. &ir_rc6_timings[2], RC6_0_NBITS,
  296. scancode);
  297. if (ret < 0)
  298. return ret;
  299. } else {
  300. int bits;
  301. switch (protocol) {
  302. case RC_PROTO_RC6_MCE:
  303. case RC_PROTO_RC6_6A_32:
  304. bits = 32;
  305. break;
  306. case RC_PROTO_RC6_6A_24:
  307. bits = 24;
  308. break;
  309. case RC_PROTO_RC6_6A_20:
  310. bits = 20;
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. /* Modulate the header (Start Bit & Header-version 6 */
  316. ret = ir_raw_gen_manchester(&e, max - (e - events),
  317. &ir_rc6_timings[0],
  318. RC6_HEADER_NBITS, (1 << 3 | 6));
  319. if (ret < 0)
  320. return ret;
  321. /* Modulate Trailer Bit */
  322. ret = ir_raw_gen_manchester(&e, max - (e - events),
  323. &ir_rc6_timings[1], 1, 0);
  324. if (ret < 0)
  325. return ret;
  326. /* Modulate rest of the data */
  327. ret = ir_raw_gen_manchester(&e, max - (e - events),
  328. &ir_rc6_timings[2],
  329. bits,
  330. scancode);
  331. if (ret < 0)
  332. return ret;
  333. }
  334. return e - events;
  335. }
  336. static struct ir_raw_handler rc6_handler = {
  337. .protocols = RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 |
  338. RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 |
  339. RC_PROTO_BIT_RC6_MCE,
  340. .decode = ir_rc6_decode,
  341. .encode = ir_rc6_encode,
  342. .carrier = 36000,
  343. .min_timeout = RC6_SUFFIX_SPACE,
  344. };
  345. static int __init ir_rc6_decode_init(void)
  346. {
  347. ir_raw_handler_register(&rc6_handler);
  348. printk(KERN_INFO "IR RC6 protocol handler initialized\n");
  349. return 0;
  350. }
  351. static void __exit ir_rc6_decode_exit(void)
  352. {
  353. ir_raw_handler_unregister(&rc6_handler);
  354. }
  355. module_init(ir_rc6_decode_init);
  356. module_exit(ir_rc6_decode_exit);
  357. MODULE_LICENSE("GPL");
  358. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  359. MODULE_DESCRIPTION("RC6 IR protocol decoder");