ir-nec-decoder.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 */
  16. #define MIN_START_TIME 3900000
  17. #define MAX_START_TIME 5100000
  18. /* Pulse time: 560 us */
  19. #define MIN_PULSE_TIME 460000
  20. #define MAX_PULSE_TIME 660000
  21. /* Bit 1 space time: 2.25ms-560 us */
  22. #define MIN_BIT1_TIME 1490000
  23. #define MAX_BIT1_TIME 1890000
  24. /* Bit 0 space time: 1.12ms-560 us */
  25. #define MIN_BIT0_TIME 360000
  26. #define MAX_BIT0_TIME 760000
  27. /** Decode NEC pulsecode. This code can take up to 76.5 ms to run.
  28. Unfortunately, using IRQ to decode pulse didn't work, since it uses
  29. a pulse train of 38KHz. This means one pulse on each 52 us
  30. */
  31. int ir_nec_decode(struct input_dev *input_dev,
  32. struct ir_raw_event *evs,
  33. int len)
  34. {
  35. int i, count = -1;
  36. int ircode = 0, not_code = 0;
  37. #if 0
  38. /* Needed only after porting the event code to the decoder */
  39. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  40. #endif
  41. /* Be sure that the first event is an start one and is a pulse */
  42. for (i = 0; i < len; i++) {
  43. if (evs[i].type & (IR_START_EVENT | IR_PULSE))
  44. break;
  45. }
  46. i++; /* First event doesn't contain data */
  47. if (i >= len)
  48. return 0;
  49. /* First space should have 4.5 ms otherwise is not NEC protocol */
  50. if ((evs[i].delta.tv_nsec < MIN_START_TIME) |
  51. (evs[i].delta.tv_nsec > MAX_START_TIME) |
  52. (evs[i].type != IR_SPACE))
  53. goto err;
  54. /*
  55. * FIXME: need to implement the repeat sequence
  56. */
  57. count = 0;
  58. for (i++; i < len; i++) {
  59. int bit;
  60. if ((evs[i].delta.tv_nsec < MIN_PULSE_TIME) |
  61. (evs[i].delta.tv_nsec > MAX_PULSE_TIME) |
  62. (evs[i].type != IR_PULSE))
  63. goto err;
  64. if (++i >= len)
  65. goto err;
  66. if (evs[i].type != IR_SPACE)
  67. goto err;
  68. if ((evs[i].delta.tv_nsec > MIN_BIT1_TIME) &&
  69. (evs[i].delta.tv_nsec < MAX_BIT1_TIME))
  70. bit = 1;
  71. else if ((evs[i].delta.tv_nsec > MIN_BIT0_TIME) &&
  72. (evs[i].delta.tv_nsec < MAX_BIT0_TIME))
  73. bit = 0;
  74. else
  75. goto err;
  76. if (bit) {
  77. int shift = count;
  78. /* Address first, then command */
  79. if (shift < 8) {
  80. shift += 8;
  81. ircode |= 1 << shift;
  82. } else if (shift < 16) {
  83. not_code |= 1 << shift;
  84. } else if (shift < 24) {
  85. shift -= 16;
  86. ircode |= 1 << shift;
  87. } else {
  88. shift -= 24;
  89. not_code |= 1 << shift;
  90. }
  91. }
  92. if (++count == 32)
  93. break;
  94. }
  95. /*
  96. * Fixme: may need to accept Extended NEC protocol?
  97. */
  98. if ((ircode & ~not_code) != ircode) {
  99. IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n",
  100. ircode, not_code);
  101. return -EINVAL;
  102. }
  103. IR_dprintk(1, "NEC scancode 0x%04x\n", ircode);
  104. return ircode;
  105. err:
  106. IR_dprintk(1, "NEC decoded failed at bit %d while decoding %luus time\n",
  107. count, (evs[i].delta.tv_nsec + 500) / 1000);
  108. return -EINVAL;
  109. }
  110. EXPORT_SYMBOL_GPL(ir_nec_decode);