ir-nec-decoder.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0
  2. // ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
  3. //
  4. // Copyright (C) 2010 by Mauro Carvalho Chehab
  5. #include <linux/bitrev.h>
  6. #include <linux/module.h>
  7. #include "rc-core-priv.h"
  8. #define NEC_NBITS 32
  9. #define NEC_UNIT 562500 /* ns */
  10. #define NEC_HEADER_PULSE (16 * NEC_UNIT)
  11. #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */
  12. #define NEC_HEADER_SPACE (8 * NEC_UNIT)
  13. #define NEC_REPEAT_SPACE (4 * NEC_UNIT)
  14. #define NEC_BIT_PULSE (1 * NEC_UNIT)
  15. #define NEC_BIT_0_SPACE (1 * NEC_UNIT)
  16. #define NEC_BIT_1_SPACE (3 * NEC_UNIT)
  17. #define NEC_TRAILER_PULSE (1 * NEC_UNIT)
  18. #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */
  19. #define NECX_REPEAT_BITS 1
  20. enum nec_state {
  21. STATE_INACTIVE,
  22. STATE_HEADER_SPACE,
  23. STATE_BIT_PULSE,
  24. STATE_BIT_SPACE,
  25. STATE_TRAILER_PULSE,
  26. STATE_TRAILER_SPACE,
  27. };
  28. /**
  29. * ir_nec_decode() - Decode one NEC 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_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
  36. {
  37. struct nec_dec *data = &dev->raw->nec;
  38. u32 scancode;
  39. enum rc_proto rc_proto;
  40. u8 address, not_address, command, not_command;
  41. if (!is_timing_event(ev)) {
  42. if (ev.reset)
  43. data->state = STATE_INACTIVE;
  44. return 0;
  45. }
  46. dev_dbg(&dev->dev, "NEC decode started at state %d (%uus %s)\n",
  47. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  48. switch (data->state) {
  49. case STATE_INACTIVE:
  50. if (!ev.pulse)
  51. break;
  52. if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
  53. data->is_nec_x = false;
  54. data->necx_repeat = false;
  55. } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
  56. data->is_nec_x = true;
  57. else
  58. break;
  59. data->count = 0;
  60. data->state = STATE_HEADER_SPACE;
  61. return 0;
  62. case STATE_HEADER_SPACE:
  63. if (ev.pulse)
  64. break;
  65. if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
  66. data->state = STATE_BIT_PULSE;
  67. return 0;
  68. } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
  69. data->state = STATE_TRAILER_PULSE;
  70. return 0;
  71. }
  72. break;
  73. case STATE_BIT_PULSE:
  74. if (!ev.pulse)
  75. break;
  76. if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
  77. break;
  78. data->state = STATE_BIT_SPACE;
  79. return 0;
  80. case STATE_BIT_SPACE:
  81. if (ev.pulse)
  82. break;
  83. if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
  84. geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
  85. dev_dbg(&dev->dev, "Repeat last key\n");
  86. rc_repeat(dev);
  87. data->state = STATE_INACTIVE;
  88. return 0;
  89. } else if (data->count > NECX_REPEAT_BITS)
  90. data->necx_repeat = false;
  91. data->bits <<= 1;
  92. if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
  93. data->bits |= 1;
  94. else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
  95. break;
  96. data->count++;
  97. if (data->count == NEC_NBITS)
  98. data->state = STATE_TRAILER_PULSE;
  99. else
  100. data->state = STATE_BIT_PULSE;
  101. return 0;
  102. case STATE_TRAILER_PULSE:
  103. if (!ev.pulse)
  104. break;
  105. if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
  106. break;
  107. data->state = STATE_TRAILER_SPACE;
  108. return 0;
  109. case STATE_TRAILER_SPACE:
  110. if (ev.pulse)
  111. break;
  112. if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
  113. break;
  114. if (data->count == NEC_NBITS) {
  115. address = bitrev8((data->bits >> 24) & 0xff);
  116. not_address = bitrev8((data->bits >> 16) & 0xff);
  117. command = bitrev8((data->bits >> 8) & 0xff);
  118. not_command = bitrev8((data->bits >> 0) & 0xff);
  119. scancode = ir_nec_bytes_to_scancode(address,
  120. not_address,
  121. command,
  122. not_command,
  123. &rc_proto);
  124. if (data->is_nec_x)
  125. data->necx_repeat = true;
  126. rc_keydown(dev, rc_proto, scancode, 0);
  127. } else {
  128. rc_repeat(dev);
  129. }
  130. data->state = STATE_INACTIVE;
  131. return 0;
  132. }
  133. dev_dbg(&dev->dev, "NEC decode failed at count %d state %d (%uus %s)\n",
  134. data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  135. data->state = STATE_INACTIVE;
  136. return -EINVAL;
  137. }
  138. /**
  139. * ir_nec_scancode_to_raw() - encode an NEC scancode ready for modulation.
  140. * @protocol: specific protocol to use
  141. * @scancode: a single NEC scancode.
  142. */
  143. static u32 ir_nec_scancode_to_raw(enum rc_proto protocol, u32 scancode)
  144. {
  145. unsigned int addr, addr_inv, data, data_inv;
  146. data = scancode & 0xff;
  147. if (protocol == RC_PROTO_NEC32) {
  148. /* 32-bit NEC (used by Apple and TiVo remotes) */
  149. /* scan encoding: aaAAddDD */
  150. addr_inv = (scancode >> 24) & 0xff;
  151. addr = (scancode >> 16) & 0xff;
  152. data_inv = (scancode >> 8) & 0xff;
  153. } else if (protocol == RC_PROTO_NECX) {
  154. /* Extended NEC */
  155. /* scan encoding AAaaDD */
  156. addr = (scancode >> 16) & 0xff;
  157. addr_inv = (scancode >> 8) & 0xff;
  158. data_inv = data ^ 0xff;
  159. } else {
  160. /* Normal NEC */
  161. /* scan encoding: AADD */
  162. addr = (scancode >> 8) & 0xff;
  163. addr_inv = addr ^ 0xff;
  164. data_inv = data ^ 0xff;
  165. }
  166. /* raw encoding: ddDDaaAA */
  167. return data_inv << 24 |
  168. data << 16 |
  169. addr_inv << 8 |
  170. addr;
  171. }
  172. static const struct ir_raw_timings_pd ir_nec_timings = {
  173. .header_pulse = NEC_HEADER_PULSE,
  174. .header_space = NEC_HEADER_SPACE,
  175. .bit_pulse = NEC_BIT_PULSE,
  176. .bit_space[0] = NEC_BIT_0_SPACE,
  177. .bit_space[1] = NEC_BIT_1_SPACE,
  178. .trailer_pulse = NEC_TRAILER_PULSE,
  179. .trailer_space = NEC_TRAILER_SPACE,
  180. .msb_first = 0,
  181. };
  182. /**
  183. * ir_nec_encode() - Encode a scancode as a stream of raw events
  184. *
  185. * @protocol: protocol to encode
  186. * @scancode: scancode to encode
  187. * @events: array of raw ir events to write into
  188. * @max: maximum size of @events
  189. *
  190. * Returns: The number of events written.
  191. * -ENOBUFS if there isn't enough space in the array to fit the
  192. * encoding. In this case all @max events will have been written.
  193. */
  194. static int ir_nec_encode(enum rc_proto protocol, u32 scancode,
  195. struct ir_raw_event *events, unsigned int max)
  196. {
  197. struct ir_raw_event *e = events;
  198. int ret;
  199. u32 raw;
  200. /* Convert a NEC scancode to raw NEC data */
  201. raw = ir_nec_scancode_to_raw(protocol, scancode);
  202. /* Modulate the raw data using a pulse distance modulation */
  203. ret = ir_raw_gen_pd(&e, max, &ir_nec_timings, NEC_NBITS, raw);
  204. if (ret < 0)
  205. return ret;
  206. return e - events;
  207. }
  208. static struct ir_raw_handler nec_handler = {
  209. .protocols = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
  210. RC_PROTO_BIT_NEC32,
  211. .decode = ir_nec_decode,
  212. .encode = ir_nec_encode,
  213. .carrier = 38000,
  214. .min_timeout = NEC_TRAILER_SPACE,
  215. };
  216. static int __init ir_nec_decode_init(void)
  217. {
  218. ir_raw_handler_register(&nec_handler);
  219. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  220. return 0;
  221. }
  222. static void __exit ir_nec_decode_exit(void)
  223. {
  224. ir_raw_handler_unregister(&nec_handler);
  225. }
  226. module_init(ir_nec_decode_init);
  227. module_exit(ir_nec_decode_exit);
  228. MODULE_LICENSE("GPL v2");
  229. MODULE_AUTHOR("Mauro Carvalho Chehab");
  230. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  231. MODULE_DESCRIPTION("NEC IR protocol decoder");