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