ir-mce_kbd-decoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 (!(dev->enabled_protocols & RC_BIT_MCE_KBD))
  197. return 0;
  198. if (!is_timing_event(ev)) {
  199. if (ev.reset)
  200. data->state = STATE_INACTIVE;
  201. return 0;
  202. }
  203. if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2))
  204. goto out;
  205. again:
  206. IR_dprintk(2, "started at state %i (%uus %s)\n",
  207. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  208. if (!geq_margin(ev.duration, MCIR2_UNIT, MCIR2_UNIT / 2))
  209. return 0;
  210. switch (data->state) {
  211. case STATE_INACTIVE:
  212. if (!ev.pulse)
  213. break;
  214. /* Note: larger margin on first pulse since each MCIR2_UNIT
  215. is quite short and some hardware takes some time to
  216. adjust to the signal */
  217. if (!eq_margin(ev.duration, MCIR2_PREFIX_PULSE, MCIR2_UNIT))
  218. break;
  219. data->state = STATE_HEADER_BIT_START;
  220. data->count = 0;
  221. data->header = 0;
  222. return 0;
  223. case STATE_HEADER_BIT_START:
  224. if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2))
  225. break;
  226. data->header <<= 1;
  227. if (ev.pulse)
  228. data->header |= 1;
  229. data->count++;
  230. data->state = STATE_HEADER_BIT_END;
  231. return 0;
  232. case STATE_HEADER_BIT_END:
  233. if (!is_transition(&ev, &dev->raw->prev_ev))
  234. break;
  235. decrease_duration(&ev, MCIR2_BIT_END);
  236. if (data->count != MCIR2_HEADER_NBITS) {
  237. data->state = STATE_HEADER_BIT_START;
  238. goto again;
  239. }
  240. switch (mce_kbd_mode(data)) {
  241. case MCIR2_MODE_KEYBOARD:
  242. data->wanted_bits = MCIR2_KEYBOARD_NBITS;
  243. break;
  244. case MCIR2_MODE_MOUSE:
  245. data->wanted_bits = MCIR2_MOUSE_NBITS;
  246. break;
  247. default:
  248. IR_dprintk(1, "not keyboard or mouse data\n");
  249. goto out;
  250. }
  251. data->count = 0;
  252. data->body = 0;
  253. data->state = STATE_BODY_BIT_START;
  254. goto again;
  255. case STATE_BODY_BIT_START:
  256. if (geq_margin(ev.duration, MCIR2_MAX_LEN, MCIR2_UNIT / 2))
  257. break;
  258. data->body <<= 1;
  259. if (ev.pulse)
  260. data->body |= 1;
  261. data->count++;
  262. data->state = STATE_BODY_BIT_END;
  263. return 0;
  264. case STATE_BODY_BIT_END:
  265. if (!is_transition(&ev, &dev->raw->prev_ev))
  266. break;
  267. if (data->count == data->wanted_bits)
  268. data->state = STATE_FINISHED;
  269. else
  270. data->state = STATE_BODY_BIT_START;
  271. decrease_duration(&ev, MCIR2_BIT_END);
  272. goto again;
  273. case STATE_FINISHED:
  274. if (ev.pulse)
  275. break;
  276. switch (data->wanted_bits) {
  277. case MCIR2_KEYBOARD_NBITS:
  278. scancode = data->body & 0xffff;
  279. IR_dprintk(1, "keyboard data 0x%08x\n", data->body);
  280. if (dev->timeout)
  281. delay = usecs_to_jiffies(dev->timeout / 1000);
  282. else
  283. delay = msecs_to_jiffies(100);
  284. mod_timer(&data->rx_timeout, jiffies + delay);
  285. /* Pass data to keyboard buffer parser */
  286. ir_mce_kbd_process_keyboard_data(data->idev, scancode);
  287. break;
  288. case MCIR2_MOUSE_NBITS:
  289. scancode = data->body & 0x1fffff;
  290. IR_dprintk(1, "mouse data 0x%06x\n", scancode);
  291. /* Pass data to mouse buffer parser */
  292. ir_mce_kbd_process_mouse_data(data->idev, scancode);
  293. break;
  294. default:
  295. IR_dprintk(1, "not keyboard or mouse data\n");
  296. goto out;
  297. }
  298. data->state = STATE_INACTIVE;
  299. input_sync(data->idev);
  300. return 0;
  301. }
  302. out:
  303. IR_dprintk(1, "failed at state %i (%uus %s)\n",
  304. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  305. data->state = STATE_INACTIVE;
  306. input_sync(data->idev);
  307. return -EINVAL;
  308. }
  309. static int ir_mce_kbd_register(struct rc_dev *dev)
  310. {
  311. struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd;
  312. struct input_dev *idev;
  313. int i, ret;
  314. idev = input_allocate_device();
  315. if (!idev)
  316. return -ENOMEM;
  317. snprintf(mce_kbd->name, sizeof(mce_kbd->name),
  318. "MCE IR Keyboard/Mouse (%s)", dev->driver_name);
  319. strlcat(mce_kbd->phys, "/input0", sizeof(mce_kbd->phys));
  320. idev->name = mce_kbd->name;
  321. idev->phys = mce_kbd->phys;
  322. /* Keyboard bits */
  323. set_bit(EV_KEY, idev->evbit);
  324. set_bit(EV_REP, idev->evbit);
  325. for (i = 0; i < sizeof(kbd_keycodes); i++)
  326. set_bit(kbd_keycodes[i], idev->keybit);
  327. /* Mouse bits */
  328. set_bit(EV_REL, idev->evbit);
  329. set_bit(REL_X, idev->relbit);
  330. set_bit(REL_Y, idev->relbit);
  331. set_bit(BTN_LEFT, idev->keybit);
  332. set_bit(BTN_RIGHT, idev->keybit);
  333. /* Report scancodes too */
  334. set_bit(EV_MSC, idev->evbit);
  335. set_bit(MSC_SCAN, idev->mscbit);
  336. setup_timer(&mce_kbd->rx_timeout, mce_kbd_rx_timeout,
  337. (unsigned long)mce_kbd);
  338. input_set_drvdata(idev, mce_kbd);
  339. #if 0
  340. /* Adding this reference means two input devices are associated with
  341. * this rc-core device, which ir-keytable doesn't cope with yet */
  342. idev->dev.parent = &dev->dev;
  343. #endif
  344. ret = input_register_device(idev);
  345. if (ret < 0) {
  346. input_free_device(idev);
  347. return -EIO;
  348. }
  349. mce_kbd->idev = idev;
  350. return 0;
  351. }
  352. static int ir_mce_kbd_unregister(struct rc_dev *dev)
  353. {
  354. struct mce_kbd_dec *mce_kbd = &dev->raw->mce_kbd;
  355. struct input_dev *idev = mce_kbd->idev;
  356. del_timer_sync(&mce_kbd->rx_timeout);
  357. input_unregister_device(idev);
  358. return 0;
  359. }
  360. static struct ir_raw_handler mce_kbd_handler = {
  361. .protocols = RC_BIT_MCE_KBD,
  362. .decode = ir_mce_kbd_decode,
  363. .raw_register = ir_mce_kbd_register,
  364. .raw_unregister = ir_mce_kbd_unregister,
  365. };
  366. static int __init ir_mce_kbd_decode_init(void)
  367. {
  368. ir_raw_handler_register(&mce_kbd_handler);
  369. printk(KERN_INFO "IR MCE Keyboard/mouse protocol handler initialized\n");
  370. return 0;
  371. }
  372. static void __exit ir_mce_kbd_decode_exit(void)
  373. {
  374. ir_raw_handler_unregister(&mce_kbd_handler);
  375. }
  376. module_init(ir_mce_kbd_decode_init);
  377. module_exit(ir_mce_kbd_decode_exit);
  378. MODULE_LICENSE("GPL");
  379. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  380. MODULE_DESCRIPTION("MCE Keyboard/mouse IR protocol decoder");