ir-mce_kbd-decoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. #include "rc-core-priv.h"
  19. /*
  20. * This decoder currently supports:
  21. * - MCIR-2 29-bit IR signals used for mouse movement and buttons
  22. * - MCIR-2 32-bit IR signals used for standard keyboard keys
  23. *
  24. * The media keys on the keyboard send RC-6 signals that are inditinguishable
  25. * from the keys of the same name on the stock MCE remote, and will be handled
  26. * by the standard RC-6 decoder, and be made available to the system via the
  27. * input device for the remote, rather than the keyboard/mouse one.
  28. */
  29. #define MCIR2_UNIT 333333 /* ns */
  30. #define MCIR2_HEADER_NBITS 5
  31. #define MCIR2_MOUSE_NBITS 29
  32. #define MCIR2_KEYBOARD_NBITS 32
  33. #define MCIR2_PREFIX_PULSE (8 * MCIR2_UNIT)
  34. #define MCIR2_PREFIX_SPACE (1 * MCIR2_UNIT)
  35. #define MCIR2_MAX_LEN (3 * MCIR2_UNIT)
  36. #define MCIR2_BIT_START (1 * MCIR2_UNIT)
  37. #define MCIR2_BIT_END (1 * MCIR2_UNIT)
  38. #define MCIR2_BIT_0 (1 * MCIR2_UNIT)
  39. #define MCIR2_BIT_SET (2 * MCIR2_UNIT)
  40. #define MCIR2_MODE_MASK 0xf /* for the header bits */
  41. #define MCIR2_KEYBOARD_HEADER 0x4
  42. #define MCIR2_MOUSE_HEADER 0x1
  43. #define MCIR2_MASK_KEYS_START 0xe0
  44. enum mce_kbd_mode {
  45. MCIR2_MODE_KEYBOARD,
  46. MCIR2_MODE_MOUSE,
  47. MCIR2_MODE_UNKNOWN,
  48. };
  49. enum mce_kbd_state {
  50. STATE_INACTIVE,
  51. STATE_HEADER_BIT_START,
  52. STATE_HEADER_BIT_END,
  53. STATE_BODY_BIT_START,
  54. STATE_BODY_BIT_END,
  55. STATE_FINISHED,
  56. };
  57. static unsigned char kbd_keycodes[256] = {
  58. KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_A,
  59. KEY_B, KEY_C, KEY_D, KEY_E, KEY_F,
  60. KEY_G, KEY_H, KEY_I, KEY_J, KEY_K,
  61. KEY_L, KEY_M, KEY_N, KEY_O, KEY_P,
  62. KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U,
  63. KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z,
  64. KEY_1, KEY_2, KEY_3, KEY_4, KEY_5,
  65. KEY_6, KEY_7, KEY_8, KEY_9, KEY_0,
  66. KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB, KEY_SPACE,
  67. KEY_MINUS, KEY_EQUAL, KEY_LEFTBRACE, KEY_RIGHTBRACE, KEY_BACKSLASH,
  68. KEY_RESERVED, KEY_SEMICOLON, KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA,
  69. KEY_DOT, KEY_SLASH, KEY_CAPSLOCK, KEY_F1, KEY_F2,
  70. KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7,
  71. KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12,
  72. KEY_SYSRQ, KEY_SCROLLLOCK, KEY_PAUSE, KEY_INSERT, KEY_HOME,
  73. KEY_PAGEUP, KEY_DELETE, KEY_END, KEY_PAGEDOWN, KEY_RIGHT,
  74. KEY_LEFT, KEY_DOWN, KEY_UP, KEY_NUMLOCK, KEY_KPSLASH,
  75. KEY_KPASTERISK, KEY_KPMINUS, KEY_KPPLUS, KEY_KPENTER, KEY_KP1,
  76. KEY_KP2, KEY_KP3, KEY_KP4, KEY_KP5, KEY_KP6,
  77. KEY_KP7, KEY_KP8, KEY_KP9, KEY_KP0, KEY_KPDOT,
  78. KEY_102ND, KEY_COMPOSE, KEY_POWER, KEY_KPEQUAL, KEY_F13,
  79. KEY_F14, KEY_F15, KEY_F16, KEY_F17, KEY_F18,
  80. KEY_F19, KEY_F20, KEY_F21, KEY_F22, KEY_F23,
  81. KEY_F24, KEY_OPEN, KEY_HELP, KEY_PROPS, KEY_FRONT,
  82. KEY_STOP, KEY_AGAIN, KEY_UNDO, KEY_CUT, KEY_COPY,
  83. KEY_PASTE, KEY_FIND, KEY_MUTE, KEY_VOLUMEUP, KEY_VOLUMEDOWN,
  84. KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_KPCOMMA, KEY_RESERVED,
  85. KEY_RO, KEY_KATAKANAHIRAGANA, KEY_YEN, KEY_HENKAN, KEY_MUHENKAN,
  86. KEY_KPJPCOMMA, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_HANGUEL,
  87. KEY_HANJA, KEY_KATAKANA, KEY_HIRAGANA, KEY_ZENKAKUHANKAKU, 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_RESERVED,
  101. KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
  102. KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_LEFTCTRL,
  103. KEY_LEFTSHIFT, KEY_LEFTALT, KEY_LEFTMETA, KEY_RIGHTCTRL, KEY_RIGHTSHIFT,
  104. KEY_RIGHTALT, KEY_RIGHTMETA, KEY_PLAYPAUSE, KEY_STOPCD, KEY_PREVIOUSSONG,
  105. KEY_NEXTSONG, KEY_EJECTCD, KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_MUTE,
  106. KEY_WWW, KEY_BACK, KEY_FORWARD, KEY_STOP, KEY_FIND,
  107. KEY_SCROLLUP, KEY_SCROLLDOWN, KEY_EDIT, KEY_SLEEP, KEY_COFFEE,
  108. KEY_REFRESH, KEY_CALC, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
  109. KEY_RESERVED
  110. };
  111. static void mce_kbd_rx_timeout(unsigned long data)
  112. {
  113. struct mce_kbd_dec *mce_kbd = (struct mce_kbd_dec *)data;
  114. int i;
  115. unsigned char maskcode;
  116. IR_dprintk(2, "timer callback clearing all keys\n");
  117. for (i = 0; i < 7; i++) {
  118. maskcode = kbd_keycodes[MCIR2_MASK_KEYS_START + i];
  119. input_report_key(mce_kbd->idev, maskcode, 0);
  120. }
  121. for (i = 0; i < MCIR2_MASK_KEYS_START; i++)
  122. input_report_key(mce_kbd->idev, kbd_keycodes[i], 0);
  123. }
  124. static enum mce_kbd_mode mce_kbd_mode(struct mce_kbd_dec *data)
  125. {
  126. switch (data->header & MCIR2_MODE_MASK) {
  127. case MCIR2_KEYBOARD_HEADER:
  128. return MCIR2_MODE_KEYBOARD;
  129. case MCIR2_MOUSE_HEADER:
  130. return MCIR2_MODE_MOUSE;
  131. default:
  132. return MCIR2_MODE_UNKNOWN;
  133. }
  134. }
  135. static void ir_mce_kbd_process_keyboard_data(struct input_dev *idev,
  136. u32 scancode)
  137. {
  138. u8 keydata = (scancode >> 8) & 0xff;
  139. u8 shiftmask = scancode & 0xff;
  140. unsigned char keycode, maskcode;
  141. int i, keystate;
  142. IR_dprintk(1, "keyboard: keydata = 0x%02x, shiftmask = 0x%02x\n",
  143. keydata, shiftmask);
  144. for (i = 0; i < 7; i++) {
  145. maskcode = kbd_keycodes[MCIR2_MASK_KEYS_START + i];
  146. if (shiftmask & (1 << i))
  147. keystate = 1;
  148. else
  149. keystate = 0;
  150. input_report_key(idev, maskcode, keystate);
  151. }
  152. if (keydata) {
  153. keycode = kbd_keycodes[keydata];
  154. input_report_key(idev, keycode, 1);
  155. } else {
  156. for (i = 0; i < MCIR2_MASK_KEYS_START; i++)
  157. input_report_key(idev, kbd_keycodes[i], 0);
  158. }
  159. }
  160. static void ir_mce_kbd_process_mouse_data(struct input_dev *idev, u32 scancode)
  161. {
  162. /* raw mouse coordinates */
  163. u8 xdata = (scancode >> 7) & 0x7f;
  164. u8 ydata = (scancode >> 14) & 0x7f;
  165. int x, y;
  166. /* mouse buttons */
  167. bool right = scancode & 0x40;
  168. bool left = scancode & 0x20;
  169. if (xdata & 0x40)
  170. x = -((~xdata & 0x7f) + 1);
  171. else
  172. x = xdata;
  173. if (ydata & 0x40)
  174. y = -((~ydata & 0x7f) + 1);
  175. else
  176. y = ydata;
  177. IR_dprintk(1, "mouse: x = %d, y = %d, btns = %s%s\n",
  178. x, y, left ? "L" : "", right ? "R" : "");
  179. input_report_rel(idev, REL_X, x);
  180. input_report_rel(idev, REL_Y, y);
  181. input_report_key(idev, BTN_LEFT, left);
  182. input_report_key(idev, BTN_RIGHT, right);
  183. }
  184. /**
  185. * ir_mce_kbd_decode() - Decode one mce_kbd pulse or space
  186. * @dev: the struct rc_dev descriptor of the device
  187. * @ev: the struct ir_raw_event descriptor of the pulse/space
  188. *
  189. * This function returns -EINVAL if the pulse violates the state machine
  190. */
  191. static int ir_mce_kbd_decode(struct rc_dev *dev, struct ir_raw_event ev)
  192. {
  193. struct mce_kbd_dec *data = &dev->raw->mce_kbd;
  194. u32 scancode;
  195. unsigned long delay;
  196. if (!is_timing_event(ev)) {
  197. if (ev.reset)
  198. data->state = STATE_INACTIVE;
  199. return 0;
  200. }
  201. if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2))
  202. goto out;
  203. again:
  204. IR_dprintk(2, "started at state %i (%uus %s)\n",
  205. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  206. if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2))
  207. return 0;
  208. switch (data->state) {
  209. case STATE_INACTIVE:
  210. if (!ev.pulse)
  211. break;
  212. /* Note: larger margin on first pulse since each MCIR2_UNIT
  213. is quite short and some hardware takes some time to
  214. adjust to the signal */
  215. if (!eq_margin(ev.duration, MCIR2_PREFIX_PULSE, MCIR2_UNIT))
  216. break;
  217. data->state = STATE_HEADER_BIT_START;
  218. data->count = 0;
  219. data->header = 0;
  220. return 0;
  221. case STATE_HEADER_BIT_START:
  222. if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2))
  223. break;
  224. data->header <<= 1;
  225. if (ev.pulse)
  226. data->header |= 1;
  227. data->count++;
  228. data->state = STATE_HEADER_BIT_END;
  229. return 0;
  230. case STATE_HEADER_BIT_END:
  231. if (!is_transition(&ev, &dev->raw->prev_ev))
  232. break;
  233. decrease_duration(&ev, MCIR2_BIT_END);
  234. if (data->count != MCIR2_HEADER_NBITS) {
  235. data->state = STATE_HEADER_BIT_START;
  236. goto again;
  237. }
  238. switch (mce_kbd_mode(data)) {
  239. case MCIR2_MODE_KEYBOARD:
  240. data->wanted_bits = MCIR2_KEYBOARD_NBITS;
  241. break;
  242. case MCIR2_MODE_MOUSE:
  243. data->wanted_bits = MCIR2_MOUSE_NBITS;
  244. break;
  245. default:
  246. IR_dprintk(1, "not keyboard or mouse data\n");
  247. goto out;
  248. }
  249. data->count = 0;
  250. data->body = 0;
  251. data->state = STATE_BODY_BIT_START;
  252. goto again;
  253. case STATE_BODY_BIT_START:
  254. if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2))
  255. break;
  256. data->body <<= 1;
  257. if (ev.pulse)
  258. data->body |= 1;
  259. data->count++;
  260. data->state = STATE_BODY_BIT_END;
  261. return 0;
  262. case STATE_BODY_BIT_END:
  263. if (!is_transition(&ev, &dev->raw->prev_ev))
  264. break;
  265. if (data->count == data->wanted_bits)
  266. data->state = STATE_FINISHED;
  267. else
  268. data->state = STATE_BODY_BIT_START;
  269. decrease_duration(&ev, MCIR2_BIT_END);
  270. goto again;
  271. case STATE_FINISHED:
  272. if (ev.pulse)
  273. break;
  274. switch (data->wanted_bits) {
  275. case MCIR2_KEYBOARD_NBITS:
  276. scancode = data->body & 0xffff;
  277. IR_dprintk(1, "keyboard data 0x%08x\n", data->body);
  278. if (dev->timeout)
  279. delay = usecs_to_jiffies(dev->timeout / 1000);
  280. else
  281. delay = msecs_to_jiffies(100);
  282. mod_timer(&data->rx_timeout, jiffies + delay);
  283. /* Pass data to keyboard buffer parser */
  284. ir_mce_kbd_process_keyboard_data(data->idev, scancode);
  285. break;
  286. case MCIR2_MOUSE_NBITS:
  287. scancode = data->body & 0x1fffff;
  288. IR_dprintk(1, "mouse data 0x%06x\n", scancode);
  289. /* Pass data to mouse buffer parser */
  290. ir_mce_kbd_process_mouse_data(data->idev, scancode);
  291. break;
  292. default:
  293. IR_dprintk(1, "not keyboard or mouse data\n");
  294. goto out;
  295. }
  296. data->state = STATE_INACTIVE;
  297. input_sync(data->idev);
  298. return 0;
  299. }
  300. out:
  301. IR_dprintk(1, "failed at state %i (%uus %s)\n",
  302. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  303. data->state = STATE_INACTIVE;
  304. input_sync(data->idev);
  305. return -EINVAL;
  306. }
  307. static int ir_mce_kbd_register(struct rc_dev *dev)
  308. {
  309. struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd;
  310. struct input_dev *idev;
  311. int i, ret;
  312. idev = input_allocate_device();
  313. if (!idev)
  314. return -ENOMEM;
  315. snprintf(mce_kbd->name, sizeof(mce_kbd->name),
  316. "MCE IR Keyboard/Mouse (%s)", dev->driver_name);
  317. strlcat(mce_kbd->phys, "/input0", sizeof(mce_kbd->phys));
  318. idev->name = mce_kbd->name;
  319. idev->phys = mce_kbd->phys;
  320. /* Keyboard bits */
  321. set_bit(EV_KEY, idev->evbit);
  322. set_bit(EV_REP, idev->evbit);
  323. for (i = 0; i < sizeof(kbd_keycodes); i++)
  324. set_bit(kbd_keycodes[i], idev->keybit);
  325. /* Mouse bits */
  326. set_bit(EV_REL, idev->evbit);
  327. set_bit(REL_X, idev->relbit);
  328. set_bit(REL_Y, idev->relbit);
  329. set_bit(BTN_LEFT, idev->keybit);
  330. set_bit(BTN_RIGHT, idev->keybit);
  331. /* Report scancodes too */
  332. set_bit(EV_MSC, idev->evbit);
  333. set_bit(MSC_SCAN, idev->mscbit);
  334. setup_timer(&mce_kbd->rx_timeout, mce_kbd_rx_timeout,
  335. (unsigned long)mce_kbd);
  336. input_set_drvdata(idev, mce_kbd);
  337. #if 0
  338. /* Adding this reference means two input devices are associated with
  339. * this rc-core device, which ir-keytable doesn't cope with yet */
  340. idev->dev.parent = &dev->dev;
  341. #endif
  342. ret = input_register_device(idev);
  343. if (ret < 0) {
  344. input_free_device(idev);
  345. return -EIO;
  346. }
  347. mce_kbd->idev = idev;
  348. return 0;
  349. }
  350. static int ir_mce_kbd_unregister(struct rc_dev *dev)
  351. {
  352. struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd;
  353. struct input_dev *idev = mce_kbd->idev;
  354. del_timer_sync(&mce_kbd->rx_timeout);
  355. input_unregister_device(idev);
  356. return 0;
  357. }
  358. static struct ir_raw_handler mce_kbd_handler = {
  359. .protocols = RC_BIT_MCE_KBD,
  360. .decode = ir_mce_kbd_decode,
  361. .raw_register = ir_mce_kbd_register,
  362. .raw_unregister = ir_mce_kbd_unregister,
  363. };
  364. static int __init ir_mce_kbd_decode_init(void)
  365. {
  366. ir_raw_handler_register(&mce_kbd_handler);
  367. printk(KERN_INFO "IR MCE Keyboard/mouse protocol handler initialized\n");
  368. return 0;
  369. }
  370. static void __exit ir_mce_kbd_decode_exit(void)
  371. {
  372. ir_raw_handler_unregister(&mce_kbd_handler);
  373. }
  374. module_init(ir_mce_kbd_decode_init);
  375. module_exit(ir_mce_kbd_decode_exit);
  376. MODULE_LICENSE("GPL");
  377. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  378. MODULE_DESCRIPTION("MCE Keyboard/mouse IR protocol decoder");