c1fe7c9369b2881ac52f922a89c88d8c143b93a1
[safe/jmp/linux-2.6] / drivers / media / video / saa7134 / saa7134-input.c
1 /*
2  *
3  * handle saa7134 IR remotes via linux kernel input layer.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/input.h>
26 #include <linux/slab.h>
27
28 #include "saa7134-reg.h"
29 #include "saa7134.h"
30
31 #define MODULE_NAME "saa7134"
32
33 static unsigned int disable_ir;
34 module_param(disable_ir, int, 0444);
35 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
36
37 static unsigned int ir_debug;
38 module_param(ir_debug, int, 0644);
39 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
40
41 static int pinnacle_remote;
42 module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
43 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
44
45 static int ir_rc5_remote_gap = 885;
46 module_param(ir_rc5_remote_gap, int, 0644);
47 static int ir_rc5_key_timeout = 115;
48 module_param(ir_rc5_key_timeout, int, 0644);
49
50 static int repeat_delay = 500;
51 module_param(repeat_delay, int, 0644);
52 MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
53 static int repeat_period = 33;
54 module_param(repeat_period, int, 0644);
55 MODULE_PARM_DESC(repeat_period, "repeat period between "
56     "keypresses when key is down");
57
58 static unsigned int disable_other_ir;
59 module_param(disable_other_ir, int, 0644);
60 MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
61     "alternative remotes from other manufacturers");
62
63 #define dprintk(fmt, arg...)    if (ir_debug) \
64         printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
65 #define i2cdprintk(fmt, arg...)    if (ir_debug) \
66         printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
67
68 /* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
69 static int saa7134_rc5_irq(struct saa7134_dev *dev);
70 static int saa7134_nec_irq(struct saa7134_dev *dev);
71 static void nec_task(unsigned long data);
72 static void saa7134_nec_timer(unsigned long data);
73
74 /* -------------------- GPIO generic keycode builder -------------------- */
75
76 static int build_key(struct saa7134_dev *dev)
77 {
78         struct card_ir *ir = dev->remote;
79         u32 gpio, data;
80
81         /* here comes the additional handshake steps for some cards */
82         switch (dev->board) {
83         case SAA7134_BOARD_GOTVIEW_7135:
84                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
85                 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
86                 break;
87         }
88         /* rising SAA7134_GPIO_GPRESCAN reads the status */
89         saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
90         saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
91
92         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
93         if (ir->polling) {
94                 if (ir->last_gpio == gpio)
95                         return 0;
96                 ir->last_gpio = gpio;
97         }
98
99         data = ir_extract_bits(gpio, ir->mask_keycode);
100         dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
101                 gpio, ir->mask_keycode, data);
102
103         switch (dev->board) {
104         case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
105                 if (data == ir->mask_keycode)
106                         ir_input_nokey(ir->dev, &ir->ir);
107                 else
108                         ir_input_keydown(ir->dev, &ir->ir, data);
109                 return 0;
110         }
111
112         if (ir->polling) {
113                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
114                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
115                         ir_input_keydown(ir->dev, &ir->ir, data);
116                 } else {
117                         ir_input_nokey(ir->dev, &ir->ir);
118                 }
119         }
120         else {  /* IRQ driven mode - handle key press and release in one go */
121                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
122                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
123                         ir_input_keydown(ir->dev, &ir->ir, data);
124                         ir_input_nokey(ir->dev, &ir->ir);
125                 }
126         }
127
128         return 0;
129 }
130
131 /* --------------------- Chip specific I2C key builders ----------------- */
132
133 static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
134 {
135         int gpio;
136         int attempt = 0;
137         unsigned char b;
138
139         /* We need this to access GPI Used by the saa_readl macro. */
140         struct saa7134_dev *dev = ir->c->adapter->algo_data;
141
142         if (dev == NULL) {
143                 dprintk("get_key_flydvb_trio: "
144                          "gir->c->adapter->algo_data is NULL!\n");
145                 return -EIO;
146         }
147
148         /* rising SAA7134_GPIGPRESCAN reads the status */
149         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
150         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
151
152         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
153
154         if (0x40000 & ~gpio)
155                 return 0; /* No button press */
156
157         /* No button press - only before first key pressed */
158         if (b == 0xFF)
159                 return 0;
160
161         /* poll IR chip */
162         /* weak up the IR chip */
163         b = 0;
164
165         while (1 != i2c_master_send(ir->c, &b, 1)) {
166                 if ((attempt++) < 10) {
167                         /*
168                          * wait a bit for next attempt -
169                          * I don't know how make it better
170                          */
171                         msleep(10);
172                         continue;
173                 }
174                 i2cdprintk("send wake up byte to pic16C505 (IR chip)"
175                            "failed %dx\n", attempt);
176                 return -EIO;
177         }
178         if (1 != i2c_master_recv(ir->c, &b, 1)) {
179                 i2cdprintk("read error\n");
180                 return -EIO;
181         }
182
183         *ir_key = b;
184         *ir_raw = b;
185         return 1;
186 }
187
188 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
189                                        u32 *ir_raw)
190 {
191         unsigned char b;
192         int gpio;
193
194         /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
195         struct saa7134_dev *dev = ir->c->adapter->algo_data;
196         if (dev == NULL) {
197                 dprintk("get_key_msi_tvanywhere_plus: "
198                         "gir->c->adapter->algo_data is NULL!\n");
199                 return -EIO;
200         }
201
202         /* rising SAA7134_GPIO_GPRESCAN reads the status */
203
204         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
205         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
206
207         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
208
209         /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
210            I2C receive if gpio&0x40 is not low. */
211
212         if (gpio & 0x40)
213                 return 0;       /* No button press */
214
215         /* GPIO says there is a button press. Get it. */
216
217         if (1 != i2c_master_recv(ir->c, &b, 1)) {
218                 i2cdprintk("read error\n");
219                 return -EIO;
220         }
221
222         /* No button press */
223
224         if (b == 0xff)
225                 return 0;
226
227         /* Button pressed */
228
229         dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
230         *ir_key = b;
231         *ir_raw = b;
232         return 1;
233 }
234
235 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
236 {
237         unsigned char b;
238
239         /* poll IR chip */
240         if (1 != i2c_master_recv(ir->c, &b, 1)) {
241                 i2cdprintk("read error\n");
242                 return -EIO;
243         }
244
245         /* no button press */
246         if (b==0)
247                 return 0;
248
249         /* repeating */
250         if (b & 0x80)
251                 return 1;
252
253         *ir_key = b;
254         *ir_raw = b;
255         return 1;
256 }
257
258 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
259 {
260         unsigned char buf[5], cod4, code3, code4;
261
262         /* poll IR chip */
263         if (5 != i2c_master_recv(ir->c, buf, 5))
264                 return -EIO;
265
266         cod4    = buf[4];
267         code4   = (cod4 >> 2);
268         code3   = buf[3];
269         if (code3 == 0)
270                 /* no key pressed */
271                 return 0;
272
273         /* return key */
274         *ir_key = code4;
275         *ir_raw = code4;
276         return 1;
277 }
278
279
280 static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
281 {
282         unsigned char data[12];
283         u32 gpio;
284
285         struct saa7134_dev *dev = ir->c->adapter->algo_data;
286
287         /* rising SAA7134_GPIO_GPRESCAN reads the status */
288         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
289         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
290
291         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
292
293         if (0x400000 & ~gpio)
294                 return 0; /* No button press */
295
296         ir->c->addr = 0x5a >> 1;
297
298         if (12 != i2c_master_recv(ir->c, data, 12)) {
299                 i2cdprintk("read error\n");
300                 return -EIO;
301         }
302         /* IR of this card normally decode signals NEC-standard from
303          * - Sven IHOO MT 5.1R remote. xxyye718
304          * - Sven DVD HD-10xx remote. xxyyf708
305          * - BBK ...
306          * - mayby others
307          * So, skip not our, if disable full codes mode.
308          */
309         if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
310                 return 0;
311
312         /* Wrong data decode fix */
313         if (data[9] != (unsigned char)(~data[8]))
314                 return 0;
315
316         *ir_key = data[9];
317         *ir_raw = data[9];
318
319         return 1;
320 }
321
322 /* Common (grey or coloured) pinnacle PCTV remote handling
323  *
324  */
325 static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
326                             int parity_offset, int marker, int code_modulo)
327 {
328         unsigned char b[4];
329         unsigned int start = 0,parity = 0,code = 0;
330
331         /* poll IR chip */
332         if (4 != i2c_master_recv(ir->c, b, 4)) {
333                 i2cdprintk("read error\n");
334                 return -EIO;
335         }
336
337         for (start = 0; start < ARRAY_SIZE(b); start++) {
338                 if (b[start] == marker) {
339                         code=b[(start+parity_offset + 1) % 4];
340                         parity=b[(start+parity_offset) % 4];
341                 }
342         }
343
344         /* Empty Request */
345         if (parity == 0)
346                 return 0;
347
348         /* Repeating... */
349         if (ir->old == parity)
350                 return 0;
351
352         ir->old = parity;
353
354         /* drop special codes when a key is held down a long time for the grey controller
355            In this case, the second bit of the code is asserted */
356         if (marker == 0xfe && (code & 0x40))
357                 return 0;
358
359         code %= code_modulo;
360
361         *ir_raw = code;
362         *ir_key = code;
363
364         i2cdprintk("Pinnacle PCTV key %02x\n", code);
365
366         return 1;
367 }
368
369 /* The grey pinnacle PCTV remote
370  *
371  *  There are one issue with this remote:
372  *   - I2c packet does not change when the same key is pressed quickly. The workaround
373  *     is to hold down each key for about half a second, so that another code is generated
374  *     in the i2c packet, and the function can distinguish key presses.
375  *
376  * Sylvain Pasche <sylvain.pasche@gmail.com>
377  */
378 static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
379 {
380
381         return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
382 }
383
384
385 /* The new pinnacle PCTV remote (with the colored buttons)
386  *
387  * Ricardo Cerqueira <v4l@cerqueira.org>
388  */
389 static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
390 {
391         /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
392          *
393          * this is the only value that results in 42 unique
394          * codes < 128
395          */
396
397         return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
398 }
399
400 void saa7134_input_irq(struct saa7134_dev *dev)
401 {
402         struct card_ir *ir = dev->remote;
403
404         if (ir->nec_gpio) {
405                 saa7134_nec_irq(dev);
406         } else if (!ir->polling && !ir->rc5_gpio) {
407                 build_key(dev);
408         } else if (ir->rc5_gpio) {
409                 saa7134_rc5_irq(dev);
410         }
411 }
412
413 static void saa7134_input_timer(unsigned long data)
414 {
415         struct saa7134_dev *dev = (struct saa7134_dev *)data;
416         struct card_ir *ir = dev->remote;
417
418         build_key(dev);
419         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
420 }
421
422 void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
423 {
424         if (ir->polling) {
425                 setup_timer(&ir->timer, saa7134_input_timer,
426                             (unsigned long)dev);
427                 ir->timer.expires  = jiffies + HZ;
428                 add_timer(&ir->timer);
429         } else if (ir->rc5_gpio) {
430                 /* set timer_end for code completion */
431                 init_timer(&ir->timer_end);
432                 ir->timer_end.function = ir_rc5_timer_end;
433                 ir->timer_end.data = (unsigned long)ir;
434                 init_timer(&ir->timer_keyup);
435                 ir->timer_keyup.function = ir_rc5_timer_keyup;
436                 ir->timer_keyup.data = (unsigned long)ir;
437                 ir->shift_by = 2;
438                 ir->start = 0x2;
439                 ir->addr = 0x17;
440                 ir->rc5_key_timeout = ir_rc5_key_timeout;
441                 ir->rc5_remote_gap = ir_rc5_remote_gap;
442         } else if (ir->nec_gpio) {
443                 setup_timer(&ir->timer_keyup, saa7134_nec_timer,
444                             (unsigned long)dev);
445                 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
446         }
447 }
448
449 void saa7134_ir_stop(struct saa7134_dev *dev)
450 {
451         if (dev->remote->polling)
452                 del_timer_sync(&dev->remote->timer);
453 }
454
455 int saa7134_input_init1(struct saa7134_dev *dev)
456 {
457         struct card_ir *ir;
458         struct input_dev *input_dev;
459         struct ir_scancode_table *ir_codes = NULL;
460         u32 mask_keycode = 0;
461         u32 mask_keydown = 0;
462         u32 mask_keyup   = 0;
463         int polling      = 0;
464         int rc5_gpio     = 0;
465         int nec_gpio     = 0;
466         u64 ir_type = IR_TYPE_OTHER;
467         int err;
468
469         if (dev->has_remote != SAA7134_REMOTE_GPIO)
470                 return -ENODEV;
471         if (disable_ir)
472                 return -ENODEV;
473
474         /* detect & configure */
475         switch (dev->board) {
476         case SAA7134_BOARD_FLYVIDEO2000:
477         case SAA7134_BOARD_FLYVIDEO3000:
478         case SAA7134_BOARD_FLYTVPLATINUM_FM:
479         case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
480         case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
481                 ir_codes     = &ir_codes_flyvideo_table;
482                 mask_keycode = 0xEC00000;
483                 mask_keydown = 0x0040000;
484                 break;
485         case SAA7134_BOARD_CINERGY400:
486         case SAA7134_BOARD_CINERGY600:
487         case SAA7134_BOARD_CINERGY600_MK3:
488                 ir_codes     = &ir_codes_cinergy_table;
489                 mask_keycode = 0x00003f;
490                 mask_keyup   = 0x040000;
491                 break;
492         case SAA7134_BOARD_ECS_TVP3XP:
493         case SAA7134_BOARD_ECS_TVP3XP_4CB5:
494                 ir_codes     = &ir_codes_eztv_table;
495                 mask_keycode = 0x00017c;
496                 mask_keyup   = 0x000002;
497                 polling      = 50; // ms
498                 break;
499         case SAA7134_BOARD_KWORLD_XPERT:
500         case SAA7134_BOARD_AVACSSMARTTV:
501                 ir_codes     = &ir_codes_pixelview_table;
502                 mask_keycode = 0x00001F;
503                 mask_keyup   = 0x000020;
504                 polling      = 50; // ms
505                 break;
506         case SAA7134_BOARD_MD2819:
507         case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
508         case SAA7134_BOARD_AVERMEDIA_305:
509         case SAA7134_BOARD_AVERMEDIA_307:
510         case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
511         case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
512         case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
513         case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
514         case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
515         case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
516         case SAA7134_BOARD_AVERMEDIA_M102:
517         case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
518                 ir_codes     = &ir_codes_avermedia_table;
519                 mask_keycode = 0x0007C8;
520                 mask_keydown = 0x000010;
521                 polling      = 50; // ms
522                 /* Set GPIO pin2 to high to enable the IR controller */
523                 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
524                 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
525                 break;
526         case SAA7134_BOARD_AVERMEDIA_M135A:
527                 ir_codes     = &ir_codes_avermedia_m135a_table;
528                 mask_keydown = 0x0040000;
529                 mask_keycode = 0x00013f;
530                 nec_gpio     = 1;
531                 break;
532         case SAA7134_BOARD_AVERMEDIA_777:
533         case SAA7134_BOARD_AVERMEDIA_A16AR:
534                 ir_codes     = &ir_codes_avermedia_table;
535                 mask_keycode = 0x02F200;
536                 mask_keydown = 0x000400;
537                 polling      = 50; // ms
538                 /* Without this we won't receive key up events */
539                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
540                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
541                 break;
542         case SAA7134_BOARD_AVERMEDIA_A16D:
543                 ir_codes     = &ir_codes_avermedia_a16d_table;
544                 mask_keycode = 0x02F200;
545                 mask_keydown = 0x000400;
546                 polling      = 50; /* ms */
547                 /* Without this we won't receive key up events */
548                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
549                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
550                 break;
551         case SAA7134_BOARD_KWORLD_TERMINATOR:
552                 ir_codes     = &ir_codes_pixelview_table;
553                 mask_keycode = 0x00001f;
554                 mask_keyup   = 0x000060;
555                 polling      = 50; // ms
556                 break;
557         case SAA7134_BOARD_MANLI_MTV001:
558         case SAA7134_BOARD_MANLI_MTV002:
559                 ir_codes     = &ir_codes_manli_table;
560                 mask_keycode = 0x001f00;
561                 mask_keyup   = 0x004000;
562                 polling      = 50; /* ms */
563                 break;
564         case SAA7134_BOARD_BEHOLD_409FM:
565         case SAA7134_BOARD_BEHOLD_401:
566         case SAA7134_BOARD_BEHOLD_403:
567         case SAA7134_BOARD_BEHOLD_403FM:
568         case SAA7134_BOARD_BEHOLD_405:
569         case SAA7134_BOARD_BEHOLD_405FM:
570         case SAA7134_BOARD_BEHOLD_407:
571         case SAA7134_BOARD_BEHOLD_407FM:
572         case SAA7134_BOARD_BEHOLD_409:
573         case SAA7134_BOARD_BEHOLD_505FM:
574         case SAA7134_BOARD_BEHOLD_505RDS_MK5:
575         case SAA7134_BOARD_BEHOLD_505RDS_MK3:
576         case SAA7134_BOARD_BEHOLD_507_9FM:
577         case SAA7134_BOARD_BEHOLD_507RDS_MK3:
578         case SAA7134_BOARD_BEHOLD_507RDS_MK5:
579                 ir_codes     = &ir_codes_manli_table;
580                 mask_keycode = 0x003f00;
581                 mask_keyup   = 0x004000;
582                 polling      = 50; /* ms */
583                 break;
584         case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
585                 ir_codes     = &ir_codes_behold_columbus_table;
586                 mask_keycode = 0x003f00;
587                 mask_keyup   = 0x004000;
588                 polling      = 50; // ms
589                 break;
590         case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
591                 ir_codes     = &ir_codes_pctv_sedna_table;
592                 mask_keycode = 0x001f00;
593                 mask_keyup   = 0x004000;
594                 polling      = 50; // ms
595                 break;
596         case SAA7134_BOARD_GOTVIEW_7135:
597                 ir_codes     = &ir_codes_gotview7135_table;
598                 mask_keycode = 0x0003CC;
599                 mask_keydown = 0x000010;
600                 polling      = 5; /* ms */
601                 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
602                 break;
603         case SAA7134_BOARD_VIDEOMATE_TV_PVR:
604         case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
605         case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
606                 ir_codes     = &ir_codes_videomate_tv_pvr_table;
607                 mask_keycode = 0x00003F;
608                 mask_keyup   = 0x400000;
609                 polling      = 50; // ms
610                 break;
611         case SAA7134_BOARD_PROTEUS_2309:
612                 ir_codes     = &ir_codes_proteus_2309_table;
613                 mask_keycode = 0x00007F;
614                 mask_keyup   = 0x000080;
615                 polling      = 50; // ms
616                 break;
617         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
618         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
619                 ir_codes     = &ir_codes_videomate_tv_pvr_table;
620                 mask_keycode = 0x003F00;
621                 mask_keyup   = 0x040000;
622                 break;
623         case SAA7134_BOARD_FLYDVBS_LR300:
624         case SAA7134_BOARD_FLYDVBT_LR301:
625         case SAA7134_BOARD_FLYDVBTDUO:
626                 ir_codes     = &ir_codes_flydvb_table;
627                 mask_keycode = 0x0001F00;
628                 mask_keydown = 0x0040000;
629                 break;
630         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
631         case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
632         case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
633                 ir_codes     = &ir_codes_asus_pc39_table;
634                 mask_keydown = 0x0040000;
635                 rc5_gpio = 1;
636                 break;
637         case SAA7134_BOARD_ENCORE_ENLTV:
638         case SAA7134_BOARD_ENCORE_ENLTV_FM:
639                 ir_codes     = &ir_codes_encore_enltv_table;
640                 mask_keycode = 0x00007f;
641                 mask_keyup   = 0x040000;
642                 polling      = 50; // ms
643                 break;
644         case SAA7134_BOARD_ENCORE_ENLTV_FM53:
645                 ir_codes     = &ir_codes_encore_enltv_fm53_table;
646                 mask_keydown = 0x0040000;
647                 mask_keycode = 0x00007f;
648                 nec_gpio = 1;
649                 break;
650         case SAA7134_BOARD_10MOONSTVMASTER3:
651                 ir_codes     = &ir_codes_encore_enltv_table;
652                 mask_keycode = 0x5f80000;
653                 mask_keyup   = 0x8000000;
654                 polling      = 50; //ms
655                 break;
656         case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
657                 ir_codes     = &ir_codes_genius_tvgo_a11mce_table;
658                 mask_keycode = 0xff;
659                 mask_keydown = 0xf00000;
660                 polling = 50; /* ms */
661                 break;
662         case SAA7134_BOARD_REAL_ANGEL_220:
663                 ir_codes     = &ir_codes_real_audio_220_32_keys_table;
664                 mask_keycode = 0x3f00;
665                 mask_keyup   = 0x4000;
666                 polling = 50; /* ms */
667                 break;
668         case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
669                 ir_codes     = &ir_codes_kworld_plus_tv_analog_table;
670                 mask_keycode = 0x7f;
671                 polling = 40; /* ms */
672                 break;
673         case SAA7134_BOARD_VIDEOMATE_S350:
674                 ir_codes     = &ir_codes_videomate_s350_table;
675                 mask_keycode = 0x003f00;
676                 mask_keydown = 0x040000;
677                 break;
678         case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
679                 ir_codes     = &ir_codes_winfast_table;
680                 mask_keycode = 0x5f00;
681                 mask_keyup   = 0x020000;
682                 polling      = 50; /* ms */
683                 break;
684         break;
685         }
686         if (NULL == ir_codes) {
687                 printk("%s: Oops: IR config error [card=%d]\n",
688                        dev->name, dev->board);
689                 return -ENODEV;
690         }
691
692         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
693         input_dev = input_allocate_device();
694         if (!ir || !input_dev) {
695                 err = -ENOMEM;
696                 goto err_out_free;
697         }
698
699         ir->dev = input_dev;
700
701         /* init hardware-specific stuff */
702         ir->mask_keycode = mask_keycode;
703         ir->mask_keydown = mask_keydown;
704         ir->mask_keyup   = mask_keyup;
705         ir->polling      = polling;
706         ir->rc5_gpio     = rc5_gpio;
707         ir->nec_gpio     = nec_gpio;
708
709         /* init input device */
710         snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
711                  saa7134_boards[dev->board].name);
712         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
713                  pci_name(dev->pci));
714
715         err = ir_input_init(input_dev, &ir->ir, ir_type);
716         if (err < 0)
717                 goto err_out_free;
718
719         input_dev->name = ir->name;
720         input_dev->phys = ir->phys;
721         input_dev->id.bustype = BUS_PCI;
722         input_dev->id.version = 1;
723         if (dev->pci->subsystem_vendor) {
724                 input_dev->id.vendor  = dev->pci->subsystem_vendor;
725                 input_dev->id.product = dev->pci->subsystem_device;
726         } else {
727                 input_dev->id.vendor  = dev->pci->vendor;
728                 input_dev->id.product = dev->pci->device;
729         }
730         input_dev->dev.parent = &dev->pci->dev;
731
732         dev->remote = ir;
733         saa7134_ir_start(dev, ir);
734
735         err = ir_input_register(ir->dev, ir_codes, NULL, MODULE_NAME);
736         if (err)
737                 goto err_out_stop;
738
739         /* the remote isn't as bouncy as a keyboard */
740         ir->dev->rep[REP_DELAY] = repeat_delay;
741         ir->dev->rep[REP_PERIOD] = repeat_period;
742
743         return 0;
744
745  err_out_stop:
746         saa7134_ir_stop(dev);
747         dev->remote = NULL;
748  err_out_free:
749         kfree(ir);
750         return err;
751 }
752
753 void saa7134_input_fini(struct saa7134_dev *dev)
754 {
755         if (NULL == dev->remote)
756                 return;
757
758         saa7134_ir_stop(dev);
759         ir_input_unregister(dev->remote->dev);
760         kfree(dev->remote);
761         dev->remote = NULL;
762 }
763
764 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
765 {
766         struct i2c_board_info info;
767
768         struct i2c_msg msg_msi = {
769                 .addr = 0x50,
770                 .flags = I2C_M_RD,
771                 .len = 0,
772                 .buf = NULL,
773         };
774
775         int rc;
776
777         if (disable_ir) {
778                 dprintk("IR has been disabled, not probing for i2c remote\n");
779                 return;
780         }
781
782         memset(&info, 0, sizeof(struct i2c_board_info));
783         memset(&dev->init_data, 0, sizeof(dev->init_data));
784         strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
785
786         switch (dev->board) {
787         case SAA7134_BOARD_PINNACLE_PCTV_110i:
788         case SAA7134_BOARD_PINNACLE_PCTV_310i:
789                 dev->init_data.name = "Pinnacle PCTV";
790                 if (pinnacle_remote == 0) {
791                         dev->init_data.get_key = get_key_pinnacle_color;
792                         dev->init_data.ir_codes = &ir_codes_pinnacle_color_table;
793                         info.addr = 0x47;
794                 } else {
795                         dev->init_data.get_key = get_key_pinnacle_grey;
796                         dev->init_data.ir_codes = &ir_codes_pinnacle_grey_table;
797                         info.addr = 0x47;
798                 }
799                 break;
800         case SAA7134_BOARD_UPMOST_PURPLE_TV:
801                 dev->init_data.name = "Purple TV";
802                 dev->init_data.get_key = get_key_purpletv;
803                 dev->init_data.ir_codes = &ir_codes_purpletv_table;
804                 info.addr = 0x7a;
805                 break;
806         case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
807                 dev->init_data.name = "MSI TV@nywhere Plus";
808                 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
809                 dev->init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
810                 info.addr = 0x30;
811                 /* MSI TV@nywhere Plus controller doesn't seem to
812                    respond to probes unless we read something from
813                    an existing device. Weird...
814                    REVISIT: might no longer be needed */
815                 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
816                 dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
817                         msg_msi.addr, dev->i2c_adap.name,
818                         (1 == rc) ? "yes" : "no");
819                 break;
820         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
821                 dev->init_data.name = "HVR 1110";
822                 dev->init_data.get_key = get_key_hvr1110;
823                 dev->init_data.ir_codes = &ir_codes_hauppauge_new_table;
824                 info.addr = 0x71;
825                 break;
826         case SAA7134_BOARD_BEHOLD_607FM_MK3:
827         case SAA7134_BOARD_BEHOLD_607FM_MK5:
828         case SAA7134_BOARD_BEHOLD_609FM_MK3:
829         case SAA7134_BOARD_BEHOLD_609FM_MK5:
830         case SAA7134_BOARD_BEHOLD_607RDS_MK3:
831         case SAA7134_BOARD_BEHOLD_607RDS_MK5:
832         case SAA7134_BOARD_BEHOLD_609RDS_MK3:
833         case SAA7134_BOARD_BEHOLD_609RDS_MK5:
834         case SAA7134_BOARD_BEHOLD_M6:
835         case SAA7134_BOARD_BEHOLD_M63:
836         case SAA7134_BOARD_BEHOLD_M6_EXTRA:
837         case SAA7134_BOARD_BEHOLD_H6:
838         case SAA7134_BOARD_BEHOLD_X7:
839                 dev->init_data.name = "BeholdTV";
840                 dev->init_data.get_key = get_key_beholdm6xx;
841                 dev->init_data.ir_codes = &ir_codes_behold_table;
842                 dev->init_data.type = IR_TYPE_NEC;
843                 info.addr = 0x2d;
844                 break;
845         case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
846         case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
847                 info.addr = 0x40;
848                 break;
849         case SAA7134_BOARD_FLYDVB_TRIO:
850                 dev->init_data.name = "FlyDVB Trio";
851                 dev->init_data.get_key = get_key_flydvb_trio;
852                 dev->init_data.ir_codes = &ir_codes_flydvb_table;
853                 info.addr = 0x0b;
854                 break;
855         default:
856                 dprintk("No I2C IR support for board %x\n", dev->board);
857                 return;
858         }
859
860         if (dev->init_data.name)
861                 info.platform_data = &dev->init_data;
862         i2c_new_device(&dev->i2c_adap, &info);
863 }
864
865 static int saa7134_rc5_irq(struct saa7134_dev *dev)
866 {
867         struct card_ir *ir = dev->remote;
868         struct timeval tv;
869         u32 gap;
870         unsigned long current_jiffies, timeout;
871
872         /* get time of bit */
873         current_jiffies = jiffies;
874         do_gettimeofday(&tv);
875
876         /* avoid overflow with gap >1s */
877         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
878                 gap = 200000;
879         } else {
880                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
881                     tv.tv_usec - ir->base_time.tv_usec;
882         }
883
884         /* active code => add bit */
885         if (ir->active) {
886                 /* only if in the code (otherwise spurious IRQ or timer
887                    late) */
888                 if (ir->last_bit < 28) {
889                         ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
890                             ir_rc5_remote_gap;
891                         ir->code |= 1 << ir->last_bit;
892                 }
893                 /* starting new code */
894         } else {
895                 ir->active = 1;
896                 ir->code = 0;
897                 ir->base_time = tv;
898                 ir->last_bit = 0;
899
900                 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
901                 mod_timer(&ir->timer_end, timeout);
902         }
903
904         return 1;
905 }
906
907
908 /* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
909    The first pulse (start) has 9 + 4.5 ms
910  */
911
912 static void saa7134_nec_timer(unsigned long data)
913 {
914         struct saa7134_dev *dev = (struct saa7134_dev *) data;
915         struct card_ir *ir = dev->remote;
916
917         dprintk("Cancel key repeat\n");
918
919         ir_input_nokey(ir->dev, &ir->ir);
920 }
921
922 static void nec_task(unsigned long data)
923 {
924         struct saa7134_dev *dev = (struct saa7134_dev *) data;
925         struct card_ir *ir;
926         struct timeval tv;
927         int count, pulse, oldpulse, gap;
928         u32 ircode = 0, not_code = 0;
929         int ngap = 0;
930
931         if (!data) {
932                 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
933                 /* GPIO will be kept disabled */
934                 return;
935         }
936
937         ir = dev->remote;
938
939         /* rising SAA7134_GPIO_GPRESCAN reads the status */
940         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
941         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
942
943         oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
944         pulse = oldpulse;
945
946         do_gettimeofday(&tv);
947         ir->base_time = tv;
948
949         /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
950            Unfortunately, using IRQ to decode pulse didn't work, since it uses
951            a pulse train of 38KHz. This means one pulse on each 52 us
952          */
953         do {
954                 /* Wait until the end of pulse/space or 5 ms */
955                 for (count = 0; count < 500; count++)  {
956                         udelay(10);
957                         /* rising SAA7134_GPIO_GPRESCAN reads the status */
958                         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
959                         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
960                         pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
961                                 & ir->mask_keydown;
962                         if (pulse != oldpulse)
963                                 break;
964                 }
965
966                 do_gettimeofday(&tv);
967                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
968                                 tv.tv_usec - ir->base_time.tv_usec;
969
970                 if (!pulse) {
971                         /* Bit 0 has 560 us, while bit 1 has 1120 us.
972                            Do something only if bit == 1
973                          */
974                         if (ngap && (gap > 560 + 280)) {
975                                 unsigned int shift = ngap - 1;
976
977                                 /* Address first, then command */
978                                 if (shift < 8) {
979                                         shift += 8;
980                                         ircode |= 1 << shift;
981                                 } else if (shift < 16) {
982                                         not_code |= 1 << shift;
983                                 } else if (shift < 24) {
984                                         shift -= 16;
985                                         ircode |= 1 << shift;
986                                 } else {
987                                         shift -= 24;
988                                         not_code |= 1 << shift;
989                                 }
990                         }
991                         ngap++;
992                 }
993
994
995                 ir->base_time = tv;
996
997                 /* TIMEOUT - Long pulse */
998                 if (gap >= 5000)
999                         break;
1000                 oldpulse = pulse;
1001         } while (ngap < 32);
1002
1003         if (ngap == 32) {
1004                 /* FIXME: should check if not_code == ~ircode */
1005                 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
1006
1007                 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
1008                          ir->code, ircode, not_code);
1009
1010                 ir_input_keydown(ir->dev, &ir->ir, ir->code);
1011         } else
1012                 dprintk("Repeat last key\n");
1013
1014         /* Keep repeating the last key */
1015         mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
1016
1017         saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1018 }
1019
1020 static int saa7134_nec_irq(struct saa7134_dev *dev)
1021 {
1022         struct card_ir *ir = dev->remote;
1023
1024         saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1025         tasklet_schedule(&ir->tlet);
1026
1027         return 1;
1028 }