img-ir-nec.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include <linux/log2.h>
  14. /* Convert NEC data to a scancode */
  15. static int img_ir_nec_scancode(int len, u64 raw, u64 enabled_protocols,
  16. struct img_ir_scancode_req *request)
  17. {
  18. unsigned int addr, addr_inv, data, data_inv;
  19. /* a repeat code has no data */
  20. if (!len)
  21. return IMG_IR_REPEATCODE;
  22. if (len != 32)
  23. return -EINVAL;
  24. /* raw encoding: ddDDaaAA */
  25. addr = (raw >> 0) & 0xff;
  26. addr_inv = (raw >> 8) & 0xff;
  27. data = (raw >> 16) & 0xff;
  28. data_inv = (raw >> 24) & 0xff;
  29. if ((data_inv ^ data) != 0xff) {
  30. /* 32-bit NEC (used by Apple and TiVo remotes) */
  31. /* scan encoding: as transmitted, MSBit = first received bit */
  32. request->scancode = bitrev8(addr) << 24 |
  33. bitrev8(addr_inv) << 16 |
  34. bitrev8(data) << 8 |
  35. bitrev8(data_inv);
  36. request->protocol = RC_PROTO_NEC32;
  37. } else if ((addr_inv ^ addr) != 0xff) {
  38. /* Extended NEC */
  39. /* scan encoding: AAaaDD */
  40. request->scancode = addr << 16 |
  41. addr_inv << 8 |
  42. data;
  43. request->protocol = RC_PROTO_NECX;
  44. } else {
  45. /* Normal NEC */
  46. /* scan encoding: AADD */
  47. request->scancode = addr << 8 |
  48. data;
  49. request->protocol = RC_PROTO_NEC;
  50. }
  51. return IMG_IR_SCANCODE;
  52. }
  53. /* Convert NEC scancode to NEC data filter */
  54. static int img_ir_nec_filter(const struct rc_scancode_filter *in,
  55. struct img_ir_filter *out, u64 protocols)
  56. {
  57. unsigned int addr, addr_inv, data, data_inv;
  58. unsigned int addr_m, addr_inv_m, data_m, data_inv_m;
  59. data = in->data & 0xff;
  60. data_m = in->mask & 0xff;
  61. protocols &= RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32;
  62. /*
  63. * If only one bit is set, we were requested to do an exact
  64. * protocol. This should be the case for wakeup filters; for
  65. * normal filters, guess the protocol from the scancode.
  66. */
  67. if (!is_power_of_2(protocols)) {
  68. if ((in->data | in->mask) & 0xff000000)
  69. protocols = RC_PROTO_BIT_NEC32;
  70. else if ((in->data | in->mask) & 0x00ff0000)
  71. protocols = RC_PROTO_BIT_NECX;
  72. else
  73. protocols = RC_PROTO_BIT_NEC;
  74. }
  75. if (protocols == RC_PROTO_BIT_NEC32) {
  76. /* 32-bit NEC (used by Apple and TiVo remotes) */
  77. /* scan encoding: as transmitted, MSBit = first received bit */
  78. addr = bitrev8(in->data >> 24);
  79. addr_m = bitrev8(in->mask >> 24);
  80. addr_inv = bitrev8(in->data >> 16);
  81. addr_inv_m = bitrev8(in->mask >> 16);
  82. data = bitrev8(in->data >> 8);
  83. data_m = bitrev8(in->mask >> 8);
  84. data_inv = bitrev8(in->data >> 0);
  85. data_inv_m = bitrev8(in->mask >> 0);
  86. } else if (protocols == RC_PROTO_BIT_NECX) {
  87. /* Extended NEC */
  88. /* scan encoding AAaaDD */
  89. addr = (in->data >> 16) & 0xff;
  90. addr_m = (in->mask >> 16) & 0xff;
  91. addr_inv = (in->data >> 8) & 0xff;
  92. addr_inv_m = (in->mask >> 8) & 0xff;
  93. data_inv = data ^ 0xff;
  94. data_inv_m = data_m;
  95. } else {
  96. /* Normal NEC */
  97. /* scan encoding: AADD */
  98. addr = (in->data >> 8) & 0xff;
  99. addr_m = (in->mask >> 8) & 0xff;
  100. addr_inv = addr ^ 0xff;
  101. addr_inv_m = addr_m;
  102. data_inv = data ^ 0xff;
  103. data_inv_m = data_m;
  104. }
  105. /* raw encoding: ddDDaaAA */
  106. out->data = data_inv << 24 |
  107. data << 16 |
  108. addr_inv << 8 |
  109. addr;
  110. out->mask = data_inv_m << 24 |
  111. data_m << 16 |
  112. addr_inv_m << 8 |
  113. addr_m;
  114. return 0;
  115. }
  116. /*
  117. * NEC decoder
  118. * See also http://www.sbprojects.com/knowledge/ir/nec.php
  119. * http://wiki.altium.com/display/ADOH/NEC+Infrared+Transmission+Protocol
  120. */
  121. struct img_ir_decoder img_ir_nec = {
  122. .type = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32,
  123. .control = {
  124. .decoden = 1,
  125. .code_type = IMG_IR_CODETYPE_PULSEDIST,
  126. },
  127. /* main timings */
  128. .unit = 562500, /* 562.5 us */
  129. .timings = {
  130. /* leader symbol */
  131. .ldr = {
  132. .pulse = { 16 /* 9ms */ },
  133. .space = { 8 /* 4.5ms */ },
  134. },
  135. /* 0 symbol */
  136. .s00 = {
  137. .pulse = { 1 /* 562.5 us */ },
  138. .space = { 1 /* 562.5 us */ },
  139. },
  140. /* 1 symbol */
  141. .s01 = {
  142. .pulse = { 1 /* 562.5 us */ },
  143. .space = { 3 /* 1687.5 us */ },
  144. },
  145. /* free time */
  146. .ft = {
  147. .minlen = 32,
  148. .maxlen = 32,
  149. .ft_min = 10, /* 5.625 ms */
  150. },
  151. },
  152. /* repeat codes */
  153. .repeat = 108, /* 108 ms */
  154. .rtimings = {
  155. /* leader symbol */
  156. .ldr = {
  157. .space = { 4 /* 2.25 ms */ },
  158. },
  159. /* free time */
  160. .ft = {
  161. .minlen = 0, /* repeat code has no data */
  162. .maxlen = 0,
  163. },
  164. },
  165. /* scancode logic */
  166. .scancode = img_ir_nec_scancode,
  167. .filter = img_ir_nec_filter,
  168. };