ir-functions.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. *
  3. * some common structs and functions to handle infrared remotes via
  4. * input layer ...
  5. *
  6. * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/string.h>
  25. #include <media/ir-common.h>
  26. /* -------------------------------------------------------------------------- */
  27. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  28. MODULE_LICENSE("GPL");
  29. static int repeat = 1;
  30. module_param(repeat, int, 0444);
  31. MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
  32. static int debug = 0; /* debug level (0,1,2) */
  33. module_param(debug, int, 0644);
  34. #define dprintk(level, fmt, arg...) if (debug >= level) \
  35. printk(KERN_DEBUG fmt , ## arg)
  36. /* -------------------------------------------------------------------------- */
  37. static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
  38. {
  39. if (KEY_RESERVED == ir->keycode) {
  40. printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
  41. dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
  42. return;
  43. }
  44. dprintk(1,"%s: key event code=%d down=%d\n",
  45. dev->name,ir->keycode,ir->keypressed);
  46. input_report_key(dev,ir->keycode,ir->keypressed);
  47. input_sync(dev);
  48. }
  49. /* -------------------------------------------------------------------------- */
  50. void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
  51. int ir_type, IR_KEYTAB_TYPE *ir_codes)
  52. {
  53. int i;
  54. ir->ir_type = ir_type;
  55. if (ir_codes)
  56. memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
  57. dev->keycode = ir->ir_codes;
  58. dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
  59. dev->keycodemax = IR_KEYTAB_SIZE;
  60. for (i = 0; i < IR_KEYTAB_SIZE; i++)
  61. set_bit(ir->ir_codes[i], dev->keybit);
  62. clear_bit(0, dev->keybit);
  63. set_bit(EV_KEY, dev->evbit);
  64. if (repeat)
  65. set_bit(EV_REP, dev->evbit);
  66. }
  67. void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
  68. {
  69. if (ir->keypressed) {
  70. ir->keypressed = 0;
  71. ir_input_key_event(dev,ir);
  72. }
  73. }
  74. void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
  75. u32 ir_key, u32 ir_raw)
  76. {
  77. u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
  78. if (ir->keypressed && ir->keycode != keycode) {
  79. ir->keypressed = 0;
  80. ir_input_key_event(dev,ir);
  81. }
  82. if (!ir->keypressed) {
  83. ir->ir_key = ir_key;
  84. ir->ir_raw = ir_raw;
  85. ir->keycode = keycode;
  86. ir->keypressed = 1;
  87. ir_input_key_event(dev,ir);
  88. }
  89. }
  90. /* -------------------------------------------------------------------------- */
  91. u32 ir_extract_bits(u32 data, u32 mask)
  92. {
  93. int mbit, vbit;
  94. u32 value;
  95. value = 0;
  96. vbit = 0;
  97. for (mbit = 0; mbit < 32; mbit++) {
  98. if (!(mask & ((u32)1 << mbit)))
  99. continue;
  100. if (data & ((u32)1 << mbit))
  101. value |= (1 << vbit);
  102. vbit++;
  103. }
  104. return value;
  105. }
  106. static int inline getbit(u32 *samples, int bit)
  107. {
  108. return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
  109. }
  110. /* sump raw samples for visual debugging ;) */
  111. int ir_dump_samples(u32 *samples, int count)
  112. {
  113. int i, bit, start;
  114. printk(KERN_DEBUG "ir samples: ");
  115. start = 0;
  116. for (i = 0; i < count * 32; i++) {
  117. bit = getbit(samples,i);
  118. if (bit)
  119. start = 1;
  120. if (0 == start)
  121. continue;
  122. printk("%s", bit ? "#" : "_");
  123. }
  124. printk("\n");
  125. return 0;
  126. }
  127. /* decode raw samples, pulse distance coding used by NEC remotes */
  128. int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
  129. {
  130. int i,last,bit,len;
  131. u32 curBit;
  132. u32 value;
  133. /* find start burst */
  134. for (i = len = 0; i < count * 32; i++) {
  135. bit = getbit(samples,i);
  136. if (bit) {
  137. len++;
  138. } else {
  139. if (len >= 29)
  140. break;
  141. len = 0;
  142. }
  143. }
  144. /* start burst to short */
  145. if (len < 29)
  146. return 0xffffffff;
  147. /* find start silence */
  148. for (len = 0; i < count * 32; i++) {
  149. bit = getbit(samples,i);
  150. if (bit) {
  151. break;
  152. } else {
  153. len++;
  154. }
  155. }
  156. /* silence to short */
  157. if (len < 7)
  158. return 0xffffffff;
  159. /* go decoding */
  160. len = 0;
  161. last = 1;
  162. value = 0; curBit = 1;
  163. for (; i < count * 32; i++) {
  164. bit = getbit(samples,i);
  165. if (last) {
  166. if(bit) {
  167. continue;
  168. } else {
  169. len = 1;
  170. }
  171. } else {
  172. if (bit) {
  173. if (len > (low + high) /2)
  174. value |= curBit;
  175. curBit <<= 1;
  176. if (curBit == 1)
  177. break;
  178. } else {
  179. len++;
  180. }
  181. }
  182. last = bit;
  183. }
  184. return value;
  185. }
  186. /* decode raw samples, biphase coding, used by rc5 for example */
  187. int ir_decode_biphase(u32 *samples, int count, int low, int high)
  188. {
  189. int i,last,bit,len,flips;
  190. u32 value;
  191. /* find start bit (1) */
  192. for (i = 0; i < 32; i++) {
  193. bit = getbit(samples,i);
  194. if (bit)
  195. break;
  196. }
  197. /* go decoding */
  198. len = 0;
  199. flips = 0;
  200. value = 1;
  201. for (; i < count * 32; i++) {
  202. if (len > high)
  203. break;
  204. if (flips > 1)
  205. break;
  206. last = bit;
  207. bit = getbit(samples,i);
  208. if (last == bit) {
  209. len++;
  210. continue;
  211. }
  212. if (len < low) {
  213. len++;
  214. flips++;
  215. continue;
  216. }
  217. value <<= 1;
  218. value |= bit;
  219. flips = 0;
  220. len = 1;
  221. }
  222. return value;
  223. }
  224. EXPORT_SYMBOL_GPL(ir_input_init);
  225. EXPORT_SYMBOL_GPL(ir_input_nokey);
  226. EXPORT_SYMBOL_GPL(ir_input_keydown);
  227. EXPORT_SYMBOL_GPL(ir_extract_bits);
  228. EXPORT_SYMBOL_GPL(ir_dump_samples);
  229. EXPORT_SYMBOL_GPL(ir_decode_biphase);
  230. EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
  231. /*
  232. * Local variables:
  233. * c-basic-offset: 8
  234. * End:
  235. */