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