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