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