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