ir-nec-decoder.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* ir-raw-event.c - handle IR Pulse/Space event
  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 <media/ir-core.h>
  15. /* Start time: 4.5 ms + 560 us of the next pulse */
  16. #define MIN_START_TIME (3900000 + 560000)
  17. #define MAX_START_TIME (5100000 + 560000)
  18. /* Bit 1 time: 2.25ms us */
  19. #define MIN_BIT1_TIME 2050000
  20. #define MAX_BIT1_TIME 2450000
  21. /* Bit 0 time: 1.12ms us */
  22. #define MIN_BIT0_TIME 920000
  23. #define MAX_BIT0_TIME 1320000
  24. /* Total IR code is 110 ms, including the 9 ms for the start pulse */
  25. #define MAX_NEC_TIME 4000000
  26. /* Total IR code is 110 ms, including the 9 ms for the start pulse */
  27. #define MIN_REPEAT_TIME 99000000
  28. #define MAX_REPEAT_TIME 112000000
  29. /* Repeat time: 2.25ms us */
  30. #define MIN_REPEAT_START_TIME 2050000
  31. #define MAX_REPEAT_START_TIME 3000000
  32. #define REPEAT_TIME 240 /* ms */
  33. /** is_repeat - Check if it is a NEC repeat event
  34. * @input_dev: the struct input_dev descriptor of the device
  35. * @pos: the position of the first event
  36. * @len: the length of the buffer
  37. */
  38. static int is_repeat(struct ir_raw_event *evs, int len, int pos)
  39. {
  40. if ((evs[pos].delta.tv_nsec < MIN_REPEAT_START_TIME) ||
  41. (evs[pos].delta.tv_nsec > MAX_REPEAT_START_TIME))
  42. return 0;
  43. if (++pos >= len)
  44. return 0;
  45. if ((evs[pos].delta.tv_nsec < MIN_REPEAT_TIME) ||
  46. (evs[pos].delta.tv_nsec > MAX_REPEAT_TIME))
  47. return 0;
  48. return 1;
  49. }
  50. /**
  51. * __ir_nec_decode() - Decode one NEC pulsecode
  52. * @input_dev: the struct input_dev descriptor of the device
  53. * @evs: event array with type/duration of pulse/space
  54. * @len: length of the array
  55. * @pos: position to start seeking for a code
  56. * This function returns -EINVAL if no pulse got decoded,
  57. * 0 if buffer is empty and 1 if one keycode were handled.
  58. */
  59. static int __ir_nec_decode(struct input_dev *input_dev,
  60. struct ir_raw_event *evs,
  61. int len, int *pos)
  62. {
  63. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  64. int count = -1;
  65. int ircode = 0, not_code = 0;
  66. /* Be sure that the first event is an start one and is a pulse */
  67. for (; *pos < len; (*pos)++) {
  68. /* Very long delays are considered as start events */
  69. if (evs[*pos].delta.tv_nsec > MAX_NEC_TIME)
  70. break;
  71. if (evs[*pos].type & IR_START_EVENT)
  72. break;
  73. IR_dprintk(1, "%luus: Spurious NEC %s\n",
  74. (evs[*pos].delta.tv_nsec + 500) / 1000,
  75. (evs[*pos].type & IR_SPACE) ? "space" : "pulse");
  76. }
  77. if (*pos >= len)
  78. return 0;
  79. (*pos)++; /* First event doesn't contain data */
  80. if (evs[*pos].type != IR_PULSE)
  81. goto err;
  82. /* Check if it is a NEC repeat event */
  83. if (is_repeat(evs, len, *pos)) {
  84. *pos += 2;
  85. if (ir->keypressed) {
  86. mod_timer(&ir->raw->timer_keyup,
  87. jiffies + msecs_to_jiffies(REPEAT_TIME));
  88. IR_dprintk(1, "NEC repeat event\n");
  89. return 1;
  90. } else {
  91. IR_dprintk(1, "missing NEC repeat event\n");
  92. return 0;
  93. }
  94. }
  95. /* First space should have 4.5 ms otherwise is not NEC protocol */
  96. if ((evs[*pos].delta.tv_nsec < MIN_START_TIME) ||
  97. (evs[*pos].delta.tv_nsec > MAX_START_TIME))
  98. goto err;
  99. count = 0;
  100. for ((*pos)++; *pos < len; (*pos)++) {
  101. int bit;
  102. if ((evs[*pos].delta.tv_nsec > MIN_BIT1_TIME) &&
  103. (evs[*pos].delta.tv_nsec < MAX_BIT1_TIME))
  104. bit = 1;
  105. else if ((evs[*pos].delta.tv_nsec > MIN_BIT0_TIME) &&
  106. (evs[*pos].delta.tv_nsec < MAX_BIT0_TIME))
  107. bit = 0;
  108. else
  109. goto err;
  110. if (bit) {
  111. int shift = count;
  112. /* Address first, then command */
  113. if (shift < 8) {
  114. shift += 8;
  115. ircode |= 1 << shift;
  116. } else if (shift < 16) {
  117. not_code |= 1 << shift;
  118. } else if (shift < 24) {
  119. shift -= 16;
  120. ircode |= 1 << shift;
  121. } else {
  122. shift -= 24;
  123. not_code |= 1 << shift;
  124. }
  125. }
  126. if (++count == 32)
  127. break;
  128. }
  129. *pos++;
  130. /*
  131. * Fixme: may need to accept Extended NEC protocol?
  132. */
  133. if ((ircode & ~not_code) != ircode) {
  134. IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n",
  135. ircode, not_code);
  136. return -EINVAL;
  137. }
  138. IR_dprintk(1, "NEC scancode 0x%04x\n", ircode);
  139. ir_keydown(input_dev, ircode);
  140. mod_timer(&ir->raw->timer_keyup,
  141. jiffies + msecs_to_jiffies(REPEAT_TIME));
  142. return 1;
  143. err:
  144. IR_dprintk(1, "NEC decoded failed at bit %d (%s) while decoding %luus time\n",
  145. count,
  146. (evs[*pos].type & IR_SPACE) ? "space" : "pulse",
  147. (evs[*pos].delta.tv_nsec + 500) / 1000);
  148. return -EINVAL;
  149. }
  150. /**
  151. * __ir_nec_decode() - Decodes all NEC pulsecodes on a given array
  152. * @input_dev: the struct input_dev descriptor of the device
  153. * @evs: event array with type/duration of pulse/space
  154. * @len: length of the array
  155. * This function returns the number of decoded pulses or -EINVAL if no
  156. * pulse got decoded
  157. */
  158. int ir_nec_decode(struct input_dev *input_dev,
  159. struct ir_raw_event *evs,
  160. int len)
  161. {
  162. int pos = 0;
  163. int rc = 0;
  164. while (pos < len) {
  165. if (__ir_nec_decode(input_dev, evs, len, &pos) > 0)
  166. rc++;
  167. }
  168. if (!rc)
  169. return -EINVAL;
  170. return rc;
  171. }
  172. EXPORT_SYMBOL_GPL(ir_nec_decode);