blob: ebdba55399167a78261e4a45d2d4cb64237d4d7c [file] [log] [blame]
David Härdeman733419b2010-04-08 20:04:30 -03001/* ir-rc5-decoder.c - handle RC5(x) IR Pulse/Space protocol
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -03002 *
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/*
David Härdeman733419b2010-04-08 20:04:30 -030016 * 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
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -030020 * the first two bits are start bits, and a third one is a filing bit
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030021 */
22
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030023#include "rc-core-priv.h"
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030024
25#define RC5_NBITS 14
David Härdeman733419b2010-04-08 20:04:30 -030026#define RC5X_NBITS 20
27#define CHECK_RC5X_NBITS 8
David Härdeman724e2492010-04-08 13:10:00 -030028#define RC5_UNIT 888888 /* ns */
David Härdemane40b1122010-04-15 18:46:00 -030029#define RC5_BIT_START (1 * RC5_UNIT)
30#define RC5_BIT_END (1 * RC5_UNIT)
31#define RC5X_SPACE (4 * RC5_UNIT)
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030032
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030033enum rc5_state {
34 STATE_INACTIVE,
David Härdeman724e2492010-04-08 13:10:00 -030035 STATE_BIT_START,
36 STATE_BIT_END,
David Härdeman733419b2010-04-08 20:04:30 -030037 STATE_CHECK_RC5X,
David Härdeman724e2492010-04-08 13:10:00 -030038 STATE_FINISHED,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030039};
40
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030041/**
David Härdeman724e2492010-04-08 13:10:00 -030042 * ir_rc5_decode() - Decode one RC-5 pulse or space
David Härdemand8b4b582010-10-29 16:08:23 -030043 * @dev: the struct rc_dev descriptor of the device
David Härdemane40b1122010-04-15 18:46:00 -030044 * @ev: the struct ir_raw_event descriptor of the pulse/space
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030045 *
46 * This function returns -EINVAL if the pulse violates the state machine
47 */
David Härdemand8b4b582010-10-29 16:08:23 -030048static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030049{
David Härdemand8b4b582010-10-29 16:08:23 -030050 struct rc5_dec *data = &dev->raw->rc5;
David Härdeman733419b2010-04-08 20:04:30 -030051 u8 toggle;
David Härdeman724e2492010-04-08 13:10:00 -030052 u32 scancode;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030053
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -030054 if (!(dev->raw->enabled_protocols & RC_TYPE_RC5))
David Härdeman667c9eb2010-06-13 17:29:31 -030055 return 0;
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -030056
Maxim Levitsky46519182010-10-16 19:56:28 -030057 if (!is_timing_event(ev)) {
58 if (ev.reset)
59 data->state = STATE_INACTIVE;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030060 return 0;
61 }
62
David Härdemane40b1122010-04-15 18:46:00 -030063 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
David Härdeman724e2492010-04-08 13:10:00 -030064 goto out;
65
66again:
David Härdemane40b1122010-04-15 18:46:00 -030067 IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n",
68 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman724e2492010-04-08 13:10:00 -030069
David Härdemane40b1122010-04-15 18:46:00 -030070 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
David Härdeman724e2492010-04-08 13:10:00 -030071 return 0;
72
73 switch (data->state) {
74
75 case STATE_INACTIVE:
David Härdemane40b1122010-04-15 18:46:00 -030076 if (!ev.pulse)
77 break;
78
79 data->state = STATE_BIT_START;
80 data->count = 1;
81 /* We just need enough bits to get to STATE_CHECK_RC5X */
82 data->wanted_bits = RC5X_NBITS;
83 decrease_duration(&ev, RC5_BIT_START);
84 goto again;
David Härdeman724e2492010-04-08 13:10:00 -030085
86 case STATE_BIT_START:
David Härdemane40b1122010-04-15 18:46:00 -030087 if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
88 break;
David Härdeman724e2492010-04-08 13:10:00 -030089
David Härdemanc2163692010-06-13 17:29:36 -030090 data->bits <<= 1;
David Härdemane40b1122010-04-15 18:46:00 -030091 if (!ev.pulse)
David Härdemanc2163692010-06-13 17:29:36 -030092 data->bits |= 1;
David Härdemane40b1122010-04-15 18:46:00 -030093 data->count++;
David Härdemane40b1122010-04-15 18:46:00 -030094 data->state = STATE_BIT_END;
95 return 0;
David Härdeman724e2492010-04-08 13:10:00 -030096
97 case STATE_BIT_END:
David Härdemand8b4b582010-10-29 16:08:23 -030098 if (!is_transition(&ev, &dev->raw->prev_ev))
David Härdemane40b1122010-04-15 18:46:00 -030099 break;
David Härdeman724e2492010-04-08 13:10:00 -0300100
David Härdemane40b1122010-04-15 18:46:00 -0300101 if (data->count == data->wanted_bits)
102 data->state = STATE_FINISHED;
103 else if (data->count == CHECK_RC5X_NBITS)
104 data->state = STATE_CHECK_RC5X;
105 else
106 data->state = STATE_BIT_START;
107
108 decrease_duration(&ev, RC5_BIT_END);
109 goto again;
David Härdeman724e2492010-04-08 13:10:00 -0300110
David Härdeman733419b2010-04-08 20:04:30 -0300111 case STATE_CHECK_RC5X:
David Härdemane40b1122010-04-15 18:46:00 -0300112 if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
David Härdeman733419b2010-04-08 20:04:30 -0300113 /* RC5X */
114 data->wanted_bits = RC5X_NBITS;
David Härdemane40b1122010-04-15 18:46:00 -0300115 decrease_duration(&ev, RC5X_SPACE);
David Härdeman733419b2010-04-08 20:04:30 -0300116 } else {
117 /* RC5 */
118 data->wanted_bits = RC5_NBITS;
119 }
120 data->state = STATE_BIT_START;
121 goto again;
David Härdeman724e2492010-04-08 13:10:00 -0300122
David Härdeman733419b2010-04-08 20:04:30 -0300123 case STATE_FINISHED:
David Härdemane40b1122010-04-15 18:46:00 -0300124 if (ev.pulse)
125 break;
126
David Härdeman733419b2010-04-08 20:04:30 -0300127 if (data->wanted_bits == RC5X_NBITS) {
128 /* RC5X */
129 u8 xdata, command, system;
David Härdemanc2163692010-06-13 17:29:36 -0300130 xdata = (data->bits & 0x0003F) >> 0;
131 command = (data->bits & 0x00FC0) >> 6;
132 system = (data->bits & 0x1F000) >> 12;
133 toggle = (data->bits & 0x20000) ? 1 : 0;
134 command += (data->bits & 0x01000) ? 0 : 0x40;
David Härdeman733419b2010-04-08 20:04:30 -0300135 scancode = system << 16 | command << 8 | xdata;
136
137 IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
138 scancode, toggle);
139
140 } else {
141 /* RC5 */
142 u8 command, system;
David Härdemanc2163692010-06-13 17:29:36 -0300143 command = (data->bits & 0x0003F) >> 0;
144 system = (data->bits & 0x007C0) >> 6;
145 toggle = (data->bits & 0x00800) ? 1 : 0;
146 command += (data->bits & 0x01000) ? 0 : 0x40;
David Härdeman733419b2010-04-08 20:04:30 -0300147 scancode = system << 8 | command;
148
149 IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
150 scancode, toggle);
151 }
152
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300153 rc_keydown(dev, scancode, toggle);
David Härdeman724e2492010-04-08 13:10:00 -0300154 data->state = STATE_INACTIVE;
155 return 0;
156 }
157
158out:
David Härdemane40b1122010-04-15 18:46:00 -0300159 IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n",
160 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300161 data->state = STATE_INACTIVE;
162 return -EINVAL;
163}
164
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300165static struct ir_raw_handler rc5_handler = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300166 .protocols = RC_TYPE_RC5,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300167 .decode = ir_rc5_decode,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300168};
169
170static int __init ir_rc5_decode_init(void)
171{
172 ir_raw_handler_register(&rc5_handler);
173
David Härdeman733419b2010-04-08 20:04:30 -0300174 printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300175 return 0;
176}
177
178static void __exit ir_rc5_decode_exit(void)
179{
180 ir_raw_handler_unregister(&rc5_handler);
181}
182
183module_init(ir_rc5_decode_init);
184module_exit(ir_rc5_decode_exit);
185
186MODULE_LICENSE("GPL");
187MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
188MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
David Härdeman733419b2010-04-08 20:04:30 -0300189MODULE_DESCRIPTION("RC5(x) IR protocol decoder");