Path: blob/master/drivers/media/video/em28xx/em28xx-input.c
17697 views
/*1handle em28xx IR remotes via linux kernel input layer.23Copyright (C) 2005 Ludovico Cavedon <[email protected]>4Markus Rechberger <[email protected]>5Mauro Carvalho Chehab <[email protected]>6Sascha Sommer <[email protected]>78This program is free software; you can redistribute it and/or modify9it under the terms of the GNU General Public License as published by10the Free Software Foundation; either version 2 of the License, or11(at your option) any later version.1213This program is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16GNU General Public License for more details.1718You should have received a copy of the GNU General Public License19along with this program; if not, write to the Free Software20Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA21*/2223#include <linux/module.h>24#include <linux/init.h>25#include <linux/delay.h>26#include <linux/interrupt.h>27#include <linux/usb.h>28#include <linux/slab.h>2930#include "em28xx.h"3132#define EM28XX_SNAPSHOT_KEY KEY_CAMERA33#define EM28XX_SBUTTON_QUERY_INTERVAL 50034#define EM28XX_R0C_USBSUSP_SNAPSHOT 0x203536static unsigned int ir_debug;37module_param(ir_debug, int, 0644);38MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");3940#define MODULE_NAME "em28xx"4142#define i2cdprintk(fmt, arg...) \43if (ir_debug) { \44printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \45}4647#define dprintk(fmt, arg...) \48if (ir_debug) { \49printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \50}5152/**********************************************************53Polling structure used by em28xx IR's54**********************************************************/5556struct em28xx_ir_poll_result {57unsigned int toggle_bit:1;58unsigned int read_count:7;59u8 rc_address;60u8 rc_data[4]; /* 1 byte on em2860/2880, 4 on em2874 */61};6263struct em28xx_IR {64struct em28xx *dev;65struct rc_dev *rc;66char name[32];67char phys[32];6869/* poll external decoder */70int polling;71struct delayed_work work;72unsigned int full_code:1;73unsigned int last_readcount;7475int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);76};7778/**********************************************************79I2C IR based get keycodes - should be used with ir-kbd-i2c80**********************************************************/8182int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)83{84unsigned char b;8586/* poll IR chip */87if (1 != i2c_master_recv(ir->c, &b, 1)) {88i2cdprintk("read error\n");89return -EIO;90}9192/* it seems that 0xFE indicates that a button is still hold93down, while 0xff indicates that no button is hold94down. 0xfe sequences are sometimes interrupted by 0xFF */9596i2cdprintk("key %02x\n", b);9798if (b == 0xff)99return 0;100101if (b == 0xfe)102/* keep old data */103return 1;104105*ir_key = b;106*ir_raw = b;107return 1;108}109110int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)111{112unsigned char buf[2];113u16 code;114int size;115116/* poll IR chip */117size = i2c_master_recv(ir->c, buf, sizeof(buf));118119if (size != 2)120return -EIO;121122/* Does eliminate repeated parity code */123if (buf[1] == 0xff)124return 0;125126ir->old = buf[1];127128/*129* Rearranges bits to the right order.130* The bit order were determined experimentally by using131* The original Hauppauge Grey IR and another RC5 that uses addr=0x08132* The RC5 code has 14 bits, but we've experimentally determined133* the meaning for only 11 bits.134* So, the code translation is not complete. Yet, it is enough to135* work with the provided RC5 IR.136*/137code =138((buf[0] & 0x01) ? 0x0020 : 0) | /* 0010 0000 */139((buf[0] & 0x02) ? 0x0010 : 0) | /* 0001 0000 */140((buf[0] & 0x04) ? 0x0008 : 0) | /* 0000 1000 */141((buf[0] & 0x08) ? 0x0004 : 0) | /* 0000 0100 */142((buf[0] & 0x10) ? 0x0002 : 0) | /* 0000 0010 */143((buf[0] & 0x20) ? 0x0001 : 0) | /* 0000 0001 */144((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000 */145((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000 */146((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100 */147((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */148((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */149150i2cdprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x%02x)\n",151code, buf[1], buf[0]);152153/* return key */154*ir_key = code;155*ir_raw = code;156return 1;157}158159int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,160u32 *ir_raw)161{162unsigned char buf[3];163164/* poll IR chip */165166if (3 != i2c_master_recv(ir->c, buf, 3)) {167i2cdprintk("read error\n");168return -EIO;169}170171i2cdprintk("key %02x\n", buf[2]&0x3f);172if (buf[0] != 0x00)173return 0;174175*ir_key = buf[2]&0x3f;176*ir_raw = buf[2]&0x3f;177178return 1;179}180181int em28xx_get_key_winfast_usbii_deluxe(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)182{183unsigned char subaddr, keydetect, key;184185struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0, .buf = &subaddr, .len = 1},186187{ .addr = ir->c->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };188189subaddr = 0x10;190if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {191i2cdprintk("read error\n");192return -EIO;193}194if (keydetect == 0x00)195return 0;196197subaddr = 0x00;198msg[1].buf = &key;199if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {200i2cdprintk("read error\n");201return -EIO;202}203if (key == 0x00)204return 0;205206*ir_key = key;207*ir_raw = key;208return 1;209}210211/**********************************************************212Poll based get keycode functions213**********************************************************/214215/* This is for the em2860/em2880 */216static int default_polling_getkey(struct em28xx_IR *ir,217struct em28xx_ir_poll_result *poll_result)218{219struct em28xx *dev = ir->dev;220int rc;221u8 msg[3] = { 0, 0, 0 };222223/* Read key toggle, brand, and key code224on registers 0x45, 0x46 and 0x47225*/226rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,227msg, sizeof(msg));228if (rc < 0)229return rc;230231/* Infrared toggle (Reg 0x45[7]) */232poll_result->toggle_bit = (msg[0] >> 7);233234/* Infrared read count (Reg 0x45[6:0] */235poll_result->read_count = (msg[0] & 0x7f);236237/* Remote Control Address (Reg 0x46) */238poll_result->rc_address = msg[1];239240/* Remote Control Data (Reg 0x47) */241poll_result->rc_data[0] = msg[2];242243return 0;244}245246static int em2874_polling_getkey(struct em28xx_IR *ir,247struct em28xx_ir_poll_result *poll_result)248{249struct em28xx *dev = ir->dev;250int rc;251u8 msg[5] = { 0, 0, 0, 0, 0 };252253/* Read key toggle, brand, and key code254on registers 0x51-55255*/256rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,257msg, sizeof(msg));258if (rc < 0)259return rc;260261/* Infrared toggle (Reg 0x51[7]) */262poll_result->toggle_bit = (msg[0] >> 7);263264/* Infrared read count (Reg 0x51[6:0] */265poll_result->read_count = (msg[0] & 0x7f);266267/* Remote Control Address (Reg 0x52) */268poll_result->rc_address = msg[1];269270/* Remote Control Data (Reg 0x53-55) */271poll_result->rc_data[0] = msg[2];272poll_result->rc_data[1] = msg[3];273poll_result->rc_data[2] = msg[4];274275return 0;276}277278/**********************************************************279Polling code for em28xx280**********************************************************/281282static void em28xx_ir_handle_key(struct em28xx_IR *ir)283{284int result;285struct em28xx_ir_poll_result poll_result;286287/* read the registers containing the IR status */288result = ir->get_key(ir, &poll_result);289if (unlikely(result < 0)) {290dprintk("ir->get_key() failed %d\n", result);291return;292}293294if (unlikely(poll_result.read_count != ir->last_readcount)) {295dprintk("%s: toggle: %d, count: %d, key 0x%02x%02x\n", __func__,296poll_result.toggle_bit, poll_result.read_count,297poll_result.rc_address, poll_result.rc_data[0]);298if (ir->full_code)299rc_keydown(ir->rc,300poll_result.rc_address << 8 |301poll_result.rc_data[0],302poll_result.toggle_bit);303else304rc_keydown(ir->rc,305poll_result.rc_data[0],306poll_result.toggle_bit);307308if (ir->dev->chip_id == CHIP_ID_EM2874)309/* The em2874 clears the readcount field every time the310register is read. The em2860/2880 datasheet says that it311is supposed to clear the readcount, but it doesn't. So with312the em2874, we are looking for a non-zero read count as313opposed to a readcount that is incrementing */314ir->last_readcount = 0;315else316ir->last_readcount = poll_result.read_count;317}318}319320static void em28xx_ir_work(struct work_struct *work)321{322struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);323324em28xx_ir_handle_key(ir);325schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));326}327328static int em28xx_ir_start(struct rc_dev *rc)329{330struct em28xx_IR *ir = rc->priv;331332INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);333schedule_delayed_work(&ir->work, 0);334335return 0;336}337338static void em28xx_ir_stop(struct rc_dev *rc)339{340struct em28xx_IR *ir = rc->priv;341342cancel_delayed_work_sync(&ir->work);343}344345int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 rc_type)346{347int rc = 0;348struct em28xx_IR *ir = rc_dev->priv;349struct em28xx *dev = ir->dev;350u8 ir_config = EM2874_IR_RC5;351352/* Adjust xclk based o IR table for RC5/NEC tables */353354if (rc_type == RC_TYPE_RC5) {355dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;356ir->full_code = 1;357} else if (rc_type == RC_TYPE_NEC) {358dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;359ir_config = EM2874_IR_NEC;360ir->full_code = 1;361} else if (rc_type != RC_TYPE_UNKNOWN)362rc = -EINVAL;363364em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,365EM28XX_XCLK_IR_RC5_MODE);366367/* Setup the proper handler based on the chip */368switch (dev->chip_id) {369case CHIP_ID_EM2860:370case CHIP_ID_EM2883:371ir->get_key = default_polling_getkey;372break;373case CHIP_ID_EM2874:374ir->get_key = em2874_polling_getkey;375em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);376break;377default:378printk("Unrecognized em28xx chip id: IR not supported\n");379rc = -EINVAL;380}381382return rc;383}384385int em28xx_ir_init(struct em28xx *dev)386{387struct em28xx_IR *ir;388struct rc_dev *rc;389int err = -ENOMEM;390391if (dev->board.ir_codes == NULL) {392/* No remote control support */393return 0;394}395396ir = kzalloc(sizeof(*ir), GFP_KERNEL);397rc = rc_allocate_device();398if (!ir || !rc)399goto err_out_free;400401/* record handles to ourself */402ir->dev = dev;403dev->ir = ir;404ir->rc = rc;405406/*407* em2874 supports more protocols. For now, let's just announce408* the two protocols that were already tested409*/410rc->allowed_protos = RC_TYPE_RC5 | RC_TYPE_NEC;411rc->priv = ir;412rc->change_protocol = em28xx_ir_change_protocol;413rc->open = em28xx_ir_start;414rc->close = em28xx_ir_stop;415416/* By default, keep protocol field untouched */417err = em28xx_ir_change_protocol(rc, RC_TYPE_UNKNOWN);418if (err)419goto err_out_free;420421/* This is how often we ask the chip for IR information */422ir->polling = 100; /* ms */423424/* init input device */425snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",426dev->name);427428usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));429strlcat(ir->phys, "/input0", sizeof(ir->phys));430431rc->input_name = ir->name;432rc->input_phys = ir->phys;433rc->input_id.bustype = BUS_USB;434rc->input_id.version = 1;435rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);436rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);437rc->dev.parent = &dev->udev->dev;438rc->map_name = dev->board.ir_codes;439rc->driver_name = MODULE_NAME;440441/* all done */442err = rc_register_device(rc);443if (err)444goto err_out_stop;445446return 0;447448err_out_stop:449dev->ir = NULL;450err_out_free:451rc_free_device(rc);452kfree(ir);453return err;454}455456int em28xx_ir_fini(struct em28xx *dev)457{458struct em28xx_IR *ir = dev->ir;459460/* skip detach on non attached boards */461if (!ir)462return 0;463464em28xx_ir_stop(ir->rc);465rc_unregister_device(ir->rc);466kfree(ir);467468/* done */469dev->ir = NULL;470return 0;471}472473/**********************************************************474Handle Webcam snapshot button475**********************************************************/476477static void em28xx_query_sbutton(struct work_struct *work)478{479/* Poll the register and see if the button is depressed */480struct em28xx *dev =481container_of(work, struct em28xx, sbutton_query_work.work);482int ret;483484ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);485486if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {487u8 cleared;488/* Button is depressed, clear the register */489cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;490em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);491492/* Not emulate the keypress */493input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,4941);495/* Now unpress the key */496input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,4970);498}499500/* Schedule next poll */501schedule_delayed_work(&dev->sbutton_query_work,502msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));503}504505void em28xx_register_snapshot_button(struct em28xx *dev)506{507struct input_dev *input_dev;508int err;509510em28xx_info("Registering snapshot button...\n");511input_dev = input_allocate_device();512if (!input_dev) {513em28xx_errdev("input_allocate_device failed\n");514return;515}516517usb_make_path(dev->udev, dev->snapshot_button_path,518sizeof(dev->snapshot_button_path));519strlcat(dev->snapshot_button_path, "/sbutton",520sizeof(dev->snapshot_button_path));521INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);522523input_dev->name = "em28xx snapshot button";524input_dev->phys = dev->snapshot_button_path;525input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);526set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);527input_dev->keycodesize = 0;528input_dev->keycodemax = 0;529input_dev->id.bustype = BUS_USB;530input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);531input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);532input_dev->id.version = 1;533input_dev->dev.parent = &dev->udev->dev;534535err = input_register_device(input_dev);536if (err) {537em28xx_errdev("input_register_device failed\n");538input_free_device(input_dev);539return;540}541542dev->sbutton_input_dev = input_dev;543schedule_delayed_work(&dev->sbutton_query_work,544msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));545return;546547}548549void em28xx_deregister_snapshot_button(struct em28xx *dev)550{551if (dev->sbutton_input_dev != NULL) {552em28xx_info("Deregistering snapshot button\n");553cancel_delayed_work_sync(&dev->sbutton_query_work);554input_unregister_device(dev->sbutton_input_dev);555dev->sbutton_input_dev = NULL;556}557return;558}559560561