xref: /qemu/hw/input/ps2.c (revision 7704bb02dd73070b218fb091cdda79679dab2b8f) !
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     if (PS2_QUEUE_SIZE - s->queue.count < 1) {
216         return;
217     }
218 
219     ps2_queue_noirq(s, b);
220     s->update_irq(s->update_arg, 1);
221 }
222 
223 void ps2_queue_2(PS2State *s, int b1, int b2)
224 {
225     if (PS2_QUEUE_SIZE - s->queue.count < 2) {
226         return;
227     }
228 
229     ps2_queue_noirq(s, b1);
230     ps2_queue_noirq(s, b2);
231     s->update_irq(s->update_arg, 1);
232 }
233 
234 void ps2_queue_3(PS2State *s, int b1, int b2, int b3)
235 {
236     if (PS2_QUEUE_SIZE - s->queue.count < 3) {
237         return;
238     }
239 
240     ps2_queue_noirq(s, b1);
241     ps2_queue_noirq(s, b2);
242     ps2_queue_noirq(s, b3);
243     s->update_irq(s->update_arg, 1);
244 }
245 
246 void ps2_queue_4(PS2State *s, int b1, int b2, int b3, int b4)
247 {
248     if (PS2_QUEUE_SIZE - s->queue.count < 4) {
249         return;
250     }
251 
252     ps2_queue_noirq(s, b1);
253     ps2_queue_noirq(s, b2);
254     ps2_queue_noirq(s, b3);
255     ps2_queue_noirq(s, b4);
256     s->update_irq(s->update_arg, 1);
257 }
258 
259 /* keycode is the untranslated scancode in the current scancode set. */
260 static void ps2_put_keycode(void *opaque, int keycode)
261 {
262     PS2KbdState *s = opaque;
263 
264     trace_ps2_put_keycode(opaque, keycode);
265     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
266 
267     if (s->translate) {
268         if (keycode == 0xf0) {
269             s->need_high_bit = true;
270         } else if (s->need_high_bit) {
271             ps2_queue(&s->common, translate_table[keycode] | 0x80);
272             s->need_high_bit = false;
273         } else {
274             ps2_queue(&s->common, translate_table[keycode]);
275         }
276     } else {
277         ps2_queue(&s->common, keycode);
278     }
279 }
280 
281 static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
282                                InputEvent *evt)
283 {
284     PS2KbdState *s = (PS2KbdState *)dev;
285     InputKeyEvent *key = evt->u.key.data;
286     int qcode;
287     uint16_t keycode = 0;
288     int mod;
289 
290     /* do not process events while disabled to prevent stream corruption */
291     if (!s->scan_enabled) {
292         return;
293     }
294 
295     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
296     assert(evt->type == INPUT_EVENT_KIND_KEY);
297     qcode = qemu_input_key_value_to_qcode(key->key);
298 
299     mod = ps2_modifier_bit(qcode);
300     trace_ps2_keyboard_event(s, qcode, key->down, mod,
301                              s->modifiers, s->scancode_set, s->translate);
302     if (key->down) {
303         s->modifiers |= mod;
304     } else {
305         s->modifiers &= ~mod;
306     }
307 
308     if (s->scancode_set == 1) {
309         if (qcode == Q_KEY_CODE_PAUSE) {
310             if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) {
311                 if (key->down) {
312                     ps2_put_keycode(s, 0xe0);
313                     ps2_put_keycode(s, 0x46);
314                     ps2_put_keycode(s, 0xe0);
315                     ps2_put_keycode(s, 0xc6);
316                 }
317             } else {
318                 if (key->down) {
319                     ps2_put_keycode(s, 0xe1);
320                     ps2_put_keycode(s, 0x1d);
321                     ps2_put_keycode(s, 0x45);
322                     ps2_put_keycode(s, 0xe1);
323                     ps2_put_keycode(s, 0x9d);
324                     ps2_put_keycode(s, 0xc5);
325                 }
326             }
327         } else if (qcode == Q_KEY_CODE_PRINT) {
328             if (s->modifiers & MOD_ALT_L) {
329                 if (key->down) {
330                     ps2_put_keycode(s, 0xb8);
331                     ps2_put_keycode(s, 0x38);
332                     ps2_put_keycode(s, 0x54);
333                 } else {
334                     ps2_put_keycode(s, 0xd4);
335                     ps2_put_keycode(s, 0xb8);
336                     ps2_put_keycode(s, 0x38);
337                 }
338             } else if (s->modifiers & MOD_ALT_R) {
339                 if (key->down) {
340                     ps2_put_keycode(s, 0xe0);
341                     ps2_put_keycode(s, 0xb8);
342                     ps2_put_keycode(s, 0xe0);
343                     ps2_put_keycode(s, 0x38);
344                     ps2_put_keycode(s, 0x54);
345                 } else {
346                     ps2_put_keycode(s, 0xd4);
347                     ps2_put_keycode(s, 0xe0);
348                     ps2_put_keycode(s, 0xb8);
349                     ps2_put_keycode(s, 0xe0);
350                     ps2_put_keycode(s, 0x38);
351                 }
352             } else if (s->modifiers & (MOD_SHIFT_L | MOD_CTRL_L |
353                                        MOD_SHIFT_R | MOD_CTRL_R)) {
354                 if (key->down) {
355                     ps2_put_keycode(s, 0xe0);
356                     ps2_put_keycode(s, 0x37);
357                 } else {
358                     ps2_put_keycode(s, 0xe0);
359                     ps2_put_keycode(s, 0xb7);
360                 }
361             } else {
362                 if (key->down) {
363                     ps2_put_keycode(s, 0xe0);
364                     ps2_put_keycode(s, 0x2a);
365                     ps2_put_keycode(s, 0xe0);
366                     ps2_put_keycode(s, 0x37);
367                 } else {
368                     ps2_put_keycode(s, 0xe0);
369                     ps2_put_keycode(s, 0xb7);
370                     ps2_put_keycode(s, 0xe0);
371                     ps2_put_keycode(s, 0xaa);
372                 }
373             }
374         } else {
375             if (qcode < qemu_input_map_qcode_to_atset1_len)
376                 keycode = qemu_input_map_qcode_to_atset1[qcode];
377             if (keycode) {
378                 if (keycode & 0xff00) {
379                     ps2_put_keycode(s, keycode >> 8);
380                 }
381                 if (!key->down) {
382                     keycode |= 0x80;
383                 }
384                 ps2_put_keycode(s, keycode & 0xff);
385             } else {
386                 qemu_log_mask(LOG_UNIMP,
387                               "ps2: ignoring key with qcode %d\n", qcode);
388             }
389         }
390     } else if (s->scancode_set == 2) {
391         if (qcode == Q_KEY_CODE_PAUSE) {
392             if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) {
393                 if (key->down) {
394                     ps2_put_keycode(s, 0xe0);
395                     ps2_put_keycode(s, 0x7e);
396                     ps2_put_keycode(s, 0xe0);
397                     ps2_put_keycode(s, 0xf0);
398                     ps2_put_keycode(s, 0x7e);
399                 }
400             } else {
401                 if (key->down) {
402                     ps2_put_keycode(s, 0xe1);
403                     ps2_put_keycode(s, 0x14);
404                     ps2_put_keycode(s, 0x77);
405                     ps2_put_keycode(s, 0xe1);
406                     ps2_put_keycode(s, 0xf0);
407                     ps2_put_keycode(s, 0x14);
408                     ps2_put_keycode(s, 0xf0);
409                     ps2_put_keycode(s, 0x77);
410                 }
411             }
412         } else if (qcode == Q_KEY_CODE_PRINT) {
413             if (s->modifiers & MOD_ALT_L) {
414                 if (key->down) {
415                     ps2_put_keycode(s, 0xf0);
416                     ps2_put_keycode(s, 0x11);
417                     ps2_put_keycode(s, 0x11);
418                     ps2_put_keycode(s, 0x84);
419                 } else {
420                     ps2_put_keycode(s, 0xf0);
421                     ps2_put_keycode(s, 0x84);
422                     ps2_put_keycode(s, 0xf0);
423                     ps2_put_keycode(s, 0x11);
424                     ps2_put_keycode(s, 0x11);
425                 }
426             } else if (s->modifiers & MOD_ALT_R) {
427                 if (key->down) {
428                     ps2_put_keycode(s, 0xe0);
429                     ps2_put_keycode(s, 0xf0);
430                     ps2_put_keycode(s, 0x11);
431                     ps2_put_keycode(s, 0xe0);
432                     ps2_put_keycode(s, 0x11);
433                     ps2_put_keycode(s, 0x84);
434                 } else {
435                     ps2_put_keycode(s, 0xf0);
436                     ps2_put_keycode(s, 0x84);
437                     ps2_put_keycode(s, 0xe0);
438                     ps2_put_keycode(s, 0xf0);
439                     ps2_put_keycode(s, 0x11);
440                     ps2_put_keycode(s, 0xe0);
441                     ps2_put_keycode(s, 0x11);
442                 }
443             } else if (s->modifiers & (MOD_SHIFT_L | MOD_CTRL_L |
444                                        MOD_SHIFT_R | MOD_CTRL_R)) {
445                 if (key->down) {
446                     ps2_put_keycode(s, 0xe0);
447                     ps2_put_keycode(s, 0x7c);
448                 } else {
449                     ps2_put_keycode(s, 0xe0);
450                     ps2_put_keycode(s, 0xf0);
451                     ps2_put_keycode(s, 0x7c);
452                 }
453             } else {
454                 if (key->down) {
455                     ps2_put_keycode(s, 0xe0);
456                     ps2_put_keycode(s, 0x12);
457                     ps2_put_keycode(s, 0xe0);
458                     ps2_put_keycode(s, 0x7c);
459                 } else {
460                     ps2_put_keycode(s, 0xe0);
461                     ps2_put_keycode(s, 0xf0);
462                     ps2_put_keycode(s, 0x7c);
463                     ps2_put_keycode(s, 0xe0);
464                     ps2_put_keycode(s, 0xf0);
465                     ps2_put_keycode(s, 0x12);
466                 }
467             }
468         } else {
469             if (qcode < qemu_input_map_qcode_to_atset2_len)
470                 keycode = qemu_input_map_qcode_to_atset2[qcode];
471             if (keycode) {
472                 if (keycode & 0xff00) {
473                     ps2_put_keycode(s, keycode >> 8);
474                 }
475                 if (!key->down) {
476                     ps2_put_keycode(s, 0xf0);
477                 }
478                 ps2_put_keycode(s, keycode & 0xff);
479             } else {
480                 qemu_log_mask(LOG_UNIMP,
481                               "ps2: ignoring key with qcode %d\n", qcode);
482             }
483         }
484     } else if (s->scancode_set == 3) {
485         if (qcode < qemu_input_map_qcode_to_atset3_len)
486             keycode = qemu_input_map_qcode_to_atset3[qcode];
487         if (keycode) {
488             /* FIXME: break code should be configured on a key by key basis */
489             if (!key->down) {
490                 ps2_put_keycode(s, 0xf0);
491             }
492             ps2_put_keycode(s, keycode);
493         } else {
494             qemu_log_mask(LOG_UNIMP,
495                           "ps2: ignoring key with qcode %d\n", qcode);
496         }
497     }
498 }
499 
500 uint32_t ps2_read_data(PS2State *s)
501 {
502     PS2Queue *q;
503     int val, index;
504 
505     trace_ps2_read_data(s);
506     q = &s->queue;
507     if (q->count == 0) {
508         /* NOTE: if no data left, we return the last keyboard one
509            (needed for EMM386) */
510         /* XXX: need a timer to do things correctly */
511         index = q->rptr - 1;
512         if (index < 0)
513             index = PS2_QUEUE_SIZE - 1;
514         val = q->data[index];
515     } else {
516         val = q->data[q->rptr];
517         if (++q->rptr == PS2_QUEUE_SIZE)
518             q->rptr = 0;
519         q->count--;
520         /* reading deasserts IRQ */
521         s->update_irq(s->update_arg, 0);
522         /* reassert IRQs if data left */
523         s->update_irq(s->update_arg, q->count != 0);
524     }
525     return val;
526 }
527 
528 static void ps2_set_ledstate(PS2KbdState *s, int ledstate)
529 {
530     trace_ps2_set_ledstate(s, ledstate);
531     s->ledstate = ledstate;
532     kbd_put_ledstate(ledstate);
533 }
534 
535 static void ps2_reset_keyboard(PS2KbdState *s)
536 {
537     trace_ps2_reset_keyboard(s);
538     s->scan_enabled = 1;
539     s->scancode_set = 2;
540     ps2_reset_queue(&s->common);
541     ps2_set_ledstate(s, 0);
542 }
543 
544 void ps2_write_keyboard(void *opaque, int val)
545 {
546     PS2KbdState *s = (PS2KbdState *)opaque;
547 
548     trace_ps2_write_keyboard(opaque, val);
549     switch(s->common.write_cmd) {
550     default:
551     case -1:
552         switch(val) {
553         case 0x00:
554             ps2_queue(&s->common, KBD_REPLY_ACK);
555             break;
556         case 0x05:
557             ps2_queue(&s->common, KBD_REPLY_RESEND);
558             break;
559         case KBD_CMD_GET_ID:
560             /* We emulate a MF2 AT keyboard here */
561             if (s->translate)
562                 ps2_queue_3(&s->common,
563                     KBD_REPLY_ACK,
564                     KBD_REPLY_ID,
565                     0x41);
566             else
567                 ps2_queue_3(&s->common,
568                     KBD_REPLY_ACK,
569                     KBD_REPLY_ID,
570                     0x83);
571             break;
572         case KBD_CMD_ECHO:
573             ps2_queue(&s->common, KBD_CMD_ECHO);
574             break;
575         case KBD_CMD_ENABLE:
576             s->scan_enabled = 1;
577             ps2_queue(&s->common, KBD_REPLY_ACK);
578             break;
579         case KBD_CMD_SCANCODE:
580         case KBD_CMD_SET_LEDS:
581         case KBD_CMD_SET_RATE:
582         case KBD_CMD_SET_MAKE_BREAK:
583             s->common.write_cmd = val;
584             ps2_queue(&s->common, KBD_REPLY_ACK);
585             break;
586         case KBD_CMD_RESET_DISABLE:
587             ps2_reset_keyboard(s);
588             s->scan_enabled = 0;
589             ps2_queue(&s->common, KBD_REPLY_ACK);
590             break;
591         case KBD_CMD_RESET_ENABLE:
592             ps2_reset_keyboard(s);
593             s->scan_enabled = 1;
594             ps2_queue(&s->common, KBD_REPLY_ACK);
595             break;
596         case KBD_CMD_RESET:
597             ps2_reset_keyboard(s);
598             ps2_queue_2(&s->common,
599                 KBD_REPLY_ACK,
600                 KBD_REPLY_POR);
601             break;
602         case KBD_CMD_SET_TYPEMATIC:
603             ps2_queue(&s->common, KBD_REPLY_ACK);
604             break;
605         default:
606             ps2_queue(&s->common, KBD_REPLY_RESEND);
607             break;
608         }
609         break;
610     case KBD_CMD_SET_MAKE_BREAK:
611         ps2_queue(&s->common, KBD_REPLY_ACK);
612         s->common.write_cmd = -1;
613         break;
614     case KBD_CMD_SCANCODE:
615         if (val == 0) {
616             if (s->common.queue.count <= PS2_QUEUE_SIZE - 2) {
617                 ps2_queue(&s->common, KBD_REPLY_ACK);
618                 ps2_put_keycode(s, s->scancode_set);
619             }
620         } else if (val >= 1 && val <= 3) {
621             s->scancode_set = val;
622             ps2_queue(&s->common, KBD_REPLY_ACK);
623         } else {
624             ps2_queue(&s->common, KBD_REPLY_RESEND);
625         }
626         s->common.write_cmd = -1;
627         break;
628     case KBD_CMD_SET_LEDS:
629         ps2_set_ledstate(s, val);
630         ps2_queue(&s->common, KBD_REPLY_ACK);
631         s->common.write_cmd = -1;
632         break;
633     case KBD_CMD_SET_RATE:
634         ps2_queue(&s->common, KBD_REPLY_ACK);
635         s->common.write_cmd = -1;
636         break;
637     }
638 }
639 
640 /* Set the scancode translation mode.
641    0 = raw scancodes.
642    1 = translated scancodes (used by qemu internally).  */
643 
644 void ps2_keyboard_set_translation(void *opaque, int mode)
645 {
646     PS2KbdState *s = (PS2KbdState *)opaque;
647     trace_ps2_keyboard_set_translation(opaque, mode);
648     s->translate = mode;
649 }
650 
651 static int ps2_mouse_send_packet(PS2MouseState *s)
652 {
653     /* IMPS/2 and IMEX send 4 bytes, PS2 sends 3 bytes */
654     const int needed = s->mouse_type ? 4 : 3;
655     unsigned int b;
656     int dx1, dy1, dz1;
657 
658     if (PS2_QUEUE_SIZE - s->common.queue.count < needed) {
659         return 0;
660     }
661 
662     dx1 = s->mouse_dx;
663     dy1 = s->mouse_dy;
664     dz1 = s->mouse_dz;
665     /* XXX: increase range to 8 bits ? */
666     if (dx1 > 127)
667         dx1 = 127;
668     else if (dx1 < -127)
669         dx1 = -127;
670     if (dy1 > 127)
671         dy1 = 127;
672     else if (dy1 < -127)
673         dy1 = -127;
674     b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
675     ps2_queue_noirq(&s->common, b);
676     ps2_queue_noirq(&s->common, dx1 & 0xff);
677     ps2_queue_noirq(&s->common, dy1 & 0xff);
678     /* extra byte for IMPS/2 or IMEX */
679     switch(s->mouse_type) {
680     default:
681         break;
682     case 3:
683         if (dz1 > 127)
684             dz1 = 127;
685         else if (dz1 < -127)
686                 dz1 = -127;
687         ps2_queue_noirq(&s->common, dz1 & 0xff);
688         break;
689     case 4:
690         if (dz1 > 7)
691             dz1 = 7;
692         else if (dz1 < -7)
693             dz1 = -7;
694         b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
695         ps2_queue_noirq(&s->common, b);
696         break;
697     }
698 
699     ps2_raise_irq(&s->common);
700 
701     trace_ps2_mouse_send_packet(s, dx1, dy1, dz1, b);
702     /* update deltas */
703     s->mouse_dx -= dx1;
704     s->mouse_dy -= dy1;
705     s->mouse_dz -= dz1;
706 
707     return 1;
708 }
709 
710 static void ps2_mouse_event(DeviceState *dev, QemuConsole *src,
711                             InputEvent *evt)
712 {
713     static const int bmap[INPUT_BUTTON__MAX] = {
714         [INPUT_BUTTON_LEFT]   = PS2_MOUSE_BUTTON_LEFT,
715         [INPUT_BUTTON_MIDDLE] = PS2_MOUSE_BUTTON_MIDDLE,
716         [INPUT_BUTTON_RIGHT]  = PS2_MOUSE_BUTTON_RIGHT,
717         [INPUT_BUTTON_SIDE]   = PS2_MOUSE_BUTTON_SIDE,
718         [INPUT_BUTTON_EXTRA]  = PS2_MOUSE_BUTTON_EXTRA,
719     };
720     PS2MouseState *s = (PS2MouseState *)dev;
721     InputMoveEvent *move;
722     InputBtnEvent *btn;
723 
724     /* check if deltas are recorded when disabled */
725     if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
726         return;
727 
728     switch (evt->type) {
729     case INPUT_EVENT_KIND_REL:
730         move = evt->u.rel.data;
731         if (move->axis == INPUT_AXIS_X) {
732             s->mouse_dx += move->value;
733         } else if (move->axis == INPUT_AXIS_Y) {
734             s->mouse_dy -= move->value;
735         }
736         break;
737 
738     case INPUT_EVENT_KIND_BTN:
739         btn = evt->u.btn.data;
740         if (btn->down) {
741             s->mouse_buttons |= bmap[btn->button];
742             if (btn->button == INPUT_BUTTON_WHEEL_UP) {
743                 s->mouse_dz--;
744             } else if (btn->button == INPUT_BUTTON_WHEEL_DOWN) {
745                 s->mouse_dz++;
746             }
747         } else {
748             s->mouse_buttons &= ~bmap[btn->button];
749         }
750         break;
751 
752     default:
753         /* keep gcc happy */
754         break;
755     }
756 }
757 
758 static void ps2_mouse_sync(DeviceState *dev)
759 {
760     PS2MouseState *s = (PS2MouseState *)dev;
761 
762     /* do not sync while disabled to prevent stream corruption */
763     if (!(s->mouse_status & MOUSE_STATUS_ENABLED)) {
764         return;
765     }
766 
767     if (s->mouse_buttons) {
768         qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
769     }
770     if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) {
771         /* if not remote, send event. Multiple events are sent if
772            too big deltas */
773         while (ps2_mouse_send_packet(s)) {
774             if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
775                 break;
776         }
777     }
778 }
779 
780 void ps2_mouse_fake_event(void *opaque)
781 {
782     PS2MouseState *s = opaque;
783     trace_ps2_mouse_fake_event(opaque);
784     s->mouse_dx++;
785     ps2_mouse_sync(opaque);
786 }
787 
788 void ps2_write_mouse(void *opaque, int val)
789 {
790     PS2MouseState *s = (PS2MouseState *)opaque;
791 
792     trace_ps2_write_mouse(opaque, val);
793     switch(s->common.write_cmd) {
794     default:
795     case -1:
796         /* mouse command */
797         if (s->mouse_wrap) {
798             if (val == AUX_RESET_WRAP) {
799                 s->mouse_wrap = 0;
800                 ps2_queue(&s->common, AUX_ACK);
801                 return;
802             } else if (val != AUX_RESET) {
803                 ps2_queue(&s->common, val);
804                 return;
805             }
806         }
807         switch(val) {
808         case AUX_SET_SCALE11:
809             s->mouse_status &= ~MOUSE_STATUS_SCALE21;
810             ps2_queue(&s->common, AUX_ACK);
811             break;
812         case AUX_SET_SCALE21:
813             s->mouse_status |= MOUSE_STATUS_SCALE21;
814             ps2_queue(&s->common, AUX_ACK);
815             break;
816         case AUX_SET_STREAM:
817             s->mouse_status &= ~MOUSE_STATUS_REMOTE;
818             ps2_queue(&s->common, AUX_ACK);
819             break;
820         case AUX_SET_WRAP:
821             s->mouse_wrap = 1;
822             ps2_queue(&s->common, AUX_ACK);
823             break;
824         case AUX_SET_REMOTE:
825             s->mouse_status |= MOUSE_STATUS_REMOTE;
826             ps2_queue(&s->common, AUX_ACK);
827             break;
828         case AUX_GET_TYPE:
829             ps2_queue_2(&s->common,
830                 AUX_ACK,
831                 s->mouse_type);
832             break;
833         case AUX_SET_RES:
834         case AUX_SET_SAMPLE:
835             s->common.write_cmd = val;
836             ps2_queue(&s->common, AUX_ACK);
837             break;
838         case AUX_GET_SCALE:
839             ps2_queue_4(&s->common,
840                 AUX_ACK,
841                 s->mouse_status,
842                 s->mouse_resolution,
843                 s->mouse_sample_rate);
844             break;
845         case AUX_POLL:
846             ps2_queue(&s->common, AUX_ACK);
847             ps2_mouse_send_packet(s);
848             break;
849         case AUX_ENABLE_DEV:
850             s->mouse_status |= MOUSE_STATUS_ENABLED;
851             ps2_queue(&s->common, AUX_ACK);
852             break;
853         case AUX_DISABLE_DEV:
854             s->mouse_status &= ~MOUSE_STATUS_ENABLED;
855             ps2_queue(&s->common, AUX_ACK);
856             break;
857         case AUX_SET_DEFAULT:
858             s->mouse_sample_rate = 100;
859             s->mouse_resolution = 2;
860             s->mouse_status = 0;
861             ps2_queue(&s->common, AUX_ACK);
862             break;
863         case AUX_RESET:
864             s->mouse_sample_rate = 100;
865             s->mouse_resolution = 2;
866             s->mouse_status = 0;
867             s->mouse_type = 0;
868             ps2_reset_queue(&s->common);
869             ps2_queue_3(&s->common,
870                 AUX_ACK,
871                 0xaa,
872                 s->mouse_type);
873             break;
874         default:
875             break;
876         }
877         break;
878     case AUX_SET_SAMPLE:
879         s->mouse_sample_rate = val;
880         /* detect IMPS/2 or IMEX */
881         switch(s->mouse_detect_state) {
882         default:
883         case 0:
884             if (val == 200)
885                 s->mouse_detect_state = 1;
886             break;
887         case 1:
888             if (val == 100)
889                 s->mouse_detect_state = 2;
890             else if (val == 200)
891                 s->mouse_detect_state = 3;
892             else
893                 s->mouse_detect_state = 0;
894             break;
895         case 2:
896             if (val == 80)
897                 s->mouse_type = 3; /* IMPS/2 */
898             s->mouse_detect_state = 0;
899             break;
900         case 3:
901             if (val == 80)
902                 s->mouse_type = 4; /* IMEX */
903             s->mouse_detect_state = 0;
904             break;
905         }
906         ps2_queue(&s->common, AUX_ACK);
907         s->common.write_cmd = -1;
908         break;
909     case AUX_SET_RES:
910         s->mouse_resolution = val;
911         ps2_queue(&s->common, AUX_ACK);
912         s->common.write_cmd = -1;
913         break;
914     }
915 }
916 
917 static void ps2_common_reset(PS2State *s)
918 {
919     s->write_cmd = -1;
920     ps2_reset_queue(s);
921     s->update_irq(s->update_arg, 0);
922 }
923 
924 static void ps2_common_post_load(PS2State *s)
925 {
926     PS2Queue *q = &s->queue;
927     uint8_t i, size;
928     uint8_t tmp_data[PS2_QUEUE_SIZE];
929 
930     /* set the useful data buffer queue size, < PS2_QUEUE_SIZE */
931     size = q->count;
932     if (q->count < 0) {
933         size = 0;
934     } else if (q->count > PS2_QUEUE_SIZE) {
935         size = PS2_QUEUE_SIZE;
936     }
937 
938     /* move the queue elements to the start of data array */
939     for (i = 0; i < size; i++) {
940         if (q->rptr < 0 || q->rptr >= sizeof(q->data)) {
941             q->rptr = 0;
942         }
943         tmp_data[i] = q->data[q->rptr++];
944     }
945     memcpy(q->data, tmp_data, size);
946 
947     /* reset rptr/wptr/count */
948     q->rptr = 0;
949     q->wptr = (size == PS2_QUEUE_SIZE) ? 0 : size;
950     q->count = size;
951 }
952 
953 static void ps2_kbd_reset(void *opaque)
954 {
955     PS2KbdState *s = (PS2KbdState *) opaque;
956 
957     trace_ps2_kbd_reset(opaque);
958     ps2_common_reset(&s->common);
959     s->scan_enabled = 1;
960     s->translate = 0;
961     s->scancode_set = 2;
962     s->modifiers = 0;
963 }
964 
965 static void ps2_mouse_reset(void *opaque)
966 {
967     PS2MouseState *s = (PS2MouseState *) opaque;
968 
969     trace_ps2_mouse_reset(opaque);
970     ps2_common_reset(&s->common);
971     s->mouse_status = 0;
972     s->mouse_resolution = 0;
973     s->mouse_sample_rate = 0;
974     s->mouse_wrap = 0;
975     s->mouse_type = 0;
976     s->mouse_detect_state = 0;
977     s->mouse_dx = 0;
978     s->mouse_dy = 0;
979     s->mouse_dz = 0;
980     s->mouse_buttons = 0;
981 }
982 
983 static const VMStateDescription vmstate_ps2_common = {
984     .name = "PS2 Common State",
985     .version_id = 3,
986     .minimum_version_id = 2,
987     .fields = (VMStateField[]) {
988         VMSTATE_INT32(write_cmd, PS2State),
989         VMSTATE_INT32(queue.rptr, PS2State),
990         VMSTATE_INT32(queue.wptr, PS2State),
991         VMSTATE_INT32(queue.count, PS2State),
992         VMSTATE_BUFFER(queue.data, PS2State),
993         VMSTATE_END_OF_LIST()
994     }
995 };
996 
997 static bool ps2_keyboard_ledstate_needed(void *opaque)
998 {
999     PS2KbdState *s = opaque;
1000 
1001     return s->ledstate != 0; /* 0 is default state */
1002 }
1003 
1004 static int ps2_kbd_ledstate_post_load(void *opaque, int version_id)
1005 {
1006     PS2KbdState *s = opaque;
1007 
1008     kbd_put_ledstate(s->ledstate);
1009     return 0;
1010 }
1011 
1012 static const VMStateDescription vmstate_ps2_keyboard_ledstate = {
1013     .name = "ps2kbd/ledstate",
1014     .version_id = 3,
1015     .minimum_version_id = 2,
1016     .post_load = ps2_kbd_ledstate_post_load,
1017     .needed = ps2_keyboard_ledstate_needed,
1018     .fields = (VMStateField[]) {
1019         VMSTATE_INT32(ledstate, PS2KbdState),
1020         VMSTATE_END_OF_LIST()
1021     }
1022 };
1023 
1024 static bool ps2_keyboard_need_high_bit_needed(void *opaque)
1025 {
1026     PS2KbdState *s = opaque;
1027     return s->need_high_bit != 0; /* 0 is the usual state */
1028 }
1029 
1030 static const VMStateDescription vmstate_ps2_keyboard_need_high_bit = {
1031     .name = "ps2kbd/need_high_bit",
1032     .version_id = 1,
1033     .minimum_version_id = 1,
1034     .needed = ps2_keyboard_need_high_bit_needed,
1035     .fields = (VMStateField[]) {
1036         VMSTATE_BOOL(need_high_bit, PS2KbdState),
1037         VMSTATE_END_OF_LIST()
1038     }
1039 };
1040 
1041 static int ps2_kbd_post_load(void* opaque, int version_id)
1042 {
1043     PS2KbdState *s = (PS2KbdState*)opaque;
1044     PS2State *ps2 = &s->common;
1045 
1046     if (version_id == 2)
1047         s->scancode_set=2;
1048 
1049     ps2_common_post_load(ps2);
1050 
1051     return 0;
1052 }
1053 
1054 static int ps2_kbd_pre_save(void *opaque)
1055 {
1056     PS2KbdState *s = (PS2KbdState *)opaque;
1057     PS2State *ps2 = &s->common;
1058 
1059     ps2_common_post_load(ps2);
1060 
1061     return 0;
1062 }
1063 
1064 static const VMStateDescription vmstate_ps2_keyboard = {
1065     .name = "ps2kbd",
1066     .version_id = 3,
1067     .minimum_version_id = 2,
1068     .post_load = ps2_kbd_post_load,
1069     .pre_save = ps2_kbd_pre_save,
1070     .fields = (VMStateField[]) {
1071         VMSTATE_STRUCT(common, PS2KbdState, 0, vmstate_ps2_common, PS2State),
1072         VMSTATE_INT32(scan_enabled, PS2KbdState),
1073         VMSTATE_INT32(translate, PS2KbdState),
1074         VMSTATE_INT32_V(scancode_set, PS2KbdState,3),
1075         VMSTATE_END_OF_LIST()
1076     },
1077     .subsections = (const VMStateDescription*[]) {
1078         &vmstate_ps2_keyboard_ledstate,
1079         &vmstate_ps2_keyboard_need_high_bit,
1080         NULL
1081     }
1082 };
1083 
1084 static int ps2_mouse_post_load(void *opaque, int version_id)
1085 {
1086     PS2MouseState *s = (PS2MouseState *)opaque;
1087     PS2State *ps2 = &s->common;
1088 
1089     ps2_common_post_load(ps2);
1090 
1091     return 0;
1092 }
1093 
1094 static int ps2_mouse_pre_save(void *opaque)
1095 {
1096     PS2MouseState *s = (PS2MouseState *)opaque;
1097     PS2State *ps2 = &s->common;
1098 
1099     ps2_common_post_load(ps2);
1100 
1101     return 0;
1102 }
1103 
1104 static const VMStateDescription vmstate_ps2_mouse = {
1105     .name = "ps2mouse",
1106     .version_id = 2,
1107     .minimum_version_id = 2,
1108     .post_load = ps2_mouse_post_load,
1109     .pre_save = ps2_mouse_pre_save,
1110     .fields = (VMStateField[]) {
1111         VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
1112         VMSTATE_UINT8(mouse_status, PS2MouseState),
1113         VMSTATE_UINT8(mouse_resolution, PS2MouseState),
1114         VMSTATE_UINT8(mouse_sample_rate, PS2MouseState),
1115         VMSTATE_UINT8(mouse_wrap, PS2MouseState),
1116         VMSTATE_UINT8(mouse_type, PS2MouseState),
1117         VMSTATE_UINT8(mouse_detect_state, PS2MouseState),
1118         VMSTATE_INT32(mouse_dx, PS2MouseState),
1119         VMSTATE_INT32(mouse_dy, PS2MouseState),
1120         VMSTATE_INT32(mouse_dz, PS2MouseState),
1121         VMSTATE_UINT8(mouse_buttons, PS2MouseState),
1122         VMSTATE_END_OF_LIST()
1123     }
1124 };
1125 
1126 static QemuInputHandler ps2_keyboard_handler = {
1127     .name  = "QEMU PS/2 Keyboard",
1128     .mask  = INPUT_EVENT_MASK_KEY,
1129     .event = ps2_keyboard_event,
1130 };
1131 
1132 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
1133 {
1134     PS2KbdState *s = (PS2KbdState *)g_malloc0(sizeof(PS2KbdState));
1135 
1136     trace_ps2_kbd_init(s);
1137     s->common.update_irq = update_irq;
1138     s->common.update_arg = update_arg;
1139     s->scancode_set = 2;
1140     vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
1141     qemu_input_handler_register((DeviceState *)s,
1142                                 &ps2_keyboard_handler);
1143     qemu_register_reset(ps2_kbd_reset, s);
1144     return s;
1145 }
1146 
1147 static QemuInputHandler ps2_mouse_handler = {
1148     .name  = "QEMU PS/2 Mouse",
1149     .mask  = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
1150     .event = ps2_mouse_event,
1151     .sync  = ps2_mouse_sync,
1152 };
1153 
1154 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
1155 {
1156     PS2MouseState *s = (PS2MouseState *)g_malloc0(sizeof(PS2MouseState));
1157 
1158     trace_ps2_mouse_init(s);
1159     s->common.update_irq = update_irq;
1160     s->common.update_arg = update_arg;
1161     vmstate_register(NULL, 0, &vmstate_ps2_mouse, s);
1162     qemu_input_handler_register((DeviceState *)s,
1163                                 &ps2_mouse_handler);
1164     qemu_register_reset(ps2_mouse_reset, s);
1165     return s;
1166 }
1167