blob: a0fd4e6b2155b81e0605e58db2225d70cdf54fc1 [file] [log] [blame]
David Härdemane87b5402014-04-03 20:32:31 -03001/* ir-rc5-decoder.c - decoder for RC5(x) and StreamZap protocols
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -03002 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003 * Copyright (C) 2010 by Mauro Carvalho Chehab
David Härdemane87b5402014-04-03 20:32:31 -03004 * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -03005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16/*
David Härdemane87b5402014-04-03 20:32:31 -030017 * This decoder handles the 14 bit RC5 protocol, 15 bit "StreamZap" protocol
18 * and 20 bit RC5x protocol.
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030019 */
20
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030021#include "rc-core-priv.h"
Paul Gortmaker7a707b82011-07-03 14:03:12 -040022#include <linux/module.h>
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030023
24#define RC5_NBITS 14
David Härdemane87b5402014-04-03 20:32:31 -030025#define RC5_SZ_NBITS 15
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)
Jonathan McDowellbbdb34c2016-05-14 14:01:26 -030032#define RC5_TRAILER (6 * RC5_UNIT) /* In reality, approx 100 */
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030033
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030034enum rc5_state {
35 STATE_INACTIVE,
David Härdeman724e2492010-04-08 13:10:00 -030036 STATE_BIT_START,
37 STATE_BIT_END,
David Härdeman733419b2010-04-08 20:04:30 -030038 STATE_CHECK_RC5X,
David Härdeman724e2492010-04-08 13:10:00 -030039 STATE_FINISHED,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030040};
41
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030042/**
David Härdeman724e2492010-04-08 13:10:00 -030043 * ir_rc5_decode() - Decode one RC-5 pulse or space
David Härdemand8b4b582010-10-29 16:08:23 -030044 * @dev: the struct rc_dev descriptor of the device
David Härdemane40b1122010-04-15 18:46:00 -030045 * @ev: the struct ir_raw_event descriptor of the pulse/space
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030046 *
47 * This function returns -EINVAL if the pulse violates the state machine
48 */
David Härdemand8b4b582010-10-29 16:08:23 -030049static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030050{
David Härdemand8b4b582010-10-29 16:08:23 -030051 struct rc5_dec *data = &dev->raw->rc5;
David Härdeman733419b2010-04-08 20:04:30 -030052 u8 toggle;
David Härdeman724e2492010-04-08 13:10:00 -030053 u32 scancode;
David Härdeman120703f2014-04-03 20:31:30 -030054 enum rc_type protocol;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030055
Maxim Levitsky46519182010-10-16 19:56:28 -030056 if (!is_timing_event(ev)) {
57 if (ev.reset)
58 data->state = STATE_INACTIVE;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030059 return 0;
60 }
61
David Härdemane40b1122010-04-15 18:46:00 -030062 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
David Härdeman724e2492010-04-08 13:10:00 -030063 goto out;
64
65again:
David Härdemane87b5402014-04-03 20:32:31 -030066 IR_dprintk(2, "RC5(x/sz) decode started at state %i (%uus %s)\n",
David Härdemane40b1122010-04-15 18:46:00 -030067 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman724e2492010-04-08 13:10:00 -030068
David Härdemane40b1122010-04-15 18:46:00 -030069 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
David Härdeman724e2492010-04-08 13:10:00 -030070 return 0;
71
72 switch (data->state) {
73
74 case STATE_INACTIVE:
David Härdemane40b1122010-04-15 18:46:00 -030075 if (!ev.pulse)
76 break;
77
78 data->state = STATE_BIT_START;
79 data->count = 1;
David Härdemane40b1122010-04-15 18:46:00 -030080 decrease_duration(&ev, RC5_BIT_START);
81 goto again;
David Härdeman724e2492010-04-08 13:10:00 -030082
83 case STATE_BIT_START:
David Härdemane87b5402014-04-03 20:32:31 -030084 if (!ev.pulse && geq_margin(ev.duration, RC5_TRAILER, RC5_UNIT / 2)) {
85 data->state = STATE_FINISHED;
86 goto again;
87 }
88
David Härdemane40b1122010-04-15 18:46:00 -030089 if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
90 break;
David Härdeman724e2492010-04-08 13:10:00 -030091
David Härdemanc2163692010-06-13 17:29:36 -030092 data->bits <<= 1;
David Härdemane40b1122010-04-15 18:46:00 -030093 if (!ev.pulse)
David Härdemanc2163692010-06-13 17:29:36 -030094 data->bits |= 1;
David Härdemane40b1122010-04-15 18:46:00 -030095 data->count++;
David Härdemane40b1122010-04-15 18:46:00 -030096 data->state = STATE_BIT_END;
97 return 0;
David Härdeman724e2492010-04-08 13:10:00 -030098
99 case STATE_BIT_END:
David Härdemand8b4b582010-10-29 16:08:23 -0300100 if (!is_transition(&ev, &dev->raw->prev_ev))
David Härdemane40b1122010-04-15 18:46:00 -0300101 break;
David Härdeman724e2492010-04-08 13:10:00 -0300102
David Härdemane87b5402014-04-03 20:32:31 -0300103 if (data->count == CHECK_RC5X_NBITS)
David Härdemane40b1122010-04-15 18:46:00 -0300104 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ärdemane87b5402014-04-03 20:32:31 -0300113 data->is_rc5x = true;
David Härdemane40b1122010-04-15 18:46:00 -0300114 decrease_duration(&ev, RC5X_SPACE);
David Härdemane87b5402014-04-03 20:32:31 -0300115 } else
116 data->is_rc5x = false;
David Härdeman733419b2010-04-08 20:04:30 -0300117 data->state = STATE_BIT_START;
118 goto again;
David Härdeman724e2492010-04-08 13:10:00 -0300119
David Härdeman733419b2010-04-08 20:04:30 -0300120 case STATE_FINISHED:
David Härdemane40b1122010-04-15 18:46:00 -0300121 if (ev.pulse)
122 break;
123
David Härdemane87b5402014-04-03 20:32:31 -0300124 if (data->is_rc5x && data->count == RC5X_NBITS) {
David Härdeman733419b2010-04-08 20:04:30 -0300125 /* RC5X */
126 u8 xdata, command, system;
David Härdemanc5540fb2014-04-03 20:32:21 -0300127 if (!(dev->enabled_protocols & RC_BIT_RC5X)) {
David Härdemanc003ab12012-10-11 19:11:54 -0300128 data->state = STATE_INACTIVE;
129 return 0;
130 }
David Härdemanc2163692010-06-13 17:29:36 -0300131 xdata = (data->bits & 0x0003F) >> 0;
132 command = (data->bits & 0x00FC0) >> 6;
133 system = (data->bits & 0x1F000) >> 12;
134 toggle = (data->bits & 0x20000) ? 1 : 0;
135 command += (data->bits & 0x01000) ? 0 : 0x40;
David Härdeman733419b2010-04-08 20:04:30 -0300136 scancode = system << 16 | command << 8 | xdata;
David Härdeman120703f2014-04-03 20:31:30 -0300137 protocol = RC_TYPE_RC5X;
David Härdeman733419b2010-04-08 20:04:30 -0300138
David Härdemane87b5402014-04-03 20:32:31 -0300139 } else if (!data->is_rc5x && data->count == RC5_NBITS) {
David Härdeman733419b2010-04-08 20:04:30 -0300140 /* RC5 */
141 u8 command, system;
David Härdemanc5540fb2014-04-03 20:32:21 -0300142 if (!(dev->enabled_protocols & RC_BIT_RC5)) {
David Härdemanc003ab12012-10-11 19:11:54 -0300143 data->state = STATE_INACTIVE;
144 return 0;
145 }
David Härdemanc2163692010-06-13 17:29:36 -0300146 command = (data->bits & 0x0003F) >> 0;
147 system = (data->bits & 0x007C0) >> 6;
148 toggle = (data->bits & 0x00800) ? 1 : 0;
149 command += (data->bits & 0x01000) ? 0 : 0x40;
David Härdeman733419b2010-04-08 20:04:30 -0300150 scancode = system << 8 | command;
David Härdeman120703f2014-04-03 20:31:30 -0300151 protocol = RC_TYPE_RC5;
David Härdeman733419b2010-04-08 20:04:30 -0300152
David Härdemane87b5402014-04-03 20:32:31 -0300153 } else if (!data->is_rc5x && data->count == RC5_SZ_NBITS) {
154 /* RC5 StreamZap */
155 u8 command, system;
156 if (!(dev->enabled_protocols & RC_BIT_RC5_SZ)) {
157 data->state = STATE_INACTIVE;
158 return 0;
159 }
160 command = (data->bits & 0x0003F) >> 0;
161 system = (data->bits & 0x02FC0) >> 6;
162 toggle = (data->bits & 0x01000) ? 1 : 0;
163 scancode = system << 6 | command;
164 protocol = RC_TYPE_RC5_SZ;
165
166 } else
167 break;
168
169 IR_dprintk(1, "RC5(x/sz) scancode 0x%06x (p: %u, t: %u)\n",
170 scancode, protocol, toggle);
David Härdeman733419b2010-04-08 20:04:30 -0300171
David Härdeman120703f2014-04-03 20:31:30 -0300172 rc_keydown(dev, protocol, scancode, toggle);
David Härdeman724e2492010-04-08 13:10:00 -0300173 data->state = STATE_INACTIVE;
174 return 0;
175 }
176
177out:
Mauro Carvalho Chehab32570572014-07-30 15:49:16 -0300178 IR_dprintk(1, "RC5(x/sz) decode failed at state %i count %d (%uus %s)\n",
179 data->state, data->count, TO_US(ev.duration), TO_STR(ev.pulse));
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300180 data->state = STATE_INACTIVE;
181 return -EINVAL;
182}
183
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300184static struct ir_raw_handler rc5_handler = {
David Härdemane87b5402014-04-03 20:32:31 -0300185 .protocols = RC_BIT_RC5 | RC_BIT_RC5X | RC_BIT_RC5_SZ,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300186 .decode = ir_rc5_decode,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300187};
188
189static int __init ir_rc5_decode_init(void)
190{
191 ir_raw_handler_register(&rc5_handler);
192
David Härdemane87b5402014-04-03 20:32:31 -0300193 printk(KERN_INFO "IR RC5(x/sz) protocol handler initialized\n");
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300194 return 0;
195}
196
197static void __exit ir_rc5_decode_exit(void)
198{
199 ir_raw_handler_unregister(&rc5_handler);
200}
201
202module_init(ir_rc5_decode_init);
203module_exit(ir_rc5_decode_exit);
204
205MODULE_LICENSE("GPL");
David Härdemane87b5402014-04-03 20:32:31 -0300206MODULE_AUTHOR("Mauro Carvalho Chehab and Jarod Wilson");
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300207MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
David Härdemane87b5402014-04-03 20:32:31 -0300208MODULE_DESCRIPTION("RC5(x/sz) IR protocol decoder");