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