Merge tag 'v2.4.0-rc1'
[qemu/ar7.git] / hw / input / ps2.c
blob7ae0c6a80d2decdefaac2d5d4810847b595bec9d
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 "hw/hw.h"
25 #include "hw/input/ps2.h"
26 #include "ui/console.h"
27 #include "ui/input.h"
28 #include "sysemu/sysemu.h"
30 #include "trace.h"
32 /* debug PC keyboard : only mouse */
33 //#define DEBUG_MOUSE
35 /* Keyboard Commands */
36 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
37 #define KBD_CMD_ECHO 0xEE
38 #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */
39 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
40 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
41 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
42 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
43 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
44 #define KBD_CMD_RESET 0xFF /* Reset */
46 /* Keyboard Replies */
47 #define KBD_REPLY_POR 0xAA /* Power on reset */
48 #define KBD_REPLY_ID 0xAB /* Keyboard ID */
49 #define KBD_REPLY_ACK 0xFA /* Command ACK */
50 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
52 /* Mouse Commands */
53 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
54 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
55 #define AUX_SET_RES 0xE8 /* Set resolution */
56 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
57 #define AUX_SET_STREAM 0xEA /* Set stream mode */
58 #define AUX_POLL 0xEB /* Poll */
59 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
60 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
61 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
62 #define AUX_GET_TYPE 0xF2 /* Get type */
63 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
64 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
65 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
66 #define AUX_SET_DEFAULT 0xF6
67 #define AUX_RESET 0xFF /* Reset aux device */
68 #define AUX_ACK 0xFA /* Command byte ACK. */
70 #define MOUSE_STATUS_REMOTE 0x40
71 #define MOUSE_STATUS_ENABLED 0x20
72 #define MOUSE_STATUS_SCALE21 0x10
74 #define PS2_QUEUE_SIZE 16 /* Buffer size required by PS/2 protocol */
76 typedef struct {
77 /* Keep the data array 256 bytes long, which compatibility
78 with older qemu versions. */
79 uint8_t data[256];
80 int rptr, wptr, count;
81 } PS2Queue;
83 typedef struct {
84 PS2Queue queue;
85 int32_t write_cmd;
86 void (*update_irq)(void *, int);
87 void *update_arg;
88 } PS2State;
90 typedef struct {
91 PS2State common;
92 int scan_enabled;
93 /* QEMU uses translated PC scancodes internally. To avoid multiple
94 conversions we do the translation (if any) in the PS/2 emulation
95 not the keyboard controller. */
96 int translate;
97 int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
98 int ledstate;
99 } PS2KbdState;
101 typedef struct {
102 PS2State common;
103 uint8_t mouse_status;
104 uint8_t mouse_resolution;
105 uint8_t mouse_sample_rate;
106 uint8_t mouse_wrap;
107 uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
108 uint8_t mouse_detect_state;
109 int mouse_dx; /* current values, needed for 'poll' mode */
110 int mouse_dy;
111 int mouse_dz;
112 uint8_t mouse_buttons;
113 } PS2MouseState;
115 /* Table to convert from PC scancodes to raw scancodes. */
116 static const unsigned char ps2_raw_keycode[128] = {
117 0, 118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85, 102, 13,
118 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
119 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
120 50, 49, 58, 65, 73, 74, 89, 124, 17, 41, 88, 5, 6, 4, 12, 3,
121 11, 2, 10, 1, 9, 119, 126, 108, 117, 125, 123, 107, 115, 116, 121, 105,
122 114, 122, 112, 113, 127, 96, 97, 120, 7, 15, 23, 31, 39, 47, 55, 63,
123 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87, 111,
124 19, 25, 57, 81, 83, 92, 95, 98, 99, 100, 101, 103, 104, 106, 109, 110
126 static const unsigned char ps2_raw_keycode_set3[128] = {
127 0, 8, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85, 102, 13,
128 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 17, 28, 27,
129 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 92, 26, 34, 33, 42,
130 50, 49, 58, 65, 73, 74, 89, 126, 25, 41, 20, 7, 15, 23, 31, 39,
131 47, 2, 63, 71, 79, 118, 95, 108, 117, 125, 132, 107, 115, 116, 124, 105,
132 114, 122, 112, 113, 127, 96, 97, 86, 94, 15, 23, 31, 39, 47, 55, 63,
133 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87, 111,
134 19, 25, 57, 81, 83, 92, 95, 98, 99, 100, 101, 103, 104, 106, 109, 110
137 void ps2_queue(void *opaque, int b)
139 PS2State *s = (PS2State *)opaque;
140 PS2Queue *q = &s->queue;
142 if (q->count >= PS2_QUEUE_SIZE - 1)
143 return;
144 q->data[q->wptr] = b;
145 if (++q->wptr == PS2_QUEUE_SIZE)
146 q->wptr = 0;
147 q->count++;
148 s->update_irq(s->update_arg, 1);
152 keycode is expressed as follow:
153 bit 7 - 0 key pressed, 1 = key released
154 bits 6-0 - translated scancode set 2
156 static void ps2_put_keycode(void *opaque, int keycode)
158 PS2KbdState *s = opaque;
160 trace_ps2_put_keycode(opaque, keycode);
161 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
162 /* XXX: add support for scancode set 1 */
163 if (!s->translate && keycode < 0xe0 && s->scancode_set > 1) {
164 if (keycode & 0x80) {
165 ps2_queue(&s->common, 0xf0);
167 if (s->scancode_set == 2) {
168 keycode = ps2_raw_keycode[keycode & 0x7f];
169 } else if (s->scancode_set == 3) {
170 keycode = ps2_raw_keycode_set3[keycode & 0x7f];
173 ps2_queue(&s->common, keycode);
176 static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
177 InputEvent *evt)
179 PS2KbdState *s = (PS2KbdState *)dev;
180 int scancodes[3], i, count;
182 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
183 count = qemu_input_key_value_to_scancode(evt->key->key,
184 evt->key->down,
185 scancodes);
186 for (i = 0; i < count; i++) {
187 ps2_put_keycode(s, scancodes[i]);
191 uint32_t ps2_read_data(void *opaque)
193 PS2State *s = (PS2State *)opaque;
194 PS2Queue *q;
195 int val, index;
197 trace_ps2_read_data(opaque);
198 q = &s->queue;
199 if (q->count == 0) {
200 /* NOTE: if no data left, we return the last keyboard one
201 (needed for EMM386) */
202 /* XXX: need a timer to do things correctly */
203 index = q->rptr - 1;
204 if (index < 0)
205 index = PS2_QUEUE_SIZE - 1;
206 val = q->data[index];
207 } else {
208 val = q->data[q->rptr];
209 if (++q->rptr == PS2_QUEUE_SIZE)
210 q->rptr = 0;
211 q->count--;
212 /* reading deasserts IRQ */
213 s->update_irq(s->update_arg, 0);
214 /* reassert IRQs if data left */
215 s->update_irq(s->update_arg, q->count != 0);
217 return val;
220 static void ps2_set_ledstate(PS2KbdState *s, int ledstate)
222 trace_ps2_set_ledstate(s, ledstate);
223 s->ledstate = ledstate;
224 kbd_put_ledstate(ledstate);
227 static void ps2_reset_keyboard(PS2KbdState *s)
229 trace_ps2_reset_keyboard(s);
230 s->scan_enabled = 1;
231 s->scancode_set = 2;
232 ps2_set_ledstate(s, 0);
235 void ps2_write_keyboard(void *opaque, int val)
237 PS2KbdState *s = (PS2KbdState *)opaque;
239 trace_ps2_write_keyboard(opaque, val);
240 switch(s->common.write_cmd) {
241 default:
242 case -1:
243 switch(val) {
244 case 0x00:
245 ps2_queue(&s->common, KBD_REPLY_ACK);
246 break;
247 case 0x05:
248 ps2_queue(&s->common, KBD_REPLY_RESEND);
249 break;
250 case KBD_CMD_GET_ID:
251 ps2_queue(&s->common, KBD_REPLY_ACK);
252 /* We emulate a MF2 AT keyboard here */
253 ps2_queue(&s->common, KBD_REPLY_ID);
254 if (s->translate)
255 ps2_queue(&s->common, 0x41);
256 else
257 ps2_queue(&s->common, 0x83);
258 break;
259 case KBD_CMD_ECHO:
260 ps2_queue(&s->common, KBD_CMD_ECHO);
261 break;
262 case KBD_CMD_ENABLE:
263 s->scan_enabled = 1;
264 ps2_queue(&s->common, KBD_REPLY_ACK);
265 break;
266 case KBD_CMD_SCANCODE:
267 case KBD_CMD_SET_LEDS:
268 case KBD_CMD_SET_RATE:
269 s->common.write_cmd = val;
270 ps2_queue(&s->common, KBD_REPLY_ACK);
271 break;
272 case KBD_CMD_RESET_DISABLE:
273 ps2_reset_keyboard(s);
274 s->scan_enabled = 0;
275 ps2_queue(&s->common, KBD_REPLY_ACK);
276 break;
277 case KBD_CMD_RESET_ENABLE:
278 ps2_reset_keyboard(s);
279 s->scan_enabled = 1;
280 ps2_queue(&s->common, KBD_REPLY_ACK);
281 break;
282 case KBD_CMD_RESET:
283 ps2_reset_keyboard(s);
284 ps2_queue(&s->common, KBD_REPLY_ACK);
285 ps2_queue(&s->common, KBD_REPLY_POR);
286 break;
287 default:
288 ps2_queue(&s->common, KBD_REPLY_ACK);
289 break;
291 break;
292 case KBD_CMD_SCANCODE:
293 if (val == 0) {
294 if (s->scancode_set == 1)
295 ps2_put_keycode(s, 0x43);
296 else if (s->scancode_set == 2)
297 ps2_put_keycode(s, 0x41);
298 else if (s->scancode_set == 3)
299 ps2_put_keycode(s, 0x3f);
300 } else {
301 if (val >= 1 && val <= 3)
302 s->scancode_set = val;
303 ps2_queue(&s->common, KBD_REPLY_ACK);
305 s->common.write_cmd = -1;
306 break;
307 case KBD_CMD_SET_LEDS:
308 ps2_set_ledstate(s, val);
309 ps2_queue(&s->common, KBD_REPLY_ACK);
310 s->common.write_cmd = -1;
311 break;
312 case KBD_CMD_SET_RATE:
313 ps2_queue(&s->common, KBD_REPLY_ACK);
314 s->common.write_cmd = -1;
315 break;
319 /* Set the scancode translation mode.
320 0 = raw scancodes.
321 1 = translated scancodes (used by qemu internally). */
323 void ps2_keyboard_set_translation(void *opaque, int mode)
325 PS2KbdState *s = (PS2KbdState *)opaque;
326 trace_ps2_keyboard_set_translation(opaque, mode);
327 s->translate = mode;
330 static void ps2_mouse_send_packet(PS2MouseState *s)
332 unsigned int b;
333 int dx1, dy1, dz1;
335 dx1 = s->mouse_dx;
336 dy1 = s->mouse_dy;
337 dz1 = s->mouse_dz;
338 /* XXX: increase range to 8 bits ? */
339 if (dx1 > 127)
340 dx1 = 127;
341 else if (dx1 < -127)
342 dx1 = -127;
343 if (dy1 > 127)
344 dy1 = 127;
345 else if (dy1 < -127)
346 dy1 = -127;
347 b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
348 ps2_queue(&s->common, b);
349 ps2_queue(&s->common, dx1 & 0xff);
350 ps2_queue(&s->common, dy1 & 0xff);
351 /* extra byte for IMPS/2 or IMEX */
352 switch(s->mouse_type) {
353 default:
354 break;
355 case 3:
356 if (dz1 > 127)
357 dz1 = 127;
358 else if (dz1 < -127)
359 dz1 = -127;
360 ps2_queue(&s->common, dz1 & 0xff);
361 break;
362 case 4:
363 if (dz1 > 7)
364 dz1 = 7;
365 else if (dz1 < -7)
366 dz1 = -7;
367 b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
368 ps2_queue(&s->common, b);
369 break;
372 trace_ps2_mouse_send_packet(s, dx1, dy1, dz1, b);
373 /* update deltas */
374 s->mouse_dx -= dx1;
375 s->mouse_dy -= dy1;
376 s->mouse_dz -= dz1;
379 static void ps2_mouse_event(DeviceState *dev, QemuConsole *src,
380 InputEvent *evt)
382 static const int bmap[INPUT_BUTTON_MAX] = {
383 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
384 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
385 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
387 PS2MouseState *s = (PS2MouseState *)dev;
389 /* check if deltas are recorded when disabled */
390 if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
391 return;
393 switch (evt->kind) {
394 case INPUT_EVENT_KIND_REL:
395 if (evt->rel->axis == INPUT_AXIS_X) {
396 s->mouse_dx += evt->rel->value;
397 } else if (evt->rel->axis == INPUT_AXIS_Y) {
398 s->mouse_dy -= evt->rel->value;
400 break;
402 case INPUT_EVENT_KIND_BTN:
403 if (evt->btn->down) {
404 s->mouse_buttons |= bmap[evt->btn->button];
405 if (evt->btn->button == INPUT_BUTTON_WHEEL_UP) {
406 s->mouse_dz--;
407 } else if (evt->btn->button == INPUT_BUTTON_WHEEL_DOWN) {
408 s->mouse_dz++;
410 } else {
411 s->mouse_buttons &= ~bmap[evt->btn->button];
413 break;
415 default:
416 /* keep gcc happy */
417 break;
421 static void ps2_mouse_sync(DeviceState *dev)
423 PS2MouseState *s = (PS2MouseState *)dev;
425 if (s->mouse_buttons) {
426 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
428 if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) {
429 while (s->common.queue.count < PS2_QUEUE_SIZE - 4) {
430 /* if not remote, send event. Multiple events are sent if
431 too big deltas */
432 ps2_mouse_send_packet(s);
433 if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
434 break;
439 void ps2_mouse_fake_event(void *opaque)
441 PS2MouseState *s = opaque;
442 trace_ps2_mouse_fake_event(opaque);
443 s->mouse_dx++;
444 ps2_mouse_sync(opaque);
447 void ps2_write_mouse(void *opaque, int val)
449 PS2MouseState *s = (PS2MouseState *)opaque;
451 trace_ps2_write_mouse(opaque, val);
452 #ifdef DEBUG_MOUSE
453 printf("kbd: write mouse 0x%02x\n", val);
454 #endif
455 switch(s->common.write_cmd) {
456 default:
457 case -1:
458 /* mouse command */
459 if (s->mouse_wrap) {
460 if (val == AUX_RESET_WRAP) {
461 s->mouse_wrap = 0;
462 ps2_queue(&s->common, AUX_ACK);
463 return;
464 } else if (val != AUX_RESET) {
465 ps2_queue(&s->common, val);
466 return;
469 switch(val) {
470 case AUX_SET_SCALE11:
471 s->mouse_status &= ~MOUSE_STATUS_SCALE21;
472 ps2_queue(&s->common, AUX_ACK);
473 break;
474 case AUX_SET_SCALE21:
475 s->mouse_status |= MOUSE_STATUS_SCALE21;
476 ps2_queue(&s->common, AUX_ACK);
477 break;
478 case AUX_SET_STREAM:
479 s->mouse_status &= ~MOUSE_STATUS_REMOTE;
480 ps2_queue(&s->common, AUX_ACK);
481 break;
482 case AUX_SET_WRAP:
483 s->mouse_wrap = 1;
484 ps2_queue(&s->common, AUX_ACK);
485 break;
486 case AUX_SET_REMOTE:
487 s->mouse_status |= MOUSE_STATUS_REMOTE;
488 ps2_queue(&s->common, AUX_ACK);
489 break;
490 case AUX_GET_TYPE:
491 ps2_queue(&s->common, AUX_ACK);
492 ps2_queue(&s->common, s->mouse_type);
493 break;
494 case AUX_SET_RES:
495 case AUX_SET_SAMPLE:
496 s->common.write_cmd = val;
497 ps2_queue(&s->common, AUX_ACK);
498 break;
499 case AUX_GET_SCALE:
500 ps2_queue(&s->common, AUX_ACK);
501 ps2_queue(&s->common, s->mouse_status);
502 ps2_queue(&s->common, s->mouse_resolution);
503 ps2_queue(&s->common, s->mouse_sample_rate);
504 break;
505 case AUX_POLL:
506 ps2_queue(&s->common, AUX_ACK);
507 ps2_mouse_send_packet(s);
508 break;
509 case AUX_ENABLE_DEV:
510 s->mouse_status |= MOUSE_STATUS_ENABLED;
511 ps2_queue(&s->common, AUX_ACK);
512 break;
513 case AUX_DISABLE_DEV:
514 s->mouse_status &= ~MOUSE_STATUS_ENABLED;
515 ps2_queue(&s->common, AUX_ACK);
516 break;
517 case AUX_SET_DEFAULT:
518 s->mouse_sample_rate = 100;
519 s->mouse_resolution = 2;
520 s->mouse_status = 0;
521 ps2_queue(&s->common, AUX_ACK);
522 break;
523 case AUX_RESET:
524 s->mouse_sample_rate = 100;
525 s->mouse_resolution = 2;
526 s->mouse_status = 0;
527 s->mouse_type = 0;
528 ps2_queue(&s->common, AUX_ACK);
529 ps2_queue(&s->common, 0xaa);
530 ps2_queue(&s->common, s->mouse_type);
531 break;
532 default:
533 break;
535 break;
536 case AUX_SET_SAMPLE:
537 s->mouse_sample_rate = val;
538 /* detect IMPS/2 or IMEX */
539 switch(s->mouse_detect_state) {
540 default:
541 case 0:
542 if (val == 200)
543 s->mouse_detect_state = 1;
544 break;
545 case 1:
546 if (val == 100)
547 s->mouse_detect_state = 2;
548 else if (val == 200)
549 s->mouse_detect_state = 3;
550 else
551 s->mouse_detect_state = 0;
552 break;
553 case 2:
554 if (val == 80)
555 s->mouse_type = 3; /* IMPS/2 */
556 s->mouse_detect_state = 0;
557 break;
558 case 3:
559 if (val == 80)
560 s->mouse_type = 4; /* IMEX */
561 s->mouse_detect_state = 0;
562 break;
564 ps2_queue(&s->common, AUX_ACK);
565 s->common.write_cmd = -1;
566 break;
567 case AUX_SET_RES:
568 s->mouse_resolution = val;
569 ps2_queue(&s->common, AUX_ACK);
570 s->common.write_cmd = -1;
571 break;
575 static void ps2_common_reset(PS2State *s)
577 PS2Queue *q;
578 s->write_cmd = -1;
579 q = &s->queue;
580 q->rptr = 0;
581 q->wptr = 0;
582 q->count = 0;
583 s->update_irq(s->update_arg, 0);
586 static void ps2_common_post_load(PS2State *s)
588 PS2Queue *q = &s->queue;
589 int size;
590 int i;
591 int tmp_data[PS2_QUEUE_SIZE];
593 /* set the useful data buffer queue size, < PS2_QUEUE_SIZE */
594 size = q->count > PS2_QUEUE_SIZE ? 0 : q->count;
596 /* move the queue elements to the start of data array */
597 if (size > 0) {
598 for (i = 0; i < size; i++) {
599 /* move the queue elements to the temporary buffer */
600 tmp_data[i] = q->data[q->rptr];
601 if (++q->rptr == 256) {
602 q->rptr = 0;
605 memcpy(q->data, tmp_data, size);
607 /* reset rptr/wptr/count */
608 q->rptr = 0;
609 q->wptr = size;
610 q->count = size;
611 s->update_irq(s->update_arg, q->count != 0);
614 static void ps2_kbd_reset(void *opaque)
616 PS2KbdState *s = (PS2KbdState *) opaque;
618 trace_ps2_kbd_reset(opaque);
619 ps2_common_reset(&s->common);
620 s->scan_enabled = 0;
621 s->translate = 0;
622 s->scancode_set = 0;
625 static void ps2_mouse_reset(void *opaque)
627 PS2MouseState *s = (PS2MouseState *) opaque;
629 trace_ps2_mouse_reset(opaque);
630 ps2_common_reset(&s->common);
631 s->mouse_status = 0;
632 s->mouse_resolution = 0;
633 s->mouse_sample_rate = 0;
634 s->mouse_wrap = 0;
635 s->mouse_type = 0;
636 s->mouse_detect_state = 0;
637 s->mouse_dx = 0;
638 s->mouse_dy = 0;
639 s->mouse_dz = 0;
640 s->mouse_buttons = 0;
643 static const VMStateDescription vmstate_ps2_common = {
644 .name = "PS2 Common State",
645 .version_id = 3,
646 .minimum_version_id = 2,
647 .fields = (VMStateField[]) {
648 VMSTATE_INT32(write_cmd, PS2State),
649 VMSTATE_INT32(queue.rptr, PS2State),
650 VMSTATE_INT32(queue.wptr, PS2State),
651 VMSTATE_INT32(queue.count, PS2State),
652 VMSTATE_BUFFER(queue.data, PS2State),
653 VMSTATE_END_OF_LIST()
657 static bool ps2_keyboard_ledstate_needed(void *opaque)
659 PS2KbdState *s = opaque;
661 return s->ledstate != 0; /* 0 is default state */
664 static int ps2_kbd_ledstate_post_load(void *opaque, int version_id)
666 PS2KbdState *s = opaque;
668 kbd_put_ledstate(s->ledstate);
669 return 0;
672 static const VMStateDescription vmstate_ps2_keyboard_ledstate = {
673 .name = "ps2kbd/ledstate",
674 .version_id = 3,
675 .minimum_version_id = 2,
676 .post_load = ps2_kbd_ledstate_post_load,
677 .needed = ps2_keyboard_ledstate_needed,
678 .fields = (VMStateField[]) {
679 VMSTATE_INT32(ledstate, PS2KbdState),
680 VMSTATE_END_OF_LIST()
684 static int ps2_kbd_post_load(void* opaque, int version_id)
686 PS2KbdState *s = (PS2KbdState*)opaque;
687 PS2State *ps2 = &s->common;
689 if (version_id == 2)
690 s->scancode_set=2;
692 ps2_common_post_load(ps2);
694 return 0;
697 static void ps2_kbd_pre_save(void *opaque)
699 PS2KbdState *s = (PS2KbdState *)opaque;
700 PS2State *ps2 = &s->common;
702 ps2_common_post_load(ps2);
705 static const VMStateDescription vmstate_ps2_keyboard = {
706 .name = "ps2kbd",
707 .version_id = 3,
708 .minimum_version_id = 2,
709 .post_load = ps2_kbd_post_load,
710 .pre_save = ps2_kbd_pre_save,
711 .fields = (VMStateField[]) {
712 VMSTATE_STRUCT(common, PS2KbdState, 0, vmstate_ps2_common, PS2State),
713 VMSTATE_INT32(scan_enabled, PS2KbdState),
714 VMSTATE_INT32(translate, PS2KbdState),
715 VMSTATE_INT32_V(scancode_set, PS2KbdState,3),
716 VMSTATE_END_OF_LIST()
718 .subsections = (const VMStateDescription*[]) {
719 &vmstate_ps2_keyboard_ledstate,
720 NULL
724 static int ps2_mouse_post_load(void *opaque, int version_id)
726 PS2MouseState *s = (PS2MouseState *)opaque;
727 PS2State *ps2 = &s->common;
729 ps2_common_post_load(ps2);
731 return 0;
734 static void ps2_mouse_pre_save(void *opaque)
736 PS2MouseState *s = (PS2MouseState *)opaque;
737 PS2State *ps2 = &s->common;
739 ps2_common_post_load(ps2);
742 static const VMStateDescription vmstate_ps2_mouse = {
743 .name = "ps2mouse",
744 .version_id = 2,
745 .minimum_version_id = 2,
746 .post_load = ps2_mouse_post_load,
747 .pre_save = ps2_mouse_pre_save,
748 .fields = (VMStateField[]) {
749 VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
750 VMSTATE_UINT8(mouse_status, PS2MouseState),
751 VMSTATE_UINT8(mouse_resolution, PS2MouseState),
752 VMSTATE_UINT8(mouse_sample_rate, PS2MouseState),
753 VMSTATE_UINT8(mouse_wrap, PS2MouseState),
754 VMSTATE_UINT8(mouse_type, PS2MouseState),
755 VMSTATE_UINT8(mouse_detect_state, PS2MouseState),
756 VMSTATE_INT32(mouse_dx, PS2MouseState),
757 VMSTATE_INT32(mouse_dy, PS2MouseState),
758 VMSTATE_INT32(mouse_dz, PS2MouseState),
759 VMSTATE_UINT8(mouse_buttons, PS2MouseState),
760 VMSTATE_END_OF_LIST()
764 static QemuInputHandler ps2_keyboard_handler = {
765 .name = "QEMU PS/2 Keyboard",
766 .mask = INPUT_EVENT_MASK_KEY,
767 .event = ps2_keyboard_event,
770 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
772 PS2KbdState *s = (PS2KbdState *)g_malloc0(sizeof(PS2KbdState));
774 trace_ps2_kbd_init(s);
775 s->common.update_irq = update_irq;
776 s->common.update_arg = update_arg;
777 s->translate = 1;
778 s->scancode_set = 2;
779 vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
780 qemu_input_handler_register((DeviceState *)s,
781 &ps2_keyboard_handler);
782 qemu_register_reset(ps2_kbd_reset, s);
783 return s;
786 static QemuInputHandler ps2_mouse_handler = {
787 .name = "QEMU PS/2 Mouse",
788 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
789 .event = ps2_mouse_event,
790 .sync = ps2_mouse_sync,
793 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
795 PS2MouseState *s = (PS2MouseState *)g_malloc0(sizeof(PS2MouseState));
797 trace_ps2_mouse_init(s);
798 s->common.update_irq = update_irq;
799 s->common.update_arg = update_arg;
800 vmstate_register(NULL, 0, &vmstate_ps2_mouse, s);
801 qemu_input_handler_register((DeviceState *)s,
802 &ps2_mouse_handler);
803 qemu_register_reset(ps2_mouse_reset, s);
804 return s;