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