ir-nec-decoder.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
  2. *
  3. * Copyright (C) 2010 by Mauro Carvalho Chehab
  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 <linux/bitrev.h>
  15. #include <linux/module.h>
  16. #include "rc-core-priv.h"
  17. #define NEC_NBITS 32
  18. #define NEC_UNIT 562500 /* ns */
  19. #define NEC_HEADER_PULSE (16 * NEC_UNIT)
  20. #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */
  21. #define NEC_HEADER_SPACE (8 * NEC_UNIT)
  22. #define NEC_REPEAT_SPACE (4 * NEC_UNIT)
  23. #define NEC_BIT_PULSE (1 * NEC_UNIT)
  24. #define NEC_BIT_0_SPACE (1 * NEC_UNIT)
  25. #define NEC_BIT_1_SPACE (3 * NEC_UNIT)
  26. #define NEC_TRAILER_PULSE (1 * NEC_UNIT)
  27. #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */
  28. #define NECX_REPEAT_BITS 1
  29. enum nec_state {
  30. STATE_INACTIVE,
  31. STATE_HEADER_SPACE,
  32. STATE_BIT_PULSE,
  33. STATE_BIT_SPACE,
  34. STATE_TRAILER_PULSE,
  35. STATE_TRAILER_SPACE,
  36. };
  37. /**
  38. * ir_nec_decode() - Decode one NEC pulse or space
  39. * @dev: the struct rc_dev descriptor of the device
  40. * @duration: 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_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
  45. {
  46. struct nec_dec *data = &dev->raw->nec;
  47. u32 scancode;
  48. u8 address, not_address, command, not_command;
  49. bool send_32bits = false;
  50. if (!(dev->enabled_protocols & RC_BIT_NEC))
  51. return 0;
  52. if (!is_timing_event(ev)) {
  53. if (ev.reset)
  54. data->state = STATE_INACTIVE;
  55. return 0;
  56. }
  57. IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
  58. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  59. switch (data->state) {
  60. case STATE_INACTIVE:
  61. if (!ev.pulse)
  62. break;
  63. if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
  64. data->is_nec_x = false;
  65. data->necx_repeat = false;
  66. } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
  67. data->is_nec_x = true;
  68. else
  69. break;
  70. data->count = 0;
  71. data->state = STATE_HEADER_SPACE;
  72. return 0;
  73. case STATE_HEADER_SPACE:
  74. if (ev.pulse)
  75. break;
  76. if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
  77. data->state = STATE_BIT_PULSE;
  78. return 0;
  79. } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
  80. if (!dev->keypressed) {
  81. IR_dprintk(1, "Discarding last key repeat: event after key up\n");
  82. } else {
  83. rc_repeat(dev);
  84. IR_dprintk(1, "Repeat last key\n");
  85. data->state = STATE_TRAILER_PULSE;
  86. }
  87. return 0;
  88. }
  89. break;
  90. case STATE_BIT_PULSE:
  91. if (!ev.pulse)
  92. break;
  93. if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
  94. break;
  95. data->state = STATE_BIT_SPACE;
  96. return 0;
  97. case STATE_BIT_SPACE:
  98. if (ev.pulse)
  99. break;
  100. if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
  101. geq_margin(ev.duration,
  102. NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
  103. IR_dprintk(1, "Repeat last key\n");
  104. rc_repeat(dev);
  105. data->state = STATE_INACTIVE;
  106. return 0;
  107. } else if (data->count > NECX_REPEAT_BITS)
  108. data->necx_repeat = false;
  109. data->bits <<= 1;
  110. if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
  111. data->bits |= 1;
  112. else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
  113. break;
  114. data->count++;
  115. if (data->count == NEC_NBITS)
  116. data->state = STATE_TRAILER_PULSE;
  117. else
  118. data->state = STATE_BIT_PULSE;
  119. return 0;
  120. case STATE_TRAILER_PULSE:
  121. if (!ev.pulse)
  122. break;
  123. if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
  124. break;
  125. data->state = STATE_TRAILER_SPACE;
  126. return 0;
  127. case STATE_TRAILER_SPACE:
  128. if (ev.pulse)
  129. break;
  130. if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
  131. break;
  132. address = bitrev8((data->bits >> 24) & 0xff);
  133. not_address = bitrev8((data->bits >> 16) & 0xff);
  134. command = bitrev8((data->bits >> 8) & 0xff);
  135. not_command = bitrev8((data->bits >> 0) & 0xff);
  136. if ((command ^ not_command) != 0xff) {
  137. IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
  138. data->bits);
  139. send_32bits = true;
  140. }
  141. if (send_32bits) {
  142. /* NEC transport, but modified protocol, used by at
  143. * least Apple and TiVo remotes */
  144. scancode = data->bits;
  145. IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
  146. } else if ((address ^ not_address) != 0xff) {
  147. /* Extended NEC */
  148. scancode = address << 16 |
  149. not_address << 8 |
  150. command;
  151. IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
  152. } else {
  153. /* Normal NEC */
  154. scancode = address << 8 | command;
  155. IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
  156. }
  157. if (data->is_nec_x)
  158. data->necx_repeat = true;
  159. rc_keydown(dev, RC_TYPE_NEC, scancode, 0);
  160. data->state = STATE_INACTIVE;
  161. return 0;
  162. }
  163. IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n",
  164. data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  165. data->state = STATE_INACTIVE;
  166. return -EINVAL;
  167. }
  168. static struct ir_raw_handler nec_handler = {
  169. .protocols = RC_BIT_NEC,
  170. .decode = ir_nec_decode,
  171. };
  172. static int __init ir_nec_decode_init(void)
  173. {
  174. ir_raw_handler_register(&nec_handler);
  175. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  176. return 0;
  177. }
  178. static void __exit ir_nec_decode_exit(void)
  179. {
  180. ir_raw_handler_unregister(&nec_handler);
  181. }
  182. module_init(ir_nec_decode_init);
  183. module_exit(ir_nec_decode_exit);
  184. MODULE_LICENSE("GPL");
  185. MODULE_AUTHOR("Mauro Carvalho Chehab");
  186. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  187. MODULE_DESCRIPTION("NEC IR protocol decoder");