ir-rc6-decoder.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 (!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. IR_dprintk(2, "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 (!is_transition(&ev, &dev->raw->prev_ev))
  128. break;
  129. if (data->count == RC6_HEADER_NBITS)
  130. data->state = STATE_TOGGLE_START;
  131. else
  132. data->state = STATE_HEADER_BIT_START;
  133. decrease_duration(&ev, RC6_BIT_END);
  134. goto again;
  135. case STATE_TOGGLE_START:
  136. if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
  137. break;
  138. data->toggle = ev.pulse;
  139. data->state = STATE_TOGGLE_END;
  140. return 0;
  141. case STATE_TOGGLE_END:
  142. if (!is_transition(&ev, &dev->raw->prev_ev) ||
  143. !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2))
  144. break;
  145. if (!(data->header & RC6_STARTBIT_MASK)) {
  146. IR_dprintk(1, "RC6 invalid start bit\n");
  147. break;
  148. }
  149. data->state = STATE_BODY_BIT_START;
  150. decrease_duration(&ev, RC6_TOGGLE_END);
  151. data->count = 0;
  152. data->body = 0;
  153. switch (rc6_mode(data)) {
  154. case RC6_MODE_0:
  155. data->wanted_bits = RC6_0_NBITS;
  156. break;
  157. case RC6_MODE_6A:
  158. data->wanted_bits = RC6_6A_NBITS;
  159. break;
  160. default:
  161. IR_dprintk(1, "RC6 unknown mode\n");
  162. goto out;
  163. }
  164. goto again;
  165. case STATE_BODY_BIT_START:
  166. if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) {
  167. /* Discard LSB's that won't fit in data->body */
  168. if (data->count++ < CHAR_BIT * sizeof data->body) {
  169. data->body <<= 1;
  170. if (ev.pulse)
  171. data->body |= 1;
  172. }
  173. data->state = STATE_BODY_BIT_END;
  174. return 0;
  175. } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse &&
  176. geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) {
  177. data->state = STATE_FINISHED;
  178. goto again;
  179. }
  180. break;
  181. case STATE_BODY_BIT_END:
  182. if (!is_transition(&ev, &dev->raw->prev_ev))
  183. break;
  184. if (data->count == data->wanted_bits)
  185. data->state = STATE_FINISHED;
  186. else
  187. data->state = STATE_BODY_BIT_START;
  188. decrease_duration(&ev, RC6_BIT_END);
  189. goto again;
  190. case STATE_FINISHED:
  191. if (ev.pulse)
  192. break;
  193. switch (rc6_mode(data)) {
  194. case RC6_MODE_0:
  195. scancode = data->body;
  196. toggle = data->toggle;
  197. protocol = RC_TYPE_RC6_0;
  198. IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n",
  199. scancode, toggle);
  200. break;
  201. case RC6_MODE_6A:
  202. if (data->count > CHAR_BIT * sizeof data->body) {
  203. IR_dprintk(1, "RC6 too many (%u) data bits\n",
  204. data->count);
  205. goto out;
  206. }
  207. scancode = data->body;
  208. switch (data->count) {
  209. case 20:
  210. protocol = RC_TYPE_RC6_6A_20;
  211. toggle = 0;
  212. break;
  213. case 24:
  214. protocol = RC_TYPE_RC6_6A_24;
  215. toggle = 0;
  216. break;
  217. case 32:
  218. if ((scancode & RC6_6A_LCC_MASK) == RC6_6A_MCE_CC) {
  219. protocol = RC_TYPE_RC6_MCE;
  220. toggle = !!(scancode & RC6_6A_MCE_TOGGLE_MASK);
  221. scancode &= ~RC6_6A_MCE_TOGGLE_MASK;
  222. } else {
  223. protocol = RC_TYPE_RC6_6A_32;
  224. toggle = 0;
  225. }
  226. break;
  227. default:
  228. IR_dprintk(1, "RC6(6A) unsupported length\n");
  229. goto out;
  230. }
  231. IR_dprintk(1, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n",
  232. protocol, scancode, toggle);
  233. break;
  234. default:
  235. IR_dprintk(1, "RC6 unknown mode\n");
  236. goto out;
  237. }
  238. rc_keydown(dev, protocol, scancode, toggle);
  239. data->state = STATE_INACTIVE;
  240. return 0;
  241. }
  242. out:
  243. IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n",
  244. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  245. data->state = STATE_INACTIVE;
  246. return -EINVAL;
  247. }
  248. static const struct ir_raw_timings_manchester ir_rc6_timings[4] = {
  249. {
  250. .leader = RC6_PREFIX_PULSE,
  251. .pulse_space_start = 0,
  252. .clock = RC6_UNIT,
  253. .invert = 1,
  254. .trailer_space = RC6_PREFIX_SPACE,
  255. },
  256. {
  257. .clock = RC6_UNIT,
  258. .invert = 1,
  259. },
  260. {
  261. .clock = RC6_UNIT * 2,
  262. .invert = 1,
  263. },
  264. {
  265. .clock = RC6_UNIT,
  266. .invert = 1,
  267. .trailer_space = RC6_SUFFIX_SPACE,
  268. },
  269. };
  270. /**
  271. * ir_rc6_encode() - Encode a scancode as a stream of raw events
  272. *
  273. * @protocol: protocol to encode
  274. * @scancode: scancode to encode
  275. * @events: array of raw ir events to write into
  276. * @max: maximum size of @events
  277. *
  278. * Returns: The number of events written.
  279. * -ENOBUFS if there isn't enough space in the array to fit the
  280. * encoding. In this case all @max events will have been written.
  281. * -EINVAL if the scancode is ambiguous or invalid.
  282. */
  283. static int ir_rc6_encode(enum rc_type protocol, u32 scancode,
  284. struct ir_raw_event *events, unsigned int max)
  285. {
  286. int ret;
  287. struct ir_raw_event *e = events;
  288. if (protocol == RC_TYPE_RC6_0) {
  289. /* Modulate the preamble */
  290. ret = ir_raw_gen_manchester(&e, max, &ir_rc6_timings[0], 0, 0);
  291. if (ret < 0)
  292. return ret;
  293. /* Modulate the header (Start Bit & Mode-0) */
  294. ret = ir_raw_gen_manchester(&e, max - (e - events),
  295. &ir_rc6_timings[1],
  296. RC6_HEADER_NBITS, (1 << 3));
  297. if (ret < 0)
  298. return ret;
  299. /* Modulate Trailer Bit */
  300. ret = ir_raw_gen_manchester(&e, max - (e - events),
  301. &ir_rc6_timings[2], 1, 0);
  302. if (ret < 0)
  303. return ret;
  304. /* Modulate rest of the data */
  305. ret = ir_raw_gen_manchester(&e, max - (e - events),
  306. &ir_rc6_timings[3], RC6_0_NBITS,
  307. scancode);
  308. if (ret < 0)
  309. return ret;
  310. } else {
  311. int bits;
  312. switch (protocol) {
  313. case RC_TYPE_RC6_MCE:
  314. case RC_TYPE_RC6_6A_32:
  315. bits = 32;
  316. break;
  317. case RC_TYPE_RC6_6A_24:
  318. bits = 24;
  319. break;
  320. case RC_TYPE_RC6_6A_20:
  321. bits = 20;
  322. break;
  323. default:
  324. return -EINVAL;
  325. }
  326. /* Modulate the preamble */
  327. ret = ir_raw_gen_manchester(&e, max, &ir_rc6_timings[0], 0, 0);
  328. if (ret < 0)
  329. return ret;
  330. /* Modulate the header (Start Bit & Header-version 6 */
  331. ret = ir_raw_gen_manchester(&e, max - (e - events),
  332. &ir_rc6_timings[1],
  333. RC6_HEADER_NBITS, (1 << 3 | 6));
  334. if (ret < 0)
  335. return ret;
  336. /* Modulate Trailer Bit */
  337. ret = ir_raw_gen_manchester(&e, max - (e - events),
  338. &ir_rc6_timings[2], 1, 0);
  339. if (ret < 0)
  340. return ret;
  341. /* Modulate rest of the data */
  342. ret = ir_raw_gen_manchester(&e, max - (e - events),
  343. &ir_rc6_timings[3],
  344. bits,
  345. scancode);
  346. if (ret < 0)
  347. return ret;
  348. }
  349. return e - events;
  350. }
  351. static struct ir_raw_handler rc6_handler = {
  352. .protocols = RC_BIT_RC6_0 | RC_BIT_RC6_6A_20 |
  353. RC_BIT_RC6_6A_24 | RC_BIT_RC6_6A_32 |
  354. RC_BIT_RC6_MCE,
  355. .decode = ir_rc6_decode,
  356. .encode = ir_rc6_encode,
  357. };
  358. static int __init ir_rc6_decode_init(void)
  359. {
  360. ir_raw_handler_register(&rc6_handler);
  361. printk(KERN_INFO "IR RC6 protocol handler initialized\n");
  362. return 0;
  363. }
  364. static void __exit ir_rc6_decode_exit(void)
  365. {
  366. ir_raw_handler_unregister(&rc6_handler);
  367. }
  368. module_init(ir_rc6_decode_init);
  369. module_exit(ir_rc6_decode_exit);
  370. MODULE_LICENSE("GPL");
  371. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  372. MODULE_DESCRIPTION("RC6 IR protocol decoder");