]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/saa7134/saa7134-input.c
7e6f125e4e3134b5d49d600cb9eb21c3886d42fd
[net-next-2.6.git] / 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->running)
425                 return;
426
427         ir->running = 1;
428         if (ir->polling) {
429                 setup_timer(&ir->timer, saa7134_input_timer,
430                             (unsigned long)dev);
431                 ir->timer.expires  = jiffies + HZ;
432                 add_timer(&ir->timer);
433         } else if (ir->rc5_gpio) {
434                 /* set timer_end for code completion */
435                 init_timer(&ir->timer_end);
436                 ir->timer_end.function = ir_rc5_timer_end;
437                 ir->timer_end.data = (unsigned long)ir;
438                 init_timer(&ir->timer_keyup);
439                 ir->timer_keyup.function = ir_rc5_timer_keyup;
440                 ir->timer_keyup.data = (unsigned long)ir;
441                 ir->shift_by = 2;
442                 ir->start = 0x2;
443                 ir->addr = 0x17;
444                 ir->rc5_key_timeout = ir_rc5_key_timeout;
445                 ir->rc5_remote_gap = ir_rc5_remote_gap;
446         } else if (ir->nec_gpio) {
447                 setup_timer(&ir->timer_keyup, saa7134_nec_timer,
448                             (unsigned long)dev);
449                 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
450         }
451 }
452
453 void saa7134_ir_stop(struct saa7134_dev *dev)
454 {
455         struct card_ir *ir = dev->remote;
456
457         if (!ir->running)
458                 return;
459         if (dev->remote->polling)
460                 del_timer_sync(&dev->remote->timer);
461         else if (ir->rc5_gpio)
462                 del_timer_sync(&ir->timer_end);
463         else if (ir->nec_gpio)
464                 tasklet_kill(&ir->tlet);
465         ir->running = 0;
466 }
467
468 int saa7134_ir_change_protocol(void *priv, u64 ir_type)
469 {
470         struct saa7134_dev *dev = priv;
471         struct card_ir *ir = dev->remote;
472         u32 nec_gpio, rc5_gpio;
473
474         if (ir_type == IR_TYPE_RC5) {
475                 dprintk("Changing protocol to RC5\n");
476                 nec_gpio = 0;
477                 rc5_gpio = 1;
478         } else if (ir_type == IR_TYPE_NEC) {
479                 dprintk("Changing protocol to NEC\n");
480                 nec_gpio = 1;
481                 rc5_gpio = 0;
482         } else {
483                 dprintk("IR protocol type %ud is not supported\n",
484                         (unsigned)ir_type);
485                 return -EINVAL;
486         }
487
488         if (ir->running) {
489                 saa7134_ir_stop(dev);
490                 ir->nec_gpio = nec_gpio;
491                 ir->rc5_gpio = rc5_gpio;
492                 saa7134_ir_start(dev, ir);
493         } else {
494                 ir->nec_gpio = nec_gpio;
495                 ir->rc5_gpio = rc5_gpio;
496         }
497
498         return 0;
499 }
500
501 int saa7134_input_init1(struct saa7134_dev *dev)
502 {
503         struct card_ir *ir;
504         struct input_dev *input_dev;
505         struct ir_scancode_table *ir_codes = NULL;
506         u32 mask_keycode = 0;
507         u32 mask_keydown = 0;
508         u32 mask_keyup   = 0;
509         int polling      = 0;
510         int rc5_gpio     = 0;
511         int nec_gpio     = 0;
512         u64 ir_type = IR_TYPE_OTHER;
513         int err;
514
515         if (dev->has_remote != SAA7134_REMOTE_GPIO)
516                 return -ENODEV;
517         if (disable_ir)
518                 return -ENODEV;
519
520         /* detect & configure */
521         switch (dev->board) {
522         case SAA7134_BOARD_FLYVIDEO2000:
523         case SAA7134_BOARD_FLYVIDEO3000:
524         case SAA7134_BOARD_HAWELL_HW_404M7:
525         case SAA7134_BOARD_FLYTVPLATINUM_FM:
526         case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
527         case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
528                 ir_codes     = &ir_codes_flyvideo_table;
529                 mask_keycode = 0xEC00000;
530                 mask_keydown = 0x0040000;
531                 break;
532         case SAA7134_BOARD_CINERGY400:
533         case SAA7134_BOARD_CINERGY600:
534         case SAA7134_BOARD_CINERGY600_MK3:
535                 ir_codes     = &ir_codes_cinergy_table;
536                 mask_keycode = 0x00003f;
537                 mask_keyup   = 0x040000;
538                 break;
539         case SAA7134_BOARD_ECS_TVP3XP:
540         case SAA7134_BOARD_ECS_TVP3XP_4CB5:
541                 ir_codes     = &ir_codes_eztv_table;
542                 mask_keycode = 0x00017c;
543                 mask_keyup   = 0x000002;
544                 polling      = 50; // ms
545                 break;
546         case SAA7134_BOARD_KWORLD_XPERT:
547         case SAA7134_BOARD_AVACSSMARTTV:
548                 ir_codes     = &ir_codes_pixelview_table;
549                 mask_keycode = 0x00001F;
550                 mask_keyup   = 0x000020;
551                 polling      = 50; // ms
552                 break;
553         case SAA7134_BOARD_MD2819:
554         case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
555         case SAA7134_BOARD_AVERMEDIA_305:
556         case SAA7134_BOARD_AVERMEDIA_307:
557         case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
558         case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
559         case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
560         case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
561         case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
562         case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
563         case SAA7134_BOARD_AVERMEDIA_M102:
564         case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
565                 ir_codes     = &ir_codes_avermedia_table;
566                 mask_keycode = 0x0007C8;
567                 mask_keydown = 0x000010;
568                 polling      = 50; // ms
569                 /* Set GPIO pin2 to high to enable the IR controller */
570                 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
571                 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
572                 break;
573         case SAA7134_BOARD_AVERMEDIA_M135A:
574                 ir_codes     = &ir_codes_avermedia_m135a_rm_jx_table;
575                 mask_keydown = 0x0040000;
576                 mask_keycode = 0xffff;
577                 nec_gpio     = 1;
578                 break;
579         case SAA7134_BOARD_AVERMEDIA_777:
580         case SAA7134_BOARD_AVERMEDIA_A16AR:
581                 ir_codes     = &ir_codes_avermedia_table;
582                 mask_keycode = 0x02F200;
583                 mask_keydown = 0x000400;
584                 polling      = 50; // ms
585                 /* Without this we won't receive key up events */
586                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
587                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
588                 break;
589         case SAA7134_BOARD_AVERMEDIA_A16D:
590                 ir_codes     = &ir_codes_avermedia_a16d_table;
591                 mask_keycode = 0x02F200;
592                 mask_keydown = 0x000400;
593                 polling      = 50; /* ms */
594                 /* Without this we won't receive key up events */
595                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
596                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
597                 break;
598         case SAA7134_BOARD_KWORLD_TERMINATOR:
599                 ir_codes     = &ir_codes_pixelview_table;
600                 mask_keycode = 0x00001f;
601                 mask_keyup   = 0x000060;
602                 polling      = 50; // ms
603                 break;
604         case SAA7134_BOARD_MANLI_MTV001:
605         case SAA7134_BOARD_MANLI_MTV002:
606                 ir_codes     = &ir_codes_manli_table;
607                 mask_keycode = 0x001f00;
608                 mask_keyup   = 0x004000;
609                 polling      = 50; /* ms */
610                 break;
611         case SAA7134_BOARD_BEHOLD_409FM:
612         case SAA7134_BOARD_BEHOLD_401:
613         case SAA7134_BOARD_BEHOLD_403:
614         case SAA7134_BOARD_BEHOLD_403FM:
615         case SAA7134_BOARD_BEHOLD_405:
616         case SAA7134_BOARD_BEHOLD_405FM:
617         case SAA7134_BOARD_BEHOLD_407:
618         case SAA7134_BOARD_BEHOLD_407FM:
619         case SAA7134_BOARD_BEHOLD_409:
620         case SAA7134_BOARD_BEHOLD_505FM:
621         case SAA7134_BOARD_BEHOLD_505RDS_MK5:
622         case SAA7134_BOARD_BEHOLD_505RDS_MK3:
623         case SAA7134_BOARD_BEHOLD_507_9FM:
624         case SAA7134_BOARD_BEHOLD_507RDS_MK3:
625         case SAA7134_BOARD_BEHOLD_507RDS_MK5:
626                 ir_codes     = &ir_codes_manli_table;
627                 mask_keycode = 0x003f00;
628                 mask_keyup   = 0x004000;
629                 polling      = 50; /* ms */
630                 break;
631         case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
632                 ir_codes     = &ir_codes_behold_columbus_table;
633                 mask_keycode = 0x003f00;
634                 mask_keyup   = 0x004000;
635                 polling      = 50; // ms
636                 break;
637         case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
638                 ir_codes     = &ir_codes_pctv_sedna_table;
639                 mask_keycode = 0x001f00;
640                 mask_keyup   = 0x004000;
641                 polling      = 50; // ms
642                 break;
643         case SAA7134_BOARD_GOTVIEW_7135:
644                 ir_codes     = &ir_codes_gotview7135_table;
645                 mask_keycode = 0x0003CC;
646                 mask_keydown = 0x000010;
647                 polling      = 5; /* ms */
648                 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
649                 break;
650         case SAA7134_BOARD_VIDEOMATE_TV_PVR:
651         case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
652         case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
653                 ir_codes     = &ir_codes_videomate_tv_pvr_table;
654                 mask_keycode = 0x00003F;
655                 mask_keyup   = 0x400000;
656                 polling      = 50; // ms
657                 break;
658         case SAA7134_BOARD_PROTEUS_2309:
659                 ir_codes     = &ir_codes_proteus_2309_table;
660                 mask_keycode = 0x00007F;
661                 mask_keyup   = 0x000080;
662                 polling      = 50; // ms
663                 break;
664         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
665         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
666                 ir_codes     = &ir_codes_videomate_tv_pvr_table;
667                 mask_keycode = 0x003F00;
668                 mask_keyup   = 0x040000;
669                 break;
670         case SAA7134_BOARD_FLYDVBS_LR300:
671         case SAA7134_BOARD_FLYDVBT_LR301:
672         case SAA7134_BOARD_FLYDVBTDUO:
673                 ir_codes     = &ir_codes_flydvb_table;
674                 mask_keycode = 0x0001F00;
675                 mask_keydown = 0x0040000;
676                 break;
677         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
678         case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
679         case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
680                 ir_codes     = &ir_codes_asus_pc39_table;
681                 mask_keydown = 0x0040000;
682                 rc5_gpio = 1;
683                 break;
684         case SAA7134_BOARD_ENCORE_ENLTV:
685         case SAA7134_BOARD_ENCORE_ENLTV_FM:
686                 ir_codes     = &ir_codes_encore_enltv_table;
687                 mask_keycode = 0x00007f;
688                 mask_keyup   = 0x040000;
689                 polling      = 50; // ms
690                 break;
691         case SAA7134_BOARD_ENCORE_ENLTV_FM53:
692                 ir_codes     = &ir_codes_encore_enltv_fm53_table;
693                 mask_keydown = 0x0040000;
694                 mask_keycode = 0x00007f;
695                 nec_gpio = 1;
696                 break;
697         case SAA7134_BOARD_10MOONSTVMASTER3:
698                 ir_codes     = &ir_codes_encore_enltv_table;
699                 mask_keycode = 0x5f80000;
700                 mask_keyup   = 0x8000000;
701                 polling      = 50; //ms
702                 break;
703         case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
704                 ir_codes     = &ir_codes_genius_tvgo_a11mce_table;
705                 mask_keycode = 0xff;
706                 mask_keydown = 0xf00000;
707                 polling = 50; /* ms */
708                 break;
709         case SAA7134_BOARD_REAL_ANGEL_220:
710                 ir_codes     = &ir_codes_real_audio_220_32_keys_table;
711                 mask_keycode = 0x3f00;
712                 mask_keyup   = 0x4000;
713                 polling = 50; /* ms */
714                 break;
715         case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
716                 ir_codes     = &ir_codes_kworld_plus_tv_analog_table;
717                 mask_keycode = 0x7f;
718                 polling = 40; /* ms */
719                 break;
720         case SAA7134_BOARD_VIDEOMATE_S350:
721                 ir_codes     = &ir_codes_videomate_s350_table;
722                 mask_keycode = 0x003f00;
723                 mask_keydown = 0x040000;
724                 break;
725         case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
726                 ir_codes     = &ir_codes_winfast_table;
727                 mask_keycode = 0x5f00;
728                 mask_keyup   = 0x020000;
729                 polling      = 50; /* ms */
730                 break;
731         break;
732         }
733         if (NULL == ir_codes) {
734                 printk("%s: Oops: IR config error [card=%d]\n",
735                        dev->name, dev->board);
736                 return -ENODEV;
737         }
738
739         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
740         input_dev = input_allocate_device();
741         if (!ir || !input_dev) {
742                 err = -ENOMEM;
743                 goto err_out_free;
744         }
745
746         ir->dev = input_dev;
747         dev->remote = ir;
748
749         ir->running = 0;
750
751         /* init hardware-specific stuff */
752         ir->mask_keycode = mask_keycode;
753         ir->mask_keydown = mask_keydown;
754         ir->mask_keyup   = mask_keyup;
755         ir->polling      = polling;
756         ir->rc5_gpio     = rc5_gpio;
757         ir->nec_gpio     = nec_gpio;
758
759         /* init input device */
760         snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
761                  saa7134_boards[dev->board].name);
762         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
763                  pci_name(dev->pci));
764
765         if (ir_codes->ir_type != IR_TYPE_OTHER) {
766                 ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
767                 ir->props.priv = dev;
768                 ir->props.change_protocol = saa7134_ir_change_protocol;
769
770                 /* Set IR protocol */
771                 saa7134_ir_change_protocol(ir->props.priv, ir_codes->ir_type);
772         }
773         err = ir_input_init(input_dev, &ir->ir, ir_type);
774         if (err < 0)
775                 goto err_out_free;
776
777         input_dev->name = ir->name;
778         input_dev->phys = ir->phys;
779         input_dev->id.bustype = BUS_PCI;
780         input_dev->id.version = 1;
781         if (dev->pci->subsystem_vendor) {
782                 input_dev->id.vendor  = dev->pci->subsystem_vendor;
783                 input_dev->id.product = dev->pci->subsystem_device;
784         } else {
785                 input_dev->id.vendor  = dev->pci->vendor;
786                 input_dev->id.product = dev->pci->device;
787         }
788         input_dev->dev.parent = &dev->pci->dev;
789
790         err = ir_input_register(ir->dev, ir_codes, &ir->props, MODULE_NAME);
791         if (err)
792                 goto err_out_stop;
793
794         saa7134_ir_start(dev, ir);
795
796         /* the remote isn't as bouncy as a keyboard */
797         ir->dev->rep[REP_DELAY] = repeat_delay;
798         ir->dev->rep[REP_PERIOD] = repeat_period;
799
800         return 0;
801
802  err_out_stop:
803         saa7134_ir_stop(dev);
804         dev->remote = NULL;
805  err_out_free:
806         kfree(ir);
807         return err;
808 }
809
810 void saa7134_input_fini(struct saa7134_dev *dev)
811 {
812         if (NULL == dev->remote)
813                 return;
814
815         saa7134_ir_stop(dev);
816         ir_input_unregister(dev->remote->dev);
817         kfree(dev->remote);
818         dev->remote = NULL;
819 }
820
821 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
822 {
823         struct i2c_board_info info;
824
825         struct i2c_msg msg_msi = {
826                 .addr = 0x50,
827                 .flags = I2C_M_RD,
828                 .len = 0,
829                 .buf = NULL,
830         };
831
832         int rc;
833
834         if (disable_ir) {
835                 dprintk("IR has been disabled, not probing for i2c remote\n");
836                 return;
837         }
838
839         memset(&info, 0, sizeof(struct i2c_board_info));
840         memset(&dev->init_data, 0, sizeof(dev->init_data));
841         strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
842
843         switch (dev->board) {
844         case SAA7134_BOARD_PINNACLE_PCTV_110i:
845         case SAA7134_BOARD_PINNACLE_PCTV_310i:
846                 dev->init_data.name = "Pinnacle PCTV";
847                 if (pinnacle_remote == 0) {
848                         dev->init_data.get_key = get_key_pinnacle_color;
849                         dev->init_data.ir_codes = &ir_codes_pinnacle_color_table;
850                         info.addr = 0x47;
851                 } else {
852                         dev->init_data.get_key = get_key_pinnacle_grey;
853                         dev->init_data.ir_codes = &ir_codes_pinnacle_grey_table;
854                         info.addr = 0x47;
855                 }
856                 break;
857         case SAA7134_BOARD_UPMOST_PURPLE_TV:
858                 dev->init_data.name = "Purple TV";
859                 dev->init_data.get_key = get_key_purpletv;
860                 dev->init_data.ir_codes = &ir_codes_purpletv_table;
861                 info.addr = 0x7a;
862                 break;
863         case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
864                 dev->init_data.name = "MSI TV@nywhere Plus";
865                 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
866                 dev->init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
867                 info.addr = 0x30;
868                 /* MSI TV@nywhere Plus controller doesn't seem to
869                    respond to probes unless we read something from
870                    an existing device. Weird...
871                    REVISIT: might no longer be needed */
872                 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
873                 dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
874                         msg_msi.addr, dev->i2c_adap.name,
875                         (1 == rc) ? "yes" : "no");
876                 break;
877         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
878                 dev->init_data.name = "HVR 1110";
879                 dev->init_data.get_key = get_key_hvr1110;
880                 dev->init_data.ir_codes = &ir_codes_hauppauge_new_table;
881                 info.addr = 0x71;
882                 break;
883         case SAA7134_BOARD_BEHOLD_607FM_MK3:
884         case SAA7134_BOARD_BEHOLD_607FM_MK5:
885         case SAA7134_BOARD_BEHOLD_609FM_MK3:
886         case SAA7134_BOARD_BEHOLD_609FM_MK5:
887         case SAA7134_BOARD_BEHOLD_607RDS_MK3:
888         case SAA7134_BOARD_BEHOLD_607RDS_MK5:
889         case SAA7134_BOARD_BEHOLD_609RDS_MK3:
890         case SAA7134_BOARD_BEHOLD_609RDS_MK5:
891         case SAA7134_BOARD_BEHOLD_M6:
892         case SAA7134_BOARD_BEHOLD_M63:
893         case SAA7134_BOARD_BEHOLD_M6_EXTRA:
894         case SAA7134_BOARD_BEHOLD_H6:
895         case SAA7134_BOARD_BEHOLD_X7:
896                 dev->init_data.name = "BeholdTV";
897                 dev->init_data.get_key = get_key_beholdm6xx;
898                 dev->init_data.ir_codes = &ir_codes_behold_table;
899                 dev->init_data.type = IR_TYPE_NEC;
900                 info.addr = 0x2d;
901                 break;
902         case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
903         case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
904                 info.addr = 0x40;
905                 break;
906         case SAA7134_BOARD_FLYDVB_TRIO:
907                 dev->init_data.name = "FlyDVB Trio";
908                 dev->init_data.get_key = get_key_flydvb_trio;
909                 dev->init_data.ir_codes = &ir_codes_flydvb_table;
910                 info.addr = 0x0b;
911                 break;
912         default:
913                 dprintk("No I2C IR support for board %x\n", dev->board);
914                 return;
915         }
916
917         if (dev->init_data.name)
918                 info.platform_data = &dev->init_data;
919         i2c_new_device(&dev->i2c_adap, &info);
920 }
921
922 static int saa7134_rc5_irq(struct saa7134_dev *dev)
923 {
924         struct card_ir *ir = dev->remote;
925         struct timeval tv;
926         u32 gap;
927         unsigned long current_jiffies, timeout;
928
929         /* get time of bit */
930         current_jiffies = jiffies;
931         do_gettimeofday(&tv);
932
933         /* avoid overflow with gap >1s */
934         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
935                 gap = 200000;
936         } else {
937                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
938                     tv.tv_usec - ir->base_time.tv_usec;
939         }
940
941         /* active code => add bit */
942         if (ir->active) {
943                 /* only if in the code (otherwise spurious IRQ or timer
944                    late) */
945                 if (ir->last_bit < 28) {
946                         ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
947                             ir_rc5_remote_gap;
948                         ir->code |= 1 << ir->last_bit;
949                 }
950                 /* starting new code */
951         } else {
952                 ir->active = 1;
953                 ir->code = 0;
954                 ir->base_time = tv;
955                 ir->last_bit = 0;
956
957                 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
958                 mod_timer(&ir->timer_end, timeout);
959         }
960
961         return 1;
962 }
963
964
965 /* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
966    The first pulse (start) has 9 + 4.5 ms
967  */
968
969 static void saa7134_nec_timer(unsigned long data)
970 {
971         struct saa7134_dev *dev = (struct saa7134_dev *) data;
972         struct card_ir *ir = dev->remote;
973
974         dprintk("Cancel key repeat\n");
975
976         ir_input_nokey(ir->dev, &ir->ir);
977 }
978
979 static void nec_task(unsigned long data)
980 {
981         struct saa7134_dev *dev = (struct saa7134_dev *) data;
982         struct card_ir *ir;
983         struct timeval tv;
984         int count, pulse, oldpulse, gap;
985         u32 ircode = 0, not_code = 0;
986         int ngap = 0;
987
988         if (!data) {
989                 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
990                 /* GPIO will be kept disabled */
991                 return;
992         }
993
994         ir = dev->remote;
995
996         /* rising SAA7134_GPIO_GPRESCAN reads the status */
997         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
998         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
999
1000         oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1001         pulse = oldpulse;
1002
1003         do_gettimeofday(&tv);
1004         ir->base_time = tv;
1005
1006         /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
1007            Unfortunately, using IRQ to decode pulse didn't work, since it uses
1008            a pulse train of 38KHz. This means one pulse on each 52 us
1009          */
1010         do {
1011                 /* Wait until the end of pulse/space or 5 ms */
1012                 for (count = 0; count < 500; count++)  {
1013                         udelay(10);
1014                         /* rising SAA7134_GPIO_GPRESCAN reads the status */
1015                         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1016                         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1017                         pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
1018                                 & ir->mask_keydown;
1019                         if (pulse != oldpulse)
1020                                 break;
1021                 }
1022
1023                 do_gettimeofday(&tv);
1024                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
1025                                 tv.tv_usec - ir->base_time.tv_usec;
1026
1027                 if (!pulse) {
1028                         /* Bit 0 has 560 us, while bit 1 has 1120 us.
1029                            Do something only if bit == 1
1030                          */
1031                         if (ngap && (gap > 560 + 280)) {
1032                                 unsigned int shift = ngap - 1;
1033
1034                                 /* Address first, then command */
1035                                 if (shift < 8) {
1036                                         shift += 8;
1037                                         ircode |= 1 << shift;
1038                                 } else if (shift < 16) {
1039                                         not_code |= 1 << shift;
1040                                 } else if (shift < 24) {
1041                                         shift -= 16;
1042                                         ircode |= 1 << shift;
1043                                 } else {
1044                                         shift -= 24;
1045                                         not_code |= 1 << shift;
1046                                 }
1047                         }
1048                         ngap++;
1049                 }
1050
1051
1052                 ir->base_time = tv;
1053
1054                 /* TIMEOUT - Long pulse */
1055                 if (gap >= 5000)
1056                         break;
1057                 oldpulse = pulse;
1058         } while (ngap < 32);
1059
1060         if (ngap == 32) {
1061                 /* FIXME: should check if not_code == ~ircode */
1062                 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
1063
1064                 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
1065                          ir->code, ircode, not_code);
1066
1067                 ir_input_keydown(ir->dev, &ir->ir, ir->code);
1068         } else
1069                 dprintk("Repeat last key\n");
1070
1071         /* Keep repeating the last key */
1072         mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
1073
1074         saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1075 }
1076
1077 static int saa7134_nec_irq(struct saa7134_dev *dev)
1078 {
1079         struct card_ir *ir = dev->remote;
1080
1081         saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1082         tasklet_schedule(&ir->tlet);
1083
1084         return 1;
1085 }