ir-nec-decoder.c 5.5 KB

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