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 /** Decode NEC pulsecode. This code can take up to 76.5 ms to run.
35 Unfortunately, using IRQ to decode pulse didn't work, since it uses
36 a pulse train of 38KHz. This means one pulse on each 52 us
39 int ir_nec_decode(struct input_dev *input_dev,
40 struct ir_raw_event *evs,
44 int ircode = 0, not_code = 0;
46 /* Needed only after porting the event code to the decoder */
47 struct ir_input_dev *ir = input_get_drvdata(input_dev);
50 /* Be sure that the first event is an start one and is a pulse */
51 for (i = 0; i < len; i++) {
52 if (evs[i].type & (IR_START_EVENT | IR_PULSE))
55 i++; /* First event doesn't contain data */
60 /* First space should have 4.5 ms otherwise is not NEC protocol */
61 if ((evs[i].delta.tv_nsec < MIN_START_TIME) |
62 (evs[i].delta.tv_nsec > MAX_START_TIME) |
63 (evs[i].type != IR_SPACE))
67 * FIXME: need to implement the repeat sequence
71 for (i++; i < len; i++) {
74 if ((evs[i].delta.tv_nsec < MIN_PULSE_TIME) |
75 (evs[i].delta.tv_nsec > MAX_PULSE_TIME) |
76 (evs[i].type != IR_PULSE))
81 if (evs[i].type != IR_SPACE)
84 if ((evs[i].delta.tv_nsec > MIN_BIT1_TIME) &&
85 (evs[i].delta.tv_nsec < MAX_BIT1_TIME))
87 else if ((evs[i].delta.tv_nsec > MIN_BIT0_TIME) &&
88 (evs[i].delta.tv_nsec < MAX_BIT0_TIME))
95 /* Address first, then command */
99 } else if (shift < 16) {
100 not_code |= 1 << shift;
101 } else if (shift < 24) {
103 ircode |= 1 << shift;
106 not_code |= 1 << shift;
114 * Fixme: may need to accept Extended NEC protocol?
116 if ((ircode & ~not_code) != ircode) {
117 IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n",
122 IR_dprintk(1, "NEC scancode 0x%04x\n", ircode);
126 IR_dprintk(1, "NEC decoded failed at bit %d while decoding %luus time\n",
127 count, (evs[i].delta.tv_nsec + 500) / 1000);
131 EXPORT_SYMBOL_GPL(ir_nec_decode);