1 /* ir-raw-event.c - handle IR Pulse/Space event
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
15 #include <media/ir-core.h>
17 /* Start time: 4.5 ms + 560 us of the next pulse */
18 #define MIN_START_TIME (3900000 + 560000)
19 #define MAX_START_TIME (5100000 + 560000)
21 /* Bit 1 time: 2.25ms us */
22 #define MIN_BIT1_TIME 2050000
23 #define MAX_BIT1_TIME 2450000
25 /* Bit 0 time: 1.12ms us */
26 #define MIN_BIT0_TIME 920000
27 #define MAX_BIT0_TIME 1320000
29 /* Total IR code is 110 ms, including the 9 ms for the start pulse */
30 #define MAX_NEC_TIME 4000000
32 /* Total IR code is 110 ms, including the 9 ms for the start pulse */
33 #define MIN_REPEAT_TIME 99000000
34 #define MAX_REPEAT_TIME 112000000
36 /* Repeat time: 2.25ms us */
37 #define MIN_REPEAT_START_TIME 2050000
38 #define MAX_REPEAT_START_TIME 3000000
40 #define REPEAT_TIME 240 /* ms */
42 /** is_repeat - Check if it is a NEC repeat event
43 * @input_dev: the struct input_dev descriptor of the device
44 * @pos: the position of the first event
45 * @len: the length of the buffer
47 static int is_repeat(struct ir_raw_event *evs, int len, int pos)
49 if ((evs[pos].delta.tv_nsec < MIN_REPEAT_START_TIME) ||
50 (evs[pos].delta.tv_nsec > MAX_REPEAT_START_TIME))
56 if ((evs[pos].delta.tv_nsec < MIN_REPEAT_TIME) ||
57 (evs[pos].delta.tv_nsec > MAX_REPEAT_TIME))
64 * __ir_nec_decode() - Decode one NEC pulsecode
65 * @input_dev: the struct input_dev descriptor of the device
66 * @evs: event array with type/duration of pulse/space
67 * @len: length of the array
68 * @pos: position to start seeking for a code
69 * This function returns -EINVAL if no pulse got decoded,
70 * 0 if buffer is empty and 1 if one keycode were handled.
72 static int __ir_nec_decode(struct input_dev *input_dev,
73 struct ir_raw_event *evs,
76 struct ir_input_dev *ir = input_get_drvdata(input_dev);
78 int ircode = 0, not_code = 0;
80 /* Be sure that the first event is an start one and is a pulse */
81 for (; *pos < len; (*pos)++) {
82 /* Very long delays are considered as start events */
83 if (evs[*pos].delta.tv_nsec > MAX_NEC_TIME)
85 if (evs[*pos].type & IR_START_EVENT)
87 IR_dprintk(1, "%luus: Spurious NEC %s\n",
88 (evs[*pos].delta.tv_nsec + 500) / 1000,
89 (evs[*pos].type & IR_SPACE) ? "space" : "pulse");
95 (*pos)++; /* First event doesn't contain data */
97 if (evs[*pos].type != IR_PULSE)
100 /* Check if it is a NEC repeat event */
101 if (is_repeat(evs, len, *pos)) {
103 if (ir->keypressed) {
104 mod_timer(&ir->raw->timer_keyup,
105 jiffies + msecs_to_jiffies(REPEAT_TIME));
106 IR_dprintk(1, "NEC repeat event\n");
109 IR_dprintk(1, "missing NEC repeat event\n");
114 /* First space should have 4.5 ms otherwise is not NEC protocol */
115 if ((evs[*pos].delta.tv_nsec < MIN_START_TIME) ||
116 (evs[*pos].delta.tv_nsec > MAX_START_TIME))
120 for ((*pos)++; *pos < len; (*pos)++) {
122 if ((evs[*pos].delta.tv_nsec > MIN_BIT1_TIME) &&
123 (evs[*pos].delta.tv_nsec < MAX_BIT1_TIME))
125 else if ((evs[*pos].delta.tv_nsec > MIN_BIT0_TIME) &&
126 (evs[*pos].delta.tv_nsec < MAX_BIT0_TIME))
133 /* Address first, then command */
136 ircode |= 1 << shift;
137 } else if (shift < 16) {
138 not_code |= 1 << shift;
139 } else if (shift < 24) {
141 ircode |= 1 << shift;
144 not_code |= 1 << shift;
153 * Fixme: may need to accept Extended NEC protocol?
155 if ((ircode & ~not_code) != ircode) {
156 IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n",
161 IR_dprintk(1, "NEC scancode 0x%04x\n", ircode);
162 ir_keydown(input_dev, ircode);
163 mod_timer(&ir->raw->timer_keyup,
164 jiffies + msecs_to_jiffies(REPEAT_TIME));
168 IR_dprintk(1, "NEC decoded failed at bit %d (%s) while decoding %luus time\n",
170 (evs[*pos].type & IR_SPACE) ? "space" : "pulse",
171 (evs[*pos].delta.tv_nsec + 500) / 1000);
177 * __ir_nec_decode() - Decodes all NEC pulsecodes on a given array
178 * @input_dev: the struct input_dev descriptor of the device
179 * @evs: event array with type/duration of pulse/space
180 * @len: length of the array
181 * This function returns the number of decoded pulses or -EINVAL if no
184 int ir_nec_decode(struct input_dev *input_dev,
185 struct ir_raw_event *evs,
192 if (__ir_nec_decode(input_dev, evs, len, &pos) > 0)
201 EXPORT_SYMBOL_GPL(ir_nec_decode);