]> Pileus Git - ~andy/linux/blob - drivers/media/video/saa7134/saa7134-input.c
72562b8cf3be949836785c5cd0ddcfb3be29f171
[~andy/linux] / 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/slab.h>
26
27 #include "saa7134-reg.h"
28 #include "saa7134.h"
29
30 #define MODULE_NAME "saa7134"
31
32 static unsigned int disable_ir;
33 module_param(disable_ir, int, 0444);
34 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
35
36 static unsigned int ir_debug;
37 module_param(ir_debug, int, 0644);
38 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
39
40 static int pinnacle_remote;
41 module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
42 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
43
44 static int ir_rc5_remote_gap = 885;
45 module_param(ir_rc5_remote_gap, int, 0644);
46
47 static unsigned int disable_other_ir;
48 module_param(disable_other_ir, int, 0644);
49 MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
50     "alternative remotes from other manufacturers");
51
52 #define dprintk(fmt, arg...)    if (ir_debug) \
53         printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
54 #define i2cdprintk(fmt, arg...)    if (ir_debug) \
55         printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
56
57 /* Helper function for raw decoding at GPIO16 or GPIO18 */
58 static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
59
60 /* -------------------- GPIO generic keycode builder -------------------- */
61
62 static int build_key(struct saa7134_dev *dev)
63 {
64         struct card_ir *ir = dev->remote;
65         u32 gpio, data;
66
67         /* here comes the additional handshake steps for some cards */
68         switch (dev->board) {
69         case SAA7134_BOARD_GOTVIEW_7135:
70                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
71                 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
72                 break;
73         }
74         /* rising SAA7134_GPIO_GPRESCAN reads the status */
75         saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
76         saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
77
78         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
79         if (ir->polling) {
80                 if (ir->last_gpio == gpio)
81                         return 0;
82                 ir->last_gpio = gpio;
83         }
84
85         data = ir_extract_bits(gpio, ir->mask_keycode);
86         dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
87                 gpio, ir->mask_keycode, data);
88
89         switch (dev->board) {
90         case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
91                 if (data == ir->mask_keycode)
92                         ir_keyup(ir->dev);
93                 else
94                         ir_keydown_notimeout(ir->dev, data, 0);
95                 return 0;
96         }
97
98         if (ir->polling) {
99                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
100                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
101                         ir_keydown_notimeout(ir->dev, data, 0);
102                 } else {
103                         ir_keyup(ir->dev);
104                 }
105         }
106         else {  /* IRQ driven mode - handle key press and release in one go */
107                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
108                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
109                         ir_keydown_notimeout(ir->dev, data, 0);
110                         ir_keyup(ir->dev);
111                 }
112         }
113
114         return 0;
115 }
116
117 /* --------------------- Chip specific I2C key builders ----------------- */
118
119 static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
120 {
121         int gpio;
122         int attempt = 0;
123         unsigned char b;
124
125         /* We need this to access GPI Used by the saa_readl macro. */
126         struct saa7134_dev *dev = ir->c->adapter->algo_data;
127
128         if (dev == NULL) {
129                 i2cdprintk("get_key_flydvb_trio: "
130                            "ir->c->adapter->algo_data is NULL!\n");
131                 return -EIO;
132         }
133
134         /* rising SAA7134_GPIGPRESCAN reads the status */
135         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
136         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
137
138         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
139
140         if (0x40000 & ~gpio)
141                 return 0; /* No button press */
142
143         /* No button press - only before first key pressed */
144         if (b == 0xFF)
145                 return 0;
146
147         /* poll IR chip */
148         /* weak up the IR chip */
149         b = 0;
150
151         while (1 != i2c_master_send(ir->c, &b, 1)) {
152                 if ((attempt++) < 10) {
153                         /*
154                          * wait a bit for next attempt -
155                          * I don't know how make it better
156                          */
157                         msleep(10);
158                         continue;
159                 }
160                 i2cdprintk("send wake up byte to pic16C505 (IR chip)"
161                            "failed %dx\n", attempt);
162                 return -EIO;
163         }
164         if (1 != i2c_master_recv(ir->c, &b, 1)) {
165                 i2cdprintk("read error\n");
166                 return -EIO;
167         }
168
169         *ir_key = b;
170         *ir_raw = b;
171         return 1;
172 }
173
174 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
175                                        u32 *ir_raw)
176 {
177         unsigned char b;
178         int gpio;
179
180         /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
181         struct saa7134_dev *dev = ir->c->adapter->algo_data;
182         if (dev == NULL) {
183                 i2cdprintk("get_key_msi_tvanywhere_plus: "
184                            "ir->c->adapter->algo_data is NULL!\n");
185                 return -EIO;
186         }
187
188         /* rising SAA7134_GPIO_GPRESCAN reads the status */
189
190         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
191         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
192
193         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
194
195         /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
196            I2C receive if gpio&0x40 is not low. */
197
198         if (gpio & 0x40)
199                 return 0;       /* No button press */
200
201         /* GPIO says there is a button press. Get it. */
202
203         if (1 != i2c_master_recv(ir->c, &b, 1)) {
204                 i2cdprintk("read error\n");
205                 return -EIO;
206         }
207
208         /* No button press */
209
210         if (b == 0xff)
211                 return 0;
212
213         /* Button pressed */
214
215         dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
216         *ir_key = b;
217         *ir_raw = b;
218         return 1;
219 }
220
221 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
222 {
223         unsigned char b;
224
225         /* poll IR chip */
226         if (1 != i2c_master_recv(ir->c, &b, 1)) {
227                 i2cdprintk("read error\n");
228                 return -EIO;
229         }
230
231         /* no button press */
232         if (b==0)
233                 return 0;
234
235         /* repeating */
236         if (b & 0x80)
237                 return 1;
238
239         *ir_key = b;
240         *ir_raw = b;
241         return 1;
242 }
243
244 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
245 {
246         unsigned char buf[5], cod4, code3, code4;
247
248         /* poll IR chip */
249         if (5 != i2c_master_recv(ir->c, buf, 5))
250                 return -EIO;
251
252         cod4    = buf[4];
253         code4   = (cod4 >> 2);
254         code3   = buf[3];
255         if (code3 == 0)
256                 /* no key pressed */
257                 return 0;
258
259         /* return key */
260         *ir_key = code4;
261         *ir_raw = code4;
262         return 1;
263 }
264
265
266 static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
267 {
268         unsigned char data[12];
269         u32 gpio;
270
271         struct saa7134_dev *dev = ir->c->adapter->algo_data;
272
273         /* rising SAA7134_GPIO_GPRESCAN reads the status */
274         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
275         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
276
277         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
278
279         if (0x400000 & ~gpio)
280                 return 0; /* No button press */
281
282         ir->c->addr = 0x5a >> 1;
283
284         if (12 != i2c_master_recv(ir->c, data, 12)) {
285                 i2cdprintk("read error\n");
286                 return -EIO;
287         }
288         /* IR of this card normally decode signals NEC-standard from
289          * - Sven IHOO MT 5.1R remote. xxyye718
290          * - Sven DVD HD-10xx remote. xxyyf708
291          * - BBK ...
292          * - mayby others
293          * So, skip not our, if disable full codes mode.
294          */
295         if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
296                 return 0;
297
298         /* Wrong data decode fix */
299         if (data[9] != (unsigned char)(~data[8]))
300                 return 0;
301
302         *ir_key = data[9];
303         *ir_raw = data[9];
304
305         return 1;
306 }
307
308 /* Common (grey or coloured) pinnacle PCTV remote handling
309  *
310  */
311 static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
312                             int parity_offset, int marker, int code_modulo)
313 {
314         unsigned char b[4];
315         unsigned int start = 0,parity = 0,code = 0;
316
317         /* poll IR chip */
318         if (4 != i2c_master_recv(ir->c, b, 4)) {
319                 i2cdprintk("read error\n");
320                 return -EIO;
321         }
322
323         for (start = 0; start < ARRAY_SIZE(b); start++) {
324                 if (b[start] == marker) {
325                         code=b[(start+parity_offset + 1) % 4];
326                         parity=b[(start+parity_offset) % 4];
327                 }
328         }
329
330         /* Empty Request */
331         if (parity == 0)
332                 return 0;
333
334         /* Repeating... */
335         if (ir->old == parity)
336                 return 0;
337
338         ir->old = parity;
339
340         /* drop special codes when a key is held down a long time for the grey controller
341            In this case, the second bit of the code is asserted */
342         if (marker == 0xfe && (code & 0x40))
343                 return 0;
344
345         code %= code_modulo;
346
347         *ir_raw = code;
348         *ir_key = code;
349
350         i2cdprintk("Pinnacle PCTV key %02x\n", code);
351
352         return 1;
353 }
354
355 /* The grey pinnacle PCTV remote
356  *
357  *  There are one issue with this remote:
358  *   - I2c packet does not change when the same key is pressed quickly. The workaround
359  *     is to hold down each key for about half a second, so that another code is generated
360  *     in the i2c packet, and the function can distinguish key presses.
361  *
362  * Sylvain Pasche <sylvain.pasche@gmail.com>
363  */
364 static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
365 {
366
367         return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
368 }
369
370
371 /* The new pinnacle PCTV remote (with the colored buttons)
372  *
373  * Ricardo Cerqueira <v4l@cerqueira.org>
374  */
375 static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
376 {
377         /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
378          *
379          * this is the only value that results in 42 unique
380          * codes < 128
381          */
382
383         return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
384 }
385
386 void saa7134_input_irq(struct saa7134_dev *dev)
387 {
388         struct card_ir *ir;
389
390         if (!dev || !dev->remote)
391                 return;
392
393         ir = dev->remote;
394         if (!ir->running)
395                 return;
396
397         if (!ir->polling && !ir->raw_decode) {
398                 build_key(dev);
399         } else if (ir->raw_decode) {
400                 saa7134_raw_decode_irq(dev);
401         }
402 }
403
404 static void saa7134_input_timer(unsigned long data)
405 {
406         struct saa7134_dev *dev = (struct saa7134_dev *)data;
407         struct card_ir *ir = dev->remote;
408
409         build_key(dev);
410         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
411 }
412
413 static void ir_raw_decode_timer_end(unsigned long data)
414 {
415         struct saa7134_dev *dev = (struct saa7134_dev *)data;
416         struct card_ir *ir = dev->remote;
417
418         ir_raw_event_handle(dev->remote->dev);
419
420         ir->active = 0;
421 }
422
423 static int __saa7134_ir_start(void *priv)
424 {
425         struct saa7134_dev *dev = priv;
426         struct card_ir *ir;
427
428         if (!dev)
429                 return -EINVAL;
430
431         ir  = dev->remote;
432         if (!ir)
433                 return -EINVAL;
434
435         if (ir->running)
436                 return 0;
437
438         ir->running = 1;
439         if (ir->polling) {
440                 setup_timer(&ir->timer, saa7134_input_timer,
441                             (unsigned long)dev);
442                 ir->timer.expires  = jiffies + HZ;
443                 add_timer(&ir->timer);
444         } else if (ir->raw_decode) {
445                 /* set timer_end for code completion */
446                 init_timer(&ir->timer_end);
447                 ir->timer_end.function = ir_raw_decode_timer_end;
448                 ir->timer_end.data = (unsigned long)dev;
449                 ir->active = 0;
450         }
451
452         return 0;
453 }
454
455 static void __saa7134_ir_stop(void *priv)
456 {
457         struct saa7134_dev *dev = priv;
458         struct card_ir *ir;
459
460         if (!dev)
461                 return;
462
463         ir  = dev->remote;
464         if (!ir)
465                 return;
466
467         if (!ir->running)
468                 return;
469         if (dev->remote->polling)
470                 del_timer_sync(&dev->remote->timer);
471         else if (ir->raw_decode) {
472                 del_timer_sync(&ir->timer_end);
473                 ir->active = 0;
474         }
475
476         ir->running = 0;
477
478         return;
479 }
480
481 int saa7134_ir_start(struct saa7134_dev *dev)
482 {
483         if (dev->remote->users)
484                 return __saa7134_ir_start(dev);
485
486         return 0;
487 }
488
489 void saa7134_ir_stop(struct saa7134_dev *dev)
490 {
491         if (dev->remote->users)
492                 __saa7134_ir_stop(dev);
493 }
494
495 static int saa7134_ir_open(struct rc_dev *rc)
496 {
497         struct saa7134_dev *dev = rc->priv;
498
499         dev->remote->users++;
500         return __saa7134_ir_start(dev);
501 }
502
503 static void saa7134_ir_close(struct rc_dev *rc)
504 {
505         struct saa7134_dev *dev = rc->priv;
506
507         dev->remote->users--;
508         if (!dev->remote->users)
509                 __saa7134_ir_stop(dev);
510 }
511
512 int saa7134_input_init1(struct saa7134_dev *dev)
513 {
514         struct card_ir *ir;
515         struct rc_dev *rc;
516         char *ir_codes = NULL;
517         u32 mask_keycode = 0;
518         u32 mask_keydown = 0;
519         u32 mask_keyup   = 0;
520         int polling      = 0;
521         int raw_decode   = 0;
522         int err;
523
524         if (dev->has_remote != SAA7134_REMOTE_GPIO)
525                 return -ENODEV;
526         if (disable_ir)
527                 return -ENODEV;
528
529         /* detect & configure */
530         switch (dev->board) {
531         case SAA7134_BOARD_FLYVIDEO2000:
532         case SAA7134_BOARD_FLYVIDEO3000:
533         case SAA7134_BOARD_FLYTVPLATINUM_FM:
534         case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
535         case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
536                 ir_codes     = RC_MAP_FLYVIDEO;
537                 mask_keycode = 0xEC00000;
538                 mask_keydown = 0x0040000;
539                 break;
540         case SAA7134_BOARD_CINERGY400:
541         case SAA7134_BOARD_CINERGY600:
542         case SAA7134_BOARD_CINERGY600_MK3:
543                 ir_codes     = RC_MAP_CINERGY;
544                 mask_keycode = 0x00003f;
545                 mask_keyup   = 0x040000;
546                 break;
547         case SAA7134_BOARD_ECS_TVP3XP:
548         case SAA7134_BOARD_ECS_TVP3XP_4CB5:
549                 ir_codes     = RC_MAP_EZTV;
550                 mask_keycode = 0x00017c;
551                 mask_keyup   = 0x000002;
552                 polling      = 50; // ms
553                 break;
554         case SAA7134_BOARD_KWORLD_XPERT:
555         case SAA7134_BOARD_AVACSSMARTTV:
556                 ir_codes     = RC_MAP_PIXELVIEW;
557                 mask_keycode = 0x00001F;
558                 mask_keyup   = 0x000020;
559                 polling      = 50; // ms
560                 break;
561         case SAA7134_BOARD_MD2819:
562         case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
563         case SAA7134_BOARD_AVERMEDIA_305:
564         case SAA7134_BOARD_AVERMEDIA_307:
565         case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
566         case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
567         case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
568         case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
569         case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
570         case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
571         case SAA7134_BOARD_AVERMEDIA_M102:
572         case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
573                 ir_codes     = RC_MAP_AVERMEDIA;
574                 mask_keycode = 0x0007C8;
575                 mask_keydown = 0x000010;
576                 polling      = 50; // ms
577                 /* Set GPIO pin2 to high to enable the IR controller */
578                 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
579                 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
580                 break;
581         case SAA7134_BOARD_AVERMEDIA_M135A:
582                 ir_codes     = RC_MAP_AVERMEDIA_M135A;
583                 mask_keydown = 0x0040000;       /* Enable GPIO18 line on both edges */
584                 mask_keyup   = 0x0040000;
585                 mask_keycode = 0xffff;
586                 raw_decode   = 1;
587                 break;
588         case SAA7134_BOARD_AVERMEDIA_M733A:
589                 ir_codes     = RC_MAP_AVERMEDIA_M733A_RM_K6;
590                 mask_keydown = 0x0040000;
591                 mask_keyup   = 0x0040000;
592                 mask_keycode = 0xffff;
593                 raw_decode   = 1;
594                 break;
595         case SAA7134_BOARD_AVERMEDIA_777:
596         case SAA7134_BOARD_AVERMEDIA_A16AR:
597                 ir_codes     = RC_MAP_AVERMEDIA;
598                 mask_keycode = 0x02F200;
599                 mask_keydown = 0x000400;
600                 polling      = 50; // ms
601                 /* Without this we won't receive key up events */
602                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
603                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
604                 break;
605         case SAA7134_BOARD_AVERMEDIA_A16D:
606                 ir_codes     = RC_MAP_AVERMEDIA_A16D;
607                 mask_keycode = 0x02F200;
608                 mask_keydown = 0x000400;
609                 polling      = 50; /* ms */
610                 /* Without this we won't receive key up events */
611                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
612                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
613                 break;
614         case SAA7134_BOARD_KWORLD_TERMINATOR:
615                 ir_codes     = RC_MAP_PIXELVIEW;
616                 mask_keycode = 0x00001f;
617                 mask_keyup   = 0x000060;
618                 polling      = 50; // ms
619                 break;
620         case SAA7134_BOARD_MANLI_MTV001:
621         case SAA7134_BOARD_MANLI_MTV002:
622                 ir_codes     = RC_MAP_MANLI;
623                 mask_keycode = 0x001f00;
624                 mask_keyup   = 0x004000;
625                 polling      = 50; /* ms */
626                 break;
627         case SAA7134_BOARD_BEHOLD_409FM:
628         case SAA7134_BOARD_BEHOLD_401:
629         case SAA7134_BOARD_BEHOLD_403:
630         case SAA7134_BOARD_BEHOLD_403FM:
631         case SAA7134_BOARD_BEHOLD_405:
632         case SAA7134_BOARD_BEHOLD_405FM:
633         case SAA7134_BOARD_BEHOLD_407:
634         case SAA7134_BOARD_BEHOLD_407FM:
635         case SAA7134_BOARD_BEHOLD_409:
636         case SAA7134_BOARD_BEHOLD_505FM:
637         case SAA7134_BOARD_BEHOLD_505RDS_MK5:
638         case SAA7134_BOARD_BEHOLD_505RDS_MK3:
639         case SAA7134_BOARD_BEHOLD_507_9FM:
640         case SAA7134_BOARD_BEHOLD_507RDS_MK3:
641         case SAA7134_BOARD_BEHOLD_507RDS_MK5:
642                 ir_codes     = RC_MAP_MANLI;
643                 mask_keycode = 0x003f00;
644                 mask_keyup   = 0x004000;
645                 polling      = 50; /* ms */
646                 break;
647         case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
648                 ir_codes     = RC_MAP_BEHOLD_COLUMBUS;
649                 mask_keycode = 0x003f00;
650                 mask_keyup   = 0x004000;
651                 polling      = 50; // ms
652                 break;
653         case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
654                 ir_codes     = RC_MAP_PCTV_SEDNA;
655                 mask_keycode = 0x001f00;
656                 mask_keyup   = 0x004000;
657                 polling      = 50; // ms
658                 break;
659         case SAA7134_BOARD_GOTVIEW_7135:
660                 ir_codes     = RC_MAP_GOTVIEW7135;
661                 mask_keycode = 0x0003CC;
662                 mask_keydown = 0x000010;
663                 polling      = 5; /* ms */
664                 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
665                 break;
666         case SAA7134_BOARD_VIDEOMATE_TV_PVR:
667         case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
668         case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
669                 ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
670                 mask_keycode = 0x00003F;
671                 mask_keyup   = 0x400000;
672                 polling      = 50; // ms
673                 break;
674         case SAA7134_BOARD_PROTEUS_2309:
675                 ir_codes     = RC_MAP_PROTEUS_2309;
676                 mask_keycode = 0x00007F;
677                 mask_keyup   = 0x000080;
678                 polling      = 50; // ms
679                 break;
680         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
681         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
682                 ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
683                 mask_keycode = 0x003F00;
684                 mask_keyup   = 0x040000;
685                 break;
686         case SAA7134_BOARD_FLYDVBS_LR300:
687         case SAA7134_BOARD_FLYDVBT_LR301:
688         case SAA7134_BOARD_FLYDVBTDUO:
689                 ir_codes     = RC_MAP_FLYDVB;
690                 mask_keycode = 0x0001F00;
691                 mask_keydown = 0x0040000;
692                 break;
693         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
694         case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
695         case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
696                 ir_codes     = RC_MAP_ASUS_PC39;
697                 mask_keydown = 0x0040000;       /* Enable GPIO18 line on both edges */
698                 mask_keyup   = 0x0040000;
699                 mask_keycode = 0xffff;
700                 raw_decode   = 1;
701                 break;
702         case SAA7134_BOARD_ENCORE_ENLTV:
703         case SAA7134_BOARD_ENCORE_ENLTV_FM:
704                 ir_codes     = RC_MAP_ENCORE_ENLTV;
705                 mask_keycode = 0x00007f;
706                 mask_keyup   = 0x040000;
707                 polling      = 50; // ms
708                 break;
709         case SAA7134_BOARD_ENCORE_ENLTV_FM53:
710                 ir_codes     = RC_MAP_ENCORE_ENLTV_FM53;
711                 mask_keydown = 0x0040000;       /* Enable GPIO18 line on both edges */
712                 mask_keyup   = 0x0040000;
713                 mask_keycode = 0xffff;
714                 raw_decode   = 1;
715                 break;
716         case SAA7134_BOARD_10MOONSTVMASTER3:
717                 ir_codes     = RC_MAP_ENCORE_ENLTV;
718                 mask_keycode = 0x5f80000;
719                 mask_keyup   = 0x8000000;
720                 polling      = 50; //ms
721                 break;
722         case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
723                 ir_codes     = RC_MAP_GENIUS_TVGO_A11MCE;
724                 mask_keycode = 0xff;
725                 mask_keydown = 0xf00000;
726                 polling = 50; /* ms */
727                 break;
728         case SAA7134_BOARD_REAL_ANGEL_220:
729                 ir_codes     = RC_MAP_REAL_AUDIO_220_32_KEYS;
730                 mask_keycode = 0x3f00;
731                 mask_keyup   = 0x4000;
732                 polling = 50; /* ms */
733                 break;
734         case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
735                 ir_codes     = RC_MAP_KWORLD_PLUS_TV_ANALOG;
736                 mask_keycode = 0x7f;
737                 polling = 40; /* ms */
738                 break;
739         case SAA7134_BOARD_VIDEOMATE_S350:
740                 ir_codes     = RC_MAP_VIDEOMATE_S350;
741                 mask_keycode = 0x003f00;
742                 mask_keydown = 0x040000;
743                 break;
744         case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
745                 ir_codes     = RC_MAP_WINFAST;
746                 mask_keycode = 0x5f00;
747                 mask_keyup   = 0x020000;
748                 polling      = 50; /* ms */
749                 break;
750         }
751         if (NULL == ir_codes) {
752                 printk("%s: Oops: IR config error [card=%d]\n",
753                        dev->name, dev->board);
754                 return -ENODEV;
755         }
756
757         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
758         rc = rc_allocate_device();
759         if (!ir || !rc) {
760                 err = -ENOMEM;
761                 goto err_out_free;
762         }
763
764         ir->dev = rc;
765         dev->remote = ir;
766
767         ir->running = 0;
768
769         /* init hardware-specific stuff */
770         ir->mask_keycode = mask_keycode;
771         ir->mask_keydown = mask_keydown;
772         ir->mask_keyup   = mask_keyup;
773         ir->polling      = polling;
774         ir->raw_decode   = raw_decode;
775
776         /* init input device */
777         snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
778                  saa7134_boards[dev->board].name);
779         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
780                  pci_name(dev->pci));
781
782         rc->priv = dev;
783         rc->open = saa7134_ir_open;
784         rc->close = saa7134_ir_close;
785         if (raw_decode)
786                 rc->driver_type = RC_DRIVER_IR_RAW;
787
788         rc->input_name = ir->name;
789         rc->input_phys = ir->phys;
790         rc->input_id.bustype = BUS_PCI;
791         rc->input_id.version = 1;
792         if (dev->pci->subsystem_vendor) {
793                 rc->input_id.vendor  = dev->pci->subsystem_vendor;
794                 rc->input_id.product = dev->pci->subsystem_device;
795         } else {
796                 rc->input_id.vendor  = dev->pci->vendor;
797                 rc->input_id.product = dev->pci->device;
798         }
799         rc->dev.parent = &dev->pci->dev;
800         rc->map_name = ir_codes;
801         rc->driver_name = MODULE_NAME;
802
803         err = rc_register_device(rc);
804         if (err)
805                 goto err_out_free;
806
807         return 0;
808
809 err_out_free:
810         rc_free_device(rc);
811         dev->remote = NULL;
812         kfree(ir);
813         return err;
814 }
815
816 void saa7134_input_fini(struct saa7134_dev *dev)
817 {
818         if (NULL == dev->remote)
819                 return;
820
821         saa7134_ir_stop(dev);
822         rc_unregister_device(dev->remote->dev);
823         kfree(dev->remote);
824         dev->remote = NULL;
825 }
826
827 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
828 {
829         struct i2c_board_info info;
830
831         struct i2c_msg msg_msi = {
832                 .addr = 0x50,
833                 .flags = I2C_M_RD,
834                 .len = 0,
835                 .buf = NULL,
836         };
837
838         int rc;
839
840         if (disable_ir) {
841                 dprintk("IR has been disabled, not probing for i2c remote\n");
842                 return;
843         }
844
845         memset(&info, 0, sizeof(struct i2c_board_info));
846         memset(&dev->init_data, 0, sizeof(dev->init_data));
847         strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
848
849         switch (dev->board) {
850         case SAA7134_BOARD_PINNACLE_PCTV_110i:
851         case SAA7134_BOARD_PINNACLE_PCTV_310i:
852                 dev->init_data.name = "Pinnacle PCTV";
853                 if (pinnacle_remote == 0) {
854                         dev->init_data.get_key = get_key_pinnacle_color;
855                         dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
856                         info.addr = 0x47;
857                 } else {
858                         dev->init_data.get_key = get_key_pinnacle_grey;
859                         dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
860                         info.addr = 0x47;
861                 }
862                 break;
863         case SAA7134_BOARD_UPMOST_PURPLE_TV:
864                 dev->init_data.name = "Purple TV";
865                 dev->init_data.get_key = get_key_purpletv;
866                 dev->init_data.ir_codes = RC_MAP_PURPLETV;
867                 info.addr = 0x7a;
868                 break;
869         case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
870                 dev->init_data.name = "MSI TV@nywhere Plus";
871                 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
872                 dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
873                 /*
874                  * MSI TV@nyware Plus requires more frequent polling
875                  * otherwise it will miss some keypresses
876                  */
877                 dev->init_data.polling_interval = 50;
878                 info.addr = 0x30;
879                 /* MSI TV@nywhere Plus controller doesn't seem to
880                    respond to probes unless we read something from
881                    an existing device. Weird...
882                    REVISIT: might no longer be needed */
883                 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
884                 dprintk("probe 0x%02x @ %s: %s\n",
885                         msg_msi.addr, dev->i2c_adap.name,
886                         (1 == rc) ? "yes" : "no");
887                 break;
888         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
889                 dev->init_data.name = "HVR 1110";
890                 dev->init_data.get_key = get_key_hvr1110;
891                 dev->init_data.ir_codes = RC_MAP_HAUPPAUGE_NEW;
892                 info.addr = 0x71;
893                 break;
894         case SAA7134_BOARD_BEHOLD_607FM_MK3:
895         case SAA7134_BOARD_BEHOLD_607FM_MK5:
896         case SAA7134_BOARD_BEHOLD_609FM_MK3:
897         case SAA7134_BOARD_BEHOLD_609FM_MK5:
898         case SAA7134_BOARD_BEHOLD_607RDS_MK3:
899         case SAA7134_BOARD_BEHOLD_607RDS_MK5:
900         case SAA7134_BOARD_BEHOLD_609RDS_MK3:
901         case SAA7134_BOARD_BEHOLD_609RDS_MK5:
902         case SAA7134_BOARD_BEHOLD_M6:
903         case SAA7134_BOARD_BEHOLD_M63:
904         case SAA7134_BOARD_BEHOLD_M6_EXTRA:
905         case SAA7134_BOARD_BEHOLD_H6:
906         case SAA7134_BOARD_BEHOLD_X7:
907         case SAA7134_BOARD_BEHOLD_H7:
908         case SAA7134_BOARD_BEHOLD_A7:
909                 dev->init_data.name = "BeholdTV";
910                 dev->init_data.get_key = get_key_beholdm6xx;
911                 dev->init_data.ir_codes = RC_MAP_BEHOLD;
912                 dev->init_data.type = IR_TYPE_NEC;
913                 info.addr = 0x2d;
914                 break;
915         case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
916         case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
917                 info.addr = 0x40;
918                 break;
919         case SAA7134_BOARD_FLYDVB_TRIO:
920                 dev->init_data.name = "FlyDVB Trio";
921                 dev->init_data.get_key = get_key_flydvb_trio;
922                 dev->init_data.ir_codes = RC_MAP_FLYDVB;
923                 info.addr = 0x0b;
924                 break;
925         default:
926                 dprintk("No I2C IR support for board %x\n", dev->board);
927                 return;
928         }
929
930         if (dev->init_data.name)
931                 info.platform_data = &dev->init_data;
932         i2c_new_device(&dev->i2c_adap, &info);
933 }
934
935 static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
936 {
937         struct card_ir  *ir = dev->remote;
938         unsigned long   timeout;
939         int space;
940
941         /* Generate initial event */
942         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
943         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
944         space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
945         ir_raw_event_store_edge(dev->remote->dev, space ? IR_SPACE : IR_PULSE);
946
947
948         /*
949          * Wait 15 ms from the start of the first IR event before processing
950          * the event. This time is enough for NEC protocol. May need adjustments
951          * to work with other protocols.
952          */
953         if (!ir->active) {
954                 timeout = jiffies + jiffies_to_msecs(15);
955                 mod_timer(&ir->timer_end, timeout);
956                 ir->active = 1;
957         }
958
959         return 1;
960 }