2 * QEMU PS/2 keyboard/mouse emulation
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
27 #include "hw/input/ps2.h"
28 #include "ui/console.h"
30 #include "sysemu/sysemu.h"
34 /* to guarantee safe serialization of input event by Munkyu Im */
35 #include "qemu/thread.h"
36 static QemuMutex mutex;
39 /* debug PC keyboard */
42 /* debug PC keyboard : only mouse */
45 /* Keyboard Commands */
46 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
47 #define KBD_CMD_ECHO 0xEE
48 #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */
49 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
50 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
51 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
52 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
53 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
54 #define KBD_CMD_RESET 0xFF /* Reset */
56 /* Keyboard Replies */
57 #define KBD_REPLY_POR 0xAA /* Power on reset */
58 #define KBD_REPLY_ID 0xAB /* Keyboard ID */
59 #define KBD_REPLY_ACK 0xFA /* Command ACK */
60 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
63 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
64 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
65 #define AUX_SET_RES 0xE8 /* Set resolution */
66 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
67 #define AUX_SET_STREAM 0xEA /* Set stream mode */
68 #define AUX_POLL 0xEB /* Poll */
69 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
70 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
71 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
72 #define AUX_GET_TYPE 0xF2 /* Get type */
73 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
74 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
75 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
76 #define AUX_SET_DEFAULT 0xF6
77 #define AUX_RESET 0xFF /* Reset aux device */
78 #define AUX_ACK 0xFA /* Command byte ACK. */
80 #define MOUSE_STATUS_REMOTE 0x40
81 #define MOUSE_STATUS_ENABLED 0x20
82 #define MOUSE_STATUS_SCALE21 0x10
84 #define PS2_QUEUE_SIZE 16 /* Buffer size required by PS/2 protocol */
87 /* Keep the data array 256 bytes long, which compatibility
88 with older qemu versions. */
90 int rptr, wptr, count;
96 void (*update_irq)(void *, int);
104 int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
111 uint8_t mouse_status;
112 uint8_t mouse_resolution;
113 uint8_t mouse_sample_rate;
115 uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
116 uint8_t mouse_detect_state;
117 int mouse_dx; /* current values, needed for 'poll' mode */
120 uint8_t mouse_buttons;
123 /* Table to convert from QEMU codes to scancodes. */
124 static const uint16_t qcode_to_keycode_set1[Q_KEY_CODE__MAX] = {
125 [0 ... Q_KEY_CODE__MAX - 1] = 0,
127 [Q_KEY_CODE_A] = 0x1e,
128 [Q_KEY_CODE_B] = 0x30,
129 [Q_KEY_CODE_C] = 0x2e,
130 [Q_KEY_CODE_D] = 0x20,
131 [Q_KEY_CODE_E] = 0x12,
132 [Q_KEY_CODE_F] = 0x21,
133 [Q_KEY_CODE_G] = 0x22,
134 [Q_KEY_CODE_H] = 0x23,
135 [Q_KEY_CODE_I] = 0x17,
136 [Q_KEY_CODE_J] = 0x24,
137 [Q_KEY_CODE_K] = 0x25,
138 [Q_KEY_CODE_L] = 0x26,
139 [Q_KEY_CODE_M] = 0x32,
140 [Q_KEY_CODE_N] = 0x31,
141 [Q_KEY_CODE_O] = 0x18,
142 [Q_KEY_CODE_P] = 0x19,
143 [Q_KEY_CODE_Q] = 0x10,
144 [Q_KEY_CODE_R] = 0x13,
145 [Q_KEY_CODE_S] = 0x1f,
146 [Q_KEY_CODE_T] = 0x14,
147 [Q_KEY_CODE_U] = 0x16,
148 [Q_KEY_CODE_V] = 0x2f,
149 [Q_KEY_CODE_W] = 0x11,
150 [Q_KEY_CODE_X] = 0x2d,
151 [Q_KEY_CODE_Y] = 0x15,
152 [Q_KEY_CODE_Z] = 0x2c,
153 [Q_KEY_CODE_0] = 0x0b,
154 [Q_KEY_CODE_1] = 0x02,
155 [Q_KEY_CODE_2] = 0x03,
156 [Q_KEY_CODE_3] = 0x04,
157 [Q_KEY_CODE_4] = 0x05,
158 [Q_KEY_CODE_5] = 0x06,
159 [Q_KEY_CODE_6] = 0x07,
160 [Q_KEY_CODE_7] = 0x08,
161 [Q_KEY_CODE_8] = 0x09,
162 [Q_KEY_CODE_9] = 0x0a,
163 [Q_KEY_CODE_GRAVE_ACCENT] = 0x29,
164 [Q_KEY_CODE_MINUS] = 0x0c,
165 [Q_KEY_CODE_EQUAL] = 0x0d,
166 [Q_KEY_CODE_BACKSLASH] = 0x2b,
167 [Q_KEY_CODE_BACKSPACE] = 0x0e,
168 [Q_KEY_CODE_SPC] = 0x39,
169 [Q_KEY_CODE_TAB] = 0x0f,
170 [Q_KEY_CODE_CAPS_LOCK] = 0x3a,
171 [Q_KEY_CODE_SHIFT] = 0x2a,
172 [Q_KEY_CODE_CTRL] = 0x1d,
173 [Q_KEY_CODE_META_L] = 0xe05b,
174 [Q_KEY_CODE_ALT] = 0x38,
175 [Q_KEY_CODE_SHIFT_R] = 0x36,
176 [Q_KEY_CODE_CTRL_R] = 0xe01d,
177 [Q_KEY_CODE_META_R] = 0xe05c,
178 [Q_KEY_CODE_ALT_R] = 0xe038,
179 [Q_KEY_CODE_MENU] = 0xe05d,
180 [Q_KEY_CODE_RET] = 0x1c,
181 [Q_KEY_CODE_ESC] = 0x01,
182 [Q_KEY_CODE_F1] = 0x3b,
183 [Q_KEY_CODE_F2] = 0x3c,
184 [Q_KEY_CODE_F3] = 0x3d,
185 [Q_KEY_CODE_F4] = 0x3e,
186 [Q_KEY_CODE_F5] = 0x3f,
187 [Q_KEY_CODE_F6] = 0x40,
188 [Q_KEY_CODE_F7] = 0x41,
189 [Q_KEY_CODE_F8] = 0x42,
190 [Q_KEY_CODE_F9] = 0x43,
191 [Q_KEY_CODE_F10] = 0x44,
192 [Q_KEY_CODE_F11] = 0x57,
193 [Q_KEY_CODE_F12] = 0x58,
194 /* special handling for Q_KEY_CODE_PRINT */
195 [Q_KEY_CODE_SCROLL_LOCK] = 0x46,
196 /* special handling for Q_KEY_CODE_PAUSE */
197 [Q_KEY_CODE_BRACKET_LEFT] = 0x1a,
198 [Q_KEY_CODE_INSERT] = 0xe052,
199 [Q_KEY_CODE_HOME] = 0xe047,
200 [Q_KEY_CODE_PGUP] = 0xe049,
201 [Q_KEY_CODE_DELETE] = 0xe053,
202 [Q_KEY_CODE_END] = 0xe04f,
203 [Q_KEY_CODE_PGDN] = 0xe051,
204 [Q_KEY_CODE_UP] = 0xe048,
205 [Q_KEY_CODE_LEFT] = 0xe04b,
206 [Q_KEY_CODE_DOWN] = 0xe050,
207 [Q_KEY_CODE_RIGHT] = 0xe04d,
208 [Q_KEY_CODE_NUM_LOCK] = 0x45,
209 [Q_KEY_CODE_KP_DIVIDE] = 0xe035,
210 [Q_KEY_CODE_KP_MULTIPLY] = 0x37,
211 [Q_KEY_CODE_KP_SUBTRACT] = 0x4a,
212 [Q_KEY_CODE_KP_ADD] = 0x4e,
213 [Q_KEY_CODE_KP_ENTER] = 0xe01c,
214 [Q_KEY_CODE_KP_DECIMAL] = 0x53,
215 [Q_KEY_CODE_KP_0] = 0x52,
216 [Q_KEY_CODE_KP_1] = 0x4f,
217 [Q_KEY_CODE_KP_2] = 0x50,
218 [Q_KEY_CODE_KP_3] = 0x51,
219 [Q_KEY_CODE_KP_4] = 0x4b,
220 [Q_KEY_CODE_KP_5] = 0x4c,
221 [Q_KEY_CODE_KP_6] = 0x4d,
222 [Q_KEY_CODE_KP_7] = 0x47,
223 [Q_KEY_CODE_KP_8] = 0x48,
224 [Q_KEY_CODE_KP_9] = 0x49,
225 [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b,
226 [Q_KEY_CODE_SEMICOLON] = 0x27,
227 [Q_KEY_CODE_APOSTROPHE] = 0x28,
228 [Q_KEY_CODE_COMMA] = 0x33,
229 [Q_KEY_CODE_DOT] = 0x34,
230 [Q_KEY_CODE_SLASH] = 0x35,
233 [Q_KEY_CODE_POWER] = 0x0e5e,
234 [Q_KEY_CODE_SLEEP] = 0x0e5f,
235 [Q_KEY_CODE_WAKE] = 0x0e63,
237 [Q_KEY_CODE_AUDIONEXT] = 0xe019,
238 [Q_KEY_CODE_AUDIOPREV] = 0xe010,
239 [Q_KEY_CODE_AUDIOSTOP] = 0xe024,
240 [Q_KEY_CODE_AUDIOPLAY] = 0xe022,
241 [Q_KEY_CODE_AUDIOMUTE] = 0xe020,
242 [Q_KEY_CODE_VOLUMEUP] = 0xe030,
243 [Q_KEY_CODE_VOLUMEDOWN] = 0xe02e,
244 [Q_KEY_CODE_MEDIASELECT] = 0xe06d,
245 [Q_KEY_CODE_MAIL] = 0xe06c,
246 [Q_KEY_CODE_CALCULATOR] = 0xe021,
247 [Q_KEY_CODE_COMPUTER] = 0xe06b,
248 [Q_KEY_CODE_AC_SEARCH] = 0xe065,
249 [Q_KEY_CODE_AC_HOME] = 0xe032,
250 [Q_KEY_CODE_AC_BACK] = 0xe06a,
251 [Q_KEY_CODE_AC_FORWARD] = 0xe069,
252 [Q_KEY_CODE_AC_STOP] = 0xe068,
253 [Q_KEY_CODE_AC_REFRESH] = 0xe067,
254 [Q_KEY_CODE_AC_BOOKMARKS] = 0xe066,
257 [Q_KEY_CODE_ASTERISK] = 0x37,
258 [Q_KEY_CODE_LESS] = 0x56,
259 [Q_KEY_CODE_RO] = 0x73,
260 [Q_KEY_CODE_KP_COMMA] = 0x7e,
263 static const uint16_t qcode_to_keycode_set2[Q_KEY_CODE__MAX] = {
264 [0 ... Q_KEY_CODE__MAX - 1] = 0,
266 [Q_KEY_CODE_A] = 0x1c,
267 [Q_KEY_CODE_B] = 0x32,
268 [Q_KEY_CODE_C] = 0x21,
269 [Q_KEY_CODE_D] = 0x23,
270 [Q_KEY_CODE_E] = 0x24,
271 [Q_KEY_CODE_F] = 0x2b,
272 [Q_KEY_CODE_G] = 0x34,
273 [Q_KEY_CODE_H] = 0x33,
274 [Q_KEY_CODE_I] = 0x43,
275 [Q_KEY_CODE_J] = 0x3b,
276 [Q_KEY_CODE_K] = 0x42,
277 [Q_KEY_CODE_L] = 0x4b,
278 [Q_KEY_CODE_M] = 0x3a,
279 [Q_KEY_CODE_N] = 0x31,
280 [Q_KEY_CODE_O] = 0x44,
281 [Q_KEY_CODE_P] = 0x4d,
282 [Q_KEY_CODE_Q] = 0x15,
283 [Q_KEY_CODE_R] = 0x2d,
284 [Q_KEY_CODE_S] = 0x1b,
285 [Q_KEY_CODE_T] = 0x2c,
286 [Q_KEY_CODE_U] = 0x3c,
287 [Q_KEY_CODE_V] = 0x2a,
288 [Q_KEY_CODE_W] = 0x1d,
289 [Q_KEY_CODE_X] = 0x22,
290 [Q_KEY_CODE_Y] = 0x35,
291 [Q_KEY_CODE_Z] = 0x1a,
292 [Q_KEY_CODE_0] = 0x45,
293 [Q_KEY_CODE_1] = 0x16,
294 [Q_KEY_CODE_2] = 0x1e,
295 [Q_KEY_CODE_3] = 0x26,
296 [Q_KEY_CODE_4] = 0x25,
297 [Q_KEY_CODE_5] = 0x2e,
298 [Q_KEY_CODE_6] = 0x36,
299 [Q_KEY_CODE_7] = 0x3d,
300 [Q_KEY_CODE_8] = 0x3e,
301 [Q_KEY_CODE_9] = 0x46,
302 [Q_KEY_CODE_GRAVE_ACCENT] = 0x0e,
303 [Q_KEY_CODE_MINUS] = 0x4e,
304 [Q_KEY_CODE_EQUAL] = 0x55,
305 [Q_KEY_CODE_BACKSLASH] = 0x5d,
306 [Q_KEY_CODE_BACKSPACE] = 0x66,
307 [Q_KEY_CODE_SPC] = 0x29,
308 [Q_KEY_CODE_TAB] = 0x0d,
309 [Q_KEY_CODE_CAPS_LOCK] = 0x58,
310 [Q_KEY_CODE_SHIFT] = 0x12,
311 [Q_KEY_CODE_CTRL] = 0x14,
312 [Q_KEY_CODE_META_L] = 0xe01f,
313 [Q_KEY_CODE_ALT] = 0x11,
314 [Q_KEY_CODE_SHIFT_R] = 0x59,
315 [Q_KEY_CODE_CTRL_R] = 0xe014,
316 [Q_KEY_CODE_META_R] = 0xe027,
317 [Q_KEY_CODE_ALT_R] = 0xe011,
318 [Q_KEY_CODE_MENU] = 0xe02f,
319 [Q_KEY_CODE_RET] = 0x5a,
320 [Q_KEY_CODE_ESC] = 0x76,
321 [Q_KEY_CODE_F1] = 0x05,
322 [Q_KEY_CODE_F2] = 0x06,
323 [Q_KEY_CODE_F3] = 0x04,
324 [Q_KEY_CODE_F4] = 0x0c,
325 [Q_KEY_CODE_F5] = 0x03,
326 [Q_KEY_CODE_F6] = 0x0b,
327 [Q_KEY_CODE_F7] = 0x83,
328 [Q_KEY_CODE_F8] = 0x0a,
329 [Q_KEY_CODE_F9] = 0x01,
330 [Q_KEY_CODE_F10] = 0x09,
331 [Q_KEY_CODE_F11] = 0x78,
332 [Q_KEY_CODE_F12] = 0x07,
333 /* special handling for Q_KEY_CODE_PRINT */
334 [Q_KEY_CODE_SCROLL_LOCK] = 0x7e,
335 /* special handling for Q_KEY_CODE_PAUSE */
336 [Q_KEY_CODE_BRACKET_LEFT] = 0x54,
337 [Q_KEY_CODE_INSERT] = 0xe070,
338 [Q_KEY_CODE_HOME] = 0xe06c,
339 [Q_KEY_CODE_PGUP] = 0xe07d,
340 [Q_KEY_CODE_DELETE] = 0xe071,
341 [Q_KEY_CODE_END] = 0xe069,
342 [Q_KEY_CODE_PGDN] = 0xe07a,
343 [Q_KEY_CODE_UP] = 0xe075,
344 [Q_KEY_CODE_LEFT] = 0xe06b,
345 [Q_KEY_CODE_DOWN] = 0xe072,
346 [Q_KEY_CODE_RIGHT] = 0xe074,
347 [Q_KEY_CODE_NUM_LOCK] = 0x77,
348 [Q_KEY_CODE_KP_DIVIDE] = 0xe04a,
349 [Q_KEY_CODE_KP_MULTIPLY] = 0x7c,
350 [Q_KEY_CODE_KP_SUBTRACT] = 0x7b,
351 [Q_KEY_CODE_KP_ADD] = 0x79,
352 [Q_KEY_CODE_KP_ENTER] = 0xe05a,
353 [Q_KEY_CODE_KP_DECIMAL] = 0x71,
354 [Q_KEY_CODE_KP_0] = 0x70,
355 [Q_KEY_CODE_KP_1] = 0x69,
356 [Q_KEY_CODE_KP_2] = 0x72,
357 [Q_KEY_CODE_KP_3] = 0x7a,
358 [Q_KEY_CODE_KP_4] = 0x6b,
359 [Q_KEY_CODE_KP_5] = 0x73,
360 [Q_KEY_CODE_KP_6] = 0x74,
361 [Q_KEY_CODE_KP_7] = 0x6c,
362 [Q_KEY_CODE_KP_8] = 0x75,
363 [Q_KEY_CODE_KP_9] = 0x7d,
364 [Q_KEY_CODE_BRACKET_RIGHT] = 0x5b,
365 [Q_KEY_CODE_SEMICOLON] = 0x4c,
366 [Q_KEY_CODE_APOSTROPHE] = 0x52,
367 [Q_KEY_CODE_COMMA] = 0x41,
368 [Q_KEY_CODE_DOT] = 0x49,
369 [Q_KEY_CODE_SLASH] = 0x4a,
372 [Q_KEY_CODE_POWER] = 0x0e37,
373 [Q_KEY_CODE_SLEEP] = 0x0e3f,
374 [Q_KEY_CODE_WAKE] = 0x0e5e,
376 [Q_KEY_CODE_AUDIONEXT] = 0xe04d,
377 [Q_KEY_CODE_AUDIOPREV] = 0xe015,
378 [Q_KEY_CODE_AUDIOSTOP] = 0xe03b,
379 [Q_KEY_CODE_AUDIOPLAY] = 0xe034,
380 [Q_KEY_CODE_AUDIOMUTE] = 0xe023,
381 [Q_KEY_CODE_VOLUMEUP] = 0xe032,
382 [Q_KEY_CODE_VOLUMEDOWN] = 0xe021,
383 [Q_KEY_CODE_MEDIASELECT] = 0xe050,
384 [Q_KEY_CODE_MAIL] = 0xe048,
385 [Q_KEY_CODE_CALCULATOR] = 0xe02b,
386 [Q_KEY_CODE_COMPUTER] = 0xe040,
387 [Q_KEY_CODE_AC_SEARCH] = 0xe010,
388 [Q_KEY_CODE_AC_HOME] = 0xe03a,
389 [Q_KEY_CODE_AC_BACK] = 0xe038,
390 [Q_KEY_CODE_AC_FORWARD] = 0xe030,
391 [Q_KEY_CODE_AC_STOP] = 0xe028,
392 [Q_KEY_CODE_AC_REFRESH] = 0xe020,
393 [Q_KEY_CODE_AC_BOOKMARKS] = 0xe018,
396 [Q_KEY_CODE_ALTGR] = 0x08,
397 [Q_KEY_CODE_ALTGR_R] = 0xe008,
398 [Q_KEY_CODE_ASTERISK] = 0x7c,
399 [Q_KEY_CODE_LESS] = 0x61,
400 [Q_KEY_CODE_SYSRQ] = 0x7f,
401 [Q_KEY_CODE_RO] = 0x51,
402 [Q_KEY_CODE_KP_COMMA] = 0x6d,
405 static const uint16_t qcode_to_keycode_set3[Q_KEY_CODE__MAX] = {
406 [0 ... Q_KEY_CODE__MAX - 1] = 0,
408 [Q_KEY_CODE_A] = 0x1c,
409 [Q_KEY_CODE_B] = 0x32,
410 [Q_KEY_CODE_C] = 0x21,
411 [Q_KEY_CODE_D] = 0x23,
412 [Q_KEY_CODE_E] = 0x24,
413 [Q_KEY_CODE_F] = 0x2b,
414 [Q_KEY_CODE_G] = 0x34,
415 [Q_KEY_CODE_H] = 0x33,
416 [Q_KEY_CODE_I] = 0x43,
417 [Q_KEY_CODE_J] = 0x3b,
418 [Q_KEY_CODE_K] = 0x42,
419 [Q_KEY_CODE_L] = 0x4b,
420 [Q_KEY_CODE_M] = 0x3a,
421 [Q_KEY_CODE_N] = 0x31,
422 [Q_KEY_CODE_O] = 0x44,
423 [Q_KEY_CODE_P] = 0x4d,
424 [Q_KEY_CODE_Q] = 0x15,
425 [Q_KEY_CODE_R] = 0x2d,
426 [Q_KEY_CODE_S] = 0x1b,
427 [Q_KEY_CODE_T] = 0x2c,
428 [Q_KEY_CODE_U] = 0x3c,
429 [Q_KEY_CODE_V] = 0x2a,
430 [Q_KEY_CODE_W] = 0x1d,
431 [Q_KEY_CODE_X] = 0x22,
432 [Q_KEY_CODE_Y] = 0x35,
433 [Q_KEY_CODE_Z] = 0x1a,
434 [Q_KEY_CODE_0] = 0x45,
435 [Q_KEY_CODE_1] = 0x16,
436 [Q_KEY_CODE_2] = 0x1e,
437 [Q_KEY_CODE_3] = 0x26,
438 [Q_KEY_CODE_4] = 0x25,
439 [Q_KEY_CODE_5] = 0x2e,
440 [Q_KEY_CODE_6] = 0x36,
441 [Q_KEY_CODE_7] = 0x3d,
442 [Q_KEY_CODE_8] = 0x3e,
443 [Q_KEY_CODE_9] = 0x46,
444 [Q_KEY_CODE_GRAVE_ACCENT] = 0x0e,
445 [Q_KEY_CODE_MINUS] = 0x4e,
446 [Q_KEY_CODE_EQUAL] = 0x55,
447 [Q_KEY_CODE_BACKSLASH] = 0x5c,
448 [Q_KEY_CODE_BACKSPACE] = 0x66,
449 [Q_KEY_CODE_SPC] = 0x29,
450 [Q_KEY_CODE_TAB] = 0x0d,
451 [Q_KEY_CODE_CAPS_LOCK] = 0x14,
452 [Q_KEY_CODE_SHIFT] = 0x12,
453 [Q_KEY_CODE_CTRL] = 0x11,
454 [Q_KEY_CODE_META_L] = 0x8b,
455 [Q_KEY_CODE_ALT] = 0x19,
456 [Q_KEY_CODE_SHIFT_R] = 0x59,
457 [Q_KEY_CODE_CTRL_R] = 0x58,
458 [Q_KEY_CODE_META_R] = 0x8c,
459 [Q_KEY_CODE_ALT_R] = 0x39,
460 [Q_KEY_CODE_MENU] = 0x8d,
461 [Q_KEY_CODE_RET] = 0x5a,
462 [Q_KEY_CODE_ESC] = 0x08,
463 [Q_KEY_CODE_F1] = 0x07,
464 [Q_KEY_CODE_F2] = 0x0f,
465 [Q_KEY_CODE_F3] = 0x17,
466 [Q_KEY_CODE_F4] = 0x1f,
467 [Q_KEY_CODE_F5] = 0x27,
468 [Q_KEY_CODE_F6] = 0x2f,
469 [Q_KEY_CODE_F7] = 0x37,
470 [Q_KEY_CODE_F8] = 0x3f,
471 [Q_KEY_CODE_F9] = 0x47,
472 [Q_KEY_CODE_F10] = 0x4f,
473 [Q_KEY_CODE_F11] = 0x56,
474 [Q_KEY_CODE_F12] = 0x5e,
475 [Q_KEY_CODE_PRINT] = 0x57,
476 [Q_KEY_CODE_SCROLL_LOCK] = 0x5f,
477 [Q_KEY_CODE_PAUSE] = 0x62,
478 [Q_KEY_CODE_BRACKET_LEFT] = 0x54,
479 [Q_KEY_CODE_INSERT] = 0x67,
480 [Q_KEY_CODE_HOME] = 0x6e,
481 [Q_KEY_CODE_PGUP] = 0x6f,
482 [Q_KEY_CODE_DELETE] = 0x64,
483 [Q_KEY_CODE_END] = 0x65,
484 [Q_KEY_CODE_PGDN] = 0x6d,
485 [Q_KEY_CODE_UP] = 0x63,
486 [Q_KEY_CODE_LEFT] = 0x61,
487 [Q_KEY_CODE_DOWN] = 0x60,
488 [Q_KEY_CODE_RIGHT] = 0x6a,
489 [Q_KEY_CODE_NUM_LOCK] = 0x76,
490 [Q_KEY_CODE_KP_DIVIDE] = 0x4a,
491 [Q_KEY_CODE_KP_MULTIPLY] = 0x7e,
492 [Q_KEY_CODE_KP_SUBTRACT] = 0x4e,
493 [Q_KEY_CODE_KP_ADD] = 0x7c,
494 [Q_KEY_CODE_KP_ENTER] = 0x79,
495 [Q_KEY_CODE_KP_DECIMAL] = 0x71,
496 [Q_KEY_CODE_KP_0] = 0x70,
497 [Q_KEY_CODE_KP_1] = 0x69,
498 [Q_KEY_CODE_KP_2] = 0x72,
499 [Q_KEY_CODE_KP_3] = 0x7a,
500 [Q_KEY_CODE_KP_4] = 0x6b,
501 [Q_KEY_CODE_KP_5] = 0x73,
502 [Q_KEY_CODE_KP_6] = 0x74,
503 [Q_KEY_CODE_KP_7] = 0x6c,
504 [Q_KEY_CODE_KP_8] = 0x75,
505 [Q_KEY_CODE_KP_9] = 0x7d,
506 [Q_KEY_CODE_BRACKET_RIGHT] = 0x5b,
507 [Q_KEY_CODE_SEMICOLON] = 0x4c,
508 [Q_KEY_CODE_APOSTROPHE] = 0x52,
509 [Q_KEY_CODE_COMMA] = 0x41,
510 [Q_KEY_CODE_DOT] = 0x49,
511 [Q_KEY_CODE_SLASH] = 0x4a,
514 static uint8_t translate_table[256] = {
515 0xff, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x3c, 0x58,
516 0x64, 0x44, 0x42, 0x40, 0x3e, 0x0f, 0x29, 0x59,
517 0x65, 0x38, 0x2a, 0x70, 0x1d, 0x10, 0x02, 0x5a,
518 0x66, 0x71, 0x2c, 0x1f, 0x1e, 0x11, 0x03, 0x5b,
519 0x67, 0x2e, 0x2d, 0x20, 0x12, 0x05, 0x04, 0x5c,
520 0x68, 0x39, 0x2f, 0x21, 0x14, 0x13, 0x06, 0x5d,
521 0x69, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, 0x5e,
522 0x6a, 0x72, 0x32, 0x24, 0x16, 0x08, 0x09, 0x5f,
523 0x6b, 0x33, 0x25, 0x17, 0x18, 0x0b, 0x0a, 0x60,
524 0x6c, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0c, 0x61,
525 0x6d, 0x73, 0x28, 0x74, 0x1a, 0x0d, 0x62, 0x6e,
526 0x3a, 0x36, 0x1c, 0x1b, 0x75, 0x2b, 0x63, 0x76,
527 0x55, 0x56, 0x77, 0x78, 0x79, 0x7a, 0x0e, 0x7b,
528 0x7c, 0x4f, 0x7d, 0x4b, 0x47, 0x7e, 0x7f, 0x6f,
529 0x52, 0x53, 0x50, 0x4c, 0x4d, 0x48, 0x01, 0x45,
530 0x57, 0x4e, 0x51, 0x4a, 0x37, 0x49, 0x46, 0x54,
531 0x80, 0x81, 0x82, 0x41, 0x54, 0x85, 0x86, 0x87,
532 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
533 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
534 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
535 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
536 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
537 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
538 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
539 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
540 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
541 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
542 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
543 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
544 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
545 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
546 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
549 void ps2_queue(void *opaque, int b)
551 PS2State *s = (PS2State *)opaque;
552 PS2Queue *q = &s->queue;
554 if (q->count >= PS2_QUEUE_SIZE - 1)
556 q->data[q->wptr] = b;
557 if (++q->wptr == PS2_QUEUE_SIZE)
560 s->update_irq(s->update_arg, 1);
563 /* keycode is the untranslated scancode in the current scancode set. */
564 static void ps2_put_keycode(void *opaque, int keycode)
566 PS2KbdState *s = opaque;
568 trace_ps2_put_keycode(opaque, keycode);
569 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
572 if (keycode == 0xf0) {
573 s->need_high_bit = true;
574 } else if (s->need_high_bit) {
575 ps2_queue(&s->common, translate_table[keycode] | 0x80);
576 s->need_high_bit = false;
578 ps2_queue(&s->common, translate_table[keycode]);
581 ps2_queue(&s->common, keycode);
585 static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
588 PS2KbdState *s = (PS2KbdState *)dev;
589 InputKeyEvent *key = evt->u.key.data;
593 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
594 assert(evt->type == INPUT_EVENT_KIND_KEY);
595 qcode = qemu_input_key_value_to_qcode(key->key);
597 if (s->scancode_set == 1) {
598 if (qcode == Q_KEY_CODE_PAUSE) {
600 ps2_put_keycode(s, 0xe1);
601 ps2_put_keycode(s, 0x1d);
602 ps2_put_keycode(s, 0x45);
603 ps2_put_keycode(s, 0x91);
604 ps2_put_keycode(s, 0x9d);
605 ps2_put_keycode(s, 0xc5);
607 } else if (qcode == Q_KEY_CODE_PRINT) {
609 ps2_put_keycode(s, 0xe0);
610 ps2_put_keycode(s, 0x2a);
611 ps2_put_keycode(s, 0xe0);
612 ps2_put_keycode(s, 0x37);
614 ps2_put_keycode(s, 0xe0);
615 ps2_put_keycode(s, 0xb7);
616 ps2_put_keycode(s, 0xe0);
617 ps2_put_keycode(s, 0xaa);
620 keycode = qcode_to_keycode_set1[qcode];
622 if (keycode & 0xff00) {
623 ps2_put_keycode(s, keycode >> 8);
628 ps2_put_keycode(s, keycode & 0xff);
630 qemu_log_mask(LOG_UNIMP,
631 "ps2: ignoring key with qcode %d\n", qcode);
634 } else if (s->scancode_set == 2) {
635 if (qcode == Q_KEY_CODE_PAUSE) {
637 ps2_put_keycode(s, 0xe1);
638 ps2_put_keycode(s, 0x14);
639 ps2_put_keycode(s, 0x77);
640 ps2_put_keycode(s, 0xe1);
641 ps2_put_keycode(s, 0xf0);
642 ps2_put_keycode(s, 0x14);
643 ps2_put_keycode(s, 0xf0);
644 ps2_put_keycode(s, 0x77);
646 } else if (qcode == Q_KEY_CODE_PRINT) {
648 ps2_put_keycode(s, 0xe0);
649 ps2_put_keycode(s, 0x12);
650 ps2_put_keycode(s, 0xe0);
651 ps2_put_keycode(s, 0x7c);
653 ps2_put_keycode(s, 0xe0);
654 ps2_put_keycode(s, 0xf0);
655 ps2_put_keycode(s, 0x7c);
656 ps2_put_keycode(s, 0xe0);
657 ps2_put_keycode(s, 0xf0);
658 ps2_put_keycode(s, 0x12);
661 keycode = qcode_to_keycode_set2[qcode];
663 if (keycode & 0xff00) {
664 ps2_put_keycode(s, keycode >> 8);
667 ps2_put_keycode(s, 0xf0);
669 ps2_put_keycode(s, keycode & 0xff);
671 qemu_log_mask(LOG_UNIMP,
672 "ps2: ignoring key with qcode %d\n", qcode);
675 } else if (s->scancode_set == 3) {
676 keycode = qcode_to_keycode_set3[qcode];
678 /* FIXME: break code should be configured on a key by key basis */
680 ps2_put_keycode(s, 0xf0);
682 ps2_put_keycode(s, keycode);
684 qemu_log_mask(LOG_UNIMP,
685 "ps2: ignoring key with qcode %d\n", qcode);
690 uint32_t ps2_read_data(void *opaque)
692 PS2State *s = (PS2State *)opaque;
696 trace_ps2_read_data(opaque);
699 /* NOTE: if no data left, we return the last keyboard one
700 (needed for EMM386) */
701 /* XXX: need a timer to do things correctly */
704 index = PS2_QUEUE_SIZE - 1;
705 val = q->data[index];
707 val = q->data[q->rptr];
708 if (++q->rptr == PS2_QUEUE_SIZE)
711 /* reading deasserts IRQ */
712 s->update_irq(s->update_arg, 0);
713 /* reassert IRQs if data left */
714 s->update_irq(s->update_arg, q->count != 0);
719 static void ps2_set_ledstate(PS2KbdState *s, int ledstate)
721 trace_ps2_set_ledstate(s, ledstate);
722 s->ledstate = ledstate;
723 kbd_put_ledstate(ledstate);
726 static void ps2_reset_keyboard(PS2KbdState *s)
728 trace_ps2_reset_keyboard(s);
731 ps2_set_ledstate(s, 0);
734 void ps2_write_keyboard(void *opaque, int val)
736 PS2KbdState *s = (PS2KbdState *)opaque;
738 trace_ps2_write_keyboard(opaque, val);
739 switch(s->common.write_cmd) {
744 ps2_queue(&s->common, KBD_REPLY_ACK);
747 ps2_queue(&s->common, KBD_REPLY_RESEND);
750 ps2_queue(&s->common, KBD_REPLY_ACK);
751 /* We emulate a MF2 AT keyboard here */
752 ps2_queue(&s->common, KBD_REPLY_ID);
754 ps2_queue(&s->common, 0x41);
756 ps2_queue(&s->common, 0x83);
759 ps2_queue(&s->common, KBD_CMD_ECHO);
763 ps2_queue(&s->common, KBD_REPLY_ACK);
765 case KBD_CMD_SCANCODE:
766 case KBD_CMD_SET_LEDS:
767 case KBD_CMD_SET_RATE:
768 s->common.write_cmd = val;
769 ps2_queue(&s->common, KBD_REPLY_ACK);
771 case KBD_CMD_RESET_DISABLE:
772 ps2_reset_keyboard(s);
774 ps2_queue(&s->common, KBD_REPLY_ACK);
776 case KBD_CMD_RESET_ENABLE:
777 ps2_reset_keyboard(s);
779 ps2_queue(&s->common, KBD_REPLY_ACK);
782 ps2_reset_keyboard(s);
783 ps2_queue(&s->common, KBD_REPLY_ACK);
784 ps2_queue(&s->common, KBD_REPLY_POR);
787 ps2_queue(&s->common, KBD_REPLY_RESEND);
791 case KBD_CMD_SCANCODE:
793 ps2_queue(&s->common, KBD_REPLY_ACK);
794 ps2_put_keycode(s, s->scancode_set);
795 } else if (val >= 1 && val <= 3) {
796 s->scancode_set = val;
797 ps2_queue(&s->common, KBD_REPLY_ACK);
799 ps2_queue(&s->common, KBD_REPLY_RESEND);
801 s->common.write_cmd = -1;
803 case KBD_CMD_SET_LEDS:
804 ps2_set_ledstate(s, val);
805 ps2_queue(&s->common, KBD_REPLY_ACK);
806 s->common.write_cmd = -1;
808 case KBD_CMD_SET_RATE:
809 ps2_queue(&s->common, KBD_REPLY_ACK);
810 s->common.write_cmd = -1;
815 /* Set the scancode translation mode.
817 1 = translated scancodes (used by qemu internally). */
819 void ps2_keyboard_set_translation(void *opaque, int mode)
821 PS2KbdState *s = (PS2KbdState *)opaque;
822 trace_ps2_keyboard_set_translation(opaque, mode);
826 static void ps2_mouse_send_packet(PS2MouseState *s)
834 /* XXX: increase range to 8 bits ? */
843 b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
844 ps2_queue(&s->common, b);
845 ps2_queue(&s->common, dx1 & 0xff);
846 ps2_queue(&s->common, dy1 & 0xff);
847 /* extra byte for IMPS/2 or IMEX */
848 switch(s->mouse_type) {
856 ps2_queue(&s->common, dz1 & 0xff);
863 b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
864 ps2_queue(&s->common, b);
868 trace_ps2_mouse_send_packet(s, dx1, dy1, dz1, b);
875 static void ps2_mouse_event(DeviceState *dev, QemuConsole *src,
878 static const int bmap[INPUT_BUTTON__MAX] = {
879 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
880 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
881 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
883 PS2MouseState *s = (PS2MouseState *)dev;
884 InputMoveEvent *move;
887 /* check if deltas are recorded when disabled */
888 if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
892 case INPUT_EVENT_KIND_REL:
893 move = evt->u.rel.data;
894 if (move->axis == INPUT_AXIS_X) {
895 s->mouse_dx += move->value;
896 } else if (move->axis == INPUT_AXIS_Y) {
897 s->mouse_dy -= move->value;
901 case INPUT_EVENT_KIND_BTN:
902 btn = evt->u.btn.data;
904 s->mouse_buttons |= bmap[btn->button];
905 if (btn->button == INPUT_BUTTON_WHEEL_UP) {
907 } else if (btn->button == INPUT_BUTTON_WHEEL_DOWN) {
911 s->mouse_buttons &= ~bmap[btn->button];
921 static void ps2_mouse_sync(DeviceState *dev)
923 PS2MouseState *s = (PS2MouseState *)dev;
925 if (s->mouse_buttons) {
926 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
928 if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) {
929 while (s->common.queue.count < PS2_QUEUE_SIZE - 4) {
930 /* if not remote, send event. Multiple events are sent if
933 /* to guarantee safe serialization of input event by Munkyu Im */
934 qemu_mutex_lock(&mutex);
935 ps2_mouse_send_packet(s);
936 qemu_mutex_unlock(&mutex);
938 ps2_mouse_send_packet(s);
940 if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
946 void ps2_mouse_fake_event(void *opaque)
948 PS2MouseState *s = opaque;
949 trace_ps2_mouse_fake_event(opaque);
951 ps2_mouse_sync(opaque);
954 void ps2_write_mouse(void *opaque, int val)
956 PS2MouseState *s = (PS2MouseState *)opaque;
958 trace_ps2_write_mouse(opaque, val);
960 printf("kbd: write mouse 0x%02x\n", val);
962 switch(s->common.write_cmd) {
967 if (val == AUX_RESET_WRAP) {
969 ps2_queue(&s->common, AUX_ACK);
971 } else if (val != AUX_RESET) {
972 ps2_queue(&s->common, val);
977 case AUX_SET_SCALE11:
978 s->mouse_status &= ~MOUSE_STATUS_SCALE21;
979 ps2_queue(&s->common, AUX_ACK);
981 case AUX_SET_SCALE21:
982 s->mouse_status |= MOUSE_STATUS_SCALE21;
983 ps2_queue(&s->common, AUX_ACK);
986 s->mouse_status &= ~MOUSE_STATUS_REMOTE;
987 ps2_queue(&s->common, AUX_ACK);
991 ps2_queue(&s->common, AUX_ACK);
994 s->mouse_status |= MOUSE_STATUS_REMOTE;
995 ps2_queue(&s->common, AUX_ACK);
998 ps2_queue(&s->common, AUX_ACK);
999 ps2_queue(&s->common, s->mouse_type);
1002 case AUX_SET_SAMPLE:
1003 s->common.write_cmd = val;
1004 ps2_queue(&s->common, AUX_ACK);
1007 ps2_queue(&s->common, AUX_ACK);
1008 ps2_queue(&s->common, s->mouse_status);
1009 ps2_queue(&s->common, s->mouse_resolution);
1010 ps2_queue(&s->common, s->mouse_sample_rate);
1013 ps2_queue(&s->common, AUX_ACK);
1014 ps2_mouse_send_packet(s);
1016 case AUX_ENABLE_DEV:
1017 s->mouse_status |= MOUSE_STATUS_ENABLED;
1018 ps2_queue(&s->common, AUX_ACK);
1020 case AUX_DISABLE_DEV:
1021 s->mouse_status &= ~MOUSE_STATUS_ENABLED;
1022 ps2_queue(&s->common, AUX_ACK);
1024 case AUX_SET_DEFAULT:
1025 s->mouse_sample_rate = 100;
1026 s->mouse_resolution = 2;
1027 s->mouse_status = 0;
1028 ps2_queue(&s->common, AUX_ACK);
1031 s->mouse_sample_rate = 100;
1032 s->mouse_resolution = 2;
1033 s->mouse_status = 0;
1035 ps2_queue(&s->common, AUX_ACK);
1036 ps2_queue(&s->common, 0xaa);
1037 ps2_queue(&s->common, s->mouse_type);
1043 case AUX_SET_SAMPLE:
1044 s->mouse_sample_rate = val;
1045 /* detect IMPS/2 or IMEX */
1046 switch(s->mouse_detect_state) {
1050 s->mouse_detect_state = 1;
1054 s->mouse_detect_state = 2;
1055 else if (val == 200)
1056 s->mouse_detect_state = 3;
1058 s->mouse_detect_state = 0;
1062 s->mouse_type = 3; /* IMPS/2 */
1063 s->mouse_detect_state = 0;
1067 s->mouse_type = 4; /* IMEX */
1068 s->mouse_detect_state = 0;
1071 ps2_queue(&s->common, AUX_ACK);
1072 s->common.write_cmd = -1;
1075 s->mouse_resolution = val;
1076 ps2_queue(&s->common, AUX_ACK);
1077 s->common.write_cmd = -1;
1082 static void ps2_common_reset(PS2State *s)
1090 s->update_irq(s->update_arg, 0);
1093 static void ps2_common_post_load(PS2State *s)
1095 PS2Queue *q = &s->queue;
1098 int tmp_data[PS2_QUEUE_SIZE];
1100 /* set the useful data buffer queue size, < PS2_QUEUE_SIZE */
1101 size = q->count > PS2_QUEUE_SIZE ? 0 : q->count;
1103 /* move the queue elements to the start of data array */
1105 for (i = 0; i < size; i++) {
1106 /* move the queue elements to the temporary buffer */
1107 tmp_data[i] = q->data[q->rptr];
1108 if (++q->rptr == 256) {
1112 memcpy(q->data, tmp_data, size);
1114 /* reset rptr/wptr/count */
1118 s->update_irq(s->update_arg, q->count != 0);
1121 static void ps2_kbd_reset(void *opaque)
1123 PS2KbdState *s = (PS2KbdState *) opaque;
1125 trace_ps2_kbd_reset(opaque);
1126 ps2_common_reset(&s->common);
1127 s->scan_enabled = 0;
1129 s->scancode_set = 2;
1132 static void ps2_mouse_reset(void *opaque)
1134 PS2MouseState *s = (PS2MouseState *) opaque;
1136 trace_ps2_mouse_reset(opaque);
1137 ps2_common_reset(&s->common);
1138 s->mouse_status = 0;
1139 s->mouse_resolution = 0;
1140 s->mouse_sample_rate = 0;
1143 s->mouse_detect_state = 0;
1147 s->mouse_buttons = 0;
1150 static const VMStateDescription vmstate_ps2_common = {
1151 .name = "PS2 Common State",
1153 .minimum_version_id = 2,
1154 .fields = (VMStateField[]) {
1155 VMSTATE_INT32(write_cmd, PS2State),
1156 VMSTATE_INT32(queue.rptr, PS2State),
1157 VMSTATE_INT32(queue.wptr, PS2State),
1158 VMSTATE_INT32(queue.count, PS2State),
1159 VMSTATE_BUFFER(queue.data, PS2State),
1160 VMSTATE_END_OF_LIST()
1164 static bool ps2_keyboard_ledstate_needed(void *opaque)
1166 PS2KbdState *s = opaque;
1168 return s->ledstate != 0; /* 0 is default state */
1171 static int ps2_kbd_ledstate_post_load(void *opaque, int version_id)
1173 PS2KbdState *s = opaque;
1175 kbd_put_ledstate(s->ledstate);
1179 static const VMStateDescription vmstate_ps2_keyboard_ledstate = {
1180 .name = "ps2kbd/ledstate",
1182 .minimum_version_id = 2,
1183 .post_load = ps2_kbd_ledstate_post_load,
1184 .needed = ps2_keyboard_ledstate_needed,
1185 .fields = (VMStateField[]) {
1186 VMSTATE_INT32(ledstate, PS2KbdState),
1187 VMSTATE_END_OF_LIST()
1191 static bool ps2_keyboard_need_high_bit_needed(void *opaque)
1193 PS2KbdState *s = opaque;
1194 return s->need_high_bit != 0; /* 0 is the usual state */
1197 static const VMStateDescription vmstate_ps2_keyboard_need_high_bit = {
1198 .name = "ps2kbd/need_high_bit",
1200 .minimum_version_id = 1,
1201 .needed = ps2_keyboard_need_high_bit_needed,
1202 .fields = (VMStateField[]) {
1203 VMSTATE_BOOL(need_high_bit, PS2KbdState),
1204 VMSTATE_END_OF_LIST()
1208 static int ps2_kbd_post_load(void* opaque, int version_id)
1210 PS2KbdState *s = (PS2KbdState*)opaque;
1211 PS2State *ps2 = &s->common;
1213 if (version_id == 2)
1216 ps2_common_post_load(ps2);
1221 static void ps2_kbd_pre_save(void *opaque)
1223 PS2KbdState *s = (PS2KbdState *)opaque;
1224 PS2State *ps2 = &s->common;
1226 ps2_common_post_load(ps2);
1229 static const VMStateDescription vmstate_ps2_keyboard = {
1232 .minimum_version_id = 2,
1233 .post_load = ps2_kbd_post_load,
1234 .pre_save = ps2_kbd_pre_save,
1235 .fields = (VMStateField[]) {
1236 VMSTATE_STRUCT(common, PS2KbdState, 0, vmstate_ps2_common, PS2State),
1237 VMSTATE_INT32(scan_enabled, PS2KbdState),
1238 VMSTATE_INT32(translate, PS2KbdState),
1239 VMSTATE_INT32_V(scancode_set, PS2KbdState,3),
1240 VMSTATE_END_OF_LIST()
1242 .subsections = (const VMStateDescription*[]) {
1243 &vmstate_ps2_keyboard_ledstate,
1244 &vmstate_ps2_keyboard_need_high_bit,
1249 static int ps2_mouse_post_load(void *opaque, int version_id)
1251 PS2MouseState *s = (PS2MouseState *)opaque;
1252 PS2State *ps2 = &s->common;
1254 ps2_common_post_load(ps2);
1259 static void ps2_mouse_pre_save(void *opaque)
1261 PS2MouseState *s = (PS2MouseState *)opaque;
1262 PS2State *ps2 = &s->common;
1264 ps2_common_post_load(ps2);
1267 static const VMStateDescription vmstate_ps2_mouse = {
1270 .minimum_version_id = 2,
1271 .post_load = ps2_mouse_post_load,
1272 .pre_save = ps2_mouse_pre_save,
1273 .fields = (VMStateField[]) {
1274 VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
1275 VMSTATE_UINT8(mouse_status, PS2MouseState),
1276 VMSTATE_UINT8(mouse_resolution, PS2MouseState),
1277 VMSTATE_UINT8(mouse_sample_rate, PS2MouseState),
1278 VMSTATE_UINT8(mouse_wrap, PS2MouseState),
1279 VMSTATE_UINT8(mouse_type, PS2MouseState),
1280 VMSTATE_UINT8(mouse_detect_state, PS2MouseState),
1281 VMSTATE_INT32(mouse_dx, PS2MouseState),
1282 VMSTATE_INT32(mouse_dy, PS2MouseState),
1283 VMSTATE_INT32(mouse_dz, PS2MouseState),
1284 VMSTATE_UINT8(mouse_buttons, PS2MouseState),
1285 VMSTATE_END_OF_LIST()
1289 static QemuInputHandler ps2_keyboard_handler = {
1290 .name = "QEMU PS/2 Keyboard",
1291 .mask = INPUT_EVENT_MASK_KEY,
1292 .event = ps2_keyboard_event,
1295 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
1297 PS2KbdState *s = (PS2KbdState *)g_malloc0(sizeof(PS2KbdState));
1299 trace_ps2_kbd_init(s);
1300 s->common.update_irq = update_irq;
1301 s->common.update_arg = update_arg;
1302 s->scancode_set = 2;
1303 vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
1304 qemu_input_handler_register((DeviceState *)s,
1305 &ps2_keyboard_handler);
1306 qemu_register_reset(ps2_kbd_reset, s);
1310 static QemuInputHandler ps2_mouse_handler = {
1311 .name = "QEMU PS/2 Mouse",
1312 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
1313 .event = ps2_mouse_event,
1314 .sync = ps2_mouse_sync,
1317 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
1319 PS2MouseState *s = (PS2MouseState *)g_malloc0(sizeof(PS2MouseState));
1321 trace_ps2_mouse_init(s);
1322 s->common.update_irq = update_irq;
1323 s->common.update_arg = update_arg;
1324 vmstate_register(NULL, 0, &vmstate_ps2_mouse, s);
1325 qemu_input_handler_register((DeviceState *)s,
1326 &ps2_mouse_handler);
1327 qemu_register_reset(ps2_mouse_reset, s);
1329 /* to guarantee safe serialization of input event by Munkyu Im */
1330 qemu_mutex_init(&mutex);