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