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