ps2: enable multimedia keys
[qemu/ar7.git] / hw / input / ps2.c
blob25ae7fc852bc0b517fe650a2ce40b06ac061d46d
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu/log.h"
26 #include "hw/hw.h"
27 #include "hw/input/ps2.h"
28 #include "ui/console.h"
29 #include "ui/input.h"
30 #include "sysemu/sysemu.h"
32 #include "trace.h"
34 /* debug PC keyboard */
35 //#define DEBUG_KBD
37 /* debug PC keyboard : only mouse */
38 //#define DEBUG_MOUSE
40 /* Keyboard Commands */
41 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
42 #define KBD_CMD_ECHO 0xEE
43 #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */
44 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
45 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
46 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
47 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
48 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
49 #define KBD_CMD_RESET 0xFF /* Reset */
51 /* Keyboard Replies */
52 #define KBD_REPLY_POR 0xAA /* Power on reset */
53 #define KBD_REPLY_ID 0xAB /* Keyboard ID */
54 #define KBD_REPLY_ACK 0xFA /* Command ACK */
55 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
57 /* Mouse Commands */
58 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
59 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
60 #define AUX_SET_RES 0xE8 /* Set resolution */
61 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
62 #define AUX_SET_STREAM 0xEA /* Set stream mode */
63 #define AUX_POLL 0xEB /* Poll */
64 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
65 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
66 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
67 #define AUX_GET_TYPE 0xF2 /* Get type */
68 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
69 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
70 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
71 #define AUX_SET_DEFAULT 0xF6
72 #define AUX_RESET 0xFF /* Reset aux device */
73 #define AUX_ACK 0xFA /* Command byte ACK. */
75 #define MOUSE_STATUS_REMOTE 0x40
76 #define MOUSE_STATUS_ENABLED 0x20
77 #define MOUSE_STATUS_SCALE21 0x10
79 #define PS2_QUEUE_SIZE 16 /* Buffer size required by PS/2 protocol */
81 typedef struct {
82 /* Keep the data array 256 bytes long, which compatibility
83 with older qemu versions. */
84 uint8_t data[256];
85 int rptr, wptr, count;
86 } PS2Queue;
88 struct PS2State {
89 PS2Queue queue;
90 int32_t write_cmd;
91 void (*update_irq)(void *, int);
92 void *update_arg;
95 typedef struct {
96 PS2State common;
97 int scan_enabled;
98 int translate;
99 int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
100 int ledstate;
101 bool need_high_bit;
102 } PS2KbdState;
104 typedef struct {
105 PS2State common;
106 uint8_t mouse_status;
107 uint8_t mouse_resolution;
108 uint8_t mouse_sample_rate;
109 uint8_t mouse_wrap;
110 uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
111 uint8_t mouse_detect_state;
112 int mouse_dx; /* current values, needed for 'poll' mode */
113 int mouse_dy;
114 int mouse_dz;
115 uint8_t mouse_buttons;
116 } PS2MouseState;
118 /* Table to convert from QEMU codes to scancodes. */
119 static const uint16_t qcode_to_keycode_set1[Q_KEY_CODE__MAX] = {
120 [0 ... Q_KEY_CODE__MAX - 1] = 0,
122 [Q_KEY_CODE_A] = 0x1e,
123 [Q_KEY_CODE_B] = 0x30,
124 [Q_KEY_CODE_C] = 0x2e,
125 [Q_KEY_CODE_D] = 0x20,
126 [Q_KEY_CODE_E] = 0x12,
127 [Q_KEY_CODE_F] = 0x21,
128 [Q_KEY_CODE_G] = 0x22,
129 [Q_KEY_CODE_H] = 0x23,
130 [Q_KEY_CODE_I] = 0x17,
131 [Q_KEY_CODE_J] = 0x24,
132 [Q_KEY_CODE_K] = 0x25,
133 [Q_KEY_CODE_L] = 0x26,
134 [Q_KEY_CODE_M] = 0x32,
135 [Q_KEY_CODE_N] = 0x31,
136 [Q_KEY_CODE_O] = 0x18,
137 [Q_KEY_CODE_P] = 0x19,
138 [Q_KEY_CODE_Q] = 0x10,
139 [Q_KEY_CODE_R] = 0x13,
140 [Q_KEY_CODE_S] = 0x1f,
141 [Q_KEY_CODE_T] = 0x14,
142 [Q_KEY_CODE_U] = 0x16,
143 [Q_KEY_CODE_V] = 0x2f,
144 [Q_KEY_CODE_W] = 0x11,
145 [Q_KEY_CODE_X] = 0x2d,
146 [Q_KEY_CODE_Y] = 0x15,
147 [Q_KEY_CODE_Z] = 0x2c,
148 [Q_KEY_CODE_0] = 0x0b,
149 [Q_KEY_CODE_1] = 0x02,
150 [Q_KEY_CODE_2] = 0x03,
151 [Q_KEY_CODE_3] = 0x04,
152 [Q_KEY_CODE_4] = 0x05,
153 [Q_KEY_CODE_5] = 0x06,
154 [Q_KEY_CODE_6] = 0x07,
155 [Q_KEY_CODE_7] = 0x08,
156 [Q_KEY_CODE_8] = 0x09,
157 [Q_KEY_CODE_9] = 0x0a,
158 [Q_KEY_CODE_GRAVE_ACCENT] = 0x29,
159 [Q_KEY_CODE_MINUS] = 0x0c,
160 [Q_KEY_CODE_EQUAL] = 0x0d,
161 [Q_KEY_CODE_BACKSLASH] = 0x2b,
162 [Q_KEY_CODE_BACKSPACE] = 0x0e,
163 [Q_KEY_CODE_SPC] = 0x39,
164 [Q_KEY_CODE_TAB] = 0x0f,
165 [Q_KEY_CODE_CAPS_LOCK] = 0x3a,
166 [Q_KEY_CODE_SHIFT] = 0x2a,
167 [Q_KEY_CODE_CTRL] = 0x1d,
168 [Q_KEY_CODE_META_L] = 0xe05b,
169 [Q_KEY_CODE_ALT] = 0x38,
170 [Q_KEY_CODE_SHIFT_R] = 0x36,
171 [Q_KEY_CODE_CTRL_R] = 0xe01d,
172 [Q_KEY_CODE_META_R] = 0xe05c,
173 [Q_KEY_CODE_ALT_R] = 0xe038,
174 [Q_KEY_CODE_MENU] = 0xe05d,
175 [Q_KEY_CODE_RET] = 0x1c,
176 [Q_KEY_CODE_ESC] = 0x01,
177 [Q_KEY_CODE_F1] = 0x3b,
178 [Q_KEY_CODE_F2] = 0x3c,
179 [Q_KEY_CODE_F3] = 0x3d,
180 [Q_KEY_CODE_F4] = 0x3e,
181 [Q_KEY_CODE_F5] = 0x3f,
182 [Q_KEY_CODE_F6] = 0x40,
183 [Q_KEY_CODE_F7] = 0x41,
184 [Q_KEY_CODE_F8] = 0x42,
185 [Q_KEY_CODE_F9] = 0x43,
186 [Q_KEY_CODE_F10] = 0x44,
187 [Q_KEY_CODE_F11] = 0x57,
188 [Q_KEY_CODE_F12] = 0x58,
189 /* special handling for Q_KEY_CODE_PRINT */
190 [Q_KEY_CODE_SCROLL_LOCK] = 0x46,
191 /* special handling for Q_KEY_CODE_PAUSE */
192 [Q_KEY_CODE_BRACKET_LEFT] = 0x1a,
193 [Q_KEY_CODE_INSERT] = 0xe052,
194 [Q_KEY_CODE_HOME] = 0xe047,
195 [Q_KEY_CODE_PGUP] = 0xe049,
196 [Q_KEY_CODE_DELETE] = 0xe053,
197 [Q_KEY_CODE_END] = 0xe04f,
198 [Q_KEY_CODE_PGDN] = 0xe051,
199 [Q_KEY_CODE_UP] = 0xe048,
200 [Q_KEY_CODE_LEFT] = 0xe04b,
201 [Q_KEY_CODE_DOWN] = 0xe050,
202 [Q_KEY_CODE_RIGHT] = 0xe04d,
203 [Q_KEY_CODE_NUM_LOCK] = 0x45,
204 [Q_KEY_CODE_KP_DIVIDE] = 0xe035,
205 [Q_KEY_CODE_KP_MULTIPLY] = 0x37,
206 [Q_KEY_CODE_KP_SUBTRACT] = 0x4a,
207 [Q_KEY_CODE_KP_ADD] = 0x4e,
208 [Q_KEY_CODE_KP_ENTER] = 0xe01c,
209 [Q_KEY_CODE_KP_DECIMAL] = 0x53,
210 [Q_KEY_CODE_KP_0] = 0x52,
211 [Q_KEY_CODE_KP_1] = 0x4f,
212 [Q_KEY_CODE_KP_2] = 0x50,
213 [Q_KEY_CODE_KP_3] = 0x51,
214 [Q_KEY_CODE_KP_4] = 0x4b,
215 [Q_KEY_CODE_KP_5] = 0x4c,
216 [Q_KEY_CODE_KP_6] = 0x4d,
217 [Q_KEY_CODE_KP_7] = 0x47,
218 [Q_KEY_CODE_KP_8] = 0x48,
219 [Q_KEY_CODE_KP_9] = 0x49,
220 [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b,
221 [Q_KEY_CODE_SEMICOLON] = 0x27,
222 [Q_KEY_CODE_APOSTROPHE] = 0x28,
223 [Q_KEY_CODE_COMMA] = 0x33,
224 [Q_KEY_CODE_DOT] = 0x34,
225 [Q_KEY_CODE_SLASH] = 0x35,
227 [Q_KEY_CODE_POWER] = 0x0e5e,
228 [Q_KEY_CODE_SLEEP] = 0x0e5f,
229 [Q_KEY_CODE_WAKE] = 0x0e63,
231 [Q_KEY_CODE_AUDIONEXT] = 0xe019,
232 [Q_KEY_CODE_AUDIOPREV] = 0xe010,
233 [Q_KEY_CODE_AUDIOSTOP] = 0xe024,
234 [Q_KEY_CODE_AUDIOPLAY] = 0xe022,
235 [Q_KEY_CODE_AUDIOMUTE] = 0xe020,
236 [Q_KEY_CODE_VOLUMEUP] = 0xe030,
237 [Q_KEY_CODE_VOLUMEDOWN] = 0xe02e,
238 [Q_KEY_CODE_MEDIASELECT] = 0xe06d,
239 [Q_KEY_CODE_MAIL] = 0xe06c,
240 [Q_KEY_CODE_CALCULATOR] = 0xe021,
241 [Q_KEY_CODE_COMPUTER] = 0xe06b,
242 [Q_KEY_CODE_AC_SEARCH] = 0xe065,
243 [Q_KEY_CODE_AC_HOME] = 0xe032,
244 [Q_KEY_CODE_AC_BACK] = 0xe06a,
245 [Q_KEY_CODE_AC_FORWARD] = 0xe069,
246 [Q_KEY_CODE_AC_STOP] = 0xe068,
247 [Q_KEY_CODE_AC_REFRESH] = 0xe067,
248 [Q_KEY_CODE_AC_BOOKMARKS] = 0xe066,
250 [Q_KEY_CODE_ASTERISK] = 0x37,
251 [Q_KEY_CODE_LESS] = 0x56,
252 [Q_KEY_CODE_RO] = 0x73,
253 [Q_KEY_CODE_HIRAGANA] = 0x70,
254 [Q_KEY_CODE_HENKAN] = 0x79,
255 [Q_KEY_CODE_YEN] = 0x7d,
256 [Q_KEY_CODE_KP_COMMA] = 0x7e,
259 static const uint16_t qcode_to_keycode_set2[Q_KEY_CODE__MAX] = {
260 [0 ... Q_KEY_CODE__MAX - 1] = 0,
262 [Q_KEY_CODE_A] = 0x1c,
263 [Q_KEY_CODE_B] = 0x32,
264 [Q_KEY_CODE_C] = 0x21,
265 [Q_KEY_CODE_D] = 0x23,
266 [Q_KEY_CODE_E] = 0x24,
267 [Q_KEY_CODE_F] = 0x2b,
268 [Q_KEY_CODE_G] = 0x34,
269 [Q_KEY_CODE_H] = 0x33,
270 [Q_KEY_CODE_I] = 0x43,
271 [Q_KEY_CODE_J] = 0x3b,
272 [Q_KEY_CODE_K] = 0x42,
273 [Q_KEY_CODE_L] = 0x4b,
274 [Q_KEY_CODE_M] = 0x3a,
275 [Q_KEY_CODE_N] = 0x31,
276 [Q_KEY_CODE_O] = 0x44,
277 [Q_KEY_CODE_P] = 0x4d,
278 [Q_KEY_CODE_Q] = 0x15,
279 [Q_KEY_CODE_R] = 0x2d,
280 [Q_KEY_CODE_S] = 0x1b,
281 [Q_KEY_CODE_T] = 0x2c,
282 [Q_KEY_CODE_U] = 0x3c,
283 [Q_KEY_CODE_V] = 0x2a,
284 [Q_KEY_CODE_W] = 0x1d,
285 [Q_KEY_CODE_X] = 0x22,
286 [Q_KEY_CODE_Y] = 0x35,
287 [Q_KEY_CODE_Z] = 0x1a,
288 [Q_KEY_CODE_0] = 0x45,
289 [Q_KEY_CODE_1] = 0x16,
290 [Q_KEY_CODE_2] = 0x1e,
291 [Q_KEY_CODE_3] = 0x26,
292 [Q_KEY_CODE_4] = 0x25,
293 [Q_KEY_CODE_5] = 0x2e,
294 [Q_KEY_CODE_6] = 0x36,
295 [Q_KEY_CODE_7] = 0x3d,
296 [Q_KEY_CODE_8] = 0x3e,
297 [Q_KEY_CODE_9] = 0x46,
298 [Q_KEY_CODE_GRAVE_ACCENT] = 0x0e,
299 [Q_KEY_CODE_MINUS] = 0x4e,
300 [Q_KEY_CODE_EQUAL] = 0x55,
301 [Q_KEY_CODE_BACKSLASH] = 0x5d,
302 [Q_KEY_CODE_BACKSPACE] = 0x66,
303 [Q_KEY_CODE_SPC] = 0x29,
304 [Q_KEY_CODE_TAB] = 0x0d,
305 [Q_KEY_CODE_CAPS_LOCK] = 0x58,
306 [Q_KEY_CODE_SHIFT] = 0x12,
307 [Q_KEY_CODE_CTRL] = 0x14,
308 [Q_KEY_CODE_META_L] = 0xe01f,
309 [Q_KEY_CODE_ALT] = 0x11,
310 [Q_KEY_CODE_SHIFT_R] = 0x59,
311 [Q_KEY_CODE_CTRL_R] = 0xe014,
312 [Q_KEY_CODE_META_R] = 0xe027,
313 [Q_KEY_CODE_ALT_R] = 0xe011,
314 [Q_KEY_CODE_MENU] = 0xe02f,
315 [Q_KEY_CODE_RET] = 0x5a,
316 [Q_KEY_CODE_ESC] = 0x76,
317 [Q_KEY_CODE_F1] = 0x05,
318 [Q_KEY_CODE_F2] = 0x06,
319 [Q_KEY_CODE_F3] = 0x04,
320 [Q_KEY_CODE_F4] = 0x0c,
321 [Q_KEY_CODE_F5] = 0x03,
322 [Q_KEY_CODE_F6] = 0x0b,
323 [Q_KEY_CODE_F7] = 0x83,
324 [Q_KEY_CODE_F8] = 0x0a,
325 [Q_KEY_CODE_F9] = 0x01,
326 [Q_KEY_CODE_F10] = 0x09,
327 [Q_KEY_CODE_F11] = 0x78,
328 [Q_KEY_CODE_F12] = 0x07,
329 /* special handling for Q_KEY_CODE_PRINT */
330 [Q_KEY_CODE_SCROLL_LOCK] = 0x7e,
331 /* special handling for Q_KEY_CODE_PAUSE */
332 [Q_KEY_CODE_BRACKET_LEFT] = 0x54,
333 [Q_KEY_CODE_INSERT] = 0xe070,
334 [Q_KEY_CODE_HOME] = 0xe06c,
335 [Q_KEY_CODE_PGUP] = 0xe07d,
336 [Q_KEY_CODE_DELETE] = 0xe071,
337 [Q_KEY_CODE_END] = 0xe069,
338 [Q_KEY_CODE_PGDN] = 0xe07a,
339 [Q_KEY_CODE_UP] = 0xe075,
340 [Q_KEY_CODE_LEFT] = 0xe06b,
341 [Q_KEY_CODE_DOWN] = 0xe072,
342 [Q_KEY_CODE_RIGHT] = 0xe074,
343 [Q_KEY_CODE_NUM_LOCK] = 0x77,
344 [Q_KEY_CODE_KP_DIVIDE] = 0xe04a,
345 [Q_KEY_CODE_KP_MULTIPLY] = 0x7c,
346 [Q_KEY_CODE_KP_SUBTRACT] = 0x7b,
347 [Q_KEY_CODE_KP_ADD] = 0x79,
348 [Q_KEY_CODE_KP_ENTER] = 0xe05a,
349 [Q_KEY_CODE_KP_DECIMAL] = 0x71,
350 [Q_KEY_CODE_KP_0] = 0x70,
351 [Q_KEY_CODE_KP_1] = 0x69,
352 [Q_KEY_CODE_KP_2] = 0x72,
353 [Q_KEY_CODE_KP_3] = 0x7a,
354 [Q_KEY_CODE_KP_4] = 0x6b,
355 [Q_KEY_CODE_KP_5] = 0x73,
356 [Q_KEY_CODE_KP_6] = 0x74,
357 [Q_KEY_CODE_KP_7] = 0x6c,
358 [Q_KEY_CODE_KP_8] = 0x75,
359 [Q_KEY_CODE_KP_9] = 0x7d,
360 [Q_KEY_CODE_BRACKET_RIGHT] = 0x5b,
361 [Q_KEY_CODE_SEMICOLON] = 0x4c,
362 [Q_KEY_CODE_APOSTROPHE] = 0x52,
363 [Q_KEY_CODE_COMMA] = 0x41,
364 [Q_KEY_CODE_DOT] = 0x49,
365 [Q_KEY_CODE_SLASH] = 0x4a,
367 [Q_KEY_CODE_POWER] = 0x0e37,
368 [Q_KEY_CODE_SLEEP] = 0x0e3f,
369 [Q_KEY_CODE_WAKE] = 0x0e5e,
371 [Q_KEY_CODE_AUDIONEXT] = 0xe04d,
372 [Q_KEY_CODE_AUDIOPREV] = 0xe015,
373 [Q_KEY_CODE_AUDIOSTOP] = 0xe03b,
374 [Q_KEY_CODE_AUDIOPLAY] = 0xe034,
375 [Q_KEY_CODE_AUDIOMUTE] = 0xe023,
376 [Q_KEY_CODE_VOLUMEUP] = 0xe032,
377 [Q_KEY_CODE_VOLUMEDOWN] = 0xe021,
378 [Q_KEY_CODE_MEDIASELECT] = 0xe050,
379 [Q_KEY_CODE_MAIL] = 0xe048,
380 [Q_KEY_CODE_CALCULATOR] = 0xe02b,
381 [Q_KEY_CODE_COMPUTER] = 0xe040,
382 [Q_KEY_CODE_AC_SEARCH] = 0xe010,
383 [Q_KEY_CODE_AC_HOME] = 0xe03a,
384 [Q_KEY_CODE_AC_BACK] = 0xe038,
385 [Q_KEY_CODE_AC_FORWARD] = 0xe030,
386 [Q_KEY_CODE_AC_STOP] = 0xe028,
387 [Q_KEY_CODE_AC_REFRESH] = 0xe020,
388 [Q_KEY_CODE_AC_BOOKMARKS] = 0xe018,
390 [Q_KEY_CODE_ALTGR] = 0x08,
391 [Q_KEY_CODE_ALTGR_R] = 0xe008,
392 [Q_KEY_CODE_ASTERISK] = 0x7c,
393 [Q_KEY_CODE_LESS] = 0x61,
394 [Q_KEY_CODE_SYSRQ] = 0x7f,
395 [Q_KEY_CODE_RO] = 0x51,
396 [Q_KEY_CODE_HIRAGANA] = 0x13,
397 [Q_KEY_CODE_HENKAN] = 0x64,
398 [Q_KEY_CODE_YEN] = 0x6a,
399 [Q_KEY_CODE_KP_COMMA] = 0x6d,
402 static const uint16_t qcode_to_keycode_set3[Q_KEY_CODE__MAX] = {
403 [0 ... Q_KEY_CODE__MAX - 1] = 0,
405 [Q_KEY_CODE_A] = 0x1c,
406 [Q_KEY_CODE_B] = 0x32,
407 [Q_KEY_CODE_C] = 0x21,
408 [Q_KEY_CODE_D] = 0x23,
409 [Q_KEY_CODE_E] = 0x24,
410 [Q_KEY_CODE_F] = 0x2b,
411 [Q_KEY_CODE_G] = 0x34,
412 [Q_KEY_CODE_H] = 0x33,
413 [Q_KEY_CODE_I] = 0x43,
414 [Q_KEY_CODE_J] = 0x3b,
415 [Q_KEY_CODE_K] = 0x42,
416 [Q_KEY_CODE_L] = 0x4b,
417 [Q_KEY_CODE_M] = 0x3a,
418 [Q_KEY_CODE_N] = 0x31,
419 [Q_KEY_CODE_O] = 0x44,
420 [Q_KEY_CODE_P] = 0x4d,
421 [Q_KEY_CODE_Q] = 0x15,
422 [Q_KEY_CODE_R] = 0x2d,
423 [Q_KEY_CODE_S] = 0x1b,
424 [Q_KEY_CODE_T] = 0x2c,
425 [Q_KEY_CODE_U] = 0x3c,
426 [Q_KEY_CODE_V] = 0x2a,
427 [Q_KEY_CODE_W] = 0x1d,
428 [Q_KEY_CODE_X] = 0x22,
429 [Q_KEY_CODE_Y] = 0x35,
430 [Q_KEY_CODE_Z] = 0x1a,
431 [Q_KEY_CODE_0] = 0x45,
432 [Q_KEY_CODE_1] = 0x16,
433 [Q_KEY_CODE_2] = 0x1e,
434 [Q_KEY_CODE_3] = 0x26,
435 [Q_KEY_CODE_4] = 0x25,
436 [Q_KEY_CODE_5] = 0x2e,
437 [Q_KEY_CODE_6] = 0x36,
438 [Q_KEY_CODE_7] = 0x3d,
439 [Q_KEY_CODE_8] = 0x3e,
440 [Q_KEY_CODE_9] = 0x46,
441 [Q_KEY_CODE_GRAVE_ACCENT] = 0x0e,
442 [Q_KEY_CODE_MINUS] = 0x4e,
443 [Q_KEY_CODE_EQUAL] = 0x55,
444 [Q_KEY_CODE_BACKSLASH] = 0x5c,
445 [Q_KEY_CODE_BACKSPACE] = 0x66,
446 [Q_KEY_CODE_SPC] = 0x29,
447 [Q_KEY_CODE_TAB] = 0x0d,
448 [Q_KEY_CODE_CAPS_LOCK] = 0x14,
449 [Q_KEY_CODE_SHIFT] = 0x12,
450 [Q_KEY_CODE_CTRL] = 0x11,
451 [Q_KEY_CODE_META_L] = 0x8b,
452 [Q_KEY_CODE_ALT] = 0x19,
453 [Q_KEY_CODE_SHIFT_R] = 0x59,
454 [Q_KEY_CODE_CTRL_R] = 0x58,
455 [Q_KEY_CODE_META_R] = 0x8c,
456 [Q_KEY_CODE_ALT_R] = 0x39,
457 [Q_KEY_CODE_MENU] = 0x8d,
458 [Q_KEY_CODE_RET] = 0x5a,
459 [Q_KEY_CODE_ESC] = 0x08,
460 [Q_KEY_CODE_F1] = 0x07,
461 [Q_KEY_CODE_F2] = 0x0f,
462 [Q_KEY_CODE_F3] = 0x17,
463 [Q_KEY_CODE_F4] = 0x1f,
464 [Q_KEY_CODE_F5] = 0x27,
465 [Q_KEY_CODE_F6] = 0x2f,
466 [Q_KEY_CODE_F7] = 0x37,
467 [Q_KEY_CODE_F8] = 0x3f,
468 [Q_KEY_CODE_F9] = 0x47,
469 [Q_KEY_CODE_F10] = 0x4f,
470 [Q_KEY_CODE_F11] = 0x56,
471 [Q_KEY_CODE_F12] = 0x5e,
472 [Q_KEY_CODE_PRINT] = 0x57,
473 [Q_KEY_CODE_SCROLL_LOCK] = 0x5f,
474 [Q_KEY_CODE_PAUSE] = 0x62,
475 [Q_KEY_CODE_BRACKET_LEFT] = 0x54,
476 [Q_KEY_CODE_INSERT] = 0x67,
477 [Q_KEY_CODE_HOME] = 0x6e,
478 [Q_KEY_CODE_PGUP] = 0x6f,
479 [Q_KEY_CODE_DELETE] = 0x64,
480 [Q_KEY_CODE_END] = 0x65,
481 [Q_KEY_CODE_PGDN] = 0x6d,
482 [Q_KEY_CODE_UP] = 0x63,
483 [Q_KEY_CODE_LEFT] = 0x61,
484 [Q_KEY_CODE_DOWN] = 0x60,
485 [Q_KEY_CODE_RIGHT] = 0x6a,
486 [Q_KEY_CODE_NUM_LOCK] = 0x76,
487 [Q_KEY_CODE_KP_DIVIDE] = 0x4a,
488 [Q_KEY_CODE_KP_MULTIPLY] = 0x7e,
489 [Q_KEY_CODE_KP_SUBTRACT] = 0x4e,
490 [Q_KEY_CODE_KP_ADD] = 0x7c,
491 [Q_KEY_CODE_KP_ENTER] = 0x79,
492 [Q_KEY_CODE_KP_DECIMAL] = 0x71,
493 [Q_KEY_CODE_KP_0] = 0x70,
494 [Q_KEY_CODE_KP_1] = 0x69,
495 [Q_KEY_CODE_KP_2] = 0x72,
496 [Q_KEY_CODE_KP_3] = 0x7a,
497 [Q_KEY_CODE_KP_4] = 0x6b,
498 [Q_KEY_CODE_KP_5] = 0x73,
499 [Q_KEY_CODE_KP_6] = 0x74,
500 [Q_KEY_CODE_KP_7] = 0x6c,
501 [Q_KEY_CODE_KP_8] = 0x75,
502 [Q_KEY_CODE_KP_9] = 0x7d,
503 [Q_KEY_CODE_BRACKET_RIGHT] = 0x5b,
504 [Q_KEY_CODE_SEMICOLON] = 0x4c,
505 [Q_KEY_CODE_APOSTROPHE] = 0x52,
506 [Q_KEY_CODE_COMMA] = 0x41,
507 [Q_KEY_CODE_DOT] = 0x49,
508 [Q_KEY_CODE_SLASH] = 0x4a,
510 [Q_KEY_CODE_HIRAGANA] = 0x87,
511 [Q_KEY_CODE_HENKAN] = 0x86,
512 [Q_KEY_CODE_YEN] = 0x5d,
515 static uint8_t translate_table[256] = {
516 0xff, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x3c, 0x58,
517 0x64, 0x44, 0x42, 0x40, 0x3e, 0x0f, 0x29, 0x59,
518 0x65, 0x38, 0x2a, 0x70, 0x1d, 0x10, 0x02, 0x5a,
519 0x66, 0x71, 0x2c, 0x1f, 0x1e, 0x11, 0x03, 0x5b,
520 0x67, 0x2e, 0x2d, 0x20, 0x12, 0x05, 0x04, 0x5c,
521 0x68, 0x39, 0x2f, 0x21, 0x14, 0x13, 0x06, 0x5d,
522 0x69, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, 0x5e,
523 0x6a, 0x72, 0x32, 0x24, 0x16, 0x08, 0x09, 0x5f,
524 0x6b, 0x33, 0x25, 0x17, 0x18, 0x0b, 0x0a, 0x60,
525 0x6c, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0c, 0x61,
526 0x6d, 0x73, 0x28, 0x74, 0x1a, 0x0d, 0x62, 0x6e,
527 0x3a, 0x36, 0x1c, 0x1b, 0x75, 0x2b, 0x63, 0x76,
528 0x55, 0x56, 0x77, 0x78, 0x79, 0x7a, 0x0e, 0x7b,
529 0x7c, 0x4f, 0x7d, 0x4b, 0x47, 0x7e, 0x7f, 0x6f,
530 0x52, 0x53, 0x50, 0x4c, 0x4d, 0x48, 0x01, 0x45,
531 0x57, 0x4e, 0x51, 0x4a, 0x37, 0x49, 0x46, 0x54,
532 0x80, 0x81, 0x82, 0x41, 0x54, 0x85, 0x86, 0x87,
533 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
534 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
535 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
536 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
537 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
538 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
539 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
540 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
541 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
542 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
543 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
544 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
545 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
546 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
547 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
550 static void ps2_reset_queue(PS2State *s)
552 PS2Queue *q = &s->queue;
554 q->rptr = 0;
555 q->wptr = 0;
556 q->count = 0;
559 void ps2_queue(PS2State *s, int b)
561 PS2Queue *q = &s->queue;
563 if (q->count >= PS2_QUEUE_SIZE - 1)
564 return;
565 q->data[q->wptr] = b;
566 if (++q->wptr == PS2_QUEUE_SIZE)
567 q->wptr = 0;
568 q->count++;
569 s->update_irq(s->update_arg, 1);
572 /* keycode is the untranslated scancode in the current scancode set. */
573 static void ps2_put_keycode(void *opaque, int keycode)
575 PS2KbdState *s = opaque;
577 trace_ps2_put_keycode(opaque, keycode);
578 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
580 if (s->translate) {
581 if (keycode == 0xf0) {
582 s->need_high_bit = true;
583 } else if (s->need_high_bit) {
584 ps2_queue(&s->common, translate_table[keycode] | 0x80);
585 s->need_high_bit = false;
586 } else {
587 ps2_queue(&s->common, translate_table[keycode]);
589 } else {
590 ps2_queue(&s->common, keycode);
594 static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
595 InputEvent *evt)
597 PS2KbdState *s = (PS2KbdState *)dev;
598 InputKeyEvent *key = evt->u.key.data;
599 int qcode;
600 uint16_t keycode;
602 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
603 assert(evt->type == INPUT_EVENT_KIND_KEY);
604 qcode = qemu_input_key_value_to_qcode(key->key);
606 if (s->scancode_set == 1) {
607 if (qcode == Q_KEY_CODE_PAUSE) {
608 if (key->down) {
609 ps2_put_keycode(s, 0xe1);
610 ps2_put_keycode(s, 0x1d);
611 ps2_put_keycode(s, 0x45);
612 ps2_put_keycode(s, 0x91);
613 ps2_put_keycode(s, 0x9d);
614 ps2_put_keycode(s, 0xc5);
616 } else if (qcode == Q_KEY_CODE_PRINT) {
617 if (key->down) {
618 ps2_put_keycode(s, 0xe0);
619 ps2_put_keycode(s, 0x2a);
620 ps2_put_keycode(s, 0xe0);
621 ps2_put_keycode(s, 0x37);
622 } else {
623 ps2_put_keycode(s, 0xe0);
624 ps2_put_keycode(s, 0xb7);
625 ps2_put_keycode(s, 0xe0);
626 ps2_put_keycode(s, 0xaa);
628 } else {
629 keycode = qcode_to_keycode_set1[qcode];
630 if (keycode) {
631 if (keycode & 0xff00) {
632 ps2_put_keycode(s, keycode >> 8);
634 if (!key->down) {
635 keycode |= 0x80;
637 ps2_put_keycode(s, keycode & 0xff);
638 } else {
639 qemu_log_mask(LOG_UNIMP,
640 "ps2: ignoring key with qcode %d\n", qcode);
643 } else if (s->scancode_set == 2) {
644 if (qcode == Q_KEY_CODE_PAUSE) {
645 if (key->down) {
646 ps2_put_keycode(s, 0xe1);
647 ps2_put_keycode(s, 0x14);
648 ps2_put_keycode(s, 0x77);
649 ps2_put_keycode(s, 0xe1);
650 ps2_put_keycode(s, 0xf0);
651 ps2_put_keycode(s, 0x14);
652 ps2_put_keycode(s, 0xf0);
653 ps2_put_keycode(s, 0x77);
655 } else if (qcode == Q_KEY_CODE_PRINT) {
656 if (key->down) {
657 ps2_put_keycode(s, 0xe0);
658 ps2_put_keycode(s, 0x12);
659 ps2_put_keycode(s, 0xe0);
660 ps2_put_keycode(s, 0x7c);
661 } else {
662 ps2_put_keycode(s, 0xe0);
663 ps2_put_keycode(s, 0xf0);
664 ps2_put_keycode(s, 0x7c);
665 ps2_put_keycode(s, 0xe0);
666 ps2_put_keycode(s, 0xf0);
667 ps2_put_keycode(s, 0x12);
669 } else {
670 keycode = qcode_to_keycode_set2[qcode];
671 if (keycode) {
672 if (keycode & 0xff00) {
673 ps2_put_keycode(s, keycode >> 8);
675 if (!key->down) {
676 ps2_put_keycode(s, 0xf0);
678 ps2_put_keycode(s, keycode & 0xff);
679 } else {
680 qemu_log_mask(LOG_UNIMP,
681 "ps2: ignoring key with qcode %d\n", qcode);
684 } else if (s->scancode_set == 3) {
685 keycode = qcode_to_keycode_set3[qcode];
686 if (keycode) {
687 /* FIXME: break code should be configured on a key by key basis */
688 if (!key->down) {
689 ps2_put_keycode(s, 0xf0);
691 ps2_put_keycode(s, keycode);
692 } else {
693 qemu_log_mask(LOG_UNIMP,
694 "ps2: ignoring key with qcode %d\n", qcode);
699 uint32_t ps2_read_data(PS2State *s)
701 PS2Queue *q;
702 int val, index;
704 trace_ps2_read_data(s);
705 q = &s->queue;
706 if (q->count == 0) {
707 /* NOTE: if no data left, we return the last keyboard one
708 (needed for EMM386) */
709 /* XXX: need a timer to do things correctly */
710 index = q->rptr - 1;
711 if (index < 0)
712 index = PS2_QUEUE_SIZE - 1;
713 val = q->data[index];
714 } else {
715 val = q->data[q->rptr];
716 if (++q->rptr == PS2_QUEUE_SIZE)
717 q->rptr = 0;
718 q->count--;
719 /* reading deasserts IRQ */
720 s->update_irq(s->update_arg, 0);
721 /* reassert IRQs if data left */
722 s->update_irq(s->update_arg, q->count != 0);
724 return val;
727 static void ps2_set_ledstate(PS2KbdState *s, int ledstate)
729 trace_ps2_set_ledstate(s, ledstate);
730 s->ledstate = ledstate;
731 kbd_put_ledstate(ledstate);
734 static void ps2_reset_keyboard(PS2KbdState *s)
736 trace_ps2_reset_keyboard(s);
737 s->scan_enabled = 1;
738 s->scancode_set = 2;
739 ps2_reset_queue(&s->common);
740 ps2_set_ledstate(s, 0);
743 void ps2_write_keyboard(void *opaque, int val)
745 PS2KbdState *s = (PS2KbdState *)opaque;
747 trace_ps2_write_keyboard(opaque, val);
748 switch(s->common.write_cmd) {
749 default:
750 case -1:
751 switch(val) {
752 case 0x00:
753 ps2_queue(&s->common, KBD_REPLY_ACK);
754 break;
755 case 0x05:
756 ps2_queue(&s->common, KBD_REPLY_RESEND);
757 break;
758 case KBD_CMD_GET_ID:
759 ps2_queue(&s->common, KBD_REPLY_ACK);
760 /* We emulate a MF2 AT keyboard here */
761 ps2_queue(&s->common, KBD_REPLY_ID);
762 if (s->translate)
763 ps2_queue(&s->common, 0x41);
764 else
765 ps2_queue(&s->common, 0x83);
766 break;
767 case KBD_CMD_ECHO:
768 ps2_queue(&s->common, KBD_CMD_ECHO);
769 break;
770 case KBD_CMD_ENABLE:
771 s->scan_enabled = 1;
772 ps2_queue(&s->common, KBD_REPLY_ACK);
773 break;
774 case KBD_CMD_SCANCODE:
775 case KBD_CMD_SET_LEDS:
776 case KBD_CMD_SET_RATE:
777 s->common.write_cmd = val;
778 ps2_queue(&s->common, KBD_REPLY_ACK);
779 break;
780 case KBD_CMD_RESET_DISABLE:
781 ps2_reset_keyboard(s);
782 s->scan_enabled = 0;
783 ps2_queue(&s->common, KBD_REPLY_ACK);
784 break;
785 case KBD_CMD_RESET_ENABLE:
786 ps2_reset_keyboard(s);
787 s->scan_enabled = 1;
788 ps2_queue(&s->common, KBD_REPLY_ACK);
789 break;
790 case KBD_CMD_RESET:
791 ps2_reset_keyboard(s);
792 ps2_queue(&s->common, KBD_REPLY_ACK);
793 ps2_queue(&s->common, KBD_REPLY_POR);
794 break;
795 default:
796 ps2_queue(&s->common, KBD_REPLY_RESEND);
797 break;
799 break;
800 case KBD_CMD_SCANCODE:
801 if (val == 0) {
802 ps2_queue(&s->common, KBD_REPLY_ACK);
803 ps2_put_keycode(s, s->scancode_set);
804 } else if (val >= 1 && val <= 3) {
805 s->scancode_set = val;
806 ps2_queue(&s->common, KBD_REPLY_ACK);
807 } else {
808 ps2_queue(&s->common, KBD_REPLY_RESEND);
810 s->common.write_cmd = -1;
811 break;
812 case KBD_CMD_SET_LEDS:
813 ps2_set_ledstate(s, val);
814 ps2_queue(&s->common, KBD_REPLY_ACK);
815 s->common.write_cmd = -1;
816 break;
817 case KBD_CMD_SET_RATE:
818 ps2_queue(&s->common, KBD_REPLY_ACK);
819 s->common.write_cmd = -1;
820 break;
824 /* Set the scancode translation mode.
825 0 = raw scancodes.
826 1 = translated scancodes (used by qemu internally). */
828 void ps2_keyboard_set_translation(void *opaque, int mode)
830 PS2KbdState *s = (PS2KbdState *)opaque;
831 trace_ps2_keyboard_set_translation(opaque, mode);
832 s->translate = mode;
835 static void ps2_mouse_send_packet(PS2MouseState *s)
837 unsigned int b;
838 int dx1, dy1, dz1;
840 dx1 = s->mouse_dx;
841 dy1 = s->mouse_dy;
842 dz1 = s->mouse_dz;
843 /* XXX: increase range to 8 bits ? */
844 if (dx1 > 127)
845 dx1 = 127;
846 else if (dx1 < -127)
847 dx1 = -127;
848 if (dy1 > 127)
849 dy1 = 127;
850 else if (dy1 < -127)
851 dy1 = -127;
852 b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
853 ps2_queue(&s->common, b);
854 ps2_queue(&s->common, dx1 & 0xff);
855 ps2_queue(&s->common, dy1 & 0xff);
856 /* extra byte for IMPS/2 or IMEX */
857 switch(s->mouse_type) {
858 default:
859 break;
860 case 3:
861 if (dz1 > 127)
862 dz1 = 127;
863 else if (dz1 < -127)
864 dz1 = -127;
865 ps2_queue(&s->common, dz1 & 0xff);
866 break;
867 case 4:
868 if (dz1 > 7)
869 dz1 = 7;
870 else if (dz1 < -7)
871 dz1 = -7;
872 b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
873 ps2_queue(&s->common, b);
874 break;
877 trace_ps2_mouse_send_packet(s, dx1, dy1, dz1, b);
878 /* update deltas */
879 s->mouse_dx -= dx1;
880 s->mouse_dy -= dy1;
881 s->mouse_dz -= dz1;
884 static void ps2_mouse_event(DeviceState *dev, QemuConsole *src,
885 InputEvent *evt)
887 static const int bmap[INPUT_BUTTON__MAX] = {
888 [INPUT_BUTTON_LEFT] = PS2_MOUSE_BUTTON_LEFT,
889 [INPUT_BUTTON_MIDDLE] = PS2_MOUSE_BUTTON_MIDDLE,
890 [INPUT_BUTTON_RIGHT] = PS2_MOUSE_BUTTON_RIGHT,
891 [INPUT_BUTTON_SIDE] = PS2_MOUSE_BUTTON_SIDE,
892 [INPUT_BUTTON_EXTRA] = PS2_MOUSE_BUTTON_EXTRA,
894 PS2MouseState *s = (PS2MouseState *)dev;
895 InputMoveEvent *move;
896 InputBtnEvent *btn;
898 /* check if deltas are recorded when disabled */
899 if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
900 return;
902 switch (evt->type) {
903 case INPUT_EVENT_KIND_REL:
904 move = evt->u.rel.data;
905 if (move->axis == INPUT_AXIS_X) {
906 s->mouse_dx += move->value;
907 } else if (move->axis == INPUT_AXIS_Y) {
908 s->mouse_dy -= move->value;
910 break;
912 case INPUT_EVENT_KIND_BTN:
913 btn = evt->u.btn.data;
914 if (btn->down) {
915 s->mouse_buttons |= bmap[btn->button];
916 if (btn->button == INPUT_BUTTON_WHEEL_UP) {
917 s->mouse_dz--;
918 } else if (btn->button == INPUT_BUTTON_WHEEL_DOWN) {
919 s->mouse_dz++;
921 } else {
922 s->mouse_buttons &= ~bmap[btn->button];
924 break;
926 default:
927 /* keep gcc happy */
928 break;
932 static void ps2_mouse_sync(DeviceState *dev)
934 PS2MouseState *s = (PS2MouseState *)dev;
936 if (s->mouse_buttons) {
937 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
939 if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) {
940 while (s->common.queue.count < PS2_QUEUE_SIZE - 4) {
941 /* if not remote, send event. Multiple events are sent if
942 too big deltas */
943 ps2_mouse_send_packet(s);
944 if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
945 break;
950 void ps2_mouse_fake_event(void *opaque)
952 PS2MouseState *s = opaque;
953 trace_ps2_mouse_fake_event(opaque);
954 s->mouse_dx++;
955 ps2_mouse_sync(opaque);
958 void ps2_write_mouse(void *opaque, int val)
960 PS2MouseState *s = (PS2MouseState *)opaque;
962 trace_ps2_write_mouse(opaque, val);
963 #ifdef DEBUG_MOUSE
964 printf("kbd: write mouse 0x%02x\n", val);
965 #endif
966 switch(s->common.write_cmd) {
967 default:
968 case -1:
969 /* mouse command */
970 if (s->mouse_wrap) {
971 if (val == AUX_RESET_WRAP) {
972 s->mouse_wrap = 0;
973 ps2_queue(&s->common, AUX_ACK);
974 return;
975 } else if (val != AUX_RESET) {
976 ps2_queue(&s->common, val);
977 return;
980 switch(val) {
981 case AUX_SET_SCALE11:
982 s->mouse_status &= ~MOUSE_STATUS_SCALE21;
983 ps2_queue(&s->common, AUX_ACK);
984 break;
985 case AUX_SET_SCALE21:
986 s->mouse_status |= MOUSE_STATUS_SCALE21;
987 ps2_queue(&s->common, AUX_ACK);
988 break;
989 case AUX_SET_STREAM:
990 s->mouse_status &= ~MOUSE_STATUS_REMOTE;
991 ps2_queue(&s->common, AUX_ACK);
992 break;
993 case AUX_SET_WRAP:
994 s->mouse_wrap = 1;
995 ps2_queue(&s->common, AUX_ACK);
996 break;
997 case AUX_SET_REMOTE:
998 s->mouse_status |= MOUSE_STATUS_REMOTE;
999 ps2_queue(&s->common, AUX_ACK);
1000 break;
1001 case AUX_GET_TYPE:
1002 ps2_queue(&s->common, AUX_ACK);
1003 ps2_queue(&s->common, s->mouse_type);
1004 break;
1005 case AUX_SET_RES:
1006 case AUX_SET_SAMPLE:
1007 s->common.write_cmd = val;
1008 ps2_queue(&s->common, AUX_ACK);
1009 break;
1010 case AUX_GET_SCALE:
1011 ps2_queue(&s->common, AUX_ACK);
1012 ps2_queue(&s->common, s->mouse_status);
1013 ps2_queue(&s->common, s->mouse_resolution);
1014 ps2_queue(&s->common, s->mouse_sample_rate);
1015 break;
1016 case AUX_POLL:
1017 ps2_queue(&s->common, AUX_ACK);
1018 ps2_mouse_send_packet(s);
1019 break;
1020 case AUX_ENABLE_DEV:
1021 s->mouse_status |= MOUSE_STATUS_ENABLED;
1022 ps2_queue(&s->common, AUX_ACK);
1023 break;
1024 case AUX_DISABLE_DEV:
1025 s->mouse_status &= ~MOUSE_STATUS_ENABLED;
1026 ps2_queue(&s->common, AUX_ACK);
1027 break;
1028 case AUX_SET_DEFAULT:
1029 s->mouse_sample_rate = 100;
1030 s->mouse_resolution = 2;
1031 s->mouse_status = 0;
1032 ps2_queue(&s->common, AUX_ACK);
1033 break;
1034 case AUX_RESET:
1035 s->mouse_sample_rate = 100;
1036 s->mouse_resolution = 2;
1037 s->mouse_status = 0;
1038 s->mouse_type = 0;
1039 ps2_queue(&s->common, AUX_ACK);
1040 ps2_queue(&s->common, 0xaa);
1041 ps2_queue(&s->common, s->mouse_type);
1042 break;
1043 default:
1044 break;
1046 break;
1047 case AUX_SET_SAMPLE:
1048 s->mouse_sample_rate = val;
1049 /* detect IMPS/2 or IMEX */
1050 switch(s->mouse_detect_state) {
1051 default:
1052 case 0:
1053 if (val == 200)
1054 s->mouse_detect_state = 1;
1055 break;
1056 case 1:
1057 if (val == 100)
1058 s->mouse_detect_state = 2;
1059 else if (val == 200)
1060 s->mouse_detect_state = 3;
1061 else
1062 s->mouse_detect_state = 0;
1063 break;
1064 case 2:
1065 if (val == 80)
1066 s->mouse_type = 3; /* IMPS/2 */
1067 s->mouse_detect_state = 0;
1068 break;
1069 case 3:
1070 if (val == 80)
1071 s->mouse_type = 4; /* IMEX */
1072 s->mouse_detect_state = 0;
1073 break;
1075 ps2_queue(&s->common, AUX_ACK);
1076 s->common.write_cmd = -1;
1077 break;
1078 case AUX_SET_RES:
1079 s->mouse_resolution = val;
1080 ps2_queue(&s->common, AUX_ACK);
1081 s->common.write_cmd = -1;
1082 break;
1086 static void ps2_common_reset(PS2State *s)
1088 s->write_cmd = -1;
1089 ps2_reset_queue(s);
1090 s->update_irq(s->update_arg, 0);
1093 static void ps2_common_post_load(PS2State *s)
1095 PS2Queue *q = &s->queue;
1096 int size;
1097 int i;
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 */
1104 if (size > 0) {
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) {
1109 q->rptr = 0;
1112 memcpy(q->data, tmp_data, size);
1114 /* reset rptr/wptr/count */
1115 q->rptr = 0;
1116 q->wptr = size;
1117 q->count = size;
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;
1128 s->translate = 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;
1141 s->mouse_wrap = 0;
1142 s->mouse_type = 0;
1143 s->mouse_detect_state = 0;
1144 s->mouse_dx = 0;
1145 s->mouse_dy = 0;
1146 s->mouse_dz = 0;
1147 s->mouse_buttons = 0;
1150 static const VMStateDescription vmstate_ps2_common = {
1151 .name = "PS2 Common State",
1152 .version_id = 3,
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);
1176 return 0;
1179 static const VMStateDescription vmstate_ps2_keyboard_ledstate = {
1180 .name = "ps2kbd/ledstate",
1181 .version_id = 3,
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",
1199 .version_id = 1,
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)
1214 s->scancode_set=2;
1216 ps2_common_post_load(ps2);
1218 return 0;
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 = {
1230 .name = "ps2kbd",
1231 .version_id = 3,
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,
1245 NULL
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);
1256 return 0;
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 = {
1268 .name = "ps2mouse",
1269 .version_id = 2,
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);
1307 return 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);
1328 return s;