Path: blob/master/drivers/media/rc/ir-rc5-decoder.c
15109 views
/* ir-rc5-decoder.c - handle RC5(x) 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/*15* This code handles 14 bits RC5 protocols and 20 bits RC5x protocols.16* There are other variants that use a different number of bits.17* This is currently unsupported.18* It considers a carrier of 36 kHz, with a total of 14/20 bits, where19* the first two bits are start bits, and a third one is a filing bit20*/2122#include "rc-core-priv.h"2324#define RC5_NBITS 1425#define RC5X_NBITS 2026#define CHECK_RC5X_NBITS 827#define RC5_UNIT 888888 /* ns */28#define RC5_BIT_START (1 * RC5_UNIT)29#define RC5_BIT_END (1 * RC5_UNIT)30#define RC5X_SPACE (4 * RC5_UNIT)3132enum rc5_state {33STATE_INACTIVE,34STATE_BIT_START,35STATE_BIT_END,36STATE_CHECK_RC5X,37STATE_FINISHED,38};3940/**41* ir_rc5_decode() - Decode one RC-5 pulse or space42* @dev: the struct rc_dev descriptor of the device43* @ev: the struct ir_raw_event descriptor of the pulse/space44*45* This function returns -EINVAL if the pulse violates the state machine46*/47static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)48{49struct rc5_dec *data = &dev->raw->rc5;50u8 toggle;51u32 scancode;5253if (!(dev->raw->enabled_protocols & RC_TYPE_RC5))54return 0;5556if (!is_timing_event(ev)) {57if (ev.reset)58data->state = STATE_INACTIVE;59return 0;60}6162if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))63goto out;6465again:66IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n",67data->state, TO_US(ev.duration), TO_STR(ev.pulse));6869if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))70return 0;7172switch (data->state) {7374case STATE_INACTIVE:75if (!ev.pulse)76break;7778data->state = STATE_BIT_START;79data->count = 1;80/* We just need enough bits to get to STATE_CHECK_RC5X */81data->wanted_bits = RC5X_NBITS;82decrease_duration(&ev, RC5_BIT_START);83goto again;8485case STATE_BIT_START:86if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))87break;8889data->bits <<= 1;90if (!ev.pulse)91data->bits |= 1;92data->count++;93data->state = STATE_BIT_END;94return 0;9596case STATE_BIT_END:97if (!is_transition(&ev, &dev->raw->prev_ev))98break;99100if (data->count == data->wanted_bits)101data->state = STATE_FINISHED;102else if (data->count == CHECK_RC5X_NBITS)103data->state = STATE_CHECK_RC5X;104else105data->state = STATE_BIT_START;106107decrease_duration(&ev, RC5_BIT_END);108goto again;109110case STATE_CHECK_RC5X:111if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {112/* RC5X */113data->wanted_bits = RC5X_NBITS;114decrease_duration(&ev, RC5X_SPACE);115} else {116/* RC5 */117data->wanted_bits = RC5_NBITS;118}119data->state = STATE_BIT_START;120goto again;121122case STATE_FINISHED:123if (ev.pulse)124break;125126if (data->wanted_bits == RC5X_NBITS) {127/* RC5X */128u8 xdata, command, system;129xdata = (data->bits & 0x0003F) >> 0;130command = (data->bits & 0x00FC0) >> 6;131system = (data->bits & 0x1F000) >> 12;132toggle = (data->bits & 0x20000) ? 1 : 0;133command += (data->bits & 0x01000) ? 0 : 0x40;134scancode = system << 16 | command << 8 | xdata;135136IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",137scancode, toggle);138139} else {140/* RC5 */141u8 command, system;142command = (data->bits & 0x0003F) >> 0;143system = (data->bits & 0x007C0) >> 6;144toggle = (data->bits & 0x00800) ? 1 : 0;145command += (data->bits & 0x01000) ? 0 : 0x40;146scancode = system << 8 | command;147148IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",149scancode, toggle);150}151152rc_keydown(dev, scancode, toggle);153data->state = STATE_INACTIVE;154return 0;155}156157out:158IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n",159data->state, TO_US(ev.duration), TO_STR(ev.pulse));160data->state = STATE_INACTIVE;161return -EINVAL;162}163164static struct ir_raw_handler rc5_handler = {165.protocols = RC_TYPE_RC5,166.decode = ir_rc5_decode,167};168169static int __init ir_rc5_decode_init(void)170{171ir_raw_handler_register(&rc5_handler);172173printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");174return 0;175}176177static void __exit ir_rc5_decode_exit(void)178{179ir_raw_handler_unregister(&rc5_handler);180}181182module_init(ir_rc5_decode_init);183module_exit(ir_rc5_decode_exit);184185MODULE_LICENSE("GPL");186MODULE_AUTHOR("Mauro Carvalho Chehab <[email protected]>");187MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");188MODULE_DESCRIPTION("RC5(x) IR protocol decoder");189190191