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