linux/drivers/media/i2c/ir-kbd-i2c.c
<<
>>
Prefs
   1/*
   2 *
   3 * keyboard input driver for i2c IR remote controls
   4 *
   5 * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org>
   6 * modified for PixelView (BT878P+W/FM) by
   7 *      Michal Kochanowicz <mkochano@pld.org.pl>
   8 *      Christoph Bartelmus <lirc@bartelmus.de>
   9 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
  10 *      Ulrich Mueller <ulrich.mueller42@web.de>
  11 * modified for em2820 based USB TV tuners by
  12 *      Markus Rechberger <mrechberger@gmail.com>
  13 * modified for DViCO Fusion HDTV 5 RT GOLD by
  14 *      Chaogui Zhang <czhang1974@gmail.com>
  15 * modified for MSI TV@nywhere Plus by
  16 *      Henry Wong <henry@stuffedcow.net>
  17 *      Mark Schultz <n9xmj@yahoo.com>
  18 *      Brian Rogers <brian_rogers@comcast.net>
  19 * modified for AVerMedia Cardbus by
  20 *      Oldrich Jedlicka <oldium.pro@seznam.cz>
  21 *
  22 *  This program is free software; you can redistribute it and/or modify
  23 *  it under the terms of the GNU General Public License as published by
  24 *  the Free Software Foundation; either version 2 of the License, or
  25 *  (at your option) any later version.
  26 *
  27 *  This program is distributed in the hope that it will be useful,
  28 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  29 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30 *  GNU General Public License for more details.
  31 *
  32 */
  33
  34#include <asm/unaligned.h>
  35#include <linux/module.h>
  36#include <linux/init.h>
  37#include <linux/kernel.h>
  38#include <linux/string.h>
  39#include <linux/timer.h>
  40#include <linux/delay.h>
  41#include <linux/errno.h>
  42#include <linux/slab.h>
  43#include <linux/i2c.h>
  44#include <linux/workqueue.h>
  45
  46#include <media/rc-core.h>
  47#include <media/i2c/ir-kbd-i2c.h>
  48
  49/* ----------------------------------------------------------------------- */
  50/* insmod parameters                                                       */
  51
  52static int debug;
  53module_param(debug, int, 0644);    /* debug level (0,1,2) */
  54
  55
  56#define MODULE_NAME "ir-kbd-i2c"
  57#define dprintk(level, fmt, arg...)     if (debug >= level) \
  58        printk(KERN_DEBUG MODULE_NAME ": " fmt , ## arg)
  59
  60/* ----------------------------------------------------------------------- */
  61
  62static int get_key_haup_common(struct IR_i2c *ir, enum rc_type *protocol,
  63                                        u32 *scancode, u8 *ptoggle, int size)
  64{
  65        unsigned char buf[6];
  66        int start, range, toggle, dev, code, ircode, vendor;
  67
  68        /* poll IR chip */
  69        if (size != i2c_master_recv(ir->c, buf, size))
  70                return -EIO;
  71
  72        if (buf[0] & 0x80) {
  73                int offset = (size == 6) ? 3 : 0;
  74
  75                /* split rc5 data block ... */
  76                start  = (buf[offset] >> 7) &    1;
  77                range  = (buf[offset] >> 6) &    1;
  78                toggle = (buf[offset] >> 5) &    1;
  79                dev    =  buf[offset]       & 0x1f;
  80                code   = (buf[offset+1] >> 2) & 0x3f;
  81
  82                /* rc5 has two start bits
  83                 * the first bit must be one
  84                 * the second bit defines the command range:
  85                 * 1 = 0-63, 0 = 64 - 127
  86                 */
  87                if (!start)
  88                        /* no key pressed */
  89                        return 0;
  90
  91                /* filter out invalid key presses */
  92                ircode = (start << 12) | (toggle << 11) | (dev << 6) | code;
  93                if ((ircode & 0x1fff) == 0x1fff)
  94                        return 0;
  95
  96                if (!range)
  97                        code += 64;
  98
  99                dprintk(1, "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
 100                        start, range, toggle, dev, code);
 101
 102                *protocol = RC_TYPE_RC5;
 103                *scancode = RC_SCANCODE_RC5(dev, code);
 104                *ptoggle = toggle;
 105
 106                return 1;
 107        } else if (size == 6 && (buf[0] & 0x40)) {
 108                code = buf[4];
 109                dev = buf[3];
 110                vendor = get_unaligned_be16(buf + 1);
 111
 112                if (vendor == 0x800f) {
 113                        *ptoggle = (dev & 0x80) != 0;
 114                        *protocol = RC_TYPE_RC6_MCE;
 115                        dev &= 0x7f;
 116                        dprintk(1, "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n",
 117                                                *ptoggle, vendor, dev, code);
 118                } else {
 119                        *ptoggle = 0;
 120                        *protocol = RC_TYPE_RC6_6A_32;
 121                        dprintk(1, "ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n",
 122                                                        vendor, dev, code);
 123                }
 124
 125                *scancode = RC_SCANCODE_RC6_6A(vendor, dev, code);
 126
 127                return 1;
 128        }
 129
 130        return 0;
 131}
 132
 133static int get_key_haup(struct IR_i2c *ir, enum rc_type *protocol,
 134                        u32 *scancode, u8 *toggle)
 135{
 136        return get_key_haup_common(ir, protocol, scancode, toggle, 3);
 137}
 138
 139static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_type *protocol,
 140                            u32 *scancode, u8 *toggle)
 141{
 142        int ret;
 143        unsigned char buf[1] = { 0 };
 144
 145        /*
 146         * This is the same apparent "are you ready?" poll command observed
 147         * watching Windows driver traffic and implemented in lirc_zilog. With
 148         * this added, we get far saner remote behavior with z8 chips on usb
 149         * connected devices, even with the default polling interval of 100ms.
 150         */
 151        ret = i2c_master_send(ir->c, buf, 1);
 152        if (ret != 1)
 153                return (ret < 0) ? ret : -EINVAL;
 154
 155        return get_key_haup_common(ir, protocol, scancode, toggle, 6);
 156}
 157
 158static int get_key_pixelview(struct IR_i2c *ir, enum rc_type *protocol,
 159                             u32 *scancode, u8 *toggle)
 160{
 161        unsigned char b;
 162
 163        /* poll IR chip */
 164        if (1 != i2c_master_recv(ir->c, &b, 1)) {
 165                dprintk(1,"read error\n");
 166                return -EIO;
 167        }
 168
 169        *protocol = RC_TYPE_OTHER;
 170        *scancode = b;
 171        *toggle = 0;
 172        return 1;
 173}
 174
 175static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_type *protocol,
 176                              u32 *scancode, u8 *toggle)
 177{
 178        unsigned char buf[4];
 179
 180        /* poll IR chip */
 181        if (4 != i2c_master_recv(ir->c, buf, 4)) {
 182                dprintk(1,"read error\n");
 183                return -EIO;
 184        }
 185
 186        if(buf[0] !=0 || buf[1] !=0 || buf[2] !=0 || buf[3] != 0)
 187                dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,
 188                        buf[0], buf[1], buf[2], buf[3]);
 189
 190        /* no key pressed or signal from other ir remote */
 191        if(buf[0] != 0x1 ||  buf[1] != 0xfe)
 192                return 0;
 193
 194        *protocol = RC_TYPE_UNKNOWN;
 195        *scancode = buf[2];
 196        *toggle = 0;
 197        return 1;
 198}
 199
 200static int get_key_knc1(struct IR_i2c *ir, enum rc_type *protocol,
 201                        u32 *scancode, u8 *toggle)
 202{
 203        unsigned char b;
 204
 205        /* poll IR chip */
 206        if (1 != i2c_master_recv(ir->c, &b, 1)) {
 207                dprintk(1,"read error\n");
 208                return -EIO;
 209        }
 210
 211        /* it seems that 0xFE indicates that a button is still hold
 212           down, while 0xff indicates that no button is hold
 213           down. 0xfe sequences are sometimes interrupted by 0xFF */
 214
 215        dprintk(2,"key %02x\n", b);
 216
 217        if (b == 0xff)
 218                return 0;
 219
 220        if (b == 0xfe)
 221                /* keep old data */
 222                return 1;
 223
 224        *protocol = RC_TYPE_UNKNOWN;
 225        *scancode = b;
 226        *toggle = 0;
 227        return 1;
 228}
 229
 230static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_type *protocol,
 231                                     u32 *scancode, u8 *toggle)
 232{
 233        unsigned char subaddr, key, keygroup;
 234        struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
 235                                   .buf = &subaddr, .len = 1},
 236                                 { .addr = ir->c->addr, .flags = I2C_M_RD,
 237                                  .buf = &key, .len = 1} };
 238        subaddr = 0x0d;
 239        if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
 240                dprintk(1, "read error\n");
 241                return -EIO;
 242        }
 243
 244        if (key == 0xff)
 245                return 0;
 246
 247        subaddr = 0x0b;
 248        msg[1].buf = &keygroup;
 249        if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
 250                dprintk(1, "read error\n");
 251                return -EIO;
 252        }
 253
 254        if (keygroup == 0xff)
 255                return 0;
 256
 257        dprintk(1, "read key 0x%02x/0x%02x\n", key, keygroup);
 258        if (keygroup < 2 || keygroup > 4) {
 259                /* Only a warning */
 260                dprintk(1, "warning: invalid key group 0x%02x for key 0x%02x\n",
 261                                                                keygroup, key);
 262        }
 263        key |= (keygroup & 1) << 6;
 264
 265        *protocol = RC_TYPE_UNKNOWN;
 266        *scancode = key;
 267        if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */
 268                *scancode |= keygroup << 8;
 269        *toggle = 0;
 270        return 1;
 271}
 272
 273/* ----------------------------------------------------------------------- */
 274
 275static int ir_key_poll(struct IR_i2c *ir)
 276{
 277        enum rc_type protocol;
 278        u32 scancode;
 279        u8 toggle;
 280        int rc;
 281
 282        dprintk(3, "%s\n", __func__);
 283        rc = ir->get_key(ir, &protocol, &scancode, &toggle);
 284        if (rc < 0) {
 285                dprintk(2,"error\n");
 286                return rc;
 287        }
 288
 289        if (rc) {
 290                dprintk(1, "%s: proto = 0x%04x, scancode = 0x%08x\n",
 291                        __func__, protocol, scancode);
 292                rc_keydown(ir->rc, protocol, scancode, toggle);
 293        }
 294        return 0;
 295}
 296
 297static void ir_work(struct work_struct *work)
 298{
 299        int rc;
 300        struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
 301
 302        rc = ir_key_poll(ir);
 303        if (rc == -ENODEV) {
 304                rc_unregister_device(ir->rc);
 305                ir->rc = NULL;
 306                return;
 307        }
 308
 309        schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
 310}
 311
 312/* ----------------------------------------------------------------------- */
 313
 314static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
 315{
 316        char *ir_codes = NULL;
 317        const char *name = NULL;
 318        u64 rc_type = RC_BIT_UNKNOWN;
 319        struct IR_i2c *ir;
 320        struct rc_dev *rc = NULL;
 321        struct i2c_adapter *adap = client->adapter;
 322        unsigned short addr = client->addr;
 323        int err;
 324
 325        ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
 326        if (!ir)
 327                return -ENOMEM;
 328
 329        ir->c = client;
 330        ir->polling_interval = DEFAULT_POLLING_INTERVAL;
 331        i2c_set_clientdata(client, ir);
 332
 333        switch(addr) {
 334        case 0x64:
 335                name        = "Pixelview";
 336                ir->get_key = get_key_pixelview;
 337                rc_type     = RC_BIT_OTHER;
 338                ir_codes    = RC_MAP_EMPTY;
 339                break;
 340        case 0x18:
 341        case 0x1f:
 342        case 0x1a:
 343                name        = "Hauppauge";
 344                ir->get_key = get_key_haup;
 345                rc_type     = RC_BIT_RC5;
 346                ir_codes    = RC_MAP_HAUPPAUGE;
 347                break;
 348        case 0x30:
 349                name        = "KNC One";
 350                ir->get_key = get_key_knc1;
 351                rc_type     = RC_BIT_OTHER;
 352                ir_codes    = RC_MAP_EMPTY;
 353                break;
 354        case 0x6b:
 355                name        = "FusionHDTV";
 356                ir->get_key = get_key_fusionhdtv;
 357                rc_type     = RC_BIT_UNKNOWN;
 358                ir_codes    = RC_MAP_FUSIONHDTV_MCE;
 359                break;
 360        case 0x40:
 361                name        = "AVerMedia Cardbus remote";
 362                ir->get_key = get_key_avermedia_cardbus;
 363                rc_type     = RC_BIT_OTHER;
 364                ir_codes    = RC_MAP_AVERMEDIA_CARDBUS;
 365                break;
 366        case 0x41:
 367                name        = "AVerMedia EM78P153";
 368                ir->get_key = get_key_avermedia_cardbus;
 369                rc_type     = RC_BIT_OTHER;
 370                /* RM-KV remote, seems to be same as RM-K6 */
 371                ir_codes    = RC_MAP_AVERMEDIA_M733A_RM_K6;
 372                break;
 373        case 0x71:
 374                name        = "Hauppauge/Zilog Z8";
 375                ir->get_key = get_key_haup_xvr;
 376                rc_type     = RC_BIT_RC5 | RC_BIT_RC6_MCE | RC_BIT_RC6_6A_32;
 377                ir_codes    = RC_MAP_HAUPPAUGE;
 378                break;
 379        }
 380
 381        /* Let the caller override settings */
 382        if (client->dev.platform_data) {
 383                const struct IR_i2c_init_data *init_data =
 384                                                client->dev.platform_data;
 385
 386                ir_codes = init_data->ir_codes;
 387                rc = init_data->rc_dev;
 388
 389                name = init_data->name;
 390                if (init_data->type)
 391                        rc_type = init_data->type;
 392
 393                if (init_data->polling_interval)
 394                        ir->polling_interval = init_data->polling_interval;
 395
 396                switch (init_data->internal_get_key_func) {
 397                case IR_KBD_GET_KEY_CUSTOM:
 398                        /* The bridge driver provided us its own function */
 399                        ir->get_key = init_data->get_key;
 400                        break;
 401                case IR_KBD_GET_KEY_PIXELVIEW:
 402                        ir->get_key = get_key_pixelview;
 403                        break;
 404                case IR_KBD_GET_KEY_HAUP:
 405                        ir->get_key = get_key_haup;
 406                        break;
 407                case IR_KBD_GET_KEY_KNC1:
 408                        ir->get_key = get_key_knc1;
 409                        break;
 410                case IR_KBD_GET_KEY_FUSIONHDTV:
 411                        ir->get_key = get_key_fusionhdtv;
 412                        break;
 413                case IR_KBD_GET_KEY_HAUP_XVR:
 414                        ir->get_key = get_key_haup_xvr;
 415                        break;
 416                case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
 417                        ir->get_key = get_key_avermedia_cardbus;
 418                        break;
 419                }
 420        }
 421
 422        if (!rc) {
 423                /*
 424                 * If platform_data doesn't specify rc_dev, initialize it
 425                 * internally
 426                 */
 427                rc = rc_allocate_device(RC_DRIVER_SCANCODE);
 428                if (!rc)
 429                        return -ENOMEM;
 430        }
 431        ir->rc = rc;
 432
 433        /* Make sure we are all setup before going on */
 434        if (!name || !ir->get_key || !rc_type || !ir_codes) {
 435                dprintk(1, ": Unsupported device at address 0x%02x\n",
 436                        addr);
 437                err = -ENODEV;
 438                goto err_out_free;
 439        }
 440
 441        /* Sets name */
 442        snprintf(ir->name, sizeof(ir->name), "i2c IR (%s)", name);
 443        ir->ir_codes = ir_codes;
 444
 445        snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
 446                 dev_name(&adap->dev),
 447                 dev_name(&client->dev));
 448
 449        /*
 450         * Initialize input_dev fields
 451         * It doesn't make sense to allow overriding them via platform_data
 452         */
 453        rc->input_id.bustype = BUS_I2C;
 454        rc->input_phys       = ir->phys;
 455        rc->input_name       = ir->name;
 456
 457        /*
 458         * Initialize the other fields of rc_dev
 459         */
 460        rc->map_name       = ir->ir_codes;
 461        rc->allowed_protocols = rc_type;
 462        rc->enabled_protocols = rc_type;
 463        if (!rc->driver_name)
 464                rc->driver_name = MODULE_NAME;
 465
 466        err = rc_register_device(rc);
 467        if (err)
 468                goto err_out_free;
 469
 470        printk(MODULE_NAME ": %s detected at %s [%s]\n",
 471               ir->name, ir->phys, adap->name);
 472
 473        /* start polling via eventd */
 474        INIT_DELAYED_WORK(&ir->work, ir_work);
 475        schedule_delayed_work(&ir->work, 0);
 476
 477        return 0;
 478
 479 err_out_free:
 480        /* Only frees rc if it were allocated internally */
 481        rc_free_device(rc);
 482        return err;
 483}
 484
 485static int ir_remove(struct i2c_client *client)
 486{
 487        struct IR_i2c *ir = i2c_get_clientdata(client);
 488
 489        /* kill outstanding polls */
 490        cancel_delayed_work_sync(&ir->work);
 491
 492        /* unregister device */
 493        rc_unregister_device(ir->rc);
 494
 495        /* free memory */
 496        return 0;
 497}
 498
 499static const struct i2c_device_id ir_kbd_id[] = {
 500        /* Generic entry for any IR receiver */
 501        { "ir_video", 0 },
 502        /* IR device specific entries should be added here */
 503        { "ir_rx_z8f0811_haup", 0 },
 504        { "ir_rx_z8f0811_hdpvr", 0 },
 505        { }
 506};
 507
 508static struct i2c_driver ir_kbd_driver = {
 509        .driver = {
 510                .name   = "ir-kbd-i2c",
 511        },
 512        .probe          = ir_probe,
 513        .remove         = ir_remove,
 514        .id_table       = ir_kbd_id,
 515};
 516
 517module_i2c_driver(ir_kbd_driver);
 518
 519/* ----------------------------------------------------------------------- */
 520
 521MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
 522MODULE_DESCRIPTION("input driver for i2c IR remote controls");
 523MODULE_LICENSE("GPL");
 524