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