[PATCH] V4L/DVB (3188): Fix compilation failure with gcc 2.95.3.
[safe/jmp/linux-2.6] / drivers / media / video / ir-kbd-gpio.c
1 /*
2  *
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>
27 #include <linux/pci.h>
28
29 #include <media/ir-common.h>
30
31 #include "bttv.h"
32
33 /* ---------------------------------------------------------------------- */
34
35 static IR_KEYTAB_TYPE ir_codes_avermedia[IR_KEYTAB_SIZE] = {
36         [ 34 ] = KEY_KP0,
37         [ 40 ] = KEY_KP1,
38         [ 24 ] = KEY_KP2,
39         [ 56 ] = KEY_KP3,
40         [ 36 ] = KEY_KP4,
41         [ 20 ] = KEY_KP5,
42         [ 52 ] = KEY_KP6,
43         [ 44 ] = KEY_KP7,
44         [ 28 ] = KEY_KP8,
45         [ 60 ] = KEY_KP9,
46
47         [ 48 ] = KEY_EJECTCD,     // Unmarked on my controller
48         [  0 ] = KEY_POWER,
49         [ 18 ] = BTN_LEFT,        // DISPLAY/L
50         [ 50 ] = BTN_RIGHT,       // LOOP/R
51         [ 10 ] = KEY_MUTE,
52         [ 38 ] = KEY_RECORD,
53         [ 22 ] = KEY_PAUSE,
54         [ 54 ] = KEY_STOP,
55         [ 30 ] = KEY_VOLUMEDOWN,
56         [ 62 ] = KEY_VOLUMEUP,
57
58         [ 32 ] = KEY_TUNER,       // TV/FM
59         [ 16 ] = KEY_CD,
60         [  8 ] = KEY_VIDEO,
61         [  4 ] = KEY_AUDIO,
62         [ 12 ] = KEY_ZOOM,        // full screen
63         [  2 ] = KEY_INFO,        // preview
64         [ 42 ] = KEY_SEARCH,      // autoscan
65         [ 26 ] = KEY_STOP,        // freeze
66         [ 58 ] = KEY_RECORD,      // capture
67         [  6 ] = KEY_PLAY,        // unmarked
68         [ 46 ] = KEY_RED,         // unmarked
69         [ 14 ] = KEY_GREEN,       // unmarked
70
71         [ 33 ] = KEY_YELLOW,      // unmarked
72         [ 17 ] = KEY_CHANNELDOWN,
73         [ 49 ] = KEY_CHANNELUP,
74         [  1 ] = KEY_BLUE,        // unmarked
75 };
76
77 /* Matt Jesson <dvb@jesson.eclipse.co.uk */
78 static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt[IR_KEYTAB_SIZE] = {
79         [ 0x28 ] = KEY_KP0,         //'0' / 'enter'
80         [ 0x22 ] = KEY_KP1,         //'1'
81         [ 0x12 ] = KEY_KP2,         //'2' / 'up arrow'
82         [ 0x32 ] = KEY_KP3,         //'3'
83         [ 0x24 ] = KEY_KP4,         //'4' / 'left arrow'
84         [ 0x14 ] = KEY_KP5,         //'5'
85         [ 0x34 ] = KEY_KP6,         //'6' / 'right arrow'
86         [ 0x26 ] = KEY_KP7,         //'7'
87         [ 0x16 ] = KEY_KP8,         //'8' / 'down arrow'
88         [ 0x36 ] = KEY_KP9,         //'9'
89
90         [ 0x20 ] = KEY_LIST,        // 'source'
91         [ 0x10 ] = KEY_TEXT,        // 'teletext'
92         [ 0x00 ] = KEY_POWER,       // 'power'
93         [ 0x04 ] = KEY_AUDIO,       // 'audio'
94         [ 0x06 ] = KEY_ZOOM,        // 'full screen'
95         [ 0x18 ] = KEY_VIDEO,       // 'display'
96         [ 0x38 ] = KEY_SEARCH,      // 'loop'
97         [ 0x08 ] = KEY_INFO,        // 'preview'
98         [ 0x2a ] = KEY_REWIND,      // 'backward <<'
99         [ 0x1a ] = KEY_FASTFORWARD, // 'forward >>'
100         [ 0x3a ] = KEY_RECORD,      // 'capture'
101         [ 0x0a ] = KEY_MUTE,        // 'mute'
102         [ 0x2c ] = KEY_RECORD,      // 'record'
103         [ 0x1c ] = KEY_PAUSE,       // 'pause'
104         [ 0x3c ] = KEY_STOP,        // 'stop'
105         [ 0x0c ] = KEY_PLAY,        // 'play'
106         [ 0x2e ] = KEY_RED,         // 'red'
107         [ 0x01 ] = KEY_BLUE,        // 'blue' / 'cancel'
108         [ 0x0e ] = KEY_YELLOW,      // 'yellow' / 'ok'
109         [ 0x21 ] = KEY_GREEN,       // 'green'
110         [ 0x11 ] = KEY_CHANNELDOWN, // 'channel -'
111         [ 0x31 ] = KEY_CHANNELUP,   // 'channel +'
112         [ 0x1e ] = KEY_VOLUMEDOWN,  // 'volume -'
113         [ 0x3e ] = KEY_VOLUMEUP,    // 'volume +'
114 };
115
116 /* Attila Kondoros <attila.kondoros@chello.hu> */
117 static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
118
119         [  1 ] = KEY_KP1,
120         [  2 ] = KEY_KP2,
121         [  3 ] = KEY_KP3,
122         [  4 ] = KEY_KP4,
123         [  5 ] = KEY_KP5,
124         [  6 ] = KEY_KP6,
125         [  7 ] = KEY_KP7,
126         [  8 ] = KEY_KP8,
127         [  9 ] = KEY_KP9,
128         [  0 ] = KEY_KP0,
129         [ 23 ] = KEY_LAST,        // +100
130         [ 10 ] = KEY_LIST,        // recall
131
132
133         [ 28 ] = KEY_TUNER,       // TV/FM
134         [ 21 ] = KEY_SEARCH,      // scan
135         [ 18 ] = KEY_POWER,       // power
136         [ 31 ] = KEY_VOLUMEDOWN,  // vol up
137         [ 27 ] = KEY_VOLUMEUP,    // vol down
138         [ 30 ] = KEY_CHANNELDOWN, // chn up
139         [ 26 ] = KEY_CHANNELUP,   // chn down
140
141         [ 17 ] = KEY_VIDEO,       // video
142         [ 15 ] = KEY_ZOOM,        // full screen
143         [ 19 ] = KEY_MUTE,        // mute/unmute
144         [ 16 ] = KEY_TEXT,        // min
145
146         [ 13 ] = KEY_STOP,        // freeze
147         [ 14 ] = KEY_RECORD,      // record
148         [ 29 ] = KEY_PLAYPAUSE,   // stop
149         [ 25 ] = KEY_PLAY,        // play
150
151         [ 22 ] = KEY_GOTO,        // osd
152         [ 20 ] = KEY_REFRESH,     // default
153         [ 12 ] = KEY_KPPLUS,      // fine tune >>>>
154         [ 24 ] = KEY_KPMINUS      // fine tune <<<<
155 };
156
157 /* ---------------------------------------------------------------------- */
158
159 /* Ricardo Cerqueira <v4l@cerqueira.org> */
160 /* Weird matching, since the remote has "uncommon" keys */
161
162 static IR_KEYTAB_TYPE ir_codes_conceptronic[IR_KEYTAB_SIZE] = {
163
164         [ 30 ] = KEY_POWER,       // power
165         [ 7  ] = KEY_MEDIA,       // source
166         [ 28 ] = KEY_SEARCH,      // scan
167
168 /* FIXME: duplicate keycodes?
169  *
170  * These four keys seem to share the same GPIO as CH+, CH-, <<< and >>>
171  * The GPIO values are
172  * 6397fb for both "Scan <" and "CH -",
173  * 639ffb for "Scan >" and "CH+",
174  * 6384fb for "Tune <" and "<<<",
175  * 638cfb for "Tune >" and ">>>", regardless of the mask.
176  *
177  *      [ 23 ] = KEY_BACK,        // fm scan <<
178  *      [ 31 ] = KEY_FORWARD,     // fm scan >>
179  *
180  *      [ 4  ] = KEY_LEFT,        // fm tuning <
181  *      [ 12 ] = KEY_RIGHT,       // fm tuning >
182  *
183  * For now, these four keys are disabled. Pressing them will generate
184  * the CH+/CH-/<<</>>> events
185  */
186
187         [ 3  ] = KEY_TUNER,       // TV/FM
188
189         [ 0  ] = KEY_RECORD,
190         [ 8  ] = KEY_STOP,
191         [ 17 ] = KEY_PLAY,
192
193         [ 26 ] = KEY_PLAYPAUSE,   // freeze
194         [ 25 ] = KEY_ZOOM,        // zoom
195         [ 15 ] = KEY_TEXT,        // min
196
197         [ 1  ] = KEY_KP1,
198         [ 11 ] = KEY_KP2,
199         [ 27 ] = KEY_KP3,
200         [ 5  ] = KEY_KP4,
201         [ 9  ] = KEY_KP5,
202         [ 21 ] = KEY_KP6,
203         [ 6  ] = KEY_KP7,
204         [ 10 ] = KEY_KP8,
205         [ 18 ] = KEY_KP9,
206         [ 2  ] = KEY_KP0,
207         [ 16 ] = KEY_LAST,        // +100
208         [ 19 ] = KEY_LIST,        // recall
209
210         [ 31 ] = KEY_CHANNELUP,   // chn down
211         [ 23 ] = KEY_CHANNELDOWN, // chn up
212         [ 22 ] = KEY_VOLUMEUP,    // vol down
213         [ 20 ] = KEY_VOLUMEDOWN,  // vol up
214
215         [ 4  ] = KEY_KPMINUS,     // <<<
216         [ 14 ] = KEY_SETUP,       // function
217         [ 12 ] = KEY_KPPLUS,      // >>>
218
219         [ 13 ] = KEY_GOTO,        // mts
220         [ 29 ] = KEY_REFRESH,     // reset
221         [ 24 ] = KEY_MUTE         // mute/unmute
222 };
223
224 static IR_KEYTAB_TYPE ir_codes_nebula[IR_KEYTAB_SIZE] = {
225         [0x00] = KEY_KP0,
226         [0x01] = KEY_KP1,
227         [0x02] = KEY_KP2,
228         [0x03] = KEY_KP3,
229         [0x04] = KEY_KP4,
230         [0x05] = KEY_KP5,
231         [0x06] = KEY_KP6,
232         [0x07] = KEY_KP7,
233         [0x08] = KEY_KP8,
234         [0x09] = KEY_KP9,
235         [0x0a] = KEY_TV,
236         [0x0b] = KEY_AUX,
237         [0x0c] = KEY_DVD,
238         [0x0d] = KEY_POWER,
239         [0x0e] = KEY_MHP,       /* labelled 'Picture' */
240         [0x0f] = KEY_AUDIO,
241         [0x10] = KEY_INFO,
242         [0x11] = KEY_F13,       /* 16:9 */
243         [0x12] = KEY_F14,       /* 14:9 */
244         [0x13] = KEY_EPG,
245         [0x14] = KEY_EXIT,
246         [0x15] = KEY_MENU,
247         [0x16] = KEY_UP,
248         [0x17] = KEY_DOWN,
249         [0x18] = KEY_LEFT,
250         [0x19] = KEY_RIGHT,
251         [0x1a] = KEY_ENTER,
252         [0x1b] = KEY_CHANNELUP,
253         [0x1c] = KEY_CHANNELDOWN,
254         [0x1d] = KEY_VOLUMEUP,
255         [0x1e] = KEY_VOLUMEDOWN,
256         [0x1f] = KEY_RED,
257         [0x20] = KEY_GREEN,
258         [0x21] = KEY_YELLOW,
259         [0x22] = KEY_BLUE,
260         [0x23] = KEY_SUBTITLE,
261         [0x24] = KEY_F15,       /* AD */
262         [0x25] = KEY_TEXT,
263         [0x26] = KEY_MUTE,
264         [0x27] = KEY_REWIND,
265         [0x28] = KEY_STOP,
266         [0x29] = KEY_PLAY,
267         [0x2a] = KEY_FASTFORWARD,
268         [0x2b] = KEY_F16,       /* chapter */
269         [0x2c] = KEY_PAUSE,
270         [0x2d] = KEY_PLAY,
271         [0x2e] = KEY_RECORD,
272         [0x2f] = KEY_F17,       /* picture in picture */
273         [0x30] = KEY_KPPLUS,    /* zoom in */
274         [0x31] = KEY_KPMINUS,   /* zoom out */
275         [0x32] = KEY_F18,       /* capture */
276         [0x33] = KEY_F19,       /* web */
277         [0x34] = KEY_EMAIL,
278         [0x35] = KEY_PHONE,
279         [0x36] = KEY_PC
280 };
281
282 struct IR {
283         struct bttv_sub_device  *sub;
284         struct input_dev        *input;
285         struct ir_input_state   ir;
286         char                    name[32];
287         char                    phys[32];
288
289         /* Usual gpio signalling */
290
291         u32                     mask_keycode;
292         u32                     mask_keydown;
293         u32                     mask_keyup;
294         u32                     polling;
295         u32                     last_gpio;
296         struct work_struct      work;
297         struct timer_list       timer;
298
299         /* RC5 gpio */
300         u32 rc5_gpio;
301         struct timer_list timer_end;    /* timer_end for code completion */
302         struct timer_list timer_keyup;  /* timer_end for key release */
303         u32 last_rc5;                   /* last good rc5 code */
304         u32 last_bit;                   /* last raw bit seen */
305         u32 code;                       /* raw code under construction */
306         struct timeval base_time;       /* time of last seen code */
307         int active;                     /* building raw code */
308 };
309
310 static int debug;
311 module_param(debug, int, 0644);    /* debug level (0,1,2) */
312 static int repeat_delay = 500;
313 module_param(repeat_delay, int, 0644);
314 static int repeat_period = 33;
315 module_param(repeat_period, int, 0644);
316
317 #define DEVNAME "ir-kbd-gpio"
318 #define dprintk(fmt, arg...)    if (debug) \
319         printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
320
321 static void ir_irq(struct bttv_sub_device *sub);
322 static int ir_probe(struct device *dev);
323 static int ir_remove(struct device *dev);
324
325 static struct bttv_sub_driver driver = {
326         .drv = {
327                 .name   = DEVNAME,
328                 .probe  = ir_probe,
329                 .remove = ir_remove,
330         },
331         .gpio_irq       = ir_irq,
332 };
333
334 /* ---------------------------------------------------------------------- */
335
336 static void ir_handle_key(struct IR *ir)
337 {
338         u32 gpio,data;
339
340         /* read gpio value */
341         gpio = bttv_gpio_read(ir->sub->core);
342         if (ir->polling) {
343                 if (ir->last_gpio == gpio)
344                         return;
345                 ir->last_gpio = gpio;
346         }
347
348         /* extract data */
349         data = ir_extract_bits(gpio, ir->mask_keycode);
350         dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
351                 gpio, data,
352                 ir->polling               ? "poll"  : "irq",
353                 (gpio & ir->mask_keydown) ? " down" : "",
354                 (gpio & ir->mask_keyup)   ? " up"   : "");
355
356         if (ir->mask_keydown) {
357                 /* bit set on keydown */
358                 if (gpio & ir->mask_keydown) {
359                         ir_input_keydown(ir->input, &ir->ir, data, data);
360                 } else {
361                         ir_input_nokey(ir->input, &ir->ir);
362                 }
363
364         } else if (ir->mask_keyup) {
365                 /* bit cleared on keydown */
366                 if (0 == (gpio & ir->mask_keyup)) {
367                         ir_input_keydown(ir->input, &ir->ir, data, data);
368                 } else {
369                         ir_input_nokey(ir->input, &ir->ir);
370                 }
371
372         } else {
373                 /* can't disturgissh keydown/up :-/ */
374                 ir_input_keydown(ir->input, &ir->ir, data, data);
375                 ir_input_nokey(ir->input, &ir->ir);
376         }
377 }
378
379 static void ir_irq(struct bttv_sub_device *sub)
380 {
381         struct IR *ir = dev_get_drvdata(&sub->dev);
382
383         if (!ir->polling)
384                 ir_handle_key(ir);
385 }
386
387 static void ir_timer(unsigned long data)
388 {
389         struct IR *ir = (struct IR*)data;
390
391         schedule_work(&ir->work);
392 }
393
394 static void ir_work(void *data)
395 {
396         struct IR *ir = data;
397         unsigned long timeout;
398
399         ir_handle_key(ir);
400         timeout = jiffies + (ir->polling * HZ / 1000);
401         mod_timer(&ir->timer, timeout);
402 }
403
404 /* ---------------------------------------------------------------*/
405
406 static int rc5_remote_gap = 885;
407 module_param(rc5_remote_gap, int, 0644);
408 static int rc5_key_timeout = 200;
409 module_param(rc5_key_timeout, int, 0644);
410
411 #define RC5_START(x)    (((x)>>12)&3)
412 #define RC5_TOGGLE(x)   (((x)>>11)&1)
413 #define RC5_ADDR(x)     (((x)>>6)&31)
414 #define RC5_INSTR(x)    ((x)&63)
415
416 /* decode raw bit pattern to RC5 code */
417 static u32 rc5_decode(unsigned int code)
418 {
419         unsigned int org_code = code;
420         unsigned int pair;
421         unsigned int rc5 = 0;
422         int i;
423
424         code = (code << 1) | 1;
425         for (i = 0; i < 14; ++i) {
426                 pair = code & 0x3;
427                 code >>= 2;
428
429                 rc5 <<= 1;
430                 switch (pair) {
431                 case 0:
432                 case 2:
433                         break;
434                 case 1:
435                         rc5 |= 1;
436                         break;
437                 case 3:
438                         dprintk("bad code: %x\n", org_code);
439                         return 0;
440                 }
441         }
442         dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
443                 "instr=%x\n", rc5, org_code, RC5_START(rc5),
444                 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
445         return rc5;
446 }
447
448 static int ir_rc5_irq(struct bttv_sub_device *sub)
449 {
450         struct IR *ir = dev_get_drvdata(&sub->dev);
451         struct timeval tv;
452         u32 gpio;
453         u32 gap;
454         unsigned long current_jiffies, timeout;
455
456         /* read gpio port */
457         gpio = bttv_gpio_read(ir->sub->core);
458
459         /* remote IRQ? */
460         if (!(gpio & 0x20))
461                 return 0;
462
463         /* get time of bit */
464         current_jiffies = jiffies;
465         do_gettimeofday(&tv);
466
467         /* avoid overflow with gap >1s */
468         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
469                 gap = 200000;
470         } else {
471                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
472                     tv.tv_usec - ir->base_time.tv_usec;
473         }
474
475         /* active code => add bit */
476         if (ir->active) {
477                 /* only if in the code (otherwise spurious IRQ or timer
478                    late) */
479                 if (ir->last_bit < 28) {
480                         ir->last_bit = (gap - rc5_remote_gap / 2) /
481                             rc5_remote_gap;
482                         ir->code |= 1 << ir->last_bit;
483                 }
484                 /* starting new code */
485         } else {
486                 ir->active = 1;
487                 ir->code = 0;
488                 ir->base_time = tv;
489                 ir->last_bit = 0;
490
491                 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
492                 mod_timer(&ir->timer_end, timeout);
493         }
494
495         /* toggle GPIO pin 4 to reset the irq */
496         bttv_gpio_write(ir->sub->core, gpio & ~(1 << 4));
497         bttv_gpio_write(ir->sub->core, gpio | (1 << 4));
498         return 1;
499 }
500
501 static void ir_rc5_timer_end(unsigned long data)
502 {
503         struct IR *ir = (struct IR *)data;
504         struct timeval tv;
505         unsigned long current_jiffies, timeout;
506         u32 gap;
507
508         /* get time */
509         current_jiffies = jiffies;
510         do_gettimeofday(&tv);
511
512         /* avoid overflow with gap >1s */
513         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
514                 gap = 200000;
515         } else {
516                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
517                     tv.tv_usec - ir->base_time.tv_usec;
518         }
519
520         /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
521         if (gap < 28000) {
522                 dprintk("spurious timer_end\n");
523                 return;
524         }
525
526         ir->active = 0;
527         if (ir->last_bit < 20) {
528                 /* ignore spurious codes (caused by light/other remotes) */
529                 dprintk("short code: %x\n", ir->code);
530         } else {
531                 u32 rc5 = rc5_decode(ir->code);
532
533                 /* two start bits? */
534                 if (RC5_START(rc5) != 3) {
535                         dprintk("rc5 start bits invalid: %u\n", RC5_START(rc5));
536
537                         /* right address? */
538                 } else if (RC5_ADDR(rc5) == 0x0) {
539                         u32 toggle = RC5_TOGGLE(rc5);
540                         u32 instr = RC5_INSTR(rc5);
541
542                         /* Good code, decide if repeat/repress */
543                         if (toggle != RC5_TOGGLE(ir->last_rc5) ||
544                             instr != RC5_INSTR(ir->last_rc5)) {
545                                 dprintk("instruction %x, toggle %x\n", instr,
546                                         toggle);
547                                 ir_input_nokey(ir->input, &ir->ir);
548                                 ir_input_keydown(ir->input, &ir->ir, instr,
549                                                  instr);
550                         }
551
552                         /* Set/reset key-up timer */
553                         timeout = current_jiffies + (500 + rc5_key_timeout
554                                                      * HZ) / 1000;
555                         mod_timer(&ir->timer_keyup, timeout);
556
557                         /* Save code for repeat test */
558                         ir->last_rc5 = rc5;
559                 }
560         }
561 }
562
563 static void ir_rc5_timer_keyup(unsigned long data)
564 {
565         struct IR *ir = (struct IR *)data;
566
567         dprintk("key released\n");
568         ir_input_nokey(ir->input, &ir->ir);
569 }
570
571 /* ---------------------------------------------------------------------- */
572
573 static int ir_probe(struct device *dev)
574 {
575         struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
576         struct IR *ir;
577         struct input_dev *input_dev;
578         IR_KEYTAB_TYPE *ir_codes = NULL;
579         int ir_type = IR_TYPE_OTHER;
580
581         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
582         input_dev = input_allocate_device();
583         if (!ir || !input_dev) {
584                 kfree(ir);
585                 input_free_device(input_dev);
586                 return -ENOMEM;
587         }
588
589         /* detect & configure */
590         switch (sub->core->type) {
591         case BTTV_BOARD_AVERMEDIA:
592         case BTTV_BOARD_AVPHONE98:
593         case BTTV_BOARD_AVERMEDIA98:
594                 ir_codes         = ir_codes_avermedia;
595                 ir->mask_keycode = 0xf88000;
596                 ir->mask_keydown = 0x010000;
597                 ir->polling      = 50; // ms
598                 break;
599
600         case BTTV_BOARD_AVDVBT_761:
601         case BTTV_BOARD_AVDVBT_771:
602                 ir_codes         = ir_codes_avermedia_dvbt;
603                 ir->mask_keycode = 0x0f00c0;
604                 ir->mask_keydown = 0x000020;
605                 ir->polling      = 50; // ms
606                 break;
607
608         case BTTV_BOARD_PXELVWPLTVPAK:
609                 ir_codes         = ir_codes_pixelview;
610                 ir->mask_keycode = 0x003e00;
611                 ir->mask_keyup   = 0x010000;
612                 ir->polling      = 50; // ms
613                 break;
614         case BTTV_BOARD_PV_BT878P_9B:
615         case BTTV_BOARD_PV_BT878P_PLUS:
616                 ir_codes         = ir_codes_pixelview;
617                 ir->mask_keycode = 0x001f00;
618                 ir->mask_keyup   = 0x008000;
619                 ir->polling      = 50; // ms
620                 break;
621
622         case BTTV_BOARD_WINFAST2000:
623                 ir_codes         = ir_codes_winfast;
624                 ir->mask_keycode = 0x1f8;
625                 break;
626         case BTTV_BOARD_MAGICTVIEW061:
627         case BTTV_BOARD_MAGICTVIEW063:
628                 ir_codes         = ir_codes_winfast;
629                 ir->mask_keycode = 0x0008e000;
630                 ir->mask_keydown = 0x00200000;
631                 break;
632         case BTTV_BOARD_APAC_VIEWCOMP:
633                 ir_codes         = ir_codes_apac_viewcomp;
634                 ir->mask_keycode = 0x001f00;
635                 ir->mask_keyup   = 0x008000;
636                 ir->polling      = 50; // ms
637                 break;
638         case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
639                 ir_codes         = ir_codes_conceptronic;
640                 ir->mask_keycode = 0x001F00;
641                 ir->mask_keyup   = 0x006000;
642                 ir->polling      = 50; // ms
643                 break;
644         case BTTV_BOARD_NEBULA_DIGITV:
645                 ir_codes = ir_codes_nebula;
646                 driver.any_irq = ir_rc5_irq;
647                 driver.gpio_irq = NULL;
648                 ir->rc5_gpio = 1;
649                 break;
650         }
651         if (NULL == ir_codes) {
652                 kfree(ir);
653                 input_free_device(input_dev);
654                 return -ENODEV;
655         }
656
657         if (ir->rc5_gpio) {
658                 u32 gpio;
659                 /* enable remote irq */
660                 bttv_gpio_inout(sub->core, (1 << 4), 1 << 4);
661                 gpio = bttv_gpio_read(sub->core);
662                 bttv_gpio_write(sub->core, gpio & ~(1 << 4));
663                 bttv_gpio_write(sub->core, gpio | (1 << 4));
664         } else {
665                 /* init hardware-specific stuff */
666                 bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
667         }
668
669         /* init input device */
670         snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
671                  sub->core->type);
672         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
673                  pci_name(sub->core->pci));
674
675         ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
676         input_dev->name = ir->name;
677         input_dev->phys = ir->phys;
678         input_dev->id.bustype = BUS_PCI;
679         input_dev->id.version = 1;
680         if (sub->core->pci->subsystem_vendor) {
681                 input_dev->id.vendor  = sub->core->pci->subsystem_vendor;
682                 input_dev->id.product = sub->core->pci->subsystem_device;
683         } else {
684                 input_dev->id.vendor  = sub->core->pci->vendor;
685                 input_dev->id.product = sub->core->pci->device;
686         }
687         input_dev->cdev.dev = &sub->core->pci->dev;
688
689         ir->input = input_dev;
690         ir->sub = sub;
691
692         if (ir->polling) {
693                 INIT_WORK(&ir->work, ir_work, ir);
694                 init_timer(&ir->timer);
695                 ir->timer.function = ir_timer;
696                 ir->timer.data     = (unsigned long)ir;
697                 schedule_work(&ir->work);
698         } else if (ir->rc5_gpio) {
699                 /* set timer_end for code completion */
700                 init_timer(&ir->timer_end);
701                 ir->timer_end.function = ir_rc5_timer_end;
702                 ir->timer_end.data = (unsigned long)ir;
703
704                 init_timer(&ir->timer_keyup);
705                 ir->timer_keyup.function = ir_rc5_timer_keyup;
706                 ir->timer_keyup.data = (unsigned long)ir;
707         }
708
709         /* all done */
710         dev_set_drvdata(dev, ir);
711         input_register_device(ir->input);
712
713         /* the remote isn't as bouncy as a keyboard */
714         ir->input->rep[REP_DELAY] = repeat_delay;
715         ir->input->rep[REP_PERIOD] = repeat_period;
716
717         return 0;
718 }
719
720 static int ir_remove(struct device *dev)
721 {
722         struct IR *ir = dev_get_drvdata(dev);
723
724         if (ir->polling) {
725                 del_timer(&ir->timer);
726                 flush_scheduled_work();
727         }
728
729         if (ir->rc5_gpio) {
730                 u32 gpio;
731
732                 del_timer(&ir->timer_end);
733                 flush_scheduled_work();
734
735                 gpio = bttv_gpio_read(ir->sub->core);
736                 bttv_gpio_write(ir->sub->core, gpio & ~(1 << 4));
737         }
738
739         input_unregister_device(ir->input);
740         kfree(ir);
741         return 0;
742 }
743
744 /* ---------------------------------------------------------------------- */
745
746 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
747 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
748 MODULE_LICENSE("GPL");
749
750 static int ir_init(void)
751 {
752         return bttv_sub_register(&driver, "remote");
753 }
754
755 static void ir_fini(void)
756 {
757         bttv_sub_unregister(&driver);
758 }
759
760 module_init(ir_init);
761 module_exit(ir_fini);
762
763
764 /*
765  * Local variables:
766  * c-basic-offset: 8
767  * End:
768  */