Merge master.kernel.org:/home/rmk/linux-2.6-mmc
[linux-2.6-microblaze.git] / 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
301         u32 rc5_gpio;
302         struct timer_list timer_end;    /* timer_end for code completion */
303         struct timer_list timer_keyup;  /* timer_end for key release */
304         u32 last_rc5;                   /* last good rc5 code */
305         u32 last_bit;                   /* last raw bit seen */
306         u32 code;                       /* raw code under construction */
307         struct timeval base_time;       /* time of last seen code */
308         int active;                     /* building raw code */
309 };
310
311 static int debug;
312 module_param(debug, int, 0644);    /* debug level (0,1,2) */
313 static int repeat_delay = 500;
314 module_param(repeat_delay, int, 0644);
315 static int repeat_period = 33;
316 module_param(repeat_period, int, 0644);
317
318 #define DEVNAME "ir-kbd-gpio"
319 #define dprintk(fmt, arg...)    if (debug) \
320         printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
321
322 static void ir_irq(struct bttv_sub_device *sub);
323 static int ir_probe(struct device *dev);
324 static int ir_remove(struct device *dev);
325
326 static struct bttv_sub_driver driver = {
327         .drv = {
328                 .name   = DEVNAME,
329                 .probe  = ir_probe,
330                 .remove = ir_remove,
331         },
332         .gpio_irq       = ir_irq,
333 };
334
335 /* ---------------------------------------------------------------------- */
336
337 static void ir_handle_key(struct IR *ir)
338 {
339         u32 gpio,data;
340
341         /* read gpio value */
342         gpio = bttv_gpio_read(ir->sub->core);
343         if (ir->polling) {
344                 if (ir->last_gpio == gpio)
345                         return;
346                 ir->last_gpio = gpio;
347         }
348
349         /* extract data */
350         data = ir_extract_bits(gpio, ir->mask_keycode);
351         dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
352                 gpio, data,
353                 ir->polling               ? "poll"  : "irq",
354                 (gpio & ir->mask_keydown) ? " down" : "",
355                 (gpio & ir->mask_keyup)   ? " up"   : "");
356
357         if (ir->mask_keydown) {
358                 /* bit set on keydown */
359                 if (gpio & ir->mask_keydown) {
360                         ir_input_keydown(ir->input, &ir->ir, data, data);
361                 } else {
362                         ir_input_nokey(ir->input, &ir->ir);
363                 }
364
365         } else if (ir->mask_keyup) {
366                 /* bit cleared on keydown */
367                 if (0 == (gpio & ir->mask_keyup)) {
368                         ir_input_keydown(ir->input, &ir->ir, data, data);
369                 } else {
370                         ir_input_nokey(ir->input, &ir->ir);
371                 }
372
373         } else {
374                 /* can't disturgissh keydown/up :-/ */
375                 ir_input_keydown(ir->input, &ir->ir, data, data);
376                 ir_input_nokey(ir->input, &ir->ir);
377         }
378 }
379
380 static void ir_irq(struct bttv_sub_device *sub)
381 {
382         struct IR *ir = dev_get_drvdata(&sub->dev);
383
384         if (!ir->polling)
385                 ir_handle_key(ir);
386 }
387
388 static void ir_timer(unsigned long data)
389 {
390         struct IR *ir = (struct IR*)data;
391
392         schedule_work(&ir->work);
393 }
394
395 static void ir_work(void *data)
396 {
397         struct IR *ir = data;
398         unsigned long timeout;
399
400         ir_handle_key(ir);
401         timeout = jiffies + (ir->polling * HZ / 1000);
402         mod_timer(&ir->timer, timeout);
403 }
404
405 /* ---------------------------------------------------------------*/
406
407 static int rc5_remote_gap = 885;
408 module_param(rc5_remote_gap, int, 0644);
409 static int rc5_key_timeout = 200;
410 module_param(rc5_key_timeout, int, 0644);
411
412 #define RC5_START(x)    (((x)>>12)&3)
413 #define RC5_TOGGLE(x)   (((x)>>11)&1)
414 #define RC5_ADDR(x)     (((x)>>6)&31)
415 #define RC5_INSTR(x)    ((x)&63)
416
417 /* decode raw bit pattern to RC5 code */
418 static u32 rc5_decode(unsigned int code)
419 {
420         unsigned int org_code = code;
421         unsigned int pair;
422         unsigned int rc5 = 0;
423         int i;
424
425         code = (code << 1) | 1;
426         for (i = 0; i < 14; ++i) {
427                 pair = code & 0x3;
428                 code >>= 2;
429
430                 rc5 <<= 1;
431                 switch (pair) {
432                 case 0:
433                 case 2:
434                         break;
435                 case 1:
436                         rc5 |= 1;
437                         break;
438                 case 3:
439                         dprintk("bad code: %x\n", org_code);
440                         return 0;
441                 }
442         }
443         dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
444                 "instr=%x\n", rc5, org_code, RC5_START(rc5),
445                 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
446         return rc5;
447 }
448
449 static int ir_rc5_irq(struct bttv_sub_device *sub)
450 {
451         struct IR *ir = dev_get_drvdata(&sub->dev);
452         struct timeval tv;
453         u32 gpio;
454         u32 gap;
455         unsigned long current_jiffies, timeout;
456
457         /* read gpio port */
458         gpio = bttv_gpio_read(ir->sub->core);
459
460         /* remote IRQ? */
461         if (!(gpio & 0x20))
462                 return 0;
463
464         /* get time of bit */
465         current_jiffies = jiffies;
466         do_gettimeofday(&tv);
467
468         /* avoid overflow with gap >1s */
469         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
470                 gap = 200000;
471         } else {
472                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
473                     tv.tv_usec - ir->base_time.tv_usec;
474         }
475
476         /* active code => add bit */
477         if (ir->active) {
478                 /* only if in the code (otherwise spurious IRQ or timer
479                    late) */
480                 if (ir->last_bit < 28) {
481                         ir->last_bit = (gap - rc5_remote_gap / 2) /
482                             rc5_remote_gap;
483                         ir->code |= 1 << ir->last_bit;
484                 }
485                 /* starting new code */
486         } else {
487                 ir->active = 1;
488                 ir->code = 0;
489                 ir->base_time = tv;
490                 ir->last_bit = 0;
491
492                 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
493                 mod_timer(&ir->timer_end, timeout);
494         }
495
496         /* toggle GPIO pin 4 to reset the irq */
497         bttv_gpio_write(ir->sub->core, gpio & ~(1 << 4));
498         bttv_gpio_write(ir->sub->core, gpio | (1 << 4));
499         return 1;
500 }
501
502 static void ir_rc5_timer_end(unsigned long data)
503 {
504         struct IR *ir = (struct IR *)data;
505         struct timeval tv;
506         unsigned long current_jiffies, timeout;
507         u32 gap;
508
509         /* get time */
510         current_jiffies = jiffies;
511         do_gettimeofday(&tv);
512
513         /* avoid overflow with gap >1s */
514         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
515                 gap = 200000;
516         } else {
517                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
518                     tv.tv_usec - ir->base_time.tv_usec;
519         }
520
521         /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
522         if (gap < 28000) {
523                 dprintk("spurious timer_end\n");
524                 return;
525         }
526
527         ir->active = 0;
528         if (ir->last_bit < 20) {
529                 /* ignore spurious codes (caused by light/other remotes) */
530                 dprintk("short code: %x\n", ir->code);
531         } else {
532                 u32 rc5 = rc5_decode(ir->code);
533
534                 /* two start bits? */
535                 if (RC5_START(rc5) != 3) {
536                         dprintk("rc5 start bits invalid: %u\n", RC5_START(rc5));
537
538                         /* right address? */
539                 } else if (RC5_ADDR(rc5) == 0x0) {
540                         u32 toggle = RC5_TOGGLE(rc5);
541                         u32 instr = RC5_INSTR(rc5);
542
543                         /* Good code, decide if repeat/repress */
544                         if (toggle != RC5_TOGGLE(ir->last_rc5) ||
545                             instr != RC5_INSTR(ir->last_rc5)) {
546                                 dprintk("instruction %x, toggle %x\n", instr,
547                                         toggle);
548                                 ir_input_nokey(ir->input, &ir->ir);
549                                 ir_input_keydown(ir->input, &ir->ir, instr,
550                                                  instr);
551                         }
552
553                         /* Set/reset key-up timer */
554                         timeout = current_jiffies + (500 + rc5_key_timeout
555                                                      * HZ) / 1000;
556                         mod_timer(&ir->timer_keyup, timeout);
557
558                         /* Save code for repeat test */
559                         ir->last_rc5 = rc5;
560                 }
561         }
562 }
563
564 static void ir_rc5_timer_keyup(unsigned long data)
565 {
566         struct IR *ir = (struct IR *)data;
567
568         dprintk("key released\n");
569         ir_input_nokey(ir->input, &ir->ir);
570 }
571
572 /* ---------------------------------------------------------------------- */
573
574 static int ir_probe(struct device *dev)
575 {
576         struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
577         struct IR *ir;
578         struct input_dev *input_dev;
579         IR_KEYTAB_TYPE *ir_codes = NULL;
580         int ir_type = IR_TYPE_OTHER;
581
582         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
583         input_dev = input_allocate_device();
584         if (!ir || !input_dev) {
585                 kfree(ir);
586                 input_free_device(input_dev);
587                 return -ENOMEM;
588         }
589
590         /* detect & configure */
591         switch (sub->core->type) {
592         case BTTV_BOARD_AVERMEDIA:
593         case BTTV_BOARD_AVPHONE98:
594         case BTTV_BOARD_AVERMEDIA98:
595                 ir_codes         = ir_codes_avermedia;
596                 ir->mask_keycode = 0xf88000;
597                 ir->mask_keydown = 0x010000;
598                 ir->polling      = 50; // ms
599                 break;
600
601         case BTTV_BOARD_AVDVBT_761:
602         case BTTV_BOARD_AVDVBT_771:
603                 ir_codes         = ir_codes_avermedia_dvbt;
604                 ir->mask_keycode = 0x0f00c0;
605                 ir->mask_keydown = 0x000020;
606                 ir->polling      = 50; // ms
607                 break;
608
609         case BTTV_BOARD_PXELVWPLTVPAK:
610                 ir_codes         = ir_codes_pixelview;
611                 ir->mask_keycode = 0x003e00;
612                 ir->mask_keyup   = 0x010000;
613                 ir->polling      = 50; // ms
614                 break;
615         case BTTV_BOARD_PV_BT878P_9B:
616         case BTTV_BOARD_PV_BT878P_PLUS:
617                 ir_codes         = ir_codes_pixelview;
618                 ir->mask_keycode = 0x001f00;
619                 ir->mask_keyup   = 0x008000;
620                 ir->polling      = 50; // ms
621                 break;
622
623         case BTTV_BOARD_WINFAST2000:
624                 ir_codes         = ir_codes_winfast;
625                 ir->mask_keycode = 0x1f8;
626                 break;
627         case BTTV_BOARD_MAGICTVIEW061:
628         case BTTV_BOARD_MAGICTVIEW063:
629                 ir_codes         = ir_codes_winfast;
630                 ir->mask_keycode = 0x0008e000;
631                 ir->mask_keydown = 0x00200000;
632                 break;
633         case BTTV_BOARD_APAC_VIEWCOMP:
634                 ir_codes         = ir_codes_apac_viewcomp;
635                 ir->mask_keycode = 0x001f00;
636                 ir->mask_keyup   = 0x008000;
637                 ir->polling      = 50; // ms
638                 break;
639         case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
640                 ir_codes         = ir_codes_conceptronic;
641                 ir->mask_keycode = 0x001F00;
642                 ir->mask_keyup   = 0x006000;
643                 ir->polling      = 50; // ms
644                 break;
645         case BTTV_BOARD_NEBULA_DIGITV:
646                 ir_codes = ir_codes_nebula;
647                 driver.any_irq = ir_rc5_irq;
648                 driver.gpio_irq = NULL;
649                 ir->rc5_gpio = 1;
650                  break;
651         }
652         if (NULL == ir_codes) {
653                 kfree(ir);
654                 input_free_device(input_dev);
655                 return -ENODEV;
656         }
657
658         if (ir->rc5_gpio) {
659                 u32 gpio;
660                 /* enable remote irq */
661                 bttv_gpio_inout(sub->core, (1 << 4), 1 << 4);
662                 gpio = bttv_gpio_read(sub->core);
663                 bttv_gpio_write(sub->core, gpio & ~(1 << 4));
664                 bttv_gpio_write(sub->core, gpio | (1 << 4));
665         } else {
666                 /* init hardware-specific stuff */
667                 bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
668         }
669
670         /* init input device */
671         snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
672                  sub->core->type);
673         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
674                  pci_name(sub->core->pci));
675
676         ir->sub = sub;
677         ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
678         input_dev->name = ir->name;
679         input_dev->phys = ir->phys;
680         input_dev->id.bustype = BUS_PCI;
681         input_dev->id.version = 1;
682         if (sub->core->pci->subsystem_vendor) {
683                 input_dev->id.vendor  = sub->core->pci->subsystem_vendor;
684                 input_dev->id.product = sub->core->pci->subsystem_device;
685         } else {
686                 input_dev->id.vendor  = sub->core->pci->vendor;
687                 input_dev->id.product = sub->core->pci->device;
688         }
689         input_dev->cdev.dev = &sub->core->pci->dev;
690
691         if (ir->polling) {
692                 INIT_WORK(&ir->work, ir_work, ir);
693                 init_timer(&ir->timer);
694                 ir->timer.function = ir_timer;
695                 ir->timer.data     = (unsigned long)ir;
696                 schedule_work(&ir->work);
697         } else if (ir->rc5_gpio) {
698                 /* set timer_end for code completion */
699                 init_timer(&ir->timer_end);
700                 ir->timer_end.function = ir_rc5_timer_end;
701                 ir->timer_end.data = (unsigned long)ir;
702
703                 init_timer(&ir->timer_keyup);
704                 ir->timer_keyup.function = ir_rc5_timer_keyup;
705                 ir->timer_keyup.data = (unsigned long)ir;
706         }
707
708         /* all done */
709         dev_set_drvdata(dev, ir);
710         input_register_device(ir->input);
711         printk(DEVNAME ": %s detected at %s\n",ir->name,ir->phys);
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         if (ir->rc5_gpio) {
729                 u32 gpio;
730
731                 del_timer(&ir->timer_end);
732                 flush_scheduled_work();
733
734                 gpio = bttv_gpio_read(ir->sub->core);
735                 bttv_gpio_write(ir->sub->core, gpio & ~(1 << 4));
736         }
737
738         input_unregister_device(ir->input);
739         kfree(ir);
740         return 0;
741 }
742
743 /* ---------------------------------------------------------------------- */
744
745 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
746 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
747 MODULE_LICENSE("GPL");
748
749 static int ir_init(void)
750 {
751         return bttv_sub_register(&driver, "remote");
752 }
753
754 static void ir_fini(void)
755 {
756         bttv_sub_unregister(&driver);
757 }
758
759 module_init(ir_init);
760 module_exit(ir_fini);
761
762
763 /*
764  * Local variables:
765  * c-basic-offset: 8
766  * End:
767  */