1 // SPDX-License-Identifier: GPL-2.0+
2 // ir-rcmm-decoder.c - A decoder for the RCMM IR protocol
4 // Copyright (C) 2018 by Patrick Lerda <patrick9876@free.fr>
6 #include "rc-core-priv.h"
7 #include <linux/module.h>
9 #define RCMM_UNIT 166667 /* nanosecs */
10 #define RCMM_PREFIX_PULSE 416666 /* 166666.666666666*2.5 */
11 #define RCMM_PULSE_0 277777 /* 166666.666666666*(1+2/3) */
12 #define RCMM_PULSE_1 444444 /* 166666.666666666*(2+2/3) */
13 #define RCMM_PULSE_2 611111 /* 166666.666666666*(3+2/3) */
14 #define RCMM_PULSE_3 777778 /* 166666.666666666*(4+2/3) */
24 static bool rcmm_mode(const struct rcmm_dec
*data
)
26 return !((0x000c0000 & data
->bits
) == 0x000c0000);
29 static int rcmm_miscmode(struct rc_dev
*dev
, struct rcmm_dec
*data
)
31 switch (data
->count
) {
33 if (dev
->enabled_protocols
& RC_PROTO_BIT_RCMM24
) {
34 rc_keydown(dev
, RC_PROTO_RCMM24
, data
->bits
, 0);
35 data
->state
= STATE_INACTIVE
;
41 if (dev
->enabled_protocols
& RC_PROTO_BIT_RCMM12
) {
42 rc_keydown(dev
, RC_PROTO_RCMM12
, data
->bits
, 0);
43 data
->state
= STATE_INACTIVE
;
53 * ir_rcmm_decode() - Decode one RCMM pulse or space
54 * @dev: the struct rc_dev descriptor of the device
55 * @ev: the struct ir_raw_event descriptor of the pulse/space
57 * This function returns -EINVAL if the pulse violates the state machine
59 static int ir_rcmm_decode(struct rc_dev
*dev
, struct ir_raw_event ev
)
61 struct rcmm_dec
*data
= &dev
->raw
->rcmm
;
66 if (!(dev
->enabled_protocols
& (RC_PROTO_BIT_RCMM32
|
68 RC_PROTO_BIT_RCMM12
)))
71 if (!is_timing_event(ev
)) {
73 data
->state
= STATE_INACTIVE
;
77 switch (data
->state
) {
82 if (!eq_margin(ev
.duration
, RCMM_PREFIX_PULSE
, RCMM_UNIT
/ 2))
85 data
->state
= STATE_LOW
;
94 if (!eq_margin(ev
.duration
, RCMM_PULSE_0
, RCMM_UNIT
/ 2))
97 data
->state
= STATE_BUMP
;
104 if (!eq_margin(ev
.duration
, RCMM_UNIT
, RCMM_UNIT
/ 2))
107 data
->state
= STATE_VALUE
;
114 if (eq_margin(ev
.duration
, RCMM_PULSE_0
, RCMM_UNIT
/ 2))
116 else if (eq_margin(ev
.duration
, RCMM_PULSE_1
, RCMM_UNIT
/ 2))
118 else if (eq_margin(ev
.duration
, RCMM_PULSE_2
, RCMM_UNIT
/ 2))
120 else if (eq_margin(ev
.duration
, RCMM_PULSE_3
, RCMM_UNIT
/ 2))
126 if (!rcmm_miscmode(dev
, data
))
136 if (data
->count
< 32)
137 data
->state
= STATE_BUMP
;
139 data
->state
= STATE_FINISHED
;
147 if (!eq_margin(ev
.duration
, RCMM_UNIT
, RCMM_UNIT
/ 2))
150 if (rcmm_mode(data
)) {
151 toggle
= !!(0x8000 & data
->bits
);
152 scancode
= data
->bits
& ~0x8000;
155 scancode
= data
->bits
;
158 if (dev
->enabled_protocols
& RC_PROTO_BIT_RCMM32
) {
159 rc_keydown(dev
, RC_PROTO_RCMM32
, scancode
, toggle
);
160 data
->state
= STATE_INACTIVE
;
167 data
->state
= STATE_INACTIVE
;
171 static const int rcmmspace
[] = {
178 static int ir_rcmm_rawencoder(struct ir_raw_event
**ev
, unsigned int max
,
179 unsigned int n
, u32 data
)
184 ret
= ir_raw_gen_pulse_space(ev
, &max
, RCMM_PREFIX_PULSE
, RCMM_PULSE_0
);
188 for (i
= n
- 2; i
>= 0; i
-= 2) {
189 const unsigned int space
= rcmmspace
[(data
>> i
) & 3];
191 ret
= ir_raw_gen_pulse_space(ev
, &max
, RCMM_UNIT
, space
);
196 return ir_raw_gen_pulse_space(ev
, &max
, RCMM_UNIT
, RCMM_PULSE_3
* 2);
199 static int ir_rcmm_encode(enum rc_proto protocol
, u32 scancode
,
200 struct ir_raw_event
*events
, unsigned int max
)
202 struct ir_raw_event
*e
= events
;
206 case RC_PROTO_RCMM32
:
207 ret
= ir_rcmm_rawencoder(&e
, max
, 32, scancode
);
209 case RC_PROTO_RCMM24
:
210 ret
= ir_rcmm_rawencoder(&e
, max
, 24, scancode
);
212 case RC_PROTO_RCMM12
:
213 ret
= ir_rcmm_rawencoder(&e
, max
, 12, scancode
);
225 static struct ir_raw_handler rcmm_handler
= {
226 .protocols
= RC_PROTO_BIT_RCMM32
|
227 RC_PROTO_BIT_RCMM24
|
229 .decode
= ir_rcmm_decode
,
230 .encode
= ir_rcmm_encode
,
232 .min_timeout
= RCMM_PULSE_3
+ RCMM_UNIT
,
235 static int __init
ir_rcmm_decode_init(void)
237 ir_raw_handler_register(&rcmm_handler
);
239 pr_info("IR RCMM protocol handler initialized\n");
243 static void __exit
ir_rcmm_decode_exit(void)
245 ir_raw_handler_unregister(&rcmm_handler
);
248 module_init(ir_rcmm_decode_init
);
249 module_exit(ir_rcmm_decode_exit
);
251 MODULE_LICENSE("GPL");
252 MODULE_AUTHOR("Patrick Lerda");
253 MODULE_DESCRIPTION("RCMM IR protocol decoder");