3 * some common structs and functions to handle infrared remotes via
6 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
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
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/string.h>
26 #include <linux/jiffies.h>
27 #include <media/ir-common.h>
29 /* -------------------------------------------------------------------------- */
31 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
32 MODULE_LICENSE("GPL");
34 static int repeat = 1;
35 module_param(repeat, int, 0444);
36 MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
38 static int debug = 0; /* debug level (0,1,2) */
39 module_param(debug, int, 0644);
41 #define dprintk(level, fmt, arg...) if (debug >= level) \
42 printk(KERN_DEBUG fmt , ## arg)
44 /* -------------------------------------------------------------------------- */
46 static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
48 if (KEY_RESERVED == ir->keycode) {
49 printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
50 dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
53 dprintk(1,"%s: key event code=%d down=%d\n",
54 dev->name,ir->keycode,ir->keypressed);
55 input_report_key(dev,ir->keycode,ir->keypressed);
59 /* -------------------------------------------------------------------------- */
61 void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
62 int ir_type, IR_KEYTAB_TYPE *ir_codes)
66 ir->ir_type = ir_type;
68 memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
71 dev->keycode = ir->ir_codes;
72 dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
73 dev->keycodemax = IR_KEYTAB_SIZE;
74 for (i = 0; i < IR_KEYTAB_SIZE; i++)
75 set_bit(ir->ir_codes[i], dev->keybit);
76 clear_bit(0, dev->keybit);
78 set_bit(EV_KEY, dev->evbit);
80 set_bit(EV_REP, dev->evbit);
83 void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
87 ir_input_key_event(dev,ir);
91 void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
92 u32 ir_key, u32 ir_raw)
94 u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
96 if (ir->keypressed && ir->keycode != keycode) {
98 ir_input_key_event(dev,ir);
100 if (!ir->keypressed) {
103 ir->keycode = keycode;
105 ir_input_key_event(dev,ir);
109 /* -------------------------------------------------------------------------- */
111 u32 ir_extract_bits(u32 data, u32 mask)
118 for (mbit = 0; mbit < 32; mbit++) {
119 if (!(mask & ((u32)1 << mbit)))
121 if (data & ((u32)1 << mbit))
122 value |= (1 << vbit);
128 static int inline getbit(u32 *samples, int bit)
130 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
133 /* sump raw samples for visual debugging ;) */
134 int ir_dump_samples(u32 *samples, int count)
138 printk(KERN_DEBUG "ir samples: ");
140 for (i = 0; i < count * 32; i++) {
141 bit = getbit(samples,i);
146 printk("%s", bit ? "#" : "_");
152 /* decode raw samples, pulse distance coding used by NEC remotes */
153 int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
159 /* find start burst */
160 for (i = len = 0; i < count * 32; i++) {
161 bit = getbit(samples,i);
171 /* start burst to short */
175 /* find start silence */
176 for (len = 0; i < count * 32; i++) {
177 bit = getbit(samples,i);
185 /* silence to short */
192 value = 0; curBit = 1;
193 for (; i < count * 32; i++) {
194 bit = getbit(samples,i);
203 if (len > (low + high) /2)
218 /* decode raw samples, biphase coding, used by rc5 for example */
219 int ir_decode_biphase(u32 *samples, int count, int low, int high)
221 int i,last,bit,len,flips;
224 /* find start bit (1) */
225 for (i = 0; i < 32; i++) {
226 bit = getbit(samples,i);
235 for (; i < count * 32; i++) {
241 bit = getbit(samples,i);
259 EXPORT_SYMBOL_GPL(ir_input_init);
260 EXPORT_SYMBOL_GPL(ir_input_nokey);
261 EXPORT_SYMBOL_GPL(ir_input_keydown);
263 EXPORT_SYMBOL_GPL(ir_extract_bits);
264 EXPORT_SYMBOL_GPL(ir_dump_samples);
265 EXPORT_SYMBOL_GPL(ir_decode_biphase);
266 EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);