blob: 1c0cf0300d2d90299a5f03251b897522ae86fd85 [file] [log] [blame]
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -03001/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03002 *
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
David Härdeman724e2492010-04-08 13:10:00 -030015#include <linux/bitrev.h>
Mauro Carvalho Chehab3f113e32010-04-08 15:10:27 -030016#include "ir-core-priv.h"
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030017
Mauro Carvalho Chehab67780d62010-04-03 20:33:00 -030018#define NEC_NBITS 32
David Härdeman724e2492010-04-08 13:10:00 -030019#define NEC_UNIT 562500 /* ns */
David Härdemane40b1122010-04-15 18:46:00 -030020#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)
Maxim Levitskye31f4122010-07-31 11:59:19 -030023#define NEC_REPEAT_SPACE (4 * NEC_UNIT)
David Härdemane40b1122010-04-15 18:46:00 -030024#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 */
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030029
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030030enum nec_state {
31 STATE_INACTIVE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030032 STATE_HEADER_SPACE,
David Härdeman724e2492010-04-08 13:10:00 -030033 STATE_BIT_PULSE,
34 STATE_BIT_SPACE,
35 STATE_TRAILER_PULSE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030036 STATE_TRAILER_SPACE,
37};
38
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030039/**
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -030040 * ir_nec_decode() - Decode one NEC pulse or space
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030041 * @input_dev: the struct input_dev descriptor of the device
David Härdemane40b1122010-04-15 18:46:00 -030042 * @duration: the struct ir_raw_event descriptor of the pulse/space
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030043 *
44 * This function returns -EINVAL if the pulse violates the state machine
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030045 */
David Härdemane40b1122010-04-15 18:46:00 -030046static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030047{
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030048 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
David Härdemanc2163692010-06-13 17:29:36 -030049 struct nec_dec *data = &ir_dev->raw->nec;
David Härdeman724e2492010-04-08 13:10:00 -030050 u32 scancode;
51 u8 address, not_address, command, not_command;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030052
David Härdeman667c9eb2010-06-13 17:29:31 -030053 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_NEC))
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -030054 return 0;
55
David Härdemane40b1122010-04-15 18:46:00 -030056 if (IS_RESET(ev)) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030057 data->state = STATE_INACTIVE;
58 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030059 }
60
David Härdemane40b1122010-04-15 18:46:00 -030061 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
62 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman724e2492010-04-08 13:10:00 -030063
64 switch (data->state) {
65
66 case STATE_INACTIVE:
David Härdemane40b1122010-04-15 18:46:00 -030067 if (!ev.pulse)
68 break;
69
70 if (!eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2) &&
71 !eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
72 break;
73
74 data->count = 0;
75 data->state = STATE_HEADER_SPACE;
David Härdeman724e2492010-04-08 13:10:00 -030076 return 0;
77
78 case STATE_HEADER_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -030079 if (ev.pulse)
80 break;
81
82 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
David Härdeman724e2492010-04-08 13:10:00 -030083 data->state = STATE_BIT_PULSE;
84 return 0;
David Härdemane40b1122010-04-15 18:46:00 -030085 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
David Härdeman724e2492010-04-08 13:10:00 -030086 ir_repeat(input_dev);
87 IR_dprintk(1, "Repeat last key\n");
88 data->state = STATE_TRAILER_PULSE;
89 return 0;
90 }
David Härdemane40b1122010-04-15 18:46:00 -030091
David Härdeman724e2492010-04-08 13:10:00 -030092 break;
93
94 case STATE_BIT_PULSE:
David Härdemane40b1122010-04-15 18:46:00 -030095 if (!ev.pulse)
96 break;
97
98 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
99 break;
100
101 data->state = STATE_BIT_SPACE;
102 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300103
104 case STATE_BIT_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -0300105 if (ev.pulse)
David Härdeman724e2492010-04-08 13:10:00 -0300106 break;
107
David Härdemanc2163692010-06-13 17:29:36 -0300108 data->bits <<= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300109 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
David Härdemanc2163692010-06-13 17:29:36 -0300110 data->bits |= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300111 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
112 break;
David Härdeman724e2492010-04-08 13:10:00 -0300113 data->count++;
114
David Härdemane40b1122010-04-15 18:46:00 -0300115 if (data->count == NEC_NBITS)
116 data->state = STATE_TRAILER_PULSE;
117 else
David Härdeman724e2492010-04-08 13:10:00 -0300118 data->state = STATE_BIT_PULSE;
David Härdemane40b1122010-04-15 18:46:00 -0300119
120 return 0;
121
122 case STATE_TRAILER_PULSE:
123 if (!ev.pulse)
124 break;
125
126 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
127 break;
128
129 data->state = STATE_TRAILER_SPACE;
130 return 0;
131
132 case STATE_TRAILER_SPACE:
133 if (ev.pulse)
134 break;
135
136 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
137 break;
David Härdeman724e2492010-04-08 13:10:00 -0300138
David Härdemanc2163692010-06-13 17:29:36 -0300139 address = bitrev8((data->bits >> 24) & 0xff);
140 not_address = bitrev8((data->bits >> 16) & 0xff);
141 command = bitrev8((data->bits >> 8) & 0xff);
142 not_command = bitrev8((data->bits >> 0) & 0xff);
David Härdeman724e2492010-04-08 13:10:00 -0300143
144 if ((command ^ not_command) != 0xff) {
145 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
David Härdemanc2163692010-06-13 17:29:36 -0300146 data->bits);
David Härdeman724e2492010-04-08 13:10:00 -0300147 break;
148 }
149
150 if ((address ^ not_address) != 0xff) {
151 /* Extended NEC */
152 scancode = address << 16 |
153 not_address << 8 |
154 command;
155 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
156 } else {
David Härdemane40b1122010-04-15 18:46:00 -0300157 /* Normal NEC */
David Härdeman724e2492010-04-08 13:10:00 -0300158 scancode = address << 8 | command;
159 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
160 }
161
162 ir_keydown(input_dev, scancode, 0);
David Härdemane40b1122010-04-15 18:46:00 -0300163 data->state = STATE_INACTIVE;
David Härdeman724e2492010-04-08 13:10:00 -0300164 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300165 }
166
David Härdemane40b1122010-04-15 18:46:00 -0300167 IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",
168 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300169 data->state = STATE_INACTIVE;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300170 return -EINVAL;
171}
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300172
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300173static struct ir_raw_handler nec_handler = {
David Härdeman667c9eb2010-06-13 17:29:31 -0300174 .protocols = IR_TYPE_NEC,
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300175 .decode = ir_nec_decode,
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300176};
177
178static int __init ir_nec_decode_init(void)
179{
180 ir_raw_handler_register(&nec_handler);
181
182 printk(KERN_INFO "IR NEC protocol handler initialized\n");
183 return 0;
184}
185
186static void __exit ir_nec_decode_exit(void)
187{
188 ir_raw_handler_unregister(&nec_handler);
189}
190
191module_init(ir_nec_decode_init);
192module_exit(ir_nec_decode_exit);
193
194MODULE_LICENSE("GPL");
195MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
196MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
197MODULE_DESCRIPTION("NEC IR protocol decoder");