1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/module.h>
18
19#include "rc-core-priv.h"
20
21
22
23
24
25
26
27
28
29
30
31
32#define MCIR2_UNIT 333333
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
44#define MCIR2_KEYBOARD_HEADER 0x4
45#define MCIR2_MOUSE_HEADER 0x1
46#define MCIR2_MASK_KEYS_START 0xe0
47
48enum mce_kbd_mode {
49 MCIR2_MODE_KEYBOARD,
50 MCIR2_MODE_MOUSE,
51 MCIR2_MODE_UNKNOWN,
52};
53
54enum 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
63static 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
118static void mce_kbd_rx_timeout(struct timer_list *t)
119{
120 struct ir_raw_event_ctrl *raw = from_timer(raw, t, mce_kbd.rx_timeout);
121 unsigned char maskcode;
122 unsigned long flags;
123 int i;
124
125 dev_dbg(&raw->dev->dev, "timer callback clearing all keys\n");
126
127 spin_lock_irqsave(&raw->mce_kbd.keylock, flags);
128
129 if (time_is_before_eq_jiffies(raw->mce_kbd.rx_timeout.expires)) {
130 for (i = 0; i < 7; i++) {
131 maskcode = kbd_keycodes[MCIR2_MASK_KEYS_START + i];
132 input_report_key(raw->mce_kbd.idev, maskcode, 0);
133 }
134
135 for (i = 0; i < MCIR2_MASK_KEYS_START; i++)
136 input_report_key(raw->mce_kbd.idev, kbd_keycodes[i], 0);
137
138 input_sync(raw->mce_kbd.idev);
139 }
140 spin_unlock_irqrestore(&raw->mce_kbd.keylock, flags);
141}
142
143static enum mce_kbd_mode mce_kbd_mode(struct mce_kbd_dec *data)
144{
145 switch (data->header & MCIR2_MODE_MASK) {
146 case MCIR2_KEYBOARD_HEADER:
147 return MCIR2_MODE_KEYBOARD;
148 case MCIR2_MOUSE_HEADER:
149 return MCIR2_MODE_MOUSE;
150 default:
151 return MCIR2_MODE_UNKNOWN;
152 }
153}
154
155static void ir_mce_kbd_process_keyboard_data(struct rc_dev *dev, u32 scancode)
156{
157 struct mce_kbd_dec *data = &dev->raw->mce_kbd;
158 u8 keydata1 = (scancode >> 8) & 0xff;
159 u8 keydata2 = (scancode >> 16) & 0xff;
160 u8 shiftmask = scancode & 0xff;
161 unsigned char maskcode;
162 int i, keystate;
163
164 dev_dbg(&dev->dev, "keyboard: keydata2 = 0x%02x, keydata1 = 0x%02x, shiftmask = 0x%02x\n",
165 keydata2, keydata1, shiftmask);
166
167 for (i = 0; i < 7; i++) {
168 maskcode = kbd_keycodes[MCIR2_MASK_KEYS_START + i];
169 if (shiftmask & (1 << i))
170 keystate = 1;
171 else
172 keystate = 0;
173 input_report_key(data->idev, maskcode, keystate);
174 }
175
176 if (keydata1)
177 input_report_key(data->idev, kbd_keycodes[keydata1], 1);
178 if (keydata2)
179 input_report_key(data->idev, kbd_keycodes[keydata2], 1);
180
181 if (!keydata1 && !keydata2) {
182 for (i = 0; i < MCIR2_MASK_KEYS_START; i++)
183 input_report_key(data->idev, kbd_keycodes[i], 0);
184 }
185}
186
187static void ir_mce_kbd_process_mouse_data(struct rc_dev *dev, u32 scancode)
188{
189 struct mce_kbd_dec *data = &dev->raw->mce_kbd;
190
191 u8 xdata = (scancode >> 7) & 0x7f;
192 u8 ydata = (scancode >> 14) & 0x7f;
193 int x, y;
194
195 bool right = scancode & 0x40;
196 bool left = scancode & 0x20;
197
198 if (xdata & 0x40)
199 x = -((~xdata & 0x7f) + 1);
200 else
201 x = xdata;
202
203 if (ydata & 0x40)
204 y = -((~ydata & 0x7f) + 1);
205 else
206 y = ydata;
207
208 dev_dbg(&dev->dev, "mouse: x = %d, y = %d, btns = %s%s\n",
209 x, y, left ? "L" : "", right ? "R" : "");
210
211 input_report_rel(data->idev, REL_X, x);
212 input_report_rel(data->idev, REL_Y, y);
213
214 input_report_key(data->idev, BTN_LEFT, left);
215 input_report_key(data->idev, BTN_RIGHT, right);
216}
217
218
219
220
221
222
223
224
225static int ir_mce_kbd_decode(struct rc_dev *dev, struct ir_raw_event ev)
226{
227 struct mce_kbd_dec *data = &dev->raw->mce_kbd;
228 u32 scancode;
229 unsigned long delay;
230 struct lirc_scancode lsc = {};
231
232 if (!is_timing_event(ev)) {
233 if (ev.reset)
234 data->state = STATE_INACTIVE;
235 return 0;
236 }
237
238 if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2))
239 goto out;
240
241again:
242 dev_dbg(&dev->dev, "started at state %i (%uus %s)\n",
243 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
244
245 if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2))
246 return 0;
247
248 switch (data->state) {
249
250 case STATE_INACTIVE:
251 if (!ev.pulse)
252 break;
253
254
255
256
257 if (!eq_margin(ev.duration, MCIR2_PREFIX_PULSE, MCIR2_UNIT))
258 break;
259
260 data->state = STATE_HEADER_BIT_START;
261 data->count = 0;
262 data->header = 0;
263 return 0;
264
265 case STATE_HEADER_BIT_START:
266 if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2))
267 break;
268
269 data->header <<= 1;
270 if (ev.pulse)
271 data->header |= 1;
272 data->count++;
273 data->state = STATE_HEADER_BIT_END;
274 return 0;
275
276 case STATE_HEADER_BIT_END:
277 decrease_duration(&ev, MCIR2_BIT_END);
278
279 if (data->count != MCIR2_HEADER_NBITS) {
280 data->state = STATE_HEADER_BIT_START;
281 goto again;
282 }
283
284 switch (mce_kbd_mode(data)) {
285 case MCIR2_MODE_KEYBOARD:
286 data->wanted_bits = MCIR2_KEYBOARD_NBITS;
287 break;
288 case MCIR2_MODE_MOUSE:
289 data->wanted_bits = MCIR2_MOUSE_NBITS;
290 break;
291 default:
292 dev_dbg(&dev->dev, "not keyboard or mouse data\n");
293 goto out;
294 }
295
296 data->count = 0;
297 data->body = 0;
298 data->state = STATE_BODY_BIT_START;
299 goto again;
300
301 case STATE_BODY_BIT_START:
302 if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2))
303 break;
304
305 data->body <<= 1;
306 if (ev.pulse)
307 data->body |= 1;
308 data->count++;
309 data->state = STATE_BODY_BIT_END;
310 return 0;
311
312 case STATE_BODY_BIT_END:
313 if (data->count == data->wanted_bits)
314 data->state = STATE_FINISHED;
315 else
316 data->state = STATE_BODY_BIT_START;
317
318 decrease_duration(&ev, MCIR2_BIT_END);
319 goto again;
320
321 case STATE_FINISHED:
322 if (ev.pulse)
323 break;
324
325 switch (data->wanted_bits) {
326 case MCIR2_KEYBOARD_NBITS:
327 scancode = data->body & 0xffffff;
328 dev_dbg(&dev->dev, "keyboard data 0x%08x\n",
329 data->body);
330 spin_lock(&data->keylock);
331 if (scancode) {
332 delay = nsecs_to_jiffies(dev->timeout) +
333 msecs_to_jiffies(100);
334 mod_timer(&data->rx_timeout, jiffies + delay);
335 } else {
336 del_timer(&data->rx_timeout);
337 }
338
339 ir_mce_kbd_process_keyboard_data(dev, scancode);
340 spin_unlock(&data->keylock);
341 lsc.rc_proto = RC_PROTO_MCIR2_KBD;
342 break;
343 case MCIR2_MOUSE_NBITS:
344 scancode = data->body & 0x1fffff;
345 dev_dbg(&dev->dev, "mouse data 0x%06x\n", scancode);
346
347 ir_mce_kbd_process_mouse_data(dev, scancode);
348 lsc.rc_proto = RC_PROTO_MCIR2_MSE;
349 break;
350 default:
351 dev_dbg(&dev->dev, "not keyboard or mouse data\n");
352 goto out;
353 }
354
355 lsc.scancode = scancode;
356 ir_lirc_scancode_event(dev, &lsc);
357 data->state = STATE_INACTIVE;
358 input_event(data->idev, EV_MSC, MSC_SCAN, scancode);
359 input_sync(data->idev);
360 return 0;
361 }
362
363out:
364 dev_dbg(&dev->dev, "failed at state %i (%uus %s)\n",
365 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
366 data->state = STATE_INACTIVE;
367 return -EINVAL;
368}
369
370static int ir_mce_kbd_register(struct rc_dev *dev)
371{
372 struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd;
373 struct input_dev *idev;
374 int i, ret;
375
376 idev = input_allocate_device();
377 if (!idev)
378 return -ENOMEM;
379
380 snprintf(mce_kbd->name, sizeof(mce_kbd->name),
381 "MCE IR Keyboard/Mouse (%s)", dev->driver_name);
382 strlcat(mce_kbd->phys, "/input0", sizeof(mce_kbd->phys));
383
384 idev->name = mce_kbd->name;
385 idev->phys = mce_kbd->phys;
386
387
388 set_bit(EV_KEY, idev->evbit);
389 set_bit(EV_REP, idev->evbit);
390 for (i = 0; i < sizeof(kbd_keycodes); i++)
391 set_bit(kbd_keycodes[i], idev->keybit);
392
393
394 set_bit(EV_REL, idev->evbit);
395 set_bit(REL_X, idev->relbit);
396 set_bit(REL_Y, idev->relbit);
397 set_bit(BTN_LEFT, idev->keybit);
398 set_bit(BTN_RIGHT, idev->keybit);
399
400
401 set_bit(EV_MSC, idev->evbit);
402 set_bit(MSC_SCAN, idev->mscbit);
403
404 timer_setup(&mce_kbd->rx_timeout, mce_kbd_rx_timeout, 0);
405 spin_lock_init(&mce_kbd->keylock);
406
407 input_set_drvdata(idev, mce_kbd);
408
409#if 0
410
411
412 idev->dev.parent = &dev->dev;
413#endif
414
415 ret = input_register_device(idev);
416 if (ret < 0) {
417 input_free_device(idev);
418 return -EIO;
419 }
420
421 mce_kbd->idev = idev;
422
423 return 0;
424}
425
426static int ir_mce_kbd_unregister(struct rc_dev *dev)
427{
428 struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd;
429 struct input_dev *idev = mce_kbd->idev;
430
431 del_timer_sync(&mce_kbd->rx_timeout);
432 input_unregister_device(idev);
433
434 return 0;
435}
436
437static const struct ir_raw_timings_manchester ir_mce_kbd_timings = {
438 .leader_pulse = MCIR2_PREFIX_PULSE,
439 .invert = 1,
440 .clock = MCIR2_UNIT,
441 .trailer_space = MCIR2_UNIT * 10,
442};
443
444
445
446
447
448
449
450
451
452
453
454
455
456static int ir_mce_kbd_encode(enum rc_proto protocol, u32 scancode,
457 struct ir_raw_event *events, unsigned int max)
458{
459 struct ir_raw_event *e = events;
460 int len, ret;
461 u64 raw;
462
463 if (protocol == RC_PROTO_MCIR2_KBD) {
464 raw = scancode |
465 ((u64)MCIR2_KEYBOARD_HEADER << MCIR2_KEYBOARD_NBITS);
466 len = MCIR2_KEYBOARD_NBITS + MCIR2_HEADER_NBITS;
467 } else {
468 raw = scancode |
469 ((u64)MCIR2_MOUSE_HEADER << MCIR2_MOUSE_NBITS);
470 len = MCIR2_MOUSE_NBITS + MCIR2_HEADER_NBITS;
471 }
472
473 ret = ir_raw_gen_manchester(&e, max, &ir_mce_kbd_timings, len, raw);
474 if (ret < 0)
475 return ret;
476
477 return e - events;
478}
479
480static struct ir_raw_handler mce_kbd_handler = {
481 .protocols = RC_PROTO_BIT_MCIR2_KBD | RC_PROTO_BIT_MCIR2_MSE,
482 .decode = ir_mce_kbd_decode,
483 .encode = ir_mce_kbd_encode,
484 .raw_register = ir_mce_kbd_register,
485 .raw_unregister = ir_mce_kbd_unregister,
486 .carrier = 36000,
487 .min_timeout = MCIR2_MAX_LEN + MCIR2_UNIT / 2,
488};
489
490static int __init ir_mce_kbd_decode_init(void)
491{
492 ir_raw_handler_register(&mce_kbd_handler);
493
494 printk(KERN_INFO "IR MCE Keyboard/mouse protocol handler initialized\n");
495 return 0;
496}
497
498static void __exit ir_mce_kbd_decode_exit(void)
499{
500 ir_raw_handler_unregister(&mce_kbd_handler);
501}
502
503module_init(ir_mce_kbd_decode_init);
504module_exit(ir_mce_kbd_decode_exit);
505
506MODULE_LICENSE("GPL");
507MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
508MODULE_DESCRIPTION("MCE Keyboard/mouse IR protocol decoder");
509