[media] rc-core: document the protocol type
[linux-2.6-block.git] / drivers / media / rc / ir-rc5-decoder.c
CommitLineData
733419b5 1/* ir-rc5-decoder.c - handle RC5(x) IR Pulse/Space protocol
db1423a6 2 *
37e59f87 3 * Copyright (C) 2010 by Mauro Carvalho Chehab
db1423a6
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
15/*
733419b5
DH
16 * This code handles 14 bits RC5 protocols and 20 bits RC5x protocols.
17 * There are other variants that use a different number of bits.
18 * This is currently unsupported.
19 * It considers a carrier of 36 kHz, with a total of 14/20 bits, where
9b09df51 20 * the first two bits are start bits, and a third one is a filing bit
db1423a6
MCC
21 */
22
f62de675 23#include "rc-core-priv.h"
7a707b89 24#include <linux/module.h>
db1423a6 25
9b09df51 26#define RC5_NBITS 14
733419b5
DH
27#define RC5X_NBITS 20
28#define CHECK_RC5X_NBITS 8
724e2495 29#define RC5_UNIT 888888 /* ns */
e40b1127
DH
30#define RC5_BIT_START (1 * RC5_UNIT)
31#define RC5_BIT_END (1 * RC5_UNIT)
32#define RC5X_SPACE (4 * RC5_UNIT)
db1423a6 33
db1423a6
MCC
34enum rc5_state {
35 STATE_INACTIVE,
724e2495
DH
36 STATE_BIT_START,
37 STATE_BIT_END,
733419b5 38 STATE_CHECK_RC5X,
724e2495 39 STATE_FINISHED,
db1423a6
MCC
40};
41
db1423a6 42/**
724e2495 43 * ir_rc5_decode() - Decode one RC-5 pulse or space
d8b4b582 44 * @dev: the struct rc_dev descriptor of the device
e40b1127 45 * @ev: the struct ir_raw_event descriptor of the pulse/space
db1423a6
MCC
46 *
47 * This function returns -EINVAL if the pulse violates the state machine
48 */
d8b4b582 49static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
db1423a6 50{
d8b4b582 51 struct rc5_dec *data = &dev->raw->rc5;
733419b5 52 u8 toggle;
724e2495 53 u32 scancode;
120703f9 54 enum rc_type protocol;
db1423a6 55
1a1934fa 56 if (!rc_protocols_enabled(dev, RC_BIT_RC5 | RC_BIT_RC5X))
c003ab1b 57 return 0;
7f20d32d 58
4651918a
ML
59 if (!is_timing_event(ev)) {
60 if (ev.reset)
61 data->state = STATE_INACTIVE;
db1423a6 62 return 0;
724e2495 63 }
db1423a6 64
e40b1127 65 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
724e2495 66 goto out;
db1423a6 67
724e2495 68again:
e40b1127
DH
69 IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n",
70 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
db1423a6 71
e40b1127 72 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
724e2495 73 return 0;
db1423a6 74
724e2495 75 switch (data->state) {
db1423a6 76
724e2495 77 case STATE_INACTIVE:
e40b1127
DH
78 if (!ev.pulse)
79 break;
80
81 data->state = STATE_BIT_START;
82 data->count = 1;
83 /* We just need enough bits to get to STATE_CHECK_RC5X */
84 data->wanted_bits = RC5X_NBITS;
85 decrease_duration(&ev, RC5_BIT_START);
86 goto again;
724e2495
DH
87
88 case STATE_BIT_START:
e40b1127
DH
89 if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
90 break;
91
c216369e 92 data->bits <<= 1;
e40b1127 93 if (!ev.pulse)
c216369e 94 data->bits |= 1;
e40b1127 95 data->count++;
e40b1127
DH
96 data->state = STATE_BIT_END;
97 return 0;
9b09df51 98
724e2495 99 case STATE_BIT_END:
d8b4b582 100 if (!is_transition(&ev, &dev->raw->prev_ev))
e40b1127
DH
101 break;
102
103 if (data->count == data->wanted_bits)
104 data->state = STATE_FINISHED;
105 else if (data->count == CHECK_RC5X_NBITS)
106 data->state = STATE_CHECK_RC5X;
107 else
108 data->state = STATE_BIT_START;
109
110 decrease_duration(&ev, RC5_BIT_END);
111 goto again;
724e2495 112
733419b5 113 case STATE_CHECK_RC5X:
e40b1127 114 if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
733419b5
DH
115 /* RC5X */
116 data->wanted_bits = RC5X_NBITS;
e40b1127 117 decrease_duration(&ev, RC5X_SPACE);
733419b5
DH
118 } else {
119 /* RC5 */
120 data->wanted_bits = RC5_NBITS;
121 }
122 data->state = STATE_BIT_START;
123 goto again;
124
724e2495 125 case STATE_FINISHED:
e40b1127
DH
126 if (ev.pulse)
127 break;
128
733419b5
DH
129 if (data->wanted_bits == RC5X_NBITS) {
130 /* RC5X */
131 u8 xdata, command, system;
1a1934fa 132 if (!rc_protocols_enabled(dev, RC_BIT_RC5X)) {
c003ab1b
DH
133 data->state = STATE_INACTIVE;
134 return 0;
135 }
c216369e
DH
136 xdata = (data->bits & 0x0003F) >> 0;
137 command = (data->bits & 0x00FC0) >> 6;
138 system = (data->bits & 0x1F000) >> 12;
139 toggle = (data->bits & 0x20000) ? 1 : 0;
140 command += (data->bits & 0x01000) ? 0 : 0x40;
733419b5 141 scancode = system << 16 | command << 8 | xdata;
120703f9 142 protocol = RC_TYPE_RC5X;
733419b5
DH
143
144 IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
145 scancode, toggle);
146
147 } else {
148 /* RC5 */
149 u8 command, system;
1a1934fa 150 if (!rc_protocols_enabled(dev, RC_BIT_RC5)) {
c003ab1b
DH
151 data->state = STATE_INACTIVE;
152 return 0;
153 }
c216369e
DH
154 command = (data->bits & 0x0003F) >> 0;
155 system = (data->bits & 0x007C0) >> 6;
156 toggle = (data->bits & 0x00800) ? 1 : 0;
157 command += (data->bits & 0x01000) ? 0 : 0x40;
733419b5 158 scancode = system << 8 | command;
120703f9 159 protocol = RC_TYPE_RC5;
733419b5
DH
160
161 IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
162 scancode, toggle);
163 }
164
120703f9 165 rc_keydown(dev, protocol, scancode, toggle);
db1423a6
MCC
166 data->state = STATE_INACTIVE;
167 return 0;
168 }
169
724e2495 170out:
e40b1127
DH
171 IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n",
172 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
db1423a6
MCC
173 data->state = STATE_INACTIVE;
174 return -EINVAL;
175}
176
db1423a6 177static struct ir_raw_handler rc5_handler = {
c003ab1b 178 .protocols = RC_BIT_RC5 | RC_BIT_RC5X,
db1423a6 179 .decode = ir_rc5_decode,
db1423a6
MCC
180};
181
182static int __init ir_rc5_decode_init(void)
183{
184 ir_raw_handler_register(&rc5_handler);
185
733419b5 186 printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");
db1423a6
MCC
187 return 0;
188}
189
190static void __exit ir_rc5_decode_exit(void)
191{
192 ir_raw_handler_unregister(&rc5_handler);
193}
194
195module_init(ir_rc5_decode_init);
196module_exit(ir_rc5_decode_exit);
197
198MODULE_LICENSE("GPL");
37e59f87 199MODULE_AUTHOR("Mauro Carvalho Chehab");
db1423a6 200MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
733419b5 201MODULE_DESCRIPTION("RC5(x) IR protocol decoder");