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