blob: 7401a79897199d27611314106e614755c9c7e50d [file] [log] [blame]
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -03001/*
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>
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030024#include <linux/string.h>
Mauro Carvalho Chehab0b778a52006-12-27 14:04:09 -020025#include <linux/jiffies.h>
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030026#include <media/ir-common.h>
27
28/* -------------------------------------------------------------------------- */
29
30MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
31MODULE_LICENSE("GPL");
32
33static int repeat = 1;
34module_param(repeat, int, 0444);
35MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
36
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030037/* -------------------------------------------------------------------------- */
38
39static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
40{
41 if (KEY_RESERVED == ir->keycode) {
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030042 printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n",
43 dev->name, ir->ir_key, ir->keypressed);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030044 return;
45 }
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -030046 IR_dprintk(1,"%s: key event code=%d down=%d\n",
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030047 dev->name,ir->keycode,ir->keypressed);
48 input_report_key(dev,ir->keycode,ir->keypressed);
49 input_sync(dev);
50}
51
52/* -------------------------------------------------------------------------- */
53
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030054int ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030055 int ir_type, struct ir_scancode_table *ir_codes)
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030056{
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030057 ir->ir_type = ir_type;
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030058
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030059 if (repeat)
60 set_bit(EV_REP, dev->evbit);
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030061
Mauro Carvalho Chehab75543cc2009-12-11 09:44:23 -030062 ir_input_register(dev, ir_codes);
63
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030064 return 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030065}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030066EXPORT_SYMBOL_GPL(ir_input_init);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030067
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030068
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030069void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
70{
71 if (ir->keypressed) {
72 ir->keypressed = 0;
73 ir_input_key_event(dev,ir);
74 }
75}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030076EXPORT_SYMBOL_GPL(ir_input_nokey);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030077
78void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030079 u32 ir_key)
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030080{
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030081 u32 keycode = ir_g_keycode_from_table(dev, ir_key);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030082
83 if (ir->keypressed && ir->keycode != keycode) {
84 ir->keypressed = 0;
85 ir_input_key_event(dev,ir);
86 }
87 if (!ir->keypressed) {
88 ir->ir_key = ir_key;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030089 ir->keycode = keycode;
90 ir->keypressed = 1;
91 ir_input_key_event(dev,ir);
92 }
93}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030094EXPORT_SYMBOL_GPL(ir_input_keydown);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030095
96/* -------------------------------------------------------------------------- */
Trent Piephod67be612007-07-11 20:28:44 -030097/* extract mask bits out of data and pack them into the result */
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030098u32 ir_extract_bits(u32 data, u32 mask)
99{
Trent Piephod67be612007-07-11 20:28:44 -0300100 u32 vbit = 1, value = 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300101
Trent Piephod67be612007-07-11 20:28:44 -0300102 do {
103 if (mask&1) {
104 if (data&1)
105 value |= vbit;
106 vbit<<=1;
107 }
108 data>>=1;
109 } while (mask>>=1);
110
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300111 return value;
112}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300113EXPORT_SYMBOL_GPL(ir_extract_bits);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300114
115static int inline getbit(u32 *samples, int bit)
116{
117 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
118}
119
120/* sump raw samples for visual debugging ;) */
121int ir_dump_samples(u32 *samples, int count)
122{
123 int i, bit, start;
124
125 printk(KERN_DEBUG "ir samples: ");
126 start = 0;
127 for (i = 0; i < count * 32; i++) {
128 bit = getbit(samples,i);
129 if (bit)
130 start = 1;
131 if (0 == start)
132 continue;
133 printk("%s", bit ? "#" : "_");
134 }
135 printk("\n");
136 return 0;
137}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300138EXPORT_SYMBOL_GPL(ir_dump_samples);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300139
140/* decode raw samples, pulse distance coding used by NEC remotes */
141int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
142{
143 int i,last,bit,len;
144 u32 curBit;
145 u32 value;
146
147 /* find start burst */
148 for (i = len = 0; i < count * 32; i++) {
149 bit = getbit(samples,i);
150 if (bit) {
151 len++;
152 } else {
153 if (len >= 29)
154 break;
155 len = 0;
156 }
157 }
158
159 /* start burst to short */
160 if (len < 29)
161 return 0xffffffff;
162
163 /* find start silence */
164 for (len = 0; i < count * 32; i++) {
165 bit = getbit(samples,i);
166 if (bit) {
167 break;
168 } else {
169 len++;
170 }
171 }
172
173 /* silence to short */
174 if (len < 7)
175 return 0xffffffff;
176
177 /* go decoding */
178 len = 0;
179 last = 1;
180 value = 0; curBit = 1;
181 for (; i < count * 32; i++) {
182 bit = getbit(samples,i);
183 if (last) {
184 if(bit) {
185 continue;
186 } else {
187 len = 1;
188 }
189 } else {
190 if (bit) {
191 if (len > (low + high) /2)
192 value |= curBit;
193 curBit <<= 1;
194 if (curBit == 1)
195 break;
196 } else {
197 len++;
198 }
199 }
200 last = bit;
201 }
202
203 return value;
204}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300205EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300206
207/* decode raw samples, biphase coding, used by rc5 for example */
208int ir_decode_biphase(u32 *samples, int count, int low, int high)
209{
210 int i,last,bit,len,flips;
211 u32 value;
212
213 /* find start bit (1) */
214 for (i = 0; i < 32; i++) {
215 bit = getbit(samples,i);
216 if (bit)
217 break;
218 }
219
220 /* go decoding */
221 len = 0;
222 flips = 0;
223 value = 1;
224 for (; i < count * 32; i++) {
225 if (len > high)
226 break;
227 if (flips > 1)
228 break;
229 last = bit;
230 bit = getbit(samples,i);
231 if (last == bit) {
232 len++;
233 continue;
234 }
235 if (len < low) {
236 len++;
237 flips++;
238 continue;
239 }
240 value <<= 1;
241 value |= bit;
242 flips = 0;
243 len = 1;
244 }
245 return value;
246}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300247EXPORT_SYMBOL_GPL(ir_decode_biphase);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300248
Hermann Pitton91607232006-12-07 21:45:28 -0300249/* RC5 decoding stuff, moved from bttv-input.c to share it with
250 * saa7134 */
251
252/* decode raw bit pattern to RC5 code */
Andy Walls1d23a002009-09-27 20:05:23 -0300253u32 ir_rc5_decode(unsigned int code)
Hermann Pitton91607232006-12-07 21:45:28 -0300254{
255 unsigned int org_code = code;
256 unsigned int pair;
257 unsigned int rc5 = 0;
258 int i;
259
260 for (i = 0; i < 14; ++i) {
261 pair = code & 0x3;
262 code >>= 2;
263
264 rc5 <<= 1;
265 switch (pair) {
266 case 0:
267 case 2:
268 break;
269 case 1:
270 rc5 |= 1;
271 break;
272 case 3:
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300273 IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
Hermann Pitton91607232006-12-07 21:45:28 -0300274 return 0;
275 }
276 }
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300277 IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
Hermann Pitton91607232006-12-07 21:45:28 -0300278 "instr=%x\n", rc5, org_code, RC5_START(rc5),
279 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
280 return rc5;
281}
Andy Walls1d23a002009-09-27 20:05:23 -0300282EXPORT_SYMBOL_GPL(ir_rc5_decode);
Hermann Pitton91607232006-12-07 21:45:28 -0300283
284void ir_rc5_timer_end(unsigned long data)
285{
286 struct card_ir *ir = (struct card_ir *)data;
287 struct timeval tv;
288 unsigned long current_jiffies, timeout;
289 u32 gap;
290 u32 rc5 = 0;
291
292 /* get time */
293 current_jiffies = jiffies;
294 do_gettimeofday(&tv);
295
296 /* avoid overflow with gap >1s */
297 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
298 gap = 200000;
299 } else {
300 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
301 tv.tv_usec - ir->base_time.tv_usec;
302 }
303
Vincent Penne726cf562007-03-25 11:58:23 -0300304 /* signal we're ready to start a new code */
305 ir->active = 0;
306
307 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
Hermann Pitton91607232006-12-07 21:45:28 -0300308 if (gap < 28000) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300309 IR_dprintk(1, "ir-common: spurious timer_end\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300310 return;
311 }
312
Hermann Pitton91607232006-12-07 21:45:28 -0300313 if (ir->last_bit < 20) {
314 /* ignore spurious codes (caused by light/other remotes) */
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300315 IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
Hermann Pitton91607232006-12-07 21:45:28 -0300316 } else {
317 ir->code = (ir->code << ir->shift_by) | 1;
318 rc5 = ir_rc5_decode(ir->code);
319
320 /* two start bits? */
321 if (RC5_START(rc5) != ir->start) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300322 IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
Hermann Pitton91607232006-12-07 21:45:28 -0300323
324 /* right address? */
325 } else if (RC5_ADDR(rc5) == ir->addr) {
326 u32 toggle = RC5_TOGGLE(rc5);
327 u32 instr = RC5_INSTR(rc5);
328
329 /* Good code, decide if repeat/repress */
330 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
331 instr != RC5_INSTR(ir->last_rc5)) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300332 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
Hermann Pitton91607232006-12-07 21:45:28 -0300333 toggle);
334 ir_input_nokey(ir->dev, &ir->ir);
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300335 ir_input_keydown(ir->dev, &ir->ir, instr);
Hermann Pitton91607232006-12-07 21:45:28 -0300336 }
337
338 /* Set/reset key-up timer */
Mauro Carvalho Chehabf7518bd2007-07-17 16:27:30 -0300339 timeout = current_jiffies +
340 msecs_to_jiffies(ir->rc5_key_timeout);
Hermann Pitton91607232006-12-07 21:45:28 -0300341 mod_timer(&ir->timer_keyup, timeout);
342
343 /* Save code for repeat test */
344 ir->last_rc5 = rc5;
345 }
346 }
347}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300348EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
Hermann Pitton91607232006-12-07 21:45:28 -0300349
350void ir_rc5_timer_keyup(unsigned long data)
351{
352 struct card_ir *ir = (struct card_ir *)data;
353
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300354 IR_dprintk(1, "ir-common: key released\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300355 ir_input_nokey(ir->dev, &ir->ir);
356}
Hermann Pitton91607232006-12-07 21:45:28 -0300357EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);