ir-rc5-decoder.c 8.0 KB

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