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