Path: blob/master/drivers/media/video/ir-kbd-i2c.c
17633 views
/*1*2* keyboard input driver for i2c IR remote controls3*4* Copyright (c) 2000-2003 Gerd Knorr <[email protected]>5* modified for PixelView (BT878P+W/FM) by6* Michal Kochanowicz <[email protected]>7* Christoph Bartelmus <[email protected]>8* modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by9* Ulrich Mueller <[email protected]>10* modified for em2820 based USB TV tuners by11* Markus Rechberger <[email protected]>12* modified for DViCO Fusion HDTV 5 RT GOLD by13* Chaogui Zhang <[email protected]>14* modified for MSI TV@nywhere Plus by15* Henry Wong <[email protected]>16* Mark Schultz <[email protected]>17* Brian Rogers <[email protected]>18* modified for AVerMedia Cardbus by19* Oldrich Jedlicka <[email protected]>20*21* This program is free software; you can redistribute it and/or modify22* it under the terms of the GNU General Public License as published by23* the Free Software Foundation; either version 2 of the License, or24* (at your option) any later version.25*26* This program is distributed in the hope that it will be useful,27* but WITHOUT ANY WARRANTY; without even the implied warranty of28* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the29* GNU General Public License for more details.30*31* You should have received a copy of the GNU General Public License32* along with this program; if not, write to the Free Software33* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA34*35*/3637#include <linux/module.h>38#include <linux/init.h>39#include <linux/kernel.h>40#include <linux/string.h>41#include <linux/timer.h>42#include <linux/delay.h>43#include <linux/errno.h>44#include <linux/slab.h>45#include <linux/i2c.h>46#include <linux/workqueue.h>4748#include <media/rc-core.h>49#include <media/ir-kbd-i2c.h>5051/* ----------------------------------------------------------------------- */52/* insmod parameters */5354static int debug;55module_param(debug, int, 0644); /* debug level (0,1,2) */565758#define MODULE_NAME "ir-kbd-i2c"59#define dprintk(level, fmt, arg...) if (debug >= level) \60printk(KERN_DEBUG MODULE_NAME ": " fmt , ## arg)6162/* ----------------------------------------------------------------------- */6364static int get_key_haup_common(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,65int size, int offset)66{67unsigned char buf[6];68int start, range, toggle, dev, code, ircode;6970/* poll IR chip */71if (size != i2c_master_recv(ir->c, buf, size))72return -EIO;7374/* split rc5 data block ... */75start = (buf[offset] >> 7) & 1;76range = (buf[offset] >> 6) & 1;77toggle = (buf[offset] >> 5) & 1;78dev = buf[offset] & 0x1f;79code = (buf[offset+1] >> 2) & 0x3f;8081/* rc5 has two start bits82* the first bit must be one83* the second bit defines the command range (1 = 0-63, 0 = 64 - 127)84*/85if (!start)86/* no key pressed */87return 0;88/*89* Hauppauge remotes (black/silver) always use90* specific device ids. If we do not filter the91* device ids then messages destined for devices92* such as TVs (id=0) will get through causing93* mis-fired events.94*95* We also filter out invalid key presses which96* produce annoying debug log entries.97*/98ircode= (start << 12) | (toggle << 11) | (dev << 6) | code;99if ((ircode & 0x1fff)==0x1fff)100/* invalid key press */101return 0;102103if (!range)104code += 64;105106dprintk(1,"ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",107start, range, toggle, dev, code);108109/* return key */110*ir_key = (dev << 8) | code;111*ir_raw = ircode;112return 1;113}114115static int get_key_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)116{117return get_key_haup_common (ir, ir_key, ir_raw, 3, 0);118}119120static int get_key_haup_xvr(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)121{122int ret;123unsigned char buf[1] = { 0 };124125/*126* This is the same apparent "are you ready?" poll command observed127* watching Windows driver traffic and implemented in lirc_zilog. With128* this added, we get far saner remote behavior with z8 chips on usb129* connected devices, even with the default polling interval of 100ms.130*/131ret = i2c_master_send(ir->c, buf, 1);132if (ret != 1)133return (ret < 0) ? ret : -EINVAL;134135return get_key_haup_common (ir, ir_key, ir_raw, 6, 3);136}137138static int get_key_pixelview(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)139{140unsigned char b;141142/* poll IR chip */143if (1 != i2c_master_recv(ir->c, &b, 1)) {144dprintk(1,"read error\n");145return -EIO;146}147*ir_key = b;148*ir_raw = b;149return 1;150}151152static int get_key_fusionhdtv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)153{154unsigned char buf[4];155156/* poll IR chip */157if (4 != i2c_master_recv(ir->c, buf, 4)) {158dprintk(1,"read error\n");159return -EIO;160}161162if(buf[0] !=0 || buf[1] !=0 || buf[2] !=0 || buf[3] != 0)163dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,164buf[0], buf[1], buf[2], buf[3]);165166/* no key pressed or signal from other ir remote */167if(buf[0] != 0x1 || buf[1] != 0xfe)168return 0;169170*ir_key = buf[2];171*ir_raw = (buf[2] << 8) | buf[3];172173return 1;174}175176static int get_key_knc1(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)177{178unsigned char b;179180/* poll IR chip */181if (1 != i2c_master_recv(ir->c, &b, 1)) {182dprintk(1,"read error\n");183return -EIO;184}185186/* it seems that 0xFE indicates that a button is still hold187down, while 0xff indicates that no button is hold188down. 0xfe sequences are sometimes interrupted by 0xFF */189190dprintk(2,"key %02x\n", b);191192if (b == 0xff)193return 0;194195if (b == 0xfe)196/* keep old data */197return 1;198199*ir_key = b;200*ir_raw = b;201return 1;202}203204static int get_key_avermedia_cardbus(struct IR_i2c *ir,205u32 *ir_key, u32 *ir_raw)206{207unsigned char subaddr, key, keygroup;208struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,209.buf = &subaddr, .len = 1},210{ .addr = ir->c->addr, .flags = I2C_M_RD,211.buf = &key, .len = 1} };212subaddr = 0x0d;213if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {214dprintk(1, "read error\n");215return -EIO;216}217218if (key == 0xff)219return 0;220221subaddr = 0x0b;222msg[1].buf = &keygroup;223if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {224dprintk(1, "read error\n");225return -EIO;226}227228if (keygroup == 0xff)229return 0;230231dprintk(1, "read key 0x%02x/0x%02x\n", key, keygroup);232if (keygroup < 2 || keygroup > 3) {233/* Only a warning */234dprintk(1, "warning: invalid key group 0x%02x for key 0x%02x\n",235keygroup, key);236}237key |= (keygroup & 1) << 6;238239*ir_key = key;240*ir_raw = key;241return 1;242}243244/* ----------------------------------------------------------------------- */245246static void ir_key_poll(struct IR_i2c *ir)247{248static u32 ir_key, ir_raw;249int rc;250251dprintk(3, "%s\n", __func__);252rc = ir->get_key(ir, &ir_key, &ir_raw);253if (rc < 0) {254dprintk(2,"error\n");255return;256}257258if (rc) {259dprintk(1, "%s: keycode = 0x%04x\n", __func__, ir_key);260rc_keydown(ir->rc, ir_key, 0);261}262}263264static void ir_work(struct work_struct *work)265{266struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);267268ir_key_poll(ir);269schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));270}271272/* ----------------------------------------------------------------------- */273274static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)275{276char *ir_codes = NULL;277const char *name = NULL;278u64 rc_type = RC_TYPE_UNKNOWN;279struct IR_i2c *ir;280struct rc_dev *rc = NULL;281struct i2c_adapter *adap = client->adapter;282unsigned short addr = client->addr;283int err;284285ir = kzalloc(sizeof(struct IR_i2c), GFP_KERNEL);286if (!ir)287return -ENOMEM;288289ir->c = client;290ir->polling_interval = DEFAULT_POLLING_INTERVAL;291i2c_set_clientdata(client, ir);292293switch(addr) {294case 0x64:295name = "Pixelview";296ir->get_key = get_key_pixelview;297rc_type = RC_TYPE_OTHER;298ir_codes = RC_MAP_EMPTY;299break;300case 0x18:301case 0x1f:302case 0x1a:303name = "Hauppauge";304ir->get_key = get_key_haup;305rc_type = RC_TYPE_RC5;306ir_codes = RC_MAP_HAUPPAUGE;307break;308case 0x30:309name = "KNC One";310ir->get_key = get_key_knc1;311rc_type = RC_TYPE_OTHER;312ir_codes = RC_MAP_EMPTY;313break;314case 0x6b:315name = "FusionHDTV";316ir->get_key = get_key_fusionhdtv;317rc_type = RC_TYPE_RC5;318ir_codes = RC_MAP_FUSIONHDTV_MCE;319break;320case 0x40:321name = "AVerMedia Cardbus remote";322ir->get_key = get_key_avermedia_cardbus;323rc_type = RC_TYPE_OTHER;324ir_codes = RC_MAP_AVERMEDIA_CARDBUS;325break;326case 0x71:327name = "Hauppauge/Zilog Z8";328ir->get_key = get_key_haup_xvr;329rc_type = RC_TYPE_RC5;330ir_codes = RC_MAP_HAUPPAUGE;331break;332}333334/* Let the caller override settings */335if (client->dev.platform_data) {336const struct IR_i2c_init_data *init_data =337client->dev.platform_data;338339ir_codes = init_data->ir_codes;340rc = init_data->rc_dev;341342name = init_data->name;343if (init_data->type)344rc_type = init_data->type;345346if (init_data->polling_interval)347ir->polling_interval = init_data->polling_interval;348349switch (init_data->internal_get_key_func) {350case IR_KBD_GET_KEY_CUSTOM:351/* The bridge driver provided us its own function */352ir->get_key = init_data->get_key;353break;354case IR_KBD_GET_KEY_PIXELVIEW:355ir->get_key = get_key_pixelview;356break;357case IR_KBD_GET_KEY_HAUP:358ir->get_key = get_key_haup;359break;360case IR_KBD_GET_KEY_KNC1:361ir->get_key = get_key_knc1;362break;363case IR_KBD_GET_KEY_FUSIONHDTV:364ir->get_key = get_key_fusionhdtv;365break;366case IR_KBD_GET_KEY_HAUP_XVR:367ir->get_key = get_key_haup_xvr;368break;369case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:370ir->get_key = get_key_avermedia_cardbus;371break;372}373}374375if (!rc) {376/*377* If platform_data doesn't specify rc_dev, initilize it378* internally379*/380rc = rc_allocate_device();381if (!rc) {382err = -ENOMEM;383goto err_out_free;384}385}386ir->rc = rc;387388/* Make sure we are all setup before going on */389if (!name || !ir->get_key || !rc_type || !ir_codes) {390dprintk(1, ": Unsupported device at address 0x%02x\n",391addr);392err = -ENODEV;393goto err_out_free;394}395396/* Sets name */397snprintf(ir->name, sizeof(ir->name), "i2c IR (%s)", name);398ir->ir_codes = ir_codes;399400snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",401dev_name(&adap->dev),402dev_name(&client->dev));403404/*405* Initialize input_dev fields406* It doesn't make sense to allow overriding them via platform_data407*/408rc->input_id.bustype = BUS_I2C;409rc->input_phys = ir->phys;410rc->input_name = ir->name;411412/*413* Initialize the other fields of rc_dev414*/415rc->map_name = ir->ir_codes;416rc->allowed_protos = rc_type;417if (!rc->driver_name)418rc->driver_name = MODULE_NAME;419420err = rc_register_device(rc);421if (err)422goto err_out_free;423424printk(MODULE_NAME ": %s detected at %s [%s]\n",425ir->name, ir->phys, adap->name);426427/* start polling via eventd */428INIT_DELAYED_WORK(&ir->work, ir_work);429schedule_delayed_work(&ir->work, 0);430431return 0;432433err_out_free:434/* Only frees rc if it were allocated internally */435rc_free_device(rc);436kfree(ir);437return err;438}439440static int ir_remove(struct i2c_client *client)441{442struct IR_i2c *ir = i2c_get_clientdata(client);443444/* kill outstanding polls */445cancel_delayed_work_sync(&ir->work);446447/* unregister device */448rc_unregister_device(ir->rc);449450/* free memory */451kfree(ir);452return 0;453}454455static const struct i2c_device_id ir_kbd_id[] = {456/* Generic entry for any IR receiver */457{ "ir_video", 0 },458/* IR device specific entries should be added here */459{ "ir_rx_z8f0811_haup", 0 },460{ "ir_rx_z8f0811_hdpvr", 0 },461{ }462};463464static struct i2c_driver driver = {465.driver = {466.name = "ir-kbd-i2c",467},468.probe = ir_probe,469.remove = ir_remove,470.id_table = ir_kbd_id,471};472473/* ----------------------------------------------------------------------- */474475MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");476MODULE_DESCRIPTION("input driver for i2c IR remote controls");477MODULE_LICENSE("GPL");478479static int __init ir_init(void)480{481return i2c_add_driver(&driver);482}483484static void __exit ir_fini(void)485{486i2c_del_driver(&driver);487}488489module_init(ir_init);490module_exit(ir_fini);491492/*493* Overrides for Emacs so that we follow Linus's tabbing style.494* ---------------------------------------------------------------------------495* Local variables:496* c-basic-offset: 8497* End:498*/499500501