[media] rc-core: add separate defines for protocol bitmaps and numbers
[deliverable/linux.git] / drivers / media / rc / ir-rc5-decoder.c
CommitLineData
733419b5 1/* ir-rc5-decoder.c - handle RC5(x) IR Pulse/Space protocol
db1423a6
MCC
2 *
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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;
db1423a6 54
c003ab1b
DH
55 if (!(dev->raw->enabled_protocols & (RC_BIT_RC5 | RC_BIT_RC5X)))
56 return 0;
7f20d32d 57
4651918a
ML
58 if (!is_timing_event(ev)) {
59 if (ev.reset)
60 data->state = STATE_INACTIVE;
db1423a6 61 return 0;
724e2495 62 }
db1423a6 63
e40b1127 64 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
724e2495 65 goto out;
db1423a6 66
724e2495 67again:
e40b1127
DH
68 IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n",
69 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
db1423a6 70
e40b1127 71 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
724e2495 72 return 0;
db1423a6 73
724e2495 74 switch (data->state) {
db1423a6 75
724e2495 76 case STATE_INACTIVE:
e40b1127
DH
77 if (!ev.pulse)
78 break;
79
80 data->state = STATE_BIT_START;
81 data->count = 1;
82 /* We just need enough bits to get to STATE_CHECK_RC5X */
83 data->wanted_bits = RC5X_NBITS;
84 decrease_duration(&ev, RC5_BIT_START);
85 goto again;
724e2495
DH
86
87 case STATE_BIT_START:
e40b1127
DH
88 if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
89 break;
90
c216369e 91 data->bits <<= 1;
e40b1127 92 if (!ev.pulse)
c216369e 93 data->bits |= 1;
e40b1127 94 data->count++;
e40b1127
DH
95 data->state = STATE_BIT_END;
96 return 0;
9b09df51 97
724e2495 98 case STATE_BIT_END:
d8b4b582 99 if (!is_transition(&ev, &dev->raw->prev_ev))
e40b1127
DH
100 break;
101
102 if (data->count == data->wanted_bits)
103 data->state = STATE_FINISHED;
104 else if (data->count == CHECK_RC5X_NBITS)
105 data->state = STATE_CHECK_RC5X;
106 else
107 data->state = STATE_BIT_START;
108
109 decrease_duration(&ev, RC5_BIT_END);
110 goto again;
724e2495 111
733419b5 112 case STATE_CHECK_RC5X:
e40b1127 113 if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
733419b5
DH
114 /* RC5X */
115 data->wanted_bits = RC5X_NBITS;
e40b1127 116 decrease_duration(&ev, RC5X_SPACE);
733419b5
DH
117 } else {
118 /* RC5 */
119 data->wanted_bits = RC5_NBITS;
120 }
121 data->state = STATE_BIT_START;
122 goto again;
123
724e2495 124 case STATE_FINISHED:
e40b1127
DH
125 if (ev.pulse)
126 break;
127
733419b5
DH
128 if (data->wanted_bits == RC5X_NBITS) {
129 /* RC5X */
130 u8 xdata, command, system;
c003ab1b
DH
131 if (!(dev->raw->enabled_protocols & RC_BIT_RC5X)) {
132 data->state = STATE_INACTIVE;
133 return 0;
134 }
c216369e
DH
135 xdata = (data->bits & 0x0003F) >> 0;
136 command = (data->bits & 0x00FC0) >> 6;
137 system = (data->bits & 0x1F000) >> 12;
138 toggle = (data->bits & 0x20000) ? 1 : 0;
139 command += (data->bits & 0x01000) ? 0 : 0x40;
733419b5
DH
140 scancode = system << 16 | command << 8 | xdata;
141
142 IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
143 scancode, toggle);
144
145 } else {
146 /* RC5 */
147 u8 command, system;
c003ab1b
DH
148 if (!(dev->raw->enabled_protocols & RC_BIT_RC5)) {
149 data->state = STATE_INACTIVE;
150 return 0;
151 }
c216369e
DH
152 command = (data->bits & 0x0003F) >> 0;
153 system = (data->bits & 0x007C0) >> 6;
154 toggle = (data->bits & 0x00800) ? 1 : 0;
155 command += (data->bits & 0x01000) ? 0 : 0x40;
733419b5
DH
156 scancode = system << 8 | command;
157
158 IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
159 scancode, toggle);
160 }
161
ca86674b 162 rc_keydown(dev, scancode, toggle);
db1423a6
MCC
163 data->state = STATE_INACTIVE;
164 return 0;
165 }
166
724e2495 167out:
e40b1127
DH
168 IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n",
169 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
db1423a6
MCC
170 data->state = STATE_INACTIVE;
171 return -EINVAL;
172}
173
db1423a6 174static struct ir_raw_handler rc5_handler = {
c003ab1b 175 .protocols = RC_BIT_RC5 | RC_BIT_RC5X,
db1423a6 176 .decode = ir_rc5_decode,
db1423a6
MCC
177};
178
179static int __init ir_rc5_decode_init(void)
180{
181 ir_raw_handler_register(&rc5_handler);
182
733419b5 183 printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");
db1423a6
MCC
184 return 0;
185}
186
187static void __exit ir_rc5_decode_exit(void)
188{
189 ir_raw_handler_unregister(&rc5_handler);
190}
191
192module_init(ir_rc5_decode_init);
193module_exit(ir_rc5_decode_exit);
194
195MODULE_LICENSE("GPL");
196MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
197MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
733419b5 198MODULE_DESCRIPTION("RC5(x) IR protocol decoder");
This page took 0.268613 seconds and 5 git commands to generate.