1 /* ir-sharp-decoder.c - handle Sharp IR Pulse/Space protocol
3 * Copyright (C) 2013-2014 Imagination Technologies Ltd.
5 * Based on NEC decoder:
6 * Copyright (C) 2010 by Mauro Carvalho Chehab
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/bitrev.h>
19 #include <linux/module.h>
20 #include "rc-core-priv.h"
22 #define SHARP_NBITS 15
23 #define SHARP_UNIT 40000 /* ns */
24 #define SHARP_BIT_PULSE (8 * SHARP_UNIT) /* 320us */
25 #define SHARP_BIT_0_PERIOD (25 * SHARP_UNIT) /* 1ms (680us space) */
26 #define SHARP_BIT_1_PERIOD (50 * SHARP_UNIT) /* 2ms (1680ms space) */
27 #define SHARP_ECHO_SPACE (1000 * SHARP_UNIT) /* 40 ms */
28 #define SHARP_TRAILER_SPACE (125 * SHARP_UNIT) /* 5 ms (even longer) */
40 * ir_sharp_decode() - Decode one Sharp pulse or space
41 * @dev: the struct rc_dev descriptor of the device
42 * @duration: the struct ir_raw_event descriptor of the pulse/space
44 * This function returns -EINVAL if the pulse violates the state machine
46 static int ir_sharp_decode(struct rc_dev *dev, struct ir_raw_event ev)
48 struct sharp_dec *data = &dev->raw->sharp;
49 u32 msg, echo, address, command, scancode;
51 if (!rc_protocols_enabled(dev, RC_BIT_SHARP))
54 if (!is_timing_event(ev)) {
56 data->state = STATE_INACTIVE;
60 IR_dprintk(2, "Sharp decode started at state %d (%uus %s)\n",
61 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
63 switch (data->state) {
69 if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
74 data->pulse_len = ev.duration;
75 data->state = STATE_BIT_SPACE;
82 if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
86 data->pulse_len = ev.duration;
87 data->state = STATE_BIT_SPACE;
95 if (eq_margin(data->pulse_len + ev.duration, SHARP_BIT_1_PERIOD,
98 else if (!eq_margin(data->pulse_len + ev.duration,
99 SHARP_BIT_0_PERIOD, SHARP_BIT_PULSE * 2))
103 if (data->count == SHARP_NBITS ||
104 data->count == SHARP_NBITS * 2)
105 data->state = STATE_TRAILER_PULSE;
107 data->state = STATE_BIT_PULSE;
111 case STATE_TRAILER_PULSE:
115 if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
116 SHARP_BIT_PULSE / 2))
119 if (data->count == SHARP_NBITS) {
120 /* exp,chk bits should be 1,0 */
121 if ((data->bits & 0x3) != 0x2)
123 data->state = STATE_ECHO_SPACE;
125 data->state = STATE_TRAILER_SPACE;
129 case STATE_ECHO_SPACE:
133 if (!eq_margin(ev.duration, SHARP_ECHO_SPACE,
134 SHARP_ECHO_SPACE / 4))
137 data->state = STATE_BIT_PULSE;
141 case STATE_TRAILER_SPACE:
145 if (!geq_margin(ev.duration, SHARP_TRAILER_SPACE,
146 SHARP_BIT_PULSE / 2))
149 /* Validate - command, ext, chk should be inverted in 2nd */
150 msg = (data->bits >> 15) & 0x7fff;
151 echo = data->bits & 0x7fff;
152 if ((msg ^ echo) != 0x3ff) {
154 "Sharp checksum error: received 0x%04x, 0x%04x\n",
159 address = bitrev8((msg >> 7) & 0xf8);
160 command = bitrev8((msg >> 2) & 0xff);
162 scancode = address << 8 | command;
163 IR_dprintk(1, "Sharp scancode 0x%04x\n", scancode);
165 rc_keydown(dev, scancode, 0);
166 data->state = STATE_INACTIVE;
170 IR_dprintk(1, "Sharp decode failed at count %d state %d (%uus %s)\n",
171 data->count, data->state, TO_US(ev.duration),
173 data->state = STATE_INACTIVE;
177 static struct ir_raw_handler sharp_handler = {
178 .protocols = RC_BIT_SHARP,
179 .decode = ir_sharp_decode,
182 static int __init ir_sharp_decode_init(void)
184 ir_raw_handler_register(&sharp_handler);
186 pr_info("IR Sharp protocol handler initialized\n");
190 static void __exit ir_sharp_decode_exit(void)
192 ir_raw_handler_unregister(&sharp_handler);
195 module_init(ir_sharp_decode_init);
196 module_exit(ir_sharp_decode_exit);
198 MODULE_LICENSE("GPL");
200 MODULE_DESCRIPTION("Sharp IR protocol decoder");