Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/egtvedt...
[deliverable/linux.git] / drivers / media / rc / ir-sanyo-decoder.c
CommitLineData
b32e7243
MCC
1/* ir-sanyo-decoder.c - handle SANYO IR Pulse/Space protocol
2 *
37e59f87 3 * Copyright (C) 2011 by Mauro Carvalho Chehab
b32e7243
MCC
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 * This protocol uses the NEC protocol timings. However, data is formatted as:
15 * 13 bits Custom Code
16 * 13 bits NOT(Custom Code)
17 * 8 bits Key data
18 * 8 bits NOT(Key data)
19 *
20 * According with LIRC, this protocol is used on Sanyo, Aiwa and Chinon
21 * Information for this protocol is available at the Sanyo LC7461 datasheet.
22 */
23
2e962f4e 24#include <linux/module.h>
b32e7243
MCC
25#include <linux/bitrev.h>
26#include "rc-core-priv.h"
27
28#define SANYO_NBITS (13+13+8+8)
29#define SANYO_UNIT 562500 /* ns */
30#define SANYO_HEADER_PULSE (16 * SANYO_UNIT)
31#define SANYO_HEADER_SPACE (8 * SANYO_UNIT)
32#define SANYO_BIT_PULSE (1 * SANYO_UNIT)
33#define SANYO_BIT_0_SPACE (1 * SANYO_UNIT)
34#define SANYO_BIT_1_SPACE (3 * SANYO_UNIT)
35#define SANYO_REPEAT_SPACE (150 * SANYO_UNIT)
36#define SANYO_TRAILER_PULSE (1 * SANYO_UNIT)
37#define SANYO_TRAILER_SPACE (10 * SANYO_UNIT) /* in fact, 42 */
38
39enum sanyo_state {
40 STATE_INACTIVE,
41 STATE_HEADER_SPACE,
42 STATE_BIT_PULSE,
43 STATE_BIT_SPACE,
44 STATE_TRAILER_PULSE,
45 STATE_TRAILER_SPACE,
46};
47
48/**
49 * ir_sanyo_decode() - Decode one SANYO pulse or space
50 * @dev: the struct rc_dev descriptor of the device
51 * @duration: the struct ir_raw_event descriptor of the pulse/space
52 *
53 * This function returns -EINVAL if the pulse violates the state machine
54 */
55static int ir_sanyo_decode(struct rc_dev *dev, struct ir_raw_event ev)
56{
57 struct sanyo_dec *data = &dev->raw->sanyo;
58 u32 scancode;
5becbc58 59 u8 address, command, not_command;
b32e7243 60
b32e7243
MCC
61 if (!is_timing_event(ev)) {
62 if (ev.reset) {
63 IR_dprintk(1, "SANYO event reset received. reset to state 0\n");
64 data->state = STATE_INACTIVE;
65 }
66 return 0;
67 }
68
69 IR_dprintk(2, "SANYO decode started at state %d (%uus %s)\n",
70 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
71
72 switch (data->state) {
73
74 case STATE_INACTIVE:
75 if (!ev.pulse)
76 break;
77
78 if (eq_margin(ev.duration, SANYO_HEADER_PULSE, SANYO_UNIT / 2)) {
79 data->count = 0;
80 data->state = STATE_HEADER_SPACE;
81 return 0;
82 }
83 break;
84
85
86 case STATE_HEADER_SPACE:
87 if (ev.pulse)
88 break;
89
90 if (eq_margin(ev.duration, SANYO_HEADER_SPACE, SANYO_UNIT / 2)) {
91 data->state = STATE_BIT_PULSE;
92 return 0;
93 }
94
95 break;
96
97 case STATE_BIT_PULSE:
98 if (!ev.pulse)
99 break;
100
101 if (!eq_margin(ev.duration, SANYO_BIT_PULSE, SANYO_UNIT / 2))
102 break;
103
104 data->state = STATE_BIT_SPACE;
105 return 0;
106
107 case STATE_BIT_SPACE:
108 if (ev.pulse)
109 break;
110
111 if (!data->count && geq_margin(ev.duration, SANYO_REPEAT_SPACE, SANYO_UNIT / 2)) {
112 if (!dev->keypressed) {
113 IR_dprintk(1, "SANYO discarding last key repeat: event after key up\n");
114 } else {
115 rc_repeat(dev);
116 IR_dprintk(1, "SANYO repeat last key\n");
117 data->state = STATE_INACTIVE;
118 }
119 return 0;
120 }
121
122 data->bits <<= 1;
123 if (eq_margin(ev.duration, SANYO_BIT_1_SPACE, SANYO_UNIT / 2))
124 data->bits |= 1;
125 else if (!eq_margin(ev.duration, SANYO_BIT_0_SPACE, SANYO_UNIT / 2))
126 break;
127 data->count++;
128
129 if (data->count == SANYO_NBITS)
130 data->state = STATE_TRAILER_PULSE;
131 else
132 data->state = STATE_BIT_PULSE;
133
134 return 0;
135
136 case STATE_TRAILER_PULSE:
137 if (!ev.pulse)
138 break;
139
140 if (!eq_margin(ev.duration, SANYO_TRAILER_PULSE, SANYO_UNIT / 2))
141 break;
142
143 data->state = STATE_TRAILER_SPACE;
144 return 0;
145
146 case STATE_TRAILER_SPACE:
147 if (ev.pulse)
148 break;
149
150 if (!geq_margin(ev.duration, SANYO_TRAILER_SPACE, SANYO_UNIT / 2))
151 break;
152
153 address = bitrev16((data->bits >> 29) & 0x1fff) >> 3;
5becbc58 154 /* not_address = bitrev16((data->bits >> 16) & 0x1fff) >> 3; */
b32e7243
MCC
155 command = bitrev8((data->bits >> 8) & 0xff);
156 not_command = bitrev8((data->bits >> 0) & 0xff);
157
158 if ((command ^ not_command) != 0xff) {
159 IR_dprintk(1, "SANYO checksum error: received 0x%08Lx\n",
160 data->bits);
161 data->state = STATE_INACTIVE;
162 return 0;
163 }
164
165 scancode = address << 8 | command;
166 IR_dprintk(1, "SANYO scancode: 0x%06x\n", scancode);
120703f9 167 rc_keydown(dev, RC_TYPE_SANYO, scancode, 0);
b32e7243
MCC
168 data->state = STATE_INACTIVE;
169 return 0;
170 }
171
172 IR_dprintk(1, "SANYO decode failed at count %d state %d (%uus %s)\n",
173 data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
174 data->state = STATE_INACTIVE;
175 return -EINVAL;
176}
177
178static struct ir_raw_handler sanyo_handler = {
c003ab1b 179 .protocols = RC_BIT_SANYO,
b32e7243
MCC
180 .decode = ir_sanyo_decode,
181};
182
183static int __init ir_sanyo_decode_init(void)
184{
185 ir_raw_handler_register(&sanyo_handler);
186
187 printk(KERN_INFO "IR SANYO protocol handler initialized\n");
188 return 0;
189}
190
191static void __exit ir_sanyo_decode_exit(void)
192{
193 ir_raw_handler_unregister(&sanyo_handler);
194}
195
196module_init(ir_sanyo_decode_init);
197module_exit(ir_sanyo_decode_exit);
198
199MODULE_LICENSE("GPL");
37e59f87 200MODULE_AUTHOR("Mauro Carvalho Chehab");
b32e7243
MCC
201MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
202MODULE_DESCRIPTION("SANYO IR protocol decoder");
This page took 0.258062 seconds and 5 git commands to generate.