132cf86f6SMauro Carvalho Chehab /* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol 232cf86f6SMauro Carvalho Chehab * 332cf86f6SMauro Carvalho Chehab * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com> 432cf86f6SMauro Carvalho Chehab * 532cf86f6SMauro Carvalho Chehab * This program is free software; you can redistribute it and/or modify 632cf86f6SMauro Carvalho Chehab * it under the terms of the GNU General Public License as published by 732cf86f6SMauro Carvalho Chehab * the Free Software Foundation version 2 of the License. 832cf86f6SMauro Carvalho Chehab * 932cf86f6SMauro Carvalho Chehab * This program is distributed in the hope that it will be useful, 1032cf86f6SMauro Carvalho Chehab * but WITHOUT ANY WARRANTY; without even the implied warranty of 1132cf86f6SMauro Carvalho Chehab * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1232cf86f6SMauro Carvalho Chehab * GNU General Public License for more details. 1332cf86f6SMauro Carvalho Chehab */ 1432cf86f6SMauro Carvalho Chehab 1532cf86f6SMauro Carvalho Chehab #include <linux/bitrev.h> 16f62de675SMauro Carvalho Chehab #include "rc-core-priv.h" 1732cf86f6SMauro Carvalho Chehab 1832cf86f6SMauro Carvalho Chehab #define NEC_NBITS 32 1932cf86f6SMauro Carvalho Chehab #define NEC_UNIT 562500 /* ns */ 2032cf86f6SMauro Carvalho Chehab #define NEC_HEADER_PULSE (16 * NEC_UNIT) 2132cf86f6SMauro Carvalho Chehab #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */ 2232cf86f6SMauro Carvalho Chehab #define NEC_HEADER_SPACE (8 * NEC_UNIT) 2332cf86f6SMauro Carvalho Chehab #define NEC_REPEAT_SPACE (4 * NEC_UNIT) 2432cf86f6SMauro Carvalho Chehab #define NEC_BIT_PULSE (1 * NEC_UNIT) 2532cf86f6SMauro Carvalho Chehab #define NEC_BIT_0_SPACE (1 * NEC_UNIT) 2632cf86f6SMauro Carvalho Chehab #define NEC_BIT_1_SPACE (3 * NEC_UNIT) 2732cf86f6SMauro Carvalho Chehab #define NEC_TRAILER_PULSE (1 * NEC_UNIT) 2832cf86f6SMauro Carvalho Chehab #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */ 2932cf86f6SMauro Carvalho Chehab #define NECX_REPEAT_BITS 1 3032cf86f6SMauro Carvalho Chehab 3132cf86f6SMauro Carvalho Chehab enum nec_state { 3232cf86f6SMauro Carvalho Chehab STATE_INACTIVE, 3332cf86f6SMauro Carvalho Chehab STATE_HEADER_SPACE, 3432cf86f6SMauro Carvalho Chehab STATE_BIT_PULSE, 3532cf86f6SMauro Carvalho Chehab STATE_BIT_SPACE, 3632cf86f6SMauro Carvalho Chehab STATE_TRAILER_PULSE, 3732cf86f6SMauro Carvalho Chehab STATE_TRAILER_SPACE, 3832cf86f6SMauro Carvalho Chehab }; 3932cf86f6SMauro Carvalho Chehab 4032cf86f6SMauro Carvalho Chehab /** 4132cf86f6SMauro Carvalho Chehab * ir_nec_decode() - Decode one NEC pulse or space 42d8b4b582SDavid Härdeman * @dev: the struct rc_dev descriptor of the device 4332cf86f6SMauro Carvalho Chehab * @duration: the struct ir_raw_event descriptor of the pulse/space 4432cf86f6SMauro Carvalho Chehab * 4532cf86f6SMauro Carvalho Chehab * This function returns -EINVAL if the pulse violates the state machine 4632cf86f6SMauro Carvalho Chehab */ 47d8b4b582SDavid Härdeman static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev) 4832cf86f6SMauro Carvalho Chehab { 49d8b4b582SDavid Härdeman struct nec_dec *data = &dev->raw->nec; 5032cf86f6SMauro Carvalho Chehab u32 scancode; 5132cf86f6SMauro Carvalho Chehab u8 address, not_address, command, not_command; 5232cf86f6SMauro Carvalho Chehab 5352b66144SMauro Carvalho Chehab if (!(dev->raw->enabled_protocols & RC_TYPE_NEC)) 5432cf86f6SMauro Carvalho Chehab return 0; 5532cf86f6SMauro Carvalho Chehab 5632cf86f6SMauro Carvalho Chehab if (!is_timing_event(ev)) { 5732cf86f6SMauro Carvalho Chehab if (ev.reset) 5832cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 5932cf86f6SMauro Carvalho Chehab return 0; 6032cf86f6SMauro Carvalho Chehab } 6132cf86f6SMauro Carvalho Chehab 6232cf86f6SMauro Carvalho Chehab IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n", 6332cf86f6SMauro Carvalho Chehab data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 6432cf86f6SMauro Carvalho Chehab 6532cf86f6SMauro Carvalho Chehab switch (data->state) { 6632cf86f6SMauro Carvalho Chehab 6732cf86f6SMauro Carvalho Chehab case STATE_INACTIVE: 6832cf86f6SMauro Carvalho Chehab if (!ev.pulse) 6932cf86f6SMauro Carvalho Chehab break; 7032cf86f6SMauro Carvalho Chehab 7132cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2)) { 7232cf86f6SMauro Carvalho Chehab data->is_nec_x = false; 7332cf86f6SMauro Carvalho Chehab data->necx_repeat = false; 7432cf86f6SMauro Carvalho Chehab } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) 7532cf86f6SMauro Carvalho Chehab data->is_nec_x = true; 7632cf86f6SMauro Carvalho Chehab else 7732cf86f6SMauro Carvalho Chehab break; 7832cf86f6SMauro Carvalho Chehab 7932cf86f6SMauro Carvalho Chehab data->count = 0; 8032cf86f6SMauro Carvalho Chehab data->state = STATE_HEADER_SPACE; 8132cf86f6SMauro Carvalho Chehab return 0; 8232cf86f6SMauro Carvalho Chehab 8332cf86f6SMauro Carvalho Chehab case STATE_HEADER_SPACE: 8432cf86f6SMauro Carvalho Chehab if (ev.pulse) 8532cf86f6SMauro Carvalho Chehab break; 8632cf86f6SMauro Carvalho Chehab 8732cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) { 8832cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE; 8932cf86f6SMauro Carvalho Chehab return 0; 9032cf86f6SMauro Carvalho Chehab } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) { 91*21d33014SMariusz Białończyk if (!dev->keypressed) { 92*21d33014SMariusz Białończyk IR_dprintk(1, "Discarding last key repeat: event after key up\n"); 93*21d33014SMariusz Białończyk } else { 94ca86674bSMauro Carvalho Chehab rc_repeat(dev); 9532cf86f6SMauro Carvalho Chehab IR_dprintk(1, "Repeat last key\n"); 9632cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_PULSE; 97*21d33014SMariusz Białończyk } 9832cf86f6SMauro Carvalho Chehab return 0; 9932cf86f6SMauro Carvalho Chehab } 10032cf86f6SMauro Carvalho Chehab 10132cf86f6SMauro Carvalho Chehab break; 10232cf86f6SMauro Carvalho Chehab 10332cf86f6SMauro Carvalho Chehab case STATE_BIT_PULSE: 10432cf86f6SMauro Carvalho Chehab if (!ev.pulse) 10532cf86f6SMauro Carvalho Chehab break; 10632cf86f6SMauro Carvalho Chehab 10732cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2)) 10832cf86f6SMauro Carvalho Chehab break; 10932cf86f6SMauro Carvalho Chehab 11032cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_SPACE; 11132cf86f6SMauro Carvalho Chehab return 0; 11232cf86f6SMauro Carvalho Chehab 11332cf86f6SMauro Carvalho Chehab case STATE_BIT_SPACE: 11432cf86f6SMauro Carvalho Chehab if (ev.pulse) 11532cf86f6SMauro Carvalho Chehab break; 11632cf86f6SMauro Carvalho Chehab 11732cf86f6SMauro Carvalho Chehab if (data->necx_repeat && data->count == NECX_REPEAT_BITS && 11832cf86f6SMauro Carvalho Chehab geq_margin(ev.duration, 11932cf86f6SMauro Carvalho Chehab NEC_TRAILER_SPACE, NEC_UNIT / 2)) { 12032cf86f6SMauro Carvalho Chehab IR_dprintk(1, "Repeat last key\n"); 121ca86674bSMauro Carvalho Chehab rc_repeat(dev); 12232cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 12332cf86f6SMauro Carvalho Chehab return 0; 12432cf86f6SMauro Carvalho Chehab 12532cf86f6SMauro Carvalho Chehab } else if (data->count > NECX_REPEAT_BITS) 12632cf86f6SMauro Carvalho Chehab data->necx_repeat = false; 12732cf86f6SMauro Carvalho Chehab 12832cf86f6SMauro Carvalho Chehab data->bits <<= 1; 12932cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2)) 13032cf86f6SMauro Carvalho Chehab data->bits |= 1; 13132cf86f6SMauro Carvalho Chehab else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2)) 13232cf86f6SMauro Carvalho Chehab break; 13332cf86f6SMauro Carvalho Chehab data->count++; 13432cf86f6SMauro Carvalho Chehab 13532cf86f6SMauro Carvalho Chehab if (data->count == NEC_NBITS) 13632cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_PULSE; 13732cf86f6SMauro Carvalho Chehab else 13832cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE; 13932cf86f6SMauro Carvalho Chehab 14032cf86f6SMauro Carvalho Chehab return 0; 14132cf86f6SMauro Carvalho Chehab 14232cf86f6SMauro Carvalho Chehab case STATE_TRAILER_PULSE: 14332cf86f6SMauro Carvalho Chehab if (!ev.pulse) 14432cf86f6SMauro Carvalho Chehab break; 14532cf86f6SMauro Carvalho Chehab 14632cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2)) 14732cf86f6SMauro Carvalho Chehab break; 14832cf86f6SMauro Carvalho Chehab 14932cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_SPACE; 15032cf86f6SMauro Carvalho Chehab return 0; 15132cf86f6SMauro Carvalho Chehab 15232cf86f6SMauro Carvalho Chehab case STATE_TRAILER_SPACE: 15332cf86f6SMauro Carvalho Chehab if (ev.pulse) 15432cf86f6SMauro Carvalho Chehab break; 15532cf86f6SMauro Carvalho Chehab 15632cf86f6SMauro Carvalho Chehab if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) 15732cf86f6SMauro Carvalho Chehab break; 15832cf86f6SMauro Carvalho Chehab 15932cf86f6SMauro Carvalho Chehab address = bitrev8((data->bits >> 24) & 0xff); 16032cf86f6SMauro Carvalho Chehab not_address = bitrev8((data->bits >> 16) & 0xff); 16132cf86f6SMauro Carvalho Chehab command = bitrev8((data->bits >> 8) & 0xff); 16232cf86f6SMauro Carvalho Chehab not_command = bitrev8((data->bits >> 0) & 0xff); 16332cf86f6SMauro Carvalho Chehab 16432cf86f6SMauro Carvalho Chehab if ((command ^ not_command) != 0xff) { 16532cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC checksum error: received 0x%08x\n", 16632cf86f6SMauro Carvalho Chehab data->bits); 16732cf86f6SMauro Carvalho Chehab break; 16832cf86f6SMauro Carvalho Chehab } 16932cf86f6SMauro Carvalho Chehab 17032cf86f6SMauro Carvalho Chehab if ((address ^ not_address) != 0xff) { 17132cf86f6SMauro Carvalho Chehab /* Extended NEC */ 17232cf86f6SMauro Carvalho Chehab scancode = address << 16 | 17332cf86f6SMauro Carvalho Chehab not_address << 8 | 17432cf86f6SMauro Carvalho Chehab command; 17532cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode); 17632cf86f6SMauro Carvalho Chehab } else { 17732cf86f6SMauro Carvalho Chehab /* Normal NEC */ 17832cf86f6SMauro Carvalho Chehab scancode = address << 8 | command; 17932cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); 18032cf86f6SMauro Carvalho Chehab } 18132cf86f6SMauro Carvalho Chehab 18232cf86f6SMauro Carvalho Chehab if (data->is_nec_x) 18332cf86f6SMauro Carvalho Chehab data->necx_repeat = true; 18432cf86f6SMauro Carvalho Chehab 185ca86674bSMauro Carvalho Chehab rc_keydown(dev, scancode, 0); 18632cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 18732cf86f6SMauro Carvalho Chehab return 0; 18832cf86f6SMauro Carvalho Chehab } 18932cf86f6SMauro Carvalho Chehab 19032cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n", 19132cf86f6SMauro Carvalho Chehab data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 19232cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 19332cf86f6SMauro Carvalho Chehab return -EINVAL; 19432cf86f6SMauro Carvalho Chehab } 19532cf86f6SMauro Carvalho Chehab 19632cf86f6SMauro Carvalho Chehab static struct ir_raw_handler nec_handler = { 19752b66144SMauro Carvalho Chehab .protocols = RC_TYPE_NEC, 19832cf86f6SMauro Carvalho Chehab .decode = ir_nec_decode, 19932cf86f6SMauro Carvalho Chehab }; 20032cf86f6SMauro Carvalho Chehab 20132cf86f6SMauro Carvalho Chehab static int __init ir_nec_decode_init(void) 20232cf86f6SMauro Carvalho Chehab { 20332cf86f6SMauro Carvalho Chehab ir_raw_handler_register(&nec_handler); 20432cf86f6SMauro Carvalho Chehab 20532cf86f6SMauro Carvalho Chehab printk(KERN_INFO "IR NEC protocol handler initialized\n"); 20632cf86f6SMauro Carvalho Chehab return 0; 20732cf86f6SMauro Carvalho Chehab } 20832cf86f6SMauro Carvalho Chehab 20932cf86f6SMauro Carvalho Chehab static void __exit ir_nec_decode_exit(void) 21032cf86f6SMauro Carvalho Chehab { 21132cf86f6SMauro Carvalho Chehab ir_raw_handler_unregister(&nec_handler); 21232cf86f6SMauro Carvalho Chehab } 21332cf86f6SMauro Carvalho Chehab 21432cf86f6SMauro Carvalho Chehab module_init(ir_nec_decode_init); 21532cf86f6SMauro Carvalho Chehab module_exit(ir_nec_decode_exit); 21632cf86f6SMauro Carvalho Chehab 21732cf86f6SMauro Carvalho Chehab MODULE_LICENSE("GPL"); 21832cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); 21932cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); 22032cf86f6SMauro Carvalho Chehab MODULE_DESCRIPTION("NEC IR protocol decoder"); 221