VirtualBox

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

Last change on this file since 89825 was 89812, checked in by vboxsync, 3 years ago

Typo.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 58.3 KB
Line 
1/* $Id: DevPS2K.cpp 89812 2021-06-21 10:41:28Z 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 if (pThisCC->Keyboard.pDrv)
437 pThisCC->Keyboard.pDrv->pfnLedStatusChange(pThisCC->Keyboard.pDrv, enmLeds);
438}
439
440#endif /* IN_RING3 */
441
442/** Clears the currently active typematic key, if any. */
443static void ps2kStopTypematicRepeat(PPDMDEVINS pDevIns, PPS2K pThis)
444{
445 if (pThis->u32TypematicKey)
446 {
447 LogFunc(("Typematic key %08X\n", pThis->u32TypematicKey));
448 pThis->enmTypematicState = KBD_TMS_IDLE;
449 pThis->u32TypematicKey = 0;
450 PDMDevHlpTimerStop(pDevIns, pThis->hKbdTypematicTimer);
451 }
452}
453
454/** Convert encoded typematic value to milliseconds. Note that the values are rated
455 * with +/- 20% accuracy, so there's no need for high precision.
456 */
457static void ps2kSetupTypematic(PPS2K pThis, uint8_t val)
458{
459 int A, B;
460 unsigned period;
461
462 pThis->u8TypematicCfg = val;
463 /* The delay is easy: (1 + value) * 250 ms */
464 pThis->uTypematicDelay = (1 + ((val >> 5) & 3)) * 250;
465 /* The rate is more complicated: (8 + A) * 2^B * 4.17 ms */
466 A = val & 7;
467 B = (val >> 3) & 3;
468 period = (8 + A) * (1 << B) * 417 / 100;
469 pThis->uTypematicRepeat = period;
470 Log(("Typematic delay %u ms, repeat period %u ms\n",
471 pThis->uTypematicDelay, pThis->uTypematicRepeat));
472}
473
474static void ps2kSetDefaults(PPDMDEVINS pDevIns, PPS2K pThis)
475{
476 LogFlowFunc(("Set keyboard defaults\n"));
477 PS2Q_CLEAR(&pThis->keyQ);
478 /* Set default Scan Set 3 typematic values. */
479 /* Set default typematic rate/delay. */
480 ps2kSetupTypematic(pThis, KBD_DFL_RATE_DELAY);
481 /* Clear last typematic key?? */
482 ps2kStopTypematicRepeat(pDevIns, pThis);
483}
484
485/**
486 * The keyboard controller disabled the keyboard serial line.
487 *
488 * @param pThis The keyboard device shared instance data.
489 */
490void PS2KLineDisable(PPS2K pThis)
491{
492 LogFlowFunc(("Disabling keyboard serial line\n"));
493
494 pThis->fLineDisabled = true;
495}
496
497/**
498 * The keyboard controller enabled the keyboard serial line.
499 *
500 * @param pThis The keyboard device shared instance data.
501 */
502void PS2KLineEnable(PPS2K pThis)
503{
504 LogFlowFunc(("Enabling keyboard serial line\n"));
505
506 pThis->fLineDisabled = false;
507
508 /* If there was anything in the input queue,
509 * consider it lost and throw it away.
510 */
511 PS2Q_CLEAR(&pThis->keyQ);
512}
513
514/**
515 * Receive and process a byte sent by the keyboard controller.
516 *
517 * @param pDevIns The device instance.
518 * @param pThis The shared PS/2 keyboard instance data.
519 * @param cmd The command (or data) byte.
520 */
521int PS2KByteToKbd(PPDMDEVINS pDevIns, PPS2K pThis, uint8_t cmd)
522{
523 bool fHandled = true;
524
525 LogFlowFunc(("new cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
526
527 if (pThis->u8CurrCmd == KCMD_RESET)
528 /* In reset mode, do not respond at all. */
529 return VINF_SUCCESS;
530
531 switch (cmd)
532 {
533 case KCMD_ECHO:
534 PS2Q_INSERT(&pThis->cmdQ, KRSP_ECHO);
535 pThis->u8CurrCmd = 0;
536 break;
537 case KCMD_READ_ID:
538 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
539 PS2Q_INSERT(&pThis->cmdQ, KRSP_ID1);
540 PS2Q_INSERT(&pThis->cmdQ, KRSP_ID2);
541 pThis->u8CurrCmd = 0;
542 break;
543 case KCMD_ENABLE:
544 pThis->fScanning = true;
545 PS2Q_CLEAR(&pThis->keyQ);
546 ps2kStopTypematicRepeat(pDevIns, pThis);
547 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
548 pThis->u8CurrCmd = 0;
549 break;
550 case KCMD_DFLT_DISABLE:
551 pThis->fScanning = false;
552 ps2kSetDefaults(pDevIns, pThis); /* Also clears buffer/typematic state. */
553 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
554 pThis->u8CurrCmd = 0;
555 break;
556 case KCMD_SET_DEFAULT:
557 ps2kSetDefaults(pDevIns, pThis);
558 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
559 pThis->u8CurrCmd = 0;
560 break;
561 case KCMD_ALL_TYPEMATIC:
562 case KCMD_ALL_MK_BRK:
563 case KCMD_ALL_MAKE:
564 case KCMD_ALL_TMB:
565 /// @todo Set the key types here.
566 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
567 pThis->u8CurrCmd = 0;
568 break;
569 case KCMD_RESEND:
570 pThis->u8CurrCmd = 0;
571 break;
572 case KCMD_RESET:
573 pThis->u8ScanSet = 2;
574 ps2kSetDefaults(pDevIns, pThis);
575 /// @todo reset more?
576 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
577 pThis->u8CurrCmd = cmd;
578 /* Delay BAT completion; the test may take hundreds of ms. */
579 PDMDevHlpTimerSetMillies(pDevIns, pThis->hKbdDelayTimer, 2);
580 break;
581 /* The following commands need a parameter. */
582 case KCMD_LEDS:
583 case KCMD_SCANSET:
584 case KCMD_RATE_DELAY:
585 case KCMD_TYPE_MATIC:
586 case KCMD_TYPE_MK_BRK:
587 case KCMD_TYPE_MAKE:
588 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
589 pThis->u8CurrCmd = cmd;
590 break;
591 default:
592 /* Sending a command instead of a parameter starts the new command. */
593 switch (pThis->u8CurrCmd)
594 {
595 case KCMD_LEDS:
596#ifndef IN_RING3
597 return VINF_IOM_R3_IOPORT_WRITE;
598#else
599 {
600 PPS2KR3 pThisCC = &PDMDEVINS_2_DATA_CC(pDevIns, PKBDSTATER3)->Kbd;
601 ps2kR3NotifyLedsState(pThisCC, cmd);
602 pThis->fNumLockOn = !!(cmd & 0x02); /* Sync internal Num Lock state. */
603 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
604 pThis->u8LEDs = cmd;
605 pThis->u8CurrCmd = 0;
606 }
607#endif
608 break;
609 case KCMD_SCANSET:
610 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
611 if (cmd == 0)
612 PS2Q_INSERT(&pThis->cmdQ, pThis->u8ScanSet);
613 else if (cmd < 4)
614 {
615 pThis->u8ScanSet = cmd;
616 LogRel(("PS2K: Selected scan set %d\n", cmd));
617 }
618 /* Other values are simply ignored. */
619 pThis->u8CurrCmd = 0;
620 break;
621 case KCMD_RATE_DELAY:
622 ps2kSetupTypematic(pThis, cmd);
623 PS2Q_INSERT(&pThis->cmdQ, KRSP_ACK);
624 pThis->u8CurrCmd = 0;
625 break;
626 default:
627 fHandled = false;
628 }
629 /* Fall through only to handle unrecognized commands. */
630 if (fHandled)
631 break;
632 RT_FALL_THRU();
633
634 case KCMD_INVALID_1:
635 case KCMD_INVALID_2:
636 PS2Q_INSERT(&pThis->cmdQ, KRSP_RESEND);
637 pThis->u8CurrCmd = 0;
638 break;
639 }
640 LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
641// KBCUpdateInterrupts(pDevIns);
642 return VINF_SUCCESS;
643}
644
645/**
646 * Send a byte (keystroke or command response) to the keyboard controller.
647 *
648 * @returns VINF_SUCCESS or VINF_TRY_AGAIN.
649 * @param pDevIns The device instance.
650 * @param pThis The shared PS/2 keyboard instance data.
651 * @param pb Where to return the byte we've read.
652 * @remarks Caller must have entered the device critical section.
653 */
654int PS2KByteFromKbd(PPDMDEVINS pDevIns, PPS2K pThis, uint8_t *pb)
655{
656 int rc;
657
658 AssertPtr(pb);
659
660 /* Anything in the command queue has priority over data
661 * in the keystroke queue. Additionally, keystrokes are
662 * blocked if a command is currently in progress, even if
663 * the command queue is empty.
664 */
665 rc = PS2Q_REMOVE(&pThis->cmdQ, pb);
666 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && pThis->fScanning)
667 if (!pThis->fThrottleActive)
668 {
669 rc = PS2Q_REMOVE(&pThis->keyQ, pb);
670 if (pThis->fThrottleEnabled)
671 {
672 pThis->fThrottleActive = true;
673 PDMDevHlpTimerSetMillies(pDevIns, pThis->hThrottleTimer, KBD_THROTTLE_DELAY);
674 }
675 }
676
677 LogFlowFunc(("keyboard sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
678 return rc;
679}
680
681#ifdef IN_RING3
682
683static int ps2kR3ProcessKeyEvent(PPDMDEVINS pDevIns, PPS2K pThis, uint32_t u32HidCode, bool fKeyDown)
684{
685 key_def const *pKeyDef;
686 uint8_t abCodes[16];
687 char *pCodes;
688 size_t cbLeft;
689 uint8_t abScan[2];
690 uint8_t u8HidPage;
691 uint8_t u8HidCode;
692 uint16_t u16HidUsage;
693
694 u8HidPage = RT_LOBYTE(RT_HIWORD(u32HidCode));
695 u16HidUsage = RT_LOWORD(u32HidCode);
696 /* For the keyboard usage page (7) we use a 8-bit code. For other pages we use the full 16-bit ID. */
697 u8HidCode = (u8HidPage == USB_HID_KB_PAGE) ? RT_LOBYTE(u32HidCode) : 0;
698
699 LogFlowFunc(("key %s: page 0x%02x ID 0x%04x (set %d)\n", fKeyDown ? "down" : "up", u8HidPage, u16HidUsage, pThis->u8ScanSet));
700
701 /* Find the key definition in somewhat sparse storage. */
702 if (u8HidPage == USB_HID_KB_PAGE)
703 /* For the standard keyboard usage page, thre are just two arrays. */
704 pKeyDef = u8HidCode >= HID_MODIFIER_FIRST ? &aPS2ModKeys[u8HidCode - HID_MODIFIER_FIRST] : &aPS2Keys[u8HidCode];
705 else if (u8HidPage == USB_HID_CC_PAGE)
706 {
707 /* For the consumer control usage page, we need to search. */
708 unsigned i;
709
710 pKeyDef = &aPS2Keys[0]; /* Dummy no-event key. */
711 for (i = 0; i < RT_ELEMENTS(aPS2CCKeys); ++i)
712 {
713 if (aPS2CCKeys[i].usageId == u16HidUsage)
714 {
715 pKeyDef = &aPS2CCKeys[i].kdef;
716 break;
717 }
718 }
719 }
720 else
721 {
722 LogFlow(("Unsupported HID usage page, ignoring key.\n"));
723 return VINF_SUCCESS;
724 }
725
726 /* Some keys are not processed at all; early return. */
727 if (pKeyDef->makeS1 == NONE)
728 {
729 LogFlow(("Skipping key processing.\n"));
730 return VINF_SUCCESS;
731 }
732
733 /* Handle modifier keys (Ctrl/Alt/Shift/GUI). We need to keep track
734 * of their state in addition to sending the scan code.
735 */
736 if (u8HidCode >= HID_MODIFIER_FIRST)
737 {
738 unsigned mod_bit = 1 << (u8HidCode - HID_MODIFIER_FIRST);
739
740 Assert((u8HidPage == USB_HID_KB_PAGE));
741 Assert((u8HidCode <= HID_MODIFIER_LAST));
742 if (fKeyDown)
743 pThis->u8Modifiers |= mod_bit;
744 else
745 pThis->u8Modifiers &= ~mod_bit;
746 }
747
748 /* Toggle NumLock state. */
749 if ((pKeyDef->keyFlags & KF_NL) && fKeyDown)
750 pThis->fNumLockOn ^= true;
751
752 abCodes[0] = 0;
753 pCodes = (char *)abCodes;
754 cbLeft = sizeof(abCodes);
755
756 if (pThis->u8ScanSet == 1 || pThis->u8ScanSet == 2)
757 {
758 /* The basic scan set 1 and 2 logic is the same, only the scan codes differ.
759 * Since scan set 2 is used almost all the time, that case is handled first.
760 */
761 if (fKeyDown)
762 {
763 /* Process key down event. */
764 if (pKeyDef->keyFlags & KF_PB)
765 {
766 /* Pause/Break sends different data if either Ctrl is held. */
767 if (pThis->u8Modifiers & (MOD_LCTRL | MOD_RCTRL))
768 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
769 "\xE0\x7E\xE0\xF0\x7E" : "\xE0\x46\xE0\xC6");
770 else
771 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
772 "\xE1\x14\x77\xE1\xF0\x14\xF0\x77" : "\xE1\x1D\x45\xE1\x9D\xC5");
773 }
774 else if (pKeyDef->keyFlags & KF_PS)
775 {
776 /* Print Screen depends on all of Ctrl, Shift, *and* Alt! */
777 if (pThis->u8Modifiers & (MOD_LALT | MOD_RALT))
778 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
779 "\x84" : "\x54");
780 else if (pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT))
781 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
782 "\xE0\x7C" : "\xE0\x37");
783 else
784 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
785 "\xE0\x12\xE0\x7C" : "\xE0\x2A\xE0\x37");
786 }
787 else if (pKeyDef->keyFlags & (KF_GK | KF_NS))
788 {
789 /* The numeric pad keys fake Shift presses or releases
790 * depending on Num Lock and Shift key state. The '/'
791 * key behaves in a similar manner but does not depend on
792 * the Num Lock state.
793 */
794 if (!pThis->fNumLockOn || (pKeyDef->keyFlags & KF_NS))
795 {
796 if (pThis->u8Modifiers & MOD_LSHIFT)
797 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
798 "\xE0\xF0\x12" : "\xE0\xAA");
799 if (pThis->u8Modifiers & MOD_RSHIFT)
800 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
801 "\xE0\xF0\x59" : "\xE0\xB6");
802 }
803 else
804 {
805 Assert(pThis->fNumLockOn); /* Not for KF_NS! */
806 if ((pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT)) == 0)
807 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
808 "\xE0\x12" : "\xE0\x2A");
809 /* Else Shift cancels NumLock, so no prefix! */
810 }
811 }
812
813 /* Standard processing for regular keys only. */
814 abScan[0] = pThis->u8ScanSet == 2 ? pKeyDef->makeS2 : pKeyDef->makeS1;
815 abScan[1] = '\0';
816 if (!(pKeyDef->keyFlags & (KF_PB | KF_PS)))
817 {
818 if (pKeyDef->keyFlags & (KF_E0 | KF_GK | KF_NS))
819 RTStrCatP(&pCodes, &cbLeft, "\xE0");
820 RTStrCatP(&pCodes, &cbLeft, (const char *)abScan);
821 }
822
823 /* Feed the bytes to the queue if there is room. */
824 /// @todo Send overrun code if sequence won't fit?
825 ps2kR3InsertStrQueue(&pThis->keyQ, abCodes, 0);
826 }
827 else if (!(pKeyDef->keyFlags & (KF_NB | KF_PB)))
828 {
829 /* Process key up event except for keys which produce none. */
830
831 /* Handle Print Screen release. */
832 if (pKeyDef->keyFlags & KF_PS)
833 {
834 /* Undo faked Print Screen state as needed. */
835 if (pThis->u8Modifiers & (MOD_LALT | MOD_RALT))
836 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
837 "\xF0\x84" : "\xD4");
838 else if (pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT))
839 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
840 "\xE0\xF0\x7C" : "\xE0\xB7");
841 else
842 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
843 "\xE0\xF0\x7C\xE0\xF0\x12" : "\xE0\xB7\xE0\xAA");
844 }
845 else
846 {
847 /* Process base scan code for less unusual keys. */
848 abScan[0] = pThis->u8ScanSet == 2 ? pKeyDef->makeS2 : pKeyDef->makeS1 | 0x80;
849 abScan[1] = '\0';
850 if (pKeyDef->keyFlags & (KF_E0 | KF_GK | KF_NS))
851 RTStrCatP(&pCodes, &cbLeft, "\xE0");
852 if (pThis->u8ScanSet == 2)
853 RTStrCatP(&pCodes, &cbLeft, "\xF0");
854 RTStrCatP(&pCodes, &cbLeft, (const char *)abScan);
855
856 /* Restore shift state for gray keys. */
857 if (pKeyDef->keyFlags & (KF_GK | KF_NS))
858 {
859 if (!pThis->fNumLockOn || (pKeyDef->keyFlags & KF_NS))
860 {
861 if (pThis->u8Modifiers & MOD_LSHIFT)
862 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
863 "\xE0\x12" : "\xE0\x2A");
864 if (pThis->u8Modifiers & MOD_RSHIFT)
865 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
866 "\xE0\x59" : "\xE0\x36");
867 }
868 else
869 {
870 Assert(pThis->fNumLockOn); /* Not for KF_NS! */
871 if ((pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT)) == 0)
872 RTStrCatP(&pCodes, &cbLeft, pThis->u8ScanSet == 2 ?
873 "\xE0\xF0\x12" : "\xE0\xAA");
874 }
875 }
876 }
877
878 /* Feed the bytes to the queue if there is room. */
879 /// @todo Send overrun code if sequence won't fit?
880 ps2kR3InsertStrQueue(&pThis->keyQ, abCodes, 0);
881 }
882 }
883 else
884 {
885 /* Handle Scan Set 3 - very straightforward. */
886 Assert(pThis->u8ScanSet == 3);
887 abScan[0] = pKeyDef->makeS3;
888 abScan[1] = '\0';
889 if (fKeyDown)
890 {
891 RTStrCatP(&pCodes, &cbLeft, (const char *)abScan);
892 }
893 else
894 {
895 /* Send a key release code unless it's a make only key. */
896 /// @todo Look up the current typematic setting, not the default!
897 if (pKeyDef->keyMatic != T_M)
898 {
899 RTStrCatP(&pCodes, &cbLeft, "\xF0");
900 RTStrCatP(&pCodes, &cbLeft, (const char *)abScan);
901 }
902 }
903 /* Feed the bytes to the queue if there is room. */
904 /// @todo Send overrun code if sequence won't fit?
905 ps2kR3InsertStrQueue(&pThis->keyQ, abCodes, 0);
906 }
907
908 /* Set up or cancel typematic key repeat. For keyboard usage page only. */
909 if (u8HidPage == USB_HID_KB_PAGE)
910 {
911 if (fKeyDown)
912 {
913 if (pThis->u32TypematicKey != u32HidCode)
914 {
915 pThis->enmTypematicState = KBD_TMS_DELAY;
916 pThis->u32TypematicKey = u32HidCode;
917 PDMDevHlpTimerSetMillies(pDevIns, pThis->hKbdTypematicTimer, pThis->uTypematicDelay);
918 Log(("Typematic delay %u ms, key %08X\n", pThis->uTypematicDelay, u32HidCode));
919 }
920 }
921 else
922 {
923 /* "Typematic operation stops when the last key pressed is released, even
924 * if other keys are still held down." (IBM PS/2 Tech Ref). The last key pressed
925 * is the one that's being repeated.
926 */
927 if (pThis->u32TypematicKey == u32HidCode)
928 {
929 /* This disables the typematic repeat. */
930 pThis->u32TypematicKey = 0;
931 pThis->enmTypematicState = KBD_TMS_IDLE;
932 /* For good measure, we cancel the timer, too. */
933 PDMDevHlpTimerStop(pDevIns, pThis->hKbdTypematicTimer);
934 Log(("Typematic action cleared for key %08X\n", u32HidCode));
935 }
936 }
937 }
938
939 /* Poke the KBC to update its state. */
940 KBCUpdateInterrupts(pDevIns);
941
942 return VINF_SUCCESS;
943}
944
945/**
946 * @callback_method_impl{FNTMTIMERDEV}
947 *
948 * Throttling timer to emulate the finite keyboard communication speed. A PS/2 keyboard is
949 * limited by the serial link speed and cannot send much more than 1,000 bytes per second.
950 * Some software (notably Borland Pascal and programs built with its run-time) relies on
951 * being able to read an incoming scan-code twice. Throttling the data rate enables such
952 * software to function, while human typists cannot tell any difference.
953 *
954 * Note: The throttling is currently only done for keyboard data, not command responses.
955 * The throttling could and perhaps should be done for any data (including command
956 * responses) coming from PS/2 devices, both keyboard and auxiliary. That is not currently
957 * done because it would needlessly slow things down.
958 */
959static DECLCALLBACK(void) ps2kR3ThrottleTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
960{
961 PPS2K pThis = (PS2K *)pvUser;
962 unsigned uHaveData;
963 RT_NOREF(hTimer);
964
965 /* Grab the lock to avoid races with event delivery or EMTs. */
966 int rc = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_SEM_BUSY);
967 AssertReleaseRC(rc);
968
969 /* If data is available, poke the KBC. Once the data
970 * is actually read, the timer may be re-triggered.
971 */
972 pThis->fThrottleActive = false;
973 uHaveData = PS2Q_COUNT(&pThis->keyQ);
974 LogFlowFunc(("Have%s bytes\n", uHaveData ? "" : " no"));
975 if (uHaveData)
976 KBCUpdateInterrupts(pDevIns);
977
978 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
979}
980
981/**
982 * @callback_method_impl{FNTMTIMERDEV,
983 * Timer handler for emulating typematic keys.}
984 *
985 * @note Note that only the last key held down repeats (if typematic).
986 */
987static DECLCALLBACK(void) ps2kR3TypematicTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
988{
989 PPS2K pThis = (PS2K *)pvUser;
990 Assert(hTimer == pThis->hKbdTypematicTimer);
991 LogFlowFunc(("Typematic state=%d, key %08X\n", pThis->enmTypematicState, pThis->u32TypematicKey));
992
993 /* If the current typematic key is zero, the repeat was canceled just when
994 * the timer was about to run. In that case, do nothing.
995 */
996 if (pThis->u32TypematicKey)
997 {
998 if (pThis->enmTypematicState == KBD_TMS_DELAY)
999 pThis->enmTypematicState = KBD_TMS_REPEAT;
1000
1001 if (pThis->enmTypematicState == KBD_TMS_REPEAT)
1002 {
1003 ps2kR3ProcessKeyEvent(pDevIns, pThis, pThis->u32TypematicKey, true /* Key down */ );
1004 PDMDevHlpTimerSetMillies(pDevIns, hTimer, pThis->uTypematicRepeat);
1005 }
1006 }
1007}
1008
1009/**
1010 * @callback_method_impl{FNTMTIMERDEV}
1011 *
1012 * The keyboard BAT is specified to take several hundred milliseconds. We need
1013 * to delay sending the result to the host for at least a tiny little while.
1014 */
1015static DECLCALLBACK(void) ps2kR3DelayTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
1016{
1017 PPS2K pThis = (PS2K *)pvUser;
1018 RT_NOREF(hTimer);
1019
1020 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
1021
1022 AssertMsg(pThis->u8CurrCmd == KCMD_RESET, ("u8CurrCmd=%02x\n", pThis->u8CurrCmd));
1023 PS2Q_INSERT(&pThis->cmdQ, KRSP_BAT_OK);
1024 pThis->fScanning = true; /* BAT completion enables scanning! */
1025 pThis->u8CurrCmd = 0;
1026
1027 /// @todo Might want a PS2KCompleteCommand() to push last response, clear command, and kick the KBC...
1028 /* Give the KBC a kick. */
1029 KBCUpdateInterrupts(pDevIns);
1030}
1031
1032/* Release any and all currently depressed keys. Used whenever the guest keyboard
1033 * is likely to be out of sync with the host, such as when loading a saved state
1034 * or resuming a suspended host.
1035 */
1036static void ps2kR3ReleaseKeys(PPDMDEVINS pDevIns, PPS2K pThis)
1037{
1038 LogFlowFunc(("Releasing keys...\n"));
1039
1040 for (unsigned uKey = 0; uKey < sizeof(pThis->abDepressedKeys); ++uKey)
1041 if (pThis->abDepressedKeys[uKey])
1042 {
1043 ps2kR3ProcessKeyEvent(pDevIns, pThis, RT_MAKE_U32(USB_HID_KB_PAGE, uKey), false /* key up */);
1044 pThis->abDepressedKeys[uKey] = 0;
1045 }
1046 LogFlowFunc(("Done releasing keys\n"));
1047}
1048
1049
1050/**
1051 * Debug device info handler. Prints basic keyboard state.
1052 *
1053 * @param pDevIns Device instance which registered the info.
1054 * @param pHlp Callback functions for doing output.
1055 * @param pszArgs Argument string. Optional and specific to the handler.
1056 */
1057static DECLCALLBACK(void) ps2kR3InfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1058{
1059 PKBDSTATE pParent = PDMDEVINS_2_DATA(pDevIns, PKBDSTATE);
1060 PPS2K pThis = &pParent->Kbd;
1061 NOREF(pszArgs);
1062
1063 pHlp->pfnPrintf(pHlp, "PS/2 Keyboard: scan set %d, scanning %s, serial line %s\n",
1064 pThis->u8ScanSet, pThis->fScanning ? "enabled" : "disabled",
1065 pThis->fLineDisabled ? "disabled" : "enabled");
1066 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
1067 pHlp->pfnPrintf(pHlp, "LED state %02X, Num Lock %s\n", pThis->u8LEDs,
1068 pThis->fNumLockOn ? "on" : "off");
1069 pHlp->pfnPrintf(pHlp, "Typematic delay %ums, repeat period %ums\n",
1070 pThis->uTypematicDelay, pThis->uTypematicRepeat);
1071 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
1072 PS2Q_COUNT(&pThis->cmdQ), PS2Q_SIZE(&pThis->cmdQ));
1073 pHlp->pfnPrintf(pHlp, "Input queue : %d items (%d max)\n",
1074 PS2Q_COUNT(&pThis->keyQ), PS2Q_SIZE(&pThis->keyQ));
1075 if (pThis->enmTypematicState != KBD_TMS_IDLE)
1076 pHlp->pfnPrintf(pHlp, "Active typematic key %08X (%s)\n", pThis->u32TypematicKey,
1077 pThis->enmTypematicState == KBD_TMS_DELAY ? "delay" : "repeat");
1078}
1079
1080
1081/* -=-=-=-=-=- Keyboard: IKeyboardPort -=-=-=-=-=- */
1082
1083/**
1084 * Keyboard event handler.
1085 *
1086 * @returns VBox status code.
1087 * @param pDevIns The device instance.
1088 * @param pThis The PS2 keyboard instance data.
1089 * @param idUsage USB HID usage code with key press/release flag.
1090 */
1091static int ps2kR3PutEventWorker(PPDMDEVINS pDevIns, PPS2K pThis, uint32_t idUsage)
1092{
1093 uint32_t u32HidCode;
1094 uint8_t u8KeyCode;
1095 uint8_t u8HidPage;
1096 bool fKeyDown;
1097 bool fHaveEvent = true;
1098 int rc = VINF_SUCCESS;
1099
1100 /* Extract the usage page and ID and ensure it's valid. */
1101 fKeyDown = !(idUsage & UINT32_C(0x80000000));
1102 u32HidCode = idUsage & 0xFFFFFF;
1103 u8HidPage = RT_LOBYTE(RT_HIWORD(idUsage));
1104 u8KeyCode = RT_LOBYTE(idUsage);
1105 if (u8HidPage == USB_HID_KB_PAGE)
1106 AssertReturn(u8KeyCode <= VBOX_USB_MAX_USAGE_CODE, VERR_INTERNAL_ERROR);
1107 else
1108 AssertReturn(u8HidPage == USB_HID_CC_PAGE, VERR_INTERNAL_ERROR);
1109
1110 if (fKeyDown)
1111 {
1112 /* Due to host key repeat, we can get key events for keys which are
1113 * already depressed. We need to ignore those. */
1114 if (pThis->abDepressedKeys[u8KeyCode])
1115 fHaveEvent = false;
1116 pThis->abDepressedKeys[u8KeyCode] = 1;
1117 }
1118 else
1119 {
1120 /* NB: We allow key release events for keys which aren't depressed.
1121 * That is unlikely to happen and should not cause trouble.
1122 */
1123 pThis->abDepressedKeys[u8KeyCode] = 0;
1124 }
1125
1126 /* Unless this is a new key press/release, don't even bother. */
1127 if (fHaveEvent)
1128 {
1129 Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->pCritSectRoR3));
1130 rc = ps2kR3ProcessKeyEvent(pDevIns, pThis, u32HidCode, fKeyDown);
1131 }
1132
1133 return rc;
1134}
1135
1136
1137/**
1138 * @interface_method_impl{PDMIKEYBOARDPORT,pfnPutEventHid}
1139 */
1140static DECLCALLBACK(int) ps2kR3KeyboardPort_PutEventHid(PPDMIKEYBOARDPORT pInterface, uint32_t idUsage)
1141{
1142 PPS2KR3 pThisCC = RT_FROM_MEMBER(pInterface, PS2KR3, Keyboard.IPort);
1143 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1144 PPS2K pThis = &PDMDEVINS_2_DATA(pDevIns, PKBDSTATE)->Kbd;
1145 int rc;
1146
1147 LogRelFlowFunc(("key code %08X\n", idUsage));
1148
1149 rc = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_SEM_BUSY);
1150 AssertReleaseRC(rc);
1151
1152 /* The 'BAT fail' scancode is reused as a signal to release keys. No actual
1153 * key is allowed to use this scancode.
1154 */
1155 if (RT_LIKELY(idUsage != KRSP_BAT_FAIL))
1156 ps2kR3PutEventWorker(pDevIns, pThis, idUsage);
1157 else
1158 ps2kR3ReleaseKeys(pDevIns, pThis);
1159
1160 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
1161
1162 return VINF_SUCCESS;
1163}
1164
1165
1166/* -=-=-=-=-=- Keyboard: IBase -=-=-=-=-=- */
1167
1168/**
1169 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1170 */
1171static DECLCALLBACK(void *) ps2kR3QueryInterface(PPDMIBASE pInterface, const char *pszIID)
1172{
1173 PPS2KR3 pThisCC = RT_FROM_MEMBER(pInterface, PS2KR3, Keyboard.IBase);
1174 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->Keyboard.IBase);
1175 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDPORT, &pThisCC->Keyboard.IPort);
1176 return NULL;
1177}
1178
1179
1180/* -=-=-=-=-=- Device management -=-=-=-=-=- */
1181
1182/**
1183 * Attach command.
1184 *
1185 * This is called to let the device attach to a driver for a
1186 * specified LUN.
1187 *
1188 * This is like plugging in the keyboard after turning on the
1189 * system.
1190 *
1191 * @returns VBox status code.
1192 * @param pDevIns The device instance.
1193 * @param pThisCC The PS/2 keyboard instance data for ring-3.
1194 * @param iLUN The logical unit which is being detached.
1195 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1196 */
1197int PS2KR3Attach(PPDMDEVINS pDevIns, PPS2KR3 pThisCC, unsigned iLUN, uint32_t fFlags)
1198{
1199 int rc;
1200
1201 /* The LUN must be 0, i.e. keyboard. */
1202 Assert(iLUN == 0);
1203 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
1204 ("PS/2 keyboard does not support hotplugging\n"),
1205 VERR_INVALID_PARAMETER);
1206
1207 LogFlowFunc(("iLUN=%d\n", iLUN));
1208
1209 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThisCC->Keyboard.IBase, &pThisCC->Keyboard.pDrvBase, "Keyboard Port");
1210 if (RT_SUCCESS(rc))
1211 {
1212 pThisCC->Keyboard.pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->Keyboard.pDrvBase, PDMIKEYBOARDCONNECTOR);
1213 if (!pThisCC->Keyboard.pDrv)
1214 {
1215 AssertLogRelMsgFailed(("LUN #0 doesn't have a keyboard interface! rc=%Rrc\n", rc));
1216 rc = VERR_PDM_MISSING_INTERFACE;
1217 }
1218 }
1219 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1220 {
1221 Log(("%s/%d: warning: no driver attached to LUN #0!\n", pDevIns->pReg->szName, pDevIns->iInstance));
1222 rc = VINF_SUCCESS;
1223 }
1224 else
1225 AssertLogRelMsgFailed(("Failed to attach LUN #0! rc=%Rrc\n", rc));
1226
1227 return rc;
1228}
1229
1230void PS2KR3SaveState(PPDMDEVINS pDevIns, PPS2K pThis, PSSMHANDLE pSSM)
1231{
1232 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1233 uint32_t cPressed = 0;
1234 uint32_t cbTMSSize = 0;
1235
1236 LogFlowFunc(("Saving PS2K state\n"));
1237
1238 /* Save the basic keyboard state. */
1239 pHlp->pfnSSMPutU8(pSSM, pThis->u8CurrCmd);
1240 pHlp->pfnSSMPutU8(pSSM, pThis->u8LEDs);
1241 pHlp->pfnSSMPutU8(pSSM, pThis->u8TypematicCfg);
1242 pHlp->pfnSSMPutU8(pSSM, (uint8_t)pThis->u32TypematicKey);
1243 pHlp->pfnSSMPutU8(pSSM, pThis->u8Modifiers);
1244 pHlp->pfnSSMPutU8(pSSM, pThis->u8ScanSet);
1245 pHlp->pfnSSMPutU8(pSSM, pThis->enmTypematicState);
1246 pHlp->pfnSSMPutBool(pSSM, pThis->fNumLockOn);
1247 pHlp->pfnSSMPutBool(pSSM, pThis->fScanning);
1248
1249 /* Save the command and keystroke queues. */
1250 PS2Q_SAVE(pHlp, pSSM, &pThis->cmdQ);
1251 PS2Q_SAVE(pHlp, pSSM, &pThis->keyQ);
1252
1253 /* Save the command delay timer. Note that the typematic repeat
1254 * timer is *not* saved.
1255 */
1256 PDMDevHlpTimerSave(pDevIns, pThis->hKbdDelayTimer, pSSM);
1257
1258 /* Save any pressed keys. This is necessary to avoid "stuck"
1259 * keys after a restore. Needs two passes.
1260 */
1261 for (unsigned i = 0; i < sizeof(pThis->abDepressedKeys); ++i)
1262 if (pThis->abDepressedKeys[i])
1263 ++cPressed;
1264
1265 pHlp->pfnSSMPutU32(pSSM, cPressed);
1266
1267 for (unsigned uKey = 0; uKey < sizeof(pThis->abDepressedKeys); ++uKey)
1268 if (pThis->abDepressedKeys[uKey])
1269 pHlp->pfnSSMPutU8(pSSM, uKey);
1270
1271 /* Save the typematic settings for Scan Set 3. */
1272 pHlp->pfnSSMPutU32(pSSM, cbTMSSize);
1273 /* Currently not implemented. */
1274}
1275
1276int PS2KR3LoadState(PPDMDEVINS pDevIns, PPS2K pThis, PSSMHANDLE pSSM, uint32_t uVersion)
1277{
1278 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1279 uint8_t u8;
1280 uint32_t cPressed;
1281 uint32_t cbTMSSize;
1282 int rc;
1283
1284 NOREF(uVersion);
1285 LogFlowFunc(("Loading PS2K state version %u\n", uVersion));
1286
1287 /* Load the basic keyboard state. */
1288 pHlp->pfnSSMGetU8(pSSM, &pThis->u8CurrCmd);
1289 pHlp->pfnSSMGetU8(pSSM, &pThis->u8LEDs);
1290 pHlp->pfnSSMGetU8(pSSM, &pThis->u8TypematicCfg);
1291 pHlp->pfnSSMGetU8(pSSM, &u8);
1292 /* Reconstruct the 32-bit code from the 8-bit value in saved state. */
1293 pThis->u32TypematicKey = u8 ? RT_MAKE_U32(USB_HID_KB_PAGE, u8) : 0;
1294 pHlp->pfnSSMGetU8(pSSM, &pThis->u8Modifiers);
1295 pHlp->pfnSSMGetU8(pSSM, &pThis->u8ScanSet);
1296 pHlp->pfnSSMGetU8(pSSM, &u8);
1297 pThis->enmTypematicState = (tmatic_state_t)u8;
1298 pHlp->pfnSSMGetBool(pSSM, &pThis->fNumLockOn);
1299 pHlp->pfnSSMGetBool(pSSM, &pThis->fScanning);
1300
1301 /* Load the command and keystroke queues. */
1302 rc = PS2Q_LOAD(pHlp, pSSM, &pThis->cmdQ);
1303 AssertRCReturn(rc, rc);
1304 rc = PS2Q_LOAD(pHlp, pSSM, &pThis->keyQ);
1305 AssertRCReturn(rc, rc);
1306
1307 /* Load the command delay timer, just in case. */
1308 rc = PDMDevHlpTimerLoad(pDevIns, pThis->hKbdDelayTimer, pSSM);
1309 AssertRCReturn(rc, rc);
1310
1311 /* Recalculate the typematic delay/rate. */
1312 ps2kSetupTypematic(pThis, pThis->u8TypematicCfg);
1313
1314 /* Fake key up events for keys that were held down at the time the state was saved. */
1315 rc = pHlp->pfnSSMGetU32(pSSM, &cPressed);
1316 AssertRCReturn(rc, rc);
1317
1318 /* If any keys were down, load and then release them. */
1319 if (cPressed)
1320 {
1321 for (unsigned i = 0; i < cPressed; ++i)
1322 {
1323 rc = pHlp->pfnSSMGetU8(pSSM, &u8);
1324 AssertRCReturn(rc, rc);
1325 pThis->abDepressedKeys[u8] = 1;
1326 }
1327 }
1328
1329 /* Load typematic settings for Scan Set 3. */
1330 rc = pHlp->pfnSSMGetU32(pSSM, &cbTMSSize);
1331 AssertRCReturn(rc, rc);
1332
1333 while (cbTMSSize--)
1334 {
1335 rc = pHlp->pfnSSMGetU8(pSSM, &u8);
1336 AssertRCReturn(rc, rc);
1337 }
1338
1339 return rc;
1340}
1341
1342int PS2KR3LoadDone(PPDMDEVINS pDevIns, PPS2K pThis, PPS2KR3 pThisCC)
1343{
1344 /* This *must* be done after the inital load because it may trigger
1345 * interrupts and change the interrupt controller state.
1346 */
1347 ps2kR3ReleaseKeys(pDevIns, pThis);
1348 ps2kR3NotifyLedsState(pThisCC, pThis->u8LEDs);
1349 return VINF_SUCCESS;
1350}
1351
1352void PS2KR3Reset(PPDMDEVINS pDevIns, PPS2K pThis, PPS2KR3 pThisCC)
1353{
1354 LogFlowFunc(("Resetting PS2K\n"));
1355
1356 pThis->fScanning = true;
1357 pThis->fThrottleActive = false;
1358 pThis->u8ScanSet = 2;
1359 pThis->u8CurrCmd = 0;
1360 pThis->u8Modifiers = 0;
1361 pThis->u32TypematicKey = 0;
1362 pThis->enmTypematicState = KBD_TMS_IDLE;
1363
1364 /* Clear queues and any pressed keys. */
1365 memset(pThis->abDepressedKeys, 0, sizeof(pThis->abDepressedKeys));
1366 PS2Q_CLEAR(&pThis->cmdQ);
1367 ps2kSetDefaults(pDevIns, pThis); /* Also clears keystroke queue. */
1368
1369 /* Activate the PS/2 keyboard by default. */
1370 if (pThisCC->Keyboard.pDrv)
1371 pThisCC->Keyboard.pDrv->pfnSetActive(pThisCC->Keyboard.pDrv, true /*fActive*/);
1372}
1373
1374int PS2KR3Construct(PPDMDEVINS pDevIns, PPS2K pThis, PPS2KR3 pThisCC, PCFGMNODE pCfg)
1375{
1376 LogFlowFunc(("\n"));
1377 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1378
1379 /*
1380 * Read configuration.
1381 */
1382 bool fThrottleEnabled;
1383 int rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "KbdThrottleEnabled", &fThrottleEnabled, true);
1384 if (RT_FAILURE(rc))
1385 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to query \"KbdThrottleEnabled\" from the config"));
1386 Log(("KbdThrottleEnabled=%RTbool\n", fThrottleEnabled));
1387 pThis->fThrottleEnabled = fThrottleEnabled;
1388
1389 /*
1390 * Initialize state.
1391 */
1392 pThisCC->pDevIns = pDevIns;
1393 pThisCC->Keyboard.IBase.pfnQueryInterface = ps2kR3QueryInterface;
1394 pThisCC->Keyboard.IPort.pfnPutEventHid = ps2kR3KeyboardPort_PutEventHid;
1395
1396 pThis->cmdQ.Hdr.pszDesc = "Kbd Cmd";
1397 pThis->keyQ.Hdr.pszDesc = "Kbd Key";
1398
1399 /*
1400 * Create the input rate throttling timer.
1401 */
1402 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2kR3ThrottleTimer, pThis,
1403 TMTIMER_FLAGS_DEFAULT_CRIT_SECT | TMTIMER_FLAGS_RING0,
1404 "PS2K Throttle", &pThis->hThrottleTimer);
1405 AssertRCReturn(rc, rc);
1406
1407 /*
1408 * Create the typematic delay/repeat timer.
1409 */
1410 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2kR3TypematicTimer, pThis,
1411 TMTIMER_FLAGS_DEFAULT_CRIT_SECT | TMTIMER_FLAGS_RING0,
1412 "PS2K Typematic", &pThis->hKbdTypematicTimer);
1413 AssertRCReturn(rc, rc);
1414
1415 /*
1416 * Create the command delay timer.
1417 */
1418 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2kR3DelayTimer, pThis,
1419 TMTIMER_FLAGS_DEFAULT_CRIT_SECT | TMTIMER_FLAGS_RING0,
1420 "PS2K Delay", &pThis->hKbdDelayTimer);
1421 AssertRCReturn(rc, rc);
1422
1423 /*
1424 * Register debugger info callbacks.
1425 */
1426 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2k", "Display PS/2 keyboard state.", ps2kR3InfoState);
1427
1428 return rc;
1429}
1430
1431#endif /* IN _RING3 */
1432
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