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