blob: 29885c2893d28d42743e555a927c71c0c1024b77 [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
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -030037int media_ir_debug; /* media_ir_debug level (0,1,2) */
38module_param_named(debug, media_ir_debug, int, 0644);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030039
40/* -------------------------------------------------------------------------- */
41
42static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
43{
44 if (KEY_RESERVED == ir->keycode) {
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030045 printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n",
46 dev->name, ir->ir_key, ir->keypressed);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030047 return;
48 }
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -030049 IR_dprintk(1,"%s: key event code=%d down=%d\n",
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030050 dev->name,ir->keycode,ir->keypressed);
51 input_report_key(dev,ir->keycode,ir->keypressed);
52 input_sync(dev);
53}
54
55/* -------------------------------------------------------------------------- */
56
57void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030058 int ir_type, struct ir_scancode_table *ir_codes)
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030059{
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030060 ir->ir_type = ir_type;
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030061
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030062 ir_set_keycode_table(dev, ir_codes);
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030063
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030064 clear_bit(0, dev->keybit);
65
66 set_bit(EV_KEY, dev->evbit);
67 if (repeat)
68 set_bit(EV_REP, dev->evbit);
69}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030070EXPORT_SYMBOL_GPL(ir_input_init);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030071
72void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
73{
74 if (ir->keypressed) {
75 ir->keypressed = 0;
76 ir_input_key_event(dev,ir);
77 }
78}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030079EXPORT_SYMBOL_GPL(ir_input_nokey);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030080
81void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030082 u32 ir_key)
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030083{
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030084 u32 keycode = ir_g_keycode_from_table(dev, ir_key);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030085
86 if (ir->keypressed && ir->keycode != keycode) {
87 ir->keypressed = 0;
88 ir_input_key_event(dev,ir);
89 }
90 if (!ir->keypressed) {
91 ir->ir_key = ir_key;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030092 ir->keycode = keycode;
93 ir->keypressed = 1;
94 ir_input_key_event(dev,ir);
95 }
96}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030097EXPORT_SYMBOL_GPL(ir_input_keydown);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030098
99/* -------------------------------------------------------------------------- */
Trent Piephod67be612007-07-11 20:28:44 -0300100/* extract mask bits out of data and pack them into the result */
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300101u32 ir_extract_bits(u32 data, u32 mask)
102{
Trent Piephod67be612007-07-11 20:28:44 -0300103 u32 vbit = 1, value = 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300104
Trent Piephod67be612007-07-11 20:28:44 -0300105 do {
106 if (mask&1) {
107 if (data&1)
108 value |= vbit;
109 vbit<<=1;
110 }
111 data>>=1;
112 } while (mask>>=1);
113
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300114 return value;
115}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300116EXPORT_SYMBOL_GPL(ir_extract_bits);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300117
118static int inline getbit(u32 *samples, int bit)
119{
120 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
121}
122
123/* sump raw samples for visual debugging ;) */
124int ir_dump_samples(u32 *samples, int count)
125{
126 int i, bit, start;
127
128 printk(KERN_DEBUG "ir samples: ");
129 start = 0;
130 for (i = 0; i < count * 32; i++) {
131 bit = getbit(samples,i);
132 if (bit)
133 start = 1;
134 if (0 == start)
135 continue;
136 printk("%s", bit ? "#" : "_");
137 }
138 printk("\n");
139 return 0;
140}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300141EXPORT_SYMBOL_GPL(ir_dump_samples);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300142
143/* decode raw samples, pulse distance coding used by NEC remotes */
144int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
145{
146 int i,last,bit,len;
147 u32 curBit;
148 u32 value;
149
150 /* find start burst */
151 for (i = len = 0; i < count * 32; i++) {
152 bit = getbit(samples,i);
153 if (bit) {
154 len++;
155 } else {
156 if (len >= 29)
157 break;
158 len = 0;
159 }
160 }
161
162 /* start burst to short */
163 if (len < 29)
164 return 0xffffffff;
165
166 /* find start silence */
167 for (len = 0; i < count * 32; i++) {
168 bit = getbit(samples,i);
169 if (bit) {
170 break;
171 } else {
172 len++;
173 }
174 }
175
176 /* silence to short */
177 if (len < 7)
178 return 0xffffffff;
179
180 /* go decoding */
181 len = 0;
182 last = 1;
183 value = 0; curBit = 1;
184 for (; i < count * 32; i++) {
185 bit = getbit(samples,i);
186 if (last) {
187 if(bit) {
188 continue;
189 } else {
190 len = 1;
191 }
192 } else {
193 if (bit) {
194 if (len > (low + high) /2)
195 value |= curBit;
196 curBit <<= 1;
197 if (curBit == 1)
198 break;
199 } else {
200 len++;
201 }
202 }
203 last = bit;
204 }
205
206 return value;
207}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300208EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300209
210/* decode raw samples, biphase coding, used by rc5 for example */
211int ir_decode_biphase(u32 *samples, int count, int low, int high)
212{
213 int i,last,bit,len,flips;
214 u32 value;
215
216 /* find start bit (1) */
217 for (i = 0; i < 32; i++) {
218 bit = getbit(samples,i);
219 if (bit)
220 break;
221 }
222
223 /* go decoding */
224 len = 0;
225 flips = 0;
226 value = 1;
227 for (; i < count * 32; i++) {
228 if (len > high)
229 break;
230 if (flips > 1)
231 break;
232 last = bit;
233 bit = getbit(samples,i);
234 if (last == bit) {
235 len++;
236 continue;
237 }
238 if (len < low) {
239 len++;
240 flips++;
241 continue;
242 }
243 value <<= 1;
244 value |= bit;
245 flips = 0;
246 len = 1;
247 }
248 return value;
249}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300250EXPORT_SYMBOL_GPL(ir_decode_biphase);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300251
Hermann Pitton91607232006-12-07 21:45:28 -0300252/* RC5 decoding stuff, moved from bttv-input.c to share it with
253 * saa7134 */
254
255/* decode raw bit pattern to RC5 code */
Andy Walls1d23a002009-09-27 20:05:23 -0300256u32 ir_rc5_decode(unsigned int code)
Hermann Pitton91607232006-12-07 21:45:28 -0300257{
258 unsigned int org_code = code;
259 unsigned int pair;
260 unsigned int rc5 = 0;
261 int i;
262
263 for (i = 0; i < 14; ++i) {
264 pair = code & 0x3;
265 code >>= 2;
266
267 rc5 <<= 1;
268 switch (pair) {
269 case 0:
270 case 2:
271 break;
272 case 1:
273 rc5 |= 1;
274 break;
275 case 3:
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300276 IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
Hermann Pitton91607232006-12-07 21:45:28 -0300277 return 0;
278 }
279 }
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300280 IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
Hermann Pitton91607232006-12-07 21:45:28 -0300281 "instr=%x\n", rc5, org_code, RC5_START(rc5),
282 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
283 return rc5;
284}
Andy Walls1d23a002009-09-27 20:05:23 -0300285EXPORT_SYMBOL_GPL(ir_rc5_decode);
Hermann Pitton91607232006-12-07 21:45:28 -0300286
287void ir_rc5_timer_end(unsigned long data)
288{
289 struct card_ir *ir = (struct card_ir *)data;
290 struct timeval tv;
291 unsigned long current_jiffies, timeout;
292 u32 gap;
293 u32 rc5 = 0;
294
295 /* get time */
296 current_jiffies = jiffies;
297 do_gettimeofday(&tv);
298
299 /* avoid overflow with gap >1s */
300 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
301 gap = 200000;
302 } else {
303 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
304 tv.tv_usec - ir->base_time.tv_usec;
305 }
306
Vincent Penne726cf562007-03-25 11:58:23 -0300307 /* signal we're ready to start a new code */
308 ir->active = 0;
309
310 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
Hermann Pitton91607232006-12-07 21:45:28 -0300311 if (gap < 28000) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300312 IR_dprintk(1, "ir-common: spurious timer_end\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300313 return;
314 }
315
Hermann Pitton91607232006-12-07 21:45:28 -0300316 if (ir->last_bit < 20) {
317 /* ignore spurious codes (caused by light/other remotes) */
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300318 IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
Hermann Pitton91607232006-12-07 21:45:28 -0300319 } else {
320 ir->code = (ir->code << ir->shift_by) | 1;
321 rc5 = ir_rc5_decode(ir->code);
322
323 /* two start bits? */
324 if (RC5_START(rc5) != ir->start) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300325 IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
Hermann Pitton91607232006-12-07 21:45:28 -0300326
327 /* right address? */
328 } else if (RC5_ADDR(rc5) == ir->addr) {
329 u32 toggle = RC5_TOGGLE(rc5);
330 u32 instr = RC5_INSTR(rc5);
331
332 /* Good code, decide if repeat/repress */
333 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
334 instr != RC5_INSTR(ir->last_rc5)) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300335 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
Hermann Pitton91607232006-12-07 21:45:28 -0300336 toggle);
337 ir_input_nokey(ir->dev, &ir->ir);
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300338 ir_input_keydown(ir->dev, &ir->ir, instr);
Hermann Pitton91607232006-12-07 21:45:28 -0300339 }
340
341 /* Set/reset key-up timer */
Mauro Carvalho Chehabf7518bd2007-07-17 16:27:30 -0300342 timeout = current_jiffies +
343 msecs_to_jiffies(ir->rc5_key_timeout);
Hermann Pitton91607232006-12-07 21:45:28 -0300344 mod_timer(&ir->timer_keyup, timeout);
345
346 /* Save code for repeat test */
347 ir->last_rc5 = rc5;
348 }
349 }
350}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300351EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
Hermann Pitton91607232006-12-07 21:45:28 -0300352
353void ir_rc5_timer_keyup(unsigned long data)
354{
355 struct card_ir *ir = (struct card_ir *)data;
356
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300357 IR_dprintk(1, "ir-common: key released\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300358 ir_input_nokey(ir->dev, &ir->ir);
359}
Hermann Pitton91607232006-12-07 21:45:28 -0300360EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);