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