ir-functions.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * some common functions to handle infrared remote protocol decoding for
  3. * drivers which have not yet been (or can't be) converted to use the
  4. * regular protocol decoders...
  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/string.h>
  24. #include <linux/jiffies.h>
  25. #include <media/ir-common.h>
  26. #include "ir-core-priv.h"
  27. /* -------------------------------------------------------------------------- */
  28. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  29. MODULE_LICENSE("GPL");
  30. /* -------------------------------------------------------------------------- */
  31. /* extract mask bits out of data and pack them into the result */
  32. u32 ir_extract_bits(u32 data, u32 mask)
  33. {
  34. u32 vbit = 1, value = 0;
  35. do {
  36. if (mask&1) {
  37. if (data&1)
  38. value |= vbit;
  39. vbit<<=1;
  40. }
  41. data>>=1;
  42. } while (mask>>=1);
  43. return value;
  44. }
  45. EXPORT_SYMBOL_GPL(ir_extract_bits);
  46. /* RC5 decoding stuff, moved from bttv-input.c to share it with
  47. * saa7134 */
  48. /* decode raw bit pattern to RC5 code */
  49. static u32 ir_rc5_decode(unsigned int code)
  50. {
  51. unsigned int org_code = code;
  52. unsigned int pair;
  53. unsigned int rc5 = 0;
  54. int i;
  55. for (i = 0; i < 14; ++i) {
  56. pair = code & 0x3;
  57. code >>= 2;
  58. rc5 <<= 1;
  59. switch (pair) {
  60. case 0:
  61. case 2:
  62. break;
  63. case 1:
  64. rc5 |= 1;
  65. break;
  66. case 3:
  67. IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
  68. return 0;
  69. }
  70. }
  71. IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
  72. "instr=%x\n", rc5, org_code, RC5_START(rc5),
  73. RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
  74. return rc5;
  75. }
  76. void ir_rc5_timer_end(unsigned long data)
  77. {
  78. struct card_ir *ir = (struct card_ir *)data;
  79. struct timeval tv;
  80. unsigned long current_jiffies;
  81. u32 gap;
  82. u32 rc5 = 0;
  83. /* get time */
  84. current_jiffies = jiffies;
  85. do_gettimeofday(&tv);
  86. /* avoid overflow with gap >1s */
  87. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  88. gap = 200000;
  89. } else {
  90. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  91. tv.tv_usec - ir->base_time.tv_usec;
  92. }
  93. /* signal we're ready to start a new code */
  94. ir->active = 0;
  95. /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
  96. if (gap < 28000) {
  97. IR_dprintk(1, "ir-common: spurious timer_end\n");
  98. return;
  99. }
  100. if (ir->last_bit < 20) {
  101. /* ignore spurious codes (caused by light/other remotes) */
  102. IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
  103. } else {
  104. ir->code = (ir->code << ir->shift_by) | 1;
  105. rc5 = ir_rc5_decode(ir->code);
  106. /* two start bits? */
  107. if (RC5_START(rc5) != ir->start) {
  108. IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
  109. /* right address? */
  110. } else if (RC5_ADDR(rc5) == ir->addr) {
  111. u32 toggle = RC5_TOGGLE(rc5);
  112. u32 instr = RC5_INSTR(rc5);
  113. /* Good code */
  114. ir_keydown(ir->dev, instr, toggle);
  115. IR_dprintk(1, "ir-common: instruction %x, toggle %x\n",
  116. instr, toggle);
  117. }
  118. }
  119. }
  120. EXPORT_SYMBOL_GPL(ir_rc5_timer_end);