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 <linux/jiffies.h>
  26. #include <media/ir-common.h>
  27. /* -------------------------------------------------------------------------- */
  28. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  29. MODULE_LICENSE("GPL");
  30. static int repeat = 1;
  31. module_param(repeat, int, 0444);
  32. MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
  33. static int debug = 0; /* debug level (0,1,2) */
  34. module_param(debug, int, 0644);
  35. #define dprintk(level, fmt, arg...) if (debug >= level) \
  36. printk(KERN_DEBUG fmt , ## arg)
  37. /* -------------------------------------------------------------------------- */
  38. static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
  39. {
  40. if (KEY_RESERVED == ir->keycode) {
  41. printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
  42. dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
  43. return;
  44. }
  45. dprintk(1,"%s: key event code=%d down=%d\n",
  46. dev->name,ir->keycode,ir->keypressed);
  47. input_report_key(dev,ir->keycode,ir->keypressed);
  48. input_sync(dev);
  49. }
  50. /* -------------------------------------------------------------------------- */
  51. void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
  52. int ir_type, IR_KEYTAB_TYPE *ir_codes)
  53. {
  54. int i;
  55. ir->ir_type = ir_type;
  56. if (ir_codes)
  57. memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
  58. dev->keycode = ir->ir_codes;
  59. dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
  60. dev->keycodemax = IR_KEYTAB_SIZE;
  61. for (i = 0; i < IR_KEYTAB_SIZE; i++)
  62. set_bit(ir->ir_codes[i], dev->keybit);
  63. clear_bit(0, dev->keybit);
  64. set_bit(EV_KEY, dev->evbit);
  65. if (repeat)
  66. set_bit(EV_REP, dev->evbit);
  67. }
  68. void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
  69. {
  70. if (ir->keypressed) {
  71. ir->keypressed = 0;
  72. ir_input_key_event(dev,ir);
  73. }
  74. }
  75. void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
  76. u32 ir_key, u32 ir_raw)
  77. {
  78. u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
  79. if (ir->keypressed && ir->keycode != keycode) {
  80. ir->keypressed = 0;
  81. ir_input_key_event(dev,ir);
  82. }
  83. if (!ir->keypressed) {
  84. ir->ir_key = ir_key;
  85. ir->ir_raw = ir_raw;
  86. ir->keycode = keycode;
  87. ir->keypressed = 1;
  88. ir_input_key_event(dev,ir);
  89. }
  90. }
  91. /* -------------------------------------------------------------------------- */
  92. u32 ir_extract_bits(u32 data, u32 mask)
  93. {
  94. int mbit, vbit;
  95. u32 value;
  96. value = 0;
  97. vbit = 0;
  98. for (mbit = 0; mbit < 32; mbit++) {
  99. if (!(mask & ((u32)1 << mbit)))
  100. continue;
  101. if (data & ((u32)1 << mbit))
  102. value |= (1 << vbit);
  103. vbit++;
  104. }
  105. return value;
  106. }
  107. static int inline getbit(u32 *samples, int bit)
  108. {
  109. return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
  110. }
  111. /* sump raw samples for visual debugging ;) */
  112. int ir_dump_samples(u32 *samples, int count)
  113. {
  114. int i, bit, start;
  115. printk(KERN_DEBUG "ir samples: ");
  116. start = 0;
  117. for (i = 0; i < count * 32; i++) {
  118. bit = getbit(samples,i);
  119. if (bit)
  120. start = 1;
  121. if (0 == start)
  122. continue;
  123. printk("%s", bit ? "#" : "_");
  124. }
  125. printk("\n");
  126. return 0;
  127. }
  128. /* decode raw samples, pulse distance coding used by NEC remotes */
  129. int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
  130. {
  131. int i,last,bit,len;
  132. u32 curBit;
  133. u32 value;
  134. /* find start burst */
  135. for (i = len = 0; i < count * 32; i++) {
  136. bit = getbit(samples,i);
  137. if (bit) {
  138. len++;
  139. } else {
  140. if (len >= 29)
  141. break;
  142. len = 0;
  143. }
  144. }
  145. /* start burst to short */
  146. if (len < 29)
  147. return 0xffffffff;
  148. /* find start silence */
  149. for (len = 0; i < count * 32; i++) {
  150. bit = getbit(samples,i);
  151. if (bit) {
  152. break;
  153. } else {
  154. len++;
  155. }
  156. }
  157. /* silence to short */
  158. if (len < 7)
  159. return 0xffffffff;
  160. /* go decoding */
  161. len = 0;
  162. last = 1;
  163. value = 0; curBit = 1;
  164. for (; i < count * 32; i++) {
  165. bit = getbit(samples,i);
  166. if (last) {
  167. if(bit) {
  168. continue;
  169. } else {
  170. len = 1;
  171. }
  172. } else {
  173. if (bit) {
  174. if (len > (low + high) /2)
  175. value |= curBit;
  176. curBit <<= 1;
  177. if (curBit == 1)
  178. break;
  179. } else {
  180. len++;
  181. }
  182. }
  183. last = bit;
  184. }
  185. return value;
  186. }
  187. /* decode raw samples, biphase coding, used by rc5 for example */
  188. int ir_decode_biphase(u32 *samples, int count, int low, int high)
  189. {
  190. int i,last,bit,len,flips;
  191. u32 value;
  192. /* find start bit (1) */
  193. for (i = 0; i < 32; i++) {
  194. bit = getbit(samples,i);
  195. if (bit)
  196. break;
  197. }
  198. /* go decoding */
  199. len = 0;
  200. flips = 0;
  201. value = 1;
  202. for (; i < count * 32; i++) {
  203. if (len > high)
  204. break;
  205. if (flips > 1)
  206. break;
  207. last = bit;
  208. bit = getbit(samples,i);
  209. if (last == bit) {
  210. len++;
  211. continue;
  212. }
  213. if (len < low) {
  214. len++;
  215. flips++;
  216. continue;
  217. }
  218. value <<= 1;
  219. value |= bit;
  220. flips = 0;
  221. len = 1;
  222. }
  223. return value;
  224. }
  225. EXPORT_SYMBOL_GPL(ir_input_init);
  226. EXPORT_SYMBOL_GPL(ir_input_nokey);
  227. EXPORT_SYMBOL_GPL(ir_input_keydown);
  228. EXPORT_SYMBOL_GPL(ir_extract_bits);
  229. EXPORT_SYMBOL_GPL(ir_dump_samples);
  230. EXPORT_SYMBOL_GPL(ir_decode_biphase);
  231. EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
  232. /*
  233. * Local variables:
  234. * c-basic-offset: 8
  235. * End:
  236. */