2 * QEMU PS/2 keyboard/mouse emulation
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
27 #include "hw/input/ps2.h"
28 #include "migration/vmstate.h"
29 #include "ui/console.h"
31 #include "sysemu/reset.h"
32 #include "sysemu/runstate.h"
36 /* debug PC keyboard */
39 /* debug PC keyboard : only mouse */
42 /* Keyboard Commands */
43 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
44 #define KBD_CMD_ECHO 0xEE
45 #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */
46 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
47 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
48 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
49 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
50 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
51 #define KBD_CMD_RESET 0xFF /* Reset */
52 #define KBD_CMD_SET_MAKE_BREAK 0xFC /* Set Make and Break mode */
53 #define KBD_CMD_SET_TYPEMATIC 0xFA /* Set Typematic Make and Break mode */
55 /* Keyboard Replies */
56 #define KBD_REPLY_POR 0xAA /* Power on reset */
57 #define KBD_REPLY_ID 0xAB /* Keyboard ID */
58 #define KBD_REPLY_ACK 0xFA /* Command ACK */
59 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
62 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
63 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
64 #define AUX_SET_RES 0xE8 /* Set resolution */
65 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
66 #define AUX_SET_STREAM 0xEA /* Set stream mode */
67 #define AUX_POLL 0xEB /* Poll */
68 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
69 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
70 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
71 #define AUX_GET_TYPE 0xF2 /* Get type */
72 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
73 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
74 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
75 #define AUX_SET_DEFAULT 0xF6
76 #define AUX_RESET 0xFF /* Reset aux device */
77 #define AUX_ACK 0xFA /* Command byte ACK. */
79 #define MOUSE_STATUS_REMOTE 0x40
80 #define MOUSE_STATUS_ENABLED 0x20
81 #define MOUSE_STATUS_SCALE21 0x10
83 #define PS2_QUEUE_SIZE 16 /* Buffer size required by PS/2 protocol */
85 /* Bits for 'modifiers' field in PS2KbdState */
86 #define MOD_CTRL_L (1 << 0)
87 #define MOD_SHIFT_L (1 << 1)
88 #define MOD_ALT_L (1 << 2)
89 #define MOD_CTRL_R (1 << 3)
90 #define MOD_SHIFT_R (1 << 4)
91 #define MOD_ALT_R (1 << 5)
94 /* Keep the data array 256 bytes long, which compatibility
95 with older qemu versions. */
97 int rptr
, wptr
, count
;
103 void (*update_irq
)(void *, int);
111 int scancode_set
; /* 1=XT, 2=AT, 3=PS/2 */
114 unsigned int modifiers
; /* bitmask of MOD_* constants above */
119 uint8_t mouse_status
;
120 uint8_t mouse_resolution
;
121 uint8_t mouse_sample_rate
;
123 uint8_t mouse_type
; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
124 uint8_t mouse_detect_state
;
125 int mouse_dx
; /* current values, needed for 'poll' mode */
128 uint8_t mouse_buttons
;
131 static uint8_t translate_table
[256] = {
132 0xff, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x3c, 0x58,
133 0x64, 0x44, 0x42, 0x40, 0x3e, 0x0f, 0x29, 0x59,
134 0x65, 0x38, 0x2a, 0x70, 0x1d, 0x10, 0x02, 0x5a,
135 0x66, 0x71, 0x2c, 0x1f, 0x1e, 0x11, 0x03, 0x5b,
136 0x67, 0x2e, 0x2d, 0x20, 0x12, 0x05, 0x04, 0x5c,
137 0x68, 0x39, 0x2f, 0x21, 0x14, 0x13, 0x06, 0x5d,
138 0x69, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, 0x5e,
139 0x6a, 0x72, 0x32, 0x24, 0x16, 0x08, 0x09, 0x5f,
140 0x6b, 0x33, 0x25, 0x17, 0x18, 0x0b, 0x0a, 0x60,
141 0x6c, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0c, 0x61,
142 0x6d, 0x73, 0x28, 0x74, 0x1a, 0x0d, 0x62, 0x6e,
143 0x3a, 0x36, 0x1c, 0x1b, 0x75, 0x2b, 0x63, 0x76,
144 0x55, 0x56, 0x77, 0x78, 0x79, 0x7a, 0x0e, 0x7b,
145 0x7c, 0x4f, 0x7d, 0x4b, 0x47, 0x7e, 0x7f, 0x6f,
146 0x52, 0x53, 0x50, 0x4c, 0x4d, 0x48, 0x01, 0x45,
147 0x57, 0x4e, 0x51, 0x4a, 0x37, 0x49, 0x46, 0x54,
148 0x80, 0x81, 0x82, 0x41, 0x54, 0x85, 0x86, 0x87,
149 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
150 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
151 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
152 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
153 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
154 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
155 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
156 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
157 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
158 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
159 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
160 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
161 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
162 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
163 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
166 static unsigned int ps2_modifier_bit(QKeyCode key
)
169 case Q_KEY_CODE_CTRL
:
171 case Q_KEY_CODE_CTRL_R
:
173 case Q_KEY_CODE_SHIFT
:
175 case Q_KEY_CODE_SHIFT_R
:
179 case Q_KEY_CODE_ALT_R
:
186 static void ps2_reset_queue(PS2State
*s
)
188 PS2Queue
*q
= &s
->queue
;
195 int ps2_queue_empty(PS2State
*s
)
197 return s
->queue
.count
== 0;
200 void ps2_queue_noirq(PS2State
*s
, int b
)
202 PS2Queue
*q
= &s
->queue
;
204 if (q
->count
== PS2_QUEUE_SIZE
) {
208 q
->data
[q
->wptr
] = b
;
209 if (++q
->wptr
== PS2_QUEUE_SIZE
)
214 void ps2_raise_irq(PS2State
*s
)
216 s
->update_irq(s
->update_arg
, 1);
219 void ps2_queue(PS2State
*s
, int b
)
221 ps2_queue_noirq(s
, b
);
222 s
->update_irq(s
->update_arg
, 1);
225 void ps2_queue_2(PS2State
*s
, int b1
, int b2
)
227 if (PS2_QUEUE_SIZE
- s
->queue
.count
< 2) {
231 ps2_queue_noirq(s
, b1
);
232 ps2_queue_noirq(s
, b2
);
233 s
->update_irq(s
->update_arg
, 1);
236 void ps2_queue_3(PS2State
*s
, int b1
, int b2
, int b3
)
238 if (PS2_QUEUE_SIZE
- s
->queue
.count
< 3) {
242 ps2_queue_noirq(s
, b1
);
243 ps2_queue_noirq(s
, b2
);
244 ps2_queue_noirq(s
, b3
);
245 s
->update_irq(s
->update_arg
, 1);
248 void ps2_queue_4(PS2State
*s
, int b1
, int b2
, int b3
, int b4
)
250 if (PS2_QUEUE_SIZE
- s
->queue
.count
< 4) {
254 ps2_queue_noirq(s
, b1
);
255 ps2_queue_noirq(s
, b2
);
256 ps2_queue_noirq(s
, b3
);
257 ps2_queue_noirq(s
, b4
);
258 s
->update_irq(s
->update_arg
, 1);
261 /* keycode is the untranslated scancode in the current scancode set. */
262 static void ps2_put_keycode(void *opaque
, int keycode
)
264 PS2KbdState
*s
= opaque
;
266 trace_ps2_put_keycode(opaque
, keycode
);
267 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
270 if (keycode
== 0xf0) {
271 s
->need_high_bit
= true;
272 } else if (s
->need_high_bit
) {
273 ps2_queue(&s
->common
, translate_table
[keycode
] | 0x80);
274 s
->need_high_bit
= false;
276 ps2_queue(&s
->common
, translate_table
[keycode
]);
279 ps2_queue(&s
->common
, keycode
);
283 static void ps2_keyboard_event(DeviceState
*dev
, QemuConsole
*src
,
286 PS2KbdState
*s
= (PS2KbdState
*)dev
;
287 InputKeyEvent
*key
= evt
->u
.key
.data
;
289 uint16_t keycode
= 0;
292 /* do not process events while disabled to prevent stream corruption */
293 if (!s
->scan_enabled
) {
297 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
298 assert(evt
->type
== INPUT_EVENT_KIND_KEY
);
299 qcode
= qemu_input_key_value_to_qcode(key
->key
);
301 mod
= ps2_modifier_bit(qcode
);
302 trace_ps2_keyboard_event(s
, qcode
, key
->down
, mod
, s
->modifiers
);
306 s
->modifiers
&= ~mod
;
309 if (s
->scancode_set
== 1) {
310 if (qcode
== Q_KEY_CODE_PAUSE
) {
311 if (s
->modifiers
& (MOD_CTRL_L
| MOD_CTRL_R
)) {
313 ps2_put_keycode(s
, 0xe0);
314 ps2_put_keycode(s
, 0x46);
315 ps2_put_keycode(s
, 0xe0);
316 ps2_put_keycode(s
, 0xc6);
320 ps2_put_keycode(s
, 0xe1);
321 ps2_put_keycode(s
, 0x1d);
322 ps2_put_keycode(s
, 0x45);
323 ps2_put_keycode(s
, 0xe1);
324 ps2_put_keycode(s
, 0x9d);
325 ps2_put_keycode(s
, 0xc5);
328 } else if (qcode
== Q_KEY_CODE_PRINT
) {
329 if (s
->modifiers
& MOD_ALT_L
) {
331 ps2_put_keycode(s
, 0xb8);
332 ps2_put_keycode(s
, 0x38);
333 ps2_put_keycode(s
, 0x54);
335 ps2_put_keycode(s
, 0xd4);
336 ps2_put_keycode(s
, 0xb8);
337 ps2_put_keycode(s
, 0x38);
339 } else if (s
->modifiers
& MOD_ALT_R
) {
341 ps2_put_keycode(s
, 0xe0);
342 ps2_put_keycode(s
, 0xb8);
343 ps2_put_keycode(s
, 0xe0);
344 ps2_put_keycode(s
, 0x38);
345 ps2_put_keycode(s
, 0x54);
347 ps2_put_keycode(s
, 0xd4);
348 ps2_put_keycode(s
, 0xe0);
349 ps2_put_keycode(s
, 0xb8);
350 ps2_put_keycode(s
, 0xe0);
351 ps2_put_keycode(s
, 0x38);
353 } else if (s
->modifiers
& (MOD_SHIFT_L
| MOD_CTRL_L
|
354 MOD_SHIFT_R
| MOD_CTRL_R
)) {
356 ps2_put_keycode(s
, 0xe0);
357 ps2_put_keycode(s
, 0x37);
359 ps2_put_keycode(s
, 0xe0);
360 ps2_put_keycode(s
, 0xb7);
364 ps2_put_keycode(s
, 0xe0);
365 ps2_put_keycode(s
, 0x2a);
366 ps2_put_keycode(s
, 0xe0);
367 ps2_put_keycode(s
, 0x37);
369 ps2_put_keycode(s
, 0xe0);
370 ps2_put_keycode(s
, 0xb7);
371 ps2_put_keycode(s
, 0xe0);
372 ps2_put_keycode(s
, 0xaa);
376 if (qcode
< qemu_input_map_qcode_to_atset1_len
)
377 keycode
= qemu_input_map_qcode_to_atset1
[qcode
];
379 if (keycode
& 0xff00) {
380 ps2_put_keycode(s
, keycode
>> 8);
385 ps2_put_keycode(s
, keycode
& 0xff);
387 qemu_log_mask(LOG_UNIMP
,
388 "ps2: ignoring key with qcode %d\n", qcode
);
391 } else if (s
->scancode_set
== 2) {
392 if (qcode
== Q_KEY_CODE_PAUSE
) {
393 if (s
->modifiers
& (MOD_CTRL_L
| MOD_CTRL_R
)) {
395 ps2_put_keycode(s
, 0xe0);
396 ps2_put_keycode(s
, 0x7e);
397 ps2_put_keycode(s
, 0xe0);
398 ps2_put_keycode(s
, 0xf0);
399 ps2_put_keycode(s
, 0x7e);
403 ps2_put_keycode(s
, 0xe1);
404 ps2_put_keycode(s
, 0x14);
405 ps2_put_keycode(s
, 0x77);
406 ps2_put_keycode(s
, 0xe1);
407 ps2_put_keycode(s
, 0xf0);
408 ps2_put_keycode(s
, 0x14);
409 ps2_put_keycode(s
, 0xf0);
410 ps2_put_keycode(s
, 0x77);
413 } else if (qcode
== Q_KEY_CODE_PRINT
) {
414 if (s
->modifiers
& MOD_ALT_L
) {
416 ps2_put_keycode(s
, 0xf0);
417 ps2_put_keycode(s
, 0x11);
418 ps2_put_keycode(s
, 0x11);
419 ps2_put_keycode(s
, 0x84);
421 ps2_put_keycode(s
, 0xf0);
422 ps2_put_keycode(s
, 0x84);
423 ps2_put_keycode(s
, 0xf0);
424 ps2_put_keycode(s
, 0x11);
425 ps2_put_keycode(s
, 0x11);
427 } else if (s
->modifiers
& MOD_ALT_R
) {
429 ps2_put_keycode(s
, 0xe0);
430 ps2_put_keycode(s
, 0xf0);
431 ps2_put_keycode(s
, 0x11);
432 ps2_put_keycode(s
, 0xe0);
433 ps2_put_keycode(s
, 0x11);
434 ps2_put_keycode(s
, 0x84);
436 ps2_put_keycode(s
, 0xf0);
437 ps2_put_keycode(s
, 0x84);
438 ps2_put_keycode(s
, 0xe0);
439 ps2_put_keycode(s
, 0xf0);
440 ps2_put_keycode(s
, 0x11);
441 ps2_put_keycode(s
, 0xe0);
442 ps2_put_keycode(s
, 0x11);
444 } else if (s
->modifiers
& (MOD_SHIFT_L
| MOD_CTRL_L
|
445 MOD_SHIFT_R
| MOD_CTRL_R
)) {
447 ps2_put_keycode(s
, 0xe0);
448 ps2_put_keycode(s
, 0x7c);
450 ps2_put_keycode(s
, 0xe0);
451 ps2_put_keycode(s
, 0xf0);
452 ps2_put_keycode(s
, 0x7c);
456 ps2_put_keycode(s
, 0xe0);
457 ps2_put_keycode(s
, 0x12);
458 ps2_put_keycode(s
, 0xe0);
459 ps2_put_keycode(s
, 0x7c);
461 ps2_put_keycode(s
, 0xe0);
462 ps2_put_keycode(s
, 0xf0);
463 ps2_put_keycode(s
, 0x7c);
464 ps2_put_keycode(s
, 0xe0);
465 ps2_put_keycode(s
, 0xf0);
466 ps2_put_keycode(s
, 0x12);
470 if (qcode
< qemu_input_map_qcode_to_atset2_len
)
471 keycode
= qemu_input_map_qcode_to_atset2
[qcode
];
473 if (keycode
& 0xff00) {
474 ps2_put_keycode(s
, keycode
>> 8);
477 ps2_put_keycode(s
, 0xf0);
479 ps2_put_keycode(s
, keycode
& 0xff);
481 qemu_log_mask(LOG_UNIMP
,
482 "ps2: ignoring key with qcode %d\n", qcode
);
485 } else if (s
->scancode_set
== 3) {
486 if (qcode
< qemu_input_map_qcode_to_atset3_len
)
487 keycode
= qemu_input_map_qcode_to_atset3
[qcode
];
489 /* FIXME: break code should be configured on a key by key basis */
491 ps2_put_keycode(s
, 0xf0);
493 ps2_put_keycode(s
, keycode
);
495 qemu_log_mask(LOG_UNIMP
,
496 "ps2: ignoring key with qcode %d\n", qcode
);
501 uint32_t ps2_read_data(PS2State
*s
)
506 trace_ps2_read_data(s
);
509 /* NOTE: if no data left, we return the last keyboard one
510 (needed for EMM386) */
511 /* XXX: need a timer to do things correctly */
514 index
= PS2_QUEUE_SIZE
- 1;
515 val
= q
->data
[index
];
517 val
= q
->data
[q
->rptr
];
518 if (++q
->rptr
== PS2_QUEUE_SIZE
)
521 /* reading deasserts IRQ */
522 s
->update_irq(s
->update_arg
, 0);
523 /* reassert IRQs if data left */
524 s
->update_irq(s
->update_arg
, q
->count
!= 0);
529 static void ps2_set_ledstate(PS2KbdState
*s
, int ledstate
)
531 trace_ps2_set_ledstate(s
, ledstate
);
532 s
->ledstate
= ledstate
;
533 kbd_put_ledstate(ledstate
);
536 static void ps2_reset_keyboard(PS2KbdState
*s
)
538 trace_ps2_reset_keyboard(s
);
541 ps2_reset_queue(&s
->common
);
542 ps2_set_ledstate(s
, 0);
545 void ps2_write_keyboard(void *opaque
, int val
)
547 PS2KbdState
*s
= (PS2KbdState
*)opaque
;
549 trace_ps2_write_keyboard(opaque
, val
);
550 switch(s
->common
.write_cmd
) {
555 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
558 ps2_queue(&s
->common
, KBD_REPLY_RESEND
);
561 /* We emulate a MF2 AT keyboard here */
563 ps2_queue_3(&s
->common
,
568 ps2_queue_3(&s
->common
,
574 ps2_queue(&s
->common
, KBD_CMD_ECHO
);
578 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
580 case KBD_CMD_SCANCODE
:
581 case KBD_CMD_SET_LEDS
:
582 case KBD_CMD_SET_RATE
:
583 case KBD_CMD_SET_MAKE_BREAK
:
584 s
->common
.write_cmd
= val
;
585 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
587 case KBD_CMD_RESET_DISABLE
:
588 ps2_reset_keyboard(s
);
590 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
592 case KBD_CMD_RESET_ENABLE
:
593 ps2_reset_keyboard(s
);
595 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
598 ps2_reset_keyboard(s
);
599 ps2_queue_2(&s
->common
,
603 case KBD_CMD_SET_TYPEMATIC
:
604 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
607 ps2_queue(&s
->common
, KBD_REPLY_RESEND
);
611 case KBD_CMD_SET_MAKE_BREAK
:
612 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
613 s
->common
.write_cmd
= -1;
615 case KBD_CMD_SCANCODE
:
617 if (s
->common
.queue
.count
<= PS2_QUEUE_SIZE
- 2) {
618 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
619 ps2_put_keycode(s
, s
->scancode_set
);
621 } else if (val
>= 1 && val
<= 3) {
622 s
->scancode_set
= val
;
623 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
625 ps2_queue(&s
->common
, KBD_REPLY_RESEND
);
627 s
->common
.write_cmd
= -1;
629 case KBD_CMD_SET_LEDS
:
630 ps2_set_ledstate(s
, val
);
631 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
632 s
->common
.write_cmd
= -1;
634 case KBD_CMD_SET_RATE
:
635 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
636 s
->common
.write_cmd
= -1;
641 /* Set the scancode translation mode.
643 1 = translated scancodes (used by qemu internally). */
645 void ps2_keyboard_set_translation(void *opaque
, int mode
)
647 PS2KbdState
*s
= (PS2KbdState
*)opaque
;
648 trace_ps2_keyboard_set_translation(opaque
, mode
);
652 static int ps2_mouse_send_packet(PS2MouseState
*s
)
654 const int needed
= 3 + (s
->mouse_type
- 2);
658 if (PS2_QUEUE_SIZE
- s
->common
.queue
.count
< needed
) {
665 /* XXX: increase range to 8 bits ? */
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
) {
687 ps2_queue_noirq(&s
->common
, dz1
& 0xff);
694 b
= (dz1
& 0x0f) | ((s
->mouse_buttons
& 0x18) << 1);
695 ps2_queue_noirq(&s
->common
, b
);
699 ps2_raise_irq(&s
->common
);
701 trace_ps2_mouse_send_packet(s
, dx1
, dy1
, dz1
, b
);
710 static void ps2_mouse_event(DeviceState
*dev
, QemuConsole
*src
,
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
,
720 PS2MouseState
*s
= (PS2MouseState
*)dev
;
721 InputMoveEvent
*move
;
724 /* check if deltas are recorded when disabled */
725 if (!(s
->mouse_status
& MOUSE_STATUS_ENABLED
))
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
;
738 case INPUT_EVENT_KIND_BTN
:
739 btn
= evt
->u
.btn
.data
;
741 s
->mouse_buttons
|= bmap
[btn
->button
];
742 if (btn
->button
== INPUT_BUTTON_WHEEL_UP
) {
744 } else if (btn
->button
== INPUT_BUTTON_WHEEL_DOWN
) {
748 s
->mouse_buttons
&= ~bmap
[btn
->button
];
758 static void ps2_mouse_sync(DeviceState
*dev
)
760 PS2MouseState
*s
= (PS2MouseState
*)dev
;
762 /* do not sync while disabled to prevent stream corruption */
763 if (!(s
->mouse_status
& MOUSE_STATUS_ENABLED
)) {
767 if (s
->mouse_buttons
) {
768 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, NULL
);
770 if (!(s
->mouse_status
& MOUSE_STATUS_REMOTE
)) {
771 /* if not remote, send event. Multiple events are sent if
773 while (ps2_mouse_send_packet(s
)) {
774 if (s
->mouse_dx
== 0 && s
->mouse_dy
== 0 && s
->mouse_dz
== 0)
780 void ps2_mouse_fake_event(void *opaque
)
782 PS2MouseState
*s
= opaque
;
783 trace_ps2_mouse_fake_event(opaque
);
785 ps2_mouse_sync(opaque
);
788 void ps2_write_mouse(void *opaque
, int val
)
790 PS2MouseState
*s
= (PS2MouseState
*)opaque
;
792 trace_ps2_write_mouse(opaque
, val
);
794 printf("kbd: write mouse 0x%02x\n", val
);
796 switch(s
->common
.write_cmd
) {
801 if (val
== AUX_RESET_WRAP
) {
803 ps2_queue(&s
->common
, AUX_ACK
);
805 } else if (val
!= AUX_RESET
) {
806 ps2_queue(&s
->common
, val
);
811 case AUX_SET_SCALE11
:
812 s
->mouse_status
&= ~MOUSE_STATUS_SCALE21
;
813 ps2_queue(&s
->common
, AUX_ACK
);
815 case AUX_SET_SCALE21
:
816 s
->mouse_status
|= MOUSE_STATUS_SCALE21
;
817 ps2_queue(&s
->common
, AUX_ACK
);
820 s
->mouse_status
&= ~MOUSE_STATUS_REMOTE
;
821 ps2_queue(&s
->common
, AUX_ACK
);
825 ps2_queue(&s
->common
, AUX_ACK
);
828 s
->mouse_status
|= MOUSE_STATUS_REMOTE
;
829 ps2_queue(&s
->common
, AUX_ACK
);
832 ps2_queue_2(&s
->common
,
838 s
->common
.write_cmd
= val
;
839 ps2_queue(&s
->common
, AUX_ACK
);
842 ps2_queue_4(&s
->common
,
846 s
->mouse_sample_rate
);
849 ps2_queue(&s
->common
, AUX_ACK
);
850 ps2_mouse_send_packet(s
);
853 s
->mouse_status
|= MOUSE_STATUS_ENABLED
;
854 ps2_queue(&s
->common
, AUX_ACK
);
856 case AUX_DISABLE_DEV
:
857 s
->mouse_status
&= ~MOUSE_STATUS_ENABLED
;
858 ps2_queue(&s
->common
, AUX_ACK
);
860 case AUX_SET_DEFAULT
:
861 s
->mouse_sample_rate
= 100;
862 s
->mouse_resolution
= 2;
864 ps2_queue(&s
->common
, AUX_ACK
);
867 s
->mouse_sample_rate
= 100;
868 s
->mouse_resolution
= 2;
871 ps2_reset_queue(&s
->common
);
872 ps2_queue_3(&s
->common
,
882 s
->mouse_sample_rate
= val
;
883 /* detect IMPS/2 or IMEX */
884 switch(s
->mouse_detect_state
) {
888 s
->mouse_detect_state
= 1;
892 s
->mouse_detect_state
= 2;
894 s
->mouse_detect_state
= 3;
896 s
->mouse_detect_state
= 0;
900 s
->mouse_type
= 3; /* IMPS/2 */
901 s
->mouse_detect_state
= 0;
905 s
->mouse_type
= 4; /* IMEX */
906 s
->mouse_detect_state
= 0;
909 ps2_queue(&s
->common
, AUX_ACK
);
910 s
->common
.write_cmd
= -1;
913 s
->mouse_resolution
= val
;
914 ps2_queue(&s
->common
, AUX_ACK
);
915 s
->common
.write_cmd
= -1;
920 static void ps2_common_reset(PS2State
*s
)
924 s
->update_irq(s
->update_arg
, 0);
927 static void ps2_common_post_load(PS2State
*s
)
929 PS2Queue
*q
= &s
->queue
;
931 uint8_t tmp_data
[PS2_QUEUE_SIZE
];
933 /* set the useful data buffer queue size, < PS2_QUEUE_SIZE */
937 } else if (q
->count
> PS2_QUEUE_SIZE
) {
938 size
= PS2_QUEUE_SIZE
;
941 /* move the queue elements to the start of data array */
942 for (i
= 0; i
< size
; i
++) {
943 if (q
->rptr
< 0 || q
->rptr
>= sizeof(q
->data
)) {
946 tmp_data
[i
] = q
->data
[q
->rptr
++];
948 memcpy(q
->data
, tmp_data
, size
);
950 /* reset rptr/wptr/count */
952 q
->wptr
= (size
== PS2_QUEUE_SIZE
) ? 0 : size
;
956 static void ps2_kbd_reset(void *opaque
)
958 PS2KbdState
*s
= (PS2KbdState
*) opaque
;
960 trace_ps2_kbd_reset(opaque
);
961 ps2_common_reset(&s
->common
);
968 static void ps2_mouse_reset(void *opaque
)
970 PS2MouseState
*s
= (PS2MouseState
*) opaque
;
972 trace_ps2_mouse_reset(opaque
);
973 ps2_common_reset(&s
->common
);
975 s
->mouse_resolution
= 0;
976 s
->mouse_sample_rate
= 0;
979 s
->mouse_detect_state
= 0;
983 s
->mouse_buttons
= 0;
986 static const VMStateDescription vmstate_ps2_common
= {
987 .name
= "PS2 Common State",
989 .minimum_version_id
= 2,
990 .fields
= (VMStateField
[]) {
991 VMSTATE_INT32(write_cmd
, PS2State
),
992 VMSTATE_INT32(queue
.rptr
, PS2State
),
993 VMSTATE_INT32(queue
.wptr
, PS2State
),
994 VMSTATE_INT32(queue
.count
, PS2State
),
995 VMSTATE_BUFFER(queue
.data
, PS2State
),
996 VMSTATE_END_OF_LIST()
1000 static bool ps2_keyboard_ledstate_needed(void *opaque
)
1002 PS2KbdState
*s
= opaque
;
1004 return s
->ledstate
!= 0; /* 0 is default state */
1007 static int ps2_kbd_ledstate_post_load(void *opaque
, int version_id
)
1009 PS2KbdState
*s
= opaque
;
1011 kbd_put_ledstate(s
->ledstate
);
1015 static const VMStateDescription vmstate_ps2_keyboard_ledstate
= {
1016 .name
= "ps2kbd/ledstate",
1018 .minimum_version_id
= 2,
1019 .post_load
= ps2_kbd_ledstate_post_load
,
1020 .needed
= ps2_keyboard_ledstate_needed
,
1021 .fields
= (VMStateField
[]) {
1022 VMSTATE_INT32(ledstate
, PS2KbdState
),
1023 VMSTATE_END_OF_LIST()
1027 static bool ps2_keyboard_need_high_bit_needed(void *opaque
)
1029 PS2KbdState
*s
= opaque
;
1030 return s
->need_high_bit
!= 0; /* 0 is the usual state */
1033 static const VMStateDescription vmstate_ps2_keyboard_need_high_bit
= {
1034 .name
= "ps2kbd/need_high_bit",
1036 .minimum_version_id
= 1,
1037 .needed
= ps2_keyboard_need_high_bit_needed
,
1038 .fields
= (VMStateField
[]) {
1039 VMSTATE_BOOL(need_high_bit
, PS2KbdState
),
1040 VMSTATE_END_OF_LIST()
1044 static int ps2_kbd_post_load(void* opaque
, int version_id
)
1046 PS2KbdState
*s
= (PS2KbdState
*)opaque
;
1047 PS2State
*ps2
= &s
->common
;
1049 if (version_id
== 2)
1052 ps2_common_post_load(ps2
);
1057 static int ps2_kbd_pre_save(void *opaque
)
1059 PS2KbdState
*s
= (PS2KbdState
*)opaque
;
1060 PS2State
*ps2
= &s
->common
;
1062 ps2_common_post_load(ps2
);
1067 static const VMStateDescription vmstate_ps2_keyboard
= {
1070 .minimum_version_id
= 2,
1071 .post_load
= ps2_kbd_post_load
,
1072 .pre_save
= ps2_kbd_pre_save
,
1073 .fields
= (VMStateField
[]) {
1074 VMSTATE_STRUCT(common
, PS2KbdState
, 0, vmstate_ps2_common
, PS2State
),
1075 VMSTATE_INT32(scan_enabled
, PS2KbdState
),
1076 VMSTATE_INT32(translate
, PS2KbdState
),
1077 VMSTATE_INT32_V(scancode_set
, PS2KbdState
,3),
1078 VMSTATE_END_OF_LIST()
1080 .subsections
= (const VMStateDescription
*[]) {
1081 &vmstate_ps2_keyboard_ledstate
,
1082 &vmstate_ps2_keyboard_need_high_bit
,
1087 static int ps2_mouse_post_load(void *opaque
, int version_id
)
1089 PS2MouseState
*s
= (PS2MouseState
*)opaque
;
1090 PS2State
*ps2
= &s
->common
;
1092 ps2_common_post_load(ps2
);
1097 static int ps2_mouse_pre_save(void *opaque
)
1099 PS2MouseState
*s
= (PS2MouseState
*)opaque
;
1100 PS2State
*ps2
= &s
->common
;
1102 ps2_common_post_load(ps2
);
1107 static const VMStateDescription vmstate_ps2_mouse
= {
1110 .minimum_version_id
= 2,
1111 .post_load
= ps2_mouse_post_load
,
1112 .pre_save
= ps2_mouse_pre_save
,
1113 .fields
= (VMStateField
[]) {
1114 VMSTATE_STRUCT(common
, PS2MouseState
, 0, vmstate_ps2_common
, PS2State
),
1115 VMSTATE_UINT8(mouse_status
, PS2MouseState
),
1116 VMSTATE_UINT8(mouse_resolution
, PS2MouseState
),
1117 VMSTATE_UINT8(mouse_sample_rate
, PS2MouseState
),
1118 VMSTATE_UINT8(mouse_wrap
, PS2MouseState
),
1119 VMSTATE_UINT8(mouse_type
, PS2MouseState
),
1120 VMSTATE_UINT8(mouse_detect_state
, PS2MouseState
),
1121 VMSTATE_INT32(mouse_dx
, PS2MouseState
),
1122 VMSTATE_INT32(mouse_dy
, PS2MouseState
),
1123 VMSTATE_INT32(mouse_dz
, PS2MouseState
),
1124 VMSTATE_UINT8(mouse_buttons
, PS2MouseState
),
1125 VMSTATE_END_OF_LIST()
1129 static QemuInputHandler ps2_keyboard_handler
= {
1130 .name
= "QEMU PS/2 Keyboard",
1131 .mask
= INPUT_EVENT_MASK_KEY
,
1132 .event
= ps2_keyboard_event
,
1135 void *ps2_kbd_init(void (*update_irq
)(void *, int), void *update_arg
)
1137 PS2KbdState
*s
= (PS2KbdState
*)g_malloc0(sizeof(PS2KbdState
));
1139 trace_ps2_kbd_init(s
);
1140 s
->common
.update_irq
= update_irq
;
1141 s
->common
.update_arg
= update_arg
;
1142 s
->scancode_set
= 2;
1143 vmstate_register(NULL
, 0, &vmstate_ps2_keyboard
, s
);
1144 qemu_input_handler_register((DeviceState
*)s
,
1145 &ps2_keyboard_handler
);
1146 qemu_register_reset(ps2_kbd_reset
, s
);
1150 static QemuInputHandler ps2_mouse_handler
= {
1151 .name
= "QEMU PS/2 Mouse",
1152 .mask
= INPUT_EVENT_MASK_BTN
| INPUT_EVENT_MASK_REL
,
1153 .event
= ps2_mouse_event
,
1154 .sync
= ps2_mouse_sync
,
1157 void *ps2_mouse_init(void (*update_irq
)(void *, int), void *update_arg
)
1159 PS2MouseState
*s
= (PS2MouseState
*)g_malloc0(sizeof(PS2MouseState
));
1161 trace_ps2_mouse_init(s
);
1162 s
->common
.update_irq
= update_irq
;
1163 s
->common
.update_arg
= update_arg
;
1164 vmstate_register(NULL
, 0, &vmstate_ps2_mouse
, s
);
1165 qemu_input_handler_register((DeviceState
*)s
,
1166 &ps2_mouse_handler
);
1167 qemu_register_reset(ps2_mouse_reset
, s
);