img-ir-nec.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * ImgTec IR Decoder setup for NEC protocol.
  3. *
  4. * Copyright 2010-2014 Imagination Technologies Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include "img-ir-hw.h"
  12. #include <linux/bitrev.h>
  13. /* Convert NEC data to a scancode */
  14. static int img_ir_nec_scancode(int len, u64 raw, u64 enabled_protocols,
  15. struct img_ir_scancode_req *request)
  16. {
  17. unsigned int addr, addr_inv, data, data_inv;
  18. /* a repeat code has no data */
  19. if (!len)
  20. return IMG_IR_REPEATCODE;
  21. if (len != 32)
  22. return -EINVAL;
  23. /* raw encoding: ddDDaaAA */
  24. addr = (raw >> 0) & 0xff;
  25. addr_inv = (raw >> 8) & 0xff;
  26. data = (raw >> 16) & 0xff;
  27. data_inv = (raw >> 24) & 0xff;
  28. if ((data_inv ^ data) != 0xff) {
  29. /* 32-bit NEC (used by Apple and TiVo remotes) */
  30. /* scan encoding: as transmitted, MSBit = first received bit */
  31. request->scancode = bitrev8(addr) << 24 |
  32. bitrev8(addr_inv) << 16 |
  33. bitrev8(data) << 8 |
  34. bitrev8(data_inv);
  35. request->protocol = RC_TYPE_NEC32;
  36. } else if ((addr_inv ^ addr) != 0xff) {
  37. /* Extended NEC */
  38. /* scan encoding: AAaaDD */
  39. request->scancode = addr << 16 |
  40. addr_inv << 8 |
  41. data;
  42. request->protocol = RC_TYPE_NECX;
  43. } else {
  44. /* Normal NEC */
  45. /* scan encoding: AADD */
  46. request->scancode = addr << 8 |
  47. data;
  48. request->protocol = RC_TYPE_NEC;
  49. }
  50. return IMG_IR_SCANCODE;
  51. }
  52. /* Convert NEC scancode to NEC data filter */
  53. static int img_ir_nec_filter(const struct rc_scancode_filter *in,
  54. struct img_ir_filter *out, u64 protocols)
  55. {
  56. unsigned int addr, addr_inv, data, data_inv;
  57. unsigned int addr_m, addr_inv_m, data_m, data_inv_m;
  58. data = in->data & 0xff;
  59. data_m = in->mask & 0xff;
  60. if ((in->data | in->mask) & 0xff000000) {
  61. /* 32-bit NEC (used by Apple and TiVo remotes) */
  62. /* scan encoding: as transmitted, MSBit = first received bit */
  63. addr = bitrev8(in->data >> 24);
  64. addr_m = bitrev8(in->mask >> 24);
  65. addr_inv = bitrev8(in->data >> 16);
  66. addr_inv_m = bitrev8(in->mask >> 16);
  67. data = bitrev8(in->data >> 8);
  68. data_m = bitrev8(in->mask >> 8);
  69. data_inv = bitrev8(in->data >> 0);
  70. data_inv_m = bitrev8(in->mask >> 0);
  71. } else if ((in->data | in->mask) & 0x00ff0000) {
  72. /* Extended NEC */
  73. /* scan encoding AAaaDD */
  74. addr = (in->data >> 16) & 0xff;
  75. addr_m = (in->mask >> 16) & 0xff;
  76. addr_inv = (in->data >> 8) & 0xff;
  77. addr_inv_m = (in->mask >> 8) & 0xff;
  78. data_inv = data ^ 0xff;
  79. data_inv_m = data_m;
  80. } else {
  81. /* Normal NEC */
  82. /* scan encoding: AADD */
  83. addr = (in->data >> 8) & 0xff;
  84. addr_m = (in->mask >> 8) & 0xff;
  85. addr_inv = addr ^ 0xff;
  86. addr_inv_m = addr_m;
  87. data_inv = data ^ 0xff;
  88. data_inv_m = data_m;
  89. }
  90. /* raw encoding: ddDDaaAA */
  91. out->data = data_inv << 24 |
  92. data << 16 |
  93. addr_inv << 8 |
  94. addr;
  95. out->mask = data_inv_m << 24 |
  96. data_m << 16 |
  97. addr_inv_m << 8 |
  98. addr_m;
  99. return 0;
  100. }
  101. /*
  102. * NEC decoder
  103. * See also http://www.sbprojects.com/knowledge/ir/nec.php
  104. * http://wiki.altium.com/display/ADOH/NEC+Infrared+Transmission+Protocol
  105. */
  106. struct img_ir_decoder img_ir_nec = {
  107. .type = RC_BIT_NEC | RC_BIT_NECX | RC_BIT_NEC32,
  108. .control = {
  109. .decoden = 1,
  110. .code_type = IMG_IR_CODETYPE_PULSEDIST,
  111. },
  112. /* main timings */
  113. .unit = 562500, /* 562.5 us */
  114. .timings = {
  115. /* leader symbol */
  116. .ldr = {
  117. .pulse = { 16 /* 9ms */ },
  118. .space = { 8 /* 4.5ms */ },
  119. },
  120. /* 0 symbol */
  121. .s00 = {
  122. .pulse = { 1 /* 562.5 us */ },
  123. .space = { 1 /* 562.5 us */ },
  124. },
  125. /* 1 symbol */
  126. .s01 = {
  127. .pulse = { 1 /* 562.5 us */ },
  128. .space = { 3 /* 1687.5 us */ },
  129. },
  130. /* free time */
  131. .ft = {
  132. .minlen = 32,
  133. .maxlen = 32,
  134. .ft_min = 10, /* 5.625 ms */
  135. },
  136. },
  137. /* repeat codes */
  138. .repeat = 108, /* 108 ms */
  139. .rtimings = {
  140. /* leader symbol */
  141. .ldr = {
  142. .space = { 4 /* 2.25 ms */ },
  143. },
  144. /* free time */
  145. .ft = {
  146. .minlen = 0, /* repeat code has no data */
  147. .maxlen = 0,
  148. },
  149. },
  150. /* scancode logic */
  151. .scancode = img_ir_nec_scancode,
  152. .filter = img_ir_nec_filter,
  153. };