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