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> 167a707b89SPaul Gortmaker #include <linux/module.h> 17f62de675SMauro Carvalho Chehab #include "rc-core-priv.h" 1832cf86f6SMauro Carvalho Chehab 1932cf86f6SMauro Carvalho Chehab #define NEC_NBITS 32 2032cf86f6SMauro Carvalho Chehab #define NEC_UNIT 562500 /* ns */ 2132cf86f6SMauro Carvalho Chehab #define NEC_HEADER_PULSE (16 * NEC_UNIT) 2232cf86f6SMauro Carvalho Chehab #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */ 2332cf86f6SMauro Carvalho Chehab #define NEC_HEADER_SPACE (8 * NEC_UNIT) 2432cf86f6SMauro Carvalho Chehab #define NEC_REPEAT_SPACE (4 * NEC_UNIT) 2532cf86f6SMauro Carvalho Chehab #define NEC_BIT_PULSE (1 * NEC_UNIT) 2632cf86f6SMauro Carvalho Chehab #define NEC_BIT_0_SPACE (1 * NEC_UNIT) 2732cf86f6SMauro Carvalho Chehab #define NEC_BIT_1_SPACE (3 * NEC_UNIT) 2832cf86f6SMauro Carvalho Chehab #define NEC_TRAILER_PULSE (1 * NEC_UNIT) 2932cf86f6SMauro Carvalho Chehab #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */ 3032cf86f6SMauro Carvalho Chehab #define NECX_REPEAT_BITS 1 3132cf86f6SMauro Carvalho Chehab 3232cf86f6SMauro Carvalho Chehab enum nec_state { 3332cf86f6SMauro Carvalho Chehab STATE_INACTIVE, 3432cf86f6SMauro Carvalho Chehab STATE_HEADER_SPACE, 3532cf86f6SMauro Carvalho Chehab STATE_BIT_PULSE, 3632cf86f6SMauro Carvalho Chehab STATE_BIT_SPACE, 3732cf86f6SMauro Carvalho Chehab STATE_TRAILER_PULSE, 3832cf86f6SMauro Carvalho Chehab STATE_TRAILER_SPACE, 3932cf86f6SMauro Carvalho Chehab }; 4032cf86f6SMauro Carvalho Chehab 4132cf86f6SMauro Carvalho Chehab /** 4232cf86f6SMauro Carvalho Chehab * ir_nec_decode() - Decode one NEC pulse or space 43d8b4b582SDavid Härdeman * @dev: the struct rc_dev descriptor of the device 4432cf86f6SMauro Carvalho Chehab * @duration: the struct ir_raw_event descriptor of the pulse/space 4532cf86f6SMauro Carvalho Chehab * 4632cf86f6SMauro Carvalho Chehab * This function returns -EINVAL if the pulse violates the state machine 4732cf86f6SMauro Carvalho Chehab */ 48d8b4b582SDavid Härdeman static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev) 4932cf86f6SMauro Carvalho Chehab { 50d8b4b582SDavid Härdeman struct nec_dec *data = &dev->raw->nec; 5132cf86f6SMauro Carvalho Chehab u32 scancode; 5232cf86f6SMauro Carvalho Chehab u8 address, not_address, command, not_command; 534be22b6aSJarod Wilson bool send_32bits = false; 5432cf86f6SMauro Carvalho Chehab 5552b66144SMauro Carvalho Chehab if (!(dev->raw->enabled_protocols & RC_TYPE_NEC)) 5632cf86f6SMauro Carvalho Chehab return 0; 5732cf86f6SMauro Carvalho Chehab 5832cf86f6SMauro Carvalho Chehab if (!is_timing_event(ev)) { 5932cf86f6SMauro Carvalho Chehab if (ev.reset) 6032cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 6132cf86f6SMauro Carvalho Chehab return 0; 6232cf86f6SMauro Carvalho Chehab } 6332cf86f6SMauro Carvalho Chehab 6432cf86f6SMauro Carvalho Chehab IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n", 6532cf86f6SMauro Carvalho Chehab data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 6632cf86f6SMauro Carvalho Chehab 6732cf86f6SMauro Carvalho Chehab switch (data->state) { 6832cf86f6SMauro Carvalho Chehab 6932cf86f6SMauro Carvalho Chehab case STATE_INACTIVE: 7032cf86f6SMauro Carvalho Chehab if (!ev.pulse) 7132cf86f6SMauro Carvalho Chehab break; 7232cf86f6SMauro Carvalho Chehab 73*743135e7SSean Young if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) { 7432cf86f6SMauro Carvalho Chehab data->is_nec_x = false; 7532cf86f6SMauro Carvalho Chehab data->necx_repeat = false; 7632cf86f6SMauro Carvalho Chehab } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) 7732cf86f6SMauro Carvalho Chehab data->is_nec_x = true; 7832cf86f6SMauro Carvalho Chehab else 7932cf86f6SMauro Carvalho Chehab break; 8032cf86f6SMauro Carvalho Chehab 8132cf86f6SMauro Carvalho Chehab data->count = 0; 8232cf86f6SMauro Carvalho Chehab data->state = STATE_HEADER_SPACE; 8332cf86f6SMauro Carvalho Chehab return 0; 8432cf86f6SMauro Carvalho Chehab 8532cf86f6SMauro Carvalho Chehab case STATE_HEADER_SPACE: 8632cf86f6SMauro Carvalho Chehab if (ev.pulse) 8732cf86f6SMauro Carvalho Chehab break; 8832cf86f6SMauro Carvalho Chehab 89*743135e7SSean Young if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) { 9032cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE; 9132cf86f6SMauro Carvalho Chehab return 0; 9232cf86f6SMauro Carvalho Chehab } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) { 9321d33014SMariusz Białończyk if (!dev->keypressed) { 9421d33014SMariusz Białończyk IR_dprintk(1, "Discarding last key repeat: event after key up\n"); 9521d33014SMariusz Białończyk } else { 96ca86674bSMauro Carvalho Chehab rc_repeat(dev); 9732cf86f6SMauro Carvalho Chehab IR_dprintk(1, "Repeat last key\n"); 9832cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_PULSE; 9921d33014SMariusz Białończyk } 10032cf86f6SMauro Carvalho Chehab return 0; 10132cf86f6SMauro Carvalho Chehab } 10232cf86f6SMauro Carvalho Chehab 10332cf86f6SMauro Carvalho Chehab break; 10432cf86f6SMauro Carvalho Chehab 10532cf86f6SMauro Carvalho Chehab case STATE_BIT_PULSE: 10632cf86f6SMauro Carvalho Chehab if (!ev.pulse) 10732cf86f6SMauro Carvalho Chehab break; 10832cf86f6SMauro Carvalho Chehab 10932cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2)) 11032cf86f6SMauro Carvalho Chehab break; 11132cf86f6SMauro Carvalho Chehab 11232cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_SPACE; 11332cf86f6SMauro Carvalho Chehab return 0; 11432cf86f6SMauro Carvalho Chehab 11532cf86f6SMauro Carvalho Chehab case STATE_BIT_SPACE: 11632cf86f6SMauro Carvalho Chehab if (ev.pulse) 11732cf86f6SMauro Carvalho Chehab break; 11832cf86f6SMauro Carvalho Chehab 11932cf86f6SMauro Carvalho Chehab if (data->necx_repeat && data->count == NECX_REPEAT_BITS && 12032cf86f6SMauro Carvalho Chehab geq_margin(ev.duration, 12132cf86f6SMauro Carvalho Chehab NEC_TRAILER_SPACE, NEC_UNIT / 2)) { 12232cf86f6SMauro Carvalho Chehab IR_dprintk(1, "Repeat last key\n"); 123ca86674bSMauro Carvalho Chehab rc_repeat(dev); 12432cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 12532cf86f6SMauro Carvalho Chehab return 0; 12632cf86f6SMauro Carvalho Chehab 12732cf86f6SMauro Carvalho Chehab } else if (data->count > NECX_REPEAT_BITS) 12832cf86f6SMauro Carvalho Chehab data->necx_repeat = false; 12932cf86f6SMauro Carvalho Chehab 13032cf86f6SMauro Carvalho Chehab data->bits <<= 1; 13132cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2)) 13232cf86f6SMauro Carvalho Chehab data->bits |= 1; 13332cf86f6SMauro Carvalho Chehab else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2)) 13432cf86f6SMauro Carvalho Chehab break; 13532cf86f6SMauro Carvalho Chehab data->count++; 13632cf86f6SMauro Carvalho Chehab 13732cf86f6SMauro Carvalho Chehab if (data->count == NEC_NBITS) 13832cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_PULSE; 13932cf86f6SMauro Carvalho Chehab else 14032cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE; 14132cf86f6SMauro Carvalho Chehab 14232cf86f6SMauro Carvalho Chehab return 0; 14332cf86f6SMauro Carvalho Chehab 14432cf86f6SMauro Carvalho Chehab case STATE_TRAILER_PULSE: 14532cf86f6SMauro Carvalho Chehab if (!ev.pulse) 14632cf86f6SMauro Carvalho Chehab break; 14732cf86f6SMauro Carvalho Chehab 14832cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2)) 14932cf86f6SMauro Carvalho Chehab break; 15032cf86f6SMauro Carvalho Chehab 15132cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_SPACE; 15232cf86f6SMauro Carvalho Chehab return 0; 15332cf86f6SMauro Carvalho Chehab 15432cf86f6SMauro Carvalho Chehab case STATE_TRAILER_SPACE: 15532cf86f6SMauro Carvalho Chehab if (ev.pulse) 15632cf86f6SMauro Carvalho Chehab break; 15732cf86f6SMauro Carvalho Chehab 15832cf86f6SMauro Carvalho Chehab if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) 15932cf86f6SMauro Carvalho Chehab break; 16032cf86f6SMauro Carvalho Chehab 16132cf86f6SMauro Carvalho Chehab address = bitrev8((data->bits >> 24) & 0xff); 16232cf86f6SMauro Carvalho Chehab not_address = bitrev8((data->bits >> 16) & 0xff); 16332cf86f6SMauro Carvalho Chehab command = bitrev8((data->bits >> 8) & 0xff); 16432cf86f6SMauro Carvalho Chehab not_command = bitrev8((data->bits >> 0) & 0xff); 16532cf86f6SMauro Carvalho Chehab 16632cf86f6SMauro Carvalho Chehab if ((command ^ not_command) != 0xff) { 16732cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC checksum error: received 0x%08x\n", 16832cf86f6SMauro Carvalho Chehab data->bits); 1694be22b6aSJarod Wilson send_32bits = true; 17032cf86f6SMauro Carvalho Chehab } 17132cf86f6SMauro Carvalho Chehab 1724be22b6aSJarod Wilson if (send_32bits) { 1734be22b6aSJarod Wilson /* NEC transport, but modified protocol, used by at 1744be22b6aSJarod Wilson * least Apple and TiVo remotes */ 1754be22b6aSJarod Wilson scancode = data->bits; 1764be22b6aSJarod Wilson IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode); 1774be22b6aSJarod Wilson } else if ((address ^ not_address) != 0xff) { 17832cf86f6SMauro Carvalho Chehab /* Extended NEC */ 17932cf86f6SMauro Carvalho Chehab scancode = address << 16 | 18032cf86f6SMauro Carvalho Chehab not_address << 8 | 18132cf86f6SMauro Carvalho Chehab command; 18232cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode); 18332cf86f6SMauro Carvalho Chehab } else { 18432cf86f6SMauro Carvalho Chehab /* Normal NEC */ 18532cf86f6SMauro Carvalho Chehab scancode = address << 8 | command; 18632cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); 18732cf86f6SMauro Carvalho Chehab } 18832cf86f6SMauro Carvalho Chehab 18932cf86f6SMauro Carvalho Chehab if (data->is_nec_x) 19032cf86f6SMauro Carvalho Chehab data->necx_repeat = true; 19132cf86f6SMauro Carvalho Chehab 192ca86674bSMauro Carvalho Chehab rc_keydown(dev, scancode, 0); 19332cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 19432cf86f6SMauro Carvalho Chehab return 0; 19532cf86f6SMauro Carvalho Chehab } 19632cf86f6SMauro Carvalho Chehab 19739cac375SMauro Carvalho Chehab IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n", 19839cac375SMauro Carvalho Chehab data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 19932cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 20032cf86f6SMauro Carvalho Chehab return -EINVAL; 20132cf86f6SMauro Carvalho Chehab } 20232cf86f6SMauro Carvalho Chehab 20332cf86f6SMauro Carvalho Chehab static struct ir_raw_handler nec_handler = { 20452b66144SMauro Carvalho Chehab .protocols = RC_TYPE_NEC, 20532cf86f6SMauro Carvalho Chehab .decode = ir_nec_decode, 20632cf86f6SMauro Carvalho Chehab }; 20732cf86f6SMauro Carvalho Chehab 20832cf86f6SMauro Carvalho Chehab static int __init ir_nec_decode_init(void) 20932cf86f6SMauro Carvalho Chehab { 21032cf86f6SMauro Carvalho Chehab ir_raw_handler_register(&nec_handler); 21132cf86f6SMauro Carvalho Chehab 21232cf86f6SMauro Carvalho Chehab printk(KERN_INFO "IR NEC protocol handler initialized\n"); 21332cf86f6SMauro Carvalho Chehab return 0; 21432cf86f6SMauro Carvalho Chehab } 21532cf86f6SMauro Carvalho Chehab 21632cf86f6SMauro Carvalho Chehab static void __exit ir_nec_decode_exit(void) 21732cf86f6SMauro Carvalho Chehab { 21832cf86f6SMauro Carvalho Chehab ir_raw_handler_unregister(&nec_handler); 21932cf86f6SMauro Carvalho Chehab } 22032cf86f6SMauro Carvalho Chehab 22132cf86f6SMauro Carvalho Chehab module_init(ir_nec_decode_init); 22232cf86f6SMauro Carvalho Chehab module_exit(ir_nec_decode_exit); 22332cf86f6SMauro Carvalho Chehab 22432cf86f6SMauro Carvalho Chehab MODULE_LICENSE("GPL"); 22532cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); 22632cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); 22732cf86f6SMauro Carvalho Chehab MODULE_DESCRIPTION("NEC IR protocol decoder"); 228