Path: blob/master/drivers/media/video/bt8xx/bttv-input.c
10785 views
/*1*2* Copyright (c) 2003 Gerd Knorr3* Copyright (c) 2003 Pavel Machek4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* (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 of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18*/1920#include <linux/module.h>21#include <linux/init.h>22#include <linux/delay.h>23#include <linux/interrupt.h>24#include <linux/input.h>25#include <linux/slab.h>2627#include "bttv.h"28#include "bttvp.h"293031static int ir_debug;32module_param(ir_debug, int, 0644);3334static int ir_rc5_remote_gap = 885;35module_param(ir_rc5_remote_gap, int, 0644);3637#undef dprintk38#define dprintk(arg...) do { \39if (ir_debug >= 1) \40printk(arg); \41} while (0)4243#define DEVNAME "bttv-input"4445#define MODULE_NAME "bttv"4647/* ---------------------------------------------------------------------- */4849static void ir_handle_key(struct bttv *btv)50{51struct bttv_ir *ir = btv->remote;52u32 gpio,data;5354/* read gpio value */55gpio = bttv_gpio_read(&btv->c);56if (ir->polling) {57if (ir->last_gpio == gpio)58return;59ir->last_gpio = gpio;60}6162/* extract data */63data = ir_extract_bits(gpio, ir->mask_keycode);64dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",65gpio, data,66ir->polling ? "poll" : "irq",67(gpio & ir->mask_keydown) ? " down" : "",68(gpio & ir->mask_keyup) ? " up" : "");6970if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||71(ir->mask_keyup && !(gpio & ir->mask_keyup))) {72rc_keydown_notimeout(ir->dev, data, 0);73} else {74/* HACK: Probably, ir->mask_keydown is missing75for this board */76if (btv->c.type == BTTV_BOARD_WINFAST2000)77rc_keydown_notimeout(ir->dev, data, 0);7879rc_keyup(ir->dev);80}81}8283static void ir_enltv_handle_key(struct bttv *btv)84{85struct bttv_ir *ir = btv->remote;86u32 gpio, data, keyup;8788/* read gpio value */89gpio = bttv_gpio_read(&btv->c);9091/* extract data */92data = ir_extract_bits(gpio, ir->mask_keycode);9394/* Check if it is keyup */95keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;9697if ((ir->last_gpio & 0x7f) != data) {98dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",99gpio, data,100(gpio & ir->mask_keyup) ? " up" : "up/down");101102rc_keydown_notimeout(ir->dev, data, 0);103if (keyup)104rc_keyup(ir->dev);105} else {106if ((ir->last_gpio & 1 << 31) == keyup)107return;108109dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",110gpio, data,111(gpio & ir->mask_keyup) ? " up" : "down");112113if (keyup)114rc_keyup(ir->dev);115else116rc_keydown_notimeout(ir->dev, data, 0);117}118119ir->last_gpio = data | keyup;120}121122static int bttv_rc5_irq(struct bttv *btv);123124void bttv_input_irq(struct bttv *btv)125{126struct bttv_ir *ir = btv->remote;127128if (ir->rc5_gpio)129bttv_rc5_irq(btv);130else if (!ir->polling)131ir_handle_key(btv);132}133134static void bttv_input_timer(unsigned long data)135{136struct bttv *btv = (struct bttv*)data;137struct bttv_ir *ir = btv->remote;138139if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)140ir_enltv_handle_key(btv);141else142ir_handle_key(btv);143mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));144}145146/*147* FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying148* on the rc-core way. As we need to be sure that both IRQ transitions are149* properly triggered, Better to touch it only with this hardware for150* testing.151*/152153#define RC5_START(x) (((x) >> 12) & 3)154#define RC5_TOGGLE(x) (((x) >> 11) & 1)155#define RC5_ADDR(x) (((x) >> 6) & 31)156#define RC5_INSTR(x) ((x) & 63)157158/* decode raw bit pattern to RC5 code */159static u32 bttv_rc5_decode(unsigned int code)160{161unsigned int org_code = code;162unsigned int pair;163unsigned int rc5 = 0;164int i;165166for (i = 0; i < 14; ++i) {167pair = code & 0x3;168code >>= 2;169170rc5 <<= 1;171switch (pair) {172case 0:173case 2:174break;175case 1:176rc5 |= 1;177break;178case 3:179dprintk(KERN_INFO DEVNAME ":rc5_decode(%x) bad code\n",180org_code);181return 0;182}183}184dprintk(KERN_INFO DEVNAME ":"185"code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "186"instr=%x\n", rc5, org_code, RC5_START(rc5),187RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));188return rc5;189}190191static void bttv_rc5_timer_end(unsigned long data)192{193struct bttv_ir *ir = (struct bttv_ir *)data;194struct timeval tv;195u32 gap;196u32 rc5 = 0;197198/* get time */199do_gettimeofday(&tv);200201/* avoid overflow with gap >1s */202if (tv.tv_sec - ir->base_time.tv_sec > 1) {203gap = 200000;204} else {205gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +206tv.tv_usec - ir->base_time.tv_usec;207}208209/* signal we're ready to start a new code */210ir->active = false;211212/* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */213if (gap < 28000) {214dprintk(KERN_INFO DEVNAME ": spurious timer_end\n");215return;216}217218if (ir->last_bit < 20) {219/* ignore spurious codes (caused by light/other remotes) */220dprintk(KERN_INFO DEVNAME ": short code: %x\n", ir->code);221} else {222ir->code = (ir->code << ir->shift_by) | 1;223rc5 = bttv_rc5_decode(ir->code);224225/* two start bits? */226if (RC5_START(rc5) != ir->start) {227printk(KERN_INFO DEVNAME ":"228" rc5 start bits invalid: %u\n", RC5_START(rc5));229230/* right address? */231} else if (RC5_ADDR(rc5) == ir->addr) {232u32 toggle = RC5_TOGGLE(rc5);233u32 instr = RC5_INSTR(rc5);234235/* Good code */236rc_keydown(ir->dev, instr, toggle);237dprintk(KERN_INFO DEVNAME ":"238" instruction %x, toggle %x\n",239instr, toggle);240}241}242}243244static int bttv_rc5_irq(struct bttv *btv)245{246struct bttv_ir *ir = btv->remote;247struct timeval tv;248u32 gpio;249u32 gap;250unsigned long current_jiffies;251252/* read gpio port */253gpio = bttv_gpio_read(&btv->c);254255/* get time of bit */256current_jiffies = jiffies;257do_gettimeofday(&tv);258259/* avoid overflow with gap >1s */260if (tv.tv_sec - ir->base_time.tv_sec > 1) {261gap = 200000;262} else {263gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +264tv.tv_usec - ir->base_time.tv_usec;265}266267dprintk(KERN_INFO DEVNAME ": RC5 IRQ: gap %d us for %s\n",268gap, (gpio & 0x20) ? "mark" : "space");269270/* remote IRQ? */271if (!(gpio & 0x20))272return 0;273274/* active code => add bit */275if (ir->active) {276/* only if in the code (otherwise spurious IRQ or timer277late) */278if (ir->last_bit < 28) {279ir->last_bit = (gap - ir_rc5_remote_gap / 2) /280ir_rc5_remote_gap;281ir->code |= 1 << ir->last_bit;282}283/* starting new code */284} else {285ir->active = true;286ir->code = 0;287ir->base_time = tv;288ir->last_bit = 0;289290mod_timer(&ir->timer, current_jiffies + msecs_to_jiffies(30));291}292293/* toggle GPIO pin 4 to reset the irq */294bttv_gpio_write(&btv->c, gpio & ~(1 << 4));295bttv_gpio_write(&btv->c, gpio | (1 << 4));296return 1;297}298299/* ---------------------------------------------------------------------- */300301static void bttv_ir_start(struct bttv *btv, struct bttv_ir *ir)302{303if (ir->polling) {304setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);305ir->timer.expires = jiffies + msecs_to_jiffies(1000);306add_timer(&ir->timer);307} else if (ir->rc5_gpio) {308/* set timer_end for code completion */309setup_timer(&ir->timer, bttv_rc5_timer_end, (unsigned long)ir);310ir->shift_by = 1;311ir->start = 3;312ir->addr = 0x0;313ir->rc5_remote_gap = ir_rc5_remote_gap;314}315}316317static void bttv_ir_stop(struct bttv *btv)318{319if (btv->remote->polling)320del_timer_sync(&btv->remote->timer);321322if (btv->remote->rc5_gpio) {323u32 gpio;324325del_timer_sync(&btv->remote->timer);326327gpio = bttv_gpio_read(&btv->c);328bttv_gpio_write(&btv->c, gpio & ~(1 << 4));329}330}331332/*333* Get_key functions used by I2C remotes334*/335336static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)337{338unsigned char b;339340/* poll IR chip */341if (1 != i2c_master_recv(ir->c, &b, 1)) {342dprintk(KERN_INFO DEVNAME ": read error\n");343return -EIO;344}345346/* ignore 0xaa */347if (b==0xaa)348return 0;349dprintk(KERN_INFO DEVNAME ": key %02x\n", b);350351/*352* NOTE:353* lirc_i2c maps the pv951 code as:354* addr = 0x61D6355* cmd = bit_reverse (b)356* So, it seems that this device uses NEC extended357* I decided to not fix the table, due to two reasons:358* 1) Without the actual device, this is only a guess;359* 2) As the addr is not reported via I2C, nor can be changed,360* the device is bound to the vendor-provided RC.361*/362363*ir_key = b;364*ir_raw = b;365return 1;366}367368/* Instantiate the I2C IR receiver device, if present */369void __devinit init_bttv_i2c_ir(struct bttv *btv)370{371const unsigned short addr_list[] = {3720x1a, 0x18, 0x64, 0x30, 0x71,373I2C_CLIENT_END374};375struct i2c_board_info info;376377if (0 != btv->i2c_rc)378return;379380memset(&info, 0, sizeof(struct i2c_board_info));381memset(&btv->init_data, 0, sizeof(btv->init_data));382strlcpy(info.type, "ir_video", I2C_NAME_SIZE);383384switch (btv->c.type) {385case BTTV_BOARD_PV951:386btv->init_data.name = "PV951";387btv->init_data.get_key = get_key_pv951;388btv->init_data.ir_codes = RC_MAP_PV951;389info.addr = 0x4b;390break;391default:392/*393* The external IR receiver is at i2c address 0x34 (0x35 for394* reads). Future Hauppauge cards will have an internal395* receiver at 0x30 (0x31 for reads). In theory, both can be396* fitted, and Hauppauge suggest an external overrides an397* internal.398* That's why we probe 0x1a (~0x34) first. CB399*/400401i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);402return;403}404405if (btv->init_data.name)406info.platform_data = &btv->init_data;407i2c_new_device(&btv->c.i2c_adap, &info);408409return;410}411412int __devexit fini_bttv_i2c(struct bttv *btv)413{414if (0 != btv->i2c_rc)415return 0;416417return i2c_del_adapter(&btv->c.i2c_adap);418}419420int bttv_input_init(struct bttv *btv)421{422struct bttv_ir *ir;423char *ir_codes = NULL;424struct rc_dev *rc;425int err = -ENOMEM;426427if (!btv->has_remote)428return -ENODEV;429430ir = kzalloc(sizeof(*ir),GFP_KERNEL);431rc = rc_allocate_device();432if (!ir || !rc)433goto err_out_free;434435/* detect & configure */436switch (btv->c.type) {437case BTTV_BOARD_AVERMEDIA:438case BTTV_BOARD_AVPHONE98:439case BTTV_BOARD_AVERMEDIA98:440ir_codes = RC_MAP_AVERMEDIA;441ir->mask_keycode = 0xf88000;442ir->mask_keydown = 0x010000;443ir->polling = 50; // ms444break;445446case BTTV_BOARD_AVDVBT_761:447case BTTV_BOARD_AVDVBT_771:448ir_codes = RC_MAP_AVERMEDIA_DVBT;449ir->mask_keycode = 0x0f00c0;450ir->mask_keydown = 0x000020;451ir->polling = 50; // ms452break;453454case BTTV_BOARD_PXELVWPLTVPAK:455ir_codes = RC_MAP_PIXELVIEW;456ir->mask_keycode = 0x003e00;457ir->mask_keyup = 0x010000;458ir->polling = 50; // ms459break;460case BTTV_BOARD_PV_M4900:461case BTTV_BOARD_PV_BT878P_9B:462case BTTV_BOARD_PV_BT878P_PLUS:463ir_codes = RC_MAP_PIXELVIEW;464ir->mask_keycode = 0x001f00;465ir->mask_keyup = 0x008000;466ir->polling = 50; // ms467break;468469case BTTV_BOARD_WINFAST2000:470ir_codes = RC_MAP_WINFAST;471ir->mask_keycode = 0x1f8;472break;473case BTTV_BOARD_MAGICTVIEW061:474case BTTV_BOARD_MAGICTVIEW063:475ir_codes = RC_MAP_WINFAST;476ir->mask_keycode = 0x0008e000;477ir->mask_keydown = 0x00200000;478break;479case BTTV_BOARD_APAC_VIEWCOMP:480ir_codes = RC_MAP_APAC_VIEWCOMP;481ir->mask_keycode = 0x001f00;482ir->mask_keyup = 0x008000;483ir->polling = 50; // ms484break;485case BTTV_BOARD_ASKEY_CPH03X:486case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:487case BTTV_BOARD_CONTVFMI:488ir_codes = RC_MAP_PIXELVIEW;489ir->mask_keycode = 0x001F00;490ir->mask_keyup = 0x006000;491ir->polling = 50; // ms492break;493case BTTV_BOARD_NEBULA_DIGITV:494ir_codes = RC_MAP_NEBULA;495ir->rc5_gpio = true;496break;497case BTTV_BOARD_MACHTV_MAGICTV:498ir_codes = RC_MAP_APAC_VIEWCOMP;499ir->mask_keycode = 0x001F00;500ir->mask_keyup = 0x004000;501ir->polling = 50; /* ms */502break;503case BTTV_BOARD_KOZUMI_KTV_01C:504ir_codes = RC_MAP_PCTV_SEDNA;505ir->mask_keycode = 0x001f00;506ir->mask_keyup = 0x006000;507ir->polling = 50; /* ms */508break;509case BTTV_BOARD_ENLTV_FM_2:510ir_codes = RC_MAP_ENCORE_ENLTV2;511ir->mask_keycode = 0x00fd00;512ir->mask_keyup = 0x000080;513ir->polling = 1; /* ms */514ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),515ir->mask_keycode);516break;517}518if (NULL == ir_codes) {519dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);520err = -ENODEV;521goto err_out_free;522}523524if (ir->rc5_gpio) {525u32 gpio;526/* enable remote irq */527bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);528gpio = bttv_gpio_read(&btv->c);529bttv_gpio_write(&btv->c, gpio & ~(1 << 4));530bttv_gpio_write(&btv->c, gpio | (1 << 4));531} else {532/* init hardware-specific stuff */533bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);534}535536/* init input device */537ir->dev = rc;538539snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",540btv->c.type);541snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",542pci_name(btv->c.pci));543544rc->input_name = ir->name;545rc->input_phys = ir->phys;546rc->input_id.bustype = BUS_PCI;547rc->input_id.version = 1;548if (btv->c.pci->subsystem_vendor) {549rc->input_id.vendor = btv->c.pci->subsystem_vendor;550rc->input_id.product = btv->c.pci->subsystem_device;551} else {552rc->input_id.vendor = btv->c.pci->vendor;553rc->input_id.product = btv->c.pci->device;554}555rc->dev.parent = &btv->c.pci->dev;556rc->map_name = ir_codes;557rc->driver_name = MODULE_NAME;558559btv->remote = ir;560bttv_ir_start(btv, ir);561562/* all done */563err = rc_register_device(rc);564if (err)565goto err_out_stop;566567return 0;568569err_out_stop:570bttv_ir_stop(btv);571btv->remote = NULL;572err_out_free:573rc_free_device(rc);574kfree(ir);575return err;576}577578void bttv_input_fini(struct bttv *btv)579{580if (btv->remote == NULL)581return;582583bttv_ir_stop(btv);584rc_unregister_device(btv->remote->dev);585kfree(btv->remote);586btv->remote = NULL;587}588589590