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 */
18 #define MIN_START_TIME 3900000
19 #define MAX_START_TIME 5100000
21 /* Pulse time: 560 us */
22 #define MIN_PULSE_TIME 460000
23 #define MAX_PULSE_TIME 660000
25 /* Bit 1 space time: 2.25ms-560 us */
26 #define MIN_BIT1_TIME 1490000
27 #define MAX_BIT1_TIME 1890000
29 /* Bit 0 space time: 1.12ms-560 us */
30 #define MIN_BIT0_TIME 360000
31 #define MAX_BIT0_TIME 760000
34 * __ir_nec_decode() - Decode one NEC pulsecode
35 * @input_dev: the struct input_dev descriptor of the device
36 * @evs: event array with type/duration of pulse/space
37 * @len: length of the array
38 * @pos: position to start seeking for a code
39 * This function returns the decoded ircode or -EINVAL if no pulse got decoded
41 static int __ir_nec_decode(struct input_dev *input_dev,
42 struct ir_raw_event *evs,
46 int ircode = 0, not_code = 0;
48 /* Be sure that the first event is an start one and is a pulse */
49 for (; *pos < len; (*pos)++) {
50 if (evs[*pos].type & (IR_START_EVENT | IR_PULSE))
53 (*pos)++; /* First event doesn't contain data */
58 /* First space should have 4.5 ms otherwise is not NEC protocol */
59 if ((evs[*pos].delta.tv_nsec < MIN_START_TIME) |
60 (evs[*pos].delta.tv_nsec > MAX_START_TIME) |
61 (evs[*pos].type != IR_SPACE))
65 * FIXME: need to implement the repeat sequence
69 for ((*pos)++; *pos < len; (*pos)++) {
72 if ((evs[*pos].delta.tv_nsec < MIN_PULSE_TIME) |
73 (evs[*pos].delta.tv_nsec > MAX_PULSE_TIME) |
74 (evs[*pos].type != IR_PULSE))
79 if (evs[*pos].type != IR_SPACE)
82 if ((evs[*pos].delta.tv_nsec > MIN_BIT1_TIME) &&
83 (evs[*pos].delta.tv_nsec < MAX_BIT1_TIME))
85 else if ((evs[*pos].delta.tv_nsec > MIN_BIT0_TIME) &&
86 (evs[*pos].delta.tv_nsec < MAX_BIT0_TIME))
93 /* Address first, then command */
97 } else if (shift < 16) {
98 not_code |= 1 << shift;
99 } else if (shift < 24) {
101 ircode |= 1 << shift;
104 not_code |= 1 << shift;
112 * Fixme: may need to accept Extended NEC protocol?
114 if ((ircode & ~not_code) != ircode) {
115 IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n",
120 IR_dprintk(1, "NEC scancode 0x%04x\n", ircode);
121 ir_keydown(input_dev, ircode);
126 IR_dprintk(1, "NEC decoded failed at bit %d while decoding %luus time\n",
127 count, (evs[*pos].delta.tv_nsec + 500) / 1000);
133 * __ir_nec_decode() - Decodes all NEC pulsecodes on a given array
134 * @input_dev: the struct input_dev descriptor of the device
135 * @evs: event array with type/duration of pulse/space
136 * @len: length of the array
137 * This function returns the number of decoded pulses or -EINVAL if no
140 int ir_nec_decode(struct input_dev *input_dev,
141 struct ir_raw_event *evs,
148 if (__ir_nec_decode(input_dev, evs, len, &pos) >= 0)
157 EXPORT_SYMBOL_GPL(ir_nec_decode);