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