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