[media] ir-core: convert drivers/media/video/cx88 to ir-core
[linux-2.6-block.git] / drivers / media / IR / ir-functions.c
CommitLineData
d15549ac
RC
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
23#include <linux/module.h>
d15549ac 24#include <linux/string.h>
0b778a56 25#include <linux/jiffies.h>
d15549ac 26#include <media/ir-common.h>
3f113e36 27#include "ir-core-priv.h"
d15549ac
RC
28
29/* -------------------------------------------------------------------------- */
30
31MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
32MODULE_LICENSE("GPL");
33
34static int repeat = 1;
35module_param(repeat, int, 0444);
36MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
37
d15549ac
RC
38/* -------------------------------------------------------------------------- */
39
40static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
41{
42 if (KEY_RESERVED == ir->keycode) {
8573b74a
MCC
43 printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n",
44 dev->name, ir->ir_key, ir->keypressed);
d15549ac
RC
45 return;
46 }
4e89217b 47 IR_dprintk(1,"%s: key event code=%d down=%d\n",
d15549ac
RC
48 dev->name,ir->keycode,ir->keypressed);
49 input_report_key(dev,ir->keycode,ir->keypressed);
50 input_sync(dev);
51}
52
53/* -------------------------------------------------------------------------- */
54
055cd556 55int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
971e8298 56 const u64 ir_type)
d15549ac 57{
d15549ac 58 ir->ir_type = ir_type;
715a2233 59
d15549ac
RC
60 if (repeat)
61 set_bit(EV_REP, dev->evbit);
055cd556
MCC
62
63 return 0;
d15549ac 64}
4db16db4 65EXPORT_SYMBOL_GPL(ir_input_init);
d15549ac 66
055cd556 67
d15549ac
RC
68void 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}
4db16db4 75EXPORT_SYMBOL_GPL(ir_input_nokey);
d15549ac
RC
76
77void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
8573b74a 78 u32 ir_key)
d15549ac 79{
8573b74a 80 u32 keycode = ir_g_keycode_from_table(dev, ir_key);
d15549ac
RC
81
82 if (ir->keypressed && ir->keycode != keycode) {
83 ir->keypressed = 0;
84 ir_input_key_event(dev,ir);
85 }
86 if (!ir->keypressed) {
87 ir->ir_key = ir_key;
d15549ac
RC
88 ir->keycode = keycode;
89 ir->keypressed = 1;
90 ir_input_key_event(dev,ir);
91 }
92}
4db16db4 93EXPORT_SYMBOL_GPL(ir_input_keydown);
d15549ac
RC
94
95/* -------------------------------------------------------------------------- */
d67be61e 96/* extract mask bits out of data and pack them into the result */
d15549ac
RC
97u32 ir_extract_bits(u32 data, u32 mask)
98{
d67be61e
TP
99 u32 vbit = 1, value = 0;
100
101 do {
102 if (mask&1) {
103 if (data&1)
104 value |= vbit;
105 vbit<<=1;
106 }
107 data>>=1;
108 } while (mask>>=1);
109
d15549ac
RC
110 return value;
111}
4db16db4 112EXPORT_SYMBOL_GPL(ir_extract_bits);
d15549ac 113
9160723e
HP
114/* RC5 decoding stuff, moved from bttv-input.c to share it with
115 * saa7134 */
116
117/* decode raw bit pattern to RC5 code */
1d23a002 118u32 ir_rc5_decode(unsigned int code)
9160723e
HP
119{
120 unsigned int org_code = code;
121 unsigned int pair;
122 unsigned int rc5 = 0;
123 int i;
124
125 for (i = 0; i < 14; ++i) {
126 pair = code & 0x3;
127 code >>= 2;
128
129 rc5 <<= 1;
130 switch (pair) {
131 case 0:
132 case 2:
133 break;
134 case 1:
135 rc5 |= 1;
136 break;
137 case 3:
4e89217b 138 IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
9160723e
HP
139 return 0;
140 }
141 }
4e89217b 142 IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
9160723e
HP
143 "instr=%x\n", rc5, org_code, RC5_START(rc5),
144 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
145 return rc5;
146}
1d23a002 147EXPORT_SYMBOL_GPL(ir_rc5_decode);
9160723e
HP
148
149void ir_rc5_timer_end(unsigned long data)
150{
151 struct card_ir *ir = (struct card_ir *)data;
152 struct timeval tv;
153 unsigned long current_jiffies, timeout;
154 u32 gap;
155 u32 rc5 = 0;
156
157 /* get time */
158 current_jiffies = jiffies;
159 do_gettimeofday(&tv);
160
161 /* avoid overflow with gap >1s */
162 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
163 gap = 200000;
164 } else {
165 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
166 tv.tv_usec - ir->base_time.tv_usec;
167 }
168
726cf56f
VP
169 /* signal we're ready to start a new code */
170 ir->active = 0;
171
172 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
9160723e 173 if (gap < 28000) {
4e89217b 174 IR_dprintk(1, "ir-common: spurious timer_end\n");
9160723e
HP
175 return;
176 }
177
9160723e
HP
178 if (ir->last_bit < 20) {
179 /* ignore spurious codes (caused by light/other remotes) */
4e89217b 180 IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
9160723e
HP
181 } else {
182 ir->code = (ir->code << ir->shift_by) | 1;
183 rc5 = ir_rc5_decode(ir->code);
184
185 /* two start bits? */
186 if (RC5_START(rc5) != ir->start) {
4e89217b 187 IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
9160723e
HP
188
189 /* right address? */
190 } else if (RC5_ADDR(rc5) == ir->addr) {
191 u32 toggle = RC5_TOGGLE(rc5);
192 u32 instr = RC5_INSTR(rc5);
193
194 /* Good code, decide if repeat/repress */
195 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
196 instr != RC5_INSTR(ir->last_rc5)) {
4e89217b 197 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
9160723e
HP
198 toggle);
199 ir_input_nokey(ir->dev, &ir->ir);
8573b74a 200 ir_input_keydown(ir->dev, &ir->ir, instr);
9160723e
HP
201 }
202
203 /* Set/reset key-up timer */
f7518bd2
MCC
204 timeout = current_jiffies +
205 msecs_to_jiffies(ir->rc5_key_timeout);
9160723e
HP
206 mod_timer(&ir->timer_keyup, timeout);
207
208 /* Save code for repeat test */
209 ir->last_rc5 = rc5;
210 }
211 }
212}
4db16db4 213EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
9160723e
HP
214
215void ir_rc5_timer_keyup(unsigned long data)
216{
217 struct card_ir *ir = (struct card_ir *)data;
218
4e89217b 219 IR_dprintk(1, "ir-common: key released\n");
9160723e
HP
220 ir_input_nokey(ir->dev, &ir->ir);
221}
9160723e 222EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);