Path: blob/master/drivers/media/rc/ir-nec-decoder.c
15111 views
/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol1*2* Copyright (C) 2010 by Mauro Carvalho Chehab <[email protected]>3*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation version 2 of the License.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*/1314#include <linux/bitrev.h>15#include "rc-core-priv.h"1617#define NEC_NBITS 3218#define NEC_UNIT 562500 /* ns */19#define NEC_HEADER_PULSE (16 * NEC_UNIT)20#define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */21#define NEC_HEADER_SPACE (8 * NEC_UNIT)22#define NEC_REPEAT_SPACE (4 * NEC_UNIT)23#define NEC_BIT_PULSE (1 * NEC_UNIT)24#define NEC_BIT_0_SPACE (1 * NEC_UNIT)25#define NEC_BIT_1_SPACE (3 * NEC_UNIT)26#define NEC_TRAILER_PULSE (1 * NEC_UNIT)27#define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */28#define NECX_REPEAT_BITS 12930enum nec_state {31STATE_INACTIVE,32STATE_HEADER_SPACE,33STATE_BIT_PULSE,34STATE_BIT_SPACE,35STATE_TRAILER_PULSE,36STATE_TRAILER_SPACE,37};3839/**40* ir_nec_decode() - Decode one NEC pulse or space41* @dev: the struct rc_dev descriptor of the device42* @duration: the struct ir_raw_event descriptor of the pulse/space43*44* This function returns -EINVAL if the pulse violates the state machine45*/46static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)47{48struct nec_dec *data = &dev->raw->nec;49u32 scancode;50u8 address, not_address, command, not_command;51bool send_32bits = false;5253if (!(dev->raw->enabled_protocols & RC_TYPE_NEC))54return 0;5556if (!is_timing_event(ev)) {57if (ev.reset)58data->state = STATE_INACTIVE;59return 0;60}6162IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",63data->state, TO_US(ev.duration), TO_STR(ev.pulse));6465switch (data->state) {6667case STATE_INACTIVE:68if (!ev.pulse)69break;7071if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2)) {72data->is_nec_x = false;73data->necx_repeat = false;74} else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))75data->is_nec_x = true;76else77break;7879data->count = 0;80data->state = STATE_HEADER_SPACE;81return 0;8283case STATE_HEADER_SPACE:84if (ev.pulse)85break;8687if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {88data->state = STATE_BIT_PULSE;89return 0;90} else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {91if (!dev->keypressed) {92IR_dprintk(1, "Discarding last key repeat: event after key up\n");93} else {94rc_repeat(dev);95IR_dprintk(1, "Repeat last key\n");96data->state = STATE_TRAILER_PULSE;97}98return 0;99}100101break;102103case STATE_BIT_PULSE:104if (!ev.pulse)105break;106107if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))108break;109110data->state = STATE_BIT_SPACE;111return 0;112113case STATE_BIT_SPACE:114if (ev.pulse)115break;116117if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&118geq_margin(ev.duration,119NEC_TRAILER_SPACE, NEC_UNIT / 2)) {120IR_dprintk(1, "Repeat last key\n");121rc_repeat(dev);122data->state = STATE_INACTIVE;123return 0;124125} else if (data->count > NECX_REPEAT_BITS)126data->necx_repeat = false;127128data->bits <<= 1;129if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))130data->bits |= 1;131else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))132break;133data->count++;134135if (data->count == NEC_NBITS)136data->state = STATE_TRAILER_PULSE;137else138data->state = STATE_BIT_PULSE;139140return 0;141142case STATE_TRAILER_PULSE:143if (!ev.pulse)144break;145146if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))147break;148149data->state = STATE_TRAILER_SPACE;150return 0;151152case STATE_TRAILER_SPACE:153if (ev.pulse)154break;155156if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))157break;158159address = bitrev8((data->bits >> 24) & 0xff);160not_address = bitrev8((data->bits >> 16) & 0xff);161command = bitrev8((data->bits >> 8) & 0xff);162not_command = bitrev8((data->bits >> 0) & 0xff);163164if ((command ^ not_command) != 0xff) {165IR_dprintk(1, "NEC checksum error: received 0x%08x\n",166data->bits);167send_32bits = true;168}169170if (send_32bits) {171/* NEC transport, but modified protocol, used by at172* least Apple and TiVo remotes */173scancode = data->bits;174IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);175} else if ((address ^ not_address) != 0xff) {176/* Extended NEC */177scancode = address << 16 |178not_address << 8 |179command;180IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);181} else {182/* Normal NEC */183scancode = address << 8 | command;184IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);185}186187if (data->is_nec_x)188data->necx_repeat = true;189190rc_keydown(dev, scancode, 0);191data->state = STATE_INACTIVE;192return 0;193}194195IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",196data->state, TO_US(ev.duration), TO_STR(ev.pulse));197data->state = STATE_INACTIVE;198return -EINVAL;199}200201static struct ir_raw_handler nec_handler = {202.protocols = RC_TYPE_NEC,203.decode = ir_nec_decode,204};205206static int __init ir_nec_decode_init(void)207{208ir_raw_handler_register(&nec_handler);209210printk(KERN_INFO "IR NEC protocol handler initialized\n");211return 0;212}213214static void __exit ir_nec_decode_exit(void)215{216ir_raw_handler_unregister(&nec_handler);217}218219module_init(ir_nec_decode_init);220module_exit(ir_nec_decode_exit);221222MODULE_LICENSE("GPL");223MODULE_AUTHOR("Mauro Carvalho Chehab <[email protected]>");224MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");225MODULE_DESCRIPTION("NEC IR protocol decoder");226227228