ir-nec-decoder.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. enum rc_type rc_type;
  49. u8 address, not_address, command, not_command;
  50. bool send_32bits = false;
  51. if (!is_timing_event(ev)) {
  52. if (ev.reset)
  53. data->state = STATE_INACTIVE;
  54. return 0;
  55. }
  56. IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
  57. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  58. switch (data->state) {
  59. case STATE_INACTIVE:
  60. if (!ev.pulse)
  61. break;
  62. if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
  63. data->is_nec_x = false;
  64. data->necx_repeat = false;
  65. } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
  66. data->is_nec_x = true;
  67. else
  68. break;
  69. data->count = 0;
  70. data->state = STATE_HEADER_SPACE;
  71. return 0;
  72. case STATE_HEADER_SPACE:
  73. if (ev.pulse)
  74. break;
  75. if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
  76. data->state = STATE_BIT_PULSE;
  77. return 0;
  78. } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
  79. if (!dev->keypressed) {
  80. IR_dprintk(1, "Discarding last key repeat: event after key up\n");
  81. } else {
  82. rc_repeat(dev);
  83. IR_dprintk(1, "Repeat last key\n");
  84. data->state = STATE_TRAILER_PULSE;
  85. }
  86. return 0;
  87. }
  88. break;
  89. case STATE_BIT_PULSE:
  90. if (!ev.pulse)
  91. break;
  92. if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
  93. break;
  94. data->state = STATE_BIT_SPACE;
  95. return 0;
  96. case STATE_BIT_SPACE:
  97. if (ev.pulse)
  98. break;
  99. if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
  100. geq_margin(ev.duration,
  101. NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
  102. IR_dprintk(1, "Repeat last key\n");
  103. rc_repeat(dev);
  104. data->state = STATE_INACTIVE;
  105. return 0;
  106. } else if (data->count > NECX_REPEAT_BITS)
  107. data->necx_repeat = false;
  108. data->bits <<= 1;
  109. if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
  110. data->bits |= 1;
  111. else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
  112. break;
  113. data->count++;
  114. if (data->count == NEC_NBITS)
  115. data->state = STATE_TRAILER_PULSE;
  116. else
  117. data->state = STATE_BIT_PULSE;
  118. return 0;
  119. case STATE_TRAILER_PULSE:
  120. if (!ev.pulse)
  121. break;
  122. if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
  123. break;
  124. data->state = STATE_TRAILER_SPACE;
  125. return 0;
  126. case STATE_TRAILER_SPACE:
  127. if (ev.pulse)
  128. break;
  129. if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
  130. break;
  131. address = bitrev8((data->bits >> 24) & 0xff);
  132. not_address = bitrev8((data->bits >> 16) & 0xff);
  133. command = bitrev8((data->bits >> 8) & 0xff);
  134. not_command = bitrev8((data->bits >> 0) & 0xff);
  135. if ((command ^ not_command) != 0xff) {
  136. IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
  137. data->bits);
  138. send_32bits = true;
  139. }
  140. if (send_32bits) {
  141. /* NEC transport, but modified protocol, used by at
  142. * least Apple and TiVo remotes */
  143. scancode = data->bits;
  144. IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
  145. rc_type = RC_TYPE_NEC32;
  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. rc_type = RC_TYPE_NECX;
  153. } else {
  154. /* Normal NEC */
  155. scancode = address << 8 | command;
  156. IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
  157. rc_type = RC_TYPE_NEC;
  158. }
  159. if (data->is_nec_x)
  160. data->necx_repeat = true;
  161. rc_keydown(dev, rc_type, scancode, 0);
  162. data->state = STATE_INACTIVE;
  163. return 0;
  164. }
  165. IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n",
  166. data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  167. data->state = STATE_INACTIVE;
  168. return -EINVAL;
  169. }
  170. static struct ir_raw_handler nec_handler = {
  171. .protocols = RC_BIT_NEC | RC_BIT_NECX | RC_BIT_NEC32,
  172. .decode = ir_nec_decode,
  173. };
  174. static int __init ir_nec_decode_init(void)
  175. {
  176. ir_raw_handler_register(&nec_handler);
  177. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  178. return 0;
  179. }
  180. static void __exit ir_nec_decode_exit(void)
  181. {
  182. ir_raw_handler_unregister(&nec_handler);
  183. }
  184. module_init(ir_nec_decode_init);
  185. module_exit(ir_nec_decode_exit);
  186. MODULE_LICENSE("GPL");
  187. MODULE_AUTHOR("Mauro Carvalho Chehab");
  188. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  189. MODULE_DESCRIPTION("NEC IR protocol decoder");