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