VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/DevPS2K.cpp@ 86997

Last change on this file since 86997 was 83443, checked in by vboxsync, 5 years ago

DevPS2: Improved logging, gave queues human-readable names.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 58.2 KB
Line 
1/* $Id: DevPS2K.cpp 83443 2020-03-26 16:49:44Z vboxsync $ */
2/** @file
3 * PS2K - PS/2 keyboard emulation.
4 */
5
6/*
7 * Copyright (C) 2007-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * References:
20 *
21 * IBM PS/2 Technical Reference, Keyboards (101- and 102-Key), 1990
22 * Keyboard Scan Code Specification, Microsoft, 2000
23 *
24 * Notes:
25 * - The keyboard never sends partial scan-code sequences; if there isn't enough
26 * room left in the buffer for the entire sequence, the keystroke is discarded
27 * and an overrun code is sent instead.
28 * - Command responses do not disturb stored keystrokes and always have priority.
29 * - Caps Lock and Scroll Lock are normal keys from the keyboard's point of view.
30 * However, Num Lock is not and the keyboard internally tracks its state.
31 * - The way Print Screen works in scan set 1/2 is totally insane.
32 * - A PS/2 keyboard can send at most 1,000 to 1,500 bytes per second. There is
33 * software which relies on that fact and assumes that a scan code can be
34 * read twice before the next scan code comes in.
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP LOG_GROUP_DEV_KBD
42#include <VBox/vmm/pdmdev.h>
43#include <VBox/err.h>
44#include <iprt/assert.h>
45#include <iprt/uuid.h>
46#include "VBoxDD.h"
47#define IN_PS2K
48#include "DevPS2.h"
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54/** @name Keyboard commands sent by the system.
55 * @{ */
56#define KCMD_LEDS 0xED
57#define KCMD_ECHO 0xEE
58#define KCMD_INVALID_1 0xEF
59#define KCMD_SCANSET 0xF0
60#define KCMD_INVALID_2 0xF1
61#define KCMD_READ_ID 0xF2
62#define KCMD_RATE_DELAY 0xF3
63#define KCMD_ENABLE 0xF4
64#define KCMD_DFLT_DISABLE 0xF5
65#define KCMD_SET_DEFAULT 0xF6
66#define KCMD_ALL_TYPEMATIC 0xF7
67#define KCMD_ALL_MK_BRK 0xF8
68#define KCMD_ALL_MAKE 0xF9
69#define KCMD_ALL_TMB 0xFA
70#define KCMD_TYPE_MATIC 0xFB
71#define KCMD_TYPE_MK_BRK 0xFC
72#define KCMD_TYPE_MAKE 0xFD
73#define KCMD_RESEND 0xFE
74#define KCMD_RESET 0xFF
75/** @} */
76
77/** @name Keyboard responses sent to the system.
78 * @{ */
79#define KRSP_ID1 0xAB
80#define KRSP_ID2 0x83
81#define KRSP_BAT_OK 0xAA
82#define KRSP_BAT_FAIL 0xFC /* Also a 'release keys' signal. */
83#define KRSP_ECHO 0xEE
84#define KRSP_ACK 0xFA
85#define KRSP_RESEND 0xFE
86/** @} */
87
88/** @name Modifier key states. Sorted in USB HID code order.
89 * @{ */
90#define MOD_LCTRL 0x01
91#define MOD_LSHIFT 0x02
92#define MOD_LALT 0x04
93#define MOD_LGUI 0x08
94#define MOD_RCTRL 0x10
95#define MOD_RSHIFT 0x20
96#define MOD_RALT 0x40
97#define MOD_RGUI 0x80
98/** @} */
99
100/* Default typematic value. */
101#define KBD_DFL_RATE_DELAY 0x2B
102
103/* Input throttling delay in milliseconds. */
104#define KBD_THROTTLE_DELAY 1
105
106
107/*********************************************************************************************************************************
108* Structures and Typedefs *
109*********************************************************************************************************************************/
110/* Key type flags. */
111#define KF_E0 0x01 /* E0 prefix. */
112#define KF_NB 0x02 /* No break code. */
113#define KF_GK 0x04 /* Gray navigation key. */
114#define KF_PS 0x08 /* Print Screen key. */
115#define KF_PB 0x10 /* Pause/Break key. */
116#define KF_NL 0x20 /* Num Lock key. */
117#define KF_NS 0x40 /* NumPad '/' key. */
118
119/* Scan Set 3 typematic defaults. */
120#define T_U 0x00 /* Unknown value. */
121#define T_T 0x01 /* Key is typematic. */
122#define T_M 0x02 /* Key is make only. */
123#define T_B 0x04 /* Key is make/break. */
124
125/* Special key values. */
126#define NONE 0x93 /* No PS/2 scan code returned. */
127#define UNAS 0x94 /* No PS/2 scan assigned to key. */
128#define RSVD 0x95 /* Reserved, do not use. */
129#define UNKN 0x96 /* Translation unknown. */
130
131/* Key definition structure. */
132typedef struct {
133 uint8_t makeS1; /* Set 1 make code. */
134 uint8_t makeS2; /* Set 2 make code. */
135 uint8_t makeS3; /* Set 3 make code. */
136 uint8_t keyFlags; /* Key flags. */
137 uint8_t keyMatic; /* Set 3 typematic default. */
138} key_def;
139
140
141/*********************************************************************************************************************************
142* Global Variables *
143*********************************************************************************************************************************/
144#ifdef IN_RING3
145/* USB to PS/2 conversion table for regular keys (HID Usage Page 7). */
146static const key_def aPS2Keys[] = {
147 /* 00 */ {NONE, NONE, NONE, KF_NB, T_U }, /* Key N/A: No Event */
148 /* 01 */ {0xFF, 0x00, 0x00, KF_NB, T_U }, /* Key N/A: Overrun Error */
149 /* 02 */ {0xFC, 0xFC, 0xFC, KF_NB, T_U }, /* Key N/A: POST Fail */
150 /* 03 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key N/A: ErrorUndefined */
151 /* 04 */ {0x1E, 0x1C, 0x1C, 0, T_T }, /* Key 31: a A */
152 /* 05 */ {0x30, 0x32, 0x32, 0, T_T }, /* Key 50: b B */
153 /* 06 */ {0x2E, 0x21, 0x21, 0, T_T }, /* Key 48: c C */
154 /* 07 */ {0x20, 0x23, 0x23, 0, T_T }, /* Key 33: d D */
155 /* 08 */ {0x12, 0x24, 0x24, 0, T_T }, /* Key 19: e E */
156 /* 09 */ {0x21, 0x2B, 0x2B, 0, T_T }, /* Key 34: f F */
157 /* 0A */ {0x22, 0x34, 0x34, 0, T_T }, /* Key 35: g G */
158 /* 0B */ {0x23, 0x33, 0x33, 0, T_T }, /* Key 36: h H */
159 /* 0C */ {0x17, 0x43, 0x43, 0, T_T }, /* Key 24: i I */
160 /* 0D */ {0x24, 0x3B, 0x3B, 0, T_T }, /* Key 37: j J */
161 /* 0E */ {0x25, 0x42, 0x42, 0, T_T }, /* Key 38: k K */
162 /* 0F */ {0x26, 0x4B, 0x4B, 0, T_T }, /* Key 39: l L */
163 /* 10 */ {0x32, 0x3A, 0x3A, 0, T_T }, /* Key 52: m M */
164 /* 11 */ {0x31, 0x31, 0x31, 0, T_T }, /* Key 51: n N */
165 /* 12 */ {0x18, 0x44, 0x44, 0, T_T }, /* Key 25: o O */
166 /* 13 */ {0x19, 0x4D, 0x4D, 0, T_T }, /* Key 26: p P */
167 /* 14 */ {0x10, 0x15, 0x15, 0, T_T }, /* Key 17: q Q */
168 /* 15 */ {0x13, 0x2D, 0x2D, 0, T_T }, /* Key 20: r R */
169 /* 16 */ {0x1F, 0x1B, 0x1B, 0, T_T }, /* Key 32: s S */
170 /* 17 */ {0x14, 0x2C, 0x2C, 0, T_T }, /* Key 21: t T */
171 /* 18 */ {0x16, 0x3C, 0x3C, 0, T_T }, /* Key 23: u U */
172 /* 19 */ {0x2F, 0x2A, 0x2A, 0, T_T }, /* Key 49: v V */
173 /* 1A */ {0x11, 0x1D, 0x1D, 0, T_T }, /* Key 18: w W */
174 /* 1B */ {0x2D, 0x22, 0x22, 0, T_T }, /* Key 47: x X */
175 /* 1C */ {0x15, 0x35, 0x35, 0, T_T }, /* Key 22: y Y */
176 /* 1D */ {0x2C, 0x1A, 0x1A, 0, T_T }, /* Key 46: z Z */
177 /* 1E */ {0x02, 0x16, 0x16, 0, T_T }, /* Key 2: 1 ! */
178 /* 1F */ {0x03, 0x1E, 0x1E, 0, T_T }, /* Key 3: 2 @ */
179 /* 20 */ {0x04, 0x26, 0x26, 0, T_T }, /* Key 4: 3 # */
180 /* 21 */ {0x05, 0x25, 0x25, 0, T_T }, /* Key 5: 4 $ */
181 /* 22 */ {0x06, 0x2E, 0x2E, 0, T_T }, /* Key 6: 5 % */
182 /* 23 */ {0x07, 0x36, 0x36, 0, T_T }, /* Key 7: 6 ^ */
183 /* 24 */ {0x08, 0x3D, 0x3D, 0, T_T }, /* Key 8: 7 & */
184 /* 25 */ {0x09, 0x3E, 0x3E, 0, T_T }, /* Key 9: 8 * */
185 /* 26 */ {0x0A, 0x46, 0x46, 0, T_T }, /* Key 10: 9 ( */
186 /* 27 */ {0x0B, 0x45, 0x45, 0, T_T }, /* Key 11: 0 ) */
187 /* 28 */ {0x1C, 0x5A, 0x5A, 0, T_T }, /* Key 43: Return */
188 /* 29 */ {0x01, 0x76, 0x08, 0, T_M }, /* Key 110: Escape */
189 /* 2A */ {0x0E, 0x66, 0x66, 0, T_T }, /* Key 15: Backspace */
190 /* 2B */ {0x0F, 0x0D, 0x0D, 0, T_T }, /* Key 16: Tab */
191 /* 2C */ {0x39, 0x29, 0x29, 0, T_T }, /* Key 61: Space */
192 /* 2D */ {0x0C, 0x4E, 0x4E, 0, T_T }, /* Key 12: - _ */
193 /* 2E */ {0x0D, 0x55, 0x55, 0, T_T }, /* Key 13: = + */
194 /* 2F */ {0x1A, 0x54, 0x54, 0, T_T }, /* Key 27: [ { */
195 /* 30 */ {0x1B, 0x5B, 0x5B, 0, T_T }, /* Key 28: ] } */
196 /* 31 */ {0x2B, 0x5D, 0x5C, 0, T_T }, /* Key 29: \ | */
197 /* 32 */ {0x2B, 0x5D, 0x5D, 0, T_T }, /* Key 42: Europe 1 (Note 2) */
198 /* 33 */ {0x27, 0x4C, 0x4C, 0, T_T }, /* Key 40: ; : */
199 /* 34 */ {0x28, 0x52, 0x52, 0, T_T }, /* Key 41: ' " */
200 /* 35 */ {0x29, 0x0E, 0x0E, 0, T_T }, /* Key 1: ` ~ */
201 /* 36 */ {0x33, 0x41, 0x41, 0, T_T }, /* Key 53: , < */
202 /* 37 */ {0x34, 0x49, 0x49, 0, T_T }, /* Key 54: . > */
203 /* 38 */ {0x35, 0x4A, 0x4A, 0, T_T }, /* Key 55: / ? */
204 /* 39 */ {0x3A, 0x58, 0x14, 0, T_B }, /* Key 30: Caps Lock */
205 /* 3A */ {0x3B, 0x05, 0x07, 0, T_M }, /* Key 112: F1 */
206 /* 3B */ {0x3C, 0x06, 0x0F, 0, T_M }, /* Key 113: F2 */
207 /* 3C */ {0x3D, 0x04, 0x17, 0, T_M }, /* Key 114: F3 */
208 /* 3D */ {0x3E, 0x0C, 0x1F, 0, T_M }, /* Key 115: F4 */
209 /* 3E */ {0x3F, 0x03, 0x27, 0, T_M }, /* Key 116: F5 */
210 /* 3F */ {0x40, 0x0B, 0x2F, 0, T_M }, /* Key 117: F6 */
211 /* 40 */ {0x41, 0x83, 0x37, 0, T_M }, /* Key 118: F7 */
212 /* 41 */ {0x42, 0x0A, 0x3F, 0, T_M }, /* Key 119: F8 */
213 /* 42 */ {0x43, 0x01, 0x47, 0, T_M }, /* Key 120: F9 */
214 /* 43 */ {0x44, 0x09, 0x4F, 0, T_M }, /* Key 121: F10 */
215 /* 44 */ {0x57, 0x78, 0x56, 0, T_M }, /* Key 122: F11 */
216 /* 45 */ {0x58, 0x07, 0x5E, 0, T_M }, /* Key 123: F12 */
217 /* 46 */ {0x37, 0x7C, 0x57, KF_PS, T_M }, /* Key 124: Print Screen (Note 1) */
218 /* 47 */ {0x46, 0x7E, 0x5F, 0, T_M }, /* Key 125: Scroll Lock */
219 /* 48 */ {RSVD, RSVD, RSVD, KF_PB, T_M }, /* Key 126: Break (Ctrl-Pause) */
220 /* 49 */ {0x52, 0x70, 0x67, KF_GK, T_M }, /* Key 75: Insert (Note 1) */
221 /* 4A */ {0x47, 0x6C, 0x6E, KF_GK, T_M }, /* Key 80: Home (Note 1) */
222 /* 4B */ {0x49, 0x7D, 0x6F, KF_GK, T_M }, /* Key 85: Page Up (Note 1) */
223 /* 4C */ {0x53, 0x71, 0x64, KF_GK, T_T }, /* Key 76: Delete (Note 1) */
224 /* 4D */ {0x4F, 0x69, 0x65, KF_GK, T_M }, /* Key 81: End (Note 1) */
225 /* 4E */ {0x51, 0x7A, 0x6D, KF_GK, T_M }, /* Key 86: Page Down (Note 1) */
226 /* 4F */ {0x4D, 0x74, 0x6A, KF_GK, T_T }, /* Key 89: Right Arrow (Note 1) */
227 /* 50 */ {0x4B, 0x6B, 0x61, KF_GK, T_T }, /* Key 79: Left Arrow (Note 1) */
228 /* 51 */ {0x50, 0x72, 0x60, KF_GK, T_T }, /* Key 84: Down Arrow (Note 1) */
229 /* 52 */ {0x48, 0x75, 0x63, KF_GK, T_T }, /* Key 83: Up Arrow (Note 1) */
230 /* 53 */ {0x45, 0x77, 0x76, KF_NL, T_M }, /* Key 90: Num Lock */
231 /* 54 */ {0x35, 0x4A, 0x77, KF_NS, T_M }, /* Key 95: Keypad / (Note 1) */
232 /* 55 */ {0x37, 0x7C, 0x7E, 0, T_M }, /* Key 100: Keypad * */
233 /* 56 */ {0x4A, 0x7B, 0x84, 0, T_M }, /* Key 105: Keypad - */
234 /* 57 */ {0x4E, 0x79, 0x7C, 0, T_T }, /* Key 106: Keypad + */
235 /* 58 */ {0x1C, 0x5A, 0x79, KF_E0, T_M }, /* Key 108: Keypad Enter */
236 /* 59 */ {0x4F, 0x69, 0x69, 0, T_M }, /* Key 93: Keypad 1 End */
237 /* 5A */ {0x50, 0x72, 0x72, 0, T_M }, /* Key 98: Keypad 2 Down */
238 /* 5B */ {0x51, 0x7A, 0x7A, 0, T_M }, /* Key 103: Keypad 3 PageDn */
239 /* 5C */ {0x4B, 0x6B, 0x6B, 0, T_M }, /* Key 92: Keypad 4 Left */
240 /* 5D */ {0x4C, 0x73, 0x73, 0, T_M }, /* Key 97: Keypad 5 */
241 /* 5E */ {0x4D, 0x74, 0x74, 0, T_M }, /* Key 102: Keypad 6 Right */
242 /* 5F */ {0x47, 0x6C, 0x6C, 0, T_M }, /* Key 91: Keypad 7 Home */
243 /* 60 */ {0x48, 0x75, 0x75, 0, T_M }, /* Key 96: Keypad 8 Up */
244 /* 61 */ {0x49, 0x7D, 0x7D, 0, T_M }, /* Key 101: Keypad 9 PageUp */
245 /* 62 */ {0x52, 0x70, 0x70, 0, T_M }, /* Key 99: Keypad 0 Insert */
246 /* 63 */ {0x53, 0x71, 0x71, 0, T_M }, /* Key 104: Keypad . Delete */
247 /* 64 */ {0x56, 0x61, 0x13, 0, T_T }, /* Key 45: Europe 2 (Note 2) */
248 /* 65 */ {0x5D, 0x2F, UNKN, KF_E0, T_U }, /* Key 129: App */
249 /* 66 */ {0x5E, 0x37, UNKN, KF_E0, T_U }, /* Key Unk: Keyboard Power */
250 /* 67 */ {0x59, 0x0F, UNKN, 0, T_U }, /* Key Unk: Keypad = */
251 /* 68 */ {0x64, 0x08, UNKN, 0, T_U }, /* Key Unk: F13 */
252 /* 69 */ {0x65, 0x10, UNKN, 0, T_U }, /* Key Unk: F14 */
253 /* 6A */ {0x66, 0x18, UNKN, 0, T_U }, /* Key Unk: F15 */
254 /* 6B */ {0x67, 0x20, UNKN, 0, T_U }, /* Key Unk: F16 */
255 /* 6C */ {0x68, 0x28, UNKN, 0, T_U }, /* Key Unk: F17 */
256 /* 6D */ {0x69, 0x30, UNKN, 0, T_U }, /* Key Unk: F18 */
257 /* 6E */ {0x6A, 0x38, UNKN, 0, T_U }, /* Key Unk: F19 */
258 /* 6F */ {0x6B, 0x40, UNKN, 0, T_U }, /* Key Unk: F20 */
259 /* 70 */ {0x6C, 0x48, UNKN, 0, T_U }, /* Key Unk: F21 */
260 /* 71 */ {0x6D, 0x50, UNKN, 0, T_U }, /* Key Unk: F22 */
261 /* 72 */ {0x6E, 0x57, UNKN, 0, T_U }, /* Key Unk: F23 */
262 /* 73 */ {0x76, 0x5F, UNKN, 0, T_U }, /* Key Unk: F24 */
263 /* 74 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Execute */
264 /* 75 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Help */
265 /* 76 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Menu */
266 /* 77 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Select */
267 /* 78 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Stop */
268 /* 79 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Again */
269 /* 7A */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Undo */
270 /* 7B */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Cut */
271 /* 7C */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Copy */
272 /* 7D */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Paste */
273 /* 7E */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Find */
274 /* 7F */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Mute */
275 /* 80 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Volume Up */
276 /* 81 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Volume Dn */
277 /* 82 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Locking Caps Lock */
278 /* 83 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Locking Num Lock */
279 /* 84 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Locking Scroll Lock */
280 /* 85 */ {0x7E, 0x6D, UNKN, 0, T_U }, /* Key Unk: Keypad , (Brazilian Keypad .) */
281 /* 86 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Equal Sign */
282 /* 87 */ {0x73, 0x51, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 1 (Ro) */
283 /* 88 */ {0x70, 0x13, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl2 (K'kana/H'gana) */
284 /* 89 */ {0x7D, 0x6A, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 2 (Yen) */
285 /* 8A */ {0x79, 0x64, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 4 (Henkan) */
286 /* 8B */ {0x7B, 0x67, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 5 (Muhenkan) */
287 /* 8C */ {0x5C, 0x27, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 6 (PC9800 Pad ,) */
288 /* 8D */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Intl 7 */
289 /* 8E */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Intl 8 */
290 /* 8F */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Intl 9 */
291 /* 90 */ {0xF2, 0xF2, UNKN, KF_NB, T_U }, /* Key Unk: Keyboard Lang 1 (Hang'l/Engl) */
292 /* 91 */ {0xF1, 0xF1, UNKN, KF_NB, T_U }, /* Key Unk: Keyboard Lang 2 (Hanja) */
293 /* 92 */ {0x78, 0x63, UNKN, 0, T_U }, /* Key Unk: Keyboard Lang 3 (Katakana) */
294 /* 93 */ {0x77, 0x62, UNKN, 0, T_U }, /* Key Unk: Keyboard Lang 4 (Hiragana) */
295 /* 94 */ {0x76, 0x5F, UNKN, 0, T_U }, /* Key Unk: Keyboard Lang 5 (Zen/Han) */
296 /* 95 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Lang 6 */
297 /* 96 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Lang 7 */
298 /* 97 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Lang 8 */
299 /* 98 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Lang 9 */
300 /* 99 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Alternate Erase */
301 /* 9A */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard SysReq/Attention (Note 3) */
302 /* 9B */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Cancel */
303 /* 9C */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Clear */
304 /* 9D */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Prior */
305 /* 9E */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Return */
306 /* 9F */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Separator */
307 /* A0 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Out */
308 /* A1 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Oper */
309 /* A2 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Clear/Again */
310 /* A3 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard CrSel/Props */
311 /* A4 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard ExSel */
312};
313
314/*
315 * Note 1: The behavior of these keys depends on the state of modifier keys
316 * at the time the key was pressed.
317 *
318 * Note 2: The key label depends on the national version of the keyboard.
319 *
320 * Note 3: Certain keys which have their own PS/2 scancodes do not exist on
321 * USB keyboards; the SysReq key is an example. The SysReq key scancode needs
322 * to be translated to the Print Screen HID usage code. The HID usage to PS/2
323 * scancode conversion then generates the correct sequence depending on the
324 * keyboard state.
325 */
326
327/* USB to PS/2 conversion table for modifier keys (HID Usage Page 7). */
328static const key_def aPS2ModKeys[] = {
329 /* E0 */ {0x1D, 0x14, 0x11, 0, T_B }, /* Key 58: Left Control */
330 /* E1 */ {0x2A, 0x12, 0x12, 0, T_B }, /* Key 44: Left Shift */
331 /* E2 */ {0x38, 0x11, 0x19, 0, T_B }, /* Key 60: Left Alt */
332 /* E3 */ {0x5B, 0x1F, UNKN, KF_E0, T_U }, /* Key 127: Left GUI */
333 /* E4 */ {0x1D, 0x14, 0x58, KF_E0, T_M }, /* Key 64: Right Control */
334 /* E5 */ {0x36, 0x59, 0x59, 0, T_B }, /* Key 57: Right Shift */
335 /* E6 */ {0x38, 0x11, 0x39, KF_E0, T_M }, /* Key 62: Right Alt */
336 /* E7 */ {0x5C, 0x27, UNKN, KF_E0, T_U }, /* Key 128: Right GUI */
337};
338
339/* Extended key definition for sparse mapping. */
340typedef struct {
341 uint16_t usageId;
342 key_def kdef;
343} ext_key_def;
344
345
346/* USB to PS/2 conversion table for consumer control keys (HID Usage Page 12). */
347/* This usage page is very sparse so we'll just search through it. */
348static const ext_key_def aPS2CCKeys[] = {
349 {0x00B5, {0x19, 0x4D, UNKN, KF_E0, T_U}}, /* Scan Next Track */
350 {0x00B6, {0x10, 0x15, UNKN, KF_E0, T_U}}, /* Scan Previous Track */
351 {0x00B7, {0x24, 0x3B, UNKN, KF_E0, T_U}}, /* Stop */
352 {0x00CD, {0x22, 0x34, UNKN, KF_E0, T_U}}, /* Play/Pause */
353 {0x00E2, {0x20, 0x23, UNKN, KF_E0, T_U}}, /* Mute */
354 {0x00E5, {UNAS, UNAS, UNAS, 0, T_U}}, /* Bass Boost */
355 {0x00E7, {UNAS, UNAS, UNAS, 0, T_U}}, /* Loudness */
356 {0x00E9, {0x30, 0x32, UNKN, KF_E0, T_U}}, /* Volume Up */
357 {0x00EA, {0x2E, 0x21, UNKN, KF_E0, T_U}}, /* Volume Down */
358 {0x0152, {UNAS, UNAS, UNAS, 0, T_U}}, /* Bass Up */
359 {0x0153, {UNAS, UNAS, UNAS, 0, T_U}}, /* Bass Down */
360 {0x0154, {UNAS, UNAS, UNAS, 0, T_U}}, /* Treble Up */
361 {0x0155, {UNAS, UNAS, UNAS, 0, T_U}}, /* Treble Down */
362 {0x0183, {0x6D, 0x50, UNKN, KF_E0, T_U}}, /* Media Select */
363 {0x018A, {0x6C, 0x48, UNKN, KF_E0, T_U}}, /* Mail */
364 {0x0192, {0x21, 0x2B, UNKN, KF_E0, T_U}}, /* Calculator */
365 {0x0194, {0x6B, 0x40, UNKN, KF_E0, T_U}}, /* My Computer */
366 {0x0221, {0x65, 0x10, UNKN, KF_E0, T_U}}, /* WWW Search */
367 {0x0223, {0x32, 0x3A, UNKN, KF_E0, T_U}}, /* WWW Home */
368 {0x0224, {0x6A, 0x38, UNKN, KF_E0, T_U}}, /* WWW Back */
369 {0x0225, {0x69, 0x30, UNKN, KF_E0, T_U}}, /* WWW Forward */
370 {0x0226, {0x68, 0x28, UNKN, KF_E0, T_U}}, /* WWW Stop */
371 {0x0227, {0x67, 0x20, UNKN, KF_E0, T_U}}, /* WWW Refresh */
372 {0x022A, {0x66, 0x18, UNKN, KF_E0, T_U}}, /* WWW Favorites */
373};
374
375#endif /* IN_RING3 */
376
377#ifdef IN_RING3
378
379/**
380 * Add a null-terminated byte sequence to a queue if there is enough room.
381 *
382 * @param pQueue Pointer to the queue.
383 * @param pStr Pointer to the bytes to store.
384 * @param cbReserve Number of bytes that must still remain available in
385 * queue.
386 * @return VBox status/error code.
387 */
388static int ps2kR3InsertStrQueue(KbdKeyQ *pQueue, const uint8_t *pStr, uint32_t cbReserve)
389{
390 /* Check if queue has enough room. */
391 size_t const cbStr = (uint32_t)strlen((const char *)pStr);
392 uint32_t cUsed = RT_MIN(pQueue->Hdr.cUsed, RT_ELEMENTS(pQueue->abQueue));
393 if (cUsed + cbReserve + cbStr >= RT_ELEMENTS(pQueue->abQueue))
394 {
395 LogRelFlowFunc(("queue %p (KbdKeyQ) full (%u entries, want room for %u), cannot insert %zu entries\n",
396 pQueue, cUsed, cbReserve, cbStr));
397 return VERR_BUFFER_OVERFLOW;
398 }
399
400 /* Insert byte sequence and update circular buffer write position. */
401 uint32_t wpos = pQueue->Hdr.wpos % RT_ELEMENTS(pQueue->abQueue);
402 for (size_t i = 0; i < cbStr; i++)
403 {
404 pQueue->abQueue[wpos] = pStr[i];
405 wpos += 1;
406 if (wpos < RT_ELEMENTS(pQueue->abQueue))
407 { /* likely */ }
408 else
409 wpos = 0; /* Roll over. */
410 }
411
412 pQueue->Hdr.wpos = wpos;
413 pQueue->Hdr.cUsed = cUsed + (uint32_t)cbStr;
414
415 LogRelFlowFunc(("inserted %u bytes into queue %p (KbdKeyQ)\n", cbStr, pQueue));
416 return VINF_SUCCESS;
417}
418
419/**
420 * Notify listener about LEDs state change.
421 *
422 * @param pThisCC The PS/2 keyboard instance data for ring-3.
423 * @param u8State Bitfield which reflects LEDs state.
424 */
425static void ps2kR3NotifyLedsState(PPS2KR3 pThisCC, uint8_t u8State)
426{
427 PDMKEYBLEDS enmLeds = PDMKEYBLEDS_NONE;
428
429 if (u8State & 0x01)
430 enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_SCROLLLOCK);
431 if (u8State & 0x02)
432 enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_NUMLOCK);
433 if (u8State & 0x04)
434 enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_CAPSLOCK);
435
436 pThisCC->Keyboard.pDrv->pfnLedStatusChange(pThisCC->Keyboard.pDrv, enmLeds);
437}
438
439#endif /* IN_RING3 */
440
441/** Clears the currently active typematic key, if any. */
442static void ps2kStopTypematicRepeat(PPDMDEVINS pDevIns, PPS2K pThis)
443{
444 if (pThis->u32TypematicKey)
445 {
446 LogFunc(("Typematic key %08X\n", pThis->u32TypematicKey));
447 pThis->enmTypematicState = KBD_TMS_IDLE;
448 pThis->u32TypematicKey = 0;
449 PDMDevHlpTimerStop(pDevIns, pThis->hKbdTypematicTimer);
450 }
451}
452
453/** Convert encoded typematic value to milliseconds. Note that the values are rated
454 * with +/- 20% accuracy, so there's no need for high precision.
455 */
456static void ps2kSetupTypematic(PPS2K pThis, uint8_t val)
457{
458 int A, B;
459 unsigned period;
460
461 pThis->u8TypematicCfg = val;
462 /* The delay is easy: (1 + value) * 250 ms */
463 pThis->uTypematicDelay = (1 + ((val >> 5) & 3)) * 250;
464 /* The rate is more complicated: (8 + A) * 2^B * 4.17 ms */
465 A = val & 7;
466 B = (val >> 3) & 3;
467 period = (8 + A) * (1 << B) * 417 / 100;
468 pThis->uTypematicRepeat = period;
469 Log(("Typematic delay %u ms, repeat period %u ms\n",
470 pThis->uTypematicDelay, pThis->uTypematicRepeat));
471}
472
473static void ps2kSetDefaults(PPDMDEVINS pDevIns, PPS2K pThis)
474{
475 LogFlowFunc(("Set keyboard defaults\n"));
476 PS2Q_CLEAR(&pThis->keyQ);
477 /* Set default Scan Set 3 typematic values. */
478 /* Set default typematic rate/delay. */
479 ps2kSetupTypematic(pThis, KBD_DFL_RATE_DELAY);
480 /* Clear last typematic key?? */
481 ps2kStopTypematicRepeat(pDevIns, pThis);
482}
483
484/**
485 * The keyboard controller disabled the keyboard serial line.
486 *
487 * @param pThis The keyboard device shared instance data.
488 */
489void PS2KLineDisable(PPS2K pThis)
490{
491 LogFlowFunc(("Disabling keyboard serial line\n"));
492
493 pThis->fLineDisabled = true;
494}
495
496/**
497 * The keyboard controller enabled the keyboard serial line.
498 *
499 * @param pThis The keyboard device shared instance data.
500 */
501void PS2KLineEnable(PPS2K pThis)
502{
503 LogFlowFunc(("Enabling keyboard serial line\n"));
504
505 pThis->fLineDisabled = false;
506
507 /* If there was anything in the input queue,
508 * consider it lost and throw it away.
509 */
510 PS2Q_CLEAR(&pThis->keyQ);
511}
512
513/**
514 * Receive and process a byte sent by the keyboard controller.
515 *
516 * @param pDevIns The device instance.
517 * @param pThis The shared PS/2 keyboard instance data.
518 * @param cmd The command (or data) byte.
519 */
520int PS2KByteToKbd(PPDMDEVINS pDevIns, PPS2K pThis, uint8_t cmd)
521{
522 bool fHandled = true;
523
524 LogFlowFunc(("new cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
525
526 if (pThis->u8CurrCmd == KCMD_RESET)
527 /* In reset mode, do not respond at all. */
528 return VINF_SUCCESS;
529
530 switch (cmd)
531 {
532 case KCMD_ECHO:
533 PS2Q_INSERT(&pThis->cmdQ, KRSP_ECHO);
534 pThis->u8CurrCmd = 0;
535 break;
536 case KCMD_READ_ID:
537 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
538 PS2Q_INSERT(&pThis->cmdQ, KRSP_ID1);
539 PS2Q_INSERT(&pThis->cmdQ, KRSP_ID2);
540 pThis->u8CurrCmd = 0;
541 break;
542 case KCMD_ENABLE:
543 pThis->fScanning = true;
544 PS2Q_CLEAR(&pThis->keyQ);
545 ps2kStopTypematicRepeat(pDevIns, pThis);
546 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
547 pThis->u8CurrCmd = 0;
548 break;
549 case KCMD_DFLT_DISABLE:
550 pThis->fScanning = false;
551 ps2kSetDefaults(pDevIns, pThis); /* Also clears buffer/typematic state. */
552 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
553 pThis->u8CurrCmd = 0;
554 break;
555 case KCMD_SET_DEFAULT:
556 ps2kSetDefaults(pDevIns, pThis);
557 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
558 pThis->u8CurrCmd = 0;
559 break;
560 case KCMD_ALL_TYPEMATIC:
561 case KCMD_ALL_MK_BRK:
562 case KCMD_ALL_MAKE:
563 case KCMD_ALL_TMB:
564 /// @todo Set the key types here.
565 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
566 pThis->u8CurrCmd = 0;
567 break;
568 case KCMD_RESEND:
569 pThis->u8CurrCmd = 0;
570 break;
571 case KCMD_RESET:
572 pThis->u8ScanSet = 2;
573 ps2kSetDefaults(pDevIns, pThis);
574 /// @todo reset more?
575 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
576 pThis->u8CurrCmd = cmd;
577 /* Delay BAT completion; the test may take hundreds of ms. */
578 PDMDevHlpTimerSetMillies(pDevIns, pThis->hKbdDelayTimer, 2);
579 break;
580 /* The following commands need a parameter. */
581 case KCMD_LEDS:
582 case KCMD_SCANSET:
583 case KCMD_RATE_DELAY:
584 case KCMD_TYPE_MATIC:
585 case KCMD_TYPE_MK_BRK:
586 case KCMD_TYPE_MAKE:
587 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
588 pThis->u8CurrCmd = cmd;
589 break;
590 default:
591 /* Sending a command instead of a parameter starts the new command. */
592 switch (pThis->u8CurrCmd)
593 {
594 case KCMD_LEDS:
595#ifndef IN_RING3
596 return VINF_IOM_R3_IOPORT_WRITE;
597#else
598 {
599 PPS2KR3 pThisCC = &PDMDEVINS_2_DATA_CC(pDevIns, PKBDSTATER3)->Kbd;
600 ps2kR3NotifyLedsState(pThisCC, cmd);
601 pThis->fNumLockOn = !!(cmd & 0x02); /* Sync internal Num Lock state. */
602 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
603 pThis->u8LEDs = cmd;
604 pThis->u8CurrCmd = 0;
605 }
606#endif
607 break;
608 case KCMD_SCANSET:
609 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
610 if (cmd == 0)
611 PS2Q_INSERT(&pThis->cmdQ, pThis->u8ScanSet);
612 else if (cmd < 4)
613 {
614 pThis->u8ScanSet = cmd;
615 LogRel(("PS2K: Selected scan set %d\n", cmd));
616 }
617 /* Other values are simply ignored. */
618 pThis->u8CurrCmd = 0;
619 break;
620 case KCMD_RATE_DELAY:
621 ps2kSetupTypematic(pThis, cmd);
622 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
623 pThis->u8CurrCmd = 0;
624 break;
625 default:
626 fHandled = false;
627 }
628 /* Fall through only to handle unrecognized commands. */
629 if (fHandled)
630 break;
631 RT_FALL_THRU();
632
633 case KCMD_INVALID_1:
634 case KCMD_INVALID_2:
635 PS2Q_INSERT(&pThis->cmdQ, KRSP_RESEND);
636 pThis->u8CurrCmd = 0;
637 break;
638 }
639 LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
640// KBCUpdateInterrupts(pDevIns);
641 return VINF_SUCCESS;
642}
643
644/**
645 * Send a byte (keystroke or command response) to the keyboard controller.
646 *
647 * @returns VINF_SUCCESS or VINF_TRY_AGAIN.
648 * @param pDevIns The device instance.
649 * @param pThis The shared PS/2 keyboard instance data.
650 * @param pb Where to return the byte we've read.
651 * @remarks Caller must have entered the device critical section.
652 */
653int PS2KByteFromKbd(PPDMDEVINS pDevIns, PPS2K pThis, uint8_t *pb)
654{
655 int rc;
656
657 AssertPtr(pb);
658
659 /* Anything in the command queue has priority over data
660 * in the keystroke queue. Additionally, keystrokes are
661 * blocked if a command is currently in progress, even if
662 * the command queue is empty.
663 */
664 rc = PS2Q_REMOVE(&pThis->cmdQ, pb);
665 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && pThis->fScanning)
666 if (!pThis->fThrottleActive)
667 {
668 rc = PS2Q_REMOVE(&pThis->keyQ, pb);
669 if (pThis->fThrottleEnabled)
670 {
671 pThis->fThrottleActive = true;
672 PDMDevHlpTimerSetMillies(pDevIns, pThis->hThrottleTimer, KBD_THROTTLE_DELAY);
673 }
674 }
675
676 LogFlowFunc(("keyboard sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
677 return rc;
678}
679
680#ifdef IN_RING3
681
682static int ps2kR3ProcessKeyEvent(PPDMDEVINS pDevIns, PPS2K pThis, uint32_t u32HidCode, bool fKeyDown)
683{
684 key_def const *pKeyDef;
685 uint8_t abCodes[16];
686 char *pCodes;
687 size_t cbLeft;
688 uint8_t abScan[2];
689 uint8_t u8HidPage;
690 uint8_t u8HidCode;
691 uint16_t u16HidUsage;
692
693 u8HidPage = RT_LOBYTE(RT_HIWORD(u32HidCode));
694 u16HidUsage = RT_LOWORD(u32HidCode);
695 /* For the keyboard usage page (7) we use a 8-bit code. For other pages we use the full 16-bit ID. */
696 u8HidCode = (u8HidPage == USB_HID_KB_PAGE) ? RT_LOBYTE(u32HidCode) : 0;
697
698 LogFlowFunc(("key %s: page 0x%02x ID 0x%04x (set %d)\n", fKeyDown ? "down" : "up", u8HidPage, u16HidUsage, pThis->u8ScanSet));
699
700 /* Find the key definition in somewhat sparse storage. */
701 if (u8HidPage == USB_HID_KB_PAGE)
702 /* For the standard keyboard usage page, thre are just two arrays. */
703 pKeyDef = u8HidCode >= HID_MODIFIER_FIRST ? &aPS2ModKeys[u8HidCode - HID_MODIFIER_FIRST] : &aPS2Keys[u8HidCode];
704 else if (u8HidPage == USB_HID_CC_PAGE)
705 {
706 /* For the consumer control usage page, we need to search. */
707 unsigned i;
708
709 pKeyDef = &aPS2Keys[0]; /* Dummy no-event key. */
710 for (i = 0; i < RT_ELEMENTS(aPS2CCKeys); ++i)
711 {
712 if (aPS2CCKeys[i].usageId == u16HidUsage)
713 {
714 pKeyDef = &aPS2CCKeys[i].kdef;
715 break;
716 }
717 }
718 }
719 else
720 {
721 LogFlow(("Unsupported HID usage page, ignoring key.\n"));
722 return VINF_SUCCESS;
723 }
724
725 /* Some keys are not processed at all; early return. */
726 if (pKeyDef->makeS1 == NONE)
727 {
728 LogFlow(("Skipping key processing.\n"));
729 return VINF_SUCCESS;
730 }
731
732 /* Handle modifier keys (Ctrl/Alt/Shift/GUI). We need to keep track
733 * of their state in addition to sending the scan code.
734 */
735 if (u8HidCode >= HID_MODIFIER_FIRST)
736 {
737 unsigned mod_bit = 1 << (u8HidCode - HID_MODIFIER_FIRST);
738
739 Assert((u8HidPage == USB_HID_KB_PAGE));
740 Assert((u8HidCode <= HID_MODIFIER_LAST));
741 if (fKeyDown)
742 pThis->u8Modifiers |= mod_bit;
743 else
744 pThis->u8Modifiers &= ~mod_bit;
745 }
746
747 /* Toggle NumLock state. */
748 if ((pKeyDef->keyFlags & KF_NL) && fKeyDown)
749 pThis->fNumLockOn ^= true;
750
751 abCodes[0] = 0;
752 pCodes = (char *)abCodes;
753 cbLeft = sizeof(abCodes);
754
755 if (pThis->u8ScanSet == 1 || pThis->u8ScanSet == 2)
756 {
757 /* The basic scan set 1 and 2 logic is the same, only the scan codes differ.
758 * Since scan set 2 is used almost all the time, that case is handled first.
759 */
760 if (fKeyDown)
761 {
762 /* Process key down event. */
763 if (pKeyDef->keyFlags & KF_PB)
764 {
765 /* Pause/Break sends different data if either Ctrl is held. */
766 if (pThis->u8Modifiers & (MOD_LCTRL | MOD_RCTRL))
767 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
768 "\xE0\x7E\xE0\xF0\x7E" : "\xE0\x46\xE0\xC6");
769 else
770 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
771 "\xE1\x14\x77\xE1\xF0\x14\xF0\x77" : "\xE1\x1D\x45\xE1\x9D\xC5");
772 }
773 else if (pKeyDef->keyFlags & KF_PS)
774 {
775 /* Print Screen depends on all of Ctrl, Shift, *and* Alt! */
776 if (pThis->u8Modifiers & (MOD_LALT | MOD_RALT))
777 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
778 "\x84" : "\x54");
779 else if (pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT))
780 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
781 "\xE0\x7C" : "\xE0\x37");
782 else
783 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
784 "\xE0\x12\xE0\x7C" : "\xE0\x2A\xE0\x37");
785 }
786 else if (pKeyDef->keyFlags & (KF_GK | KF_NS))
787 {
788 /* The numeric pad keys fake Shift presses or releases
789 * depending on Num Lock and Shift key state. The '/'
790 * key behaves in a similar manner but does not depend on
791 * the Num Lock state.
792 */
793 if (!pThis->fNumLockOn || (pKeyDef->keyFlags & KF_NS))
794 {
795 if (pThis->u8Modifiers & MOD_LSHIFT)
796 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
797 "\xE0\xF0\x12" : "\xE0\xAA");
798 if (pThis->u8Modifiers & MOD_RSHIFT)
799 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
800 "\xE0\xF0\x59" : "\xE0\xB6");
801 }
802 else
803 {
804 Assert(pThis->fNumLockOn); /* Not for KF_NS! */
805 if ((pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT)) == 0)
806 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
807 "\xE0\x12" : "\xE0\x2A");
808 /* Else Shift cancels NumLock, so no prefix! */
809 }
810 }
811
812 /* Standard processing for regular keys only. */
813 abScan[0] = pThis->u8ScanSet == 2 ? pKeyDef->makeS2 : pKeyDef->makeS1;
814 abScan[1] = '\0';
815 if (!(pKeyDef->keyFlags & (KF_PB | KF_PS)))
816 {
817 if (pKeyDef->keyFlags & (KF_E0 | KF_GK | KF_NS))
818 RTStrCatP(&pCodes, &cbLeft, "\xE0");
819 RTStrCatP(&pCodes, &cbLeft, (const char *)abScan);
820 }
821
822 /* Feed the bytes to the queue if there is room. */
823 /// @todo Send overrun code if sequence won't fit?
824 ps2kR3InsertStrQueue(&pThis->keyQ, abCodes, 0);
825 }
826 else if (!(pKeyDef->keyFlags & (KF_NB | KF_PB)))
827 {
828 /* Process key up event except for keys which produce none. */
829
830 /* Handle Print Screen release. */
831 if (pKeyDef->keyFlags & KF_PS)
832 {
833 /* Undo faked Print Screen state as needed. */
834 if (pThis->u8Modifiers & (MOD_LALT | MOD_RALT))
835 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
836 "\xF0\x84" : "\xD4");
837 else if (pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT))
838 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
839 "\xE0\xF0\x7C" : "\xE0\xB7");
840 else
841 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
842 "\xE0\xF0\x7C\xE0\xF0\x12" : "\xE0\xB7\xE0\xAA");
843 }
844 else
845 {
846 /* Process base scan code for less unusual keys. */
847 abScan[0] = pThis->u8ScanSet == 2 ? pKeyDef->makeS2 : pKeyDef->makeS1 | 0x80;
848 abScan[1] = '\0';
849 if (pKeyDef->keyFlags & (KF_E0 | KF_GK | KF_NS))
850 RTStrCatP(&pCodes, &cbLeft, "\xE0");
851 if (pThis->u8ScanSet == 2)
852 RTStrCatP(&pCodes, &cbLeft, "\xF0");
853 RTStrCatP(&pCodes, &cbLeft, (const char *)abScan);
854
855 /* Restore shift state for gray keys. */
856 if (pKeyDef->keyFlags & (KF_GK | KF_NS))
857 {
858 if (!pThis->fNumLockOn || (pKeyDef->keyFlags & KF_NS))
859 {
860 if (pThis->u8Modifiers & MOD_LSHIFT)
861 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
862 "\xE0\x12" : "\xE0\x2A");
863 if (pThis->u8Modifiers & MOD_RSHIFT)
864 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
865 "\xE0\x59" : "\xE0\x36");
866 }
867 else
868 {
869 Assert(pThis->fNumLockOn); /* Not for KF_NS! */
870 if ((pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT)) == 0)
871 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
872 "\xE0\xF0\x12" : "\xE0\xAA");
873 }
874 }
875 }
876
877 /* Feed the bytes to the queue if there is room. */
878 /// @todo Send overrun code if sequence won't fit?
879 ps2kR3InsertStrQueue(&pThis->keyQ, abCodes, 0);
880 }
881 }
882 else
883 {
884 /* Handle Scan Set 3 - very straightforward. */
885 Assert(pThis->u8ScanSet == 3);
886 abScan[0] = pKeyDef->makeS3;
887 abScan[1] = '\0';
888 if (fKeyDown)
889 {
890 RTStrCatP(&pCodes, &cbLeft, (const char *)abScan);
891 }
892 else
893 {
894 /* Send a key release code unless it's a make only key. */
895 /// @todo Look up the current typematic setting, not the default!
896 if (pKeyDef->keyMatic != T_M)
897 {
898 RTStrCatP(&pCodes, &cbLeft, "\xF0");
899 RTStrCatP(&pCodes, &cbLeft, (const char *)abScan);
900 }
901 }
902 /* Feed the bytes to the queue if there is room. */
903 /// @todo Send overrun code if sequence won't fit?
904 ps2kR3InsertStrQueue(&pThis->keyQ, abCodes, 0);
905 }
906
907 /* Set up or cancel typematic key repeat. For keyboard usage page only. */
908 if (u8HidPage == USB_HID_KB_PAGE)
909 {
910 if (fKeyDown)
911 {
912 if (pThis->u32TypematicKey != u32HidCode)
913 {
914 pThis->enmTypematicState = KBD_TMS_DELAY;
915 pThis->u32TypematicKey = u32HidCode;
916 PDMDevHlpTimerSetMillies(pDevIns, pThis->hKbdTypematicTimer, pThis->uTypematicDelay);
917 Log(("Typematic delay %u ms, key %08X\n", pThis->uTypematicDelay, u32HidCode));
918 }
919 }
920 else
921 {
922 /* "Typematic operation stops when the last key pressed is released, even
923 * if other keys are still held down." (IBM PS/2 Tech Ref). The last key pressed
924 * is the one that's being repeated.
925 */
926 if (pThis->u32TypematicKey == u32HidCode)
927 {
928 /* This disables the typematic repeat. */
929 pThis->u32TypematicKey = 0;
930 pThis->enmTypematicState = KBD_TMS_IDLE;
931 /* For good measure, we cancel the timer, too. */
932 PDMDevHlpTimerStop(pDevIns, pThis->hKbdTypematicTimer);
933 Log(("Typematic action cleared for key %08X\n", u32HidCode));
934 }
935 }
936 }
937
938 /* Poke the KBC to update its state. */
939 KBCUpdateInterrupts(pDevIns);
940
941 return VINF_SUCCESS;
942}
943
944/**
945 * @callback_method_impl{FNTMTIMERDEV}
946 *
947 * Throttling timer to emulate the finite keyboard communication speed. A PS/2 keyboard is
948 * limited by the serial link speed and cannot send much more than 1,000 bytes per second.
949 * Some software (notably Borland Pascal and programs built with its run-time) relies on
950 * being able to read an incoming scan-code twice. Throttling the data rate enables such
951 * software to function, while human typists cannot tell any difference.
952 *
953 * Note: The throttling is currently only done for keyboard data, not command responses.
954 * The throttling could and perhaps should be done for any data (including command
955 * response) scoming from PS/2 devices, both keyboard and auxiliary. That is not currently
956 * done because it would needlessly slow things down.
957 */
958static DECLCALLBACK(void) ps2kR3ThrottleTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
959{
960 RT_NOREF(pDevIns, pTimer);
961 PPS2K pThis = (PS2K *)pvUser;
962 unsigned uHaveData;
963
964 /* Grab the lock to avoid races with event delivery or EMTs. */
965 int rc = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_SEM_BUSY);
966 AssertReleaseRC(rc);
967
968 /* If data is available, poke the KBC. Once the data
969 * is actually read, the timer may be re-triggered.
970 */
971 pThis->fThrottleActive = false;
972 uHaveData = PS2Q_COUNT(&pThis->keyQ);
973 LogFlowFunc(("Have%s bytes\n", uHaveData ? "" : " no"));
974 if (uHaveData)
975 KBCUpdateInterrupts(pDevIns);
976
977 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
978}
979
980/**
981 * @callback_method_impl{FNTMTIMERDEV,
982 * Timer handler for emulating typematic keys.}
983 *
984 * @note Note that only the last key held down repeats (if typematic).
985 */
986static DECLCALLBACK(void) ps2kR3TypematicTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
987{
988 RT_NOREF(pDevIns, pTimer);
989 PPS2K pThis = (PS2K *)pvUser;
990 LogFlowFunc(("Typematic state=%d, key %08X\n", pThis->enmTypematicState, pThis->u32TypematicKey));
991
992 /* If the current typematic key is zero, the repeat was canceled just when
993 * the timer was about to run. In that case, do nothing.
994 */
995 if (pThis->u32TypematicKey)
996 {
997 if (pThis->enmTypematicState == KBD_TMS_DELAY)
998 pThis->enmTypematicState = KBD_TMS_REPEAT;
999
1000 if (pThis->enmTypematicState == KBD_TMS_REPEAT)
1001 {
1002 ps2kR3ProcessKeyEvent(pDevIns, pThis, pThis->u32TypematicKey, true /* Key down */ );
1003 PDMDevHlpTimerSetMillies(pDevIns, pThis->hKbdTypematicTimer, pThis->uTypematicRepeat);
1004 }
1005 }
1006}
1007
1008/**
1009 * @callback_method_impl{FNTMTIMERDEV}
1010 *
1011 * The keyboard BAT is specified to take several hundred milliseconds. We need
1012 * to delay sending the result to the host for at least a tiny little while.
1013 */
1014static DECLCALLBACK(void) ps2kR3DelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1015{
1016 RT_NOREF(pDevIns, pTimer);
1017 PPS2K pThis = (PS2K *)pvUser;
1018
1019 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
1020
1021 AssertMsg(pThis->u8CurrCmd == KCMD_RESET, ("u8CurrCmd=%02x\n", pThis->u8CurrCmd));
1022 PS2Q_INSERT(&pThis->cmdQ, KRSP_BAT_OK);
1023 pThis->fScanning = true; /* BAT completion enables scanning! */
1024 pThis->u8CurrCmd = 0;
1025
1026 /// @todo Might want a PS2KCompleteCommand() to push last response, clear command, and kick the KBC...
1027 /* Give the KBC a kick. */
1028 KBCUpdateInterrupts(pDevIns);
1029}
1030
1031/* Release any and all currently depressed keys. Used whenever the guest keyboard
1032 * is likely to be out of sync with the host, such as when loading a saved state
1033 * or resuming a suspended host.
1034 */
1035static void ps2kR3ReleaseKeys(PPDMDEVINS pDevIns, PPS2K pThis)
1036{
1037 LogFlowFunc(("Releasing keys...\n"));
1038
1039 for (unsigned uKey = 0; uKey < sizeof(pThis->abDepressedKeys); ++uKey)
1040 if (pThis->abDepressedKeys[uKey])
1041 {
1042 ps2kR3ProcessKeyEvent(pDevIns, pThis, RT_MAKE_U32(USB_HID_KB_PAGE, uKey), false /* key up */);
1043 pThis->abDepressedKeys[uKey] = 0;
1044 }
1045 LogFlowFunc(("Done releasing keys\n"));
1046}
1047
1048
1049/**
1050 * Debug device info handler. Prints basic keyboard state.
1051 *
1052 * @param pDevIns Device instance which registered the info.
1053 * @param pHlp Callback functions for doing output.
1054 * @param pszArgs Argument string. Optional and specific to the handler.
1055 */
1056static DECLCALLBACK(void) ps2kR3InfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1057{
1058 PKBDSTATE pParent = PDMDEVINS_2_DATA(pDevIns, PKBDSTATE);
1059 PPS2K pThis = &pParent->Kbd;
1060 NOREF(pszArgs);
1061
1062 pHlp->pfnPrintf(pHlp, "PS/2 Keyboard: scan set %d, scanning %s, serial line %s\n",
1063 pThis->u8ScanSet, pThis->fScanning ? "enabled" : "disabled",
1064 pThis->fLineDisabled ? "disabled" : "enabled");
1065 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
1066 pHlp->pfnPrintf(pHlp, "LED state %02X, Num Lock %s\n", pThis->u8LEDs,
1067 pThis->fNumLockOn ? "on" : "off");
1068 pHlp->pfnPrintf(pHlp, "Typematic delay %ums, repeat period %ums\n",
1069 pThis->uTypematicDelay, pThis->uTypematicRepeat);
1070 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
1071 PS2Q_COUNT(&pThis->cmdQ), PS2Q_SIZE(&pThis->cmdQ));
1072 pHlp->pfnPrintf(pHlp, "Input queue : %d items (%d max)\n",
1073 PS2Q_COUNT(&pThis->keyQ), PS2Q_SIZE(&pThis->keyQ));
1074 if (pThis->enmTypematicState != KBD_TMS_IDLE)
1075 pHlp->pfnPrintf(pHlp, "Active typematic key %08X (%s)\n", pThis->u32TypematicKey,
1076 pThis->enmTypematicState == KBD_TMS_DELAY ? "delay" : "repeat");
1077}
1078
1079
1080/* -=-=-=-=-=- Keyboard: IKeyboardPort -=-=-=-=-=- */
1081
1082/**
1083 * Keyboard event handler.
1084 *
1085 * @returns VBox status code.
1086 * @param pDevIns The device instance.
1087 * @param pThis The PS2 keyboard instance data.
1088 * @param idUsage USB HID usage code with key press/release flag.
1089 */
1090static int ps2kR3PutEventWorker(PPDMDEVINS pDevIns, PPS2K pThis, uint32_t idUsage)
1091{
1092 uint32_t u32HidCode;
1093 uint8_t u8KeyCode;
1094 uint8_t u8HidPage;
1095 bool fKeyDown;
1096 bool fHaveEvent = true;
1097 int rc = VINF_SUCCESS;
1098
1099 /* Extract the usage page and ID and ensure it's valid. */
1100 fKeyDown = !(idUsage & UINT32_C(0x80000000));
1101 u32HidCode = idUsage & 0xFFFFFF;
1102 u8HidPage = RT_LOBYTE(RT_HIWORD(idUsage));
1103 u8KeyCode = RT_LOBYTE(idUsage);
1104 if (u8HidPage == USB_HID_KB_PAGE)
1105 AssertReturn(u8KeyCode <= VBOX_USB_MAX_USAGE_CODE, VERR_INTERNAL_ERROR);
1106 else
1107 AssertReturn(u8HidPage == USB_HID_CC_PAGE, VERR_INTERNAL_ERROR);
1108
1109 if (fKeyDown)
1110 {
1111 /* Due to host key repeat, we can get key events for keys which are
1112 * already depressed. We need to ignore those. */
1113 if (pThis->abDepressedKeys[u8KeyCode])
1114 fHaveEvent = false;
1115 pThis->abDepressedKeys[u8KeyCode] = 1;
1116 }
1117 else
1118 {
1119 /* NB: We allow key release events for keys which aren't depressed.
1120 * That is unlikely to happen and should not cause trouble.
1121 */
1122 pThis->abDepressedKeys[u8KeyCode] = 0;
1123 }
1124
1125 /* Unless this is a new key press/release, don't even bother. */
1126 if (fHaveEvent)
1127 {
1128 Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->pCritSectRoR3));
1129 rc = ps2kR3ProcessKeyEvent(pDevIns, pThis, u32HidCode, fKeyDown);
1130 }
1131
1132 return rc;
1133}
1134
1135
1136/**
1137 * @interface_method_impl{PDMIKEYBOARDPORT,pfnPutEventHid}
1138 */
1139static DECLCALLBACK(int) ps2kR3KeyboardPort_PutEventHid(PPDMIKEYBOARDPORT pInterface, uint32_t idUsage)
1140{
1141 PPS2KR3 pThisCC = RT_FROM_MEMBER(pInterface, PS2KR3, Keyboard.IPort);
1142 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1143 PPS2K pThis = &PDMDEVINS_2_DATA(pDevIns, PKBDSTATE)->Kbd;
1144 int rc;
1145
1146 LogRelFlowFunc(("key code %08X\n", idUsage));
1147
1148 rc = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_SEM_BUSY);
1149 AssertReleaseRC(rc);
1150
1151 /* The 'BAT fail' scancode is reused as a signal to release keys. No actual
1152 * key is allowed to use this scancode.
1153 */
1154 if (RT_LIKELY(idUsage != KRSP_BAT_FAIL))
1155 ps2kR3PutEventWorker(pDevIns, pThis, idUsage);
1156 else
1157 ps2kR3ReleaseKeys(pDevIns, pThis);
1158
1159 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
1160
1161 return VINF_SUCCESS;
1162}
1163
1164
1165/* -=-=-=-=-=- Keyboard: IBase -=-=-=-=-=- */
1166
1167/**
1168 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1169 */
1170static DECLCALLBACK(void *) ps2kR3QueryInterface(PPDMIBASE pInterface, const char *pszIID)
1171{
1172 PPS2KR3 pThisCC = RT_FROM_MEMBER(pInterface, PS2KR3, Keyboard.IBase);
1173 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->Keyboard.IBase);
1174 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDPORT, &pThisCC->Keyboard.IPort);
1175 return NULL;
1176}
1177
1178
1179/* -=-=-=-=-=- Device management -=-=-=-=-=- */
1180
1181/**
1182 * Attach command.
1183 *
1184 * This is called to let the device attach to a driver for a
1185 * specified LUN.
1186 *
1187 * This is like plugging in the keyboard after turning on the
1188 * system.
1189 *
1190 * @returns VBox status code.
1191 * @param pDevIns The device instance.
1192 * @param pThisCC The PS/2 keyboard instance data for ring-3.
1193 * @param iLUN The logical unit which is being detached.
1194 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1195 */
1196int PS2KR3Attach(PPDMDEVINS pDevIns, PPS2KR3 pThisCC, unsigned iLUN, uint32_t fFlags)
1197{
1198 int rc;
1199
1200 /* The LUN must be 0, i.e. keyboard. */
1201 Assert(iLUN == 0);
1202 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
1203 ("PS/2 keyboard does not support hotplugging\n"),
1204 VERR_INVALID_PARAMETER);
1205
1206 LogFlowFunc(("iLUN=%d\n", iLUN));
1207
1208 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThisCC->Keyboard.IBase, &pThisCC->Keyboard.pDrvBase, "Keyboard Port");
1209 if (RT_SUCCESS(rc))
1210 {
1211 pThisCC->Keyboard.pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->Keyboard.pDrvBase, PDMIKEYBOARDCONNECTOR);
1212 if (!pThisCC->Keyboard.pDrv)
1213 {
1214 AssertLogRelMsgFailed(("LUN #0 doesn't have a keyboard interface! rc=%Rrc\n", rc));
1215 rc = VERR_PDM_MISSING_INTERFACE;
1216 }
1217 }
1218 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1219 {
1220 Log(("%s/%d: warning: no driver attached to LUN #0!\n", pDevIns->pReg->szName, pDevIns->iInstance));
1221 rc = VINF_SUCCESS;
1222 }
1223 else
1224 AssertLogRelMsgFailed(("Failed to attach LUN #0! rc=%Rrc\n", rc));
1225
1226 return rc;
1227}
1228
1229void PS2KR3SaveState(PPDMDEVINS pDevIns, PPS2K pThis, PSSMHANDLE pSSM)
1230{
1231 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1232 uint32_t cPressed = 0;
1233 uint32_t cbTMSSize = 0;
1234
1235 LogFlowFunc(("Saving PS2K state\n"));
1236
1237 /* Save the basic keyboard state. */
1238 pHlp->pfnSSMPutU8(pSSM, pThis->u8CurrCmd);
1239 pHlp->pfnSSMPutU8(pSSM, pThis->u8LEDs);
1240 pHlp->pfnSSMPutU8(pSSM, pThis->u8TypematicCfg);
1241 pHlp->pfnSSMPutU8(pSSM, (uint8_t)pThis->u32TypematicKey);
1242 pHlp->pfnSSMPutU8(pSSM, pThis->u8Modifiers);
1243 pHlp->pfnSSMPutU8(pSSM, pThis->u8ScanSet);
1244 pHlp->pfnSSMPutU8(pSSM, pThis->enmTypematicState);
1245 pHlp->pfnSSMPutBool(pSSM, pThis->fNumLockOn);
1246 pHlp->pfnSSMPutBool(pSSM, pThis->fScanning);
1247
1248 /* Save the command and keystroke queues. */
1249 PS2Q_SAVE(pHlp, pSSM, &pThis->cmdQ);
1250 PS2Q_SAVE(pHlp, pSSM, &pThis->keyQ);
1251
1252 /* Save the command delay timer. Note that the typematic repeat
1253 * timer is *not* saved.
1254 */
1255 PDMDevHlpTimerSave(pDevIns, pThis->hKbdDelayTimer, pSSM);
1256
1257 /* Save any pressed keys. This is necessary to avoid "stuck"
1258 * keys after a restore. Needs two passes.
1259 */
1260 for (unsigned i = 0; i < sizeof(pThis->abDepressedKeys); ++i)
1261 if (pThis->abDepressedKeys[i])
1262 ++cPressed;
1263
1264 pHlp->pfnSSMPutU32(pSSM, cPressed);
1265
1266 for (unsigned uKey = 0; uKey < sizeof(pThis->abDepressedKeys); ++uKey)
1267 if (pThis->abDepressedKeys[uKey])
1268 pHlp->pfnSSMPutU8(pSSM, uKey);
1269
1270 /* Save the typematic settings for Scan Set 3. */
1271 pHlp->pfnSSMPutU32(pSSM, cbTMSSize);
1272 /* Currently not implemented. */
1273}
1274
1275int PS2KR3LoadState(PPDMDEVINS pDevIns, PPS2K pThis, PSSMHANDLE pSSM, uint32_t uVersion)
1276{
1277 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1278 uint8_t u8;
1279 uint32_t cPressed;
1280 uint32_t cbTMSSize;
1281 int rc;
1282
1283 NOREF(uVersion);
1284 LogFlowFunc(("Loading PS2K state version %u\n", uVersion));
1285
1286 /* Load the basic keyboard state. */
1287 pHlp->pfnSSMGetU8(pSSM, &pThis->u8CurrCmd);
1288 pHlp->pfnSSMGetU8(pSSM, &pThis->u8LEDs);
1289 pHlp->pfnSSMGetU8(pSSM, &pThis->u8TypematicCfg);
1290 pHlp->pfnSSMGetU8(pSSM, &u8);
1291 /* Reconstruct the 32-bit code from the 8-bit value in saved state. */
1292 pThis->u32TypematicKey = u8 ? RT_MAKE_U32(USB_HID_KB_PAGE, u8) : 0;
1293 pHlp->pfnSSMGetU8(pSSM, &pThis->u8Modifiers);
1294 pHlp->pfnSSMGetU8(pSSM, &pThis->u8ScanSet);
1295 pHlp->pfnSSMGetU8(pSSM, &u8);
1296 pThis->enmTypematicState = (tmatic_state_t)u8;
1297 pHlp->pfnSSMGetBool(pSSM, &pThis->fNumLockOn);
1298 pHlp->pfnSSMGetBool(pSSM, &pThis->fScanning);
1299
1300 /* Load the command and keystroke queues. */
1301 rc = PS2Q_LOAD(pHlp, pSSM, &pThis->cmdQ);
1302 AssertRCReturn(rc, rc);
1303 rc = PS2Q_LOAD(pHlp, pSSM, &pThis->keyQ);
1304 AssertRCReturn(rc, rc);
1305
1306 /* Load the command delay timer, just in case. */
1307 rc = PDMDevHlpTimerLoad(pDevIns, pThis->hKbdDelayTimer, pSSM);
1308 AssertRCReturn(rc, rc);
1309
1310 /* Recalculate the typematic delay/rate. */
1311 ps2kSetupTypematic(pThis, pThis->u8TypematicCfg);
1312
1313 /* Fake key up events for keys that were held down at the time the state was saved. */
1314 rc = pHlp->pfnSSMGetU32(pSSM, &cPressed);
1315 AssertRCReturn(rc, rc);
1316
1317 /* If any keys were down, load and then release them. */
1318 if (cPressed)
1319 {
1320 for (unsigned i = 0; i < cPressed; ++i)
1321 {
1322 rc = pHlp->pfnSSMGetU8(pSSM, &u8);
1323 AssertRCReturn(rc, rc);
1324 pThis->abDepressedKeys[u8] = 1;
1325 }
1326 }
1327
1328 /* Load typematic settings for Scan Set 3. */
1329 rc = pHlp->pfnSSMGetU32(pSSM, &cbTMSSize);
1330 AssertRCReturn(rc, rc);
1331
1332 while (cbTMSSize--)
1333 {
1334 rc = pHlp->pfnSSMGetU8(pSSM, &u8);
1335 AssertRCReturn(rc, rc);
1336 }
1337
1338 return rc;
1339}
1340
1341int PS2KR3LoadDone(PPDMDEVINS pDevIns, PPS2K pThis, PPS2KR3 pThisCC)
1342{
1343 /* This *must* be done after the inital load because it may trigger
1344 * interrupts and change the interrupt controller state.
1345 */
1346 ps2kR3ReleaseKeys(pDevIns, pThis);
1347 ps2kR3NotifyLedsState(pThisCC, pThis->u8LEDs);
1348 return VINF_SUCCESS;
1349}
1350
1351void PS2KR3Reset(PPDMDEVINS pDevIns, PPS2K pThis, PPS2KR3 pThisCC)
1352{
1353 LogFlowFunc(("Resetting PS2K\n"));
1354
1355 pThis->fScanning = true;
1356 pThis->fThrottleActive = false;
1357 pThis->u8ScanSet = 2;
1358 pThis->u8CurrCmd = 0;
1359 pThis->u8Modifiers = 0;
1360 pThis->u32TypematicKey = 0;
1361 pThis->enmTypematicState = KBD_TMS_IDLE;
1362
1363 /* Clear queues and any pressed keys. */
1364 memset(pThis->abDepressedKeys, 0, sizeof(pThis->abDepressedKeys));
1365 PS2Q_CLEAR(&pThis->cmdQ);
1366 ps2kSetDefaults(pDevIns, pThis); /* Also clears keystroke queue. */
1367
1368 /* Activate the PS/2 keyboard by default. */
1369 if (pThisCC->Keyboard.pDrv)
1370 pThisCC->Keyboard.pDrv->pfnSetActive(pThisCC->Keyboard.pDrv, true /*fActive*/);
1371}
1372
1373int PS2KR3Construct(PPDMDEVINS pDevIns, PPS2K pThis, PPS2KR3 pThisCC, PCFGMNODE pCfg)
1374{
1375 LogFlowFunc(("\n"));
1376 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1377
1378 /*
1379 * Read configuration.
1380 */
1381 bool fThrottleEnabled;
1382 int rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "KbdThrottleEnabled", &fThrottleEnabled, true);
1383 if (RT_FAILURE(rc))
1384 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to query \"KbdThrottleEnabled\" from the config"));
1385 Log(("KbdThrottleEnabled=%RTbool\n", fThrottleEnabled));
1386 pThis->fThrottleEnabled = fThrottleEnabled;
1387
1388 /*
1389 * Initialize state.
1390 */
1391 pThisCC->pDevIns = pDevIns;
1392 pThisCC->Keyboard.IBase.pfnQueryInterface = ps2kR3QueryInterface;
1393 pThisCC->Keyboard.IPort.pfnPutEventHid = ps2kR3KeyboardPort_PutEventHid;
1394
1395 pThis->cmdQ.Hdr.pszDesc = "Kbd Cmd";
1396 pThis->keyQ.Hdr.pszDesc = "Kbd Key";
1397
1398 /*
1399 * Create the input rate throttling timer.
1400 */
1401 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2kR3ThrottleTimer, pThis,
1402 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2K Throttle Timer", &pThis->hThrottleTimer);
1403 AssertRCReturn(rc, rc);
1404
1405 /*
1406 * Create the typematic delay/repeat timer.
1407 */
1408 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2kR3TypematicTimer, pThis,
1409 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2K Typematic Timer", &pThis->hKbdTypematicTimer);
1410 AssertRCReturn(rc, rc);
1411
1412 /*
1413 * Create the command delay timer.
1414 */
1415 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2kR3DelayTimer, pThis,
1416 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2K Delay Timer", &pThis->hKbdDelayTimer);
1417 AssertRCReturn(rc, rc);
1418
1419 /*
1420 * Register debugger info callbacks.
1421 */
1422 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2k", "Display PS/2 keyboard state.", ps2kR3InfoState);
1423
1424 return rc;
1425}
1426
1427#endif /* IN _RING3 */
1428
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette