ir-rc6-decoder.c 9.5 KB

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