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