VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/PS2K.cpp@ 55535

Last change on this file since 55535 was 55401, checked in by vboxsync, 10 years ago

added a couple of missing Id headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.9 KB
Line 
1/* $Id: PS2K.cpp 55401 2015-04-23 10:03:17Z vboxsync $ */
2/** @file
3 * PS2K - PS/2 keyboard emulation.
4 */
5
6/*
7 * Copyright (C) 2007-2012 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 */
33
34
35/*******************************************************************************
36* Header Files *
37*******************************************************************************/
38#define LOG_GROUP LOG_GROUP_DEV_KBD
39#include <VBox/vmm/pdmdev.h>
40#include <VBox/err.h>
41#include <iprt/assert.h>
42#include <iprt/uuid.h>
43#include "VBoxDD.h"
44#define IN_PS2K
45#include "PS2Dev.h"
46
47/*******************************************************************************
48* Defined Constants And Macros *
49*******************************************************************************/
50/** @name Keyboard commands sent by the system.
51 * @{ */
52#define KCMD_LEDS 0xED
53#define KCMD_ECHO 0xEE
54#define KCMD_INVALID_1 0xEF
55#define KCMD_SCANSET 0xF0
56#define KCMD_INVALID_2 0xF1
57#define KCMD_READ_ID 0xF2
58#define KCMD_RATE_DELAY 0xF3
59#define KCMD_ENABLE 0xF4
60#define KCMD_DFLT_DISABLE 0xF5
61#define KCMD_SET_DEFAULT 0xF6
62#define KCMD_ALL_TYPEMATIC 0xF7
63#define KCMD_ALL_MK_BRK 0xF8
64#define KCMD_ALL_MAKE 0xF9
65#define KCMD_ALL_TMB 0xFA
66#define KCMD_TYPE_MATIC 0xFB
67#define KCMD_TYPE_MK_BRK 0xFC
68#define KCMD_TYPE_MAKE 0xFD
69#define KCMD_RESEND 0xFE
70#define KCMD_RESET 0xFF
71/** @} */
72
73/** @name Keyboard responses sent to the system.
74 * @{ */
75#define KRSP_ID1 0xAB
76#define KRSP_ID2 0x83
77#define KRSP_BAT_OK 0xAA
78#define KRSP_BAT_FAIL 0xFC /* Also a 'release keys' signal. */
79#define KRSP_ECHO 0xEE
80#define KRSP_ACK 0xFA
81#define KRSP_RESEND 0xFE
82/** @} */
83
84/** @name HID modifier range.
85 * @{ */
86#define HID_MODIFIER_FIRST 0xE0
87#define HID_MODIFIER_LAST 0xE8
88/** @} */
89
90/** @name USB HID additional constants
91 * @{ */
92/** The highest USB usage code reported by VirtualBox. */
93#define VBOX_USB_MAX_USAGE_CODE 0xE7
94/** The size of an array needed to store all USB usage codes */
95#define VBOX_USB_USAGE_ARRAY_SIZE (VBOX_USB_MAX_USAGE_CODE + 1)
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/** Define a simple PS/2 input device queue. */
114#define DEF_PS2Q_TYPE(name, size) \
115 typedef struct { \
116 uint32_t rpos; \
117 uint32_t wpos; \
118 uint32_t cUsed; \
119 uint32_t cSize; \
120 uint8_t abQueue[size]; \
121 } name
122
123/* Internal keyboard queue sizes. The input queue doesn't need to be
124 * extra huge and the command queue only needs to handle a few bytes.
125 */
126#define KBD_KEY_QUEUE_SIZE 64
127#define KBD_CMD_QUEUE_SIZE 4
128
129/*******************************************************************************
130* Structures and Typedefs *
131*******************************************************************************/
132
133/** Scancode translator state. */
134typedef enum {
135 SS_IDLE, /**< Starting state. */
136 SS_EXT, /**< E0 byte was received. */
137 SS_EXT1 /**< E1 byte was received. */
138} scan_state_t;
139
140/** Typematic state. */
141typedef enum {
142 KBD_TMS_IDLE = 0, /* No typematic key active. */
143 KBD_TMS_DELAY = 1, /* In the initial delay period. */
144 KBD_TMS_REPEAT = 2, /* Key repeating at set rate. */
145 KBD_TMS_32BIT_HACK = 0x7fffffff
146} tmatic_state_t;
147
148
149DEF_PS2Q_TYPE(KbdKeyQ, KBD_KEY_QUEUE_SIZE);
150DEF_PS2Q_TYPE(KbdCmdQ, KBD_CMD_QUEUE_SIZE);
151DEF_PS2Q_TYPE(GeneriQ, 1);
152
153/**
154 * The PS/2 keyboard instance data.
155 */
156typedef struct PS2K
157{
158 /** Pointer to parent device (keyboard controller). */
159 R3PTRTYPE(void *) pParent;
160 /** Set if keyboard is enabled ('scans' for input). */
161 bool fScanning;
162 /** Set NumLock is on. */
163 bool fNumLockOn;
164 /** Selected scan set. */
165 uint8_t u8ScanSet;
166 /** Modifier key state. */
167 uint8_t u8Modifiers;
168 /** Currently processed command (if any). */
169 uint8_t u8CurrCmd;
170 /** Status indicator (LED) state. */
171 uint8_t u8LEDs;
172 /** Selected typematic delay/rate. */
173 uint8_t u8Typematic;
174 /** Usage code of current typematic key, if any. */
175 uint8_t u8TypematicKey;
176 /** Current typematic repeat state. */
177 tmatic_state_t enmTypematicState;
178 /** Buffer holding scan codes to be sent to the host. */
179 KbdKeyQ keyQ;
180 /** Command response queue (priority). */
181 KbdCmdQ cmdQ;
182 /** Currently depressed keys. */
183 uint8_t abDepressedKeys[VBOX_USB_USAGE_ARRAY_SIZE];
184 /** Typematic delay in milliseconds. */
185 unsigned uTypematicDelay;
186 /** Typematic repeat period in milliseconds. */
187 unsigned uTypematicRepeat;
188#if HC_ARCH_BITS == 32
189 uint32_t Alignment0;
190#endif
191
192 /** Command delay timer - RC Ptr. */
193 PTMTIMERRC pKbdDelayTimerRC;
194 /** Typematic timer - RC Ptr. */
195 PTMTIMERRC pKbdTypematicTimerRC;
196
197 /** The device critical section protecting everything - R3 Ptr */
198 R3PTRTYPE(PPDMCRITSECT) pCritSectR3;
199 /** Command delay timer - R3 Ptr. */
200 PTMTIMERR3 pKbdDelayTimerR3;
201 /** Typematic timer - R3 Ptr. */
202 PTMTIMERR3 pKbdTypematicTimerR3;
203 RTR3PTR Alignment2;
204
205 /** Command delay timer - R0 Ptr. */
206 PTMTIMERR0 pKbdDelayTimerR0;
207 /** Typematic timer - R0 Ptr. */
208 PTMTIMERR0 pKbdTypematicTimerR0;
209
210 scan_state_t XlatState; ///@todo: temporary
211 uint32_t Alignment1;
212
213 /**
214 * Keyboard port - LUN#0.
215 *
216 * @implements PDMIBASE
217 * @implements PDMIKEYBOARDPORT
218 */
219 struct
220 {
221 /** The base interface for the keyboard port. */
222 PDMIBASE IBase;
223 /** The keyboard port base interface. */
224 PDMIKEYBOARDPORT IPort;
225
226 /** The base interface of the attached keyboard driver. */
227 R3PTRTYPE(PPDMIBASE) pDrvBase;
228 /** The keyboard interface of the attached keyboard driver. */
229 R3PTRTYPE(PPDMIKEYBOARDCONNECTOR) pDrv;
230 } Keyboard;
231} PS2K, *PPS2K;
232
233AssertCompile(PS2K_STRUCT_FILLER >= sizeof(PS2K));
234
235#ifndef VBOX_DEVICE_STRUCT_TESTCASE
236
237/* Key type flags. */
238#define KF_E0 0x01 /* E0 prefix. */
239#define KF_NB 0x02 /* No break code. */
240#define KF_GK 0x04 /* Gray navigation key. */
241#define KF_PS 0x08 /* Print Screen key. */
242#define KF_PB 0x10 /* Pause/Break key. */
243#define KF_NL 0x20 /* Num Lock key. */
244#define KF_NS 0x40 /* NumPad '/' key. */
245
246/* Scan Set 3 typematic defaults. */
247#define T_U 0x00 /* Unknown value. */
248#define T_T 0x01 /* Key is typematic. */
249#define T_M 0x02 /* Key is make only. */
250#define T_B 0x04 /* Key is make/break. */
251
252/* Special key values. */
253#define NONE 0x93 /* No PS/2 scan code returned. */
254#define UNAS 0x94 /* No PS/2 scan assigned to key. */
255#define RSVD 0x95 /* Reserved, do not use. */
256#define UNKN 0x96 /* Translation unknown. */
257
258/* Key definition structure. */
259typedef struct {
260 uint8_t makeS1; /* Set 1 make code. */
261 uint8_t makeS2; /* Set 2 make code. */
262 uint8_t makeS3; /* Set 3 make code. */
263 uint8_t keyFlags; /* Key flags. */
264 uint8_t keyMatic; /* Set 3 typematic default. */
265} key_def;
266
267/* USB to PS/2 conversion table for regular keys. */
268static const key_def aPS2Keys[] = {
269 /* 00 */ {NONE, NONE, NONE, KF_NB, T_U }, /* Key N/A: No Event */
270 /* 01 */ {0xFF, 0x00, 0x00, KF_NB, T_U }, /* Key N/A: Overrun Error */
271 /* 02 */ {0xFC, 0xFC, 0xFC, KF_NB, T_U }, /* Key N/A: POST Fail */
272 /* 03 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key N/A: ErrorUndefined */
273 /* 04 */ {0x1E, 0x1C, 0x1C, 0, T_T }, /* Key 31: a A */
274 /* 05 */ {0x30, 0x32, 0x32, 0, T_T }, /* Key 50: b B */
275 /* 06 */ {0x2E, 0x21, 0x21, 0, T_T }, /* Key 48: c C */
276 /* 07 */ {0x20, 0x23, 0x23, 0, T_T }, /* Key 33: d D */
277 /* 08 */ {0x12, 0x24, 0x24, 0, T_T }, /* Key 19: e E */
278 /* 09 */ {0x21, 0x2B, 0x2B, 0, T_T }, /* Key 34: f F */
279 /* 0A */ {0x22, 0x34, 0x34, 0, T_T }, /* Key 35: g G */
280 /* 0B */ {0x23, 0x33, 0x33, 0, T_T }, /* Key 36: h H */
281 /* 0C */ {0x17, 0x43, 0x43, 0, T_T }, /* Key 24: i I */
282 /* 0D */ {0x24, 0x3B, 0x3B, 0, T_T }, /* Key 37: j J */
283 /* 0E */ {0x25, 0x42, 0x42, 0, T_T }, /* Key 38: k K */
284 /* 0F */ {0x26, 0x4B, 0x4B, 0, T_T }, /* Key 39: l L */
285 /* 10 */ {0x32, 0x3A, 0x3A, 0, T_T }, /* Key 52: m M */
286 /* 11 */ {0x31, 0x31, 0x31, 0, T_T }, /* Key 51: n N */
287 /* 12 */ {0x18, 0x44, 0x44, 0, T_T }, /* Key 25: o O */
288 /* 13 */ {0x19, 0x4D, 0x4D, 0, T_T }, /* Key 26: p P */
289 /* 14 */ {0x10, 0x15, 0x15, 0, T_T }, /* Key 17: q Q */
290 /* 15 */ {0x13, 0x2D, 0x2D, 0, T_T }, /* Key 20: r R */
291 /* 16 */ {0x1F, 0x1B, 0x1B, 0, T_T }, /* Key 32: s S */
292 /* 17 */ {0x14, 0x2C, 0x2C, 0, T_T }, /* Key 21: t T */
293 /* 18 */ {0x16, 0x3C, 0x3C, 0, T_T }, /* Key 23: u U */
294 /* 19 */ {0x2F, 0x2A, 0x2A, 0, T_T }, /* Key 49: v V */
295 /* 1A */ {0x11, 0x1D, 0x1D, 0, T_T }, /* Key 18: w W */
296 /* 1B */ {0x2D, 0x22, 0x22, 0, T_T }, /* Key 47: x X */
297 /* 1C */ {0x15, 0x35, 0x35, 0, T_T }, /* Key 22: y Y */
298 /* 1D */ {0x2C, 0x1A, 0x1A, 0, T_T }, /* Key 46: z Z */
299 /* 1E */ {0x02, 0x16, 0x16, 0, T_T }, /* Key 2: 1 ! */
300 /* 1F */ {0x03, 0x1E, 0x1E, 0, T_T }, /* Key 3: 2 @ */
301 /* 20 */ {0x04, 0x26, 0x26, 0, T_T }, /* Key 4: 3 # */
302 /* 21 */ {0x05, 0x25, 0x25, 0, T_T }, /* Key 5: 4 $ */
303 /* 22 */ {0x06, 0x2E, 0x2E, 0, T_T }, /* Key 6: 5 % */
304 /* 23 */ {0x07, 0x36, 0x36, 0, T_T }, /* Key 7: 6 ^ */
305 /* 24 */ {0x08, 0x3D, 0x3D, 0, T_T }, /* Key 8: 7 & */
306 /* 25 */ {0x09, 0x3E, 0x3E, 0, T_T }, /* Key 9: 8 * */
307 /* 26 */ {0x0A, 0x46, 0x46, 0, T_T }, /* Key 10: 9 ( */
308 /* 27 */ {0x0B, 0x45, 0x45, 0, T_T }, /* Key 11: 0 ) */
309 /* 28 */ {0x1C, 0x5A, 0x5A, 0, T_T }, /* Key 43: Return */
310 /* 29 */ {0x01, 0x76, 0x08, 0, T_M }, /* Key 110: Escape */
311 /* 2A */ {0x0E, 0x66, 0x66, 0, T_T }, /* Key 15: Backspace */
312 /* 2B */ {0x0F, 0x0D, 0x0D, 0, T_T }, /* Key 16: Tab */
313 /* 2C */ {0x39, 0x29, 0x29, 0, T_T }, /* Key 61: Space */
314 /* 2D */ {0x0C, 0x4E, 0x4E, 0, T_T }, /* Key 12: - _ */
315 /* 2E */ {0x0D, 0x55, 0x55, 0, T_T }, /* Key 13: = + */
316 /* 2F */ {0x1A, 0x54, 0x54, 0, T_T }, /* Key 27: [ { */
317 /* 30 */ {0x1B, 0x5B, 0x5B, 0, T_T }, /* Key 28: ] } */
318 /* 31 */ {0x2B, 0x5D, 0x5C, 0, T_T }, /* Key 29: \ | */
319 /* 32 */ {0x2B, 0x5D, 0x5D, 0, T_T }, /* Key 42: Europe 1 (Note 2) */
320 /* 33 */ {0x27, 0x4C, 0x4C, 0, T_T }, /* Key 40: ; : */
321 /* 34 */ {0x28, 0x52, 0x52, 0, T_T }, /* Key 41: ' " */
322 /* 35 */ {0x29, 0x0E, 0x0E, 0, T_T }, /* Key 1: ` ~ */
323 /* 36 */ {0x33, 0x41, 0x41, 0, T_T }, /* Key 53: , < */
324 /* 37 */ {0x34, 0x49, 0x49, 0, T_T }, /* Key 54: . > */
325 /* 38 */ {0x35, 0x4A, 0x4A, 0, T_T }, /* Key 55: / ? */
326 /* 39 */ {0x3A, 0x58, 0x14, 0, T_B }, /* Key 30: Caps Lock */
327 /* 3A */ {0x3B, 0x05, 0x07, 0, T_M }, /* Key 112: F1 */
328 /* 3B */ {0x3C, 0x06, 0x0F, 0, T_M }, /* Key 113: F2 */
329 /* 3C */ {0x3D, 0x04, 0x17, 0, T_M }, /* Key 114: F3 */
330 /* 3D */ {0x3E, 0x0C, 0x1F, 0, T_M }, /* Key 115: F4 */
331 /* 3E */ {0x3F, 0x03, 0x27, 0, T_M }, /* Key 116: F5 */
332 /* 3F */ {0x40, 0x0B, 0x2F, 0, T_M }, /* Key 117: F6 */
333 /* 40 */ {0x41, 0x83, 0x37, 0, T_M }, /* Key 118: F7 */
334 /* 41 */ {0x42, 0x0A, 0x3F, 0, T_M }, /* Key 119: F8 */
335 /* 42 */ {0x43, 0x01, 0x47, 0, T_M }, /* Key 120: F9 */
336 /* 43 */ {0x44, 0x09, 0x4F, 0, T_M }, /* Key 121: F10 */
337 /* 44 */ {0x57, 0x78, 0x56, 0, T_M }, /* Key 122: F11 */
338 /* 45 */ {0x58, 0x07, 0x5E, 0, T_M }, /* Key 123: F12 */
339 /* 46 */ {0x37, 0x7C, 0x57, KF_PS, T_M }, /* Key 124: Print Screen (Note 1) */
340 /* 47 */ {0x46, 0x7E, 0x5F, 0, T_M }, /* Key 125: Scroll Lock */
341 /* 48 */ {RSVD, RSVD, RSVD, KF_PB, T_M }, /* Key 126: Break (Ctrl-Pause) */
342 /* 49 */ {0x52, 0x70, 0x67, KF_GK, T_M }, /* Key 75: Insert (Note 1) */
343 /* 4A */ {0x47, 0x6C, 0x6E, KF_GK, T_M }, /* Key 80: Home (Note 1) */
344 /* 4B */ {0x49, 0x7D, 0x6F, KF_GK, T_M }, /* Key 85: Page Up (Note 1) */
345 /* 4C */ {0x53, 0x71, 0x64, KF_GK, T_T }, /* Key 76: Delete (Note 1) */
346 /* 4D */ {0x4F, 0x69, 0x65, KF_GK, T_M }, /* Key 81: End (Note 1) */
347 /* 4E */ {0x51, 0x7A, 0x6D, KF_GK, T_M }, /* Key 86: Page Down (Note 1) */
348 /* 4F */ {0x4D, 0x74, 0x6A, KF_GK, T_T }, /* Key 89: Right Arrow (Note 1) */
349 /* 50 */ {0x4B, 0x6B, 0x61, KF_GK, T_T }, /* Key 79: Left Arrow (Note 1) */
350 /* 51 */ {0x50, 0x72, 0x60, KF_GK, T_T }, /* Key 84: Down Arrow (Note 1) */
351 /* 52 */ {0x48, 0x75, 0x63, KF_GK, T_T }, /* Key 83: Up Arrow (Note 1) */
352 /* 53 */ {0x45, 0x77, 0x76, KF_NL, T_M }, /* Key 90: Num Lock */
353 /* 54 */ {0x35, 0x4A, 0x77, KF_NS, T_M }, /* Key 95: Keypad / (Note 1) */
354 /* 55 */ {0x37, 0x7C, 0x7E, 0, T_M }, /* Key 100: Keypad * */
355 /* 56 */ {0x4A, 0x7B, 0x84, 0, T_M }, /* Key 105: Keypad - */
356 /* 57 */ {0x4E, 0x79, 0x7C, 0, T_T }, /* Key 106: Keypad + */
357 /* 58 */ {0x1C, 0x5A, 0x79, KF_E0, T_M }, /* Key 108: Keypad Enter */
358 /* 59 */ {0x4F, 0x69, 0x69, 0, T_M }, /* Key 93: Keypad 1 End */
359 /* 5A */ {0x50, 0x72, 0x72, 0, T_M }, /* Key 98: Keypad 2 Down */
360 /* 5B */ {0x51, 0x7A, 0x7A, 0, T_M }, /* Key 103: Keypad 3 PageDn */
361 /* 5C */ {0x4B, 0x6B, 0x6B, 0, T_M }, /* Key 92: Keypad 4 Left */
362 /* 5D */ {0x4C, 0x73, 0x73, 0, T_M }, /* Key 97: Keypad 5 */
363 /* 5E */ {0x4D, 0x74, 0x74, 0, T_M }, /* Key 102: Keypad 6 Right */
364 /* 5F */ {0x47, 0x6C, 0x6C, 0, T_M }, /* Key 91: Keypad 7 Home */
365 /* 60 */ {0x48, 0x75, 0x75, 0, T_M }, /* Key 96: Keypad 8 Up */
366 /* 61 */ {0x49, 0x7D, 0x7D, 0, T_M }, /* Key 101: Keypad 9 PageUp */
367 /* 62 */ {0x52, 0x70, 0x70, 0, T_M }, /* Key 99: Keypad 0 Insert */
368 /* 63 */ {0x53, 0x71, 0x71, 0, T_M }, /* Key 104: Keypad . Delete */
369 /* 64 */ {0x56, 0x61, 0x13, 0, T_T }, /* Key 45: Europe 2 (Note 2) */
370 /* 65 */ {0x5D, 0x2F, UNKN, KF_E0, T_U }, /* Key 129: App */
371 /* 66 */ {0x5E, 0x37, UNKN, KF_E0, T_U }, /* Key Unk: Keyboard Power */
372 /* 67 */ {0x59, 0x0F, UNKN, 0, T_U }, /* Key Unk: Keypad = */
373 /* 68 */ {0x64, 0x08, UNKN, 0, T_U }, /* Key Unk: F13 */
374 /* 69 */ {0x65, 0x10, UNKN, 0, T_U }, /* Key Unk: F14 */
375 /* 6A */ {0x66, 0x18, UNKN, 0, T_U }, /* Key Unk: F15 */
376 /* 6B */ {0x67, 0x20, UNKN, 0, T_U }, /* Key Unk: F16 */
377 /* 6C */ {0x68, 0x28, UNKN, 0, T_U }, /* Key Unk: F17 */
378 /* 6D */ {0x69, 0x30, UNKN, 0, T_U }, /* Key Unk: F18 */
379 /* 6E */ {0x6A, 0x38, UNKN, 0, T_U }, /* Key Unk: F19 */
380 /* 6F */ {0x6B, 0x40, UNKN, 0, T_U }, /* Key Unk: F20 */
381 /* 70 */ {0x6C, 0x48, UNKN, 0, T_U }, /* Key Unk: F21 */
382 /* 71 */ {0x6D, 0x50, UNKN, 0, T_U }, /* Key Unk: F22 */
383 /* 72 */ {0x6E, 0x57, UNKN, 0, T_U }, /* Key Unk: F23 */
384 /* 73 */ {0x76, 0x5F, UNKN, 0, T_U }, /* Key Unk: F24 */
385 /* 74 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Execute */
386 /* 75 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Help */
387 /* 76 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Menu */
388 /* 77 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Select */
389 /* 78 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Stop */
390 /* 79 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Again */
391 /* 7A */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Undo */
392 /* 7B */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Cut */
393 /* 7C */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Copy */
394 /* 7D */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Paste */
395 /* 7E */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Find */
396 /* 7F */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Mute */
397 /* 80 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Volume Up */
398 /* 81 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Volume Dn */
399 /* 82 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Locking Caps Lock */
400 /* 83 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Locking Num Lock */
401 /* 84 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Locking Scroll Lock */
402 /* 85 */ {0x7E, 0x6D, UNKN, 0, T_U }, /* Key Unk: Keypad , (Brazilian Keypad .) */
403 /* 86 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Equal Sign */
404 /* 87 */ {0x73, 0x51, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 1 (Ro) */
405 /* 88 */ {0x70, 0x13, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl2 (K'kana/H'gana) */
406 /* 89 */ {0x7D, 0x6A, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 2 (Yen) */
407 /* 8A */ {0x79, 0x64, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 4 (Henkan) */
408 /* 8B */ {0x7B, 0x67, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 5 (Muhenkan) */
409 /* 8C */ {0x5C, 0x27, UNKN, 0, T_U }, /* Key Unk: Keyboard Intl 6 (PC9800 Pad ,) */
410 /* 8D */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Intl 7 */
411 /* 8E */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Intl 8 */
412 /* 8F */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Intl 9 */
413 /* 90 */ {0xF2, 0xF2, UNKN, KF_NB, T_U }, /* Key Unk: Keyboard Lang 1 (Hang'l/Engl) */
414 /* 91 */ {0xF1, 0xF1, UNKN, KF_NB, T_U }, /* Key Unk: Keyboard Lang 2 (Hanja) */
415 /* 92 */ {0x78, 0x63, UNKN, 0, T_U }, /* Key Unk: Keyboard Lang 3 (Katakana) */
416 /* 93 */ {0x77, 0x62, UNKN, 0, T_U }, /* Key Unk: Keyboard Lang 4 (Hiragana) */
417 /* 94 */ {0x76, 0x5F, UNKN, 0, T_U }, /* Key Unk: Keyboard Lang 5 (Zen/Han) */
418 /* 95 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Lang 6 */
419 /* 96 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Lang 7 */
420 /* 97 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Lang 8 */
421 /* 98 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Lang 9 */
422 /* 99 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Alternate Erase */
423 /* 9A */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard SysReq/Attention (Note 3) */
424 /* 9B */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Cancel */
425 /* 9C */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Clear */
426 /* 9D */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Prior */
427 /* 9E */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Return */
428 /* 9F */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Separator */
429 /* A0 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Out */
430 /* A1 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Oper */
431 /* A2 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard Clear/Again */
432 /* A3 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard CrSel/Props */
433 /* A4 */ {UNAS, UNAS, UNAS, 0, T_U }, /* Key Unk: Keyboard ExSel */
434};
435
436/*
437 * Note 1: The behavior of these keys depends on the state of modifier keys
438 * at the time the key was pressed.
439 *
440 * Note 2: The key label depends on the national version of the keyboard.
441 *
442 * Note 3: Certain keys which have their own PS/2 scancodes do not exist on
443 * USB keyboards; the SysReq key is an example. The SysReq key scancode needs
444 * to be translated to the Print Screen HID usage code. The HID usage to PS/2
445 * scancode conversion then generates the correct sequence depending on the
446 * keyboard state.
447 */
448
449/* USB to PS/2 conversion table for modifier keys. */
450static const key_def aPS2ModKeys[] = {
451 /* E0 */ {0x1D, 0x14, 0x11, 0, T_B }, /* Key 58: Left Control */
452 /* E1 */ {0x2A, 0x12, 0x12, 0, T_B }, /* Key 44: Left Shift */
453 /* E2 */ {0x38, 0x11, 0x19, 0, T_B }, /* Key 60: Left Alt */
454 /* E3 */ {0x5B, 0x1F, UNKN, KF_E0, T_U }, /* Key 127: Left GUI */
455 /* E4 */ {0x1D, 0x14, 0x58, KF_E0, T_M }, /* Key 64: Right Control */
456 /* E5 */ {0x36, 0x59, 0x59, 0, T_B }, /* Key 57: Right Shift */
457 /* E6 */ {0x38, 0x11, 0x39, KF_E0, T_M }, /* Key 62: Right Alt */
458 /* E7 */ {0x5C, 0x27, UNKN, KF_E0, T_U }, /* Key 128: Right GUI */
459};
460
461/*******************************************************************************
462* Global Variables *
463*******************************************************************************/
464
465/*******************************************************************************
466* Internal Functions *
467*******************************************************************************/
468
469
470/**
471 * Clear a queue.
472 *
473 * @param pQ Pointer to the queue.
474 */
475static void ps2kClearQueue(GeneriQ *pQ)
476{
477 LogFlowFunc(("Clearing queue %p\n", pQ));
478 pQ->wpos = pQ->rpos;
479 pQ->cUsed = 0;
480}
481
482
483/**
484 * Add a byte to a queue.
485 *
486 * @param pQ Pointer to the queue.
487 * @param val The byte to store.
488 */
489static void ps2kInsertQueue(GeneriQ *pQ, uint8_t val)
490{
491 /* Check if queue is full. */
492 if (pQ->cUsed >= pQ->cSize)
493 {
494 LogFlowFunc(("queue %p full (%d entries)\n", pQ, pQ->cUsed));
495 return;
496 }
497 /* Insert data and update circular buffer write position. */
498 pQ->abQueue[pQ->wpos] = val;
499 if (++pQ->wpos == pQ->cSize)
500 pQ->wpos = 0; /* Roll over. */
501 ++pQ->cUsed;
502 LogFlowFunc(("inserted 0x%02X into queue %p\n", val, pQ));
503}
504
505#ifdef IN_RING3
506
507/**
508 * Save a queue state.
509 *
510 * @param pSSM SSM handle to write the state to.
511 * @param pQ Pointer to the queue.
512 */
513static void ps2kSaveQueue(PSSMHANDLE pSSM, GeneriQ *pQ)
514{
515 uint32_t cItems = pQ->cUsed;
516 int i;
517
518 /* Only save the number of items. Note that the read/write
519 * positions aren't saved as they will be rebuilt on load.
520 */
521 SSMR3PutU32(pSSM, cItems);
522
523 LogFlow(("Storing %d items from queue %p\n", cItems, pQ));
524
525 /* Save queue data - only the bytes actually used (typically zero). */
526 for (i = pQ->rpos; cItems-- > 0; i = (i + 1) % pQ->cSize)
527 SSMR3PutU8(pSSM, pQ->abQueue[i]);
528}
529
530/**
531 * Load a queue state.
532 *
533 * @param pSSM SSM handle to read the state from.
534 * @param pQ Pointer to the queue.
535 *
536 * @return int VBox status/error code.
537 */
538static int ps2kLoadQueue(PSSMHANDLE pSSM, GeneriQ *pQ)
539{
540 /* On load, always put the read pointer at zero. */
541 int rc = SSMR3GetU32(pSSM, &pQ->cUsed);
542 AssertRCReturn(rc, rc);
543 LogFlow(("Loading %u items to queue %p\n", pQ->cUsed, pQ));
544 AssertMsgReturn(pQ->cUsed <= pQ->cSize, ("Saved size=%u, actual=%u\n", pQ->cUsed, pQ->cSize),
545 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
546
547 /* Recalculate queue positions and load data in one go. */
548 pQ->rpos = 0;
549 pQ->wpos = pQ->cUsed;
550 rc = SSMR3GetMem(pSSM, pQ->abQueue, pQ->cUsed);
551
552 return rc;
553}
554
555/**
556 * Notify listener about LEDs state change.
557 *
558 * @param pThis The PS/2 keyboard instance data.
559 * @param u8State Bitfield which reflects LEDs state.
560 */
561static void ps2kNotifyLedsState(PPS2K pThis, uint8_t u8State)
562{
563
564 PDMKEYBLEDS enmLeds = PDMKEYBLEDS_NONE;
565
566 if (u8State & 0x01)
567 enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_SCROLLLOCK);
568 if (u8State & 0x02)
569 enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_NUMLOCK);
570 if (u8State & 0x04)
571 enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_CAPSLOCK);
572
573 pThis->Keyboard.pDrv->pfnLedStatusChange(pThis->Keyboard.pDrv, enmLeds);
574
575}
576#endif /* IN_RING3 */
577
578/**
579 * Retrieve a byte from a queue.
580 *
581 * @param pQ Pointer to the queue.
582 * @param pVal Pointer to storage for the byte.
583 *
584 * @return int VINF_TRY_AGAIN if queue is empty,
585 * VINF_SUCCESS if a byte was read.
586 */
587static int ps2kRemoveQueue(GeneriQ *pQ, uint8_t *pVal)
588{
589 int rc = VINF_TRY_AGAIN;
590
591 Assert(pVal);
592 if (pQ->cUsed)
593 {
594 *pVal = pQ->abQueue[pQ->rpos];
595 if (++pQ->rpos == pQ->cSize)
596 pQ->rpos = 0; /* Roll over. */
597 --pQ->cUsed;
598 rc = VINF_SUCCESS;
599 LogFlowFunc(("removed 0x%02X from queue %p\n", *pVal, pQ));
600 } else
601 LogFlowFunc(("queue %p empty\n", pQ));
602 return rc;
603}
604
605/* Convert encoded typematic value to milliseconds. Note that the values are rated
606 * with +/- 20% accuracy, so there's no need for high precision.
607 */
608static void ps2kSetupTypematic(PPS2K pThis, uint8_t val)
609{
610 int A, B;
611 unsigned period;
612
613 pThis->u8Typematic = val;
614 /* The delay is easy: (1 + value) * 250 ms */
615 pThis->uTypematicDelay = (1 + ((val >> 5) & 3)) * 250;
616 /* The rate is more complicated: (8 + A) * 2^B * 4.17 ms */
617 A = val & 7;
618 B = (val >> 3) & 3;
619 period = (8 + A) * (1 << B) * 417 / 100;
620 pThis->uTypematicRepeat = period;
621 Log(("Typematic delay %u ms, repeat period %u ms\n",
622 pThis->uTypematicDelay, pThis->uTypematicRepeat));
623}
624
625static void ps2kSetDefaults(PPS2K pThis)
626{
627 LogFlowFunc(("Set keyboard defaults\n"));
628 ps2kClearQueue((GeneriQ *)&pThis->keyQ);
629 /* Set default Scan Set 3 typematic values. */
630 /* Set default typematic rate/delay. */
631 ps2kSetupTypematic(pThis, KBD_DFL_RATE_DELAY);
632 /* Clear last typematic key?? */
633}
634
635/**
636 * Receive and process a byte sent by the keyboard controller.
637 *
638 * @param pThis The PS/2 keyboard instance data.
639 * @param cmd The command (or data) byte.
640 */
641int PS2KByteToKbd(PPS2K pThis, uint8_t cmd)
642{
643 bool fHandled = true;
644
645 LogFlowFunc(("new cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
646
647 if (pThis->u8CurrCmd == KCMD_RESET)
648 /* In reset mode, do not respond at all. */
649 return VINF_SUCCESS;
650
651 switch (cmd)
652 {
653 case KCMD_ECHO:
654 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ECHO);
655 pThis->u8CurrCmd = 0;
656 break;
657 case KCMD_READ_ID:
658 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
659 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ID1);
660 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ID2);
661 pThis->u8CurrCmd = 0;
662 break;
663 case KCMD_ENABLE:
664 pThis->fScanning = true;
665 ps2kClearQueue((GeneriQ *)&pThis->keyQ);
666 /* Clear last typematic key?? */
667 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
668 pThis->u8CurrCmd = 0;
669 break;
670 case KCMD_DFLT_DISABLE:
671 pThis->fScanning = false;
672 ps2kSetDefaults(pThis);
673 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
674 pThis->u8CurrCmd = 0;
675 break;
676 case KCMD_SET_DEFAULT:
677 ps2kSetDefaults(pThis);
678 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
679 pThis->u8CurrCmd = 0;
680 break;
681 case KCMD_ALL_TYPEMATIC:
682 case KCMD_ALL_MK_BRK:
683 case KCMD_ALL_MAKE:
684 case KCMD_ALL_TMB:
685 ///@todo Set the key types here.
686 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
687 pThis->u8CurrCmd = 0;
688 break;
689 case KCMD_RESEND:
690 pThis->u8CurrCmd = 0;
691 break;
692 case KCMD_RESET:
693 pThis->u8ScanSet = 2;
694 ps2kSetDefaults(pThis);
695 ///@todo reset more?
696 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
697 pThis->u8CurrCmd = cmd;
698 /* Delay BAT completion; the test may take hundreds of ms. */
699 TMTimerSetMillies(pThis->CTX_SUFF(pKbdDelayTimer), 2);
700 break;
701 /* The following commands need a parameter. */
702 case KCMD_LEDS:
703 case KCMD_SCANSET:
704 case KCMD_RATE_DELAY:
705 case KCMD_TYPE_MATIC:
706 case KCMD_TYPE_MK_BRK:
707 case KCMD_TYPE_MAKE:
708 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
709 pThis->u8CurrCmd = cmd;
710 break;
711 default:
712 /* Sending a command instead of a parameter starts the new command. */
713 switch (pThis->u8CurrCmd)
714 {
715 case KCMD_LEDS:
716#ifndef IN_RING3
717 return VINF_IOM_R3_IOPORT_WRITE;
718#else
719 {
720 ps2kNotifyLedsState(pThis, cmd);
721 pThis->fNumLockOn = !!(cmd & 0x02); /* Sync internal Num Lock state. */
722 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
723 pThis->u8LEDs = cmd;
724 pThis->u8CurrCmd = 0;
725 }
726#endif
727 break;
728 case KCMD_SCANSET:
729 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
730 if (cmd == 0)
731 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8ScanSet);
732 else if (cmd < 4)
733 {
734 pThis->u8ScanSet = cmd;
735 LogRel(("PS2K: Selected scan set %d.\n", cmd));
736 }
737 /* Other values are simply ignored. */
738 pThis->u8CurrCmd = 0;
739 break;
740 case KCMD_RATE_DELAY:
741 ps2kSetupTypematic(pThis, cmd);
742 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_ACK);
743 pThis->u8CurrCmd = 0;
744 break;
745 default:
746 fHandled = false;
747 }
748 /* Fall through only to handle unrecognized commands. */
749 if (fHandled)
750 break;
751
752 case KCMD_INVALID_1:
753 case KCMD_INVALID_2:
754 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_RESEND);
755 pThis->u8CurrCmd = 0;
756 break;
757 }
758 LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
759// KBCUpdateInterrupts(pThis->pParent);
760 return VINF_SUCCESS;
761}
762
763/**
764 * Send a byte (keystroke or command response) to the keyboard controller.
765 *
766 * @returns VINF_SUCCESS or VINF_TRY_AGAIN.
767 * @param pThis The PS/2 keyboard instance data.
768 * @param pb Where to return the byte we've read.
769 * @remarks Caller must have entered the device critical section.
770 */
771int PS2KByteFromKbd(PPS2K pThis, uint8_t *pb)
772{
773 int rc;
774
775 AssertPtr(pb);
776
777 /* Anything in the command queue has priority over data
778 * in the keystroke queue. Additionally, keystrokes are
779 * blocked if a command is currently in progress, even if
780 * the command queue is empty.
781 */
782 rc = ps2kRemoveQueue((GeneriQ *)&pThis->cmdQ, pb);
783 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && pThis->fScanning)
784 rc = ps2kRemoveQueue((GeneriQ *)&pThis->keyQ, pb);
785
786 LogFlowFunc(("keyboard sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
787 return rc;
788}
789
790#ifdef IN_RING3
791
792static int ps2kProcessKeyEvent(PPS2K pThis, uint8_t u8HidCode, bool fKeyDown)
793{
794 unsigned int i = 0;
795 key_def const *pKeyDef;
796 uint8_t abCodes[16];
797 uint8_t u8MakeCode;
798
799 LogFlowFunc(("key %s: 0x%02x (set %d)\n", fKeyDown ? "down" : "up", u8HidCode, pThis->u8ScanSet));
800
801 /* Find the key definition in somewhat sparse storage. */
802 pKeyDef = u8HidCode >= HID_MODIFIER_FIRST ? &aPS2ModKeys[u8HidCode - HID_MODIFIER_FIRST] : &aPS2Keys[u8HidCode];
803
804 /* Some keys are not processed at all; early return. */
805 if (pKeyDef->makeS1 == NONE)
806 {
807 LogFlow(("Skipping key processing.\n"));
808 return VINF_SUCCESS;
809 }
810
811 /* Handle modifier keys (Ctrl/Alt/Shift/GUI). We need to keep track
812 * of their state in addition to sending the scan code.
813 */
814 if (u8HidCode >= HID_MODIFIER_FIRST)
815 {
816 unsigned mod_bit = 1 << (u8HidCode - HID_MODIFIER_FIRST);
817
818 Assert((u8HidCode <= HID_MODIFIER_LAST));
819 if (fKeyDown)
820 pThis->u8Modifiers |= mod_bit;
821 else
822 pThis->u8Modifiers &= ~mod_bit;
823 }
824
825 /* Toggle NumLock state. */
826 if ((pKeyDef->keyFlags & KF_NL) && fKeyDown)
827 pThis->fNumLockOn ^= true;
828
829 if (pThis->u8ScanSet == 1 || pThis->u8ScanSet == 2)
830 {
831 /* The basic scan set 1 and 2 logic is the same, only the scan codes differ.
832 * Since scan set 2 is used almost all the time, that case is handled first.
833 */
834 abCodes[0] = 0;
835 if (fKeyDown)
836 {
837 /* Process key down event. */
838 if (pKeyDef->keyFlags & KF_PB)
839 {
840 /* Pause/Break sends different data if either Ctrl is held. */
841 if (pThis->u8Modifiers & (MOD_LCTRL | MOD_RCTRL))
842 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
843 "\xE0\x7E\xE0\xF0\x7E" : "\xE0\x46\xE0\xC6");
844 else
845 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
846 "\xE1\x14\x77\xE1\xF0\x14\xF0\x77" : "\xE1\x1D\x45\xE1\x9D\xC5");
847 }
848 else if (pKeyDef->keyFlags & KF_PS)
849 {
850 /* Print Screen depends on all of Ctrl, Shift, *and* Alt! */
851 if (pThis->u8Modifiers & (MOD_LALT | MOD_RALT))
852 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
853 "\x84" : "\x54");
854 else if (pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT))
855 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
856 "\xE0\x7C" : "\xE0\x37");
857 else
858 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
859 "\xE0\x12\xE0\x7C" : "\xE0\x2A\xE0\x37");
860 }
861 else if (pKeyDef->keyFlags & (KF_GK | KF_NS))
862 {
863 /* The numeric pad keys fake Shift presses or releases
864 * depending on Num Lock and Shift key state. The '/'
865 * key behaves in a similar manner but does not depend on
866 * the Num Lock state.
867 */
868 if (!pThis->fNumLockOn || (pKeyDef->keyFlags & KF_NS))
869 {
870 if (pThis->u8Modifiers & MOD_LSHIFT)
871 strcat((char *)abCodes, pThis->u8ScanSet == 2 ?
872 "\xE0\xF0\x12" : "\xE0\xAA");
873 if (pThis->u8Modifiers & MOD_RSHIFT)
874 strcat((char *)abCodes, pThis->u8ScanSet == 2 ?
875 "\xE0\xF0\x59" : "\xE0\xB6");
876 }
877 else
878 {
879 Assert(pThis->fNumLockOn); /* Not for KF_NS! */
880 if ((pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT)) == 0)
881 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
882 "\xE0\x12" : "\xE0\x2A");
883 /* Else Shift cancels NumLock, so no prefix! */
884 }
885 }
886 /* Feed the bytes to the queue if there is room. */
887 ///@todo check empty space!
888 while (abCodes[i])
889 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, abCodes[i++]);
890 Assert(i < sizeof(abCodes));
891
892 /* Standard processing for regular keys only. */
893 u8MakeCode = pThis->u8ScanSet == 2 ? pKeyDef->makeS2 : pKeyDef->makeS1;
894 if (!(pKeyDef->keyFlags & (KF_PB | KF_PS)))
895 {
896 if (pKeyDef->keyFlags & (KF_E0 | KF_GK | KF_NS))
897 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, 0xE0);
898 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, u8MakeCode);
899 }
900 }
901 else if (!(pKeyDef->keyFlags & (KF_NB | KF_PB)))
902 {
903 /* Process key up event except for keys which produce none. */
904
905 /* Handle Print Screen release. */
906 if (pKeyDef->keyFlags & KF_PS)
907 {
908 /* Undo faked Print Screen state as needed. */
909 if (pThis->u8Modifiers & (MOD_LALT | MOD_RALT))
910 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
911 "\xF0\x84" : "\xD4");
912 else if (pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT))
913 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
914 "\xE0\xF0\x7C" : "\xE0\xB7");
915 else
916 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
917 "\xE0\xF0\x7C\xE0\xF0\x12" : "\xE0\xB7\xE0\xAA");
918 }
919 else
920 {
921 /* Process base scan code for less unusual keys. */
922 if (pKeyDef->keyFlags & (KF_E0 | KF_GK | KF_NS))
923 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, 0xE0);
924 if (pThis->u8ScanSet == 2) {
925 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, 0xF0);
926 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, pKeyDef->makeS2);
927 } else {
928 Assert(pThis->u8ScanSet == 1);
929 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, pKeyDef->makeS1 | 0x80);
930 }
931
932 /* Restore shift state for gray keys. */
933 if (pKeyDef->keyFlags & (KF_GK | KF_NS))
934 {
935 if (!pThis->fNumLockOn || (pKeyDef->keyFlags & KF_NS))
936 {
937 if (pThis->u8Modifiers & MOD_LSHIFT)
938 strcat((char *)abCodes, pThis->u8ScanSet == 2 ?
939 "\xE0\x12" : "\xE0\x2A");
940 if (pThis->u8Modifiers & MOD_RSHIFT)
941 strcat((char *)abCodes, pThis->u8ScanSet == 2 ?
942 "\xE0\x59" : "\xE0\x36");
943 }
944 else
945 {
946 Assert(pThis->fNumLockOn); /* Not for KF_NS! */
947 if ((pThis->u8Modifiers & (MOD_LSHIFT | MOD_RSHIFT)) == 0)
948 strcpy((char *)abCodes, pThis->u8ScanSet == 2 ?
949 "\xE0\xF0\x12" : "\xE0\xAA");
950 }
951 }
952 }
953
954 /* Feed any additional bytes to the queue if there is room. */
955 ///@todo check empty space!
956 while (abCodes[i])
957 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, abCodes[i++]);
958 Assert(i < sizeof(abCodes));
959 }
960 }
961 else
962 {
963 /* Handle Scan Set 3 - very straightforward. */
964 Assert(pThis->u8ScanSet == 3);
965 if (fKeyDown)
966 {
967 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, pKeyDef->makeS3);
968 }
969 else
970 {
971 /* Send a key release code unless it's a make only key. */
972 ///@todo Look up the current typematic setting, not the default!
973 if (pKeyDef->keyMatic != T_M)
974 {
975 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, 0xF0);
976 ps2kInsertQueue((GeneriQ *)&pThis->keyQ, pKeyDef->makeS3);
977 }
978 }
979 }
980
981 /* Set up or cancel typematic key repeat. */
982 if (fKeyDown)
983 {
984 if (pThis->u8TypematicKey != u8HidCode)
985 {
986 pThis->enmTypematicState = KBD_TMS_DELAY;
987 pThis->u8TypematicKey = u8HidCode;
988 TMTimerSetMillies(pThis->CTX_SUFF(pKbdTypematicTimer), pThis->uTypematicDelay);
989 Log(("Typematic delay %u ms, key %02X\n", pThis->uTypematicDelay, u8HidCode));
990 }
991 }
992 else
993 {
994 pThis->u8TypematicKey = 0;
995 pThis->enmTypematicState = KBD_TMS_IDLE;
996 ///@todo Cancel timer right away?
997 ///@todo Cancel timer before pushing key up code!?
998 }
999
1000 /* Poke the KBC to update its state. */
1001 KBCUpdateInterrupts(pThis->pParent);
1002
1003 return VINF_SUCCESS;
1004}
1005
1006/* Timer handler for emulating typematic keys. Note that only the last key
1007 * held down repeats (if typematic).
1008 */
1009static DECLCALLBACK(void) ps2kTypematicTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1010{
1011 PPS2K pThis = (PS2K *)pvUser; NOREF(pDevIns);
1012 LogFlowFunc(("Typematic state=%d, key %02X\n", pThis->enmTypematicState, pThis->u8TypematicKey));
1013
1014 /* If the current typematic key is zero, the repeat was canceled just when
1015 * the timer was about to run. In that case, do nothing.
1016 */
1017 if (pThis->u8TypematicKey)
1018 {
1019 if (pThis->enmTypematicState == KBD_TMS_DELAY)
1020 pThis->enmTypematicState = KBD_TMS_REPEAT;
1021
1022 if (pThis->enmTypematicState == KBD_TMS_REPEAT)
1023 {
1024 ps2kProcessKeyEvent(pThis, pThis->u8TypematicKey, true /* Key down */ );
1025 TMTimerSetMillies(pThis->CTX_SUFF(pKbdTypematicTimer), pThis->uTypematicRepeat);
1026 }
1027 }
1028}
1029
1030/* The keyboard BAT is specified to take several hundred milliseconds. We need
1031 * to delay sending the result to the host for at least a tiny little while.
1032 */
1033static DECLCALLBACK(void) ps2kDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1034{
1035 PPS2K pThis = (PS2K *)pvUser; NOREF(pDevIns);
1036
1037 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
1038
1039 AssertMsg(pThis->u8CurrCmd == KCMD_RESET, ("u8CurrCmd=%02x\n", pThis->u8CurrCmd));
1040 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, KRSP_BAT_OK);
1041 pThis->fScanning = true; /* BAT completion enables scanning! */
1042 pThis->u8CurrCmd = 0;
1043
1044 ///@todo Might want a PS2KCompleteCommand() to push last response, clear command, and kick the KBC...
1045 /* Give the KBC a kick. */
1046 KBCUpdateInterrupts(pThis->pParent);
1047}
1048
1049/* Release any and all currently depressed keys. Used whenever the guest keyboard
1050 * is likely to be out of sync with the host, such as when loading a saved state
1051 * or resuming a suspended host.
1052 */
1053static void ps2kReleaseKeys(PPS2K pThis)
1054{
1055 LogFlowFunc(("Releasing keys...\n"));
1056
1057 for (unsigned uKey = 0; uKey < sizeof(pThis->abDepressedKeys); ++uKey)
1058 if (pThis->abDepressedKeys[uKey])
1059 {
1060 ps2kProcessKeyEvent(pThis, uKey, false /* key up */);
1061 pThis->abDepressedKeys[uKey] = 0;
1062 }
1063 LogFlowFunc(("Done releasing keys\n"));
1064}
1065
1066
1067/**
1068 * Debug device info handler. Prints basic keyboard state.
1069 *
1070 * @param pDevIns Device instance which registered the info.
1071 * @param pHlp Callback functions for doing output.
1072 * @param pszArgs Argument string. Optional and specific to the handler.
1073 */
1074static DECLCALLBACK(void) ps2kInfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1075{
1076 PPS2K pThis = KBDGetPS2KFromDevIns(pDevIns);
1077 NOREF(pszArgs);
1078
1079 pHlp->pfnPrintf(pHlp, "PS/2 Keyboard: scan set %d, scanning %s\n",
1080 pThis->u8ScanSet, pThis->fScanning ? "enabled" : "disabled");
1081 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
1082 pHlp->pfnPrintf(pHlp, "LED state %02X, Num Lock %s\n", pThis->u8LEDs,
1083 pThis->fNumLockOn ? "on" : "off");
1084 pHlp->pfnPrintf(pHlp, "Typematic delay %ums, repeat period %ums\n",
1085 pThis->uTypematicDelay, pThis->uTypematicRepeat);
1086 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
1087 pThis->cmdQ.cUsed, pThis->cmdQ.cSize);
1088 pHlp->pfnPrintf(pHlp, "Input queue : %d items (%d max)\n",
1089 pThis->keyQ.cUsed, pThis->keyQ.cSize);
1090 if (pThis->enmTypematicState != KBD_TMS_IDLE)
1091 pHlp->pfnPrintf(pHlp, "Active typematic key %02X (%s)\n", pThis->u8Typematic,
1092 pThis->enmTypematicState == KBD_TMS_DELAY ? "delay" : "repeat");
1093}
1094
1095/* -=-=-=-=-=- Keyboard: IBase -=-=-=-=-=- */
1096
1097/**
1098 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1099 */
1100static DECLCALLBACK(void *) ps2kQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1101{
1102 PPS2K pThis = RT_FROM_MEMBER(pInterface, PS2K, Keyboard.IBase);
1103 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Keyboard.IBase);
1104 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDPORT, &pThis->Keyboard.IPort);
1105 return NULL;
1106}
1107
1108
1109/* -=-=-=-=-=- Keyboard: IKeyboardPort -=-=-=-=-=- */
1110
1111/**
1112 * Keyboard event handler.
1113 *
1114 * @returns VBox status code.
1115 * @param pThis The PS2 keyboard instance data.
1116 * @param u32Usage USB HID usage code with key
1117 * press/release flag.
1118 */
1119static int ps2kPutEventWorker(PPS2K pThis, uint32_t u32Usage)
1120{
1121 uint8_t u8HidCode;
1122 bool fKeyDown;
1123 bool fHaveEvent = true;
1124 int rc = VINF_SUCCESS;
1125
1126 /* Extract the usage code and ensure it's valid. */
1127 fKeyDown = !(u32Usage & 0x80000000);
1128 u8HidCode = u32Usage & 0xFF;
1129 AssertReturn(u8HidCode <= VBOX_USB_MAX_USAGE_CODE, VERR_INTERNAL_ERROR);
1130
1131 if (fKeyDown)
1132 {
1133 /* Due to host key repeat, we can get key events for keys which are
1134 * already depressed. We need to ignore those. */
1135 if (pThis->abDepressedKeys[u8HidCode])
1136 fHaveEvent = false;
1137 pThis->abDepressedKeys[u8HidCode] = 1;
1138 }
1139 else
1140 {
1141 /* NB: We allow key release events for keys which aren't depressed.
1142 * That is unlikely to happen and should not cause trouble.
1143 */
1144 pThis->abDepressedKeys[u8HidCode] = 0;
1145 }
1146
1147 /* Unless this is a new key press/release, don't even bother. */
1148 if (fHaveEvent)
1149 {
1150 rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
1151 AssertReleaseRC(rc);
1152
1153 rc = ps2kProcessKeyEvent(pThis, u8HidCode, fKeyDown);
1154
1155 PDMCritSectLeave(pThis->pCritSectR3);
1156 }
1157
1158 return rc;
1159}
1160
1161static DECLCALLBACK(int) ps2kPutEventWrapper(PPDMIKEYBOARDPORT pInterface, uint32_t u32UsageCode)
1162{
1163 PPS2K pThis = RT_FROM_MEMBER(pInterface, PS2K, Keyboard.IPort);
1164 int rc;
1165
1166 LogFlowFunc(("key code %08X\n", u32UsageCode));
1167
1168 rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
1169 AssertReleaseRC(rc);
1170
1171 /* The 'BAT fail' scancode is reused as a signal to release keys. No actual
1172 * key is allowed to use this scancode.
1173 */
1174 if (RT_UNLIKELY(u32UsageCode == KRSP_BAT_FAIL))
1175 {
1176 ps2kReleaseKeys(pThis);
1177 }
1178 else
1179 {
1180 ps2kPutEventWorker(pThis, u32UsageCode);
1181 }
1182
1183 PDMCritSectLeave(pThis->pCritSectR3);
1184
1185 return VINF_SUCCESS;
1186}
1187
1188
1189/**
1190 * Attach command.
1191 *
1192 * This is called to let the device attach to a driver for a
1193 * specified LUN.
1194 *
1195 * This is like plugging in the keyboard after turning on the
1196 * system.
1197 *
1198 * @returns VBox status code.
1199 * @param pThis The PS/2 keyboard instance data.
1200 * @param pDevIns The device instance.
1201 * @param iLUN The logical unit which is being detached.
1202 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1203 */
1204int PS2KAttach(PPS2K pThis, PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
1205{
1206 int rc;
1207
1208 /* The LUN must be 0, i.e. keyboard. */
1209 Assert(iLUN == 0);
1210 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
1211 ("PS/2 keyboard does not support hotplugging\n"),
1212 VERR_INVALID_PARAMETER);
1213
1214 LogFlowFunc(("iLUN=%d\n", iLUN));
1215
1216 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Keyboard.IBase, &pThis->Keyboard.pDrvBase, "Keyboard Port");
1217 if (RT_SUCCESS(rc))
1218 {
1219 pThis->Keyboard.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Keyboard.pDrvBase, PDMIKEYBOARDCONNECTOR);
1220 if (!pThis->Keyboard.pDrv)
1221 {
1222 AssertLogRelMsgFailed(("LUN #0 doesn't have a keyboard interface! rc=%Rrc\n", rc));
1223 rc = VERR_PDM_MISSING_INTERFACE;
1224 }
1225 }
1226 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1227 {
1228 Log(("%s/%d: warning: no driver attached to LUN #0!\n", pDevIns->pReg->szName, pDevIns->iInstance));
1229 rc = VINF_SUCCESS;
1230 }
1231 else
1232 AssertLogRelMsgFailed(("Failed to attach LUN #0! rc=%Rrc\n", rc));
1233
1234 return rc;
1235}
1236
1237void PS2KSaveState(PPS2K pThis, PSSMHANDLE pSSM)
1238{
1239 uint32_t cPressed = 0;
1240 uint32_t cbTMSSize = 0;
1241
1242 LogFlowFunc(("Saving PS2K state\n"));
1243
1244 /* Save the basic keyboard state. */
1245 SSMR3PutU8(pSSM, pThis->u8CurrCmd);
1246 SSMR3PutU8(pSSM, pThis->u8LEDs);
1247 SSMR3PutU8(pSSM, pThis->u8Typematic);
1248 SSMR3PutU8(pSSM, pThis->u8TypematicKey);
1249 SSMR3PutU8(pSSM, pThis->u8Modifiers);
1250 SSMR3PutU8(pSSM, pThis->u8ScanSet);
1251 SSMR3PutU8(pSSM, pThis->enmTypematicState);
1252 SSMR3PutBool(pSSM, pThis->fNumLockOn);
1253 SSMR3PutBool(pSSM, pThis->fScanning);
1254
1255 /* Save the command and keystroke queues. */
1256 ps2kSaveQueue(pSSM, (GeneriQ *)&pThis->cmdQ);
1257 ps2kSaveQueue(pSSM, (GeneriQ *)&pThis->keyQ);
1258
1259 /* Save the command delay timer. Note that the typematic repeat
1260 * timer is *not* saved.
1261 */
1262 TMR3TimerSave(pThis->CTX_SUFF(pKbdDelayTimer), pSSM);
1263
1264 /* Save any pressed keys. This is necessary to avoid "stuck"
1265 * keys after a restore. Needs two passes.
1266 */
1267 for (unsigned i = 0; i < sizeof(pThis->abDepressedKeys); ++i)
1268 if (pThis->abDepressedKeys[i])
1269 ++cPressed;
1270
1271 SSMR3PutU32(pSSM, cPressed);
1272
1273 for (unsigned uKey = 0; uKey < sizeof(pThis->abDepressedKeys); ++uKey)
1274 if (pThis->abDepressedKeys[uKey])
1275 SSMR3PutU8(pSSM, uKey);
1276
1277 /* Save the typematic settings for Scan Set 3. */
1278 SSMR3PutU32(pSSM, cbTMSSize);
1279 /* Currently not implemented. */
1280}
1281
1282int PS2KLoadState(PPS2K pThis, PSSMHANDLE pSSM, uint32_t uVersion)
1283{
1284 uint8_t u8;
1285 uint32_t cPressed;
1286 uint32_t cbTMSSize;
1287 int rc;
1288
1289 NOREF(uVersion);
1290 LogFlowFunc(("Loading PS2K state version %u\n", uVersion));
1291
1292 /* Load the basic keyboard state. */
1293 SSMR3GetU8(pSSM, &pThis->u8CurrCmd);
1294 SSMR3GetU8(pSSM, &pThis->u8LEDs);
1295 SSMR3GetU8(pSSM, &pThis->u8Typematic);
1296 SSMR3GetU8(pSSM, &pThis->u8TypematicKey);
1297 SSMR3GetU8(pSSM, &pThis->u8Modifiers);
1298 SSMR3GetU8(pSSM, &pThis->u8ScanSet);
1299 SSMR3GetU8(pSSM, &u8);
1300 pThis->enmTypematicState = (tmatic_state_t)u8;
1301 SSMR3GetBool(pSSM, &pThis->fNumLockOn);
1302 SSMR3GetBool(pSSM, &pThis->fScanning);
1303
1304 /* Load the command and keystroke queues. */
1305 rc = ps2kLoadQueue(pSSM, (GeneriQ *)&pThis->cmdQ);
1306 AssertRCReturn(rc, rc);
1307 rc = ps2kLoadQueue(pSSM, (GeneriQ *)&pThis->keyQ);
1308 AssertRCReturn(rc, rc);
1309
1310 /* Load the command delay timer, just in case. */
1311 rc = TMR3TimerLoad(pThis->CTX_SUFF(pKbdDelayTimer), pSSM);
1312 AssertRCReturn(rc, rc);
1313
1314 /* Recalculate the typematic delay/rate. */
1315 ps2kSetupTypematic(pThis, pThis->u8Typematic);
1316
1317 /* Fake key up events for keys that were held down at the time the state was saved. */
1318 rc = SSMR3GetU32(pSSM, &cPressed);
1319 AssertRCReturn(rc, rc);
1320
1321 /* If any keys were down, load and then release them. */
1322 if (cPressed)
1323 {
1324 for (unsigned i = 0; i < cPressed; ++i)
1325 {
1326 rc = SSMR3GetU8(pSSM, &u8);
1327 AssertRCReturn(rc, rc);
1328 pThis->abDepressedKeys[u8] = 1;
1329 }
1330 }
1331
1332 /* Load typematic settings for Scan Set 3. */
1333 rc = SSMR3GetU32(pSSM, &cbTMSSize);
1334 AssertRCReturn(rc, rc);
1335
1336 while (cbTMSSize--)
1337 {
1338 rc = SSMR3GetU8(pSSM, &u8);
1339 AssertRCReturn(rc, rc);
1340 }
1341
1342 return rc;
1343}
1344
1345int PS2KLoadDone(PPS2K pThis, PSSMHANDLE pSSM)
1346{
1347 /* This *must* be done after the inital load because it may trigger
1348 * interrupts and change the interrupt controller state.
1349 */
1350 ps2kReleaseKeys(pThis);
1351 ps2kNotifyLedsState(pThis, pThis->u8LEDs);
1352 return VINF_SUCCESS;
1353}
1354
1355void PS2KReset(PPS2K pThis)
1356{
1357 LogFlowFunc(("Resetting PS2K\n"));
1358
1359 pThis->fScanning = true;
1360 pThis->u8ScanSet = 2;
1361 pThis->u8CurrCmd = 0;
1362 pThis->u8Modifiers = 0;
1363 pThis->u8TypematicKey = 0;
1364 pThis->enmTypematicState = KBD_TMS_IDLE;
1365
1366 /* Clear queues and any pressed keys. */
1367 memset(pThis->abDepressedKeys, 0, sizeof(pThis->abDepressedKeys));
1368 ps2kClearQueue((GeneriQ *)&pThis->cmdQ);
1369 ps2kSetDefaults(pThis); /* Also clears keystroke queue. */
1370
1371 /* Activate the PS/2 keyboard by default. */
1372 if (pThis->Keyboard.pDrv)
1373 pThis->Keyboard.pDrv->pfnSetActive(pThis->Keyboard.pDrv, true);
1374}
1375
1376void PS2KRelocate(PPS2K pThis, RTGCINTPTR offDelta, PPDMDEVINS pDevIns)
1377{
1378 LogFlowFunc(("Relocating PS2K\n"));
1379 pThis->pKbdDelayTimerRC = TMTimerRCPtr(pThis->pKbdDelayTimerR3);
1380 pThis->pKbdTypematicTimerRC = TMTimerRCPtr(pThis->pKbdTypematicTimerR3);
1381 NOREF(offDelta);
1382}
1383
1384int PS2KConstruct(PPS2K pThis, PPDMDEVINS pDevIns, void *pParent, int iInstance)
1385{
1386 int rc;
1387
1388 LogFlowFunc(("iInstance=%d\n", iInstance));
1389
1390 pThis->pParent = pParent;
1391
1392 /* Initialize the queues. */
1393 pThis->keyQ.cSize = KBD_KEY_QUEUE_SIZE;
1394 pThis->cmdQ.cSize = KBD_CMD_QUEUE_SIZE;
1395
1396 pThis->Keyboard.IBase.pfnQueryInterface = ps2kQueryInterface;
1397 pThis->Keyboard.IPort.pfnPutEventHid = ps2kPutEventWrapper;
1398
1399 /*
1400 * Initialize the critical section pointer(s).
1401 */
1402 pThis->pCritSectR3 = pDevIns->pCritSectRoR3;
1403
1404 /*
1405 * Create the typematic delay/repeat timer. Does not use virtual time!
1406 */
1407 PTMTIMER pTimer;
1408 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_REAL, ps2kTypematicTimer, pThis,
1409 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2K Typematic Timer", &pTimer);
1410 if (RT_FAILURE(rc))
1411 return rc;
1412
1413 pThis->pKbdTypematicTimerR3 = pTimer;
1414 pThis->pKbdTypematicTimerR0 = TMTimerR0Ptr(pTimer);
1415 pThis->pKbdTypematicTimerRC = TMTimerRCPtr(pTimer);
1416
1417 /*
1418 * Create the command delay timer.
1419 */
1420 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2kDelayTimer, pThis,
1421 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2K Delay Timer", &pTimer);
1422 if (RT_FAILURE(rc))
1423 return rc;
1424
1425 pThis->pKbdDelayTimerR3 = pTimer;
1426 pThis->pKbdDelayTimerR0 = TMTimerR0Ptr(pTimer);
1427 pThis->pKbdDelayTimerRC = TMTimerRCPtr(pTimer);
1428
1429 /*
1430 * Register debugger info callbacks.
1431 */
1432 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2k", "Display PS/2 keyboard state.", ps2kInfoState);
1433
1434 return rc;
1435}
1436
1437#endif
1438
1439///@todo The following should live with the KBC implementation.
1440
1441/* Table used by the keyboard controller to optionally translate the incoming
1442 * keyboard data. Note that the translation is designed for essentially taking
1443 * Scan Set 2 input and producing Scan Set 1 output, but can be turned on and
1444 * off regardless of what the keyboard is sending.
1445 */
1446static uint8_t aAT2PC[128] = {
1447 0xff,0x43,0x41,0x3f,0x3d,0x3b,0x3c,0x58,0x64,0x44,0x42,0x40,0x3e,0x0f,0x29,0x59,
1448 0x65,0x38,0x2a,0x70,0x1d,0x10,0x02,0x5a,0x66,0x71,0x2c,0x1f,0x1e,0x11,0x03,0x5b,
1449 0x67,0x2e,0x2d,0x20,0x12,0x05,0x04,0x5c,0x68,0x39,0x2f,0x21,0x14,0x13,0x06,0x5d,
1450 0x69,0x31,0x30,0x23,0x22,0x15,0x07,0x5e,0x6a,0x72,0x32,0x24,0x16,0x08,0x09,0x5f,
1451 0x6b,0x33,0x25,0x17,0x18,0x0b,0x0a,0x60,0x6c,0x34,0x35,0x26,0x27,0x19,0x0c,0x61,
1452 0x6d,0x73,0x28,0x74,0x1a,0x0d,0x62,0x6e,0x3a,0x36,0x1c,0x1b,0x75,0x2b,0x63,0x76,
1453 0x55,0x56,0x77,0x78,0x79,0x7a,0x0e,0x7b,0x7c,0x4f,0x7d,0x4b,0x47,0x7e,0x7f,0x6f,
1454 0x52,0x53,0x50,0x4c,0x4d,0x48,0x01,0x45,0x57,0x4e,0x51,0x4a,0x37,0x49,0x46,0x54
1455};
1456
1457/**
1458 * Convert an AT (Scan Set 2) scancode to PC (Scan Set 1).
1459 *
1460 * @param state Current state of the translator
1461 * (xlat_state_t).
1462 * @param scanIn Incoming scan code.
1463 * @param pScanOut Pointer to outgoing scan code. The
1464 * contents are only valid if returned
1465 * state is not XS_BREAK.
1466 *
1467 * @return xlat_state_t New state of the translator.
1468 */
1469int32_t XlateAT2PC(int32_t state, uint8_t scanIn, uint8_t *pScanOut)
1470{
1471 uint8_t scan_in;
1472 uint8_t scan_out;
1473
1474 Assert(pScanOut);
1475 Assert(state == XS_IDLE || state == XS_BREAK || state == XS_HIBIT);
1476
1477 /* Preprocess the scan code for a 128-entry translation table. */
1478 if (scanIn == 0x83) /* Check for F7 key. */
1479 scan_in = 0x02;
1480 else if (scanIn == 0x84) /* Check for SysRq key. */
1481 scan_in = 0x7f;
1482 else
1483 scan_in = scanIn;
1484
1485 /* Values 0x80 and above are passed through, except for 0xF0
1486 * which indicates a key release.
1487 */
1488 if (scan_in < 0x80)
1489 {
1490 scan_out = aAT2PC[scan_in];
1491 /* Turn into break code if required. */
1492 if (state == XS_BREAK || state == XS_HIBIT)
1493 scan_out |= 0x80;
1494
1495 state = XS_IDLE;
1496 }
1497 else
1498 {
1499 /* NB: F0 E0 10 will be translated to E0 E5 (high bit set on last byte)! */
1500 if (scan_in == 0xF0) /* Check for break code. */
1501 state = XS_BREAK;
1502 else if (state == XS_BREAK)
1503 state = XS_HIBIT; /* Remember the break bit. */
1504 scan_out = scan_in;
1505 }
1506 LogFlowFunc(("scan code %02X translated to %02X; new state is %d\n",
1507 scanIn, scan_out, state));
1508
1509 *pScanOut = scan_out;
1510 return state;
1511}
1512
1513#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
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