1 // SPDX-License-Identifier: GPL-2.0-only
2 /* ir-mce_kbd-decoder.c - A decoder for the RC6-ish keyboard/mouse IR protocol
3  * used by the Microsoft Remote Keyboard for Windows Media Center Edition,
4  * referred to by Microsoft's Windows Media Center remote specification docs
5  * as "an internal protocol called MCIR-2".
6  *
7  * Copyright (C) 2011 by Jarod Wilson <jarod@redhat.com>
8  */
9 #include <linux/module.h>
10 
11 #include "rc-core-priv.h"
12 
13 /*
14  * This decoder currently supports:
15  * - MCIR-2 29-bit IR signals used for mouse movement and buttons
16  * - MCIR-2 32-bit IR signals used for standard keyboard keys
17  *
18  * The media keys on the keyboard send RC-6 signals that are indistinguishable
19  * from the keys of the same name on the stock MCE remote, and will be handled
20  * by the standard RC-6 decoder, and be made available to the system via the
21  * input device for the remote, rather than the keyboard/mouse one.
22  */
23 
24 #define MCIR2_UNIT		333	/* us */
25 #define MCIR2_HEADER_NBITS	5
26 #define MCIR2_MOUSE_NBITS	29
27 #define MCIR2_KEYBOARD_NBITS	32
28 #define MCIR2_PREFIX_PULSE	(8 * MCIR2_UNIT)
29 #define MCIR2_PREFIX_SPACE	(1 * MCIR2_UNIT)
30 #define MCIR2_MAX_LEN		(3 * MCIR2_UNIT)
31 #define MCIR2_BIT_START		(1 * MCIR2_UNIT)
32 #define MCIR2_BIT_END		(1 * MCIR2_UNIT)
33 #define MCIR2_BIT_0		(1 * MCIR2_UNIT)
34 #define MCIR2_BIT_SET		(2 * MCIR2_UNIT)
35 #define MCIR2_MODE_MASK		0xf	/* for the header bits */
36 #define MCIR2_KEYBOARD_HEADER	0x4
37 #define MCIR2_MOUSE_HEADER	0x1
38 #define MCIR2_MASK_KEYS_START	0xe0
39 
40 enum mce_kbd_mode {
41 	MCIR2_MODE_KEYBOARD,
42 	MCIR2_MODE_MOUSE,
43 	MCIR2_MODE_UNKNOWN,
44 };
45 
46 enum mce_kbd_state {
47 	STATE_INACTIVE,
48 	STATE_HEADER_BIT_START,
49 	STATE_HEADER_BIT_END,
50 	STATE_BODY_BIT_START,
51 	STATE_BODY_BIT_END,
52 	STATE_FINISHED,
53 };
54 
55 static unsigned char kbd_keycodes[256] = {
56 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_A,
57 	KEY_B,		KEY_C,		KEY_D,		KEY_E,		KEY_F,
58 	KEY_G,		KEY_H,		KEY_I,		KEY_J,		KEY_K,
59 	KEY_L,		KEY_M,		KEY_N,		KEY_O,		KEY_P,
60 	KEY_Q,		KEY_R,		KEY_S,		KEY_T,		KEY_U,
61 	KEY_V,		KEY_W,		KEY_X,		KEY_Y,		KEY_Z,
62 	KEY_1,		KEY_2,		KEY_3,		KEY_4,		KEY_5,
63 	KEY_6,		KEY_7,		KEY_8,		KEY_9,		KEY_0,
64 	KEY_ENTER,	KEY_ESC,	KEY_BACKSPACE,	KEY_TAB,	KEY_SPACE,
65 	KEY_MINUS,	KEY_EQUAL,	KEY_LEFTBRACE,	KEY_RIGHTBRACE,	KEY_BACKSLASH,
66 	KEY_BACKSLASH,	KEY_SEMICOLON,	KEY_APOSTROPHE,	KEY_GRAVE,	KEY_COMMA,
67 	KEY_DOT,	KEY_SLASH,	KEY_CAPSLOCK,	KEY_F1,		KEY_F2,
68 	KEY_F3,		KEY_F4,		KEY_F5,		KEY_F6,		KEY_F7,
69 	KEY_F8,		KEY_F9,		KEY_F10,	KEY_F11,	KEY_F12,
70 	KEY_SYSRQ,	KEY_SCROLLLOCK,	KEY_PAUSE,	KEY_INSERT,	KEY_HOME,
71 	KEY_PAGEUP,	KEY_DELETE,	KEY_END,	KEY_PAGEDOWN,	KEY_RIGHT,
72 	KEY_LEFT,	KEY_DOWN,	KEY_UP,		KEY_NUMLOCK,	KEY_KPSLASH,
73 	KEY_KPASTERISK,	KEY_KPMINUS,	KEY_KPPLUS,	KEY_KPENTER,	KEY_KP1,
74 	KEY_KP2,	KEY_KP3,	KEY_KP4,	KEY_KP5,	KEY_KP6,
75 	KEY_KP7,	KEY_KP8,	KEY_KP9,	KEY_KP0,	KEY_KPDOT,
76 	KEY_102ND,	KEY_COMPOSE,	KEY_POWER,	KEY_KPEQUAL,	KEY_F13,
77 	KEY_F14,	KEY_F15,	KEY_F16,	KEY_F17,	KEY_F18,
78 	KEY_F19,	KEY_F20,	KEY_F21,	KEY_F22,	KEY_F23,
79 	KEY_F24,	KEY_OPEN,	KEY_HELP,	KEY_PROPS,	KEY_FRONT,
80 	KEY_STOP,	KEY_AGAIN,	KEY_UNDO,	KEY_CUT,	KEY_COPY,
81 	KEY_PASTE,	KEY_FIND,	KEY_MUTE,	KEY_VOLUMEUP,	KEY_VOLUMEDOWN,
82 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_KPCOMMA,	KEY_RESERVED,
83 	KEY_RO,		KEY_KATAKANAHIRAGANA, KEY_YEN,	KEY_HENKAN,	KEY_MUHENKAN,
84 	KEY_KPJPCOMMA,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_HANGUEL,
85 	KEY_HANJA,	KEY_KATAKANA,	KEY_HIRAGANA,	KEY_ZENKAKUHANKAKU, KEY_RESERVED,
86 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
87 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
88 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
89 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
90 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
91 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
92 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
93 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
94 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
95 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
96 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
97 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
98 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
99 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
100 	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,	KEY_LEFTCTRL,
101 	KEY_LEFTSHIFT,	KEY_LEFTALT,	KEY_LEFTMETA,	KEY_RIGHTCTRL,	KEY_RIGHTSHIFT,
102 	KEY_RIGHTALT,	KEY_RIGHTMETA,	KEY_PLAYPAUSE,	KEY_STOPCD,	KEY_PREVIOUSSONG,
103 	KEY_NEXTSONG,	KEY_EJECTCD,	KEY_VOLUMEUP,	KEY_VOLUMEDOWN,	KEY_MUTE,
104 	KEY_WWW,	KEY_BACK,	KEY_FORWARD,	KEY_STOP,	KEY_FIND,
105 	KEY_SCROLLUP,	KEY_SCROLLDOWN,	KEY_EDIT,	KEY_SLEEP,	KEY_COFFEE,
106 	KEY_REFRESH,	KEY_CALC,	KEY_RESERVED,	KEY_RESERVED,	KEY_RESERVED,
107 	KEY_RESERVED
108 };
109 
110 static void mce_kbd_rx_timeout(struct timer_list *t)
111 {
112 	struct ir_raw_event_ctrl *raw = from_timer(raw, t, mce_kbd.rx_timeout);
113 	unsigned char maskcode;
114 	unsigned long flags;
115 	int i;
116 
117 	dev_dbg(&raw->dev->dev, "timer callback clearing all keys\n");
118 
119 	spin_lock_irqsave(&raw->mce_kbd.keylock, flags);
120 
121 	if (time_is_before_eq_jiffies(raw->mce_kbd.rx_timeout.expires)) {
122 		for (i = 0; i < 7; i++) {
123 			maskcode = kbd_keycodes[MCIR2_MASK_KEYS_START + i];
124 			input_report_key(raw->dev->input_dev, maskcode, 0);
125 		}
126 
127 		for (i = 0; i < MCIR2_MASK_KEYS_START; i++)
128 			input_report_key(raw->dev->input_dev, kbd_keycodes[i],
129 					 0);
130 
131 		input_sync(raw->dev->input_dev);
132 	}
133 	spin_unlock_irqrestore(&raw->mce_kbd.keylock, flags);
134 }
135 
136 static enum mce_kbd_mode mce_kbd_mode(struct mce_kbd_dec *data)
137 {
138 	switch (data->header & MCIR2_MODE_MASK) {
139 	case MCIR2_KEYBOARD_HEADER:
140 		return MCIR2_MODE_KEYBOARD;
141 	case MCIR2_MOUSE_HEADER:
142 		return MCIR2_MODE_MOUSE;
143 	default:
144 		return MCIR2_MODE_UNKNOWN;
145 	}
146 }
147 
148 static void ir_mce_kbd_process_keyboard_data(struct rc_dev *dev, u32 scancode)
149 {
150 	u8 keydata1  = (scancode >> 8) & 0xff;
151 	u8 keydata2  = (scancode >> 16) & 0xff;
152 	u8 shiftmask = scancode & 0xff;
153 	unsigned char maskcode;
154 	int i, keystate;
155 
156 	dev_dbg(&dev->dev, "keyboard: keydata2 = 0x%02x, keydata1 = 0x%02x, shiftmask = 0x%02x\n",
157 		keydata2, keydata1, shiftmask);
158 
159 	for (i = 0; i < 7; i++) {
160 		maskcode = kbd_keycodes[MCIR2_MASK_KEYS_START + i];
161 		if (shiftmask & (1 << i))
162 			keystate = 1;
163 		else
164 			keystate = 0;
165 		input_report_key(dev->input_dev, maskcode, keystate);
166 	}
167 
168 	if (keydata1)
169 		input_report_key(dev->input_dev, kbd_keycodes[keydata1], 1);
170 	if (keydata2)
171 		input_report_key(dev->input_dev, kbd_keycodes[keydata2], 1);
172 
173 	if (!keydata1 && !keydata2) {
174 		for (i = 0; i < MCIR2_MASK_KEYS_START; i++)
175 			input_report_key(dev->input_dev, kbd_keycodes[i], 0);
176 	}
177 }
178 
179 static void ir_mce_kbd_process_mouse_data(struct rc_dev *dev, u32 scancode)
180 {
181 	/* raw mouse coordinates */
182 	u8 xdata = (scancode >> 7) & 0x7f;
183 	u8 ydata = (scancode >> 14) & 0x7f;
184 	int x, y;
185 	/* mouse buttons */
186 	bool right = scancode & 0x40;
187 	bool left  = scancode & 0x20;
188 
189 	if (xdata & 0x40)
190 		x = -((~xdata & 0x7f) + 1);
191 	else
192 		x = xdata;
193 
194 	if (ydata & 0x40)
195 		y = -((~ydata & 0x7f) + 1);
196 	else
197 		y = ydata;
198 
199 	dev_dbg(&dev->dev, "mouse: x = %d, y = %d, btns = %s%s\n",
200 		x, y, left ? "L" : "", right ? "R" : "");
201 
202 	input_report_rel(dev->input_dev, REL_X, x);
203 	input_report_rel(dev->input_dev, REL_Y, y);
204 
205 	input_report_key(dev->input_dev, BTN_LEFT, left);
206 	input_report_key(dev->input_dev, BTN_RIGHT, right);
207 }
208 
209 /**
210  * ir_mce_kbd_decode() - Decode one mce_kbd pulse or space
211  * @dev:	the struct rc_dev descriptor of the device
212  * @ev:		the struct ir_raw_event descriptor of the pulse/space
213  *
214  * This function returns -EINVAL if the pulse violates the state machine
215  */
216 static int ir_mce_kbd_decode(struct rc_dev *dev, struct ir_raw_event ev)
217 {
218 	struct mce_kbd_dec *data = &dev->raw->mce_kbd;
219 	u32 scancode;
220 	unsigned long delay;
221 	struct lirc_scancode lsc = {};
222 
223 	if (!is_timing_event(ev)) {
224 		if (ev.reset)
225 			data->state = STATE_INACTIVE;
226 		return 0;
227 	}
228 
229 	if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2))
230 		goto out;
231 
232 again:
233 	dev_dbg(&dev->dev, "started at state %i (%uus %s)\n",
234 		data->state, ev.duration, TO_STR(ev.pulse));
235 
236 	if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2))
237 		return 0;
238 
239 	switch (data->state) {
240 
241 	case STATE_INACTIVE:
242 		if (!ev.pulse)
243 			break;
244 
245 		/* Note: larger margin on first pulse since each MCIR2_UNIT
246 		   is quite short and some hardware takes some time to
247 		   adjust to the signal */
248 		if (!eq_margin(ev.duration, MCIR2_PREFIX_PULSE, MCIR2_UNIT))
249 			break;
250 
251 		data->state = STATE_HEADER_BIT_START;
252 		data->count = 0;
253 		data->header = 0;
254 		return 0;
255 
256 	case STATE_HEADER_BIT_START:
257 		if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2))
258 			break;
259 
260 		data->header <<= 1;
261 		if (ev.pulse)
262 			data->header |= 1;
263 		data->count++;
264 		data->state = STATE_HEADER_BIT_END;
265 		return 0;
266 
267 	case STATE_HEADER_BIT_END:
268 		decrease_duration(&ev, MCIR2_BIT_END);
269 
270 		if (data->count != MCIR2_HEADER_NBITS) {
271 			data->state = STATE_HEADER_BIT_START;
272 			goto again;
273 		}
274 
275 		switch (mce_kbd_mode(data)) {
276 		case MCIR2_MODE_KEYBOARD:
277 			data->wanted_bits = MCIR2_KEYBOARD_NBITS;
278 			break;
279 		case MCIR2_MODE_MOUSE:
280 			data->wanted_bits = MCIR2_MOUSE_NBITS;
281 			break;
282 		default:
283 			dev_dbg(&dev->dev, "not keyboard or mouse data\n");
284 			goto out;
285 		}
286 
287 		data->count = 0;
288 		data->body = 0;
289 		data->state = STATE_BODY_BIT_START;
290 		goto again;
291 
292 	case STATE_BODY_BIT_START:
293 		if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2))
294 			break;
295 
296 		data->body <<= 1;
297 		if (ev.pulse)
298 			data->body |= 1;
299 		data->count++;
300 		data->state = STATE_BODY_BIT_END;
301 		return 0;
302 
303 	case STATE_BODY_BIT_END:
304 		if (data->count == data->wanted_bits)
305 			data->state = STATE_FINISHED;
306 		else
307 			data->state = STATE_BODY_BIT_START;
308 
309 		decrease_duration(&ev, MCIR2_BIT_END);
310 		goto again;
311 
312 	case STATE_FINISHED:
313 		if (ev.pulse)
314 			break;
315 
316 		switch (data->wanted_bits) {
317 		case MCIR2_KEYBOARD_NBITS:
318 			scancode = data->body & 0xffffff;
319 			dev_dbg(&dev->dev, "keyboard data 0x%08x\n",
320 				data->body);
321 			spin_lock(&data->keylock);
322 			if (scancode) {
323 				delay = nsecs_to_jiffies(dev->timeout) +
324 					msecs_to_jiffies(100);
325 				mod_timer(&data->rx_timeout, jiffies + delay);
326 			} else {
327 				del_timer(&data->rx_timeout);
328 			}
329 			/* Pass data to keyboard buffer parser */
330 			ir_mce_kbd_process_keyboard_data(dev, scancode);
331 			spin_unlock(&data->keylock);
332 			lsc.rc_proto = RC_PROTO_MCIR2_KBD;
333 			break;
334 		case MCIR2_MOUSE_NBITS:
335 			scancode = data->body & 0x1fffff;
336 			dev_dbg(&dev->dev, "mouse data 0x%06x\n", scancode);
337 			/* Pass data to mouse buffer parser */
338 			ir_mce_kbd_process_mouse_data(dev, scancode);
339 			lsc.rc_proto = RC_PROTO_MCIR2_MSE;
340 			break;
341 		default:
342 			dev_dbg(&dev->dev, "not keyboard or mouse data\n");
343 			goto out;
344 		}
345 
346 		lsc.scancode = scancode;
347 		lirc_scancode_event(dev, &lsc);
348 		data->state = STATE_INACTIVE;
349 		input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
350 		input_sync(dev->input_dev);
351 		return 0;
352 	}
353 
354 out:
355 	dev_dbg(&dev->dev, "failed at state %i (%uus %s)\n",
356 		data->state, ev.duration, TO_STR(ev.pulse));
357 	data->state = STATE_INACTIVE;
358 	return -EINVAL;
359 }
360 
361 static int ir_mce_kbd_register(struct rc_dev *dev)
362 {
363 	struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd;
364 
365 	timer_setup(&mce_kbd->rx_timeout, mce_kbd_rx_timeout, 0);
366 	spin_lock_init(&mce_kbd->keylock);
367 
368 	return 0;
369 }
370 
371 static int ir_mce_kbd_unregister(struct rc_dev *dev)
372 {
373 	struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd;
374 
375 	del_timer_sync(&mce_kbd->rx_timeout);
376 
377 	return 0;
378 }
379 
380 static const struct ir_raw_timings_manchester ir_mce_kbd_timings = {
381 	.leader_pulse	= MCIR2_PREFIX_PULSE,
382 	.invert		= 1,
383 	.clock		= MCIR2_UNIT,
384 	.trailer_space	= MCIR2_UNIT * 10,
385 };
386 
387 /**
388  * ir_mce_kbd_encode() - Encode a scancode as a stream of raw events
389  *
390  * @protocol:   protocol to encode
391  * @scancode:   scancode to encode
392  * @events:     array of raw ir events to write into
393  * @max:        maximum size of @events
394  *
395  * Returns:     The number of events written.
396  *              -ENOBUFS if there isn't enough space in the array to fit the
397  *              encoding. In this case all @max events will have been written.
398  */
399 static int ir_mce_kbd_encode(enum rc_proto protocol, u32 scancode,
400 			     struct ir_raw_event *events, unsigned int max)
401 {
402 	struct ir_raw_event *e = events;
403 	int len, ret;
404 	u64 raw;
405 
406 	if (protocol == RC_PROTO_MCIR2_KBD) {
407 		raw = scancode |
408 		      ((u64)MCIR2_KEYBOARD_HEADER << MCIR2_KEYBOARD_NBITS);
409 		len = MCIR2_KEYBOARD_NBITS + MCIR2_HEADER_NBITS;
410 	} else {
411 		raw = scancode |
412 		      ((u64)MCIR2_MOUSE_HEADER << MCIR2_MOUSE_NBITS);
413 		len = MCIR2_MOUSE_NBITS + MCIR2_HEADER_NBITS;
414 	}
415 
416 	ret = ir_raw_gen_manchester(&e, max, &ir_mce_kbd_timings, len, raw);
417 	if (ret < 0)
418 		return ret;
419 
420 	return e - events;
421 }
422 
423 static struct ir_raw_handler mce_kbd_handler = {
424 	.protocols	= RC_PROTO_BIT_MCIR2_KBD | RC_PROTO_BIT_MCIR2_MSE,
425 	.decode		= ir_mce_kbd_decode,
426 	.encode		= ir_mce_kbd_encode,
427 	.raw_register	= ir_mce_kbd_register,
428 	.raw_unregister	= ir_mce_kbd_unregister,
429 	.carrier	= 36000,
430 	.min_timeout	= MCIR2_MAX_LEN + MCIR2_UNIT / 2,
431 };
432 
433 static int __init ir_mce_kbd_decode_init(void)
434 {
435 	ir_raw_handler_register(&mce_kbd_handler);
436 
437 	printk(KERN_INFO "IR MCE Keyboard/mouse protocol handler initialized\n");
438 	return 0;
439 }
440 
441 static void __exit ir_mce_kbd_decode_exit(void)
442 {
443 	ir_raw_handler_unregister(&mce_kbd_handler);
444 }
445 
446 module_init(ir_mce_kbd_decode_init);
447 module_exit(ir_mce_kbd_decode_exit);
448 
449 MODULE_LICENSE("GPL");
450 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
451 MODULE_DESCRIPTION("MCE Keyboard/mouse IR protocol decoder");
452