ir-rc6-decoder.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_proto protocol;
  85. if (!is_timing_event(ev)) {
  86. if (ev.reset)
  87. data->state = STATE_INACTIVE;
  88. return 0;
  89. }
  90. if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  91. goto out;
  92. again:
  93. dev_dbg(&dev->dev, "RC6 decode started at state %i (%uus %s)\n",
  94. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  95. if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  96. return 0;
  97. switch (data->state) {
  98. case STATE_INACTIVE:
  99. if (!ev.pulse)
  100. break;
  101. /* Note: larger margin on first pulse since each RC6_UNIT
  102. is quite short and some hardware takes some time to
  103. adjust to the signal */
  104. if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
  105. break;
  106. data->state = STATE_PREFIX_SPACE;
  107. data->count = 0;
  108. return 0;
  109. case STATE_PREFIX_SPACE:
  110. if (ev.pulse)
  111. break;
  112. if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
  113. break;
  114. data->state = STATE_HEADER_BIT_START;
  115. data->header = 0;
  116. return 0;
  117. case STATE_HEADER_BIT_START:
  118. if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
  119. break;
  120. data->header <<= 1;
  121. if (ev.pulse)
  122. data->header |= 1;
  123. data->count++;
  124. data->state = STATE_HEADER_BIT_END;
  125. return 0;
  126. case STATE_HEADER_BIT_END:
  127. if (data->count == RC6_HEADER_NBITS)
  128. data->state = STATE_TOGGLE_START;
  129. else
  130. data->state = STATE_HEADER_BIT_START;
  131. decrease_duration(&ev, RC6_BIT_END);
  132. goto again;
  133. case STATE_TOGGLE_START:
  134. if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
  135. break;
  136. data->toggle = ev.pulse;
  137. data->state = STATE_TOGGLE_END;
  138. return 0;
  139. case STATE_TOGGLE_END:
  140. if (!(data->header & RC6_STARTBIT_MASK)) {
  141. dev_dbg(&dev->dev, "RC6 invalid start bit\n");
  142. break;
  143. }
  144. data->state = STATE_BODY_BIT_START;
  145. decrease_duration(&ev, RC6_TOGGLE_END);
  146. data->count = 0;
  147. data->body = 0;
  148. switch (rc6_mode(data)) {
  149. case RC6_MODE_0:
  150. data->wanted_bits = RC6_0_NBITS;
  151. break;
  152. case RC6_MODE_6A:
  153. data->wanted_bits = RC6_6A_NBITS;
  154. break;
  155. default:
  156. dev_dbg(&dev->dev, "RC6 unknown mode\n");
  157. goto out;
  158. }
  159. goto again;
  160. case STATE_BODY_BIT_START:
  161. if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) {
  162. /* Discard LSB's that won't fit in data->body */
  163. if (data->count++ < CHAR_BIT * sizeof data->body) {
  164. data->body <<= 1;
  165. if (ev.pulse)
  166. data->body |= 1;
  167. }
  168. data->state = STATE_BODY_BIT_END;
  169. return 0;
  170. } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse &&
  171. geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) {
  172. data->state = STATE_FINISHED;
  173. goto again;
  174. }
  175. break;
  176. case STATE_BODY_BIT_END:
  177. if (data->count == data->wanted_bits)
  178. data->state = STATE_FINISHED;
  179. else
  180. data->state = STATE_BODY_BIT_START;
  181. decrease_duration(&ev, RC6_BIT_END);
  182. goto again;
  183. case STATE_FINISHED:
  184. if (ev.pulse)
  185. break;
  186. switch (rc6_mode(data)) {
  187. case RC6_MODE_0:
  188. scancode = data->body;
  189. toggle = data->toggle;
  190. protocol = RC_PROTO_RC6_0;
  191. dev_dbg(&dev->dev, "RC6(0) scancode 0x%04x (toggle: %u)\n",
  192. scancode, toggle);
  193. break;
  194. case RC6_MODE_6A:
  195. if (data->count > CHAR_BIT * sizeof data->body) {
  196. dev_dbg(&dev->dev, "RC6 too many (%u) data bits\n",
  197. data->count);
  198. goto out;
  199. }
  200. scancode = data->body;
  201. switch (data->count) {
  202. case 20:
  203. protocol = RC_PROTO_RC6_6A_20;
  204. toggle = 0;
  205. break;
  206. case 24:
  207. protocol = RC_PROTO_RC6_6A_24;
  208. toggle = 0;
  209. break;
  210. case 32:
  211. if ((scancode & RC6_6A_LCC_MASK) == RC6_6A_MCE_CC) {
  212. protocol = RC_PROTO_RC6_MCE;
  213. toggle = !!(scancode & RC6_6A_MCE_TOGGLE_MASK);
  214. scancode &= ~RC6_6A_MCE_TOGGLE_MASK;
  215. } else {
  216. protocol = RC_PROTO_RC6_6A_32;
  217. toggle = 0;
  218. }
  219. break;
  220. default:
  221. dev_dbg(&dev->dev, "RC6(6A) unsupported length\n");
  222. goto out;
  223. }
  224. dev_dbg(&dev->dev, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n",
  225. protocol, scancode, toggle);
  226. break;
  227. default:
  228. dev_dbg(&dev->dev, "RC6 unknown mode\n");
  229. goto out;
  230. }
  231. rc_keydown(dev, protocol, scancode, toggle);
  232. data->state = STATE_INACTIVE;
  233. return 0;
  234. }
  235. out:
  236. dev_dbg(&dev->dev, "RC6 decode failed at state %i (%uus %s)\n",
  237. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  238. data->state = STATE_INACTIVE;
  239. return -EINVAL;
  240. }
  241. static const struct ir_raw_timings_manchester ir_rc6_timings[4] = {
  242. {
  243. .leader_pulse = RC6_PREFIX_PULSE,
  244. .leader_space = RC6_PREFIX_SPACE,
  245. .clock = RC6_UNIT,
  246. .invert = 1,
  247. },
  248. {
  249. .clock = RC6_UNIT * 2,
  250. .invert = 1,
  251. },
  252. {
  253. .clock = RC6_UNIT,
  254. .invert = 1,
  255. .trailer_space = RC6_SUFFIX_SPACE,
  256. },
  257. };
  258. /**
  259. * ir_rc6_encode() - Encode a scancode as a stream of raw events
  260. *
  261. * @protocol: protocol to encode
  262. * @scancode: scancode to encode
  263. * @events: array of raw ir events to write into
  264. * @max: maximum size of @events
  265. *
  266. * Returns: The number of events written.
  267. * -ENOBUFS if there isn't enough space in the array to fit the
  268. * encoding. In this case all @max events will have been written.
  269. * -EINVAL if the scancode is ambiguous or invalid.
  270. */
  271. static int ir_rc6_encode(enum rc_proto protocol, u32 scancode,
  272. struct ir_raw_event *events, unsigned int max)
  273. {
  274. int ret;
  275. struct ir_raw_event *e = events;
  276. if (protocol == RC_PROTO_RC6_0) {
  277. /* Modulate the header (Start Bit & Mode-0) */
  278. ret = ir_raw_gen_manchester(&e, max - (e - events),
  279. &ir_rc6_timings[0],
  280. RC6_HEADER_NBITS, (1 << 3));
  281. if (ret < 0)
  282. return ret;
  283. /* Modulate Trailer Bit */
  284. ret = ir_raw_gen_manchester(&e, max - (e - events),
  285. &ir_rc6_timings[1], 1, 0);
  286. if (ret < 0)
  287. return ret;
  288. /* Modulate rest of the data */
  289. ret = ir_raw_gen_manchester(&e, max - (e - events),
  290. &ir_rc6_timings[2], RC6_0_NBITS,
  291. scancode);
  292. if (ret < 0)
  293. return ret;
  294. } else {
  295. int bits;
  296. switch (protocol) {
  297. case RC_PROTO_RC6_MCE:
  298. case RC_PROTO_RC6_6A_32:
  299. bits = 32;
  300. break;
  301. case RC_PROTO_RC6_6A_24:
  302. bits = 24;
  303. break;
  304. case RC_PROTO_RC6_6A_20:
  305. bits = 20;
  306. break;
  307. default:
  308. return -EINVAL;
  309. }
  310. /* Modulate the header (Start Bit & Header-version 6 */
  311. ret = ir_raw_gen_manchester(&e, max - (e - events),
  312. &ir_rc6_timings[0],
  313. RC6_HEADER_NBITS, (1 << 3 | 6));
  314. if (ret < 0)
  315. return ret;
  316. /* Modulate Trailer Bit */
  317. ret = ir_raw_gen_manchester(&e, max - (e - events),
  318. &ir_rc6_timings[1], 1, 0);
  319. if (ret < 0)
  320. return ret;
  321. /* Modulate rest of the data */
  322. ret = ir_raw_gen_manchester(&e, max - (e - events),
  323. &ir_rc6_timings[2],
  324. bits,
  325. scancode);
  326. if (ret < 0)
  327. return ret;
  328. }
  329. return e - events;
  330. }
  331. static struct ir_raw_handler rc6_handler = {
  332. .protocols = RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 |
  333. RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 |
  334. RC_PROTO_BIT_RC6_MCE,
  335. .decode = ir_rc6_decode,
  336. .encode = ir_rc6_encode,
  337. .carrier = 36000,
  338. .min_timeout = RC6_SUFFIX_SPACE,
  339. };
  340. static int __init ir_rc6_decode_init(void)
  341. {
  342. ir_raw_handler_register(&rc6_handler);
  343. printk(KERN_INFO "IR RC6 protocol handler initialized\n");
  344. return 0;
  345. }
  346. static void __exit ir_rc6_decode_exit(void)
  347. {
  348. ir_raw_handler_unregister(&rc6_handler);
  349. }
  350. module_init(ir_rc6_decode_init);
  351. module_exit(ir_rc6_decode_exit);
  352. MODULE_LICENSE("GPL");
  353. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  354. MODULE_DESCRIPTION("RC6 IR protocol decoder");