VirtualBox

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

Last change on this file since 104838 was 99558, checked in by vboxsync, 19 months ago

DevPS2K/UsbKbd: Improved range checks.

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