0001
0002
0003
0004
0005
0006 #include <linux/bitrev.h>
0007 #include <linux/module.h>
0008 #include "rc-core-priv.h"
0009
0010 #define NEC_NBITS 32
0011 #define NEC_UNIT 563
0012 #define NEC_HEADER_PULSE (16 * NEC_UNIT)
0013 #define NECX_HEADER_PULSE (8 * NEC_UNIT)
0014 #define NEC_HEADER_SPACE (8 * NEC_UNIT)
0015 #define NEC_REPEAT_SPACE (4 * NEC_UNIT)
0016 #define NEC_BIT_PULSE (1 * NEC_UNIT)
0017 #define NEC_BIT_0_SPACE (1 * NEC_UNIT)
0018 #define NEC_BIT_1_SPACE (3 * NEC_UNIT)
0019 #define NEC_TRAILER_PULSE (1 * NEC_UNIT)
0020 #define NEC_TRAILER_SPACE (10 * NEC_UNIT)
0021 #define NECX_REPEAT_BITS 1
0022
0023 enum nec_state {
0024 STATE_INACTIVE,
0025 STATE_HEADER_SPACE,
0026 STATE_BIT_PULSE,
0027 STATE_BIT_SPACE,
0028 STATE_TRAILER_PULSE,
0029 STATE_TRAILER_SPACE,
0030 };
0031
0032
0033
0034
0035
0036
0037
0038
0039 static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
0040 {
0041 struct nec_dec *data = &dev->raw->nec;
0042 u32 scancode;
0043 enum rc_proto rc_proto;
0044 u8 address, not_address, command, not_command;
0045
0046 if (!is_timing_event(ev)) {
0047 if (ev.overflow)
0048 data->state = STATE_INACTIVE;
0049 return 0;
0050 }
0051
0052 dev_dbg(&dev->dev, "NEC decode started at state %d (%uus %s)\n",
0053 data->state, ev.duration, TO_STR(ev.pulse));
0054
0055 switch (data->state) {
0056
0057 case STATE_INACTIVE:
0058 if (!ev.pulse)
0059 break;
0060
0061 if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
0062 data->is_nec_x = false;
0063 data->necx_repeat = false;
0064 } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
0065 data->is_nec_x = true;
0066 else
0067 break;
0068
0069 data->count = 0;
0070 data->state = STATE_HEADER_SPACE;
0071 return 0;
0072
0073 case STATE_HEADER_SPACE:
0074 if (ev.pulse)
0075 break;
0076
0077 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
0078 data->state = STATE_BIT_PULSE;
0079 return 0;
0080 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
0081 data->state = STATE_TRAILER_PULSE;
0082 return 0;
0083 }
0084
0085 break;
0086
0087 case STATE_BIT_PULSE:
0088 if (!ev.pulse)
0089 break;
0090
0091 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
0092 break;
0093
0094 data->state = STATE_BIT_SPACE;
0095 return 0;
0096
0097 case STATE_BIT_SPACE:
0098 if (ev.pulse)
0099 break;
0100
0101 if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
0102 geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
0103 dev_dbg(&dev->dev, "Repeat last key\n");
0104 rc_repeat(dev);
0105 data->state = STATE_INACTIVE;
0106 return 0;
0107 } else if (data->count > NECX_REPEAT_BITS)
0108 data->necx_repeat = false;
0109
0110 data->bits <<= 1;
0111 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
0112 data->bits |= 1;
0113 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
0114 break;
0115 data->count++;
0116
0117 if (data->count == NEC_NBITS)
0118 data->state = STATE_TRAILER_PULSE;
0119 else
0120 data->state = STATE_BIT_PULSE;
0121
0122 return 0;
0123
0124 case STATE_TRAILER_PULSE:
0125 if (!ev.pulse)
0126 break;
0127
0128 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
0129 break;
0130
0131 data->state = STATE_TRAILER_SPACE;
0132 return 0;
0133
0134 case STATE_TRAILER_SPACE:
0135 if (ev.pulse)
0136 break;
0137
0138 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
0139 break;
0140
0141 if (data->count == NEC_NBITS) {
0142 address = bitrev8((data->bits >> 24) & 0xff);
0143 not_address = bitrev8((data->bits >> 16) & 0xff);
0144 command = bitrev8((data->bits >> 8) & 0xff);
0145 not_command = bitrev8((data->bits >> 0) & 0xff);
0146
0147 scancode = ir_nec_bytes_to_scancode(address,
0148 not_address,
0149 command,
0150 not_command,
0151 &rc_proto);
0152
0153 if (data->is_nec_x)
0154 data->necx_repeat = true;
0155
0156 rc_keydown(dev, rc_proto, scancode, 0);
0157 } else {
0158 rc_repeat(dev);
0159 }
0160
0161 data->state = STATE_INACTIVE;
0162 return 0;
0163 }
0164
0165 dev_dbg(&dev->dev, "NEC decode failed at count %d state %d (%uus %s)\n",
0166 data->count, data->state, ev.duration, TO_STR(ev.pulse));
0167 data->state = STATE_INACTIVE;
0168 return -EINVAL;
0169 }
0170
0171
0172
0173
0174
0175
0176 static u32 ir_nec_scancode_to_raw(enum rc_proto protocol, u32 scancode)
0177 {
0178 unsigned int addr, addr_inv, data, data_inv;
0179
0180 data = scancode & 0xff;
0181
0182 if (protocol == RC_PROTO_NEC32) {
0183
0184
0185 addr_inv = (scancode >> 24) & 0xff;
0186 addr = (scancode >> 16) & 0xff;
0187 data_inv = (scancode >> 8) & 0xff;
0188 } else if (protocol == RC_PROTO_NECX) {
0189
0190
0191 addr = (scancode >> 16) & 0xff;
0192 addr_inv = (scancode >> 8) & 0xff;
0193 data_inv = data ^ 0xff;
0194 } else {
0195
0196
0197 addr = (scancode >> 8) & 0xff;
0198 addr_inv = addr ^ 0xff;
0199 data_inv = data ^ 0xff;
0200 }
0201
0202
0203 return data_inv << 24 |
0204 data << 16 |
0205 addr_inv << 8 |
0206 addr;
0207 }
0208
0209 static const struct ir_raw_timings_pd ir_nec_timings = {
0210 .header_pulse = NEC_HEADER_PULSE,
0211 .header_space = NEC_HEADER_SPACE,
0212 .bit_pulse = NEC_BIT_PULSE,
0213 .bit_space[0] = NEC_BIT_0_SPACE,
0214 .bit_space[1] = NEC_BIT_1_SPACE,
0215 .trailer_pulse = NEC_TRAILER_PULSE,
0216 .trailer_space = NEC_TRAILER_SPACE,
0217 .msb_first = 0,
0218 };
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 static int ir_nec_encode(enum rc_proto protocol, u32 scancode,
0233 struct ir_raw_event *events, unsigned int max)
0234 {
0235 struct ir_raw_event *e = events;
0236 int ret;
0237 u32 raw;
0238
0239
0240 raw = ir_nec_scancode_to_raw(protocol, scancode);
0241
0242
0243 ret = ir_raw_gen_pd(&e, max, &ir_nec_timings, NEC_NBITS, raw);
0244 if (ret < 0)
0245 return ret;
0246
0247 return e - events;
0248 }
0249
0250 static struct ir_raw_handler nec_handler = {
0251 .protocols = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
0252 RC_PROTO_BIT_NEC32,
0253 .decode = ir_nec_decode,
0254 .encode = ir_nec_encode,
0255 .carrier = 38000,
0256 .min_timeout = NEC_TRAILER_SPACE,
0257 };
0258
0259 static int __init ir_nec_decode_init(void)
0260 {
0261 ir_raw_handler_register(&nec_handler);
0262
0263 printk(KERN_INFO "IR NEC protocol handler initialized\n");
0264 return 0;
0265 }
0266
0267 static void __exit ir_nec_decode_exit(void)
0268 {
0269 ir_raw_handler_unregister(&nec_handler);
0270 }
0271
0272 module_init(ir_nec_decode_init);
0273 module_exit(ir_nec_decode_exit);
0274
0275 MODULE_LICENSE("GPL v2");
0276 MODULE_AUTHOR("Mauro Carvalho Chehab");
0277 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
0278 MODULE_DESCRIPTION("NEC IR protocol decoder");