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 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->dev->input_dev, maskcode, 0); 133 } 134 135 for (i = 0; i < MCIR2_MASK_KEYS_START; i++) 136 input_report_key(raw->dev->input_dev, kbd_keycodes[i], 137 0); 138 139 input_sync(raw->dev->input_dev); 140 } 141 spin_unlock_irqrestore(&raw->mce_kbd.keylock, flags); 142 } 143 144 static enum mce_kbd_mode mce_kbd_mode(struct mce_kbd_dec *data) 145 { 146 switch (data->header & MCIR2_MODE_MASK) { 147 case MCIR2_KEYBOARD_HEADER: 148 return MCIR2_MODE_KEYBOARD; 149 case MCIR2_MOUSE_HEADER: 150 return MCIR2_MODE_MOUSE; 151 default: 152 return MCIR2_MODE_UNKNOWN; 153 } 154 } 155 156 static void ir_mce_kbd_process_keyboard_data(struct rc_dev *dev, u32 scancode) 157 { 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(dev->input_dev, maskcode, keystate); 174 } 175 176 if (keydata1) 177 input_report_key(dev->input_dev, kbd_keycodes[keydata1], 1); 178 if (keydata2) 179 input_report_key(dev->input_dev, kbd_keycodes[keydata2], 1); 180 181 if (!keydata1 && !keydata2) { 182 for (i = 0; i < MCIR2_MASK_KEYS_START; i++) 183 input_report_key(dev->input_dev, kbd_keycodes[i], 0); 184 } 185 } 186 187 static void ir_mce_kbd_process_mouse_data(struct rc_dev *dev, u32 scancode) 188 { 189 /* raw mouse coordinates */ 190 u8 xdata = (scancode >> 7) & 0x7f; 191 u8 ydata = (scancode >> 14) & 0x7f; 192 int x, y; 193 /* mouse buttons */ 194 bool right = scancode & 0x40; 195 bool left = scancode & 0x20; 196 197 if (xdata & 0x40) 198 x = -((~xdata & 0x7f) + 1); 199 else 200 x = xdata; 201 202 if (ydata & 0x40) 203 y = -((~ydata & 0x7f) + 1); 204 else 205 y = ydata; 206 207 dev_dbg(&dev->dev, "mouse: x = %d, y = %d, btns = %s%s\n", 208 x, y, left ? "L" : "", right ? "R" : ""); 209 210 input_report_rel(dev->input_dev, REL_X, x); 211 input_report_rel(dev->input_dev, REL_Y, y); 212 213 input_report_key(dev->input_dev, BTN_LEFT, left); 214 input_report_key(dev->input_dev, BTN_RIGHT, right); 215 } 216 217 /** 218 * ir_mce_kbd_decode() - Decode one mce_kbd pulse or space 219 * @dev: the struct rc_dev descriptor of the device 220 * @ev: the struct ir_raw_event descriptor of the pulse/space 221 * 222 * This function returns -EINVAL if the pulse violates the state machine 223 */ 224 static int ir_mce_kbd_decode(struct rc_dev *dev, struct ir_raw_event ev) 225 { 226 struct mce_kbd_dec *data = &dev->raw->mce_kbd; 227 u32 scancode; 228 unsigned long delay; 229 struct lirc_scancode lsc = {}; 230 231 if (!is_timing_event(ev)) { 232 if (ev.reset) 233 data->state = STATE_INACTIVE; 234 return 0; 235 } 236 237 if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2)) 238 goto out; 239 240 again: 241 dev_dbg(&dev->dev, "started at state %i (%uus %s)\n", 242 data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 243 244 if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2)) 245 return 0; 246 247 switch (data->state) { 248 249 case STATE_INACTIVE: 250 if (!ev.pulse) 251 break; 252 253 /* Note: larger margin on first pulse since each MCIR2_UNIT 254 is quite short and some hardware takes some time to 255 adjust to the signal */ 256 if (!eq_margin(ev.duration, MCIR2_PREFIX_PULSE, MCIR2_UNIT)) 257 break; 258 259 data->state = STATE_HEADER_BIT_START; 260 data->count = 0; 261 data->header = 0; 262 return 0; 263 264 case STATE_HEADER_BIT_START: 265 if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2)) 266 break; 267 268 data->header <<= 1; 269 if (ev.pulse) 270 data->header |= 1; 271 data->count++; 272 data->state = STATE_HEADER_BIT_END; 273 return 0; 274 275 case STATE_HEADER_BIT_END: 276 decrease_duration(&ev, MCIR2_BIT_END); 277 278 if (data->count != MCIR2_HEADER_NBITS) { 279 data->state = STATE_HEADER_BIT_START; 280 goto again; 281 } 282 283 switch (mce_kbd_mode(data)) { 284 case MCIR2_MODE_KEYBOARD: 285 data->wanted_bits = MCIR2_KEYBOARD_NBITS; 286 break; 287 case MCIR2_MODE_MOUSE: 288 data->wanted_bits = MCIR2_MOUSE_NBITS; 289 break; 290 default: 291 dev_dbg(&dev->dev, "not keyboard or mouse data\n"); 292 goto out; 293 } 294 295 data->count = 0; 296 data->body = 0; 297 data->state = STATE_BODY_BIT_START; 298 goto again; 299 300 case STATE_BODY_BIT_START: 301 if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2)) 302 break; 303 304 data->body <<= 1; 305 if (ev.pulse) 306 data->body |= 1; 307 data->count++; 308 data->state = STATE_BODY_BIT_END; 309 return 0; 310 311 case STATE_BODY_BIT_END: 312 if (data->count == data->wanted_bits) 313 data->state = STATE_FINISHED; 314 else 315 data->state = STATE_BODY_BIT_START; 316 317 decrease_duration(&ev, MCIR2_BIT_END); 318 goto again; 319 320 case STATE_FINISHED: 321 if (ev.pulse) 322 break; 323 324 switch (data->wanted_bits) { 325 case MCIR2_KEYBOARD_NBITS: 326 scancode = data->body & 0xffffff; 327 dev_dbg(&dev->dev, "keyboard data 0x%08x\n", 328 data->body); 329 spin_lock(&data->keylock); 330 if (scancode) { 331 delay = nsecs_to_jiffies(dev->timeout) + 332 msecs_to_jiffies(100); 333 mod_timer(&data->rx_timeout, jiffies + delay); 334 } else { 335 del_timer(&data->rx_timeout); 336 } 337 /* Pass data to keyboard buffer parser */ 338 ir_mce_kbd_process_keyboard_data(dev, scancode); 339 spin_unlock(&data->keylock); 340 lsc.rc_proto = RC_PROTO_MCIR2_KBD; 341 break; 342 case MCIR2_MOUSE_NBITS: 343 scancode = data->body & 0x1fffff; 344 dev_dbg(&dev->dev, "mouse data 0x%06x\n", scancode); 345 /* Pass data to mouse buffer parser */ 346 ir_mce_kbd_process_mouse_data(dev, scancode); 347 lsc.rc_proto = RC_PROTO_MCIR2_MSE; 348 break; 349 default: 350 dev_dbg(&dev->dev, "not keyboard or mouse data\n"); 351 goto out; 352 } 353 354 lsc.scancode = scancode; 355 ir_lirc_scancode_event(dev, &lsc); 356 data->state = STATE_INACTIVE; 357 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode); 358 input_sync(dev->input_dev); 359 return 0; 360 } 361 362 out: 363 dev_dbg(&dev->dev, "failed at state %i (%uus %s)\n", 364 data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 365 data->state = STATE_INACTIVE; 366 return -EINVAL; 367 } 368 369 static int ir_mce_kbd_register(struct rc_dev *dev) 370 { 371 struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd; 372 373 timer_setup(&mce_kbd->rx_timeout, mce_kbd_rx_timeout, 0); 374 spin_lock_init(&mce_kbd->keylock); 375 376 return 0; 377 } 378 379 static int ir_mce_kbd_unregister(struct rc_dev *dev) 380 { 381 struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd; 382 383 del_timer_sync(&mce_kbd->rx_timeout); 384 385 return 0; 386 } 387 388 static const struct ir_raw_timings_manchester ir_mce_kbd_timings = { 389 .leader_pulse = MCIR2_PREFIX_PULSE, 390 .invert = 1, 391 .clock = MCIR2_UNIT, 392 .trailer_space = MCIR2_UNIT * 10, 393 }; 394 395 /** 396 * ir_mce_kbd_encode() - Encode a scancode as a stream of raw events 397 * 398 * @protocol: protocol to encode 399 * @scancode: scancode to encode 400 * @events: array of raw ir events to write into 401 * @max: maximum size of @events 402 * 403 * Returns: The number of events written. 404 * -ENOBUFS if there isn't enough space in the array to fit the 405 * encoding. In this case all @max events will have been written. 406 */ 407 static int ir_mce_kbd_encode(enum rc_proto protocol, u32 scancode, 408 struct ir_raw_event *events, unsigned int max) 409 { 410 struct ir_raw_event *e = events; 411 int len, ret; 412 u64 raw; 413 414 if (protocol == RC_PROTO_MCIR2_KBD) { 415 raw = scancode | 416 ((u64)MCIR2_KEYBOARD_HEADER << MCIR2_KEYBOARD_NBITS); 417 len = MCIR2_KEYBOARD_NBITS + MCIR2_HEADER_NBITS; 418 } else { 419 raw = scancode | 420 ((u64)MCIR2_MOUSE_HEADER << MCIR2_MOUSE_NBITS); 421 len = MCIR2_MOUSE_NBITS + MCIR2_HEADER_NBITS; 422 } 423 424 ret = ir_raw_gen_manchester(&e, max, &ir_mce_kbd_timings, len, raw); 425 if (ret < 0) 426 return ret; 427 428 return e - events; 429 } 430 431 static struct ir_raw_handler mce_kbd_handler = { 432 .protocols = RC_PROTO_BIT_MCIR2_KBD | RC_PROTO_BIT_MCIR2_MSE, 433 .decode = ir_mce_kbd_decode, 434 .encode = ir_mce_kbd_encode, 435 .raw_register = ir_mce_kbd_register, 436 .raw_unregister = ir_mce_kbd_unregister, 437 .carrier = 36000, 438 .min_timeout = MCIR2_MAX_LEN + MCIR2_UNIT / 2, 439 }; 440 441 static int __init ir_mce_kbd_decode_init(void) 442 { 443 ir_raw_handler_register(&mce_kbd_handler); 444 445 printk(KERN_INFO "IR MCE Keyboard/mouse protocol handler initialized\n"); 446 return 0; 447 } 448 449 static void __exit ir_mce_kbd_decode_exit(void) 450 { 451 ir_raw_handler_unregister(&mce_kbd_handler); 452 } 453 454 module_init(ir_mce_kbd_decode_init); 455 module_exit(ir_mce_kbd_decode_exit); 456 457 MODULE_LICENSE("GPL"); 458 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>"); 459 MODULE_DESCRIPTION("MCE Keyboard/mouse IR protocol decoder"); 460