hid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * QEMU HID devices
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. * Copyright (c) 2007 OpenMoko, Inc. (andrew@openedhand.com)
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "hw/hw.h"
  26. #include "ui/console.h"
  27. #include "qemu/timer.h"
  28. #include "hw/input/hid.h"
  29. #define HID_USAGE_ERROR_ROLLOVER 0x01
  30. #define HID_USAGE_POSTFAIL 0x02
  31. #define HID_USAGE_ERROR_UNDEFINED 0x03
  32. /* Indices are QEMU keycodes, values are from HID Usage Table. Indices
  33. * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d. */
  34. static const uint8_t hid_usage_keys[0x100] = {
  35. 0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  36. 0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
  37. 0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
  38. 0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
  39. 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
  40. 0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
  41. 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
  42. 0xe2, 0x2c, 0x32, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
  43. 0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
  44. 0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
  45. 0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x00, 0x44,
  46. 0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  47. 0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
  58. 0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x4a,
  60. 0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
  61. 0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. };
  68. bool hid_has_events(HIDState *hs)
  69. {
  70. return hs->n > 0 || hs->idle_pending;
  71. }
  72. static void hid_idle_timer(void *opaque)
  73. {
  74. HIDState *hs = opaque;
  75. hs->idle_pending = true;
  76. hs->event(hs);
  77. }
  78. static void hid_del_idle_timer(HIDState *hs)
  79. {
  80. if (hs->idle_timer) {
  81. timer_del(hs->idle_timer);
  82. timer_free(hs->idle_timer);
  83. hs->idle_timer = NULL;
  84. }
  85. }
  86. void hid_set_next_idle(HIDState *hs)
  87. {
  88. if (hs->idle) {
  89. uint64_t expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  90. get_ticks_per_sec() * hs->idle * 4 / 1000;
  91. if (!hs->idle_timer) {
  92. hs->idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hid_idle_timer, hs);
  93. }
  94. timer_mod_ns(hs->idle_timer, expire_time);
  95. } else {
  96. hid_del_idle_timer(hs);
  97. }
  98. }
  99. static void hid_pointer_event_clear(HIDPointerEvent *e, int buttons)
  100. {
  101. e->xdx = e->ydy = e->dz = 0;
  102. e->buttons_state = buttons;
  103. }
  104. static void hid_pointer_event_combine(HIDPointerEvent *e, int xyrel,
  105. int x1, int y1, int z1) {
  106. if (xyrel) {
  107. e->xdx += x1;
  108. e->ydy += y1;
  109. } else {
  110. e->xdx = x1;
  111. e->ydy = y1;
  112. /* Windows drivers do not like the 0/0 position and ignore such
  113. * events. */
  114. if (!(x1 | y1)) {
  115. e->xdx = 1;
  116. }
  117. }
  118. e->dz += z1;
  119. }
  120. static void hid_pointer_event(void *opaque,
  121. int x1, int y1, int z1, int buttons_state)
  122. {
  123. HIDState *hs = opaque;
  124. unsigned use_slot = (hs->head + hs->n - 1) & QUEUE_MASK;
  125. unsigned previous_slot = (use_slot - 1) & QUEUE_MASK;
  126. /* We combine events where feasible to keep the queue small. We shouldn't
  127. * combine anything with the first event of a particular button state, as
  128. * that would change the location of the button state change. When the
  129. * queue is empty, a second event is needed because we don't know if
  130. * the first event changed the button state. */
  131. if (hs->n == QUEUE_LENGTH) {
  132. /* Queue full. Discard old button state, combine motion normally. */
  133. hs->ptr.queue[use_slot].buttons_state = buttons_state;
  134. } else if (hs->n < 2 ||
  135. hs->ptr.queue[use_slot].buttons_state != buttons_state ||
  136. hs->ptr.queue[previous_slot].buttons_state !=
  137. hs->ptr.queue[use_slot].buttons_state) {
  138. /* Cannot or should not combine, so add an empty item to the queue. */
  139. QUEUE_INCR(use_slot);
  140. hs->n++;
  141. hid_pointer_event_clear(&hs->ptr.queue[use_slot], buttons_state);
  142. }
  143. hid_pointer_event_combine(&hs->ptr.queue[use_slot],
  144. hs->kind == HID_MOUSE,
  145. x1, y1, z1);
  146. hs->event(hs);
  147. }
  148. static void hid_keyboard_event(void *opaque, int keycode)
  149. {
  150. HIDState *hs = opaque;
  151. int slot;
  152. if (hs->n == QUEUE_LENGTH) {
  153. fprintf(stderr, "usb-kbd: warning: key event queue full\n");
  154. return;
  155. }
  156. slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
  157. hs->kbd.keycodes[slot] = keycode;
  158. hs->event(hs);
  159. }
  160. static void hid_keyboard_process_keycode(HIDState *hs)
  161. {
  162. uint8_t hid_code, key;
  163. int i, keycode, slot;
  164. if (hs->n == 0) {
  165. return;
  166. }
  167. slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
  168. keycode = hs->kbd.keycodes[slot];
  169. key = keycode & 0x7f;
  170. hid_code = hid_usage_keys[key | ((hs->kbd.modifiers >> 1) & (1 << 7))];
  171. hs->kbd.modifiers &= ~(1 << 8);
  172. switch (hid_code) {
  173. case 0x00:
  174. return;
  175. case 0xe0:
  176. if (hs->kbd.modifiers & (1 << 9)) {
  177. hs->kbd.modifiers ^= 3 << 8;
  178. return;
  179. }
  180. case 0xe1 ... 0xe7:
  181. if (keycode & (1 << 7)) {
  182. hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
  183. return;
  184. }
  185. case 0xe8 ... 0xef:
  186. hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
  187. return;
  188. }
  189. if (keycode & (1 << 7)) {
  190. for (i = hs->kbd.keys - 1; i >= 0; i--) {
  191. if (hs->kbd.key[i] == hid_code) {
  192. hs->kbd.key[i] = hs->kbd.key[-- hs->kbd.keys];
  193. hs->kbd.key[hs->kbd.keys] = 0x00;
  194. break;
  195. }
  196. }
  197. if (i < 0) {
  198. return;
  199. }
  200. } else {
  201. for (i = hs->kbd.keys - 1; i >= 0; i--) {
  202. if (hs->kbd.key[i] == hid_code) {
  203. break;
  204. }
  205. }
  206. if (i < 0) {
  207. if (hs->kbd.keys < sizeof(hs->kbd.key)) {
  208. hs->kbd.key[hs->kbd.keys++] = hid_code;
  209. }
  210. } else {
  211. return;
  212. }
  213. }
  214. }
  215. static inline int int_clamp(int val, int vmin, int vmax)
  216. {
  217. if (val < vmin) {
  218. return vmin;
  219. } else if (val > vmax) {
  220. return vmax;
  221. } else {
  222. return val;
  223. }
  224. }
  225. void hid_pointer_activate(HIDState *hs)
  226. {
  227. if (!hs->ptr.mouse_grabbed) {
  228. qemu_activate_mouse_event_handler(hs->ptr.eh_entry);
  229. hs->ptr.mouse_grabbed = 1;
  230. }
  231. }
  232. int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
  233. {
  234. int dx, dy, dz, b, l;
  235. int index;
  236. HIDPointerEvent *e;
  237. hs->idle_pending = false;
  238. hid_pointer_activate(hs);
  239. /* When the buffer is empty, return the last event. Relative
  240. movements will all be zero. */
  241. index = (hs->n ? hs->head : hs->head - 1);
  242. e = &hs->ptr.queue[index & QUEUE_MASK];
  243. if (hs->kind == HID_MOUSE) {
  244. dx = int_clamp(e->xdx, -127, 127);
  245. dy = int_clamp(e->ydy, -127, 127);
  246. e->xdx -= dx;
  247. e->ydy -= dy;
  248. } else {
  249. dx = e->xdx;
  250. dy = e->ydy;
  251. }
  252. dz = int_clamp(e->dz, -127, 127);
  253. e->dz -= dz;
  254. b = 0;
  255. if (e->buttons_state & MOUSE_EVENT_LBUTTON) {
  256. b |= 0x01;
  257. }
  258. if (e->buttons_state & MOUSE_EVENT_RBUTTON) {
  259. b |= 0x02;
  260. }
  261. if (e->buttons_state & MOUSE_EVENT_MBUTTON) {
  262. b |= 0x04;
  263. }
  264. if (hs->n &&
  265. !e->dz &&
  266. (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
  267. /* that deals with this event */
  268. QUEUE_INCR(hs->head);
  269. hs->n--;
  270. }
  271. /* Appears we have to invert the wheel direction */
  272. dz = 0 - dz;
  273. l = 0;
  274. switch (hs->kind) {
  275. case HID_MOUSE:
  276. if (len > l) {
  277. buf[l++] = b;
  278. }
  279. if (len > l) {
  280. buf[l++] = dx;
  281. }
  282. if (len > l) {
  283. buf[l++] = dy;
  284. }
  285. if (len > l) {
  286. buf[l++] = dz;
  287. }
  288. break;
  289. case HID_TABLET:
  290. if (len > l) {
  291. buf[l++] = b;
  292. }
  293. if (len > l) {
  294. buf[l++] = dx & 0xff;
  295. }
  296. if (len > l) {
  297. buf[l++] = dx >> 8;
  298. }
  299. if (len > l) {
  300. buf[l++] = dy & 0xff;
  301. }
  302. if (len > l) {
  303. buf[l++] = dy >> 8;
  304. }
  305. if (len > l) {
  306. buf[l++] = dz;
  307. }
  308. break;
  309. default:
  310. abort();
  311. }
  312. return l;
  313. }
  314. int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
  315. {
  316. hs->idle_pending = false;
  317. if (len < 2) {
  318. return 0;
  319. }
  320. hid_keyboard_process_keycode(hs);
  321. buf[0] = hs->kbd.modifiers & 0xff;
  322. buf[1] = 0;
  323. if (hs->kbd.keys > 6) {
  324. memset(buf + 2, HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
  325. } else {
  326. memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
  327. }
  328. return MIN(8, len);
  329. }
  330. int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
  331. {
  332. if (len > 0) {
  333. int ledstate = 0;
  334. /* 0x01: Num Lock LED
  335. * 0x02: Caps Lock LED
  336. * 0x04: Scroll Lock LED
  337. * 0x08: Compose LED
  338. * 0x10: Kana LED */
  339. hs->kbd.leds = buf[0];
  340. if (hs->kbd.leds & 0x04) {
  341. ledstate |= QEMU_SCROLL_LOCK_LED;
  342. }
  343. if (hs->kbd.leds & 0x01) {
  344. ledstate |= QEMU_NUM_LOCK_LED;
  345. }
  346. if (hs->kbd.leds & 0x02) {
  347. ledstate |= QEMU_CAPS_LOCK_LED;
  348. }
  349. kbd_put_ledstate(ledstate);
  350. }
  351. return 0;
  352. }
  353. void hid_reset(HIDState *hs)
  354. {
  355. switch (hs->kind) {
  356. case HID_KEYBOARD:
  357. memset(hs->kbd.keycodes, 0, sizeof(hs->kbd.keycodes));
  358. memset(hs->kbd.key, 0, sizeof(hs->kbd.key));
  359. hs->kbd.keys = 0;
  360. break;
  361. case HID_MOUSE:
  362. case HID_TABLET:
  363. memset(hs->ptr.queue, 0, sizeof(hs->ptr.queue));
  364. break;
  365. }
  366. hs->head = 0;
  367. hs->n = 0;
  368. hs->protocol = 1;
  369. hs->idle = 0;
  370. hs->idle_pending = false;
  371. hid_del_idle_timer(hs);
  372. }
  373. void hid_free(HIDState *hs)
  374. {
  375. switch (hs->kind) {
  376. case HID_KEYBOARD:
  377. qemu_remove_kbd_event_handler(hs->kbd.eh_entry);
  378. break;
  379. case HID_MOUSE:
  380. case HID_TABLET:
  381. qemu_remove_mouse_event_handler(hs->ptr.eh_entry);
  382. break;
  383. }
  384. hid_del_idle_timer(hs);
  385. }
  386. void hid_init(HIDState *hs, int kind, HIDEventFunc event)
  387. {
  388. hs->kind = kind;
  389. hs->event = event;
  390. if (hs->kind == HID_KEYBOARD) {
  391. hs->kbd.eh_entry = qemu_add_kbd_event_handler(hid_keyboard_event, hs);
  392. } else if (hs->kind == HID_MOUSE) {
  393. hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
  394. 0, "QEMU HID Mouse");
  395. } else if (hs->kind == HID_TABLET) {
  396. hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
  397. 1, "QEMU HID Tablet");
  398. }
  399. }
  400. static int hid_post_load(void *opaque, int version_id)
  401. {
  402. HIDState *s = opaque;
  403. hid_set_next_idle(s);
  404. return 0;
  405. }
  406. static const VMStateDescription vmstate_hid_ptr_queue = {
  407. .name = "HIDPointerEventQueue",
  408. .version_id = 1,
  409. .minimum_version_id = 1,
  410. .fields = (VMStateField[]) {
  411. VMSTATE_INT32(xdx, HIDPointerEvent),
  412. VMSTATE_INT32(ydy, HIDPointerEvent),
  413. VMSTATE_INT32(dz, HIDPointerEvent),
  414. VMSTATE_INT32(buttons_state, HIDPointerEvent),
  415. VMSTATE_END_OF_LIST()
  416. }
  417. };
  418. const VMStateDescription vmstate_hid_ptr_device = {
  419. .name = "HIDPointerDevice",
  420. .version_id = 1,
  421. .minimum_version_id = 1,
  422. .post_load = hid_post_load,
  423. .fields = (VMStateField[]) {
  424. VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0,
  425. vmstate_hid_ptr_queue, HIDPointerEvent),
  426. VMSTATE_UINT32(head, HIDState),
  427. VMSTATE_UINT32(n, HIDState),
  428. VMSTATE_INT32(protocol, HIDState),
  429. VMSTATE_UINT8(idle, HIDState),
  430. VMSTATE_END_OF_LIST(),
  431. }
  432. };
  433. const VMStateDescription vmstate_hid_keyboard_device = {
  434. .name = "HIDKeyboardDevice",
  435. .version_id = 1,
  436. .minimum_version_id = 1,
  437. .post_load = hid_post_load,
  438. .fields = (VMStateField[]) {
  439. VMSTATE_UINT32_ARRAY(kbd.keycodes, HIDState, QUEUE_LENGTH),
  440. VMSTATE_UINT32(head, HIDState),
  441. VMSTATE_UINT32(n, HIDState),
  442. VMSTATE_UINT16(kbd.modifiers, HIDState),
  443. VMSTATE_UINT8(kbd.leds, HIDState),
  444. VMSTATE_UINT8_ARRAY(kbd.key, HIDState, 16),
  445. VMSTATE_INT32(kbd.keys, HIDState),
  446. VMSTATE_INT32(protocol, HIDState),
  447. VMSTATE_UINT8(idle, HIDState),
  448. VMSTATE_END_OF_LIST(),
  449. }
  450. };