linux/drivers/media/pci/bt8xx/bttv-input.c
<<
>>
Prefs
   1/*
   2 *
   3 * Copyright (c) 2003 Gerd Knorr
   4 * Copyright (c) 2003 Pavel Machek
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or
   9 * (at your option) any later version.
  10 *
  11 * This program is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 */
  16
  17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18
  19#include <linux/module.h>
  20#include <linux/init.h>
  21#include <linux/delay.h>
  22#include <linux/interrupt.h>
  23#include <linux/input.h>
  24#include <linux/slab.h>
  25
  26#include "bttv.h"
  27#include "bttvp.h"
  28
  29
  30static int ir_debug;
  31module_param(ir_debug, int, 0644);
  32
  33static int ir_rc5_remote_gap = 885;
  34module_param(ir_rc5_remote_gap, int, 0644);
  35
  36#undef dprintk
  37#define dprintk(fmt, ...)                       \
  38do {                                            \
  39        if (ir_debug >= 1)                      \
  40                pr_info(fmt, ##__VA_ARGS__);    \
  41} while (0)
  42
  43#define DEVNAME "bttv-input"
  44
  45#define MODULE_NAME "bttv"
  46
  47/* ---------------------------------------------------------------------- */
  48
  49static void ir_handle_key(struct bttv *btv)
  50{
  51        struct bttv_ir *ir = btv->remote;
  52        u32 gpio,data;
  53
  54        /* read gpio value */
  55        gpio = bttv_gpio_read(&btv->c);
  56        if (ir->polling) {
  57                if (ir->last_gpio == gpio)
  58                        return;
  59                ir->last_gpio = gpio;
  60        }
  61
  62        /* extract data */
  63        data = ir_extract_bits(gpio, ir->mask_keycode);
  64        dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
  65                gpio, data,
  66                ir->polling               ? "poll"  : "irq",
  67                (gpio & ir->mask_keydown) ? " down" : "",
  68                (gpio & ir->mask_keyup)   ? " up"   : "");
  69
  70        if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
  71            (ir->mask_keyup   && !(gpio & ir->mask_keyup))) {
  72                rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
  73        } else {
  74                /* HACK: Probably, ir->mask_keydown is missing
  75                   for this board */
  76                if (btv->c.type == BTTV_BOARD_WINFAST2000)
  77                        rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
  78                                             0);
  79
  80                rc_keyup(ir->dev);
  81        }
  82}
  83
  84static void ir_enltv_handle_key(struct bttv *btv)
  85{
  86        struct bttv_ir *ir = btv->remote;
  87        u32 gpio, data, keyup;
  88
  89        /* read gpio value */
  90        gpio = bttv_gpio_read(&btv->c);
  91
  92        /* extract data */
  93        data = ir_extract_bits(gpio, ir->mask_keycode);
  94
  95        /* Check if it is keyup */
  96        keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
  97
  98        if ((ir->last_gpio & 0x7f) != data) {
  99                dprintk("gpio=0x%x code=%d | %s\n",
 100                        gpio, data,
 101                        (gpio & ir->mask_keyup) ? " up" : "up/down");
 102
 103                rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
 104                if (keyup)
 105                        rc_keyup(ir->dev);
 106        } else {
 107                if ((ir->last_gpio & 1 << 31) == keyup)
 108                        return;
 109
 110                dprintk("(cnt) gpio=0x%x code=%d | %s\n",
 111                        gpio, data,
 112                        (gpio & ir->mask_keyup) ? " up" : "down");
 113
 114                if (keyup)
 115                        rc_keyup(ir->dev);
 116                else
 117                        rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
 118                                             0);
 119        }
 120
 121        ir->last_gpio = data | keyup;
 122}
 123
 124static int bttv_rc5_irq(struct bttv *btv);
 125
 126void bttv_input_irq(struct bttv *btv)
 127{
 128        struct bttv_ir *ir = btv->remote;
 129
 130        if (ir->rc5_gpio)
 131                bttv_rc5_irq(btv);
 132        else if (!ir->polling)
 133                ir_handle_key(btv);
 134}
 135
 136static void bttv_input_timer(struct timer_list *t)
 137{
 138        struct bttv_ir *ir = from_timer(ir, t, timer);
 139        struct bttv *btv = ir->btv;
 140
 141        if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
 142                ir_enltv_handle_key(btv);
 143        else
 144                ir_handle_key(btv);
 145        mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
 146}
 147
 148/*
 149 * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
 150 * on the rc-core way. As we need to be sure that both IRQ transitions are
 151 * properly triggered, Better to touch it only with this hardware for
 152 * testing.
 153 */
 154
 155#define RC5_START(x)    (((x) >> 12) & 0x03)
 156#define RC5_TOGGLE(x)   (((x) >> 11) & 0x01)
 157#define RC5_ADDR(x)     (((x) >> 6)  & 0x1f)
 158#define RC5_INSTR(x)    (((x) >> 0)  & 0x3f)
 159
 160/* decode raw bit pattern to RC5 code */
 161static u32 bttv_rc5_decode(unsigned int code)
 162{
 163        unsigned int org_code = code;
 164        unsigned int pair;
 165        unsigned int rc5 = 0;
 166        int i;
 167
 168        for (i = 0; i < 14; ++i) {
 169                pair = code & 0x3;
 170                code >>= 2;
 171
 172                rc5 <<= 1;
 173                switch (pair) {
 174                case 0:
 175                case 2:
 176                        break;
 177                case 1:
 178                        rc5 |= 1;
 179                break;
 180                case 3:
 181                        dprintk("rc5_decode(%x) bad code\n",
 182                                org_code);
 183                        return 0;
 184                }
 185        }
 186        dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, instr=%x\n",
 187                rc5, org_code, RC5_START(rc5),
 188                RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
 189        return rc5;
 190}
 191
 192static void bttv_rc5_timer_end(struct timer_list *t)
 193{
 194        struct bttv_ir *ir = from_timer(ir, t, timer);
 195        ktime_t tv;
 196        u32 gap, rc5, scancode;
 197        u8 toggle, command, system;
 198
 199        /* get time */
 200        tv = ktime_get();
 201
 202        gap = ktime_to_us(ktime_sub(tv, ir->base_time));
 203        /* avoid overflow with gap >1s */
 204        if (gap > USEC_PER_SEC) {
 205                gap = 200000;
 206        }
 207        /* signal we're ready to start a new code */
 208        ir->active = false;
 209
 210        /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
 211        if (gap < 28000) {
 212                dprintk("spurious timer_end\n");
 213                return;
 214        }
 215
 216        if (ir->last_bit < 20) {
 217                /* ignore spurious codes (caused by light/other remotes) */
 218                dprintk("short code: %x\n", ir->code);
 219                return;
 220        }
 221
 222        ir->code = (ir->code << ir->shift_by) | 1;
 223        rc5 = bttv_rc5_decode(ir->code);
 224
 225        toggle = RC5_TOGGLE(rc5);
 226        system = RC5_ADDR(rc5);
 227        command = RC5_INSTR(rc5);
 228
 229        switch (RC5_START(rc5)) {
 230        case 0x3:
 231                break;
 232        case 0x2:
 233                command += 0x40;
 234                break;
 235        default:
 236                return;
 237        }
 238
 239        scancode = RC_SCANCODE_RC5(system, command);
 240        rc_keydown(ir->dev, RC_PROTO_RC5, scancode, toggle);
 241        dprintk("scancode %x, toggle %x\n", scancode, toggle);
 242}
 243
 244static int bttv_rc5_irq(struct bttv *btv)
 245{
 246        struct bttv_ir *ir = btv->remote;
 247        ktime_t tv;
 248        u32 gpio;
 249        u32 gap;
 250        unsigned long current_jiffies;
 251
 252        /* read gpio port */
 253        gpio = bttv_gpio_read(&btv->c);
 254
 255        /* get time of bit */
 256        current_jiffies = jiffies;
 257        tv = ktime_get();
 258
 259        gap = ktime_to_us(ktime_sub(tv, ir->base_time));
 260        /* avoid overflow with gap >1s */
 261        if (gap > USEC_PER_SEC) {
 262                gap = 200000;
 263        }
 264
 265        dprintk("RC5 IRQ: gap %d us for %s\n",
 266                gap, (gpio & 0x20) ? "mark" : "space");
 267
 268        /* remote IRQ? */
 269        if (!(gpio & 0x20))
 270                return 0;
 271
 272        /* active code => add bit */
 273        if (ir->active) {
 274                /* only if in the code (otherwise spurious IRQ or timer
 275                   late) */
 276                if (ir->last_bit < 28) {
 277                        ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
 278                            ir_rc5_remote_gap;
 279                        ir->code |= 1 << ir->last_bit;
 280                }
 281                /* starting new code */
 282        } else {
 283                ir->active = true;
 284                ir->code = 0;
 285                ir->base_time = tv;
 286                ir->last_bit = 0;
 287
 288                mod_timer(&ir->timer, current_jiffies + msecs_to_jiffies(30));
 289        }
 290
 291        /* toggle GPIO pin 4 to reset the irq */
 292        bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
 293        bttv_gpio_write(&btv->c, gpio | (1 << 4));
 294        return 1;
 295}
 296
 297/* ---------------------------------------------------------------------- */
 298
 299static void bttv_ir_start(struct bttv_ir *ir)
 300{
 301        if (ir->polling) {
 302                timer_setup(&ir->timer, bttv_input_timer, 0);
 303                ir->timer.expires  = jiffies + msecs_to_jiffies(1000);
 304                add_timer(&ir->timer);
 305        } else if (ir->rc5_gpio) {
 306                /* set timer_end for code completion */
 307                timer_setup(&ir->timer, bttv_rc5_timer_end, 0);
 308                ir->shift_by = 1;
 309                ir->rc5_remote_gap = ir_rc5_remote_gap;
 310        }
 311}
 312
 313static void bttv_ir_stop(struct bttv *btv)
 314{
 315        if (btv->remote->polling)
 316                del_timer_sync(&btv->remote->timer);
 317
 318        if (btv->remote->rc5_gpio) {
 319                u32 gpio;
 320
 321                del_timer_sync(&btv->remote->timer);
 322
 323                gpio = bttv_gpio_read(&btv->c);
 324                bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
 325        }
 326}
 327
 328/*
 329 * Get_key functions used by I2C remotes
 330 */
 331
 332static int get_key_pv951(struct IR_i2c *ir, enum rc_proto *protocol,
 333                         u32 *scancode, u8 *toggle)
 334{
 335        unsigned char b;
 336
 337        /* poll IR chip */
 338        if (1 != i2c_master_recv(ir->c, &b, 1)) {
 339                dprintk("read error\n");
 340                return -EIO;
 341        }
 342
 343        /* ignore 0xaa */
 344        if (b==0xaa)
 345                return 0;
 346        dprintk("key %02x\n", b);
 347
 348        /*
 349         * NOTE:
 350         * lirc_i2c maps the pv951 code as:
 351         *      addr = 0x61D6
 352         *      cmd = bit_reverse (b)
 353         * So, it seems that this device uses NEC extended
 354         * I decided to not fix the table, due to two reasons:
 355         *      1) Without the actual device, this is only a guess;
 356         *      2) As the addr is not reported via I2C, nor can be changed,
 357         *         the device is bound to the vendor-provided RC.
 358         */
 359
 360        *protocol = RC_PROTO_UNKNOWN;
 361        *scancode = b;
 362        *toggle = 0;
 363        return 1;
 364}
 365
 366/* Instantiate the I2C IR receiver device, if present */
 367void init_bttv_i2c_ir(struct bttv *btv)
 368{
 369        const unsigned short addr_list[] = {
 370                0x1a, 0x18, 0x64, 0x30, 0x71,
 371                I2C_CLIENT_END
 372        };
 373        struct i2c_board_info info;
 374        struct i2c_client *i2c_dev;
 375
 376        if (0 != btv->i2c_rc)
 377                return;
 378
 379        memset(&info, 0, sizeof(struct i2c_board_info));
 380        memset(&btv->init_data, 0, sizeof(btv->init_data));
 381        strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
 382
 383        switch (btv->c.type) {
 384        case BTTV_BOARD_PV951:
 385                btv->init_data.name = "PV951";
 386                btv->init_data.get_key = get_key_pv951;
 387                btv->init_data.ir_codes = RC_MAP_PV951;
 388                info.addr = 0x4b;
 389                break;
 390        }
 391
 392        if (btv->init_data.name) {
 393                info.platform_data = &btv->init_data;
 394                i2c_dev = i2c_new_device(&btv->c.i2c_adap, &info);
 395        } else {
 396                /*
 397                 * The external IR receiver is at i2c address 0x34 (0x35 for
 398                 * reads).  Future Hauppauge cards will have an internal
 399                 * receiver at 0x30 (0x31 for reads).  In theory, both can be
 400                 * fitted, and Hauppauge suggest an external overrides an
 401                 * internal.
 402                 * That's why we probe 0x1a (~0x34) first. CB
 403                 */
 404                i2c_dev = i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
 405        }
 406        if (NULL == i2c_dev)
 407                return;
 408
 409#if defined(CONFIG_MODULES) && defined(MODULE)
 410        request_module("ir-kbd-i2c");
 411#endif
 412}
 413
 414int bttv_input_init(struct bttv *btv)
 415{
 416        struct bttv_ir *ir;
 417        char *ir_codes = NULL;
 418        struct rc_dev *rc;
 419        int err = -ENOMEM;
 420
 421        if (!btv->has_remote)
 422                return -ENODEV;
 423
 424        ir = kzalloc(sizeof(*ir),GFP_KERNEL);
 425        rc = rc_allocate_device(RC_DRIVER_SCANCODE);
 426        if (!ir || !rc)
 427                goto err_out_free;
 428
 429        /* detect & configure */
 430        switch (btv->c.type) {
 431        case BTTV_BOARD_AVERMEDIA:
 432        case BTTV_BOARD_AVPHONE98:
 433        case BTTV_BOARD_AVERMEDIA98:
 434                ir_codes         = RC_MAP_AVERMEDIA;
 435                ir->mask_keycode = 0xf88000;
 436                ir->mask_keydown = 0x010000;
 437                ir->polling      = 50; // ms
 438                break;
 439
 440        case BTTV_BOARD_AVDVBT_761:
 441        case BTTV_BOARD_AVDVBT_771:
 442                ir_codes         = RC_MAP_AVERMEDIA_DVBT;
 443                ir->mask_keycode = 0x0f00c0;
 444                ir->mask_keydown = 0x000020;
 445                ir->polling      = 50; // ms
 446                break;
 447
 448        case BTTV_BOARD_PXELVWPLTVPAK:
 449                ir_codes         = RC_MAP_PIXELVIEW;
 450                ir->mask_keycode = 0x003e00;
 451                ir->mask_keyup   = 0x010000;
 452                ir->polling      = 50; // ms
 453                break;
 454        case BTTV_BOARD_PV_M4900:
 455        case BTTV_BOARD_PV_BT878P_9B:
 456        case BTTV_BOARD_PV_BT878P_PLUS:
 457                ir_codes         = RC_MAP_PIXELVIEW;
 458                ir->mask_keycode = 0x001f00;
 459                ir->mask_keyup   = 0x008000;
 460                ir->polling      = 50; // ms
 461                break;
 462
 463        case BTTV_BOARD_WINFAST2000:
 464                ir_codes         = RC_MAP_WINFAST;
 465                ir->mask_keycode = 0x1f8;
 466                break;
 467        case BTTV_BOARD_MAGICTVIEW061:
 468        case BTTV_BOARD_MAGICTVIEW063:
 469                ir_codes         = RC_MAP_WINFAST;
 470                ir->mask_keycode = 0x0008e000;
 471                ir->mask_keydown = 0x00200000;
 472                break;
 473        case BTTV_BOARD_APAC_VIEWCOMP:
 474                ir_codes         = RC_MAP_APAC_VIEWCOMP;
 475                ir->mask_keycode = 0x001f00;
 476                ir->mask_keyup   = 0x008000;
 477                ir->polling      = 50; // ms
 478                break;
 479        case BTTV_BOARD_ASKEY_CPH03X:
 480        case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
 481        case BTTV_BOARD_CONTVFMI:
 482        case BTTV_BOARD_KWORLD_VSTREAM_XPERT:
 483                ir_codes         = RC_MAP_PIXELVIEW;
 484                ir->mask_keycode = 0x001F00;
 485                ir->mask_keyup   = 0x006000;
 486                ir->polling      = 50; // ms
 487                break;
 488        case BTTV_BOARD_NEBULA_DIGITV:
 489                ir_codes         = RC_MAP_NEBULA;
 490                ir->rc5_gpio     = true;
 491                break;
 492        case BTTV_BOARD_MACHTV_MAGICTV:
 493                ir_codes         = RC_MAP_APAC_VIEWCOMP;
 494                ir->mask_keycode = 0x001F00;
 495                ir->mask_keyup   = 0x004000;
 496                ir->polling      = 50; /* ms */
 497                break;
 498        case BTTV_BOARD_KOZUMI_KTV_01C:
 499                ir_codes         = RC_MAP_PCTV_SEDNA;
 500                ir->mask_keycode = 0x001f00;
 501                ir->mask_keyup   = 0x006000;
 502                ir->polling      = 50; /* ms */
 503                break;
 504        case BTTV_BOARD_ENLTV_FM_2:
 505                ir_codes         = RC_MAP_ENCORE_ENLTV2;
 506                ir->mask_keycode = 0x00fd00;
 507                ir->mask_keyup   = 0x000080;
 508                ir->polling      = 1; /* ms */
 509                ir->last_gpio    = ir_extract_bits(bttv_gpio_read(&btv->c),
 510                                                   ir->mask_keycode);
 511                break;
 512        }
 513
 514        if (!ir_codes) {
 515                dprintk("Ooops: IR config error [card=%d]\n", btv->c.type);
 516                err = -ENODEV;
 517                goto err_out_free;
 518        }
 519
 520        if (ir->rc5_gpio) {
 521                u32 gpio;
 522                /* enable remote irq */
 523                bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
 524                gpio = bttv_gpio_read(&btv->c);
 525                bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
 526                bttv_gpio_write(&btv->c, gpio | (1 << 4));
 527        } else {
 528                /* init hardware-specific stuff */
 529                bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
 530        }
 531
 532        /* init input device */
 533        ir->dev = rc;
 534        ir->btv = btv;
 535
 536        snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
 537                 btv->c.type);
 538        snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
 539                 pci_name(btv->c.pci));
 540
 541        rc->device_name = ir->name;
 542        rc->input_phys = ir->phys;
 543        rc->input_id.bustype = BUS_PCI;
 544        rc->input_id.version = 1;
 545        if (btv->c.pci->subsystem_vendor) {
 546                rc->input_id.vendor  = btv->c.pci->subsystem_vendor;
 547                rc->input_id.product = btv->c.pci->subsystem_device;
 548        } else {
 549                rc->input_id.vendor  = btv->c.pci->vendor;
 550                rc->input_id.product = btv->c.pci->device;
 551        }
 552        rc->dev.parent = &btv->c.pci->dev;
 553        rc->map_name = ir_codes;
 554        rc->driver_name = MODULE_NAME;
 555
 556        btv->remote = ir;
 557        bttv_ir_start(ir);
 558
 559        /* all done */
 560        err = rc_register_device(rc);
 561        if (err)
 562                goto err_out_stop;
 563
 564        return 0;
 565
 566 err_out_stop:
 567        bttv_ir_stop(btv);
 568        btv->remote = NULL;
 569 err_out_free:
 570        rc_free_device(rc);
 571        kfree(ir);
 572        return err;
 573}
 574
 575void bttv_input_fini(struct bttv *btv)
 576{
 577        if (btv->remote == NULL)
 578                return;
 579
 580        bttv_ir_stop(btv);
 581        rc_unregister_device(btv->remote->dev);
 582        kfree(btv->remote);
 583        btv->remote = NULL;
 584}
 585