[27969] | 1 | /* $Id: UsbKbd.cpp 57393 2015-08-17 15:02:05Z vboxsync $ */
|
---|
[26473] | 2 | /** @file
|
---|
[27969] | 3 | * UsbKbd - USB Human Interface Device Emulation, Keyboard.
|
---|
[26473] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[56292] | 7 | * Copyright (C) 2007-2015 Oracle Corporation
|
---|
[26473] | 8 | *
|
---|
[27901] | 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.
|
---|
[26473] | 16 | */
|
---|
| 17 |
|
---|
[51546] | 18 | /** @page pg_usb_kbd USB Keyboard Device Emulation.
|
---|
| 19 | *
|
---|
| 20 | * This module implements a standard USB keyboard which uses the boot
|
---|
| 21 | * interface. The keyboard sends reports which have room for up to six
|
---|
| 22 | * normal keys and all standard modifier keys. A report always reflects the
|
---|
| 23 | * current state of the keyboard and indicates which keys are held down.
|
---|
| 24 | *
|
---|
| 25 | * Software normally utilizes the keyboard's interrupt endpoint to request
|
---|
| 26 | * reports to be sent whenever a state change occurs. However, reports can
|
---|
| 27 | * also be sent whenever an interrupt transfer is initiated (the keyboard is
|
---|
| 28 | * not "idle") or requested via the control endpoint (polling).
|
---|
| 29 | *
|
---|
| 30 | * Because turnaround on USB is relatively slow, the keyboard often ends up
|
---|
| 31 | * in a situation where new input arrived but there is no URB available
|
---|
| 32 | * where a report could be written to. The PDM queue maintained by the
|
---|
| 33 | * keyboard driver is utilized to provide buffering and hold incoming events
|
---|
| 34 | * until they can be passed along. The USB keyboard can effectively buffer
|
---|
| 35 | * up to one event.
|
---|
| 36 | *
|
---|
| 37 | * If there is a pending event and a new URB becomes available, a report is
|
---|
| 38 | * built and the keyboard queue is flushed. This ensures that queued events
|
---|
| 39 | * are processed as quickly as possible.
|
---|
| 40 | *
|
---|
| 41 | *
|
---|
| 42 | * References:
|
---|
| 43 | *
|
---|
| 44 | * Device Class Definition for Human Interface Devices (HID), Version 1.11
|
---|
| 45 | *
|
---|
| 46 | */
|
---|
| 47 |
|
---|
[57358] | 48 |
|
---|
| 49 | /*********************************************************************************************************************************
|
---|
| 50 | * Header Files *
|
---|
| 51 | *********************************************************************************************************************************/
|
---|
[27838] | 52 | #define LOG_GROUP LOG_GROUP_USB_KBD
|
---|
[35346] | 53 | #include <VBox/vmm/pdmusb.h>
|
---|
[26473] | 54 | #include <VBox/log.h>
|
---|
| 55 | #include <VBox/err.h>
|
---|
| 56 | #include <iprt/assert.h>
|
---|
| 57 | #include <iprt/critsect.h>
|
---|
| 58 | #include <iprt/mem.h>
|
---|
| 59 | #include <iprt/semaphore.h>
|
---|
| 60 | #include <iprt/string.h>
|
---|
| 61 | #include <iprt/uuid.h>
|
---|
[35353] | 62 | #include "VBoxDD.h"
|
---|
[26473] | 63 |
|
---|
| 64 |
|
---|
[57358] | 65 | /*********************************************************************************************************************************
|
---|
| 66 | * Defined Constants And Macros *
|
---|
| 67 | *********************************************************************************************************************************/
|
---|
[26473] | 68 | /** @name USB HID string IDs
|
---|
| 69 | * @{ */
|
---|
| 70 | #define USBHID_STR_ID_MANUFACTURER 1
|
---|
| 71 | #define USBHID_STR_ID_PRODUCT 2
|
---|
| 72 | /** @} */
|
---|
| 73 |
|
---|
| 74 | /** @name USB HID specific descriptor types
|
---|
| 75 | * @{ */
|
---|
[37388] | 76 | #define DT_IF_HID_DESCRIPTOR 0x21
|
---|
[26473] | 77 | #define DT_IF_HID_REPORT 0x22
|
---|
| 78 | /** @} */
|
---|
| 79 |
|
---|
| 80 | /** @name USB HID vendor and product IDs
|
---|
| 81 | * @{ */
|
---|
| 82 | #define VBOX_USB_VENDOR 0x80EE
|
---|
| 83 | #define USBHID_PID_KEYBOARD 0x0010
|
---|
| 84 | /** @} */
|
---|
| 85 |
|
---|
| 86 | /** @name USB HID class specific requests
|
---|
| 87 | * @{ */
|
---|
| 88 | #define HID_REQ_GET_REPORT 0x01
|
---|
| 89 | #define HID_REQ_GET_IDLE 0x02
|
---|
| 90 | #define HID_REQ_SET_REPORT 0x09
|
---|
| 91 | #define HID_REQ_SET_IDLE 0x0A
|
---|
| 92 | /** @} */
|
---|
| 93 |
|
---|
[30507] | 94 | /** @name USB HID additional constants
|
---|
| 95 | * @{ */
|
---|
| 96 | /** The highest USB usage code reported by the VBox emulated keyboard */
|
---|
[34028] | 97 | #define VBOX_USB_MAX_USAGE_CODE 0xE7
|
---|
| 98 | /** The size of an array needed to store all USB usage codes */
|
---|
| 99 | #define VBOX_USB_USAGE_ARRAY_SIZE (VBOX_USB_MAX_USAGE_CODE + 1)
|
---|
[30507] | 100 | #define USBHID_USAGE_ROLL_OVER 1
|
---|
[44764] | 101 | /** The usage code of the first modifier key. */
|
---|
| 102 | #define USBHID_MODIFIER_FIRST 0xE0
|
---|
| 103 | /** The usage code of the last modifier key. */
|
---|
| 104 | #define USBHID_MODIFIER_LAST 0xE7
|
---|
[30507] | 105 | /** @} */
|
---|
| 106 |
|
---|
[26473] | 107 |
|
---|
[57358] | 108 | /*********************************************************************************************************************************
|
---|
| 109 | * Structures and Typedefs *
|
---|
| 110 | *********************************************************************************************************************************/
|
---|
| 111 |
|
---|
[26473] | 112 | /**
|
---|
| 113 | * The USB HID request state.
|
---|
| 114 | */
|
---|
| 115 | typedef enum USBHIDREQSTATE
|
---|
| 116 | {
|
---|
| 117 | /** Invalid status. */
|
---|
| 118 | USBHIDREQSTATE_INVALID = 0,
|
---|
| 119 | /** Ready to receive a new read request. */
|
---|
| 120 | USBHIDREQSTATE_READY,
|
---|
| 121 | /** Have (more) data for the host. */
|
---|
| 122 | USBHIDREQSTATE_DATA_TO_HOST,
|
---|
| 123 | /** Waiting to supply status information to the host. */
|
---|
| 124 | USBHIDREQSTATE_STATUS,
|
---|
| 125 | /** The end of the valid states. */
|
---|
| 126 | USBHIDREQSTATE_END
|
---|
| 127 | } USBHIDREQSTATE;
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Endpoint status data.
|
---|
| 132 | */
|
---|
| 133 | typedef struct USBHIDEP
|
---|
| 134 | {
|
---|
| 135 | bool fHalted;
|
---|
| 136 | } USBHIDEP;
|
---|
| 137 | /** Pointer to the endpoint status. */
|
---|
| 138 | typedef USBHIDEP *PUSBHIDEP;
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * A URB queue.
|
---|
| 143 | */
|
---|
| 144 | typedef struct USBHIDURBQUEUE
|
---|
| 145 | {
|
---|
| 146 | /** The head pointer. */
|
---|
| 147 | PVUSBURB pHead;
|
---|
| 148 | /** Where to insert the next entry. */
|
---|
| 149 | PVUSBURB *ppTail;
|
---|
| 150 | } USBHIDURBQUEUE;
|
---|
| 151 | /** Pointer to a URB queue. */
|
---|
| 152 | typedef USBHIDURBQUEUE *PUSBHIDURBQUEUE;
|
---|
| 153 | /** Pointer to a const URB queue. */
|
---|
| 154 | typedef USBHIDURBQUEUE const *PCUSBHIDURBQUEUE;
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * The USB HID report structure for regular keys.
|
---|
| 159 | */
|
---|
| 160 | typedef struct USBHIDK_REPORT
|
---|
| 161 | {
|
---|
[27969] | 162 | uint8_t ShiftState; /**< Modifier keys bitfield */
|
---|
| 163 | uint8_t Reserved; /**< Currently unused */
|
---|
| 164 | uint8_t aKeys[6]; /**< Normal keys */
|
---|
[26473] | 165 | } USBHIDK_REPORT, *PUSBHIDK_REPORT;
|
---|
| 166 |
|
---|
| 167 | /**
|
---|
| 168 | * The USB HID instance data.
|
---|
| 169 | */
|
---|
| 170 | typedef struct USBHID
|
---|
| 171 | {
|
---|
| 172 | /** Pointer back to the PDM USB Device instance structure. */
|
---|
| 173 | PPDMUSBINS pUsbIns;
|
---|
| 174 | /** Critical section protecting the device state. */
|
---|
[27969] | 175 | RTCRITSECT CritSect;
|
---|
[26473] | 176 |
|
---|
| 177 | /** The current configuration.
|
---|
| 178 | * (0 - default, 1 - the one supported configuration, i.e configured.) */
|
---|
| 179 | uint8_t bConfigurationValue;
|
---|
| 180 | /** USB HID Idle value..
|
---|
| 181 | * (0 - only report state change, !=0 - report in bIdle * 4ms intervals.) */
|
---|
| 182 | uint8_t bIdle;
|
---|
| 183 | /** Endpoint 0 is the default control pipe, 1 is the dev->host interrupt one. */
|
---|
| 184 | USBHIDEP aEps[2];
|
---|
| 185 | /** The state of the HID (state machine).*/
|
---|
| 186 | USBHIDREQSTATE enmState;
|
---|
| 187 |
|
---|
| 188 | /** Pending to-host queue.
|
---|
| 189 | * The URBs waiting here are waiting for data to become available.
|
---|
| 190 | */
|
---|
| 191 | USBHIDURBQUEUE ToHostQueue;
|
---|
| 192 |
|
---|
| 193 | /** Done queue
|
---|
| 194 | * The URBs stashed here are waiting to be reaped. */
|
---|
| 195 | USBHIDURBQUEUE DoneQueue;
|
---|
| 196 | /** Signalled when adding an URB to the done queue and fHaveDoneQueueWaiter
|
---|
| 197 | * is set. */
|
---|
| 198 | RTSEMEVENT hEvtDoneQueue;
|
---|
| 199 | /** Someone is waiting on the done queue. */
|
---|
| 200 | bool fHaveDoneQueueWaiter;
|
---|
[27969] | 201 | /** If device has pending changes. */
|
---|
[27890] | 202 | bool fHasPendingChanges;
|
---|
[30507] | 203 | /** Currently depressed keys */
|
---|
[34028] | 204 | uint8_t abDepressedKeys[VBOX_USB_USAGE_ARRAY_SIZE];
|
---|
[26473] | 205 |
|
---|
| 206 | /**
|
---|
| 207 | * Keyboard port - LUN#0.
|
---|
| 208 | *
|
---|
| 209 | * @implements PDMIBASE
|
---|
| 210 | * @implements PDMIKEYBOARDPORT
|
---|
| 211 | */
|
---|
| 212 | struct
|
---|
| 213 | {
|
---|
| 214 | /** The base interface for the keyboard port. */
|
---|
| 215 | PDMIBASE IBase;
|
---|
| 216 | /** The keyboard port base interface. */
|
---|
| 217 | PDMIKEYBOARDPORT IPort;
|
---|
| 218 |
|
---|
| 219 | /** The base interface of the attached keyboard driver. */
|
---|
| 220 | R3PTRTYPE(PPDMIBASE) pDrvBase;
|
---|
| 221 | /** The keyboard interface of the attached keyboard driver. */
|
---|
| 222 | R3PTRTYPE(PPDMIKEYBOARDCONNECTOR) pDrv;
|
---|
| 223 | } Lun0;
|
---|
| 224 | } USBHID;
|
---|
| 225 | /** Pointer to the USB HID instance data. */
|
---|
| 226 | typedef USBHID *PUSBHID;
|
---|
| 227 |
|
---|
[57358] | 228 |
|
---|
| 229 | /*********************************************************************************************************************************
|
---|
| 230 | * Global Variables *
|
---|
| 231 | *********************************************************************************************************************************/
|
---|
[26473] | 232 | static const PDMUSBDESCCACHESTRING g_aUsbHidStrings_en_US[] =
|
---|
| 233 | {
|
---|
| 234 | { USBHID_STR_ID_MANUFACTURER, "VirtualBox" },
|
---|
| 235 | { USBHID_STR_ID_PRODUCT, "USB Keyboard" },
|
---|
| 236 | };
|
---|
| 237 |
|
---|
| 238 | static const PDMUSBDESCCACHELANG g_aUsbHidLanguages[] =
|
---|
| 239 | {
|
---|
| 240 | { 0x0409, RT_ELEMENTS(g_aUsbHidStrings_en_US), g_aUsbHidStrings_en_US }
|
---|
| 241 | };
|
---|
| 242 |
|
---|
| 243 | static const VUSBDESCENDPOINTEX g_aUsbHidEndpointDescs[] =
|
---|
| 244 | {
|
---|
| 245 | {
|
---|
| 246 | {
|
---|
| 247 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
| 248 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
| 249 | /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
|
---|
| 250 | /* .bmAttributes = */ 3 /* interrupt */,
|
---|
| 251 | /* .wMaxPacketSize = */ 8,
|
---|
| 252 | /* .bInterval = */ 10,
|
---|
| 253 | },
|
---|
| 254 | /* .pvMore = */ NULL,
|
---|
| 255 | /* .pvClass = */ NULL,
|
---|
| 256 | /* .cbClass = */ 0
|
---|
| 257 | },
|
---|
| 258 | };
|
---|
| 259 |
|
---|
[27969] | 260 | /** HID report descriptor. */
|
---|
[26473] | 261 | static const uint8_t g_UsbHidReportDesc[] =
|
---|
| 262 | {
|
---|
| 263 | /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
|
---|
| 264 | /* Usage */ 0x09, 0x06, /* Keyboard */
|
---|
| 265 | /* Collection */ 0xA1, 0x01, /* Application */
|
---|
| 266 | /* Usage Page */ 0x05, 0x07, /* Keyboard */
|
---|
| 267 | /* Usage Minimum */ 0x19, 0xE0, /* Left Ctrl Key */
|
---|
| 268 | /* Usage Maximum */ 0x29, 0xE7, /* Right GUI Key */
|
---|
| 269 | /* Logical Minimum */ 0x15, 0x00, /* 0 */
|
---|
| 270 | /* Logical Maximum */ 0x25, 0x01, /* 1 */
|
---|
| 271 | /* Report Count */ 0x95, 0x08, /* 8 */
|
---|
| 272 | /* Report Size */ 0x75, 0x01, /* 1 */
|
---|
| 273 | /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
|
---|
| 274 | /* Report Count */ 0x95, 0x01, /* 1 */
|
---|
| 275 | /* Report Size */ 0x75, 0x08, /* 8 (padding bits) */
|
---|
| 276 | /* Input */ 0x81, 0x01, /* Constant, Array, Absolute, Bit field */
|
---|
| 277 | /* Report Count */ 0x95, 0x05, /* 5 */
|
---|
| 278 | /* Report Size */ 0x75, 0x01, /* 1 */
|
---|
| 279 | /* Usage Page */ 0x05, 0x08, /* LEDs */
|
---|
| 280 | /* Usage Minimum */ 0x19, 0x01, /* Num Lock */
|
---|
| 281 | /* Usage Maximum */ 0x29, 0x05, /* Kana */
|
---|
| 282 | /* Output */ 0x91, 0x02, /* Data, Value, Absolute, Non-volatile,Bit field */
|
---|
| 283 | /* Report Count */ 0x95, 0x01, /* 1 */
|
---|
| 284 | /* Report Size */ 0x75, 0x03, /* 3 */
|
---|
| 285 | /* Output */ 0x91, 0x01, /* Constant, Value, Absolute, Non-volatile, Bit field */
|
---|
| 286 | /* Report Count */ 0x95, 0x06, /* 6 */
|
---|
| 287 | /* Report Size */ 0x75, 0x08, /* 8 */
|
---|
| 288 | /* Logical Minimum */ 0x15, 0x00, /* 0 */
|
---|
[26966] | 289 | /* Logical Maximum */ 0x26, 0xFF,0x00,/* 255 */
|
---|
[26473] | 290 | /* Usage Page */ 0x05, 0x07, /* Keyboard */
|
---|
| 291 | /* Usage Minimum */ 0x19, 0x00, /* 0 */
|
---|
[26948] | 292 | /* Usage Maximum */ 0x29, 0xFF, /* 255 */
|
---|
[26473] | 293 | /* Input */ 0x81, 0x00, /* Data, Array, Absolute, Bit field */
|
---|
| 294 | /* End Collection */ 0xC0,
|
---|
| 295 | };
|
---|
| 296 |
|
---|
[27969] | 297 | /** Additional HID class interface descriptor. */
|
---|
[26495] | 298 | static const uint8_t g_UsbHidIfHidDesc[] =
|
---|
[26473] | 299 | {
|
---|
| 300 | /* .bLength = */ 0x09,
|
---|
| 301 | /* .bDescriptorType = */ 0x21, /* HID */
|
---|
| 302 | /* .bcdHID = */ 0x10, 0x01, /* 1.1 */
|
---|
| 303 | /* .bCountryCode = */ 0x0D, /* International (ISO) */
|
---|
| 304 | /* .bNumDescriptors = */ 1,
|
---|
| 305 | /* .bDescriptorType = */ 0x22, /* Report */
|
---|
| 306 | /* .wDescriptorLength = */ sizeof(g_UsbHidReportDesc), 0x00
|
---|
| 307 | };
|
---|
| 308 |
|
---|
| 309 | static const VUSBDESCINTERFACEEX g_UsbHidInterfaceDesc =
|
---|
| 310 | {
|
---|
| 311 | {
|
---|
| 312 | /* .bLength = */ sizeof(VUSBDESCINTERFACE),
|
---|
| 313 | /* .bDescriptorType = */ VUSB_DT_INTERFACE,
|
---|
| 314 | /* .bInterfaceNumber = */ 0,
|
---|
| 315 | /* .bAlternateSetting = */ 0,
|
---|
| 316 | /* .bNumEndpoints = */ 1,
|
---|
| 317 | /* .bInterfaceClass = */ 3 /* HID */,
|
---|
| 318 | /* .bInterfaceSubClass = */ 1 /* Boot Interface */,
|
---|
| 319 | /* .bInterfaceProtocol = */ 1 /* Keyboard */,
|
---|
| 320 | /* .iInterface = */ 0
|
---|
| 321 | },
|
---|
| 322 | /* .pvMore = */ NULL,
|
---|
| 323 | /* .pvClass = */ &g_UsbHidIfHidDesc,
|
---|
| 324 | /* .cbClass = */ sizeof(g_UsbHidIfHidDesc),
|
---|
[43960] | 325 | &g_aUsbHidEndpointDescs[0],
|
---|
| 326 | /* .pIAD = */ NULL,
|
---|
| 327 | /* .cbIAD = */ 0
|
---|
[26473] | 328 | };
|
---|
| 329 |
|
---|
| 330 | static const VUSBINTERFACE g_aUsbHidInterfaces[] =
|
---|
| 331 | {
|
---|
| 332 | { &g_UsbHidInterfaceDesc, /* .cSettings = */ 1 },
|
---|
| 333 | };
|
---|
| 334 |
|
---|
| 335 | static const VUSBDESCCONFIGEX g_UsbHidConfigDesc =
|
---|
| 336 | {
|
---|
| 337 | {
|
---|
| 338 | /* .bLength = */ sizeof(VUSBDESCCONFIG),
|
---|
| 339 | /* .bDescriptorType = */ VUSB_DT_CONFIG,
|
---|
| 340 | /* .wTotalLength = */ 0 /* recalculated on read */,
|
---|
| 341 | /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidInterfaces),
|
---|
| 342 | /* .bConfigurationValue =*/ 1,
|
---|
| 343 | /* .iConfiguration = */ 0,
|
---|
| 344 | /* .bmAttributes = */ RT_BIT(7),
|
---|
| 345 | /* .MaxPower = */ 50 /* 100mA */
|
---|
| 346 | },
|
---|
[37388] | 347 | NULL, /* pvMore */
|
---|
| 348 | &g_aUsbHidInterfaces[0],
|
---|
| 349 | NULL /* pvOriginal */
|
---|
[26473] | 350 | };
|
---|
| 351 |
|
---|
| 352 | static const VUSBDESCDEVICE g_UsbHidDeviceDesc =
|
---|
| 353 | {
|
---|
| 354 | /* .bLength = */ sizeof(g_UsbHidDeviceDesc),
|
---|
| 355 | /* .bDescriptorType = */ VUSB_DT_DEVICE,
|
---|
| 356 | /* .bcdUsb = */ 0x110, /* 1.1 */
|
---|
| 357 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
| 358 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
| 359 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
| 360 | /* .bMaxPacketSize0 = */ 8,
|
---|
| 361 | /* .idVendor = */ VBOX_USB_VENDOR,
|
---|
| 362 | /* .idProduct = */ USBHID_PID_KEYBOARD,
|
---|
| 363 | /* .bcdDevice = */ 0x0100, /* 1.0 */
|
---|
| 364 | /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
|
---|
| 365 | /* .iProduct = */ USBHID_STR_ID_PRODUCT,
|
---|
| 366 | /* .iSerialNumber = */ 0,
|
---|
| 367 | /* .bNumConfigurations = */ 1
|
---|
| 368 | };
|
---|
| 369 |
|
---|
| 370 | static const PDMUSBDESCCACHE g_UsbHidDescCache =
|
---|
| 371 | {
|
---|
| 372 | /* .pDevice = */ &g_UsbHidDeviceDesc,
|
---|
| 373 | /* .paConfigs = */ &g_UsbHidConfigDesc,
|
---|
| 374 | /* .paLanguages = */ g_aUsbHidLanguages,
|
---|
| 375 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
|
---|
| 376 | /* .fUseCachedDescriptors = */ true,
|
---|
| 377 | /* .fUseCachedStringsDescriptors = */ true
|
---|
| 378 | };
|
---|
| 379 |
|
---|
| 380 |
|
---|
[57358] | 381 | /*********************************************************************************************************************************
|
---|
| 382 | * Internal Functions *
|
---|
| 383 | *********************************************************************************************************************************/
|
---|
[26473] | 384 |
|
---|
[27890] | 385 |
|
---|
| 386 | /**
|
---|
[26473] | 387 | * Initializes an URB queue.
|
---|
| 388 | *
|
---|
| 389 | * @param pQueue The URB queue.
|
---|
| 390 | */
|
---|
| 391 | static void usbHidQueueInit(PUSBHIDURBQUEUE pQueue)
|
---|
| 392 | {
|
---|
| 393 | pQueue->pHead = NULL;
|
---|
| 394 | pQueue->ppTail = &pQueue->pHead;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | /**
|
---|
| 398 | * Inserts an URB at the end of the queue.
|
---|
| 399 | *
|
---|
| 400 | * @param pQueue The URB queue.
|
---|
| 401 | * @param pUrb The URB to insert.
|
---|
| 402 | */
|
---|
| 403 | DECLINLINE(void) usbHidQueueAddTail(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
|
---|
| 404 | {
|
---|
| 405 | pUrb->Dev.pNext = NULL;
|
---|
| 406 | *pQueue->ppTail = pUrb;
|
---|
| 407 | pQueue->ppTail = &pUrb->Dev.pNext;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 |
|
---|
| 411 | /**
|
---|
| 412 | * Unlinks the head of the queue and returns it.
|
---|
| 413 | *
|
---|
| 414 | * @returns The head entry.
|
---|
| 415 | * @param pQueue The URB queue.
|
---|
| 416 | */
|
---|
| 417 | DECLINLINE(PVUSBURB) usbHidQueueRemoveHead(PUSBHIDURBQUEUE pQueue)
|
---|
| 418 | {
|
---|
| 419 | PVUSBURB pUrb = pQueue->pHead;
|
---|
| 420 | if (pUrb)
|
---|
| 421 | {
|
---|
| 422 | PVUSBURB pNext = pUrb->Dev.pNext;
|
---|
| 423 | pQueue->pHead = pNext;
|
---|
| 424 | if (!pNext)
|
---|
| 425 | pQueue->ppTail = &pQueue->pHead;
|
---|
| 426 | else
|
---|
| 427 | pUrb->Dev.pNext = NULL;
|
---|
| 428 | }
|
---|
| 429 | return pUrb;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 |
|
---|
| 433 | /**
|
---|
| 434 | * Removes an URB from anywhere in the queue.
|
---|
| 435 | *
|
---|
| 436 | * @returns true if found, false if not.
|
---|
| 437 | * @param pQueue The URB queue.
|
---|
| 438 | * @param pUrb The URB to remove.
|
---|
| 439 | */
|
---|
| 440 | DECLINLINE(bool) usbHidQueueRemove(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
|
---|
| 441 | {
|
---|
| 442 | PVUSBURB pCur = pQueue->pHead;
|
---|
| 443 | if (pCur == pUrb)
|
---|
| 444 | pQueue->pHead = pUrb->Dev.pNext;
|
---|
| 445 | else
|
---|
| 446 | {
|
---|
| 447 | while (pCur)
|
---|
| 448 | {
|
---|
| 449 | if (pCur->Dev.pNext == pUrb)
|
---|
| 450 | {
|
---|
| 451 | pCur->Dev.pNext = pUrb->Dev.pNext;
|
---|
| 452 | break;
|
---|
| 453 | }
|
---|
| 454 | pCur = pCur->Dev.pNext;
|
---|
| 455 | }
|
---|
| 456 | if (!pCur)
|
---|
| 457 | return false;
|
---|
| 458 | }
|
---|
| 459 | if (!pUrb->Dev.pNext)
|
---|
| 460 | pQueue->ppTail = &pQueue->pHead;
|
---|
| 461 | return true;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 |
|
---|
| 465 | /**
|
---|
| 466 | * Checks if the queue is empty or not.
|
---|
| 467 | *
|
---|
| 468 | * @returns true if it is, false if it isn't.
|
---|
| 469 | * @param pQueue The URB queue.
|
---|
| 470 | */
|
---|
| 471 | DECLINLINE(bool) usbHidQueueIsEmpty(PCUSBHIDURBQUEUE pQueue)
|
---|
| 472 | {
|
---|
| 473 | return pQueue->pHead == NULL;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 |
|
---|
| 477 | /**
|
---|
| 478 | * Links an URB into the done queue.
|
---|
| 479 | *
|
---|
| 480 | * @param pThis The HID instance.
|
---|
| 481 | * @param pUrb The URB.
|
---|
| 482 | */
|
---|
| 483 | static void usbHidLinkDone(PUSBHID pThis, PVUSBURB pUrb)
|
---|
| 484 | {
|
---|
| 485 | usbHidQueueAddTail(&pThis->DoneQueue, pUrb);
|
---|
| 486 |
|
---|
| 487 | if (pThis->fHaveDoneQueueWaiter)
|
---|
| 488 | {
|
---|
| 489 | int rc = RTSemEventSignal(pThis->hEvtDoneQueue);
|
---|
| 490 | AssertRC(rc);
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 |
|
---|
| 495 | /**
|
---|
| 496 | * Completes the URB with a stalled state, halting the pipe.
|
---|
| 497 | */
|
---|
| 498 | static int usbHidCompleteStall(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb, const char *pszWhy)
|
---|
| 499 | {
|
---|
| 500 | Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
|
---|
| 501 |
|
---|
| 502 | pUrb->enmStatus = VUSBSTATUS_STALL;
|
---|
| 503 |
|
---|
| 504 | /** @todo figure out if the stall is global or pipe-specific or both. */
|
---|
| 505 | if (pEp)
|
---|
| 506 | pEp->fHalted = true;
|
---|
| 507 | else
|
---|
| 508 | {
|
---|
[35438] | 509 | pThis->aEps[0].fHalted = true;
|
---|
[26473] | 510 | pThis->aEps[1].fHalted = true;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | usbHidLinkDone(pThis, pUrb);
|
---|
| 514 | return VINF_SUCCESS;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 |
|
---|
| 518 | /**
|
---|
| 519 | * Completes the URB with a OK state.
|
---|
| 520 | */
|
---|
| 521 | static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
|
---|
| 522 | {
|
---|
| 523 | Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
|
---|
| 524 |
|
---|
| 525 | pUrb->enmStatus = VUSBSTATUS_OK;
|
---|
[30224] | 526 | pUrb->cbData = (uint32_t)cbData;
|
---|
[26473] | 527 |
|
---|
| 528 | usbHidLinkDone(pThis, pUrb);
|
---|
| 529 | return VINF_SUCCESS;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 |
|
---|
| 533 | /**
|
---|
| 534 | * Reset worker for usbHidUsbReset, usbHidUsbSetConfiguration and
|
---|
[36445] | 535 | * usbHidHandleDefaultPipe.
|
---|
[26473] | 536 | *
|
---|
| 537 | * @returns VBox status code.
|
---|
| 538 | * @param pThis The HID instance.
|
---|
[36445] | 539 | * @param pUrb Set when usbHidHandleDefaultPipe is the
|
---|
[26473] | 540 | * caller.
|
---|
| 541 | * @param fSetConfig Set when usbHidUsbSetConfiguration is the
|
---|
| 542 | * caller.
|
---|
| 543 | */
|
---|
| 544 | static int usbHidResetWorker(PUSBHID pThis, PVUSBURB pUrb, bool fSetConfig)
|
---|
| 545 | {
|
---|
| 546 | /*
|
---|
[28909] | 547 | * Deactivate the keyboard.
|
---|
| 548 | */
|
---|
| 549 | pThis->Lun0.pDrv->pfnSetActive(pThis->Lun0.pDrv, false);
|
---|
| 550 |
|
---|
| 551 | /*
|
---|
[26473] | 552 | * Reset the device state.
|
---|
| 553 | */
|
---|
| 554 | pThis->enmState = USBHIDREQSTATE_READY;
|
---|
| 555 | pThis->bIdle = 0;
|
---|
[27890] | 556 | pThis->fHasPendingChanges = false;
|
---|
[26473] | 557 |
|
---|
| 558 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
|
---|
| 559 | pThis->aEps[i].fHalted = false;
|
---|
| 560 |
|
---|
| 561 | if (!pUrb && !fSetConfig) /* (only device reset) */
|
---|
| 562 | pThis->bConfigurationValue = 0; /* default */
|
---|
| 563 |
|
---|
| 564 | /*
|
---|
| 565 | * Ditch all pending URBs.
|
---|
| 566 | */
|
---|
| 567 | PVUSBURB pCurUrb;
|
---|
| 568 | while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
|
---|
| 569 | {
|
---|
| 570 | pCurUrb->enmStatus = VUSBSTATUS_CRC;
|
---|
| 571 | usbHidLinkDone(pThis, pCurUrb);
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | if (pUrb)
|
---|
| 575 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
| 576 | return VINF_SUCCESS;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | /**
|
---|
[30507] | 580 | * Returns true if the usage code corresponds to a keyboard modifier key
|
---|
| 581 | * (left or right ctrl, shift, alt or GUI). The usage codes for these keys
|
---|
| 582 | * are the range 0xe0 to 0xe7.
|
---|
| 583 | */
|
---|
| 584 | static bool usbHidUsageCodeIsModifier(uint8_t u8Usage)
|
---|
| 585 | {
|
---|
[44764] | 586 | return u8Usage >= USBHID_MODIFIER_FIRST && u8Usage <= USBHID_MODIFIER_LAST;
|
---|
[30507] | 587 | }
|
---|
| 588 |
|
---|
| 589 | /**
|
---|
| 590 | * Convert a USB HID usage code to a keyboard modifier flag. The arithmetic
|
---|
| 591 | * is simple: the modifier keys have usage codes from 0xe0 to 0xe7, and the
|
---|
| 592 | * lower nibble is the bit number of the flag.
|
---|
| 593 | */
|
---|
| 594 | static uint8_t usbHidModifierToFlag(uint8_t u8Usage)
|
---|
| 595 | {
|
---|
| 596 | Assert(usbHidUsageCodeIsModifier(u8Usage));
|
---|
| 597 | return RT_BIT(u8Usage & 0xf);
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | /**
|
---|
[51539] | 601 | * Create a USB HID keyboard report reflecting the current state of the
|
---|
| 602 | * keyboard (up/down keys).
|
---|
[30507] | 603 | */
|
---|
[51539] | 604 | static void usbHidBuildReport(PUSBHIDK_REPORT pReport, uint8_t *pabDepressedKeys)
|
---|
[30507] | 605 | {
|
---|
| 606 | unsigned iBuf = 0;
|
---|
| 607 | RT_ZERO(*pReport);
|
---|
[34028] | 608 | for (unsigned iKey = 0; iKey < VBOX_USB_USAGE_ARRAY_SIZE; ++iKey)
|
---|
[30507] | 609 | {
|
---|
[51539] | 610 | Assert(iBuf <= RT_ELEMENTS(pReport->aKeys));
|
---|
| 611 | if (pabDepressedKeys[iKey])
|
---|
[30507] | 612 | {
|
---|
| 613 | if (usbHidUsageCodeIsModifier(iKey))
|
---|
| 614 | pReport->ShiftState |= usbHidModifierToFlag(iKey);
|
---|
| 615 | else if (iBuf == RT_ELEMENTS(pReport->aKeys))
|
---|
| 616 | {
|
---|
| 617 | /* The USB HID spec says that the entire vector should be
|
---|
| 618 | * set to ErrorRollOver on overflow. We don't mind if this
|
---|
| 619 | * path is taken several times for one report. */
|
---|
| 620 | for (unsigned iBuf2 = 0;
|
---|
| 621 | iBuf2 < RT_ELEMENTS(pReport->aKeys); ++iBuf2)
|
---|
| 622 | pReport->aKeys[iBuf2] = USBHID_USAGE_ROLL_OVER;
|
---|
| 623 | }
|
---|
| 624 | else
|
---|
| 625 | {
|
---|
| 626 | pReport->aKeys[iBuf] = iKey;
|
---|
| 627 | ++iBuf;
|
---|
| 628 | }
|
---|
| 629 | }
|
---|
| 630 | }
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | /**
|
---|
[47675] | 634 | * Handles a SET_REPORT request sent to the default control pipe. Note
|
---|
| 635 | * that unrecognized requests are ignored without reporting an error.
|
---|
| 636 | */
|
---|
| 637 | static void usbHidSetReport(PUSBHID pThis, PVUSBURB pUrb)
|
---|
| 638 | {
|
---|
| 639 | PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
|
---|
| 640 | Assert(pSetup->bRequest == HID_REQ_SET_REPORT);
|
---|
| 641 |
|
---|
| 642 | /* The LED report is the 3rd report, ID 0 (-> wValue 0x200). */
|
---|
| 643 | if (pSetup->wIndex == 0 && pSetup->wLength == 1 && pSetup->wValue == 0x200)
|
---|
| 644 | {
|
---|
| 645 | PDMKEYBLEDS enmLeds = PDMKEYBLEDS_NONE;
|
---|
| 646 | uint8_t u8LEDs = pUrb->abData[sizeof(*pSetup)];
|
---|
| 647 | LogFlowFunc(("Setting keybooard LEDs to u8LEDs=%02X\n", u8LEDs));
|
---|
| 648 |
|
---|
| 649 | /* Translate LED state to PDM format and send upstream. */
|
---|
| 650 | if (u8LEDs & 0x01)
|
---|
| 651 | enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_NUMLOCK);
|
---|
| 652 | if (u8LEDs & 0x02)
|
---|
| 653 | enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_CAPSLOCK);
|
---|
| 654 | if (u8LEDs & 0x04)
|
---|
| 655 | enmLeds = (PDMKEYBLEDS)(enmLeds | PDMKEYBLEDS_SCROLLLOCK);
|
---|
| 656 |
|
---|
| 657 | pThis->Lun0.pDrv->pfnLedStatusChange(pThis->Lun0.pDrv, enmLeds);
|
---|
| 658 | }
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | /**
|
---|
[51539] | 662 | * Sends a state report to the guest if there is a URB available.
|
---|
[26473] | 663 | */
|
---|
[51539] | 664 | static void usbHidSendReport(PUSBHID pThis)
|
---|
[26473] | 665 | {
|
---|
| 666 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
|
---|
| 667 | if (pUrb)
|
---|
| 668 | {
|
---|
[30507] | 669 | PUSBHIDK_REPORT pReport = (PUSBHIDK_REPORT)&pUrb->abData[0];
|
---|
[26473] | 670 |
|
---|
[51539] | 671 | usbHidBuildReport(pReport, pThis->abDepressedKeys);
|
---|
| 672 | pThis->fHasPendingChanges = false;
|
---|
| 673 | usbHidCompleteOk(pThis, pUrb, sizeof(*pReport));
|
---|
[26473] | 674 | }
|
---|
[27838] | 675 | else
|
---|
| 676 | {
|
---|
[27890] | 677 | Log2(("No available URB for USB kbd\n"));
|
---|
| 678 | pThis->fHasPendingChanges = true;
|
---|
[27838] | 679 | }
|
---|
[26473] | 680 | }
|
---|
| 681 |
|
---|
| 682 | /**
|
---|
| 683 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
| 684 | */
|
---|
| 685 | static DECLCALLBACK(void *) usbHidKeyboardQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
| 686 | {
|
---|
| 687 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
|
---|
| 688 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
|
---|
| 689 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDPORT, &pThis->Lun0.IPort);
|
---|
| 690 | return NULL;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
[49385] | 693 | /* See the PS2K device. */
|
---|
| 694 | #define KRSP_BAT_FAIL 0xFC /* Also a 'release keys' signal. */
|
---|
| 695 |
|
---|
[26473] | 696 | /**
|
---|
| 697 | * Keyboard event handler.
|
---|
| 698 | *
|
---|
| 699 | * @returns VBox status code.
|
---|
| 700 | * @param pInterface Pointer to the keyboard port interface (KBDState::Keyboard.IPort).
|
---|
[50962] | 701 | * @param u32UsageCode The key usage ID.
|
---|
[26473] | 702 | */
|
---|
[50962] | 703 | static DECLCALLBACK(int) usbHidKeyboardPutEvent(PPDMIKEYBOARDPORT pInterface, uint32_t u32UsageCode)
|
---|
[26473] | 704 | {
|
---|
| 705 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
|
---|
[26867] | 706 | uint8_t u8HidCode;
|
---|
[51539] | 707 | bool fKeyDown;
|
---|
[27917] | 708 | bool fHaveEvent = true;
|
---|
[51539] | 709 | int rc = VINF_SUCCESS;
|
---|
[26473] | 710 |
|
---|
[27969] | 711 | RTCritSectEnter(&pThis->CritSect);
|
---|
[26473] | 712 |
|
---|
[51539] | 713 | /* Let's see what we got... */
|
---|
| 714 | fKeyDown = !(u32UsageCode & 0x80000000);
|
---|
| 715 | u8HidCode = u32UsageCode & 0xFF;
|
---|
| 716 | AssertReturn(u8HidCode <= VBOX_USB_MAX_USAGE_CODE, VERR_INTERNAL_ERROR);
|
---|
| 717 |
|
---|
| 718 | LogFlowFunc(("key %s: 0x%x\n", fKeyDown ? "down" : "up", u8HidCode));
|
---|
| 719 |
|
---|
| 720 | /*
|
---|
| 721 | * Due to host key repeat, we can get key events for keys which are
|
---|
| 722 | * already depressed. Drop those right here.
|
---|
| 723 | */
|
---|
| 724 | if (fKeyDown && pThis->abDepressedKeys[u8HidCode])
|
---|
| 725 | fHaveEvent = false;
|
---|
| 726 |
|
---|
| 727 | /* If there is already a pending event, we won't accept a new one yet. */
|
---|
| 728 | if (pThis->fHasPendingChanges && fHaveEvent)
|
---|
[26473] | 729 | {
|
---|
[51539] | 730 | rc = VERR_TRY_AGAIN;
|
---|
[49385] | 731 | }
|
---|
[51539] | 732 | else if (fHaveEvent)
|
---|
[49385] | 733 | {
|
---|
[51539] | 734 | if (RT_UNLIKELY(u32UsageCode == KRSP_BAT_FAIL))
|
---|
[26473] | 735 | {
|
---|
[51539] | 736 | /* Clear all currently depressed and unreported keys. */
|
---|
| 737 | RT_ZERO(pThis->abDepressedKeys);
|
---|
[50962] | 738 | }
|
---|
| 739 | else
|
---|
| 740 | {
|
---|
[51539] | 741 | /* Regular key event - update keyboard state. */
|
---|
| 742 | if (fKeyDown)
|
---|
| 743 | pThis->abDepressedKeys[u8HidCode] = 1;
|
---|
| 744 | else
|
---|
| 745 | pThis->abDepressedKeys[u8HidCode] = 0;
|
---|
[50962] | 746 | }
|
---|
[27890] | 747 |
|
---|
[51539] | 748 | /*
|
---|
| 749 | * Try sending a report. Note that we already decided to consume the
|
---|
| 750 | * event regardless of whether a URB is available or not. If it's not,
|
---|
| 751 | * we will simply not accept any further events.
|
---|
| 752 | */
|
---|
| 753 | usbHidSendReport(pThis);
|
---|
[26473] | 754 | }
|
---|
| 755 |
|
---|
[27969] | 756 | RTCritSectLeave(&pThis->CritSect);
|
---|
[27890] | 757 |
|
---|
[51539] | 758 | return rc;
|
---|
[26473] | 759 | }
|
---|
| 760 |
|
---|
| 761 | /**
|
---|
| 762 | * @copydoc PDMUSBREG::pfnUrbReap
|
---|
| 763 | */
|
---|
| 764 | static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
|
---|
| 765 | {
|
---|
| 766 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
[27890] | 767 | //LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
|
---|
[26473] | 768 |
|
---|
[27969] | 769 | RTCritSectEnter(&pThis->CritSect);
|
---|
[26473] | 770 |
|
---|
| 771 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
|
---|
| 772 | if (!pUrb && cMillies)
|
---|
| 773 | {
|
---|
| 774 | /* Wait */
|
---|
| 775 | pThis->fHaveDoneQueueWaiter = true;
|
---|
[27969] | 776 | RTCritSectLeave(&pThis->CritSect);
|
---|
[26473] | 777 |
|
---|
| 778 | RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
|
---|
| 779 |
|
---|
[27969] | 780 | RTCritSectEnter(&pThis->CritSect);
|
---|
[26473] | 781 | pThis->fHaveDoneQueueWaiter = false;
|
---|
| 782 |
|
---|
| 783 | pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
|
---|
| 784 | }
|
---|
| 785 |
|
---|
[27969] | 786 | RTCritSectLeave(&pThis->CritSect);
|
---|
[26473] | 787 |
|
---|
| 788 | if (pUrb)
|
---|
| 789 | Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
| 790 | return pUrb;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 |
|
---|
| 794 | /**
|
---|
[49814] | 795 | * @copydoc PDMUSBREG::pfnWakeup
|
---|
| 796 | */
|
---|
| 797 | static DECLCALLBACK(int) usbHidWakeup(PPDMUSBINS pUsbIns)
|
---|
| 798 | {
|
---|
| 799 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 800 |
|
---|
| 801 | return RTSemEventSignal(pThis->hEvtDoneQueue);
|
---|
| 802 | }
|
---|
| 803 |
|
---|
| 804 |
|
---|
| 805 | /**
|
---|
[26473] | 806 | * @copydoc PDMUSBREG::pfnUrbCancel
|
---|
| 807 | */
|
---|
| 808 | static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
| 809 | {
|
---|
| 810 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 811 | LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
[27969] | 812 | RTCritSectEnter(&pThis->CritSect);
|
---|
[26473] | 813 |
|
---|
| 814 | /*
|
---|
| 815 | * Remove the URB from the to-host queue and move it onto the done queue.
|
---|
| 816 | */
|
---|
| 817 | if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
|
---|
| 818 | usbHidLinkDone(pThis, pUrb);
|
---|
| 819 |
|
---|
[27969] | 820 | RTCritSectLeave(&pThis->CritSect);
|
---|
[26473] | 821 | return VINF_SUCCESS;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 |
|
---|
| 825 | /**
|
---|
| 826 | * Handles request sent to the inbound (device to host) interrupt pipe. This is
|
---|
| 827 | * rather different from bulk requests because an interrupt read URB may complete
|
---|
| 828 | * after arbitrarily long time.
|
---|
| 829 | */
|
---|
| 830 | static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
|
---|
| 831 | {
|
---|
| 832 | /*
|
---|
| 833 | * Stall the request if the pipe is halted.
|
---|
| 834 | */
|
---|
| 835 | if (RT_UNLIKELY(pEp->fHalted))
|
---|
| 836 | return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
|
---|
| 837 |
|
---|
| 838 | /*
|
---|
| 839 | * Deal with the URB according to the state.
|
---|
| 840 | */
|
---|
| 841 | switch (pThis->enmState)
|
---|
| 842 | {
|
---|
| 843 | /*
|
---|
| 844 | * We've data left to transfer to the host.
|
---|
| 845 | */
|
---|
| 846 | case USBHIDREQSTATE_DATA_TO_HOST:
|
---|
| 847 | {
|
---|
| 848 | AssertFailed();
|
---|
| 849 | Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
|
---|
| 850 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 | /*
|
---|
| 854 | * Status transfer.
|
---|
| 855 | */
|
---|
| 856 | case USBHIDREQSTATE_STATUS:
|
---|
| 857 | {
|
---|
| 858 | AssertFailed();
|
---|
| 859 | Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
|
---|
| 860 | pThis->enmState = USBHIDREQSTATE_READY;
|
---|
| 861 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | case USBHIDREQSTATE_READY:
|
---|
| 865 | usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
|
---|
[44022] | 866 | /* If device was not set idle, send the current report right away. */
|
---|
[27890] | 867 | if (pThis->bIdle != 0 || pThis->fHasPendingChanges)
|
---|
[51539] | 868 | {
|
---|
[26473] | 869 | usbHidSendReport(pThis);
|
---|
[51539] | 870 | LogFlow(("usbHidHandleIntrDevToHost: Sent report via %p:%s\n", pUrb, pUrb->pszDesc));
|
---|
| 871 | Assert(!pThis->fHasPendingChanges); /* Since we just got a URB... */
|
---|
| 872 | /* There may be more input queued up. Ask for it now. */
|
---|
| 873 | pThis->Lun0.pDrv->pfnFlushQueue(pThis->Lun0.pDrv);
|
---|
| 874 | }
|
---|
[26473] | 875 | return VINF_SUCCESS;
|
---|
| 876 |
|
---|
| 877 | /*
|
---|
| 878 | * Bad states, stall.
|
---|
| 879 | */
|
---|
| 880 | default:
|
---|
| 881 | Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
|
---|
| 882 | return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
|
---|
| 883 | }
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 |
|
---|
| 887 | /**
|
---|
| 888 | * Handles request sent to the default control pipe.
|
---|
| 889 | */
|
---|
| 890 | static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
|
---|
| 891 | {
|
---|
| 892 | PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
|
---|
[27838] | 893 | LogFlow(("usbHidHandleDefaultPipe: cbData=%d\n", pUrb->cbData));
|
---|
| 894 |
|
---|
[26473] | 895 | AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
|
---|
| 896 |
|
---|
| 897 | if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
|
---|
| 898 | {
|
---|
| 899 | switch (pSetup->bRequest)
|
---|
| 900 | {
|
---|
| 901 | case VUSB_REQ_GET_DESCRIPTOR:
|
---|
| 902 | {
|
---|
| 903 | switch (pSetup->bmRequestType)
|
---|
| 904 | {
|
---|
| 905 | case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
| 906 | {
|
---|
| 907 | switch (pSetup->wValue >> 8)
|
---|
| 908 | {
|
---|
| 909 | case VUSB_DT_STRING:
|
---|
| 910 | Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
| 911 | break;
|
---|
| 912 | default:
|
---|
| 913 | Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
| 914 | break;
|
---|
| 915 | }
|
---|
| 916 | break;
|
---|
| 917 | }
|
---|
| 918 |
|
---|
| 919 | case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
| 920 | {
|
---|
| 921 | switch (pSetup->wValue >> 8)
|
---|
| 922 | {
|
---|
[37388] | 923 | case DT_IF_HID_DESCRIPTOR:
|
---|
| 924 | {
|
---|
| 925 | uint32_t cbCopy;
|
---|
| 926 |
|
---|
| 927 | /* Returned data is written after the setup message. */
|
---|
| 928 | cbCopy = pUrb->cbData - sizeof(*pSetup);
|
---|
| 929 | cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidIfHidDesc));
|
---|
| 930 | Log(("usbHidKbd: GET_DESCRIPTOR DT_IF_HID_DESCRIPTOR wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
|
---|
| 931 | memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidIfHidDesc, cbCopy);
|
---|
| 932 | return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
|
---|
| 933 | }
|
---|
| 934 |
|
---|
[26473] | 935 | case DT_IF_HID_REPORT:
|
---|
[37388] | 936 | {
|
---|
[26473] | 937 | uint32_t cbCopy;
|
---|
| 938 |
|
---|
| 939 | /* Returned data is written after the setup message. */
|
---|
| 940 | cbCopy = pUrb->cbData - sizeof(*pSetup);
|
---|
| 941 | cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidReportDesc));
|
---|
| 942 | Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
|
---|
| 943 | memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidReportDesc, cbCopy);
|
---|
| 944 | return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
|
---|
[37388] | 945 | }
|
---|
| 946 |
|
---|
[26473] | 947 | default:
|
---|
| 948 | Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
| 949 | break;
|
---|
| 950 | }
|
---|
| 951 | break;
|
---|
| 952 | }
|
---|
| 953 |
|
---|
| 954 | default:
|
---|
| 955 | Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
|
---|
| 956 | return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
|
---|
| 957 | }
|
---|
| 958 | break;
|
---|
| 959 | }
|
---|
| 960 |
|
---|
[26945] | 961 | case VUSB_REQ_GET_STATUS:
|
---|
| 962 | {
|
---|
| 963 | uint16_t wRet = 0;
|
---|
| 964 |
|
---|
[27129] | 965 | if (pSetup->wLength != 2)
|
---|
[26945] | 966 | {
|
---|
| 967 | Log(("usbHid: Bad GET_STATUS req: wLength=%#x\n", pSetup->wLength));
|
---|
| 968 | break;
|
---|
| 969 | }
|
---|
| 970 | Assert(pSetup->wValue == 0);
|
---|
| 971 | switch (pSetup->bmRequestType)
|
---|
| 972 | {
|
---|
| 973 | case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
| 974 | {
|
---|
| 975 | Assert(pSetup->wIndex == 0);
|
---|
| 976 | Log(("usbHid: GET_STATUS (device)\n"));
|
---|
| 977 | wRet = 0; /* Not self-powered, no remote wakeup. */
|
---|
| 978 | memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
|
---|
| 979 | return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
|
---|
| 980 | }
|
---|
[27129] | 981 |
|
---|
[26945] | 982 | case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
| 983 | {
|
---|
| 984 | if (pSetup->wIndex == 0)
|
---|
| 985 | {
|
---|
| 986 | memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
|
---|
| 987 | return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
|
---|
| 988 | }
|
---|
| 989 | else
|
---|
| 990 | {
|
---|
| 991 | Log(("usbHid: GET_STATUS (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
|
---|
| 992 | }
|
---|
| 993 | break;
|
---|
| 994 | }
|
---|
| 995 |
|
---|
| 996 | case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
| 997 | {
|
---|
| 998 | if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
|
---|
| 999 | {
|
---|
| 1000 | wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
|
---|
| 1001 | memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
|
---|
| 1002 | return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
|
---|
| 1003 | }
|
---|
| 1004 | else
|
---|
| 1005 | {
|
---|
| 1006 | Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
|
---|
| 1007 | }
|
---|
| 1008 | break;
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | default:
|
---|
| 1012 | Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
|
---|
| 1013 | return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
|
---|
| 1014 | }
|
---|
| 1015 | break;
|
---|
| 1016 | }
|
---|
| 1017 |
|
---|
[26473] | 1018 | case VUSB_REQ_CLEAR_FEATURE:
|
---|
| 1019 | break;
|
---|
| 1020 | }
|
---|
| 1021 |
|
---|
| 1022 | /** @todo implement this. */
|
---|
| 1023 | Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
| 1024 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
| 1025 |
|
---|
| 1026 | usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
|
---|
| 1027 | }
|
---|
| 1028 | else if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_CLASS)
|
---|
| 1029 | {
|
---|
| 1030 | switch (pSetup->bRequest)
|
---|
| 1031 | {
|
---|
| 1032 | case HID_REQ_SET_IDLE:
|
---|
| 1033 | {
|
---|
| 1034 | switch (pSetup->bmRequestType)
|
---|
| 1035 | {
|
---|
| 1036 | case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
|
---|
| 1037 | {
|
---|
| 1038 | Log(("usbHid: SET_IDLE wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
| 1039 | pThis->bIdle = pSetup->wValue >> 8;
|
---|
| 1040 | /* Consider 24ms to mean zero for keyboards (see IOUSBHIDDriver) */
|
---|
| 1041 | if (pThis->bIdle == 6) pThis->bIdle = 0;
|
---|
| 1042 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
| 1043 | }
|
---|
| 1044 | break;
|
---|
| 1045 | }
|
---|
| 1046 | break;
|
---|
[26495] | 1047 | }
|
---|
[26473] | 1048 | case HID_REQ_GET_IDLE:
|
---|
| 1049 | {
|
---|
| 1050 | switch (pSetup->bmRequestType)
|
---|
| 1051 | {
|
---|
| 1052 | case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_HOST:
|
---|
| 1053 | {
|
---|
| 1054 | Log(("usbHid: GET_IDLE wValue=%#x wIndex=%#x, returning %#x\n", pSetup->wValue, pSetup->wIndex, pThis->bIdle));
|
---|
| 1055 | pUrb->abData[sizeof(*pSetup)] = pThis->bIdle;
|
---|
| 1056 | return usbHidCompleteOk(pThis, pUrb, 1);
|
---|
| 1057 | }
|
---|
| 1058 | break;
|
---|
| 1059 | }
|
---|
| 1060 | break;
|
---|
[26495] | 1061 | }
|
---|
[47675] | 1062 | case HID_REQ_SET_REPORT:
|
---|
| 1063 | {
|
---|
| 1064 | switch (pSetup->bmRequestType)
|
---|
| 1065 | {
|
---|
| 1066 | case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
|
---|
| 1067 | {
|
---|
| 1068 | Log(("usbHid: SET_REPORT wValue=%#x wIndex=%#x wLength=%#x\n", pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
| 1069 | usbHidSetReport(pThis, pUrb);
|
---|
| 1070 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
| 1071 | }
|
---|
| 1072 | break;
|
---|
| 1073 | }
|
---|
| 1074 | break;
|
---|
| 1075 | }
|
---|
[26473] | 1076 | }
|
---|
| 1077 | Log(("usbHid: Unimplemented class request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
| 1078 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
| 1079 |
|
---|
| 1080 | usbHidCompleteStall(pThis, pEp, pUrb, "TODO: class request stuff");
|
---|
| 1081 | }
|
---|
| 1082 | else
|
---|
| 1083 | {
|
---|
| 1084 | Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
| 1085 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
| 1086 | return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
|
---|
| 1087 | }
|
---|
| 1088 |
|
---|
| 1089 | return VINF_SUCCESS;
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 |
|
---|
| 1093 | /**
|
---|
[36421] | 1094 | * @copydoc PDMUSBREG::pfnUrbQueue
|
---|
[26473] | 1095 | */
|
---|
| 1096 | static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
| 1097 | {
|
---|
| 1098 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 1099 | LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
|
---|
[27969] | 1100 | RTCritSectEnter(&pThis->CritSect);
|
---|
[26473] | 1101 |
|
---|
| 1102 | /*
|
---|
| 1103 | * Parse on a per end-point basis.
|
---|
| 1104 | */
|
---|
| 1105 | int rc;
|
---|
| 1106 | switch (pUrb->EndPt)
|
---|
| 1107 | {
|
---|
| 1108 | case 0:
|
---|
| 1109 | rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
|
---|
| 1110 | break;
|
---|
| 1111 |
|
---|
| 1112 | case 0x81:
|
---|
| 1113 | AssertFailed();
|
---|
| 1114 | case 0x01:
|
---|
| 1115 | rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
|
---|
| 1116 | break;
|
---|
| 1117 |
|
---|
| 1118 | default:
|
---|
| 1119 | AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
|
---|
| 1120 | rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
|
---|
| 1121 | break;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
[27969] | 1124 | RTCritSectLeave(&pThis->CritSect);
|
---|
[26473] | 1125 | return rc;
|
---|
| 1126 | }
|
---|
| 1127 |
|
---|
| 1128 |
|
---|
| 1129 | /**
|
---|
| 1130 | * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
|
---|
| 1131 | */
|
---|
| 1132 | static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
|
---|
| 1133 | {
|
---|
| 1134 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 1135 | LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
|
---|
| 1136 |
|
---|
| 1137 | if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
|
---|
| 1138 | {
|
---|
[27969] | 1139 | RTCritSectEnter(&pThis->CritSect);
|
---|
[26473] | 1140 | pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
|
---|
[27969] | 1141 | RTCritSectLeave(&pThis->CritSect);
|
---|
[26473] | 1142 | }
|
---|
| 1143 |
|
---|
| 1144 | return VINF_SUCCESS;
|
---|
| 1145 | }
|
---|
| 1146 |
|
---|
| 1147 |
|
---|
| 1148 | /**
|
---|
| 1149 | * @copydoc PDMUSBREG::pfnUsbSetInterface
|
---|
| 1150 | */
|
---|
| 1151 | static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
|
---|
| 1152 | {
|
---|
| 1153 | LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
|
---|
| 1154 | Assert(bAlternateSetting == 0);
|
---|
| 1155 | return VINF_SUCCESS;
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
| 1158 |
|
---|
| 1159 | /**
|
---|
| 1160 | * @copydoc PDMUSBREG::pfnUsbSetConfiguration
|
---|
| 1161 | */
|
---|
| 1162 | static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
|
---|
| 1163 | const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
|
---|
| 1164 | {
|
---|
| 1165 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 1166 | LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
|
---|
| 1167 | Assert(bConfigurationValue == 1);
|
---|
[27969] | 1168 | RTCritSectEnter(&pThis->CritSect);
|
---|
[26473] | 1169 |
|
---|
| 1170 | /*
|
---|
| 1171 | * If the same config is applied more than once, it's a kind of reset.
|
---|
| 1172 | */
|
---|
| 1173 | if (pThis->bConfigurationValue == bConfigurationValue)
|
---|
| 1174 | usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
|
---|
| 1175 | pThis->bConfigurationValue = bConfigurationValue;
|
---|
| 1176 |
|
---|
[28909] | 1177 | /*
|
---|
| 1178 | * Tell the other end that the keyboard is now enabled and wants
|
---|
| 1179 | * to receive keystrokes.
|
---|
| 1180 | */
|
---|
| 1181 | pThis->Lun0.pDrv->pfnSetActive(pThis->Lun0.pDrv, true);
|
---|
| 1182 |
|
---|
[27969] | 1183 | RTCritSectLeave(&pThis->CritSect);
|
---|
[26473] | 1184 | return VINF_SUCCESS;
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
| 1187 |
|
---|
| 1188 | /**
|
---|
| 1189 | * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
|
---|
| 1190 | */
|
---|
| 1191 | static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
|
---|
| 1192 | {
|
---|
| 1193 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 1194 | LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
|
---|
| 1195 | return &g_UsbHidDescCache;
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 |
|
---|
| 1199 | /**
|
---|
| 1200 | * @copydoc PDMUSBREG::pfnUsbReset
|
---|
| 1201 | */
|
---|
| 1202 | static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
|
---|
| 1203 | {
|
---|
| 1204 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 1205 | LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
|
---|
[27969] | 1206 | RTCritSectEnter(&pThis->CritSect);
|
---|
[26473] | 1207 |
|
---|
| 1208 | int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
|
---|
| 1209 |
|
---|
[27969] | 1210 | RTCritSectLeave(&pThis->CritSect);
|
---|
[26473] | 1211 | return rc;
|
---|
| 1212 | }
|
---|
| 1213 |
|
---|
| 1214 |
|
---|
| 1215 | /**
|
---|
| 1216 | * @copydoc PDMUSBREG::pfnDestruct
|
---|
| 1217 | */
|
---|
[57393] | 1218 | static DECLCALLBACK(void) usbHidDestruct(PPDMUSBINS pUsbIns)
|
---|
[26473] | 1219 | {
|
---|
| 1220 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 1221 | LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
|
---|
| 1222 |
|
---|
[27969] | 1223 | if (RTCritSectIsInitialized(&pThis->CritSect))
|
---|
[26473] | 1224 | {
|
---|
[27969] | 1225 | /* Let whoever runs in this critical section complete. */
|
---|
| 1226 | RTCritSectEnter(&pThis->CritSect);
|
---|
| 1227 | RTCritSectLeave(&pThis->CritSect);
|
---|
| 1228 | RTCritSectDelete(&pThis->CritSect);
|
---|
[26473] | 1229 | }
|
---|
| 1230 |
|
---|
| 1231 | if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
|
---|
| 1232 | {
|
---|
| 1233 | RTSemEventDestroy(pThis->hEvtDoneQueue);
|
---|
| 1234 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
| 1235 | }
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 |
|
---|
| 1239 | /**
|
---|
| 1240 | * @copydoc PDMUSBREG::pfnConstruct
|
---|
| 1241 | */
|
---|
| 1242 | static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
|
---|
| 1243 | {
|
---|
| 1244 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
| 1245 | Log(("usbHidConstruct/#%u:\n", iInstance));
|
---|
| 1246 |
|
---|
| 1247 | /*
|
---|
| 1248 | * Perform the basic structure initialization first so the destructor
|
---|
| 1249 | * will not misbehave.
|
---|
| 1250 | */
|
---|
| 1251 | pThis->pUsbIns = pUsbIns;
|
---|
| 1252 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
| 1253 | usbHidQueueInit(&pThis->ToHostQueue);
|
---|
| 1254 | usbHidQueueInit(&pThis->DoneQueue);
|
---|
| 1255 |
|
---|
[27969] | 1256 | int rc = RTCritSectInit(&pThis->CritSect);
|
---|
[26473] | 1257 | AssertRCReturn(rc, rc);
|
---|
| 1258 |
|
---|
| 1259 | rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
|
---|
| 1260 | AssertRCReturn(rc, rc);
|
---|
| 1261 |
|
---|
| 1262 | /*
|
---|
| 1263 | * Validate and read the configuration.
|
---|
| 1264 | */
|
---|
| 1265 | rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbHid", iInstance);
|
---|
| 1266 | if (RT_FAILURE(rc))
|
---|
| 1267 | return rc;
|
---|
| 1268 |
|
---|
| 1269 | pThis->Lun0.IBase.pfnQueryInterface = usbHidKeyboardQueryInterface;
|
---|
[50962] | 1270 | pThis->Lun0.IPort.pfnPutEventHid = usbHidKeyboardPutEvent;
|
---|
[26473] | 1271 |
|
---|
| 1272 | /*
|
---|
| 1273 | * Attach the keyboard driver.
|
---|
| 1274 | */
|
---|
[37795] | 1275 | rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Keyboard Port");
|
---|
[26473] | 1276 | if (RT_FAILURE(rc))
|
---|
| 1277 | return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach keyboard driver"));
|
---|
| 1278 |
|
---|
[28909] | 1279 | pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIKEYBOARDCONNECTOR);
|
---|
| 1280 | if (!pThis->Lun0.pDrv)
|
---|
| 1281 | return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query keyboard interface"));
|
---|
| 1282 |
|
---|
[26473] | 1283 | return VINF_SUCCESS;
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
| 1286 |
|
---|
| 1287 | /**
|
---|
| 1288 | * The USB Human Interface Device (HID) Keyboard registration record.
|
---|
| 1289 | */
|
---|
| 1290 | const PDMUSBREG g_UsbHidKbd =
|
---|
| 1291 | {
|
---|
| 1292 | /* u32Version */
|
---|
| 1293 | PDM_USBREG_VERSION,
|
---|
| 1294 | /* szName */
|
---|
| 1295 | "HidKeyboard",
|
---|
| 1296 | /* pszDescription */
|
---|
| 1297 | "USB HID Keyboard.",
|
---|
| 1298 | /* fFlags */
|
---|
| 1299 | 0,
|
---|
| 1300 | /* cMaxInstances */
|
---|
[40282] | 1301 | ~0U,
|
---|
[26473] | 1302 | /* cbInstance */
|
---|
| 1303 | sizeof(USBHID),
|
---|
| 1304 | /* pfnConstruct */
|
---|
| 1305 | usbHidConstruct,
|
---|
| 1306 | /* pfnDestruct */
|
---|
| 1307 | usbHidDestruct,
|
---|
| 1308 | /* pfnVMInitComplete */
|
---|
| 1309 | NULL,
|
---|
| 1310 | /* pfnVMPowerOn */
|
---|
| 1311 | NULL,
|
---|
| 1312 | /* pfnVMReset */
|
---|
| 1313 | NULL,
|
---|
| 1314 | /* pfnVMSuspend */
|
---|
| 1315 | NULL,
|
---|
| 1316 | /* pfnVMResume */
|
---|
| 1317 | NULL,
|
---|
| 1318 | /* pfnVMPowerOff */
|
---|
| 1319 | NULL,
|
---|
| 1320 | /* pfnHotPlugged */
|
---|
| 1321 | NULL,
|
---|
| 1322 | /* pfnHotUnplugged */
|
---|
| 1323 | NULL,
|
---|
| 1324 | /* pfnDriverAttach */
|
---|
| 1325 | NULL,
|
---|
| 1326 | /* pfnDriverDetach */
|
---|
| 1327 | NULL,
|
---|
| 1328 | /* pfnQueryInterface */
|
---|
| 1329 | NULL,
|
---|
| 1330 | /* pfnUsbReset */
|
---|
| 1331 | usbHidUsbReset,
|
---|
[36322] | 1332 | /* pfnUsbGetDescriptorCache */
|
---|
[26473] | 1333 | usbHidUsbGetDescriptorCache,
|
---|
| 1334 | /* pfnUsbSetConfiguration */
|
---|
| 1335 | usbHidUsbSetConfiguration,
|
---|
| 1336 | /* pfnUsbSetInterface */
|
---|
| 1337 | usbHidUsbSetInterface,
|
---|
| 1338 | /* pfnUsbClearHaltedEndpoint */
|
---|
| 1339 | usbHidUsbClearHaltedEndpoint,
|
---|
| 1340 | /* pfnUrbNew */
|
---|
| 1341 | NULL/*usbHidUrbNew*/,
|
---|
[36421] | 1342 | /* pfnUrbQueue */
|
---|
[26473] | 1343 | usbHidQueue,
|
---|
| 1344 | /* pfnUrbCancel */
|
---|
| 1345 | usbHidUrbCancel,
|
---|
| 1346 | /* pfnUrbReap */
|
---|
| 1347 | usbHidUrbReap,
|
---|
[49814] | 1348 | /* pfnWakeup */
|
---|
| 1349 | usbHidWakeup,
|
---|
[26473] | 1350 | /* u32TheEnd */
|
---|
| 1351 | PDM_USBREG_VERSION
|
---|
| 1352 | };
|
---|