blob: 221b36e7f3922d0cac81da1ae06490a2a0d50cb3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (c) 2003 Gerd Knorr
4 * Copyright (c) 2003 Pavel Machek
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/interrupt.h>
26#include <linux/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "bttv.h"
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -020029#include "bttvp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* ---------------------------------------------------------------------- */
32
33static IR_KEYTAB_TYPE ir_codes_avermedia[IR_KEYTAB_SIZE] = {
34 [ 34 ] = KEY_KP0,
35 [ 40 ] = KEY_KP1,
36 [ 24 ] = KEY_KP2,
37 [ 56 ] = KEY_KP3,
38 [ 36 ] = KEY_KP4,
39 [ 20 ] = KEY_KP5,
40 [ 52 ] = KEY_KP6,
41 [ 44 ] = KEY_KP7,
42 [ 28 ] = KEY_KP8,
43 [ 60 ] = KEY_KP9,
44
45 [ 48 ] = KEY_EJECTCD, // Unmarked on my controller
46 [ 0 ] = KEY_POWER,
47 [ 18 ] = BTN_LEFT, // DISPLAY/L
48 [ 50 ] = BTN_RIGHT, // LOOP/R
49 [ 10 ] = KEY_MUTE,
50 [ 38 ] = KEY_RECORD,
51 [ 22 ] = KEY_PAUSE,
52 [ 54 ] = KEY_STOP,
53 [ 30 ] = KEY_VOLUMEDOWN,
54 [ 62 ] = KEY_VOLUMEUP,
55
56 [ 32 ] = KEY_TUNER, // TV/FM
57 [ 16 ] = KEY_CD,
58 [ 8 ] = KEY_VIDEO,
59 [ 4 ] = KEY_AUDIO,
60 [ 12 ] = KEY_ZOOM, // full screen
61 [ 2 ] = KEY_INFO, // preview
62 [ 42 ] = KEY_SEARCH, // autoscan
63 [ 26 ] = KEY_STOP, // freeze
64 [ 58 ] = KEY_RECORD, // capture
65 [ 6 ] = KEY_PLAY, // unmarked
66 [ 46 ] = KEY_RED, // unmarked
67 [ 14 ] = KEY_GREEN, // unmarked
68
69 [ 33 ] = KEY_YELLOW, // unmarked
70 [ 17 ] = KEY_CHANNELDOWN,
71 [ 49 ] = KEY_CHANNELUP,
72 [ 1 ] = KEY_BLUE, // unmarked
73};
74
75/* Matt Jesson <dvb@jesson.eclipse.co.uk */
76static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt[IR_KEYTAB_SIZE] = {
77 [ 0x28 ] = KEY_KP0, //'0' / 'enter'
78 [ 0x22 ] = KEY_KP1, //'1'
79 [ 0x12 ] = KEY_KP2, //'2' / 'up arrow'
80 [ 0x32 ] = KEY_KP3, //'3'
81 [ 0x24 ] = KEY_KP4, //'4' / 'left arrow'
82 [ 0x14 ] = KEY_KP5, //'5'
83 [ 0x34 ] = KEY_KP6, //'6' / 'right arrow'
84 [ 0x26 ] = KEY_KP7, //'7'
85 [ 0x16 ] = KEY_KP8, //'8' / 'down arrow'
86 [ 0x36 ] = KEY_KP9, //'9'
87
88 [ 0x20 ] = KEY_LIST, // 'source'
89 [ 0x10 ] = KEY_TEXT, // 'teletext'
90 [ 0x00 ] = KEY_POWER, // 'power'
91 [ 0x04 ] = KEY_AUDIO, // 'audio'
92 [ 0x06 ] = KEY_ZOOM, // 'full screen'
93 [ 0x18 ] = KEY_VIDEO, // 'display'
94 [ 0x38 ] = KEY_SEARCH, // 'loop'
95 [ 0x08 ] = KEY_INFO, // 'preview'
96 [ 0x2a ] = KEY_REWIND, // 'backward <<'
97 [ 0x1a ] = KEY_FASTFORWARD, // 'forward >>'
98 [ 0x3a ] = KEY_RECORD, // 'capture'
99 [ 0x0a ] = KEY_MUTE, // 'mute'
100 [ 0x2c ] = KEY_RECORD, // 'record'
101 [ 0x1c ] = KEY_PAUSE, // 'pause'
102 [ 0x3c ] = KEY_STOP, // 'stop'
103 [ 0x0c ] = KEY_PLAY, // 'play'
104 [ 0x2e ] = KEY_RED, // 'red'
105 [ 0x01 ] = KEY_BLUE, // 'blue' / 'cancel'
106 [ 0x0e ] = KEY_YELLOW, // 'yellow' / 'ok'
107 [ 0x21 ] = KEY_GREEN, // 'green'
108 [ 0x11 ] = KEY_CHANNELDOWN, // 'channel -'
109 [ 0x31 ] = KEY_CHANNELUP, // 'channel +'
110 [ 0x1e ] = KEY_VOLUMEDOWN, // 'volume -'
111 [ 0x3e ] = KEY_VOLUMEUP, // 'volume +'
112};
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* Attila Kondoros <attila.kondoros@chello.hu> */
115static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
116
117 [ 1 ] = KEY_KP1,
118 [ 2 ] = KEY_KP2,
119 [ 3 ] = KEY_KP3,
120 [ 4 ] = KEY_KP4,
121 [ 5 ] = KEY_KP5,
122 [ 6 ] = KEY_KP6,
123 [ 7 ] = KEY_KP7,
124 [ 8 ] = KEY_KP8,
125 [ 9 ] = KEY_KP9,
126 [ 0 ] = KEY_KP0,
127 [ 23 ] = KEY_LAST, // +100
128 [ 10 ] = KEY_LIST, // recall
129
130
131 [ 28 ] = KEY_TUNER, // TV/FM
132 [ 21 ] = KEY_SEARCH, // scan
133 [ 18 ] = KEY_POWER, // power
134 [ 31 ] = KEY_VOLUMEDOWN, // vol up
135 [ 27 ] = KEY_VOLUMEUP, // vol down
136 [ 30 ] = KEY_CHANNELDOWN, // chn up
137 [ 26 ] = KEY_CHANNELUP, // chn down
138
139 [ 17 ] = KEY_VIDEO, // video
140 [ 15 ] = KEY_ZOOM, // full screen
141 [ 19 ] = KEY_MUTE, // mute/unmute
142 [ 16 ] = KEY_TEXT, // min
143
144 [ 13 ] = KEY_STOP, // freeze
145 [ 14 ] = KEY_RECORD, // record
146 [ 29 ] = KEY_PLAYPAUSE, // stop
147 [ 25 ] = KEY_PLAY, // play
148
149 [ 22 ] = KEY_GOTO, // osd
150 [ 20 ] = KEY_REFRESH, // default
151 [ 12 ] = KEY_KPPLUS, // fine tune >>>>
152 [ 24 ] = KEY_KPMINUS // fine tune <<<<
153};
154
155/* ---------------------------------------------------------------------- */
156
Ricardo Cerqueiracc9d8d42005-11-08 21:36:20 -0800157static IR_KEYTAB_TYPE ir_codes_conceptronic[IR_KEYTAB_SIZE] = {
158
159 [ 30 ] = KEY_POWER, // power
Nickolay V. Shmyrev84cd9612005-11-08 21:36:24 -0800160 [ 7 ] = KEY_MEDIA, // source
Ricardo Cerqueiracc9d8d42005-11-08 21:36:20 -0800161 [ 28 ] = KEY_SEARCH, // scan
162
163/* FIXME: duplicate keycodes?
164 *
165 * These four keys seem to share the same GPIO as CH+, CH-, <<< and >>>
166 * The GPIO values are
167 * 6397fb for both "Scan <" and "CH -",
168 * 639ffb for "Scan >" and "CH+",
169 * 6384fb for "Tune <" and "<<<",
170 * 638cfb for "Tune >" and ">>>", regardless of the mask.
171 *
172 * [ 23 ] = KEY_BACK, // fm scan <<
173 * [ 31 ] = KEY_FORWARD, // fm scan >>
174 *
175 * [ 4 ] = KEY_LEFT, // fm tuning <
176 * [ 12 ] = KEY_RIGHT, // fm tuning >
177 *
178 * For now, these four keys are disabled. Pressing them will generate
179 * the CH+/CH-/<<</>>> events
180 */
181
182 [ 3 ] = KEY_TUNER, // TV/FM
183
184 [ 0 ] = KEY_RECORD,
185 [ 8 ] = KEY_STOP,
186 [ 17 ] = KEY_PLAY,
187
188 [ 26 ] = KEY_PLAYPAUSE, // freeze
189 [ 25 ] = KEY_ZOOM, // zoom
190 [ 15 ] = KEY_TEXT, // min
191
192 [ 1 ] = KEY_KP1,
193 [ 11 ] = KEY_KP2,
194 [ 27 ] = KEY_KP3,
195 [ 5 ] = KEY_KP4,
196 [ 9 ] = KEY_KP5,
197 [ 21 ] = KEY_KP6,
198 [ 6 ] = KEY_KP7,
199 [ 10 ] = KEY_KP8,
200 [ 18 ] = KEY_KP9,
201 [ 2 ] = KEY_KP0,
202 [ 16 ] = KEY_LAST, // +100
203 [ 19 ] = KEY_LIST, // recall
204
205 [ 31 ] = KEY_CHANNELUP, // chn down
206 [ 23 ] = KEY_CHANNELDOWN, // chn up
207 [ 22 ] = KEY_VOLUMEUP, // vol down
208 [ 20 ] = KEY_VOLUMEDOWN, // vol up
209
210 [ 4 ] = KEY_KPMINUS, // <<<
211 [ 14 ] = KEY_SETUP, // function
212 [ 12 ] = KEY_KPPLUS, // >>>
213
214 [ 13 ] = KEY_GOTO, // mts
215 [ 29 ] = KEY_REFRESH, // reset
216 [ 24 ] = KEY_MUTE // mute/unmute
217};
218
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800219static IR_KEYTAB_TYPE ir_codes_nebula[IR_KEYTAB_SIZE] = {
220 [0x00] = KEY_KP0,
221 [0x01] = KEY_KP1,
222 [0x02] = KEY_KP2,
223 [0x03] = KEY_KP3,
224 [0x04] = KEY_KP4,
225 [0x05] = KEY_KP5,
226 [0x06] = KEY_KP6,
227 [0x07] = KEY_KP7,
228 [0x08] = KEY_KP8,
229 [0x09] = KEY_KP9,
230 [0x0a] = KEY_TV,
231 [0x0b] = KEY_AUX,
232 [0x0c] = KEY_DVD,
233 [0x0d] = KEY_POWER,
234 [0x0e] = KEY_MHP, /* labelled 'Picture' */
235 [0x0f] = KEY_AUDIO,
236 [0x10] = KEY_INFO,
237 [0x11] = KEY_F13, /* 16:9 */
238 [0x12] = KEY_F14, /* 14:9 */
239 [0x13] = KEY_EPG,
240 [0x14] = KEY_EXIT,
241 [0x15] = KEY_MENU,
242 [0x16] = KEY_UP,
243 [0x17] = KEY_DOWN,
244 [0x18] = KEY_LEFT,
245 [0x19] = KEY_RIGHT,
246 [0x1a] = KEY_ENTER,
247 [0x1b] = KEY_CHANNELUP,
248 [0x1c] = KEY_CHANNELDOWN,
249 [0x1d] = KEY_VOLUMEUP,
250 [0x1e] = KEY_VOLUMEDOWN,
251 [0x1f] = KEY_RED,
252 [0x20] = KEY_GREEN,
253 [0x21] = KEY_YELLOW,
254 [0x22] = KEY_BLUE,
255 [0x23] = KEY_SUBTITLE,
256 [0x24] = KEY_F15, /* AD */
257 [0x25] = KEY_TEXT,
258 [0x26] = KEY_MUTE,
259 [0x27] = KEY_REWIND,
260 [0x28] = KEY_STOP,
261 [0x29] = KEY_PLAY,
262 [0x2a] = KEY_FASTFORWARD,
263 [0x2b] = KEY_F16, /* chapter */
264 [0x2c] = KEY_PAUSE,
265 [0x2d] = KEY_PLAY,
266 [0x2e] = KEY_RECORD,
267 [0x2f] = KEY_F17, /* picture in picture */
268 [0x30] = KEY_KPPLUS, /* zoom in */
269 [0x31] = KEY_KPMINUS, /* zoom out */
270 [0x32] = KEY_F18, /* capture */
271 [0x33] = KEY_F19, /* web */
272 [0x34] = KEY_EMAIL,
273 [0x35] = KEY_PHONE,
274 [0x36] = KEY_PC
275};
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static int debug;
278module_param(debug, int, 0644); /* debug level (0,1,2) */
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800279static int repeat_delay = 500;
280module_param(repeat_delay, int, 0644);
281static int repeat_period = 33;
282module_param(repeat_period, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200284#define DEVNAME "bttv-input"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/* ---------------------------------------------------------------------- */
287
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200288static void ir_handle_key(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200290 struct bttv_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 u32 gpio,data;
292
293 /* read gpio value */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200294 gpio = bttv_gpio_read(&btv->c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (ir->polling) {
296 if (ir->last_gpio == gpio)
297 return;
298 ir->last_gpio = gpio;
299 }
300
301 /* extract data */
302 data = ir_extract_bits(gpio, ir->mask_keycode);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200303 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 gpio, data,
305 ir->polling ? "poll" : "irq",
306 (gpio & ir->mask_keydown) ? " down" : "",
307 (gpio & ir->mask_keyup) ? " up" : "");
308
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200309 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
310 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
311 ir_input_keydown(ir->dev,&ir->ir,data,data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 } else {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200313 ir_input_nokey(ir->dev,&ir->ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200318void bttv_input_irq(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200320 struct bttv_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (!ir->polling)
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200323 ir_handle_key(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200326static void bttv_input_timer(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200328 struct bttv *btv = (struct bttv*)data;
329 struct bttv_ir *ir = btv->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned long timeout;
331
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200332 ir_handle_key(btv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 timeout = jiffies + (ir->polling * HZ / 1000);
334 mod_timer(&ir->timer, timeout);
335}
336
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800337/* ---------------------------------------------------------------*/
338
339static int rc5_remote_gap = 885;
340module_param(rc5_remote_gap, int, 0644);
341static int rc5_key_timeout = 200;
342module_param(rc5_key_timeout, int, 0644);
343
344#define RC5_START(x) (((x)>>12)&3)
345#define RC5_TOGGLE(x) (((x)>>11)&1)
346#define RC5_ADDR(x) (((x)>>6)&31)
347#define RC5_INSTR(x) ((x)&63)
348
349/* decode raw bit pattern to RC5 code */
350static u32 rc5_decode(unsigned int code)
351{
352 unsigned int org_code = code;
353 unsigned int pair;
354 unsigned int rc5 = 0;
355 int i;
356
357 code = (code << 1) | 1;
358 for (i = 0; i < 14; ++i) {
359 pair = code & 0x3;
360 code >>= 2;
361
362 rc5 <<= 1;
363 switch (pair) {
364 case 0:
365 case 2:
366 break;
367 case 1:
368 rc5 |= 1;
369 break;
370 case 3:
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200371 dprintk(KERN_WARNING "bad code: %x\n", org_code);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800372 return 0;
373 }
374 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200375 dprintk(KERN_WARNING "code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800376 "instr=%x\n", rc5, org_code, RC5_START(rc5),
377 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
378 return rc5;
379}
380
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200381static int bttv_rc5_irq(struct bttv *btv)
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800382{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200383 struct bttv_ir *ir = btv->remote;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800384 struct timeval tv;
385 u32 gpio;
386 u32 gap;
387 unsigned long current_jiffies, timeout;
388
389 /* read gpio port */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200390 gpio = bttv_gpio_read(&btv->c);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800391
392 /* remote IRQ? */
393 if (!(gpio & 0x20))
394 return 0;
395
396 /* get time of bit */
397 current_jiffies = jiffies;
398 do_gettimeofday(&tv);
399
400 /* avoid overflow with gap >1s */
401 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
402 gap = 200000;
403 } else {
404 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
405 tv.tv_usec - ir->base_time.tv_usec;
406 }
407
408 /* active code => add bit */
409 if (ir->active) {
410 /* only if in the code (otherwise spurious IRQ or timer
411 late) */
412 if (ir->last_bit < 28) {
413 ir->last_bit = (gap - rc5_remote_gap / 2) /
414 rc5_remote_gap;
415 ir->code |= 1 << ir->last_bit;
416 }
417 /* starting new code */
418 } else {
419 ir->active = 1;
420 ir->code = 0;
421 ir->base_time = tv;
422 ir->last_bit = 0;
423
424 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
425 mod_timer(&ir->timer_end, timeout);
426 }
427
428 /* toggle GPIO pin 4 to reset the irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200429 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
430 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800431 return 1;
432}
433
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200434
435static void bttv_rc5_timer_end(unsigned long data)
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800436{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200437 struct bttv_ir *ir = (struct bttv_ir *)data;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800438 struct timeval tv;
439 unsigned long current_jiffies, timeout;
440 u32 gap;
441
442 /* get time */
443 current_jiffies = jiffies;
444 do_gettimeofday(&tv);
445
446 /* avoid overflow with gap >1s */
447 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
448 gap = 200000;
449 } else {
450 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
451 tv.tv_usec - ir->base_time.tv_usec;
452 }
453
454 /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
455 if (gap < 28000) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200456 dprintk(KERN_WARNING "spurious timer_end\n");
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800457 return;
458 }
459
460 ir->active = 0;
461 if (ir->last_bit < 20) {
462 /* ignore spurious codes (caused by light/other remotes) */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200463 dprintk(KERN_WARNING "short code: %x\n", ir->code);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800464 } else {
465 u32 rc5 = rc5_decode(ir->code);
466
467 /* two start bits? */
468 if (RC5_START(rc5) != 3) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200469 dprintk(KERN_WARNING "rc5 start bits invalid: %u\n", RC5_START(rc5));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800470
471 /* right address? */
472 } else if (RC5_ADDR(rc5) == 0x0) {
473 u32 toggle = RC5_TOGGLE(rc5);
474 u32 instr = RC5_INSTR(rc5);
475
476 /* Good code, decide if repeat/repress */
477 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
478 instr != RC5_INSTR(ir->last_rc5)) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200479 dprintk(KERN_WARNING "instruction %x, toggle %x\n", instr,
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800480 toggle);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200481 ir_input_nokey(ir->dev, &ir->ir);
482 ir_input_keydown(ir->dev, &ir->ir, instr,
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800483 instr);
484 }
485
486 /* Set/reset key-up timer */
487 timeout = current_jiffies + (500 + rc5_key_timeout
488 * HZ) / 1000;
489 mod_timer(&ir->timer_keyup, timeout);
490
491 /* Save code for repeat test */
492 ir->last_rc5 = rc5;
493 }
494 }
495}
496
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200497static void bttv_rc5_timer_keyup(unsigned long data)
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800498{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200499 struct bttv_ir *ir = (struct bttv_ir *)data;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800500
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200501 dprintk(KERN_DEBUG "key released\n");
502 ir_input_nokey(ir->dev, &ir->ir);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/* ---------------------------------------------------------------------- */
506
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200507int bttv_input_init(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200509 struct bttv_ir *ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 IR_KEYTAB_TYPE *ir_codes = NULL;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200511 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int ir_type = IR_TYPE_OTHER;
513
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200514 if (!btv->has_remote)
515 return -ENODEV;
516
517 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500518 input_dev = input_allocate_device();
519 if (!ir || !input_dev) {
520 kfree(ir);
521 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -ENOMEM;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500523 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200524 memset(ir,0,sizeof(*ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* detect & configure */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200527 switch (btv->c.type) {
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800528 case BTTV_BOARD_AVERMEDIA:
529 case BTTV_BOARD_AVPHONE98:
530 case BTTV_BOARD_AVERMEDIA98:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 ir_codes = ir_codes_avermedia;
532 ir->mask_keycode = 0xf88000;
533 ir->mask_keydown = 0x010000;
534 ir->polling = 50; // ms
535 break;
536
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800537 case BTTV_BOARD_AVDVBT_761:
538 case BTTV_BOARD_AVDVBT_771:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 ir_codes = ir_codes_avermedia_dvbt;
540 ir->mask_keycode = 0x0f00c0;
541 ir->mask_keydown = 0x000020;
542 ir->polling = 50; // ms
543 break;
544
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800545 case BTTV_BOARD_PXELVWPLTVPAK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ir_codes = ir_codes_pixelview;
547 ir->mask_keycode = 0x003e00;
548 ir->mask_keyup = 0x010000;
549 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800550 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800551 case BTTV_BOARD_PV_BT878P_9B:
552 case BTTV_BOARD_PV_BT878P_PLUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ir_codes = ir_codes_pixelview;
554 ir->mask_keycode = 0x001f00;
555 ir->mask_keyup = 0x008000;
556 ir->polling = 50; // ms
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800557 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800559 case BTTV_BOARD_WINFAST2000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ir_codes = ir_codes_winfast;
561 ir->mask_keycode = 0x1f8;
562 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800563 case BTTV_BOARD_MAGICTVIEW061:
564 case BTTV_BOARD_MAGICTVIEW063:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ir_codes = ir_codes_winfast;
566 ir->mask_keycode = 0x0008e000;
567 ir->mask_keydown = 0x00200000;
568 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800569 case BTTV_BOARD_APAC_VIEWCOMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ir_codes = ir_codes_apac_viewcomp;
571 ir->mask_keycode = 0x001f00;
572 ir->mask_keyup = 0x008000;
573 ir->polling = 50; // ms
574 break;
Mauro Carvalho Chehab5a25e842005-11-08 21:36:52 -0800575 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
Ricardo Cerqueiracc9d8d42005-11-08 21:36:20 -0800576 ir_codes = ir_codes_conceptronic;
577 ir->mask_keycode = 0x001F00;
578 ir->mask_keyup = 0x006000;
579 ir->polling = 50; // ms
580 break;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800581 case BTTV_BOARD_NEBULA_DIGITV:
582 ir_codes = ir_codes_nebula;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200583 btv->custom_irq = bttv_rc5_irq;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800584 ir->rc5_gpio = 1;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800585 break;
Julian Calaby2d05ae62006-01-11 19:40:09 -0200586 case BTTV_BOARD_MACHTV_MAGICTV:
587 ir_codes = ir_codes_apac_viewcomp;
588 ir->mask_keycode = 0x001F00;
589 ir->mask_keyup = 0x004000;
590 ir->polling = 50; /* ms */
591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 if (NULL == ir_codes) {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200594 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n",btv->c.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 kfree(ir);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500596 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -ENODEV;
598 }
599
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800600 if (ir->rc5_gpio) {
601 u32 gpio;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800602 /* enable remote irq */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200603 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
604 gpio = bttv_gpio_read(&btv->c);
605 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
606 bttv_gpio_write(&btv->c, gpio | (1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800607 } else {
608 /* init hardware-specific stuff */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200609 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /* init input device */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200613 ir->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200615 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
616 btv->c.type);
617 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
618 pci_name(btv->c.pci));
619
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500620 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
621 input_dev->name = ir->name;
622 input_dev->phys = ir->phys;
623 input_dev->id.bustype = BUS_PCI;
624 input_dev->id.version = 1;
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200625 if (btv->c.pci->subsystem_vendor) {
626 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
627 input_dev->id.product = btv->c.pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 } else {
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200629 input_dev->id.vendor = btv->c.pci->vendor;
630 input_dev->id.product = btv->c.pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200632 input_dev->cdev.dev = &btv->c.pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200634 btv->remote = ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (ir->polling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 init_timer(&ir->timer);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200637 ir->timer.function = bttv_input_timer;
638 ir->timer.data = (unsigned long)btv;
639 ir->timer.expires = jiffies + HZ;
640 add_timer(&ir->timer);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800641 } else if (ir->rc5_gpio) {
642 /* set timer_end for code completion */
643 init_timer(&ir->timer_end);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200644 ir->timer_end.function = bttv_rc5_timer_end;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800645 ir->timer_end.data = (unsigned long)ir;
646
647 init_timer(&ir->timer_keyup);
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200648 ir->timer_keyup.function = bttv_rc5_timer_keyup;
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800649 ir->timer_keyup.data = (unsigned long)ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 /* all done */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200653 input_register_device(btv->remote->dev);
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200654 printk(DEVNAME ": %s detected at %s\n",ir->name,ir->phys);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800655
656 /* the remote isn't as bouncy as a keyboard */
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200657 ir->dev->rep[REP_DELAY] = repeat_delay;
658 ir->dev->rep[REP_PERIOD] = repeat_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 return 0;
661}
662
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200663void bttv_input_fini(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200665 if (btv->remote == NULL)
666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200668 if (btv->remote->polling) {
669 del_timer_sync(&btv->remote->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 flush_scheduled_work();
671 }
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800672
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200673
674 if (btv->remote->rc5_gpio) {
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800675 u32 gpio;
676
Andrew Mortonbc61b012006-01-09 15:25:28 -0200677 del_timer_sync(&btv->remote->timer_end);
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800678 flush_scheduled_work();
679
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200680 gpio = bttv_gpio_read(&btv->c);
681 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
Mark Weaver6c6c0b22005-11-13 16:07:52 -0800682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Ricardo Cerqueira4abdfed2006-01-09 15:25:25 -0200684 input_unregister_device(btv->remote->dev);
685 kfree(btv->remote);
686 btv->remote = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690/*
691 * Local variables:
692 * c-basic-offset: 8
693 * End:
694 */