blob: e616f624ceaac7a0fe1518875d5b712cc1f74304 [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
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030057int 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 Chehabf6fc5042009-11-29 11:08:02 -030062 ir->keytable.size = ir_roundup_tablesize(ir_codes->size);
63 ir->keytable.scan = kzalloc(ir->keytable.size *
64 sizeof(struct ir_scancode), GFP_KERNEL);
65 if (!ir->keytable.scan)
66 return -ENOMEM;
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030067
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030068 IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
69 ir->keytable.size,
70 ir->keytable.size * sizeof(ir->keytable.scan));
71
72 ir_copy_table(&ir->keytable, ir_codes);
73 ir_set_keycode_table(dev, &ir->keytable);
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030074
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030075 clear_bit(0, dev->keybit);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030076 set_bit(EV_KEY, dev->evbit);
77 if (repeat)
78 set_bit(EV_REP, dev->evbit);
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030079
80 return 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030081}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030082EXPORT_SYMBOL_GPL(ir_input_init);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030083
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030084
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030085void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
86{
87 if (ir->keypressed) {
88 ir->keypressed = 0;
89 ir_input_key_event(dev,ir);
90 }
91}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030092EXPORT_SYMBOL_GPL(ir_input_nokey);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030093
94void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030095 u32 ir_key)
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030096{
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030097 u32 keycode = ir_g_keycode_from_table(dev, ir_key);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030098
99 if (ir->keypressed && ir->keycode != keycode) {
100 ir->keypressed = 0;
101 ir_input_key_event(dev,ir);
102 }
103 if (!ir->keypressed) {
104 ir->ir_key = ir_key;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300105 ir->keycode = keycode;
106 ir->keypressed = 1;
107 ir_input_key_event(dev,ir);
108 }
109}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300110EXPORT_SYMBOL_GPL(ir_input_keydown);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300111
112/* -------------------------------------------------------------------------- */
Trent Piephod67be612007-07-11 20:28:44 -0300113/* extract mask bits out of data and pack them into the result */
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300114u32 ir_extract_bits(u32 data, u32 mask)
115{
Trent Piephod67be612007-07-11 20:28:44 -0300116 u32 vbit = 1, value = 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300117
Trent Piephod67be612007-07-11 20:28:44 -0300118 do {
119 if (mask&1) {
120 if (data&1)
121 value |= vbit;
122 vbit<<=1;
123 }
124 data>>=1;
125 } while (mask>>=1);
126
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300127 return value;
128}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300129EXPORT_SYMBOL_GPL(ir_extract_bits);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300130
131static int inline getbit(u32 *samples, int bit)
132{
133 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
134}
135
136/* sump raw samples for visual debugging ;) */
137int ir_dump_samples(u32 *samples, int count)
138{
139 int i, bit, start;
140
141 printk(KERN_DEBUG "ir samples: ");
142 start = 0;
143 for (i = 0; i < count * 32; i++) {
144 bit = getbit(samples,i);
145 if (bit)
146 start = 1;
147 if (0 == start)
148 continue;
149 printk("%s", bit ? "#" : "_");
150 }
151 printk("\n");
152 return 0;
153}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300154EXPORT_SYMBOL_GPL(ir_dump_samples);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300155
156/* decode raw samples, pulse distance coding used by NEC remotes */
157int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
158{
159 int i,last,bit,len;
160 u32 curBit;
161 u32 value;
162
163 /* find start burst */
164 for (i = len = 0; i < count * 32; i++) {
165 bit = getbit(samples,i);
166 if (bit) {
167 len++;
168 } else {
169 if (len >= 29)
170 break;
171 len = 0;
172 }
173 }
174
175 /* start burst to short */
176 if (len < 29)
177 return 0xffffffff;
178
179 /* find start silence */
180 for (len = 0; i < count * 32; i++) {
181 bit = getbit(samples,i);
182 if (bit) {
183 break;
184 } else {
185 len++;
186 }
187 }
188
189 /* silence to short */
190 if (len < 7)
191 return 0xffffffff;
192
193 /* go decoding */
194 len = 0;
195 last = 1;
196 value = 0; curBit = 1;
197 for (; i < count * 32; i++) {
198 bit = getbit(samples,i);
199 if (last) {
200 if(bit) {
201 continue;
202 } else {
203 len = 1;
204 }
205 } else {
206 if (bit) {
207 if (len > (low + high) /2)
208 value |= curBit;
209 curBit <<= 1;
210 if (curBit == 1)
211 break;
212 } else {
213 len++;
214 }
215 }
216 last = bit;
217 }
218
219 return value;
220}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300221EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300222
223/* decode raw samples, biphase coding, used by rc5 for example */
224int ir_decode_biphase(u32 *samples, int count, int low, int high)
225{
226 int i,last,bit,len,flips;
227 u32 value;
228
229 /* find start bit (1) */
230 for (i = 0; i < 32; i++) {
231 bit = getbit(samples,i);
232 if (bit)
233 break;
234 }
235
236 /* go decoding */
237 len = 0;
238 flips = 0;
239 value = 1;
240 for (; i < count * 32; i++) {
241 if (len > high)
242 break;
243 if (flips > 1)
244 break;
245 last = bit;
246 bit = getbit(samples,i);
247 if (last == bit) {
248 len++;
249 continue;
250 }
251 if (len < low) {
252 len++;
253 flips++;
254 continue;
255 }
256 value <<= 1;
257 value |= bit;
258 flips = 0;
259 len = 1;
260 }
261 return value;
262}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300263EXPORT_SYMBOL_GPL(ir_decode_biphase);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300264
Hermann Pitton91607232006-12-07 21:45:28 -0300265/* RC5 decoding stuff, moved from bttv-input.c to share it with
266 * saa7134 */
267
268/* decode raw bit pattern to RC5 code */
Andy Walls1d23a002009-09-27 20:05:23 -0300269u32 ir_rc5_decode(unsigned int code)
Hermann Pitton91607232006-12-07 21:45:28 -0300270{
271 unsigned int org_code = code;
272 unsigned int pair;
273 unsigned int rc5 = 0;
274 int i;
275
276 for (i = 0; i < 14; ++i) {
277 pair = code & 0x3;
278 code >>= 2;
279
280 rc5 <<= 1;
281 switch (pair) {
282 case 0:
283 case 2:
284 break;
285 case 1:
286 rc5 |= 1;
287 break;
288 case 3:
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300289 IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
Hermann Pitton91607232006-12-07 21:45:28 -0300290 return 0;
291 }
292 }
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300293 IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
Hermann Pitton91607232006-12-07 21:45:28 -0300294 "instr=%x\n", rc5, org_code, RC5_START(rc5),
295 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
296 return rc5;
297}
Andy Walls1d23a002009-09-27 20:05:23 -0300298EXPORT_SYMBOL_GPL(ir_rc5_decode);
Hermann Pitton91607232006-12-07 21:45:28 -0300299
300void ir_rc5_timer_end(unsigned long data)
301{
302 struct card_ir *ir = (struct card_ir *)data;
303 struct timeval tv;
304 unsigned long current_jiffies, timeout;
305 u32 gap;
306 u32 rc5 = 0;
307
308 /* get time */
309 current_jiffies = jiffies;
310 do_gettimeofday(&tv);
311
312 /* avoid overflow with gap >1s */
313 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
314 gap = 200000;
315 } else {
316 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
317 tv.tv_usec - ir->base_time.tv_usec;
318 }
319
Vincent Penne726cf562007-03-25 11:58:23 -0300320 /* signal we're ready to start a new code */
321 ir->active = 0;
322
323 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
Hermann Pitton91607232006-12-07 21:45:28 -0300324 if (gap < 28000) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300325 IR_dprintk(1, "ir-common: spurious timer_end\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300326 return;
327 }
328
Hermann Pitton91607232006-12-07 21:45:28 -0300329 if (ir->last_bit < 20) {
330 /* ignore spurious codes (caused by light/other remotes) */
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300331 IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
Hermann Pitton91607232006-12-07 21:45:28 -0300332 } else {
333 ir->code = (ir->code << ir->shift_by) | 1;
334 rc5 = ir_rc5_decode(ir->code);
335
336 /* two start bits? */
337 if (RC5_START(rc5) != ir->start) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300338 IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
Hermann Pitton91607232006-12-07 21:45:28 -0300339
340 /* right address? */
341 } else if (RC5_ADDR(rc5) == ir->addr) {
342 u32 toggle = RC5_TOGGLE(rc5);
343 u32 instr = RC5_INSTR(rc5);
344
345 /* Good code, decide if repeat/repress */
346 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
347 instr != RC5_INSTR(ir->last_rc5)) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300348 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
Hermann Pitton91607232006-12-07 21:45:28 -0300349 toggle);
350 ir_input_nokey(ir->dev, &ir->ir);
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300351 ir_input_keydown(ir->dev, &ir->ir, instr);
Hermann Pitton91607232006-12-07 21:45:28 -0300352 }
353
354 /* Set/reset key-up timer */
Mauro Carvalho Chehabf7518bd2007-07-17 16:27:30 -0300355 timeout = current_jiffies +
356 msecs_to_jiffies(ir->rc5_key_timeout);
Hermann Pitton91607232006-12-07 21:45:28 -0300357 mod_timer(&ir->timer_keyup, timeout);
358
359 /* Save code for repeat test */
360 ir->last_rc5 = rc5;
361 }
362 }
363}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300364EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
Hermann Pitton91607232006-12-07 21:45:28 -0300365
366void ir_rc5_timer_keyup(unsigned long data)
367{
368 struct card_ir *ir = (struct card_ir *)data;
369
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300370 IR_dprintk(1, "ir-common: key released\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300371 ir_input_nokey(ir->dev, &ir->ir);
372}
Hermann Pitton91607232006-12-07 21:45:28 -0300373EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);