1 | /* $Id: DevPS2.h 82189 2019-11-25 17:37:49Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PS/2 devices - Internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2019 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 | #ifndef VBOX_INCLUDED_SRC_Input_DevPS2_h
|
---|
19 | #define VBOX_INCLUDED_SRC_Input_DevPS2_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | /** @defgroup grp_devps2 PS/2 Device
|
---|
25 | * @{
|
---|
26 | */
|
---|
27 |
|
---|
28 | /** Pointer to the shared keyboard (PS/2) controller / device state. */
|
---|
29 | typedef struct KBDSTATE *PKBDSTATE;
|
---|
30 |
|
---|
31 | /** Define a simple PS/2 input device queue. */
|
---|
32 | #define DEF_PS2Q_TYPE(name, size) \
|
---|
33 | typedef struct { \
|
---|
34 | uint32_t rpos; \
|
---|
35 | uint32_t wpos; \
|
---|
36 | uint32_t cUsed; \
|
---|
37 | uint32_t cSize; \
|
---|
38 | uint8_t abQueue[size]; \
|
---|
39 | } name
|
---|
40 |
|
---|
41 | DEF_PS2Q_TYPE(GeneriQ, 1);
|
---|
42 | void PS2CmnClearQueue(GeneriQ *pQ);
|
---|
43 | void PS2CmnInsertQueue(GeneriQ *pQ, uint8_t val);
|
---|
44 | void PS2CmnR3SaveQueue(PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, GeneriQ *pQ);
|
---|
45 | int PS2CmnR3LoadQueue(PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, GeneriQ *pQ);
|
---|
46 |
|
---|
47 |
|
---|
48 | /** @defgroup grp_devps2k DevPS2K - Keyboard
|
---|
49 | * @{
|
---|
50 | */
|
---|
51 |
|
---|
52 | /** @name HID modifier range.
|
---|
53 | * @{ */
|
---|
54 | #define HID_MODIFIER_FIRST 0xE0
|
---|
55 | #define HID_MODIFIER_LAST 0xE8
|
---|
56 | /** @} */
|
---|
57 |
|
---|
58 | /** @name USB HID additional constants
|
---|
59 | * @{ */
|
---|
60 | /** The highest USB usage code reported by VirtualBox. */
|
---|
61 | #define VBOX_USB_MAX_USAGE_CODE 0xE7
|
---|
62 | /** The size of an array needed to store all USB usage codes */
|
---|
63 | #define VBOX_USB_USAGE_ARRAY_SIZE (VBOX_USB_MAX_USAGE_CODE + 1)
|
---|
64 | /** USB HID Keyboard Usage Page. */
|
---|
65 | #define USB_HID_KB_PAGE 7
|
---|
66 | /** USB HID Consumer Control Usage Page. */
|
---|
67 | #define USB_HID_CC_PAGE 12
|
---|
68 | /** @} */
|
---|
69 |
|
---|
70 | /* Internal keyboard queue sizes. The input queue doesn't need to be
|
---|
71 | * extra huge and the command queue only needs to handle a few bytes.
|
---|
72 | */
|
---|
73 | #define KBD_KEY_QUEUE_SIZE 64
|
---|
74 | #define KBD_CMD_QUEUE_SIZE 4
|
---|
75 |
|
---|
76 | DEF_PS2Q_TYPE(KbdKeyQ, KBD_KEY_QUEUE_SIZE);
|
---|
77 | DEF_PS2Q_TYPE(KbdCmdQ, KBD_CMD_QUEUE_SIZE);
|
---|
78 |
|
---|
79 | /** Typematic state. */
|
---|
80 | typedef enum {
|
---|
81 | KBD_TMS_IDLE = 0, /* No typematic key active. */
|
---|
82 | KBD_TMS_DELAY = 1, /* In the initial delay period. */
|
---|
83 | KBD_TMS_REPEAT = 2, /* Key repeating at set rate. */
|
---|
84 | KBD_TMS_32BIT_HACK = 0x7fffffff
|
---|
85 | } tmatic_state_t;
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * The shared PS/2 keyboard instance data.
|
---|
90 | */
|
---|
91 | typedef struct PS2K
|
---|
92 | {
|
---|
93 | /** Pointer to parent device (keyboard controller). */
|
---|
94 | R3PTRTYPE(PKBDSTATE) pParent;
|
---|
95 | /** Set if keyboard is enabled ('scans' for input). */
|
---|
96 | bool fScanning;
|
---|
97 | /** Set NumLock is on. */
|
---|
98 | bool fNumLockOn;
|
---|
99 | /** Selected scan set. */
|
---|
100 | uint8_t u8ScanSet;
|
---|
101 | /** Modifier key state. */
|
---|
102 | uint8_t u8Modifiers;
|
---|
103 | /** Currently processed command (if any). */
|
---|
104 | uint8_t u8CurrCmd;
|
---|
105 | /** Status indicator (LED) state. */
|
---|
106 | uint8_t u8LEDs;
|
---|
107 | /** Selected typematic delay/rate. */
|
---|
108 | uint8_t u8TypematicCfg;
|
---|
109 | /** Usage code of current typematic key, if any. */
|
---|
110 | uint32_t u32TypematicKey;
|
---|
111 | /** Current typematic repeat state. */
|
---|
112 | tmatic_state_t enmTypematicState;
|
---|
113 | /** Buffer holding scan codes to be sent to the host. */
|
---|
114 | KbdKeyQ keyQ;
|
---|
115 | /** Command response queue (priority). */
|
---|
116 | KbdCmdQ cmdQ;
|
---|
117 | /** Currently depressed keys. */
|
---|
118 | uint8_t abDepressedKeys[VBOX_USB_USAGE_ARRAY_SIZE];
|
---|
119 | /** Typematic delay in milliseconds. */
|
---|
120 | unsigned uTypematicDelay;
|
---|
121 | /** Typematic repeat period in milliseconds. */
|
---|
122 | unsigned uTypematicRepeat;
|
---|
123 | /** Set if the throttle delay is currently active. */
|
---|
124 | bool fThrottleActive;
|
---|
125 | /** Set if the input rate should be throttled. */
|
---|
126 | bool fThrottleEnabled;
|
---|
127 |
|
---|
128 | uint8_t Alignment0[2];
|
---|
129 |
|
---|
130 | /** Command delay timer - RC Ptr. */
|
---|
131 | PTMTIMERRC pKbdDelayTimerRC;
|
---|
132 | /** Typematic timer - RC Ptr. */
|
---|
133 | PTMTIMERRC pKbdTypematicTimerRC;
|
---|
134 | /** Input throttle timer - RC Ptr. */
|
---|
135 | PTMTIMERRC pThrottleTimerRC;
|
---|
136 |
|
---|
137 | /** The device critical section protecting everything - R3 Ptr */
|
---|
138 | R3PTRTYPE(PPDMCRITSECT) pCritSectR3;
|
---|
139 |
|
---|
140 | /** Command delay timer - R3 Ptr. */
|
---|
141 | PTMTIMERR3 pKbdDelayTimerR3;
|
---|
142 | /** Typematic timer - R3 Ptr. */
|
---|
143 | PTMTIMERR3 pKbdTypematicTimerR3;
|
---|
144 | /** Input throttle timer - R3 Ptr. */
|
---|
145 | PTMTIMERR3 pThrottleTimerR3;
|
---|
146 |
|
---|
147 | /** Command delay timer - R0 Ptr. */
|
---|
148 | PTMTIMERR0 pKbdDelayTimerR0;
|
---|
149 | /** Typematic timer - R0 Ptr. */
|
---|
150 | PTMTIMERR0 pKbdTypematicTimerR0;
|
---|
151 | /** Input throttle timer - R0 Ptr. */
|
---|
152 | PTMTIMERR0 pThrottleTimerR0;
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Keyboard port - LUN#0.
|
---|
156 | *
|
---|
157 | * @implements PDMIBASE
|
---|
158 | * @implements PDMIKEYBOARDPORT
|
---|
159 | */
|
---|
160 | struct
|
---|
161 | {
|
---|
162 | /** The base interface for the keyboard port. */
|
---|
163 | PDMIBASE IBase;
|
---|
164 | /** The keyboard port base interface. */
|
---|
165 | PDMIKEYBOARDPORT IPort;
|
---|
166 |
|
---|
167 | /** The base interface of the attached keyboard driver. */
|
---|
168 | R3PTRTYPE(PPDMIBASE) pDrvBase;
|
---|
169 | /** The keyboard interface of the attached keyboard driver. */
|
---|
170 | R3PTRTYPE(PPDMIKEYBOARDCONNECTOR) pDrv;
|
---|
171 | } Keyboard;
|
---|
172 | } PS2K;
|
---|
173 | /** Pointer to the PS/2 keyboard instance data. */
|
---|
174 | typedef PS2K *PPS2K;
|
---|
175 |
|
---|
176 |
|
---|
177 | int PS2KByteToKbd(PPS2K pThis, uint8_t cmd);
|
---|
178 | int PS2KByteFromKbd(PPS2K pThis, uint8_t *pVal);
|
---|
179 |
|
---|
180 | int PS2KR3Construct(PPS2K pThis, PPDMDEVINS pDevIns, PKBDSTATE pParent, unsigned iInstance, PCFGMNODE pCfg);
|
---|
181 | int PS2KR3Attach(PPS2K pThis, PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags);
|
---|
182 | void PS2KR3Reset(PPS2K pThis);
|
---|
183 | void PS2KR3Relocate(PPS2K pThis, RTGCINTPTR offDelta, PPDMDEVINS pDevIns);
|
---|
184 | void PS2KR3SaveState(PPDMDEVINS pDevIns, PPS2K pThis, PSSMHANDLE pSSM);
|
---|
185 | int PS2KR3LoadState(PPDMDEVINS pDevIns, PPS2K pThis, PSSMHANDLE pSSM, uint32_t uVersion);
|
---|
186 | int PS2KR3LoadDone(PPS2K pThis, PSSMHANDLE pSSM);
|
---|
187 |
|
---|
188 | PS2K *KBDGetPS2KFromDevIns(PPDMDEVINS pDevIns);
|
---|
189 |
|
---|
190 | /** @} */
|
---|
191 |
|
---|
192 |
|
---|
193 | /** @defgroup grp_devps2m DevPS2M - Auxiliary Device (Mouse)
|
---|
194 | * @{
|
---|
195 | */
|
---|
196 |
|
---|
197 | /* Internal mouse queue sizes. The input queue is relatively large,
|
---|
198 | * but the command queue only needs to handle a few bytes.
|
---|
199 | */
|
---|
200 | #define AUX_EVT_QUEUE_SIZE 256
|
---|
201 | #define AUX_CMD_QUEUE_SIZE 8
|
---|
202 |
|
---|
203 | DEF_PS2Q_TYPE(AuxEvtQ, AUX_EVT_QUEUE_SIZE);
|
---|
204 | DEF_PS2Q_TYPE(AuxCmdQ, AUX_CMD_QUEUE_SIZE);
|
---|
205 |
|
---|
206 | /** Auxiliary device special modes of operation. */
|
---|
207 | typedef enum {
|
---|
208 | AUX_MODE_STD, /* Standard operation. */
|
---|
209 | AUX_MODE_RESET, /* Currently in reset. */
|
---|
210 | AUX_MODE_WRAP /* Wrap mode (echoing input). */
|
---|
211 | } PS2M_MODE;
|
---|
212 |
|
---|
213 | /** Auxiliary device operational state. */
|
---|
214 | typedef enum {
|
---|
215 | AUX_STATE_RATE_ERR = RT_BIT(0), /* Invalid rate received. */
|
---|
216 | AUX_STATE_RES_ERR = RT_BIT(1), /* Invalid resolution received. */
|
---|
217 | AUX_STATE_SCALING = RT_BIT(4), /* 2:1 scaling in effect. */
|
---|
218 | AUX_STATE_ENABLED = RT_BIT(5), /* Reporting enabled in stream mode. */
|
---|
219 | AUX_STATE_REMOTE = RT_BIT(6) /* Remote mode (reports on request). */
|
---|
220 | } PS2M_STATE;
|
---|
221 |
|
---|
222 | /** Externally visible state bits. */
|
---|
223 | #define AUX_STATE_EXTERNAL (AUX_STATE_SCALING | AUX_STATE_ENABLED | AUX_STATE_REMOTE)
|
---|
224 |
|
---|
225 | /** Protocols supported by the PS/2 mouse. */
|
---|
226 | typedef enum {
|
---|
227 | PS2M_PROTO_PS2STD = 0, /* Standard PS/2 mouse protocol. */
|
---|
228 | PS2M_PROTO_IMPS2 = 3, /* IntelliMouse PS/2 protocol. */
|
---|
229 | PS2M_PROTO_IMEX = 4, /* IntelliMouse Explorer protocol. */
|
---|
230 | PS2M_PROTO_IMEX_HORZ = 5 /* IntelliMouse Explorer with horizontal reports. */
|
---|
231 | } PS2M_PROTO;
|
---|
232 |
|
---|
233 | /** Protocol selection 'knock' states. */
|
---|
234 | typedef enum {
|
---|
235 | PS2M_KNOCK_INITIAL,
|
---|
236 | PS2M_KNOCK_1ST,
|
---|
237 | PS2M_KNOCK_IMPS2_2ND,
|
---|
238 | PS2M_KNOCK_IMEX_2ND,
|
---|
239 | PS2M_KNOCK_IMEX_HORZ_2ND
|
---|
240 | } PS2M_KNOCK_STATE;
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * The PS/2 auxiliary device instance data.
|
---|
244 | */
|
---|
245 | typedef struct PS2M
|
---|
246 | {
|
---|
247 | /** Pointer to parent device (keyboard controller). */
|
---|
248 | R3PTRTYPE(PKBDSTATE) pParent;
|
---|
249 | /** Operational state. */
|
---|
250 | uint8_t u8State;
|
---|
251 | /** Configured sampling rate. */
|
---|
252 | uint8_t u8SampleRate;
|
---|
253 | /** Configured resolution. */
|
---|
254 | uint8_t u8Resolution;
|
---|
255 | /** Currently processed command (if any). */
|
---|
256 | uint8_t u8CurrCmd;
|
---|
257 | /** Set if the throttle delay is active. */
|
---|
258 | bool fThrottleActive;
|
---|
259 | /** Set if the throttle delay is active. */
|
---|
260 | bool fDelayReset;
|
---|
261 | /** Operational mode. */
|
---|
262 | PS2M_MODE enmMode;
|
---|
263 | /** Currently used protocol. */
|
---|
264 | PS2M_PROTO enmProtocol;
|
---|
265 | /** Currently used protocol. */
|
---|
266 | PS2M_KNOCK_STATE enmKnockState;
|
---|
267 | /** Buffer holding mouse events to be sent to the host. */
|
---|
268 | AuxEvtQ evtQ;
|
---|
269 | /** Command response queue (priority). */
|
---|
270 | AuxCmdQ cmdQ;
|
---|
271 | /** Accumulated horizontal movement. */
|
---|
272 | int32_t iAccumX;
|
---|
273 | /** Accumulated vertical movement. */
|
---|
274 | int32_t iAccumY;
|
---|
275 | /** Accumulated Z axis (vertical scroll) movement. */
|
---|
276 | int32_t iAccumZ;
|
---|
277 | /** Accumulated W axis (horizontal scroll) movement. */
|
---|
278 | int32_t iAccumW;
|
---|
279 | /** Accumulated button presses. */
|
---|
280 | uint32_t fAccumB;
|
---|
281 | /** Instantaneous button data. */
|
---|
282 | uint32_t fCurrB;
|
---|
283 | /** Button state last sent to the guest. */
|
---|
284 | uint32_t fReportedB;
|
---|
285 | /** Throttling delay in milliseconds. */
|
---|
286 | uint32_t uThrottleDelay;
|
---|
287 |
|
---|
288 | /** The device critical section protecting everything - R3 Ptr */
|
---|
289 | R3PTRTYPE(PPDMCRITSECT) pCritSectR3;
|
---|
290 | /** Command delay timer - R3 Ptr. */
|
---|
291 | PTMTIMERR3 pDelayTimerR3;
|
---|
292 | /** Interrupt throttling timer - R3 Ptr. */
|
---|
293 | PTMTIMERR3 pThrottleTimerR3;
|
---|
294 | RTR3PTR Alignment1;
|
---|
295 |
|
---|
296 | /** Command delay timer - RC Ptr. */
|
---|
297 | PTMTIMERRC pDelayTimerRC;
|
---|
298 | /** Interrupt throttling timer - RC Ptr. */
|
---|
299 | PTMTIMERRC pThrottleTimerRC;
|
---|
300 |
|
---|
301 | /** Command delay timer - R0 Ptr. */
|
---|
302 | PTMTIMERR0 pDelayTimerR0;
|
---|
303 | /** Interrupt throttling timer - R0 Ptr. */
|
---|
304 | PTMTIMERR0 pThrottleTimerR0;
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Mouse port - LUN#1.
|
---|
308 | *
|
---|
309 | * @implements PDMIBASE
|
---|
310 | * @implements PDMIMOUSEPORT
|
---|
311 | */
|
---|
312 | struct
|
---|
313 | {
|
---|
314 | /** The base interface for the mouse port. */
|
---|
315 | PDMIBASE IBase;
|
---|
316 | /** The keyboard port base interface. */
|
---|
317 | PDMIMOUSEPORT IPort;
|
---|
318 |
|
---|
319 | /** The base interface of the attached mouse driver. */
|
---|
320 | R3PTRTYPE(PPDMIBASE) pDrvBase;
|
---|
321 | /** The keyboard interface of the attached mouse driver. */
|
---|
322 | R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
|
---|
323 | } Mouse;
|
---|
324 | } PS2M;
|
---|
325 | /** Pointer to the PS/2 auxiliary device instance data. */
|
---|
326 | typedef PS2M *PPS2M;
|
---|
327 |
|
---|
328 | int PS2MByteToAux(PPS2M pThis, uint8_t cmd);
|
---|
329 | int PS2MByteFromAux(PPS2M pThis, uint8_t *pVal);
|
---|
330 |
|
---|
331 | int PS2MR3Construct(PPS2M pThis, PPDMDEVINS pDevIns, PKBDSTATE pParent, unsigned iInstance);
|
---|
332 | int PS2MR3Attach(PPS2M pThis, PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags);
|
---|
333 | void PS2MR3Reset(PPS2M pThis);
|
---|
334 | void PS2MR3Relocate(PPS2M pThis, RTGCINTPTR offDelta, PPDMDEVINS pDevIns);
|
---|
335 | void PS2MR3SaveState(PPDMDEVINS pDevIns, PPS2M pThis, PSSMHANDLE pSSM);
|
---|
336 | int PS2MR3LoadState(PPDMDEVINS pDevIns, PPS2M pThis, PSSMHANDLE pSSM, uint32_t uVersion);
|
---|
337 | void PS2MR3FixupState(PPS2M pThis, uint8_t u8State, uint8_t u8Rate, uint8_t u8Proto);
|
---|
338 |
|
---|
339 | PS2M *KBDGetPS2MFromDevIns(PPDMDEVINS pDevIns);
|
---|
340 |
|
---|
341 | /** @} */
|
---|
342 |
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * The shared keyboard controller/device state.
|
---|
346 | *
|
---|
347 | * @note We use the default critical section for serialize data access.
|
---|
348 | */
|
---|
349 | typedef struct KBDSTATE
|
---|
350 | {
|
---|
351 | uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
|
---|
352 | uint8_t status;
|
---|
353 | uint8_t mode;
|
---|
354 | uint8_t dbbout; /* data buffer byte */
|
---|
355 | /* keyboard state */
|
---|
356 | int32_t translate;
|
---|
357 | int32_t xlat_state;
|
---|
358 |
|
---|
359 | /** Pointer to the device instance - RC. */
|
---|
360 | PPDMDEVINSRC pDevInsRC;
|
---|
361 | /** Pointer to the device instance - R3 . */
|
---|
362 | PPDMDEVINSR3 pDevInsR3;
|
---|
363 | /** Pointer to the device instance. */
|
---|
364 | PPDMDEVINSR0 pDevInsR0;
|
---|
365 |
|
---|
366 | /** Keyboard state (implemented in separate PS2K module). */
|
---|
367 | PS2K Kbd;
|
---|
368 |
|
---|
369 | /** Mouse state (implemented in separate PS2M module). */
|
---|
370 | PS2M Aux;
|
---|
371 | } KBDSTATE, KBDState;
|
---|
372 |
|
---|
373 |
|
---|
374 | /* Shared keyboard/aux internal interface. */
|
---|
375 | void KBCUpdateInterrupts(void *pKbc);
|
---|
376 |
|
---|
377 |
|
---|
378 | ///@todo: This should live with the KBC implementation.
|
---|
379 | /** AT to PC scancode translator state. */
|
---|
380 | typedef enum
|
---|
381 | {
|
---|
382 | XS_IDLE, /**< Starting state. */
|
---|
383 | XS_BREAK, /**< F0 break byte was received. */
|
---|
384 | XS_HIBIT /**< Break code still active. */
|
---|
385 | } xlat_state_t;
|
---|
386 |
|
---|
387 | int32_t XlateAT2PC(int32_t state, uint8_t scanIn, uint8_t *pScanOut);
|
---|
388 |
|
---|
389 | /** @} */
|
---|
390 |
|
---|
391 | #endif /* !VBOX_INCLUDED_SRC_Input_DevPS2_h */
|
---|
392 |
|
---|