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