1*32cf86f6SMauro Carvalho Chehab /* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol 2*32cf86f6SMauro Carvalho Chehab * 3*32cf86f6SMauro Carvalho Chehab * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com> 4*32cf86f6SMauro Carvalho Chehab * 5*32cf86f6SMauro Carvalho Chehab * This program is free software; you can redistribute it and/or modify 6*32cf86f6SMauro Carvalho Chehab * it under the terms of the GNU General Public License as published by 7*32cf86f6SMauro Carvalho Chehab * the Free Software Foundation version 2 of the License. 8*32cf86f6SMauro Carvalho Chehab * 9*32cf86f6SMauro Carvalho Chehab * This program is distributed in the hope that it will be useful, 10*32cf86f6SMauro Carvalho Chehab * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*32cf86f6SMauro Carvalho Chehab * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*32cf86f6SMauro Carvalho Chehab * GNU General Public License for more details. 13*32cf86f6SMauro Carvalho Chehab */ 14*32cf86f6SMauro Carvalho Chehab 15*32cf86f6SMauro Carvalho Chehab #include <linux/bitrev.h> 16*32cf86f6SMauro Carvalho Chehab #include "ir-core-priv.h" 17*32cf86f6SMauro Carvalho Chehab 18*32cf86f6SMauro Carvalho Chehab #define NEC_NBITS 32 19*32cf86f6SMauro Carvalho Chehab #define NEC_UNIT 562500 /* ns */ 20*32cf86f6SMauro Carvalho Chehab #define NEC_HEADER_PULSE (16 * NEC_UNIT) 21*32cf86f6SMauro Carvalho Chehab #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */ 22*32cf86f6SMauro Carvalho Chehab #define NEC_HEADER_SPACE (8 * NEC_UNIT) 23*32cf86f6SMauro Carvalho Chehab #define NEC_REPEAT_SPACE (4 * NEC_UNIT) 24*32cf86f6SMauro Carvalho Chehab #define NEC_BIT_PULSE (1 * NEC_UNIT) 25*32cf86f6SMauro Carvalho Chehab #define NEC_BIT_0_SPACE (1 * NEC_UNIT) 26*32cf86f6SMauro Carvalho Chehab #define NEC_BIT_1_SPACE (3 * NEC_UNIT) 27*32cf86f6SMauro Carvalho Chehab #define NEC_TRAILER_PULSE (1 * NEC_UNIT) 28*32cf86f6SMauro Carvalho Chehab #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */ 29*32cf86f6SMauro Carvalho Chehab #define NECX_REPEAT_BITS 1 30*32cf86f6SMauro Carvalho Chehab 31*32cf86f6SMauro Carvalho Chehab enum nec_state { 32*32cf86f6SMauro Carvalho Chehab STATE_INACTIVE, 33*32cf86f6SMauro Carvalho Chehab STATE_HEADER_SPACE, 34*32cf86f6SMauro Carvalho Chehab STATE_BIT_PULSE, 35*32cf86f6SMauro Carvalho Chehab STATE_BIT_SPACE, 36*32cf86f6SMauro Carvalho Chehab STATE_TRAILER_PULSE, 37*32cf86f6SMauro Carvalho Chehab STATE_TRAILER_SPACE, 38*32cf86f6SMauro Carvalho Chehab }; 39*32cf86f6SMauro Carvalho Chehab 40*32cf86f6SMauro Carvalho Chehab /** 41*32cf86f6SMauro Carvalho Chehab * ir_nec_decode() - Decode one NEC pulse or space 42*32cf86f6SMauro Carvalho Chehab * @input_dev: the struct input_dev descriptor of the device 43*32cf86f6SMauro Carvalho Chehab * @duration: the struct ir_raw_event descriptor of the pulse/space 44*32cf86f6SMauro Carvalho Chehab * 45*32cf86f6SMauro Carvalho Chehab * This function returns -EINVAL if the pulse violates the state machine 46*32cf86f6SMauro Carvalho Chehab */ 47*32cf86f6SMauro Carvalho Chehab static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev) 48*32cf86f6SMauro Carvalho Chehab { 49*32cf86f6SMauro Carvalho Chehab struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); 50*32cf86f6SMauro Carvalho Chehab struct nec_dec *data = &ir_dev->raw->nec; 51*32cf86f6SMauro Carvalho Chehab u32 scancode; 52*32cf86f6SMauro Carvalho Chehab u8 address, not_address, command, not_command; 53*32cf86f6SMauro Carvalho Chehab 54*32cf86f6SMauro Carvalho Chehab if (!(ir_dev->raw->enabled_protocols & IR_TYPE_NEC)) 55*32cf86f6SMauro Carvalho Chehab return 0; 56*32cf86f6SMauro Carvalho Chehab 57*32cf86f6SMauro Carvalho Chehab if (!is_timing_event(ev)) { 58*32cf86f6SMauro Carvalho Chehab if (ev.reset) 59*32cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 60*32cf86f6SMauro Carvalho Chehab return 0; 61*32cf86f6SMauro Carvalho Chehab } 62*32cf86f6SMauro Carvalho Chehab 63*32cf86f6SMauro Carvalho Chehab IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n", 64*32cf86f6SMauro Carvalho Chehab data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 65*32cf86f6SMauro Carvalho Chehab 66*32cf86f6SMauro Carvalho Chehab switch (data->state) { 67*32cf86f6SMauro Carvalho Chehab 68*32cf86f6SMauro Carvalho Chehab case STATE_INACTIVE: 69*32cf86f6SMauro Carvalho Chehab if (!ev.pulse) 70*32cf86f6SMauro Carvalho Chehab break; 71*32cf86f6SMauro Carvalho Chehab 72*32cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2)) { 73*32cf86f6SMauro Carvalho Chehab data->is_nec_x = false; 74*32cf86f6SMauro Carvalho Chehab data->necx_repeat = false; 75*32cf86f6SMauro Carvalho Chehab } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) 76*32cf86f6SMauro Carvalho Chehab data->is_nec_x = true; 77*32cf86f6SMauro Carvalho Chehab else 78*32cf86f6SMauro Carvalho Chehab break; 79*32cf86f6SMauro Carvalho Chehab 80*32cf86f6SMauro Carvalho Chehab data->count = 0; 81*32cf86f6SMauro Carvalho Chehab data->state = STATE_HEADER_SPACE; 82*32cf86f6SMauro Carvalho Chehab return 0; 83*32cf86f6SMauro Carvalho Chehab 84*32cf86f6SMauro Carvalho Chehab case STATE_HEADER_SPACE: 85*32cf86f6SMauro Carvalho Chehab if (ev.pulse) 86*32cf86f6SMauro Carvalho Chehab break; 87*32cf86f6SMauro Carvalho Chehab 88*32cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) { 89*32cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE; 90*32cf86f6SMauro Carvalho Chehab return 0; 91*32cf86f6SMauro Carvalho Chehab } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) { 92*32cf86f6SMauro Carvalho Chehab ir_repeat(input_dev); 93*32cf86f6SMauro Carvalho Chehab IR_dprintk(1, "Repeat last key\n"); 94*32cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_PULSE; 95*32cf86f6SMauro Carvalho Chehab return 0; 96*32cf86f6SMauro Carvalho Chehab } 97*32cf86f6SMauro Carvalho Chehab 98*32cf86f6SMauro Carvalho Chehab break; 99*32cf86f6SMauro Carvalho Chehab 100*32cf86f6SMauro Carvalho Chehab case STATE_BIT_PULSE: 101*32cf86f6SMauro Carvalho Chehab if (!ev.pulse) 102*32cf86f6SMauro Carvalho Chehab break; 103*32cf86f6SMauro Carvalho Chehab 104*32cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2)) 105*32cf86f6SMauro Carvalho Chehab break; 106*32cf86f6SMauro Carvalho Chehab 107*32cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_SPACE; 108*32cf86f6SMauro Carvalho Chehab return 0; 109*32cf86f6SMauro Carvalho Chehab 110*32cf86f6SMauro Carvalho Chehab case STATE_BIT_SPACE: 111*32cf86f6SMauro Carvalho Chehab if (ev.pulse) 112*32cf86f6SMauro Carvalho Chehab break; 113*32cf86f6SMauro Carvalho Chehab 114*32cf86f6SMauro Carvalho Chehab if (data->necx_repeat && data->count == NECX_REPEAT_BITS && 115*32cf86f6SMauro Carvalho Chehab geq_margin(ev.duration, 116*32cf86f6SMauro Carvalho Chehab NEC_TRAILER_SPACE, NEC_UNIT / 2)) { 117*32cf86f6SMauro Carvalho Chehab IR_dprintk(1, "Repeat last key\n"); 118*32cf86f6SMauro Carvalho Chehab ir_repeat(input_dev); 119*32cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 120*32cf86f6SMauro Carvalho Chehab return 0; 121*32cf86f6SMauro Carvalho Chehab 122*32cf86f6SMauro Carvalho Chehab } else if (data->count > NECX_REPEAT_BITS) 123*32cf86f6SMauro Carvalho Chehab data->necx_repeat = false; 124*32cf86f6SMauro Carvalho Chehab 125*32cf86f6SMauro Carvalho Chehab data->bits <<= 1; 126*32cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2)) 127*32cf86f6SMauro Carvalho Chehab data->bits |= 1; 128*32cf86f6SMauro Carvalho Chehab else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2)) 129*32cf86f6SMauro Carvalho Chehab break; 130*32cf86f6SMauro Carvalho Chehab data->count++; 131*32cf86f6SMauro Carvalho Chehab 132*32cf86f6SMauro Carvalho Chehab if (data->count == NEC_NBITS) 133*32cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_PULSE; 134*32cf86f6SMauro Carvalho Chehab else 135*32cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE; 136*32cf86f6SMauro Carvalho Chehab 137*32cf86f6SMauro Carvalho Chehab return 0; 138*32cf86f6SMauro Carvalho Chehab 139*32cf86f6SMauro Carvalho Chehab case STATE_TRAILER_PULSE: 140*32cf86f6SMauro Carvalho Chehab if (!ev.pulse) 141*32cf86f6SMauro Carvalho Chehab break; 142*32cf86f6SMauro Carvalho Chehab 143*32cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2)) 144*32cf86f6SMauro Carvalho Chehab break; 145*32cf86f6SMauro Carvalho Chehab 146*32cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_SPACE; 147*32cf86f6SMauro Carvalho Chehab return 0; 148*32cf86f6SMauro Carvalho Chehab 149*32cf86f6SMauro Carvalho Chehab case STATE_TRAILER_SPACE: 150*32cf86f6SMauro Carvalho Chehab if (ev.pulse) 151*32cf86f6SMauro Carvalho Chehab break; 152*32cf86f6SMauro Carvalho Chehab 153*32cf86f6SMauro Carvalho Chehab if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) 154*32cf86f6SMauro Carvalho Chehab break; 155*32cf86f6SMauro Carvalho Chehab 156*32cf86f6SMauro Carvalho Chehab address = bitrev8((data->bits >> 24) & 0xff); 157*32cf86f6SMauro Carvalho Chehab not_address = bitrev8((data->bits >> 16) & 0xff); 158*32cf86f6SMauro Carvalho Chehab command = bitrev8((data->bits >> 8) & 0xff); 159*32cf86f6SMauro Carvalho Chehab not_command = bitrev8((data->bits >> 0) & 0xff); 160*32cf86f6SMauro Carvalho Chehab 161*32cf86f6SMauro Carvalho Chehab if ((command ^ not_command) != 0xff) { 162*32cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC checksum error: received 0x%08x\n", 163*32cf86f6SMauro Carvalho Chehab data->bits); 164*32cf86f6SMauro Carvalho Chehab break; 165*32cf86f6SMauro Carvalho Chehab } 166*32cf86f6SMauro Carvalho Chehab 167*32cf86f6SMauro Carvalho Chehab if ((address ^ not_address) != 0xff) { 168*32cf86f6SMauro Carvalho Chehab /* Extended NEC */ 169*32cf86f6SMauro Carvalho Chehab scancode = address << 16 | 170*32cf86f6SMauro Carvalho Chehab not_address << 8 | 171*32cf86f6SMauro Carvalho Chehab command; 172*32cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode); 173*32cf86f6SMauro Carvalho Chehab } else { 174*32cf86f6SMauro Carvalho Chehab /* Normal NEC */ 175*32cf86f6SMauro Carvalho Chehab scancode = address << 8 | command; 176*32cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); 177*32cf86f6SMauro Carvalho Chehab } 178*32cf86f6SMauro Carvalho Chehab 179*32cf86f6SMauro Carvalho Chehab if (data->is_nec_x) 180*32cf86f6SMauro Carvalho Chehab data->necx_repeat = true; 181*32cf86f6SMauro Carvalho Chehab 182*32cf86f6SMauro Carvalho Chehab ir_keydown(input_dev, scancode, 0); 183*32cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 184*32cf86f6SMauro Carvalho Chehab return 0; 185*32cf86f6SMauro Carvalho Chehab } 186*32cf86f6SMauro Carvalho Chehab 187*32cf86f6SMauro Carvalho Chehab IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n", 188*32cf86f6SMauro Carvalho Chehab data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 189*32cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE; 190*32cf86f6SMauro Carvalho Chehab return -EINVAL; 191*32cf86f6SMauro Carvalho Chehab } 192*32cf86f6SMauro Carvalho Chehab 193*32cf86f6SMauro Carvalho Chehab static struct ir_raw_handler nec_handler = { 194*32cf86f6SMauro Carvalho Chehab .protocols = IR_TYPE_NEC, 195*32cf86f6SMauro Carvalho Chehab .decode = ir_nec_decode, 196*32cf86f6SMauro Carvalho Chehab }; 197*32cf86f6SMauro Carvalho Chehab 198*32cf86f6SMauro Carvalho Chehab static int __init ir_nec_decode_init(void) 199*32cf86f6SMauro Carvalho Chehab { 200*32cf86f6SMauro Carvalho Chehab ir_raw_handler_register(&nec_handler); 201*32cf86f6SMauro Carvalho Chehab 202*32cf86f6SMauro Carvalho Chehab printk(KERN_INFO "IR NEC protocol handler initialized\n"); 203*32cf86f6SMauro Carvalho Chehab return 0; 204*32cf86f6SMauro Carvalho Chehab } 205*32cf86f6SMauro Carvalho Chehab 206*32cf86f6SMauro Carvalho Chehab static void __exit ir_nec_decode_exit(void) 207*32cf86f6SMauro Carvalho Chehab { 208*32cf86f6SMauro Carvalho Chehab ir_raw_handler_unregister(&nec_handler); 209*32cf86f6SMauro Carvalho Chehab } 210*32cf86f6SMauro Carvalho Chehab 211*32cf86f6SMauro Carvalho Chehab module_init(ir_nec_decode_init); 212*32cf86f6SMauro Carvalho Chehab module_exit(ir_nec_decode_exit); 213*32cf86f6SMauro Carvalho Chehab 214*32cf86f6SMauro Carvalho Chehab MODULE_LICENSE("GPL"); 215*32cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); 216*32cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); 217*32cf86f6SMauro Carvalho Chehab MODULE_DESCRIPTION("NEC IR protocol decoder"); 218