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