1 | /* $Id: DrvKeyboardQueue.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox input devices: Keyboard queue driver
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_DRV_KBD_QUEUE
|
---|
33 | #include <VBox/vmm/pdmdrv.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/uuid.h>
|
---|
36 |
|
---|
37 | #include "VBoxDD.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Defined Constants And Macros *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 |
|
---|
44 | /** Keyboard usage page bits to be OR-ed into the code. */
|
---|
45 | #define HID_PG_KB_BITS RT_MAKE_U32(0, USB_HID_KB_PAGE)
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 |
|
---|
52 | /** Scancode translator state. */
|
---|
53 | typedef enum {
|
---|
54 | SS_IDLE, /**< Starting state. */
|
---|
55 | SS_EXT, /**< E0 byte was received. */
|
---|
56 | SS_EXT1 /**< E1 byte was received. */
|
---|
57 | } scan_state_t;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Keyboard queue driver instance data.
|
---|
61 | *
|
---|
62 | * @implements PDMIKEYBOARDCONNECTOR
|
---|
63 | * @implements PDMIKEYBOARDPORT
|
---|
64 | */
|
---|
65 | typedef struct DRVKBDQUEUE
|
---|
66 | {
|
---|
67 | /** Pointer to the driver instance structure. */
|
---|
68 | PPDMDRVINS pDrvIns;
|
---|
69 | /** Pointer to the keyboard port interface of the driver/device above us. */
|
---|
70 | PPDMIKEYBOARDPORT pUpPort;
|
---|
71 | /** Pointer to the keyboard port interface of the driver/device below us. */
|
---|
72 | PPDMIKEYBOARDCONNECTOR pDownConnector;
|
---|
73 | /** Our keyboard connector interface. */
|
---|
74 | PDMIKEYBOARDCONNECTOR IConnector;
|
---|
75 | /** Our keyboard port interface. */
|
---|
76 | PDMIKEYBOARDPORT IPort;
|
---|
77 | /** The queue handle. */
|
---|
78 | PDMQUEUEHANDLE hQueue;
|
---|
79 | /** State of the scancode translation. */
|
---|
80 | scan_state_t XlatState;
|
---|
81 | /** Discard input when this flag is set. */
|
---|
82 | bool fInactive;
|
---|
83 | /** When VM is suspended, queue full errors are not fatal. */
|
---|
84 | bool fSuspended;
|
---|
85 | } DRVKBDQUEUE, *PDRVKBDQUEUE;
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Keyboard queue item.
|
---|
90 | */
|
---|
91 | typedef struct DRVKBDQUEUEITEM
|
---|
92 | {
|
---|
93 | /** The core part owned by the queue manager. */
|
---|
94 | PDMQUEUEITEMCORE Core;
|
---|
95 | /** The keycode. */
|
---|
96 | uint32_t idUsage;
|
---|
97 | } DRVKBDQUEUEITEM, *PDRVKBDQUEUEITEM;
|
---|
98 |
|
---|
99 |
|
---|
100 | /*********************************************************************************************************************************
|
---|
101 | * Global Variables *
|
---|
102 | *********************************************************************************************************************************/
|
---|
103 |
|
---|
104 | /** Lookup table for converting PC/XT scan codes to USB HID usage codes. */
|
---|
105 | static const uint8_t aScancode2Hid[] =
|
---|
106 | {
|
---|
107 | 0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, /* 00-07 */
|
---|
108 | 0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b, /* 08-1F */
|
---|
109 | 0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c, /* 10-17 */
|
---|
110 | 0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16, /* 18-1F */
|
---|
111 | 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33, /* 20-27 */
|
---|
112 | 0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19, /* 28-2F */
|
---|
113 | 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55, /* 30-37 */
|
---|
114 | 0xe2, 0x2c, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, /* 38-3F */
|
---|
115 | 0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f, /* 40-47 */
|
---|
116 | 0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59, /* 48-4F */
|
---|
117 | 0x5a, 0x5b, 0x62, 0x63, 0x46, 0x00, 0x64, 0x44, /* 50-57 */
|
---|
118 | 0x45, 0x67, 0x00, 0x00, 0x8c, 0x00, 0x00, 0x00, /* 58-5F */
|
---|
119 | 0x00, 0x00, 0x00, 0x00, 0x68, 0x69, 0x6a, 0x6b, /* 60-67 */
|
---|
120 | 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, /* 68-6F */
|
---|
121 | 0x88, 0x91, 0x90, 0x87, 0x00, 0x00, 0x00, 0x00, /* 70-77 */
|
---|
122 | 0x00, 0x8a, 0x00, 0x8b, 0x00, 0x89, 0x85, 0x00 /* 78-7F */
|
---|
123 | };
|
---|
124 |
|
---|
125 | /* Keyboard usage page (07h). */
|
---|
126 | #define KB(key) (RT_MAKE_U32(0, USB_HID_KB_PAGE) | (uint16_t)key)
|
---|
127 | /* Consumer Control usage page (0Ch). */
|
---|
128 | #define CC(key) (RT_MAKE_U32(0, USB_HID_CC_PAGE) | (uint16_t)key)
|
---|
129 | /* Generic Desktop Control usage page (01h). */
|
---|
130 | #define DC(key) (RT_MAKE_U32(0, USB_HID_DC_PAGE) | (uint16_t)key)
|
---|
131 | /* Untranslated/unised, shouldn't be encountered. */
|
---|
132 | #define XX(key) 0
|
---|
133 |
|
---|
134 | /** Lookup table for extended scancodes (arrow keys etc.).
|
---|
135 | * Some of these keys use HID usage pages other than the
|
---|
136 | * standard (07). */
|
---|
137 | static const uint32_t aExtScan2Hid[] =
|
---|
138 | {
|
---|
139 | XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), /* 00-07 */
|
---|
140 | XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), /* 08-1F */
|
---|
141 | CC(0x0B6), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), /* 10-17 */
|
---|
142 | XX(0x000), CC(0x0B5), XX(0x000), XX(0x000), KB(0x058), KB(0x0e4), XX(0x000), XX(0x000), /* 18-1F */
|
---|
143 | CC(0x0E2), CC(0x192), CC(0x0CD), XX(0x000), CC(0x0B7), XX(0x000), XX(0x000), XX(0x000), /* 20-27 */
|
---|
144 | XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), CC(0x0EA), XX(0x000), /* 28-2F */
|
---|
145 | CC(0x0E9), XX(0x000), CC(0x223), XX(0x000), XX(0x000), KB(0x054), XX(0x000), KB(0x046), /* 30-37 */
|
---|
146 | /* Sun-specific keys. Most of the XT codes are made up */
|
---|
147 | KB(0x0e6), XX(0x000), XX(0x000), KB(0x075), KB(0x076), KB(0x077), KB(0x0A3), KB(0x078), /* 38-3F */
|
---|
148 | KB(0x080), KB(0x081), KB(0x082), KB(0x079), XX(0x000), XX(0x000), KB(0x048), KB(0x04a), /* 40-47 */
|
---|
149 | KB(0x052), KB(0x04b), XX(0x000), KB(0x050), XX(0x000), KB(0x04f), XX(0x000), KB(0x04d), /* 48-4F */
|
---|
150 | KB(0x051), KB(0x04e), KB(0x049), KB(0x04c), XX(0x000), XX(0x000), XX(0x000), XX(0x000), /* 50-57 */
|
---|
151 | XX(0x000), XX(0x000), XX(0x000), KB(0x0e3), KB(0x0e7), KB(0x065), KB(0x066), DC(0x082), /* 58-5F */
|
---|
152 | XX(0x000), XX(0x000), XX(0x000), DC(0x083), XX(0x000), CC(0x221), CC(0x22A), CC(0x227), /* 60-67 */
|
---|
153 | CC(0x226), CC(0x225), CC(0x224), CC(0x194), CC(0x18A), CC(0x183), XX(0x000), XX(0x000), /* 68-6F */
|
---|
154 | XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), /* 70-77 */
|
---|
155 | XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000), XX(0x000) /* 78-7F */
|
---|
156 | };
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Convert a PC scan code to a USB HID usage byte.
|
---|
160 | *
|
---|
161 | * @param state Current state of the translator (scan_state_t).
|
---|
162 | * @param scanCode Incoming scan code.
|
---|
163 | * @param pUsage Pointer to usage; high bit set for key up events. The
|
---|
164 | * contents are only valid if returned state is SS_IDLE.
|
---|
165 | *
|
---|
166 | * @return scan_state_t New state of the translator.
|
---|
167 | */
|
---|
168 | static scan_state_t ScancodeToHidUsage(scan_state_t state, uint8_t scanCode, uint32_t *pUsage)
|
---|
169 | {
|
---|
170 | uint32_t keyUp;
|
---|
171 | uint32_t usagePg;
|
---|
172 | uint8_t usage;
|
---|
173 |
|
---|
174 | Assert(pUsage);
|
---|
175 |
|
---|
176 | /* Isolate the scan code and key break flag. */
|
---|
177 | keyUp = (scanCode & 0x80) ? PDMIKBDPORT_KEY_UP : 0;
|
---|
178 |
|
---|
179 | switch (state) {
|
---|
180 | case SS_IDLE:
|
---|
181 | if (scanCode == 0xE0) {
|
---|
182 | state = SS_EXT;
|
---|
183 | } else if (scanCode == 0xE1) {
|
---|
184 | state = SS_EXT1;
|
---|
185 | } else {
|
---|
186 | usage = aScancode2Hid[scanCode & 0x7F];
|
---|
187 | AssertMsg(usage, ("SS_IDLE: scanCode=%02X\n", scanCode));
|
---|
188 | *pUsage = usage | keyUp | HID_PG_KB_BITS;
|
---|
189 | /* Remain in SS_IDLE state. */
|
---|
190 | }
|
---|
191 | break;
|
---|
192 | case SS_EXT:
|
---|
193 | usagePg = aExtScan2Hid[scanCode & 0x7F];
|
---|
194 | AssertMsg(usagePg, ("SS_EXT: scanCode=%02X\n", scanCode));
|
---|
195 | *pUsage = usagePg | keyUp;
|
---|
196 | state = SS_IDLE;
|
---|
197 | break;
|
---|
198 | case SS_EXT1:
|
---|
199 | /* The sequence is E1 1D 45 E1 9D C5. We take the easy way out and remain
|
---|
200 | * in the SS_EXT1 state until 45 or C5 is received.
|
---|
201 | */
|
---|
202 | if ((scanCode & 0x7F) == 0x45) {
|
---|
203 | *pUsage = 0x48 | HID_PG_KB_BITS;
|
---|
204 | if (scanCode == 0xC5)
|
---|
205 | *pUsage |= keyUp;
|
---|
206 | state = SS_IDLE;
|
---|
207 | }
|
---|
208 | /* Else remain in SS_EXT1 state. */
|
---|
209 | break;
|
---|
210 | }
|
---|
211 | return state;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /* -=-=-=-=- IBase -=-=-=-=- */
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
219 | */
|
---|
220 | static DECLCALLBACK(void *) drvKbdQueueQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
221 | {
|
---|
222 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
223 | PDRVKBDQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
|
---|
224 |
|
---|
225 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
226 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDCONNECTOR, &pThis->IConnector);
|
---|
227 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDPORT, &pThis->IPort);
|
---|
228 | return NULL;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /* -=-=-=-=- IKeyboardPort -=-=-=-=- */
|
---|
233 |
|
---|
234 | /** Converts a pointer to DRVKBDQUEUE::IPort to a DRVKBDQUEUE pointer. */
|
---|
235 | #define IKEYBOARDPORT_2_DRVKBDQUEUE(pInterface) ( (PDRVKBDQUEUE)((char *)(pInterface) - RT_UOFFSETOF(DRVKBDQUEUE, IPort)) )
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * @interface_method_impl{PDMIKEYBOARDPORT,pfnPutEventScan}
|
---|
240 | *
|
---|
241 | * Because of the event queueing the EMT context requirement is lifted.
|
---|
242 | * @thread Any thread.
|
---|
243 | */
|
---|
244 | static DECLCALLBACK(int) drvKbdQueuePutEventScan(PPDMIKEYBOARDPORT pInterface, uint8_t u8ScanCode)
|
---|
245 | {
|
---|
246 | PDRVKBDQUEUE pDrv = IKEYBOARDPORT_2_DRVKBDQUEUE(pInterface);
|
---|
247 | /* Ignore any attempt to send events if queue is inactive. */
|
---|
248 | if (pDrv->fInactive)
|
---|
249 | return VINF_SUCCESS;
|
---|
250 |
|
---|
251 | uint32_t idUsage = 0;
|
---|
252 | pDrv->XlatState = ScancodeToHidUsage(pDrv->XlatState, u8ScanCode, &idUsage);
|
---|
253 |
|
---|
254 | if (pDrv->XlatState == SS_IDLE)
|
---|
255 | {
|
---|
256 | PDRVKBDQUEUEITEM pItem = (PDRVKBDQUEUEITEM)PDMDrvHlpQueueAlloc(pDrv->pDrvIns, pDrv->hQueue);
|
---|
257 | if (pItem)
|
---|
258 | {
|
---|
259 | /*
|
---|
260 | * Work around incredibly poorly desgined Korean keyboards which
|
---|
261 | * only send break events for Hangul/Hanja keys -- convert a lone
|
---|
262 | * key up into a key up/key down sequence.
|
---|
263 | */
|
---|
264 | if ( (idUsage == (PDMIKBDPORT_KEY_UP | HID_PG_KB_BITS | 0x90))
|
---|
265 | || (idUsage == (PDMIKBDPORT_KEY_UP | HID_PG_KB_BITS | 0x91)))
|
---|
266 | {
|
---|
267 | PDRVKBDQUEUEITEM pItem2 = (PDRVKBDQUEUEITEM)PDMDrvHlpQueueAlloc(pDrv->pDrvIns, pDrv->hQueue);
|
---|
268 | /*
|
---|
269 | * NB: If there's no room in the queue, we will drop the faked
|
---|
270 | * key down event. Probably less bad than the alternatives.
|
---|
271 | */
|
---|
272 | if (pItem2)
|
---|
273 | {
|
---|
274 | /* Manufacture a key down event. */
|
---|
275 | pItem2->idUsage = idUsage & ~PDMIKBDPORT_KEY_UP;
|
---|
276 | PDMDrvHlpQueueInsert(pDrv->pDrvIns, pDrv->hQueue, &pItem2->Core);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | pItem->idUsage = idUsage;
|
---|
281 | PDMDrvHlpQueueInsert(pDrv->pDrvIns, pDrv->hQueue, &pItem->Core);
|
---|
282 |
|
---|
283 | return VINF_SUCCESS;
|
---|
284 | }
|
---|
285 | if (!pDrv->fSuspended)
|
---|
286 | AssertMsgFailed(("drvKbdQueuePutEventScan: Queue is full!!!!\n"));
|
---|
287 | return VERR_PDM_NO_QUEUE_ITEMS;
|
---|
288 | }
|
---|
289 | return VINF_SUCCESS;
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * @interface_method_impl{PDMIKEYBOARDPORT,pfnPutEventHid}
|
---|
295 | *
|
---|
296 | * Because of the event queueing the EMT context requirement is lifted.
|
---|
297 | * @thread Any thread.
|
---|
298 | */
|
---|
299 | static DECLCALLBACK(int) drvKbdQueuePutEventHid(PPDMIKEYBOARDPORT pInterface, uint32_t idUsage)
|
---|
300 | {
|
---|
301 | PDRVKBDQUEUE pDrv = IKEYBOARDPORT_2_DRVKBDQUEUE(pInterface);
|
---|
302 | /* Ignore any attempt to send events if queue is inactive. */
|
---|
303 | if (pDrv->fInactive)
|
---|
304 | return VINF_SUCCESS;
|
---|
305 |
|
---|
306 | PDRVKBDQUEUEITEM pItem = (PDRVKBDQUEUEITEM)PDMDrvHlpQueueAlloc(pDrv->pDrvIns, pDrv->hQueue);
|
---|
307 | if (pItem)
|
---|
308 | {
|
---|
309 | pItem->idUsage = idUsage;
|
---|
310 | PDMDrvHlpQueueInsert(pDrv->pDrvIns, pDrv->hQueue, &pItem->Core);
|
---|
311 |
|
---|
312 | return VINF_SUCCESS;
|
---|
313 | }
|
---|
314 | AssertMsg(pDrv->fSuspended, ("drvKbdQueuePutEventHid: Queue is full!!!!\n"));
|
---|
315 | return VERR_PDM_NO_QUEUE_ITEMS;
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * @interface_method_impl{PDMIKEYBOARDPORT,pfnReleaseKeys}
|
---|
321 | *
|
---|
322 | * Because of the event queueing the EMT context requirement is lifted.
|
---|
323 | * @thread Any thread.
|
---|
324 | */
|
---|
325 | static DECLCALLBACK(int) drvKbdQueueReleaseKeys(PPDMIKEYBOARDPORT pInterface)
|
---|
326 | {
|
---|
327 | PDRVKBDQUEUE pDrv = IKEYBOARDPORT_2_DRVKBDQUEUE(pInterface);
|
---|
328 |
|
---|
329 | /* Ignore any attempt to send events if queue is inactive. */
|
---|
330 | if (pDrv->fInactive)
|
---|
331 | return VINF_SUCCESS;
|
---|
332 |
|
---|
333 | PDRVKBDQUEUEITEM pItem = (PDRVKBDQUEUEITEM)PDMDrvHlpQueueAlloc(pDrv->pDrvIns, pDrv->hQueue);
|
---|
334 | if (pItem)
|
---|
335 | {
|
---|
336 | /* Send a special key event that forces all keys to be released.
|
---|
337 | * Goes through the queue so that it would take effect only after
|
---|
338 | * any key events that might already be queued up.
|
---|
339 | */
|
---|
340 | pItem->idUsage = PDMIKBDPORT_RELEASE_KEYS | HID_PG_KB_BITS;
|
---|
341 | PDMDrvHlpQueueInsert(pDrv->pDrvIns, pDrv->hQueue, &pItem->Core);
|
---|
342 |
|
---|
343 | return VINF_SUCCESS;
|
---|
344 | }
|
---|
345 | AssertMsg(pDrv->fSuspended, ("drvKbdQueueReleaseKeys: Queue is full!!!!\n"));
|
---|
346 | return VERR_PDM_NO_QUEUE_ITEMS;
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | /* -=-=-=-=- IConnector -=-=-=-=- */
|
---|
351 |
|
---|
352 | #define PPDMIKEYBOARDCONNECTOR_2_DRVKBDQUEUE(pInterface) ( (PDRVKBDQUEUE)((char *)(pInterface) - RT_UOFFSETOF(DRVKBDQUEUE, IConnector)) )
|
---|
353 |
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Pass LED status changes from the guest thru to the frontend driver.
|
---|
357 | *
|
---|
358 | * @param pInterface Pointer to the keyboard connector interface structure.
|
---|
359 | * @param enmLeds The new LED mask.
|
---|
360 | */
|
---|
361 | static DECLCALLBACK(void) drvKbdPassThruLedsChange(PPDMIKEYBOARDCONNECTOR pInterface, PDMKEYBLEDS enmLeds)
|
---|
362 | {
|
---|
363 | PDRVKBDQUEUE pDrv = PPDMIKEYBOARDCONNECTOR_2_DRVKBDQUEUE(pInterface);
|
---|
364 | pDrv->pDownConnector->pfnLedStatusChange(pDrv->pDownConnector, enmLeds);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Pass keyboard state changes from the guest thru to the frontend driver.
|
---|
369 | *
|
---|
370 | * @param pInterface Pointer to the keyboard connector interface structure.
|
---|
371 | * @param fActive The new active/inactive state.
|
---|
372 | */
|
---|
373 | static DECLCALLBACK(void) drvKbdPassThruSetActive(PPDMIKEYBOARDCONNECTOR pInterface, bool fActive)
|
---|
374 | {
|
---|
375 | PDRVKBDQUEUE pDrv = PPDMIKEYBOARDCONNECTOR_2_DRVKBDQUEUE(pInterface);
|
---|
376 |
|
---|
377 | AssertPtr(pDrv->pDownConnector->pfnSetActive);
|
---|
378 | pDrv->pDownConnector->pfnSetActive(pDrv->pDownConnector, fActive);
|
---|
379 | }
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Flush the keyboard queue if there are pending events.
|
---|
383 | *
|
---|
384 | * @param pInterface Pointer to the keyboard connector interface structure.
|
---|
385 | */
|
---|
386 | static DECLCALLBACK(void) drvKbdFlushQueue(PPDMIKEYBOARDCONNECTOR pInterface)
|
---|
387 | {
|
---|
388 | PDRVKBDQUEUE pDrv = PPDMIKEYBOARDCONNECTOR_2_DRVKBDQUEUE(pInterface);
|
---|
389 |
|
---|
390 | PDMDrvHlpQueueFlushIfNecessary(pDrv->pDrvIns, pDrv->hQueue);
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /* -=-=-=-=- queue -=-=-=-=- */
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * Queue callback for processing a queued item.
|
---|
398 | *
|
---|
399 | * @returns Success indicator.
|
---|
400 | * If false the item will not be removed and the flushing will stop.
|
---|
401 | * @param pDrvIns The driver instance.
|
---|
402 | * @param pItemCore Pointer to the queue item to process.
|
---|
403 | */
|
---|
404 | static DECLCALLBACK(bool) drvKbdQueueConsumer(PPDMDRVINS pDrvIns, PPDMQUEUEITEMCORE pItemCore)
|
---|
405 | {
|
---|
406 | PDRVKBDQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
|
---|
407 | PDRVKBDQUEUEITEM pItem = (PDRVKBDQUEUEITEM)pItemCore;
|
---|
408 | int rc = pThis->pUpPort->pfnPutEventHid(pThis->pUpPort, pItem->idUsage);
|
---|
409 | return rc != VERR_TRY_AGAIN;
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | /* -=-=-=-=- driver interface -=-=-=-=- */
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Power On notification.
|
---|
417 | *
|
---|
418 | * @param pDrvIns The drive instance data.
|
---|
419 | */
|
---|
420 | static DECLCALLBACK(void) drvKbdQueuePowerOn(PPDMDRVINS pDrvIns)
|
---|
421 | {
|
---|
422 | PDRVKBDQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
|
---|
423 | pThis->fInactive = false;
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Reset notification.
|
---|
429 | *
|
---|
430 | * @param pDrvIns The drive instance data.
|
---|
431 | */
|
---|
432 | static DECLCALLBACK(void) drvKbdQueueReset(PPDMDRVINS pDrvIns)
|
---|
433 | {
|
---|
434 | //PDRVKBDQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
|
---|
435 | /** @todo purge the queue on reset. */
|
---|
436 | RT_NOREF(pDrvIns);
|
---|
437 | }
|
---|
438 |
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Suspend notification.
|
---|
442 | *
|
---|
443 | * @param pDrvIns The drive instance data.
|
---|
444 | */
|
---|
445 | static DECLCALLBACK(void) drvKbdQueueSuspend(PPDMDRVINS pDrvIns)
|
---|
446 | {
|
---|
447 | PDRVKBDQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
|
---|
448 | pThis->fSuspended = true;
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * Resume notification.
|
---|
454 | *
|
---|
455 | * @param pDrvIns The drive instance data.
|
---|
456 | */
|
---|
457 | static DECLCALLBACK(void) drvKbdQueueResume(PPDMDRVINS pDrvIns)
|
---|
458 | {
|
---|
459 | PDRVKBDQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
|
---|
460 | pThis->fInactive = false;
|
---|
461 | pThis->fSuspended = false;
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Power Off notification.
|
---|
467 | *
|
---|
468 | * @param pDrvIns The drive instance data.
|
---|
469 | */
|
---|
470 | static DECLCALLBACK(void) drvKbdQueuePowerOff(PPDMDRVINS pDrvIns)
|
---|
471 | {
|
---|
472 | PDRVKBDQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
|
---|
473 | pThis->fInactive = true;
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Construct a keyboard driver instance.
|
---|
479 | *
|
---|
480 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
481 | */
|
---|
482 | static DECLCALLBACK(int) drvKbdQueueConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
483 | {
|
---|
484 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
485 | PDRVKBDQUEUE pDrv = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
|
---|
486 | PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
|
---|
487 |
|
---|
488 | LogFlow(("drvKbdQueueConstruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
489 |
|
---|
490 |
|
---|
491 | /*
|
---|
492 | * Validate configuration.
|
---|
493 | */
|
---|
494 | PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "QueueSize|Interval", "");
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * Init basic data members and interfaces.
|
---|
498 | */
|
---|
499 | pDrv->pDrvIns = pDrvIns;
|
---|
500 | pDrv->fInactive = true;
|
---|
501 | pDrv->fSuspended = false;
|
---|
502 | pDrv->XlatState = SS_IDLE;
|
---|
503 | /* IBase. */
|
---|
504 | pDrvIns->IBase.pfnQueryInterface = drvKbdQueueQueryInterface;
|
---|
505 | /* IKeyboardConnector. */
|
---|
506 | pDrv->IConnector.pfnLedStatusChange = drvKbdPassThruLedsChange;
|
---|
507 | pDrv->IConnector.pfnSetActive = drvKbdPassThruSetActive;
|
---|
508 | pDrv->IConnector.pfnFlushQueue = drvKbdFlushQueue;
|
---|
509 | /* IKeyboardPort. */
|
---|
510 | pDrv->IPort.pfnPutEventScan = drvKbdQueuePutEventScan;
|
---|
511 | pDrv->IPort.pfnPutEventHid = drvKbdQueuePutEventHid;
|
---|
512 | pDrv->IPort.pfnReleaseKeys = drvKbdQueueReleaseKeys;
|
---|
513 |
|
---|
514 | /*
|
---|
515 | * Get the IKeyboardPort interface of the above driver/device.
|
---|
516 | */
|
---|
517 | pDrv->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIKEYBOARDPORT);
|
---|
518 | AssertMsgReturn(pDrv->pUpPort, ("Configuration error: No keyboard port interface above!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
|
---|
519 |
|
---|
520 | /*
|
---|
521 | * Attach driver below and query it's connector interface.
|
---|
522 | */
|
---|
523 | PPDMIBASE pDownBase;
|
---|
524 | int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pDownBase);
|
---|
525 | AssertMsgRCReturn(rc, ("Failed to attach driver below us! rc=%Rra\n", rc), rc);
|
---|
526 |
|
---|
527 | pDrv->pDownConnector = PDMIBASE_QUERY_INTERFACE(pDownBase, PDMIKEYBOARDCONNECTOR);
|
---|
528 | AssertMsgReturn(pDrv->pDownConnector, ("Configuration error: No keyboard connector interface below!\n"),
|
---|
529 | VERR_PDM_MISSING_INTERFACE_BELOW);
|
---|
530 |
|
---|
531 | /*
|
---|
532 | * Create the queue.
|
---|
533 | */
|
---|
534 | uint32_t cMilliesInterval = 0;
|
---|
535 | rc = pHlp->pfnCFGMQueryU32(pCfg, "Interval", &cMilliesInterval);
|
---|
536 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
537 | cMilliesInterval = 0;
|
---|
538 | else
|
---|
539 | AssertMsgRCReturn(rc, ("Configuration error: 32-bit \"Interval\" -> rc=%Rrc\n", rc), rc);
|
---|
540 |
|
---|
541 | uint32_t cItems = 0;
|
---|
542 | rc = pHlp->pfnCFGMQueryU32(pCfg, "QueueSize", &cItems);
|
---|
543 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
544 | cItems = 128;
|
---|
545 | else
|
---|
546 | AssertMsgRCReturn(rc, ("Configuration error: 32-bit \"QueueSize\" -> rc=%Rrc\n", rc), rc);
|
---|
547 |
|
---|
548 | rc = PDMDrvHlpQueueCreate(pDrvIns, sizeof(DRVKBDQUEUEITEM), cItems, cMilliesInterval,
|
---|
549 | drvKbdQueueConsumer, "Keyboard", &pDrv->hQueue);
|
---|
550 | AssertMsgRCReturn(rc, ("Failed to create driver: cItems=%d cMilliesInterval=%d rc=%Rrc\n", cItems, cMilliesInterval, rc), rc);
|
---|
551 |
|
---|
552 | return VINF_SUCCESS;
|
---|
553 | }
|
---|
554 |
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Keyboard queue driver registration record.
|
---|
558 | */
|
---|
559 | const PDMDRVREG g_DrvKeyboardQueue =
|
---|
560 | {
|
---|
561 | /* u32Version */
|
---|
562 | PDM_DRVREG_VERSION,
|
---|
563 | /* szName */
|
---|
564 | "KeyboardQueue",
|
---|
565 | /* szRCMod */
|
---|
566 | "",
|
---|
567 | /* szR0Mod */
|
---|
568 | "",
|
---|
569 | /* pszDescription */
|
---|
570 | "Keyboard queue driver to plug in between the key source and the device to do queueing and inter-thread transport.",
|
---|
571 | /* fFlags */
|
---|
572 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
573 | /* fClass. */
|
---|
574 | PDM_DRVREG_CLASS_KEYBOARD,
|
---|
575 | /* cMaxInstances */
|
---|
576 | ~0U,
|
---|
577 | /* cbInstance */
|
---|
578 | sizeof(DRVKBDQUEUE),
|
---|
579 | /* pfnConstruct */
|
---|
580 | drvKbdQueueConstruct,
|
---|
581 | /* pfnRelocate */
|
---|
582 | NULL,
|
---|
583 | /* pfnDestruct */
|
---|
584 | NULL,
|
---|
585 | /* pfnIOCtl */
|
---|
586 | NULL,
|
---|
587 | /* pfnPowerOn */
|
---|
588 | drvKbdQueuePowerOn,
|
---|
589 | /* pfnReset */
|
---|
590 | drvKbdQueueReset,
|
---|
591 | /* pfnSuspend */
|
---|
592 | drvKbdQueueSuspend,
|
---|
593 | /* pfnResume */
|
---|
594 | drvKbdQueueResume,
|
---|
595 | /* pfnAttach */
|
---|
596 | NULL,
|
---|
597 | /* pfnDetach */
|
---|
598 | NULL,
|
---|
599 | /* pfnPowerOff */
|
---|
600 | drvKbdQueuePowerOff,
|
---|
601 | /* pfnSoftReset */
|
---|
602 | NULL,
|
---|
603 | /* u32EndVersion */
|
---|
604 | PDM_DRVREG_VERSION
|
---|
605 | };
|
---|
606 |
|
---|