ir-rc5-decoder.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0
  2. // ir-rc5-decoder.c - decoder for RC5(x) and StreamZap protocols
  3. //
  4. // Copyright (C) 2010 by Mauro Carvalho Chehab
  5. // Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
  6. /*
  7. * This decoder handles the 14 bit RC5 protocol, 15 bit "StreamZap" protocol
  8. * and 20 bit RC5x protocol.
  9. */
  10. #include "rc-core-priv.h"
  11. #include <linux/module.h>
  12. #define RC5_NBITS 14
  13. #define RC5_SZ_NBITS 15
  14. #define RC5X_NBITS 20
  15. #define CHECK_RC5X_NBITS 8
  16. #define RC5_UNIT 889 /* us */
  17. #define RC5_BIT_START (1 * RC5_UNIT)
  18. #define RC5_BIT_END (1 * RC5_UNIT)
  19. #define RC5X_SPACE (4 * RC5_UNIT)
  20. #define RC5_TRAILER (6 * RC5_UNIT) /* In reality, approx 100 */
  21. enum rc5_state {
  22. STATE_INACTIVE,
  23. STATE_BIT_START,
  24. STATE_BIT_END,
  25. STATE_CHECK_RC5X,
  26. STATE_FINISHED,
  27. };
  28. /**
  29. * ir_rc5_decode() - Decode one RC-5 pulse or space
  30. * @dev: the struct rc_dev descriptor of the device
  31. * @ev: the struct ir_raw_event descriptor of the pulse/space
  32. *
  33. * This function returns -EINVAL if the pulse violates the state machine
  34. */
  35. static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
  36. {
  37. struct rc5_dec *data = &dev->raw->rc5;
  38. u8 toggle;
  39. u32 scancode;
  40. enum rc_proto protocol;
  41. if (!is_timing_event(ev)) {
  42. if (ev.reset)
  43. data->state = STATE_INACTIVE;
  44. return 0;
  45. }
  46. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  47. goto out;
  48. again:
  49. dev_dbg(&dev->dev, "RC5(x/sz) decode started at state %i (%uus %s)\n",
  50. data->state, ev.duration, TO_STR(ev.pulse));
  51. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  52. return 0;
  53. switch (data->state) {
  54. case STATE_INACTIVE:
  55. if (!ev.pulse)
  56. break;
  57. data->state = STATE_BIT_START;
  58. data->count = 1;
  59. decrease_duration(&ev, RC5_BIT_START);
  60. goto again;
  61. case STATE_BIT_START:
  62. if (!ev.pulse && geq_margin(ev.duration, RC5_TRAILER, RC5_UNIT / 2)) {
  63. data->state = STATE_FINISHED;
  64. goto again;
  65. }
  66. if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
  67. break;
  68. data->bits <<= 1;
  69. if (!ev.pulse)
  70. data->bits |= 1;
  71. data->count++;
  72. data->state = STATE_BIT_END;
  73. return 0;
  74. case STATE_BIT_END:
  75. if (data->count == CHECK_RC5X_NBITS)
  76. data->state = STATE_CHECK_RC5X;
  77. else
  78. data->state = STATE_BIT_START;
  79. decrease_duration(&ev, RC5_BIT_END);
  80. goto again;
  81. case STATE_CHECK_RC5X:
  82. if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
  83. data->is_rc5x = true;
  84. decrease_duration(&ev, RC5X_SPACE);
  85. } else
  86. data->is_rc5x = false;
  87. data->state = STATE_BIT_START;
  88. goto again;
  89. case STATE_FINISHED:
  90. if (ev.pulse)
  91. break;
  92. if (data->is_rc5x && data->count == RC5X_NBITS) {
  93. /* RC5X */
  94. u8 xdata, command, system;
  95. if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5X_20)) {
  96. data->state = STATE_INACTIVE;
  97. return 0;
  98. }
  99. xdata = (data->bits & 0x0003F) >> 0;
  100. command = (data->bits & 0x00FC0) >> 6;
  101. system = (data->bits & 0x1F000) >> 12;
  102. toggle = (data->bits & 0x20000) ? 1 : 0;
  103. command += (data->bits & 0x40000) ? 0 : 0x40;
  104. scancode = system << 16 | command << 8 | xdata;
  105. protocol = RC_PROTO_RC5X_20;
  106. } else if (!data->is_rc5x && data->count == RC5_NBITS) {
  107. /* RC5 */
  108. u8 command, system;
  109. if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5)) {
  110. data->state = STATE_INACTIVE;
  111. return 0;
  112. }
  113. command = (data->bits & 0x0003F) >> 0;
  114. system = (data->bits & 0x007C0) >> 6;
  115. toggle = (data->bits & 0x00800) ? 1 : 0;
  116. command += (data->bits & 0x01000) ? 0 : 0x40;
  117. scancode = system << 8 | command;
  118. protocol = RC_PROTO_RC5;
  119. } else if (!data->is_rc5x && data->count == RC5_SZ_NBITS) {
  120. /* RC5 StreamZap */
  121. u8 command, system;
  122. if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5_SZ)) {
  123. data->state = STATE_INACTIVE;
  124. return 0;
  125. }
  126. command = (data->bits & 0x0003F) >> 0;
  127. system = (data->bits & 0x02FC0) >> 6;
  128. toggle = (data->bits & 0x01000) ? 1 : 0;
  129. scancode = system << 6 | command;
  130. protocol = RC_PROTO_RC5_SZ;
  131. } else
  132. break;
  133. dev_dbg(&dev->dev, "RC5(x/sz) scancode 0x%06x (p: %u, t: %u)\n",
  134. scancode, protocol, toggle);
  135. rc_keydown(dev, protocol, scancode, toggle);
  136. data->state = STATE_INACTIVE;
  137. return 0;
  138. }
  139. out:
  140. dev_dbg(&dev->dev, "RC5(x/sz) decode failed at state %i count %d (%uus %s)\n",
  141. data->state, data->count, ev.duration, TO_STR(ev.pulse));
  142. data->state = STATE_INACTIVE;
  143. return -EINVAL;
  144. }
  145. static const struct ir_raw_timings_manchester ir_rc5_timings = {
  146. .leader_pulse = RC5_UNIT,
  147. .clock = RC5_UNIT,
  148. .trailer_space = RC5_UNIT * 10,
  149. };
  150. static const struct ir_raw_timings_manchester ir_rc5x_timings[2] = {
  151. {
  152. .leader_pulse = RC5_UNIT,
  153. .clock = RC5_UNIT,
  154. .trailer_space = RC5X_SPACE,
  155. },
  156. {
  157. .clock = RC5_UNIT,
  158. .trailer_space = RC5_UNIT * 10,
  159. },
  160. };
  161. static const struct ir_raw_timings_manchester ir_rc5_sz_timings = {
  162. .leader_pulse = RC5_UNIT,
  163. .clock = RC5_UNIT,
  164. .trailer_space = RC5_UNIT * 10,
  165. };
  166. /**
  167. * ir_rc5_encode() - Encode a scancode as a stream of raw events
  168. *
  169. * @protocol: protocol variant to encode
  170. * @scancode: scancode to encode
  171. * @events: array of raw ir events to write into
  172. * @max: maximum size of @events
  173. *
  174. * Returns: The number of events written.
  175. * -ENOBUFS if there isn't enough space in the array to fit the
  176. * encoding. In this case all @max events will have been written.
  177. * -EINVAL if the scancode is ambiguous or invalid.
  178. */
  179. static int ir_rc5_encode(enum rc_proto protocol, u32 scancode,
  180. struct ir_raw_event *events, unsigned int max)
  181. {
  182. int ret;
  183. struct ir_raw_event *e = events;
  184. unsigned int data, xdata, command, commandx, system, pre_space_data;
  185. /* Detect protocol and convert scancode to raw data */
  186. if (protocol == RC_PROTO_RC5) {
  187. /* decode scancode */
  188. command = (scancode & 0x003f) >> 0;
  189. commandx = (scancode & 0x0040) >> 6;
  190. system = (scancode & 0x1f00) >> 8;
  191. /* encode data */
  192. data = !commandx << 12 | system << 6 | command;
  193. /* First bit is encoded by leader_pulse */
  194. ret = ir_raw_gen_manchester(&e, max, &ir_rc5_timings,
  195. RC5_NBITS - 1, data);
  196. if (ret < 0)
  197. return ret;
  198. } else if (protocol == RC_PROTO_RC5X_20) {
  199. /* decode scancode */
  200. xdata = (scancode & 0x00003f) >> 0;
  201. command = (scancode & 0x003f00) >> 8;
  202. commandx = !(scancode & 0x004000);
  203. system = (scancode & 0x1f0000) >> 16;
  204. /* encode data */
  205. data = commandx << 18 | system << 12 | command << 6 | xdata;
  206. /* First bit is encoded by leader_pulse */
  207. pre_space_data = data >> (RC5X_NBITS - CHECK_RC5X_NBITS);
  208. ret = ir_raw_gen_manchester(&e, max, &ir_rc5x_timings[0],
  209. CHECK_RC5X_NBITS - 1,
  210. pre_space_data);
  211. if (ret < 0)
  212. return ret;
  213. ret = ir_raw_gen_manchester(&e, max - (e - events),
  214. &ir_rc5x_timings[1],
  215. RC5X_NBITS - CHECK_RC5X_NBITS,
  216. data);
  217. if (ret < 0)
  218. return ret;
  219. } else if (protocol == RC_PROTO_RC5_SZ) {
  220. /* RC5-SZ scancode is raw enough for Manchester as it is */
  221. /* First bit is encoded by leader_pulse */
  222. ret = ir_raw_gen_manchester(&e, max, &ir_rc5_sz_timings,
  223. RC5_SZ_NBITS - 1,
  224. scancode & 0x2fff);
  225. if (ret < 0)
  226. return ret;
  227. } else {
  228. return -EINVAL;
  229. }
  230. return e - events;
  231. }
  232. static struct ir_raw_handler rc5_handler = {
  233. .protocols = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC5X_20 |
  234. RC_PROTO_BIT_RC5_SZ,
  235. .decode = ir_rc5_decode,
  236. .encode = ir_rc5_encode,
  237. .carrier = 36000,
  238. .min_timeout = RC5_TRAILER,
  239. };
  240. static int __init ir_rc5_decode_init(void)
  241. {
  242. ir_raw_handler_register(&rc5_handler);
  243. printk(KERN_INFO "IR RC5(x/sz) protocol handler initialized\n");
  244. return 0;
  245. }
  246. static void __exit ir_rc5_decode_exit(void)
  247. {
  248. ir_raw_handler_unregister(&rc5_handler);
  249. }
  250. module_init(ir_rc5_decode_init);
  251. module_exit(ir_rc5_decode_exit);
  252. MODULE_LICENSE("GPL v2");
  253. MODULE_AUTHOR("Mauro Carvalho Chehab and Jarod Wilson");
  254. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  255. MODULE_DESCRIPTION("RC5(x/sz) IR protocol decoder");