ecp: merge develop branch into ecp branch
[sdk/emulator/qemu.git] / hw / input / ps2.c
1 /*
2  * QEMU PS/2 keyboard/mouse emulation
3  *
4  * Copyright (c) 2003 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 #include "hw/hw.h"
25 #include "hw/input/ps2.h"
26 #include "ui/console.h"
27 #include "sysemu/sysemu.h"
28
29 #ifdef CONFIG_MARU
30 /* to guarantee safe serialization of input event by Munkyu Im */
31 #include "qemu/thread.h"
32 static QemuMutex mutex;
33 #endif
34 /* debug PC keyboard */
35 //#define DEBUG_KBD
36
37 /* debug PC keyboard : only mouse */
38 //#define DEBUG_MOUSE
39
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 */
50
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 */
56
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. */
74
75 #define MOUSE_STATUS_REMOTE     0x40
76 #define MOUSE_STATUS_ENABLED    0x20
77 #define MOUSE_STATUS_SCALE21    0x10
78
79 #define PS2_QUEUE_SIZE 256
80
81 typedef struct {
82     uint8_t data[PS2_QUEUE_SIZE];
83     int rptr, wptr, count;
84 } PS2Queue;
85
86 typedef struct {
87     PS2Queue queue;
88     int32_t write_cmd;
89     void (*update_irq)(void *, int);
90     void *update_arg;
91 } PS2State;
92
93 typedef struct {
94     PS2State common;
95     int scan_enabled;
96     /* QEMU uses translated PC scancodes internally.  To avoid multiple
97        conversions we do the translation (if any) in the PS/2 emulation
98        not the keyboard controller.  */
99     int translate;
100     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
101     int ledstate;
102 } PS2KbdState;
103
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;
117
118 /* Table to convert from PC scancodes to raw scancodes.  */
119 static const unsigned char ps2_raw_keycode[128] = {
120   0, 118,  22,  30,  38,  37,  46,  54,  61,  62,  70,  69,  78,  85, 102,  13,
121  21,  29,  36,  45,  44,  53,  60,  67,  68,  77,  84,  91,  90,  20,  28,  27,
122  35,  43,  52,  51,  59,  66,  75,  76,  82,  14,  18,  93,  26,  34,  33,  42,
123  50,  49,  58,  65,  73,  74,  89, 124,  17,  41,  88,   5,   6,   4,  12,   3,
124  11,   2,  10,   1,   9, 119, 126, 108, 117, 125, 123, 107, 115, 116, 121, 105,
125 114, 122, 112, 113, 127,  96,  97, 120,   7,  15,  23,  31,  39,  47,  55,  63,
126  71,  79,  86,  94,   8,  16,  24,  32,  40,  48,  56,  64,  72,  80,  87, 111,
127  19,  25,  57,  81,  83,  92,  95,  98,  99, 100, 101, 103, 104, 106, 109, 110
128 };
129 static const unsigned char ps2_raw_keycode_set3[128] = {
130   0,   8,  22,  30,  38,  37,  46,  54,  61,  62,  70,  69,  78,  85, 102,  13,
131  21,  29,  36,  45,  44,  53,  60,  67,  68,  77,  84,  91,  90,  17,  28,  27,
132  35,  43,  52,  51,  59,  66,  75,  76,  82,  14,  18,  92,  26,  34,  33,  42,
133  50,  49,  58,  65,  73,  74,  89, 126,  25,  41,  20,   7,  15,  23,  31,  39,
134  47,   2,  63,  71,  79, 118,  95, 108, 117, 125, 132, 107, 115, 116, 124, 105,
135 114, 122, 112, 113, 127,  96,  97,  86,  94,  15,  23,  31,  39,  47,  55,  63,
136  71,  79,  86,  94,   8,  16,  24,  32,  40,  48,  56,  64,  72,  80,  87, 111,
137  19,  25,  57,  81,  83,  92,  95,  98,  99, 100, 101, 103, 104, 106, 109, 110
138 };
139
140 void ps2_queue(void *opaque, int b)
141 {
142     PS2State *s = (PS2State *)opaque;
143     PS2Queue *q = &s->queue;
144
145     if (q->count >= PS2_QUEUE_SIZE)
146         return;
147     q->data[q->wptr] = b;
148     if (++q->wptr == PS2_QUEUE_SIZE)
149         q->wptr = 0;
150     q->count++;
151     s->update_irq(s->update_arg, 1);
152 }
153
154 /*
155    keycode is expressed as follow:
156    bit 7    - 0 key pressed, 1 = key released
157    bits 6-0 - translated scancode set 2
158  */
159 static void ps2_put_keycode(void *opaque, int keycode)
160 {
161     PS2KbdState *s = opaque;
162
163     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
164     /* XXX: add support for scancode set 1 */
165     if (!s->translate && keycode < 0xe0 && s->scancode_set > 1) {
166         if (keycode & 0x80) {
167             ps2_queue(&s->common, 0xf0);
168         }
169         if (s->scancode_set == 2) {
170             keycode = ps2_raw_keycode[keycode & 0x7f];
171         } else if (s->scancode_set == 3) {
172             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
173         }
174       }
175     ps2_queue(&s->common, keycode);
176 }
177
178 uint32_t ps2_read_data(void *opaque)
179 {
180     PS2State *s = (PS2State *)opaque;
181     PS2Queue *q;
182     int val, index;
183
184     q = &s->queue;
185     if (q->count == 0) {
186         /* NOTE: if no data left, we return the last keyboard one
187            (needed for EMM386) */
188         /* XXX: need a timer to do things correctly */
189         index = q->rptr - 1;
190         if (index < 0)
191             index = PS2_QUEUE_SIZE - 1;
192         val = q->data[index];
193     } else {
194         val = q->data[q->rptr];
195         if (++q->rptr == PS2_QUEUE_SIZE)
196             q->rptr = 0;
197         q->count--;
198         /* reading deasserts IRQ */
199         s->update_irq(s->update_arg, 0);
200         /* reassert IRQs if data left */
201         s->update_irq(s->update_arg, q->count != 0);
202     }
203     return val;
204 }
205
206 static void ps2_set_ledstate(PS2KbdState *s, int ledstate)
207 {
208     s->ledstate = ledstate;
209     kbd_put_ledstate(ledstate);
210 }
211
212 static void ps2_reset_keyboard(PS2KbdState *s)
213 {
214     s->scan_enabled = 1;
215     s->scancode_set = 2;
216     ps2_set_ledstate(s, 0);
217 }
218
219 void ps2_write_keyboard(void *opaque, int val)
220 {
221     PS2KbdState *s = (PS2KbdState *)opaque;
222
223     switch(s->common.write_cmd) {
224     default:
225     case -1:
226         switch(val) {
227         case 0x00:
228             ps2_queue(&s->common, KBD_REPLY_ACK);
229             break;
230         case 0x05:
231             ps2_queue(&s->common, KBD_REPLY_RESEND);
232             break;
233         case KBD_CMD_GET_ID:
234             ps2_queue(&s->common, KBD_REPLY_ACK);
235             /* We emulate a MF2 AT keyboard here */
236             ps2_queue(&s->common, KBD_REPLY_ID);
237             if (s->translate)
238                 ps2_queue(&s->common, 0x41);
239             else
240                 ps2_queue(&s->common, 0x83);
241             break;
242         case KBD_CMD_ECHO:
243             ps2_queue(&s->common, KBD_CMD_ECHO);
244             break;
245         case KBD_CMD_ENABLE:
246             s->scan_enabled = 1;
247             ps2_queue(&s->common, KBD_REPLY_ACK);
248             break;
249         case KBD_CMD_SCANCODE:
250         case KBD_CMD_SET_LEDS:
251         case KBD_CMD_SET_RATE:
252             s->common.write_cmd = val;
253             ps2_queue(&s->common, KBD_REPLY_ACK);
254             break;
255         case KBD_CMD_RESET_DISABLE:
256             ps2_reset_keyboard(s);
257             s->scan_enabled = 0;
258             ps2_queue(&s->common, KBD_REPLY_ACK);
259             break;
260         case KBD_CMD_RESET_ENABLE:
261             ps2_reset_keyboard(s);
262             s->scan_enabled = 1;
263             ps2_queue(&s->common, KBD_REPLY_ACK);
264             break;
265         case KBD_CMD_RESET:
266             ps2_reset_keyboard(s);
267             ps2_queue(&s->common, KBD_REPLY_ACK);
268             ps2_queue(&s->common, KBD_REPLY_POR);
269             break;
270         default:
271             ps2_queue(&s->common, KBD_REPLY_ACK);
272             break;
273         }
274         break;
275     case KBD_CMD_SCANCODE:
276         if (val == 0) {
277             if (s->scancode_set == 1)
278                 ps2_put_keycode(s, 0x43);
279             else if (s->scancode_set == 2)
280                 ps2_put_keycode(s, 0x41);
281             else if (s->scancode_set == 3)
282                 ps2_put_keycode(s, 0x3f);
283         } else {
284             if (val >= 1 && val <= 3)
285                 s->scancode_set = val;
286             ps2_queue(&s->common, KBD_REPLY_ACK);
287         }
288         s->common.write_cmd = -1;
289         break;
290     case KBD_CMD_SET_LEDS:
291         ps2_set_ledstate(s, val);
292         ps2_queue(&s->common, KBD_REPLY_ACK);
293         s->common.write_cmd = -1;
294         break;
295     case KBD_CMD_SET_RATE:
296         ps2_queue(&s->common, KBD_REPLY_ACK);
297         s->common.write_cmd = -1;
298         break;
299     }
300 }
301
302 /* Set the scancode translation mode.
303    0 = raw scancodes.
304    1 = translated scancodes (used by qemu internally).  */
305
306 void ps2_keyboard_set_translation(void *opaque, int mode)
307 {
308     PS2KbdState *s = (PS2KbdState *)opaque;
309     s->translate = mode;
310 }
311
312 static void ps2_mouse_send_packet(PS2MouseState *s)
313 {
314     unsigned int b;
315     int dx1, dy1, dz1;
316
317     dx1 = s->mouse_dx;
318     dy1 = s->mouse_dy;
319     dz1 = s->mouse_dz;
320     /* XXX: increase range to 8 bits ? */
321     if (dx1 > 127)
322         dx1 = 127;
323     else if (dx1 < -127)
324         dx1 = -127;
325     if (dy1 > 127)
326         dy1 = 127;
327     else if (dy1 < -127)
328         dy1 = -127;
329     b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
330     ps2_queue(&s->common, b);
331     ps2_queue(&s->common, dx1 & 0xff);
332     ps2_queue(&s->common, dy1 & 0xff);
333     /* extra byte for IMPS/2 or IMEX */
334     switch(s->mouse_type) {
335     default:
336         break;
337     case 3:
338         if (dz1 > 127)
339             dz1 = 127;
340         else if (dz1 < -127)
341                 dz1 = -127;
342         ps2_queue(&s->common, dz1 & 0xff);
343         break;
344     case 4:
345         if (dz1 > 7)
346             dz1 = 7;
347         else if (dz1 < -7)
348             dz1 = -7;
349         b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
350         ps2_queue(&s->common, b);
351         break;
352     }
353
354     /* update deltas */
355     s->mouse_dx -= dx1;
356     s->mouse_dy -= dy1;
357     s->mouse_dz -= dz1;
358 }
359
360 static void ps2_mouse_event(void *opaque,
361                             int dx, int dy, int dz, int buttons_state)
362 {
363     PS2MouseState *s = opaque;
364
365     /* check if deltas are recorded when disabled */
366     if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
367         return;
368
369     s->mouse_dx += dx;
370     s->mouse_dy -= dy;
371     s->mouse_dz += dz;
372     /* XXX: SDL sometimes generates nul events: we delete them */
373     if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0 &&
374         s->mouse_buttons == buttons_state)
375         return;
376     s->mouse_buttons = buttons_state;
377
378     if (buttons_state) {
379         qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
380     }
381
382     if (!(s->mouse_status & MOUSE_STATUS_REMOTE) &&
383         (s->common.queue.count < (PS2_QUEUE_SIZE - 16))) {
384         for(;;) {
385             /* if not remote, send event. Multiple events are sent if
386                too big deltas */
387 #ifdef CONFIG_MARU
388 /* to guarantee safe serialization of input event by Munkyu Im */
389             qemu_mutex_lock(&mutex);
390             ps2_mouse_send_packet(s);
391             qemu_mutex_unlock(&mutex);
392 #else
393             ps2_mouse_send_packet(s);
394 #endif
395             if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
396                 break;
397         }
398     }
399 }
400
401 void ps2_mouse_fake_event(void *opaque)
402 {
403     ps2_mouse_event(opaque, 1, 0, 0, 0);
404 }
405
406 void ps2_write_mouse(void *opaque, int val)
407 {
408     PS2MouseState *s = (PS2MouseState *)opaque;
409 #ifdef DEBUG_MOUSE
410     printf("kbd: write mouse 0x%02x\n", val);
411 #endif
412     switch(s->common.write_cmd) {
413     default:
414     case -1:
415         /* mouse command */
416         if (s->mouse_wrap) {
417             if (val == AUX_RESET_WRAP) {
418                 s->mouse_wrap = 0;
419                 ps2_queue(&s->common, AUX_ACK);
420                 return;
421             } else if (val != AUX_RESET) {
422                 ps2_queue(&s->common, val);
423                 return;
424             }
425         }
426         switch(val) {
427         case AUX_SET_SCALE11:
428             s->mouse_status &= ~MOUSE_STATUS_SCALE21;
429             ps2_queue(&s->common, AUX_ACK);
430             break;
431         case AUX_SET_SCALE21:
432             s->mouse_status |= MOUSE_STATUS_SCALE21;
433             ps2_queue(&s->common, AUX_ACK);
434             break;
435         case AUX_SET_STREAM:
436             s->mouse_status &= ~MOUSE_STATUS_REMOTE;
437             ps2_queue(&s->common, AUX_ACK);
438             break;
439         case AUX_SET_WRAP:
440             s->mouse_wrap = 1;
441             ps2_queue(&s->common, AUX_ACK);
442             break;
443         case AUX_SET_REMOTE:
444             s->mouse_status |= MOUSE_STATUS_REMOTE;
445             ps2_queue(&s->common, AUX_ACK);
446             break;
447         case AUX_GET_TYPE:
448             ps2_queue(&s->common, AUX_ACK);
449             ps2_queue(&s->common, s->mouse_type);
450             break;
451         case AUX_SET_RES:
452         case AUX_SET_SAMPLE:
453             s->common.write_cmd = val;
454             ps2_queue(&s->common, AUX_ACK);
455             break;
456         case AUX_GET_SCALE:
457             ps2_queue(&s->common, AUX_ACK);
458             ps2_queue(&s->common, s->mouse_status);
459             ps2_queue(&s->common, s->mouse_resolution);
460             ps2_queue(&s->common, s->mouse_sample_rate);
461             break;
462         case AUX_POLL:
463             ps2_queue(&s->common, AUX_ACK);
464             ps2_mouse_send_packet(s);
465             break;
466         case AUX_ENABLE_DEV:
467             s->mouse_status |= MOUSE_STATUS_ENABLED;
468             ps2_queue(&s->common, AUX_ACK);
469             break;
470         case AUX_DISABLE_DEV:
471             s->mouse_status &= ~MOUSE_STATUS_ENABLED;
472             ps2_queue(&s->common, AUX_ACK);
473             break;
474         case AUX_SET_DEFAULT:
475             s->mouse_sample_rate = 100;
476             s->mouse_resolution = 2;
477             s->mouse_status = 0;
478             ps2_queue(&s->common, AUX_ACK);
479             break;
480         case AUX_RESET:
481             s->mouse_sample_rate = 100;
482             s->mouse_resolution = 2;
483             s->mouse_status = 0;
484             s->mouse_type = 0;
485             ps2_queue(&s->common, AUX_ACK);
486             ps2_queue(&s->common, 0xaa);
487             ps2_queue(&s->common, s->mouse_type);
488             break;
489         default:
490             break;
491         }
492         break;
493     case AUX_SET_SAMPLE:
494         s->mouse_sample_rate = val;
495         /* detect IMPS/2 or IMEX */
496         switch(s->mouse_detect_state) {
497         default:
498         case 0:
499             if (val == 200)
500                 s->mouse_detect_state = 1;
501             break;
502         case 1:
503             if (val == 100)
504                 s->mouse_detect_state = 2;
505             else if (val == 200)
506                 s->mouse_detect_state = 3;
507             else
508                 s->mouse_detect_state = 0;
509             break;
510         case 2:
511             if (val == 80)
512                 s->mouse_type = 3; /* IMPS/2 */
513             s->mouse_detect_state = 0;
514             break;
515         case 3:
516             if (val == 80)
517                 s->mouse_type = 4; /* IMEX */
518             s->mouse_detect_state = 0;
519             break;
520         }
521         ps2_queue(&s->common, AUX_ACK);
522         s->common.write_cmd = -1;
523         break;
524     case AUX_SET_RES:
525         s->mouse_resolution = val;
526         ps2_queue(&s->common, AUX_ACK);
527         s->common.write_cmd = -1;
528         break;
529     }
530 }
531
532 static void ps2_common_reset(PS2State *s)
533 {
534     PS2Queue *q;
535     s->write_cmd = -1;
536     q = &s->queue;
537     q->rptr = 0;
538     q->wptr = 0;
539     q->count = 0;
540     s->update_irq(s->update_arg, 0);
541 }
542
543 static void ps2_kbd_reset(void *opaque)
544 {
545     PS2KbdState *s = (PS2KbdState *) opaque;
546
547     ps2_common_reset(&s->common);
548     s->scan_enabled = 0;
549     s->translate = 0;
550     s->scancode_set = 0;
551 }
552
553 static void ps2_mouse_reset(void *opaque)
554 {
555     PS2MouseState *s = (PS2MouseState *) opaque;
556
557     ps2_common_reset(&s->common);
558     s->mouse_status = 0;
559     s->mouse_resolution = 0;
560     s->mouse_sample_rate = 0;
561     s->mouse_wrap = 0;
562     s->mouse_type = 0;
563     s->mouse_detect_state = 0;
564     s->mouse_dx = 0;
565     s->mouse_dy = 0;
566     s->mouse_dz = 0;
567     s->mouse_buttons = 0;
568 }
569
570 static const VMStateDescription vmstate_ps2_common = {
571     .name = "PS2 Common State",
572     .version_id = 3,
573     .minimum_version_id = 2,
574     .minimum_version_id_old = 2,
575     .fields      = (VMStateField []) {
576         VMSTATE_INT32(write_cmd, PS2State),
577         VMSTATE_INT32(queue.rptr, PS2State),
578         VMSTATE_INT32(queue.wptr, PS2State),
579         VMSTATE_INT32(queue.count, PS2State),
580         VMSTATE_BUFFER(queue.data, PS2State),
581         VMSTATE_END_OF_LIST()
582     }
583 };
584
585 static bool ps2_keyboard_ledstate_needed(void *opaque)
586 {
587     PS2KbdState *s = opaque;
588
589     return s->ledstate != 0; /* 0 is default state */
590 }
591
592 static int ps2_kbd_ledstate_post_load(void *opaque, int version_id)
593 {
594     PS2KbdState *s = opaque;
595
596     kbd_put_ledstate(s->ledstate);
597     return 0;
598 }
599
600 static const VMStateDescription vmstate_ps2_keyboard_ledstate = {
601     .name = "ps2kbd/ledstate",
602     .version_id = 3,
603     .minimum_version_id = 2,
604     .minimum_version_id_old = 2,
605     .post_load = ps2_kbd_ledstate_post_load,
606     .fields      = (VMStateField []) {
607         VMSTATE_INT32(ledstate, PS2KbdState),
608         VMSTATE_END_OF_LIST()
609     }
610 };
611
612 static int ps2_kbd_post_load(void* opaque, int version_id)
613 {
614     PS2KbdState *s = (PS2KbdState*)opaque;
615
616     if (version_id == 2)
617         s->scancode_set=2;
618     return 0;
619 }
620
621 static const VMStateDescription vmstate_ps2_keyboard = {
622     .name = "ps2kbd",
623     .version_id = 3,
624     .minimum_version_id = 2,
625     .minimum_version_id_old = 2,
626     .post_load = ps2_kbd_post_load,
627     .fields      = (VMStateField []) {
628         VMSTATE_STRUCT(common, PS2KbdState, 0, vmstate_ps2_common, PS2State),
629         VMSTATE_INT32(scan_enabled, PS2KbdState),
630         VMSTATE_INT32(translate, PS2KbdState),
631         VMSTATE_INT32_V(scancode_set, PS2KbdState,3),
632         VMSTATE_END_OF_LIST()
633     },
634     .subsections = (VMStateSubsection []) {
635         {
636             .vmsd = &vmstate_ps2_keyboard_ledstate,
637             .needed = ps2_keyboard_ledstate_needed,
638         }, {
639             /* empty */
640         }
641     }
642 };
643
644 static const VMStateDescription vmstate_ps2_mouse = {
645     .name = "ps2mouse",
646     .version_id = 2,
647     .minimum_version_id = 2,
648     .minimum_version_id_old = 2,
649     .fields      = (VMStateField []) {
650         VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
651         VMSTATE_UINT8(mouse_status, PS2MouseState),
652         VMSTATE_UINT8(mouse_resolution, PS2MouseState),
653         VMSTATE_UINT8(mouse_sample_rate, PS2MouseState),
654         VMSTATE_UINT8(mouse_wrap, PS2MouseState),
655         VMSTATE_UINT8(mouse_type, PS2MouseState),
656         VMSTATE_UINT8(mouse_detect_state, PS2MouseState),
657         VMSTATE_INT32(mouse_dx, PS2MouseState),
658         VMSTATE_INT32(mouse_dy, PS2MouseState),
659         VMSTATE_INT32(mouse_dz, PS2MouseState),
660         VMSTATE_UINT8(mouse_buttons, PS2MouseState),
661         VMSTATE_END_OF_LIST()
662     }
663 };
664
665 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
666 {
667     PS2KbdState *s = (PS2KbdState *)g_malloc0(sizeof(PS2KbdState));
668
669     s->common.update_irq = update_irq;
670     s->common.update_arg = update_arg;
671     s->scancode_set = 2;
672     vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
673     qemu_add_kbd_event_handler(ps2_put_keycode, s);
674     qemu_register_reset(ps2_kbd_reset, s);
675     return s;
676 }
677
678 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
679 {
680     PS2MouseState *s = (PS2MouseState *)g_malloc0(sizeof(PS2MouseState));
681
682     s->common.update_irq = update_irq;
683     s->common.update_arg = update_arg;
684     vmstate_register(NULL, 0, &vmstate_ps2_mouse, s);
685     qemu_add_mouse_event_handler(ps2_mouse_event, s, 0, "QEMU PS/2 Mouse");
686     qemu_register_reset(ps2_mouse_reset, s);
687 #ifdef CONFIG_MARU
688     /* to guarantee safe serialization of input event by Munkyu Im */
689     qemu_mutex_init(&mutex);
690 #endif
691     return s;
692 }