1 // SPDX-License-Identifier: GPL-2.0+
2 // ir-imon-decoder.c - handle iMon protocol
3 //
4 // Copyright (C) 2018 by Sean Young <sean@mess.org>
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/module.h>
9 #include "rc-core-priv.h"
10 
11 #define IMON_UNIT		415662 /* ns */
12 #define IMON_BITS		30
13 #define IMON_CHKBITS		(BIT(30) | BIT(25) | BIT(24) | BIT(22) | \
14 				 BIT(21) | BIT(20) | BIT(19) | BIT(18) | \
15 				 BIT(17) | BIT(16) | BIT(14) | BIT(13) | \
16 				 BIT(12) | BIT(11) | BIT(10) | BIT(9))
17 
18 /*
19  * This protocol has 30 bits. The format is one IMON_UNIT header pulse,
20  * followed by 30 bits. Each bit is one IMON_UNIT check field, and then
21  * one IMON_UNIT field with the actual bit (1=space, 0=pulse).
22  * The check field is always space for some bits, for others it is pulse if
23  * both the preceding and current bit are zero, else space. IMON_CHKBITS
24  * defines which bits are of type check.
25  *
26  * There is no way to distinguish an incomplete message from one where
27  * the lower bits are all set, iow. the last pulse is for the lowest
28  * bit which is 0.
29  */
30 enum imon_state {
31 	STATE_INACTIVE,
32 	STATE_BIT_CHK,
33 	STATE_BIT_START,
34 	STATE_FINISHED,
35 	STATE_ERROR,
36 };
37 
38 static void ir_imon_decode_scancode(struct rc_dev *dev)
39 {
40 	struct imon_dec *imon = &dev->raw->imon;
41 
42 	/* Keyboard/Mouse toggle */
43 	if (imon->bits == 0x299115b7)
44 		imon->stick_keyboard = !imon->stick_keyboard;
45 
46 	if ((imon->bits & 0xfc0000ff) == 0x680000b7) {
47 		int rel_x, rel_y;
48 		u8 buf;
49 
50 		buf = imon->bits >> 16;
51 		rel_x = (buf & 0x08) | (buf & 0x10) >> 2 |
52 			(buf & 0x20) >> 4 | (buf & 0x40) >> 6;
53 		if (imon->bits & 0x02000000)
54 			rel_x |= ~0x0f;
55 		buf = imon->bits >> 8;
56 		rel_y = (buf & 0x08) | (buf & 0x10) >> 2 |
57 			(buf & 0x20) >> 4 | (buf & 0x40) >> 6;
58 		if (imon->bits & 0x01000000)
59 			rel_y |= ~0x0f;
60 
61 		if (rel_x && rel_y && imon->stick_keyboard) {
62 			if (abs(rel_y) > abs(rel_x))
63 				imon->bits = rel_y > 0 ?
64 					0x289515b7 : /* KEY_DOWN */
65 					0x2aa515b7;  /* KEY_UP */
66 			else
67 				imon->bits = rel_x > 0 ?
68 					0x2ba515b7 : /* KEY_RIGHT */
69 					0x29a515b7;  /* KEY_LEFT */
70 		}
71 
72 		if (!imon->stick_keyboard) {
73 			struct lirc_scancode lsc = {
74 				.scancode = imon->bits,
75 				.rc_proto = RC_PROTO_IMON,
76 			};
77 
78 			ir_lirc_scancode_event(dev, &lsc);
79 
80 			input_event(imon->idev, EV_MSC, MSC_SCAN, imon->bits);
81 
82 			input_report_rel(imon->idev, REL_X, rel_x);
83 			input_report_rel(imon->idev, REL_Y, rel_y);
84 
85 			input_report_key(imon->idev, BTN_LEFT,
86 					 (imon->bits & 0x00010000) != 0);
87 			input_report_key(imon->idev, BTN_RIGHT,
88 					 (imon->bits & 0x00040000) != 0);
89 			input_sync(imon->idev);
90 			return;
91 		}
92 	}
93 
94 	rc_keydown(dev, RC_PROTO_IMON, imon->bits, 0);
95 }
96 
97 /**
98  * ir_imon_decode() - Decode one iMON pulse or space
99  * @dev:	the struct rc_dev descriptor of the device
100  * @ev:		the struct ir_raw_event descriptor of the pulse/space
101  *
102  * This function returns -EINVAL if the pulse violates the state machine
103  */
104 static int ir_imon_decode(struct rc_dev *dev, struct ir_raw_event ev)
105 {
106 	struct imon_dec *data = &dev->raw->imon;
107 
108 	if (!is_timing_event(ev)) {
109 		if (ev.reset)
110 			data->state = STATE_INACTIVE;
111 		return 0;
112 	}
113 
114 	dev_dbg(&dev->dev,
115 		"iMON decode started at state %d bitno %d (%uus %s)\n",
116 		data->state, data->count, TO_US(ev.duration),
117 		TO_STR(ev.pulse));
118 
119 	/*
120 	 * Since iMON protocol is a series of bits, if at any point
121 	 * we encounter an error, make sure that any remaining bits
122 	 * aren't parsed as a scancode made up of less bits.
123 	 *
124 	 * Note that if the stick is held, then the remote repeats
125 	 * the scancode with about 12ms between them. So, make sure
126 	 * we have at least 10ms of space after an error. That way,
127 	 * we're at a new scancode.
128 	 */
129 	if (data->state == STATE_ERROR) {
130 		if (!ev.pulse && ev.duration > MS_TO_NS(10))
131 			data->state = STATE_INACTIVE;
132 		return 0;
133 	}
134 
135 	for (;;) {
136 		if (!geq_margin(ev.duration, IMON_UNIT, IMON_UNIT / 2))
137 			return 0;
138 
139 		decrease_duration(&ev, IMON_UNIT);
140 
141 		switch (data->state) {
142 		case STATE_INACTIVE:
143 			if (ev.pulse) {
144 				data->state = STATE_BIT_CHK;
145 				data->bits = 0;
146 				data->count = IMON_BITS;
147 			}
148 			break;
149 		case STATE_BIT_CHK:
150 			if (IMON_CHKBITS & BIT(data->count))
151 				data->last_chk = ev.pulse;
152 			else if (ev.pulse)
153 				goto err_out;
154 			data->state = STATE_BIT_START;
155 			break;
156 		case STATE_BIT_START:
157 			data->bits <<= 1;
158 			if (!ev.pulse)
159 				data->bits |= 1;
160 
161 			if (IMON_CHKBITS & BIT(data->count)) {
162 				if (data->last_chk != !(data->bits & 3))
163 					goto err_out;
164 			}
165 
166 			if (!data->count--)
167 				data->state = STATE_FINISHED;
168 			else
169 				data->state = STATE_BIT_CHK;
170 			break;
171 		case STATE_FINISHED:
172 			if (ev.pulse)
173 				goto err_out;
174 			ir_imon_decode_scancode(dev);
175 			data->state = STATE_INACTIVE;
176 			break;
177 		}
178 	}
179 
180 err_out:
181 	dev_dbg(&dev->dev,
182 		"iMON decode failed at state %d bitno %d (%uus %s)\n",
183 		data->state, data->count, TO_US(ev.duration),
184 		TO_STR(ev.pulse));
185 
186 	data->state = STATE_ERROR;
187 
188 	return -EINVAL;
189 }
190 
191 /**
192  * ir_imon_encode() - Encode a scancode as a stream of raw events
193  *
194  * @protocol:	protocol to encode
195  * @scancode:	scancode to encode
196  * @events:	array of raw ir events to write into
197  * @max:	maximum size of @events
198  *
199  * Returns:	The number of events written.
200  *		-ENOBUFS if there isn't enough space in the array to fit the
201  *		encoding. In this case all @max events will have been written.
202  */
203 static int ir_imon_encode(enum rc_proto protocol, u32 scancode,
204 			  struct ir_raw_event *events, unsigned int max)
205 {
206 	struct ir_raw_event *e = events;
207 	int i, pulse;
208 
209 	if (!max--)
210 		return -ENOBUFS;
211 	init_ir_raw_event_duration(e, 1, IMON_UNIT);
212 
213 	for (i = IMON_BITS; i >= 0; i--) {
214 		if (BIT(i) & IMON_CHKBITS)
215 			pulse = !(scancode & (BIT(i) | BIT(i + 1)));
216 		else
217 			pulse = 0;
218 
219 		if (pulse == e->pulse) {
220 			e->duration += IMON_UNIT;
221 		} else {
222 			if (!max--)
223 				return -ENOBUFS;
224 			init_ir_raw_event_duration(++e, pulse, IMON_UNIT);
225 		}
226 
227 		pulse = !(scancode & BIT(i));
228 
229 		if (pulse == e->pulse) {
230 			e->duration += IMON_UNIT;
231 		} else {
232 			if (!max--)
233 				return -ENOBUFS;
234 			init_ir_raw_event_duration(++e, pulse, IMON_UNIT);
235 		}
236 	}
237 
238 	if (e->pulse)
239 		e++;
240 
241 	return e - events;
242 }
243 
244 static int ir_imon_register(struct rc_dev *dev)
245 {
246 	struct input_dev *idev;
247 	struct imon_dec *imon = &dev->raw->imon;
248 	int ret;
249 
250 	idev = input_allocate_device();
251 	if (!idev)
252 		return -ENOMEM;
253 
254 	snprintf(imon->name, sizeof(imon->name),
255 		 "iMON PAD Stick (%s)", dev->device_name);
256 	idev->name = imon->name;
257 	idev->phys = dev->input_phys;
258 
259 	/* Mouse bits */
260 	set_bit(EV_REL, idev->evbit);
261 	set_bit(EV_KEY, idev->evbit);
262 	set_bit(REL_X, idev->relbit);
263 	set_bit(REL_Y, idev->relbit);
264 	set_bit(BTN_LEFT, idev->keybit);
265 	set_bit(BTN_RIGHT, idev->keybit);
266 
267 	/* Report scancodes too */
268 	set_bit(EV_MSC, idev->evbit);
269 	set_bit(MSC_SCAN, idev->mscbit);
270 
271 	input_set_drvdata(idev, imon);
272 
273 	ret = input_register_device(idev);
274 	if (ret < 0) {
275 		input_free_device(idev);
276 		return -EIO;
277 	}
278 
279 	imon->idev = idev;
280 	imon->stick_keyboard = false;
281 
282 	return 0;
283 }
284 
285 static int ir_imon_unregister(struct rc_dev *dev)
286 {
287 	struct imon_dec *imon = &dev->raw->imon;
288 
289 	input_unregister_device(imon->idev);
290 	imon->idev = NULL;
291 
292 	return 0;
293 }
294 
295 static struct ir_raw_handler imon_handler = {
296 	.protocols	= RC_PROTO_BIT_IMON,
297 	.decode		= ir_imon_decode,
298 	.encode		= ir_imon_encode,
299 	.carrier	= 38000,
300 	.raw_register	= ir_imon_register,
301 	.raw_unregister	= ir_imon_unregister,
302 	.min_timeout	= IMON_UNIT * IMON_BITS * 2,
303 };
304 
305 static int __init ir_imon_decode_init(void)
306 {
307 	ir_raw_handler_register(&imon_handler);
308 
309 	pr_info("IR iMON protocol handler initialized\n");
310 	return 0;
311 }
312 
313 static void __exit ir_imon_decode_exit(void)
314 {
315 	ir_raw_handler_unregister(&imon_handler);
316 }
317 
318 module_init(ir_imon_decode_init);
319 module_exit(ir_imon_decode_exit);
320 
321 MODULE_LICENSE("GPL");
322 MODULE_AUTHOR("Sean Young <sean@mess.org>");
323 MODULE_DESCRIPTION("iMON IR protocol decoder");
324