xref: /openbmc/qemu/hw/input/ps2.c (revision 2d135409e642bb31cff032dc533bf7ce6b0d0051)
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 
25 #include "qemu/osdep.h"
26 #include "qemu/log.h"
27 #include "hw/sysbus.h"
28 #include "hw/input/ps2.h"
29 #include "migration/vmstate.h"
30 #include "ui/console.h"
31 #include "ui/input.h"
32 #include "sysemu/reset.h"
33 #include "sysemu/runstate.h"
34 #include "qapi/error.h"
35 
36 #include "trace.h"
37 
38 /* Keyboard Commands */
39 #define KBD_CMD_SET_LEDS        0xED    /* Set keyboard leds */
40 #define KBD_CMD_ECHO            0xEE
41 #define KBD_CMD_SCANCODE        0xF0    /* Get/set scancode set */
42 #define KBD_CMD_GET_ID          0xF2    /* get keyboard ID */
43 #define KBD_CMD_SET_RATE        0xF3    /* Set typematic rate */
44 #define KBD_CMD_ENABLE          0xF4    /* Enable scanning */
45 #define KBD_CMD_RESET_DISABLE   0xF5    /* reset and disable scanning */
46 #define KBD_CMD_RESET_ENABLE    0xF6    /* reset and enable scanning */
47 #define KBD_CMD_RESET           0xFF    /* Reset */
48 #define KBD_CMD_SET_MAKE_BREAK  0xFC    /* Set Make and Break mode */
49 #define KBD_CMD_SET_TYPEMATIC   0xFA    /* Set Typematic Make and Break mode */
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 /*
80  * PS/2 buffer size. Keep 256 bytes for compatibility with
81  * older QEMU versions.
82  */
83 #define PS2_BUFFER_SIZE     256
84 #define PS2_QUEUE_SIZE      16  /* Queue size required by PS/2 protocol */
85 #define PS2_QUEUE_HEADROOM  8   /* Queue size for keyboard command replies */
86 
87 /* Bits for 'modifiers' field in PS2KbdState */
88 #define MOD_CTRL_L  (1 << 0)
89 #define MOD_SHIFT_L (1 << 1)
90 #define MOD_ALT_L   (1 << 2)
91 #define MOD_CTRL_R  (1 << 3)
92 #define MOD_SHIFT_R (1 << 4)
93 #define MOD_ALT_R   (1 << 5)
94 
95 typedef struct {
96     uint8_t data[PS2_BUFFER_SIZE];
97     int rptr, wptr, cwptr, count;
98 } PS2Queue;
99 
100 struct PS2State {
101     SysBusDevice parent_obj;
102 
103     PS2Queue queue;
104     int32_t write_cmd;
105     void (*update_irq)(void *, int);
106     void *update_arg;
107 };
108 
109 #define TYPE_PS2_DEVICE "ps2-device"
110 OBJECT_DECLARE_SIMPLE_TYPE(PS2State, PS2_DEVICE)
111 
112 struct PS2KbdState {
113     PS2State parent_obj;
114 
115     int scan_enabled;
116     int translate;
117     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
118     int ledstate;
119     bool need_high_bit;
120     unsigned int modifiers; /* bitmask of MOD_* constants above */
121 };
122 
123 #define TYPE_PS2_KBD_DEVICE "ps2-kbd"
124 OBJECT_DECLARE_SIMPLE_TYPE(PS2KbdState, PS2_KBD_DEVICE)
125 
126 struct PS2MouseState {
127     PS2State parent_obj;
128 
129     uint8_t mouse_status;
130     uint8_t mouse_resolution;
131     uint8_t mouse_sample_rate;
132     uint8_t mouse_wrap;
133     uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
134     uint8_t mouse_detect_state;
135     int mouse_dx; /* current values, needed for 'poll' mode */
136     int mouse_dy;
137     int mouse_dz;
138     int mouse_dw;
139     uint8_t mouse_buttons;
140 };
141 
142 #define TYPE_PS2_MOUSE_DEVICE "ps2-mouse"
143 OBJECT_DECLARE_SIMPLE_TYPE(PS2MouseState, PS2_MOUSE_DEVICE)
144 
145 static uint8_t translate_table[256] = {
146     0xff, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x3c, 0x58,
147     0x64, 0x44, 0x42, 0x40, 0x3e, 0x0f, 0x29, 0x59,
148     0x65, 0x38, 0x2a, 0x70, 0x1d, 0x10, 0x02, 0x5a,
149     0x66, 0x71, 0x2c, 0x1f, 0x1e, 0x11, 0x03, 0x5b,
150     0x67, 0x2e, 0x2d, 0x20, 0x12, 0x05, 0x04, 0x5c,
151     0x68, 0x39, 0x2f, 0x21, 0x14, 0x13, 0x06, 0x5d,
152     0x69, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, 0x5e,
153     0x6a, 0x72, 0x32, 0x24, 0x16, 0x08, 0x09, 0x5f,
154     0x6b, 0x33, 0x25, 0x17, 0x18, 0x0b, 0x0a, 0x60,
155     0x6c, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0c, 0x61,
156     0x6d, 0x73, 0x28, 0x74, 0x1a, 0x0d, 0x62, 0x6e,
157     0x3a, 0x36, 0x1c, 0x1b, 0x75, 0x2b, 0x63, 0x76,
158     0x55, 0x56, 0x77, 0x78, 0x79, 0x7a, 0x0e, 0x7b,
159     0x7c, 0x4f, 0x7d, 0x4b, 0x47, 0x7e, 0x7f, 0x6f,
160     0x52, 0x53, 0x50, 0x4c, 0x4d, 0x48, 0x01, 0x45,
161     0x57, 0x4e, 0x51, 0x4a, 0x37, 0x49, 0x46, 0x54,
162     0x80, 0x81, 0x82, 0x41, 0x54, 0x85, 0x86, 0x87,
163     0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
164     0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
165     0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
166     0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
167     0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
168     0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
169     0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
170     0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
171     0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
172     0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
173     0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
174     0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
175     0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
176     0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
177     0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
178 };
179 
180 static unsigned int ps2_modifier_bit(QKeyCode key)
181 {
182     switch (key) {
183     case Q_KEY_CODE_CTRL:
184         return MOD_CTRL_L;
185     case Q_KEY_CODE_CTRL_R:
186         return MOD_CTRL_R;
187     case Q_KEY_CODE_SHIFT:
188         return MOD_SHIFT_L;
189     case Q_KEY_CODE_SHIFT_R:
190         return MOD_SHIFT_R;
191     case Q_KEY_CODE_ALT:
192         return MOD_ALT_L;
193     case Q_KEY_CODE_ALT_R:
194         return MOD_ALT_R;
195     default:
196         return 0;
197     }
198 }
199 
200 static void ps2_reset_queue(PS2State *s)
201 {
202     PS2Queue *q = &s->queue;
203 
204     q->rptr = 0;
205     q->wptr = 0;
206     q->cwptr = -1;
207     q->count = 0;
208 }
209 
210 int ps2_queue_empty(PS2State *s)
211 {
212     return s->queue.count == 0;
213 }
214 
215 void ps2_queue_noirq(PS2State *s, int b)
216 {
217     PS2Queue *q = &s->queue;
218 
219     if (q->count >= PS2_QUEUE_SIZE) {
220         return;
221     }
222 
223     q->data[q->wptr] = b;
224     if (++q->wptr == PS2_BUFFER_SIZE) {
225         q->wptr = 0;
226     }
227     q->count++;
228 }
229 
230 void ps2_raise_irq(PS2State *s)
231 {
232     s->update_irq(s->update_arg, 1);
233 }
234 
235 void ps2_queue(PS2State *s, int b)
236 {
237     if (PS2_QUEUE_SIZE - s->queue.count < 1) {
238         return;
239     }
240 
241     ps2_queue_noirq(s, b);
242     ps2_raise_irq(s);
243 }
244 
245 void ps2_queue_2(PS2State *s, int b1, int b2)
246 {
247     if (PS2_QUEUE_SIZE - s->queue.count < 2) {
248         return;
249     }
250 
251     ps2_queue_noirq(s, b1);
252     ps2_queue_noirq(s, b2);
253     ps2_raise_irq(s);
254 }
255 
256 void ps2_queue_3(PS2State *s, int b1, int b2, int b3)
257 {
258     if (PS2_QUEUE_SIZE - s->queue.count < 3) {
259         return;
260     }
261 
262     ps2_queue_noirq(s, b1);
263     ps2_queue_noirq(s, b2);
264     ps2_queue_noirq(s, b3);
265     ps2_raise_irq(s);
266 }
267 
268 void ps2_queue_4(PS2State *s, int b1, int b2, int b3, int b4)
269 {
270     if (PS2_QUEUE_SIZE - s->queue.count < 4) {
271         return;
272     }
273 
274     ps2_queue_noirq(s, b1);
275     ps2_queue_noirq(s, b2);
276     ps2_queue_noirq(s, b3);
277     ps2_queue_noirq(s, b4);
278     ps2_raise_irq(s);
279 }
280 
281 static void ps2_cqueue_data(PS2Queue *q, int b)
282 {
283     q->data[q->cwptr] = b;
284     if (++q->cwptr >= PS2_BUFFER_SIZE) {
285         q->cwptr = 0;
286     }
287     q->count++;
288 }
289 
290 static void ps2_cqueue_1(PS2State *s, int b1)
291 {
292     PS2Queue *q = &s->queue;
293 
294     q->rptr = (q->rptr - 1) & (PS2_BUFFER_SIZE - 1);
295     q->cwptr = q->rptr;
296     ps2_cqueue_data(q, b1);
297     ps2_raise_irq(s);
298 }
299 
300 static void ps2_cqueue_2(PS2State *s, int b1, int b2)
301 {
302     PS2Queue *q = &s->queue;
303 
304     q->rptr = (q->rptr - 2) & (PS2_BUFFER_SIZE - 1);
305     q->cwptr = q->rptr;
306     ps2_cqueue_data(q, b1);
307     ps2_cqueue_data(q, b2);
308     ps2_raise_irq(s);
309 }
310 
311 static void ps2_cqueue_3(PS2State *s, int b1, int b2, int b3)
312 {
313     PS2Queue *q = &s->queue;
314 
315     q->rptr = (q->rptr - 3) & (PS2_BUFFER_SIZE - 1);
316     q->cwptr = q->rptr;
317     ps2_cqueue_data(q, b1);
318     ps2_cqueue_data(q, b2);
319     ps2_cqueue_data(q, b3);
320     ps2_raise_irq(s);
321 }
322 
323 static void ps2_cqueue_reset(PS2State *s)
324 {
325     PS2Queue *q = &s->queue;
326     int ccount;
327 
328     if (q->cwptr == -1) {
329         return;
330     }
331 
332     ccount = (q->cwptr - q->rptr) & (PS2_BUFFER_SIZE - 1);
333     q->count -= ccount;
334     q->rptr = q->cwptr;
335     q->cwptr = -1;
336 }
337 
338 /* keycode is the untranslated scancode in the current scancode set. */
339 static void ps2_put_keycode(void *opaque, int keycode)
340 {
341     PS2KbdState *s = opaque;
342     PS2State *ps = PS2_DEVICE(s);
343 
344     trace_ps2_put_keycode(opaque, keycode);
345     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
346 
347     if (s->translate) {
348         if (keycode == 0xf0) {
349             s->need_high_bit = true;
350         } else if (s->need_high_bit) {
351             ps2_queue(ps, translate_table[keycode] | 0x80);
352             s->need_high_bit = false;
353         } else {
354             ps2_queue(ps, translate_table[keycode]);
355         }
356     } else {
357         ps2_queue(ps, keycode);
358     }
359 }
360 
361 static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
362                                InputEvent *evt)
363 {
364     PS2KbdState *s = (PS2KbdState *)dev;
365     InputKeyEvent *key = evt->u.key.data;
366     int qcode;
367     uint16_t keycode = 0;
368     int mod;
369 
370     /* do not process events while disabled to prevent stream corruption */
371     if (!s->scan_enabled) {
372         return;
373     }
374 
375     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
376     assert(evt->type == INPUT_EVENT_KIND_KEY);
377     qcode = qemu_input_key_value_to_qcode(key->key);
378 
379     mod = ps2_modifier_bit(qcode);
380     trace_ps2_keyboard_event(s, qcode, key->down, mod,
381                              s->modifiers, s->scancode_set, s->translate);
382     if (key->down) {
383         s->modifiers |= mod;
384     } else {
385         s->modifiers &= ~mod;
386     }
387 
388     if (s->scancode_set == 1) {
389         if (qcode == Q_KEY_CODE_PAUSE) {
390             if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) {
391                 if (key->down) {
392                     ps2_put_keycode(s, 0xe0);
393                     ps2_put_keycode(s, 0x46);
394                     ps2_put_keycode(s, 0xe0);
395                     ps2_put_keycode(s, 0xc6);
396                 }
397             } else {
398                 if (key->down) {
399                     ps2_put_keycode(s, 0xe1);
400                     ps2_put_keycode(s, 0x1d);
401                     ps2_put_keycode(s, 0x45);
402                     ps2_put_keycode(s, 0xe1);
403                     ps2_put_keycode(s, 0x9d);
404                     ps2_put_keycode(s, 0xc5);
405                 }
406             }
407         } else if (qcode == Q_KEY_CODE_PRINT) {
408             if (s->modifiers & MOD_ALT_L) {
409                 if (key->down) {
410                     ps2_put_keycode(s, 0xb8);
411                     ps2_put_keycode(s, 0x38);
412                     ps2_put_keycode(s, 0x54);
413                 } else {
414                     ps2_put_keycode(s, 0xd4);
415                     ps2_put_keycode(s, 0xb8);
416                     ps2_put_keycode(s, 0x38);
417                 }
418             } else if (s->modifiers & MOD_ALT_R) {
419                 if (key->down) {
420                     ps2_put_keycode(s, 0xe0);
421                     ps2_put_keycode(s, 0xb8);
422                     ps2_put_keycode(s, 0xe0);
423                     ps2_put_keycode(s, 0x38);
424                     ps2_put_keycode(s, 0x54);
425                 } else {
426                     ps2_put_keycode(s, 0xd4);
427                     ps2_put_keycode(s, 0xe0);
428                     ps2_put_keycode(s, 0xb8);
429                     ps2_put_keycode(s, 0xe0);
430                     ps2_put_keycode(s, 0x38);
431                 }
432             } else if (s->modifiers & (MOD_SHIFT_L | MOD_CTRL_L |
433                                        MOD_SHIFT_R | MOD_CTRL_R)) {
434                 if (key->down) {
435                     ps2_put_keycode(s, 0xe0);
436                     ps2_put_keycode(s, 0x37);
437                 } else {
438                     ps2_put_keycode(s, 0xe0);
439                     ps2_put_keycode(s, 0xb7);
440                 }
441             } else {
442                 if (key->down) {
443                     ps2_put_keycode(s, 0xe0);
444                     ps2_put_keycode(s, 0x2a);
445                     ps2_put_keycode(s, 0xe0);
446                     ps2_put_keycode(s, 0x37);
447                 } else {
448                     ps2_put_keycode(s, 0xe0);
449                     ps2_put_keycode(s, 0xb7);
450                     ps2_put_keycode(s, 0xe0);
451                     ps2_put_keycode(s, 0xaa);
452                 }
453             }
454         } else {
455             if (qcode < qemu_input_map_qcode_to_atset1_len) {
456                 keycode = qemu_input_map_qcode_to_atset1[qcode];
457             }
458             if (keycode) {
459                 if (keycode & 0xff00) {
460                     ps2_put_keycode(s, keycode >> 8);
461                 }
462                 if (!key->down) {
463                     keycode |= 0x80;
464                 }
465                 ps2_put_keycode(s, keycode & 0xff);
466             } else {
467                 qemu_log_mask(LOG_UNIMP,
468                               "ps2: ignoring key with qcode %d\n", qcode);
469             }
470         }
471     } else if (s->scancode_set == 2) {
472         if (qcode == Q_KEY_CODE_PAUSE) {
473             if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) {
474                 if (key->down) {
475                     ps2_put_keycode(s, 0xe0);
476                     ps2_put_keycode(s, 0x7e);
477                     ps2_put_keycode(s, 0xe0);
478                     ps2_put_keycode(s, 0xf0);
479                     ps2_put_keycode(s, 0x7e);
480                 }
481             } else {
482                 if (key->down) {
483                     ps2_put_keycode(s, 0xe1);
484                     ps2_put_keycode(s, 0x14);
485                     ps2_put_keycode(s, 0x77);
486                     ps2_put_keycode(s, 0xe1);
487                     ps2_put_keycode(s, 0xf0);
488                     ps2_put_keycode(s, 0x14);
489                     ps2_put_keycode(s, 0xf0);
490                     ps2_put_keycode(s, 0x77);
491                 }
492             }
493         } else if (qcode == Q_KEY_CODE_PRINT) {
494             if (s->modifiers & MOD_ALT_L) {
495                 if (key->down) {
496                     ps2_put_keycode(s, 0xf0);
497                     ps2_put_keycode(s, 0x11);
498                     ps2_put_keycode(s, 0x11);
499                     ps2_put_keycode(s, 0x84);
500                 } else {
501                     ps2_put_keycode(s, 0xf0);
502                     ps2_put_keycode(s, 0x84);
503                     ps2_put_keycode(s, 0xf0);
504                     ps2_put_keycode(s, 0x11);
505                     ps2_put_keycode(s, 0x11);
506                 }
507             } else if (s->modifiers & MOD_ALT_R) {
508                 if (key->down) {
509                     ps2_put_keycode(s, 0xe0);
510                     ps2_put_keycode(s, 0xf0);
511                     ps2_put_keycode(s, 0x11);
512                     ps2_put_keycode(s, 0xe0);
513                     ps2_put_keycode(s, 0x11);
514                     ps2_put_keycode(s, 0x84);
515                 } else {
516                     ps2_put_keycode(s, 0xf0);
517                     ps2_put_keycode(s, 0x84);
518                     ps2_put_keycode(s, 0xe0);
519                     ps2_put_keycode(s, 0xf0);
520                     ps2_put_keycode(s, 0x11);
521                     ps2_put_keycode(s, 0xe0);
522                     ps2_put_keycode(s, 0x11);
523                 }
524             } else if (s->modifiers & (MOD_SHIFT_L | MOD_CTRL_L |
525                                        MOD_SHIFT_R | MOD_CTRL_R)) {
526                 if (key->down) {
527                     ps2_put_keycode(s, 0xe0);
528                     ps2_put_keycode(s, 0x7c);
529                 } else {
530                     ps2_put_keycode(s, 0xe0);
531                     ps2_put_keycode(s, 0xf0);
532                     ps2_put_keycode(s, 0x7c);
533                 }
534             } else {
535                 if (key->down) {
536                     ps2_put_keycode(s, 0xe0);
537                     ps2_put_keycode(s, 0x12);
538                     ps2_put_keycode(s, 0xe0);
539                     ps2_put_keycode(s, 0x7c);
540                 } else {
541                     ps2_put_keycode(s, 0xe0);
542                     ps2_put_keycode(s, 0xf0);
543                     ps2_put_keycode(s, 0x7c);
544                     ps2_put_keycode(s, 0xe0);
545                     ps2_put_keycode(s, 0xf0);
546                     ps2_put_keycode(s, 0x12);
547                 }
548             }
549         } else {
550             if (qcode < qemu_input_map_qcode_to_atset2_len) {
551                 keycode = qemu_input_map_qcode_to_atset2[qcode];
552             }
553             if (keycode) {
554                 if (keycode & 0xff00) {
555                     ps2_put_keycode(s, keycode >> 8);
556                 }
557                 if (!key->down) {
558                     ps2_put_keycode(s, 0xf0);
559                 }
560                 ps2_put_keycode(s, keycode & 0xff);
561             } else {
562                 qemu_log_mask(LOG_UNIMP,
563                               "ps2: ignoring key with qcode %d\n", qcode);
564             }
565         }
566     } else if (s->scancode_set == 3) {
567         if (qcode < qemu_input_map_qcode_to_atset3_len) {
568             keycode = qemu_input_map_qcode_to_atset3[qcode];
569         }
570         if (keycode) {
571             /* FIXME: break code should be configured on a key by key basis */
572             if (!key->down) {
573                 ps2_put_keycode(s, 0xf0);
574             }
575             ps2_put_keycode(s, keycode);
576         } else {
577             qemu_log_mask(LOG_UNIMP,
578                           "ps2: ignoring key with qcode %d\n", qcode);
579         }
580     }
581 }
582 
583 uint32_t ps2_read_data(PS2State *s)
584 {
585     PS2Queue *q;
586     int val, index;
587 
588     trace_ps2_read_data(s);
589     q = &s->queue;
590     if (q->count == 0) {
591         /*
592          * NOTE: if no data left, we return the last keyboard one
593          * (needed for EMM386)
594          */
595         /* XXX: need a timer to do things correctly */
596         index = q->rptr - 1;
597         if (index < 0) {
598             index = PS2_BUFFER_SIZE - 1;
599         }
600         val = q->data[index];
601     } else {
602         val = q->data[q->rptr];
603         if (++q->rptr == PS2_BUFFER_SIZE) {
604             q->rptr = 0;
605         }
606         q->count--;
607         if (q->rptr == q->cwptr) {
608             /* command reply queue is empty */
609             q->cwptr = -1;
610         }
611         /* reading deasserts IRQ */
612         s->update_irq(s->update_arg, 0);
613         /* reassert IRQs if data left */
614         if (q->count) {
615             s->update_irq(s->update_arg, 1);
616         }
617     }
618     return val;
619 }
620 
621 static void ps2_set_ledstate(PS2KbdState *s, int ledstate)
622 {
623     trace_ps2_set_ledstate(s, ledstate);
624     s->ledstate = ledstate;
625     kbd_put_ledstate(ledstate);
626 }
627 
628 static void ps2_reset_keyboard(PS2KbdState *s)
629 {
630     PS2State *ps2 = PS2_DEVICE(s);
631 
632     trace_ps2_reset_keyboard(s);
633     s->scan_enabled = 1;
634     s->scancode_set = 2;
635     ps2_reset_queue(ps2);
636     ps2_set_ledstate(s, 0);
637 }
638 
639 void ps2_write_keyboard(void *opaque, int val)
640 {
641     PS2KbdState *s = (PS2KbdState *)opaque;
642     PS2State *ps2 = PS2_DEVICE(s);
643 
644     trace_ps2_write_keyboard(opaque, val);
645     ps2_cqueue_reset(ps2);
646     switch (ps2->write_cmd) {
647     default:
648     case -1:
649         switch (val) {
650         case 0x00:
651             ps2_cqueue_1(ps2, KBD_REPLY_ACK);
652             break;
653         case 0x05:
654             ps2_cqueue_1(ps2, KBD_REPLY_RESEND);
655             break;
656         case KBD_CMD_GET_ID:
657             /* We emulate a MF2 AT keyboard here */
658             ps2_cqueue_3(ps2, KBD_REPLY_ACK, KBD_REPLY_ID,
659                          s->translate ? 0x41 : 0x83);
660             break;
661         case KBD_CMD_ECHO:
662             ps2_cqueue_1(ps2, KBD_CMD_ECHO);
663             break;
664         case KBD_CMD_ENABLE:
665             s->scan_enabled = 1;
666             ps2_cqueue_1(ps2, KBD_REPLY_ACK);
667             break;
668         case KBD_CMD_SCANCODE:
669         case KBD_CMD_SET_LEDS:
670         case KBD_CMD_SET_RATE:
671         case KBD_CMD_SET_MAKE_BREAK:
672             ps2->write_cmd = val;
673             ps2_cqueue_1(ps2, KBD_REPLY_ACK);
674             break;
675         case KBD_CMD_RESET_DISABLE:
676             ps2_reset_keyboard(s);
677             s->scan_enabled = 0;
678             ps2_cqueue_1(ps2, KBD_REPLY_ACK);
679             break;
680         case KBD_CMD_RESET_ENABLE:
681             ps2_reset_keyboard(s);
682             s->scan_enabled = 1;
683             ps2_cqueue_1(ps2, KBD_REPLY_ACK);
684             break;
685         case KBD_CMD_RESET:
686             ps2_reset_keyboard(s);
687             ps2_cqueue_2(ps2,
688                          KBD_REPLY_ACK,
689                          KBD_REPLY_POR);
690             break;
691         case KBD_CMD_SET_TYPEMATIC:
692             ps2_cqueue_1(ps2, KBD_REPLY_ACK);
693             break;
694         default:
695             ps2_cqueue_1(ps2, KBD_REPLY_RESEND);
696             break;
697         }
698         break;
699     case KBD_CMD_SET_MAKE_BREAK:
700         ps2_cqueue_1(ps2, KBD_REPLY_ACK);
701         ps2->write_cmd = -1;
702         break;
703     case KBD_CMD_SCANCODE:
704         if (val == 0) {
705             ps2_cqueue_2(ps2, KBD_REPLY_ACK, s->translate ?
706                 translate_table[s->scancode_set] : s->scancode_set);
707         } else if (val >= 1 && val <= 3) {
708             s->scancode_set = val;
709             ps2_cqueue_1(ps2, KBD_REPLY_ACK);
710         } else {
711             ps2_cqueue_1(ps2, KBD_REPLY_RESEND);
712         }
713         ps2->write_cmd = -1;
714         break;
715     case KBD_CMD_SET_LEDS:
716         ps2_set_ledstate(s, val);
717         ps2_cqueue_1(ps2, KBD_REPLY_ACK);
718         ps2->write_cmd = -1;
719         break;
720     case KBD_CMD_SET_RATE:
721         ps2_cqueue_1(ps2, KBD_REPLY_ACK);
722         ps2->write_cmd = -1;
723         break;
724     }
725 }
726 
727 /*
728  * Set the scancode translation mode.
729  * 0 = raw scancodes.
730  * 1 = translated scancodes (used by qemu internally).
731  */
732 
733 void ps2_keyboard_set_translation(void *opaque, int mode)
734 {
735     PS2KbdState *s = (PS2KbdState *)opaque;
736     trace_ps2_keyboard_set_translation(opaque, mode);
737     s->translate = mode;
738 }
739 
740 static int ps2_mouse_send_packet(PS2MouseState *s)
741 {
742     PS2State *ps2 = PS2_DEVICE(s);
743     /* IMPS/2 and IMEX send 4 bytes, PS2 sends 3 bytes */
744     const int needed = s->mouse_type ? 4 : 3;
745     unsigned int b;
746     int dx1, dy1, dz1, dw1;
747 
748     if (PS2_QUEUE_SIZE - ps2->queue.count < needed) {
749         return 0;
750     }
751 
752     dx1 = s->mouse_dx;
753     dy1 = s->mouse_dy;
754     dz1 = s->mouse_dz;
755     dw1 = s->mouse_dw;
756     /* XXX: increase range to 8 bits ? */
757     if (dx1 > 127) {
758         dx1 = 127;
759     } else if (dx1 < -127) {
760         dx1 = -127;
761     }
762     if (dy1 > 127) {
763         dy1 = 127;
764     } else if (dy1 < -127) {
765         dy1 = -127;
766     }
767     b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
768     ps2_queue_noirq(ps2, b);
769     ps2_queue_noirq(ps2, dx1 & 0xff);
770     ps2_queue_noirq(ps2, dy1 & 0xff);
771     /* extra byte for IMPS/2 or IMEX */
772     switch (s->mouse_type) {
773     default:
774         /* Just ignore the wheels if not supported */
775         s->mouse_dz = 0;
776         s->mouse_dw = 0;
777         break;
778     case 3:
779         if (dz1 > 127) {
780             dz1 = 127;
781         } else if (dz1 < -127) {
782             dz1 = -127;
783         }
784         ps2_queue_noirq(ps2, dz1 & 0xff);
785         s->mouse_dz -= dz1;
786         s->mouse_dw = 0;
787         break;
788     case 4:
789         /*
790          * This matches what the Linux kernel expects for exps/2 in
791          * drivers/input/mouse/psmouse-base.c. Note, if you happen to
792          * press/release the 4th or 5th buttons at the same moment as a
793          * horizontal wheel scroll, those button presses will get lost. I'm not
794          * sure what to do about that, since by this point we don't know
795          * whether those buttons actually changed state.
796          */
797         if (dw1 != 0) {
798             if (dw1 > 31) {
799                 dw1 = 31;
800             } else if (dw1 < -31) {
801                 dw1 = -31;
802             }
803 
804             /*
805              * linux kernel expects first 6 bits to represent the value
806              * for horizontal scroll
807              */
808             b = (dw1 & 0x3f) | 0x40;
809             s->mouse_dw -= dw1;
810         } else {
811             if (dz1 > 7) {
812                 dz1 = 7;
813             } else if (dz1 < -7) {
814                 dz1 = -7;
815             }
816 
817             b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
818             s->mouse_dz -= dz1;
819         }
820         ps2_queue_noirq(ps2, b);
821         break;
822     }
823 
824     ps2_raise_irq(ps2);
825 
826     trace_ps2_mouse_send_packet(s, dx1, dy1, dz1, b);
827     /* update deltas */
828     s->mouse_dx -= dx1;
829     s->mouse_dy -= dy1;
830 
831     return 1;
832 }
833 
834 static void ps2_mouse_event(DeviceState *dev, QemuConsole *src,
835                             InputEvent *evt)
836 {
837     static const int bmap[INPUT_BUTTON__MAX] = {
838         [INPUT_BUTTON_LEFT]   = PS2_MOUSE_BUTTON_LEFT,
839         [INPUT_BUTTON_MIDDLE] = PS2_MOUSE_BUTTON_MIDDLE,
840         [INPUT_BUTTON_RIGHT]  = PS2_MOUSE_BUTTON_RIGHT,
841         [INPUT_BUTTON_SIDE]   = PS2_MOUSE_BUTTON_SIDE,
842         [INPUT_BUTTON_EXTRA]  = PS2_MOUSE_BUTTON_EXTRA,
843     };
844     PS2MouseState *s = (PS2MouseState *)dev;
845     InputMoveEvent *move;
846     InputBtnEvent *btn;
847 
848     /* check if deltas are recorded when disabled */
849     if (!(s->mouse_status & MOUSE_STATUS_ENABLED)) {
850         return;
851     }
852 
853     switch (evt->type) {
854     case INPUT_EVENT_KIND_REL:
855         move = evt->u.rel.data;
856         if (move->axis == INPUT_AXIS_X) {
857             s->mouse_dx += move->value;
858         } else if (move->axis == INPUT_AXIS_Y) {
859             s->mouse_dy -= move->value;
860         }
861         break;
862 
863     case INPUT_EVENT_KIND_BTN:
864         btn = evt->u.btn.data;
865         if (btn->down) {
866             s->mouse_buttons |= bmap[btn->button];
867             if (btn->button == INPUT_BUTTON_WHEEL_UP) {
868                 s->mouse_dz--;
869             } else if (btn->button == INPUT_BUTTON_WHEEL_DOWN) {
870                 s->mouse_dz++;
871             }
872 
873             if (btn->button == INPUT_BUTTON_WHEEL_RIGHT) {
874                 s->mouse_dw--;
875             } else if (btn->button == INPUT_BUTTON_WHEEL_LEFT) {
876                 s->mouse_dw++;
877             }
878         } else {
879             s->mouse_buttons &= ~bmap[btn->button];
880         }
881         break;
882 
883     default:
884         /* keep gcc happy */
885         break;
886     }
887 }
888 
889 static void ps2_mouse_sync(DeviceState *dev)
890 {
891     PS2MouseState *s = (PS2MouseState *)dev;
892 
893     /* do not sync while disabled to prevent stream corruption */
894     if (!(s->mouse_status & MOUSE_STATUS_ENABLED)) {
895         return;
896     }
897 
898     if (s->mouse_buttons) {
899         qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
900     }
901     if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) {
902         /*
903          * if not remote, send event. Multiple events are sent if
904          * too big deltas
905          */
906         while (ps2_mouse_send_packet(s)) {
907             if (s->mouse_dx == 0 && s->mouse_dy == 0
908                     && s->mouse_dz == 0 && s->mouse_dw == 0) {
909                 break;
910             }
911         }
912     }
913 }
914 
915 void ps2_mouse_fake_event(void *opaque)
916 {
917     PS2MouseState *s = opaque;
918     trace_ps2_mouse_fake_event(opaque);
919     s->mouse_dx++;
920     ps2_mouse_sync(opaque);
921 }
922 
923 void ps2_write_mouse(void *opaque, int val)
924 {
925     PS2MouseState *s = (PS2MouseState *)opaque;
926     PS2State *ps2 = PS2_DEVICE(s);
927 
928     trace_ps2_write_mouse(opaque, val);
929     switch (ps2->write_cmd) {
930     default:
931     case -1:
932         /* mouse command */
933         if (s->mouse_wrap) {
934             if (val == AUX_RESET_WRAP) {
935                 s->mouse_wrap = 0;
936                 ps2_queue(ps2, AUX_ACK);
937                 return;
938             } else if (val != AUX_RESET) {
939                 ps2_queue(ps2, val);
940                 return;
941             }
942         }
943         switch (val) {
944         case AUX_SET_SCALE11:
945             s->mouse_status &= ~MOUSE_STATUS_SCALE21;
946             ps2_queue(ps2, AUX_ACK);
947             break;
948         case AUX_SET_SCALE21:
949             s->mouse_status |= MOUSE_STATUS_SCALE21;
950             ps2_queue(ps2, AUX_ACK);
951             break;
952         case AUX_SET_STREAM:
953             s->mouse_status &= ~MOUSE_STATUS_REMOTE;
954             ps2_queue(ps2, AUX_ACK);
955             break;
956         case AUX_SET_WRAP:
957             s->mouse_wrap = 1;
958             ps2_queue(ps2, AUX_ACK);
959             break;
960         case AUX_SET_REMOTE:
961             s->mouse_status |= MOUSE_STATUS_REMOTE;
962             ps2_queue(ps2, AUX_ACK);
963             break;
964         case AUX_GET_TYPE:
965             ps2_queue_2(ps2,
966                 AUX_ACK,
967                 s->mouse_type);
968             break;
969         case AUX_SET_RES:
970         case AUX_SET_SAMPLE:
971             ps2->write_cmd = val;
972             ps2_queue(ps2, AUX_ACK);
973             break;
974         case AUX_GET_SCALE:
975             ps2_queue_4(ps2,
976                 AUX_ACK,
977                 s->mouse_status,
978                 s->mouse_resolution,
979                 s->mouse_sample_rate);
980             break;
981         case AUX_POLL:
982             ps2_queue(ps2, AUX_ACK);
983             ps2_mouse_send_packet(s);
984             break;
985         case AUX_ENABLE_DEV:
986             s->mouse_status |= MOUSE_STATUS_ENABLED;
987             ps2_queue(ps2, AUX_ACK);
988             break;
989         case AUX_DISABLE_DEV:
990             s->mouse_status &= ~MOUSE_STATUS_ENABLED;
991             ps2_queue(ps2, AUX_ACK);
992             break;
993         case AUX_SET_DEFAULT:
994             s->mouse_sample_rate = 100;
995             s->mouse_resolution = 2;
996             s->mouse_status = 0;
997             ps2_queue(ps2, AUX_ACK);
998             break;
999         case AUX_RESET:
1000             s->mouse_sample_rate = 100;
1001             s->mouse_resolution = 2;
1002             s->mouse_status = 0;
1003             s->mouse_type = 0;
1004             ps2_reset_queue(ps2);
1005             ps2_queue_3(ps2,
1006                 AUX_ACK,
1007                 0xaa,
1008                 s->mouse_type);
1009             break;
1010         default:
1011             break;
1012         }
1013         break;
1014     case AUX_SET_SAMPLE:
1015         s->mouse_sample_rate = val;
1016         /* detect IMPS/2 or IMEX */
1017         switch (s->mouse_detect_state) {
1018         default:
1019         case 0:
1020             if (val == 200) {
1021                 s->mouse_detect_state = 1;
1022             }
1023             break;
1024         case 1:
1025             if (val == 100) {
1026                 s->mouse_detect_state = 2;
1027             } else if (val == 200) {
1028                 s->mouse_detect_state = 3;
1029             } else {
1030                 s->mouse_detect_state = 0;
1031             }
1032             break;
1033         case 2:
1034             if (val == 80) {
1035                 s->mouse_type = 3; /* IMPS/2 */
1036             }
1037             s->mouse_detect_state = 0;
1038             break;
1039         case 3:
1040             if (val == 80) {
1041                 s->mouse_type = 4; /* IMEX */
1042             }
1043             s->mouse_detect_state = 0;
1044             break;
1045         }
1046         ps2_queue(ps2, AUX_ACK);
1047         ps2->write_cmd = -1;
1048         break;
1049     case AUX_SET_RES:
1050         s->mouse_resolution = val;
1051         ps2_queue(ps2, AUX_ACK);
1052         ps2->write_cmd = -1;
1053         break;
1054     }
1055 }
1056 
1057 static void ps2_common_reset(PS2State *s)
1058 {
1059     s->write_cmd = -1;
1060     ps2_reset_queue(s);
1061     s->update_irq(s->update_arg, 0);
1062 }
1063 
1064 static void ps2_common_post_load(PS2State *s)
1065 {
1066     PS2Queue *q = &s->queue;
1067     int ccount = 0;
1068 
1069     /* limit the number of queued command replies to PS2_QUEUE_HEADROOM */
1070     if (q->cwptr != -1) {
1071         ccount = (q->cwptr - q->rptr) & (PS2_BUFFER_SIZE - 1);
1072         if (ccount > PS2_QUEUE_HEADROOM) {
1073             ccount = PS2_QUEUE_HEADROOM;
1074         }
1075     }
1076 
1077     /* limit the scancode queue size to PS2_QUEUE_SIZE */
1078     if (q->count < ccount) {
1079         q->count = ccount;
1080     } else if (q->count > ccount + PS2_QUEUE_SIZE) {
1081         q->count = ccount + PS2_QUEUE_SIZE;
1082     }
1083 
1084     /* sanitize rptr and recalculate wptr and cwptr */
1085     q->rptr = q->rptr & (PS2_BUFFER_SIZE - 1);
1086     q->wptr = (q->rptr + q->count) & (PS2_BUFFER_SIZE - 1);
1087     q->cwptr = ccount ? (q->rptr + ccount) & (PS2_BUFFER_SIZE - 1) : -1;
1088 }
1089 
1090 static void ps2_kbd_reset(void *opaque)
1091 {
1092     PS2KbdState *s = (PS2KbdState *) opaque;
1093     PS2State *ps2 = PS2_DEVICE(s);
1094 
1095     trace_ps2_kbd_reset(opaque);
1096     ps2_common_reset(ps2);
1097     s->scan_enabled = 1;
1098     s->translate = 0;
1099     s->scancode_set = 2;
1100     s->modifiers = 0;
1101 }
1102 
1103 static void ps2_mouse_reset(void *opaque)
1104 {
1105     PS2MouseState *s = (PS2MouseState *) opaque;
1106     PS2State *ps2 = PS2_DEVICE(s);
1107 
1108     trace_ps2_mouse_reset(opaque);
1109     ps2_common_reset(ps2);
1110     s->mouse_status = 0;
1111     s->mouse_resolution = 0;
1112     s->mouse_sample_rate = 0;
1113     s->mouse_wrap = 0;
1114     s->mouse_type = 0;
1115     s->mouse_detect_state = 0;
1116     s->mouse_dx = 0;
1117     s->mouse_dy = 0;
1118     s->mouse_dz = 0;
1119     s->mouse_dw = 0;
1120     s->mouse_buttons = 0;
1121 }
1122 
1123 static const VMStateDescription vmstate_ps2_common = {
1124     .name = "PS2 Common State",
1125     .version_id = 3,
1126     .minimum_version_id = 2,
1127     .fields = (VMStateField[]) {
1128         VMSTATE_INT32(write_cmd, PS2State),
1129         VMSTATE_INT32(queue.rptr, PS2State),
1130         VMSTATE_INT32(queue.wptr, PS2State),
1131         VMSTATE_INT32(queue.count, PS2State),
1132         VMSTATE_BUFFER(queue.data, PS2State),
1133         VMSTATE_END_OF_LIST()
1134     }
1135 };
1136 
1137 static bool ps2_keyboard_ledstate_needed(void *opaque)
1138 {
1139     PS2KbdState *s = opaque;
1140 
1141     return s->ledstate != 0; /* 0 is default state */
1142 }
1143 
1144 static int ps2_kbd_ledstate_post_load(void *opaque, int version_id)
1145 {
1146     PS2KbdState *s = opaque;
1147 
1148     kbd_put_ledstate(s->ledstate);
1149     return 0;
1150 }
1151 
1152 static const VMStateDescription vmstate_ps2_keyboard_ledstate = {
1153     .name = "ps2kbd/ledstate",
1154     .version_id = 3,
1155     .minimum_version_id = 2,
1156     .post_load = ps2_kbd_ledstate_post_load,
1157     .needed = ps2_keyboard_ledstate_needed,
1158     .fields = (VMStateField[]) {
1159         VMSTATE_INT32(ledstate, PS2KbdState),
1160         VMSTATE_END_OF_LIST()
1161     }
1162 };
1163 
1164 static bool ps2_keyboard_need_high_bit_needed(void *opaque)
1165 {
1166     PS2KbdState *s = opaque;
1167     return s->need_high_bit != 0; /* 0 is the usual state */
1168 }
1169 
1170 static const VMStateDescription vmstate_ps2_keyboard_need_high_bit = {
1171     .name = "ps2kbd/need_high_bit",
1172     .version_id = 1,
1173     .minimum_version_id = 1,
1174     .needed = ps2_keyboard_need_high_bit_needed,
1175     .fields = (VMStateField[]) {
1176         VMSTATE_BOOL(need_high_bit, PS2KbdState),
1177         VMSTATE_END_OF_LIST()
1178     }
1179 };
1180 
1181 static bool ps2_keyboard_cqueue_needed(void *opaque)
1182 {
1183     PS2KbdState *s = opaque;
1184     PS2State *ps2 = PS2_DEVICE(s);
1185 
1186     return ps2->queue.cwptr != -1; /* the queue is mostly empty */
1187 }
1188 
1189 static const VMStateDescription vmstate_ps2_keyboard_cqueue = {
1190     .name = "ps2kbd/command_reply_queue",
1191     .needed = ps2_keyboard_cqueue_needed,
1192     .fields = (VMStateField[]) {
1193         VMSTATE_INT32(parent_obj.queue.cwptr, PS2KbdState),
1194         VMSTATE_END_OF_LIST()
1195     }
1196 };
1197 
1198 static int ps2_kbd_post_load(void *opaque, int version_id)
1199 {
1200     PS2KbdState *s = (PS2KbdState *)opaque;
1201     PS2State *ps2 = PS2_DEVICE(s);
1202 
1203     if (version_id == 2) {
1204         s->scancode_set = 2;
1205     }
1206 
1207     ps2_common_post_load(ps2);
1208 
1209     return 0;
1210 }
1211 
1212 static const VMStateDescription vmstate_ps2_keyboard = {
1213     .name = "ps2kbd",
1214     .version_id = 3,
1215     .minimum_version_id = 2,
1216     .post_load = ps2_kbd_post_load,
1217     .fields = (VMStateField[]) {
1218         VMSTATE_STRUCT(parent_obj, PS2KbdState, 0, vmstate_ps2_common,
1219                        PS2State),
1220         VMSTATE_INT32(scan_enabled, PS2KbdState),
1221         VMSTATE_INT32(translate, PS2KbdState),
1222         VMSTATE_INT32_V(scancode_set, PS2KbdState, 3),
1223         VMSTATE_END_OF_LIST()
1224     },
1225     .subsections = (const VMStateDescription * []) {
1226         &vmstate_ps2_keyboard_ledstate,
1227         &vmstate_ps2_keyboard_need_high_bit,
1228         &vmstate_ps2_keyboard_cqueue,
1229         NULL
1230     }
1231 };
1232 
1233 static int ps2_mouse_post_load(void *opaque, int version_id)
1234 {
1235     PS2MouseState *s = (PS2MouseState *)opaque;
1236     PS2State *ps2 = PS2_DEVICE(s);
1237 
1238     ps2_common_post_load(ps2);
1239 
1240     return 0;
1241 }
1242 
1243 static const VMStateDescription vmstate_ps2_mouse = {
1244     .name = "ps2mouse",
1245     .version_id = 2,
1246     .minimum_version_id = 2,
1247     .post_load = ps2_mouse_post_load,
1248     .fields = (VMStateField[]) {
1249         VMSTATE_STRUCT(parent_obj, PS2MouseState, 0, vmstate_ps2_common,
1250                        PS2State),
1251         VMSTATE_UINT8(mouse_status, PS2MouseState),
1252         VMSTATE_UINT8(mouse_resolution, PS2MouseState),
1253         VMSTATE_UINT8(mouse_sample_rate, PS2MouseState),
1254         VMSTATE_UINT8(mouse_wrap, PS2MouseState),
1255         VMSTATE_UINT8(mouse_type, PS2MouseState),
1256         VMSTATE_UINT8(mouse_detect_state, PS2MouseState),
1257         VMSTATE_INT32(mouse_dx, PS2MouseState),
1258         VMSTATE_INT32(mouse_dy, PS2MouseState),
1259         VMSTATE_INT32(mouse_dz, PS2MouseState),
1260         VMSTATE_UINT8(mouse_buttons, PS2MouseState),
1261         VMSTATE_END_OF_LIST()
1262     }
1263 };
1264 
1265 static QemuInputHandler ps2_keyboard_handler = {
1266     .name  = "QEMU PS/2 Keyboard",
1267     .mask  = INPUT_EVENT_MASK_KEY,
1268     .event = ps2_keyboard_event,
1269 };
1270 
1271 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
1272 {
1273     DeviceState *dev;
1274     PS2KbdState *s;
1275     PS2State *ps2;
1276 
1277     dev = qdev_new(TYPE_PS2_KBD_DEVICE);
1278     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1279     s = PS2_KBD_DEVICE(dev);
1280     ps2 = PS2_DEVICE(s);
1281 
1282     trace_ps2_kbd_init(s);
1283     ps2->update_irq = update_irq;
1284     ps2->update_arg = update_arg;
1285     s->scancode_set = 2;
1286     vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
1287     qemu_input_handler_register((DeviceState *)s,
1288                                 &ps2_keyboard_handler);
1289     qemu_register_reset(ps2_kbd_reset, s);
1290     return s;
1291 }
1292 
1293 static QemuInputHandler ps2_mouse_handler = {
1294     .name  = "QEMU PS/2 Mouse",
1295     .mask  = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
1296     .event = ps2_mouse_event,
1297     .sync  = ps2_mouse_sync,
1298 };
1299 
1300 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
1301 {
1302     DeviceState *dev;
1303     PS2MouseState *s;
1304     PS2State *ps2;
1305 
1306     dev = qdev_new(TYPE_PS2_MOUSE_DEVICE);
1307     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1308     s = PS2_MOUSE_DEVICE(dev);
1309     ps2 = PS2_DEVICE(s);
1310 
1311     trace_ps2_mouse_init(s);
1312     ps2->update_irq = update_irq;
1313     ps2->update_arg = update_arg;
1314     vmstate_register(NULL, 0, &vmstate_ps2_mouse, s);
1315     qemu_input_handler_register((DeviceState *)s,
1316                                 &ps2_mouse_handler);
1317     qemu_register_reset(ps2_mouse_reset, s);
1318     return s;
1319 }
1320 
1321 static const TypeInfo ps2_kbd_info = {
1322     .name          = TYPE_PS2_KBD_DEVICE,
1323     .parent        = TYPE_PS2_DEVICE,
1324     .instance_size = sizeof(PS2KbdState),
1325 };
1326 
1327 static const TypeInfo ps2_mouse_info = {
1328     .name          = TYPE_PS2_MOUSE_DEVICE,
1329     .parent        = TYPE_PS2_DEVICE,
1330     .instance_size = sizeof(PS2MouseState),
1331 };
1332 
1333 static void ps2_class_init(ObjectClass *klass, void *data)
1334 {
1335     DeviceClass *dc = DEVICE_CLASS(klass);
1336 
1337     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1338 }
1339 
1340 static const TypeInfo ps2_info = {
1341     .name          = TYPE_PS2_DEVICE,
1342     .parent        = TYPE_SYS_BUS_DEVICE,
1343     .instance_size = sizeof(PS2State),
1344     .class_init    = ps2_class_init,
1345     .abstract      = true
1346 };
1347 
1348 static void ps2_register_types(void)
1349 {
1350     type_register_static(&ps2_info);
1351     type_register_static(&ps2_kbd_info);
1352     type_register_static(&ps2_mouse_info);
1353 }
1354 
1355 type_init(ps2_register_types)
1356