1 | /* $Id: UsbKbd.cpp 30511 2010-06-29 16:35:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * UsbKbd - USB Human Interface Device Emulation, Keyboard.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2010 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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_USB_KBD
|
---|
22 | #include <VBox/pdmusb.h>
|
---|
23 | #include <VBox/log.h>
|
---|
24 | #include <VBox/err.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/critsect.h>
|
---|
27 | #include <iprt/mem.h>
|
---|
28 | #include <iprt/semaphore.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/uuid.h>
|
---|
31 | #include "../Builtins.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | /*******************************************************************************
|
---|
35 | * Defined Constants And Macros *
|
---|
36 | *******************************************************************************/
|
---|
37 | /** @name USB HID string IDs
|
---|
38 | * @{ */
|
---|
39 | #define USBHID_STR_ID_MANUFACTURER 1
|
---|
40 | #define USBHID_STR_ID_PRODUCT 2
|
---|
41 | /** @} */
|
---|
42 |
|
---|
43 | /** @name USB HID specific descriptor types
|
---|
44 | * @{ */
|
---|
45 | #define DT_IF_HID_REPORT 0x22
|
---|
46 | /** @} */
|
---|
47 |
|
---|
48 | /** @name USB HID vendor and product IDs
|
---|
49 | * @{ */
|
---|
50 | #define VBOX_USB_VENDOR 0x80EE
|
---|
51 | #define USBHID_PID_KEYBOARD 0x0010
|
---|
52 | /** @} */
|
---|
53 |
|
---|
54 | /** @name USB HID class specific requests
|
---|
55 | * @{ */
|
---|
56 | #define HID_REQ_GET_REPORT 0x01
|
---|
57 | #define HID_REQ_GET_IDLE 0x02
|
---|
58 | #define HID_REQ_SET_REPORT 0x09
|
---|
59 | #define HID_REQ_SET_IDLE 0x0A
|
---|
60 | /** @} */
|
---|
61 |
|
---|
62 | /** @name USB HID additional constants
|
---|
63 | * @{ */
|
---|
64 | /** The highest USB usage code reported by the VBox emulated keyboard */
|
---|
65 | #define VBOX_USB_MAX_USAGE_KBD 231
|
---|
66 | #define USBHID_USAGE_ROLL_OVER 1
|
---|
67 | /** @} */
|
---|
68 |
|
---|
69 | /*******************************************************************************
|
---|
70 | * Structures and Typedefs *
|
---|
71 | *******************************************************************************/
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * The USB HID request state.
|
---|
75 | */
|
---|
76 | typedef enum USBHIDREQSTATE
|
---|
77 | {
|
---|
78 | /** Invalid status. */
|
---|
79 | USBHIDREQSTATE_INVALID = 0,
|
---|
80 | /** Ready to receive a new read request. */
|
---|
81 | USBHIDREQSTATE_READY,
|
---|
82 | /** Have (more) data for the host. */
|
---|
83 | USBHIDREQSTATE_DATA_TO_HOST,
|
---|
84 | /** Waiting to supply status information to the host. */
|
---|
85 | USBHIDREQSTATE_STATUS,
|
---|
86 | /** The end of the valid states. */
|
---|
87 | USBHIDREQSTATE_END
|
---|
88 | } USBHIDREQSTATE;
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Endpoint status data.
|
---|
93 | */
|
---|
94 | typedef struct USBHIDEP
|
---|
95 | {
|
---|
96 | bool fHalted;
|
---|
97 | } USBHIDEP;
|
---|
98 | /** Pointer to the endpoint status. */
|
---|
99 | typedef USBHIDEP *PUSBHIDEP;
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * A URB queue.
|
---|
104 | */
|
---|
105 | typedef struct USBHIDURBQUEUE
|
---|
106 | {
|
---|
107 | /** The head pointer. */
|
---|
108 | PVUSBURB pHead;
|
---|
109 | /** Where to insert the next entry. */
|
---|
110 | PVUSBURB *ppTail;
|
---|
111 | } USBHIDURBQUEUE;
|
---|
112 | /** Pointer to a URB queue. */
|
---|
113 | typedef USBHIDURBQUEUE *PUSBHIDURBQUEUE;
|
---|
114 | /** Pointer to a const URB queue. */
|
---|
115 | typedef USBHIDURBQUEUE const *PCUSBHIDURBQUEUE;
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * The USB HID report structure for regular keys.
|
---|
120 | */
|
---|
121 | typedef struct USBHIDK_REPORT
|
---|
122 | {
|
---|
123 | uint8_t ShiftState; /**< Modifier keys bitfield */
|
---|
124 | uint8_t Reserved; /**< Currently unused */
|
---|
125 | uint8_t aKeys[6]; /**< Normal keys */
|
---|
126 | } USBHIDK_REPORT, *PUSBHIDK_REPORT;
|
---|
127 |
|
---|
128 | /** Scancode translator state. */
|
---|
129 | typedef enum {
|
---|
130 | SS_IDLE, /**< Starting state. */
|
---|
131 | SS_EXT, /**< E0 byte was received. */
|
---|
132 | SS_EXT1 /**< E1 byte was received. */
|
---|
133 | } scan_state_t;
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * The USB HID instance data.
|
---|
137 | */
|
---|
138 | typedef struct USBHID
|
---|
139 | {
|
---|
140 | /** Pointer back to the PDM USB Device instance structure. */
|
---|
141 | PPDMUSBINS pUsbIns;
|
---|
142 | /** Critical section protecting the device state. */
|
---|
143 | RTCRITSECT CritSect;
|
---|
144 |
|
---|
145 | /** The current configuration.
|
---|
146 | * (0 - default, 1 - the one supported configuration, i.e configured.) */
|
---|
147 | uint8_t bConfigurationValue;
|
---|
148 | /** USB HID Idle value..
|
---|
149 | * (0 - only report state change, !=0 - report in bIdle * 4ms intervals.) */
|
---|
150 | uint8_t bIdle;
|
---|
151 | /** Endpoint 0 is the default control pipe, 1 is the dev->host interrupt one. */
|
---|
152 | USBHIDEP aEps[2];
|
---|
153 | /** The state of the HID (state machine).*/
|
---|
154 | USBHIDREQSTATE enmState;
|
---|
155 |
|
---|
156 | /** State of the scancode translation. */
|
---|
157 | scan_state_t XlatState;
|
---|
158 |
|
---|
159 | /** Pending to-host queue.
|
---|
160 | * The URBs waiting here are waiting for data to become available.
|
---|
161 | */
|
---|
162 | USBHIDURBQUEUE ToHostQueue;
|
---|
163 |
|
---|
164 | /** Done queue
|
---|
165 | * The URBs stashed here are waiting to be reaped. */
|
---|
166 | USBHIDURBQUEUE DoneQueue;
|
---|
167 | /** Signalled when adding an URB to the done queue and fHaveDoneQueueWaiter
|
---|
168 | * is set. */
|
---|
169 | RTSEMEVENT hEvtDoneQueue;
|
---|
170 | /** Someone is waiting on the done queue. */
|
---|
171 | bool fHaveDoneQueueWaiter;
|
---|
172 | /** If device has pending changes. */
|
---|
173 | bool fHasPendingChanges;
|
---|
174 | /** Keypresses which have not yet been reported. A workaround for the
|
---|
175 | * problem of keys being released before the keypress could be reported. */
|
---|
176 | uint8_t abUnreportedKeys[VBOX_USB_MAX_USAGE_KBD];
|
---|
177 | /** Currently depressed keys */
|
---|
178 | uint8_t abDepressedKeys[VBOX_USB_MAX_USAGE_KBD];
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Keyboard port - LUN#0.
|
---|
182 | *
|
---|
183 | * @implements PDMIBASE
|
---|
184 | * @implements PDMIKEYBOARDPORT
|
---|
185 | */
|
---|
186 | struct
|
---|
187 | {
|
---|
188 | /** The base interface for the keyboard port. */
|
---|
189 | PDMIBASE IBase;
|
---|
190 | /** The keyboard port base interface. */
|
---|
191 | PDMIKEYBOARDPORT IPort;
|
---|
192 |
|
---|
193 | /** The base interface of the attached keyboard driver. */
|
---|
194 | R3PTRTYPE(PPDMIBASE) pDrvBase;
|
---|
195 | /** The keyboard interface of the attached keyboard driver. */
|
---|
196 | R3PTRTYPE(PPDMIKEYBOARDCONNECTOR) pDrv;
|
---|
197 | } Lun0;
|
---|
198 | } USBHID;
|
---|
199 | /** Pointer to the USB HID instance data. */
|
---|
200 | typedef USBHID *PUSBHID;
|
---|
201 |
|
---|
202 | /*******************************************************************************
|
---|
203 | * Global Variables *
|
---|
204 | *******************************************************************************/
|
---|
205 | static const PDMUSBDESCCACHESTRING g_aUsbHidStrings_en_US[] =
|
---|
206 | {
|
---|
207 | { USBHID_STR_ID_MANUFACTURER, "VirtualBox" },
|
---|
208 | { USBHID_STR_ID_PRODUCT, "USB Keyboard" },
|
---|
209 | };
|
---|
210 |
|
---|
211 | static const PDMUSBDESCCACHELANG g_aUsbHidLanguages[] =
|
---|
212 | {
|
---|
213 | { 0x0409, RT_ELEMENTS(g_aUsbHidStrings_en_US), g_aUsbHidStrings_en_US }
|
---|
214 | };
|
---|
215 |
|
---|
216 | static const VUSBDESCENDPOINTEX g_aUsbHidEndpointDescs[] =
|
---|
217 | {
|
---|
218 | {
|
---|
219 | {
|
---|
220 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
221 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
222 | /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
|
---|
223 | /* .bmAttributes = */ 3 /* interrupt */,
|
---|
224 | /* .wMaxPacketSize = */ 8,
|
---|
225 | /* .bInterval = */ 10,
|
---|
226 | },
|
---|
227 | /* .pvMore = */ NULL,
|
---|
228 | /* .pvClass = */ NULL,
|
---|
229 | /* .cbClass = */ 0
|
---|
230 | },
|
---|
231 | };
|
---|
232 |
|
---|
233 | /** HID report descriptor. */
|
---|
234 | static const uint8_t g_UsbHidReportDesc[] =
|
---|
235 | {
|
---|
236 | /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
|
---|
237 | /* Usage */ 0x09, 0x06, /* Keyboard */
|
---|
238 | /* Collection */ 0xA1, 0x01, /* Application */
|
---|
239 | /* Usage Page */ 0x05, 0x07, /* Keyboard */
|
---|
240 | /* Usage Minimum */ 0x19, 0xE0, /* Left Ctrl Key */
|
---|
241 | /* Usage Maximum */ 0x29, 0xE7, /* Right GUI Key */
|
---|
242 | /* Logical Minimum */ 0x15, 0x00, /* 0 */
|
---|
243 | /* Logical Maximum */ 0x25, 0x01, /* 1 */
|
---|
244 | /* Report Count */ 0x95, 0x08, /* 8 */
|
---|
245 | /* Report Size */ 0x75, 0x01, /* 1 */
|
---|
246 | /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
|
---|
247 | /* Report Count */ 0x95, 0x01, /* 1 */
|
---|
248 | /* Report Size */ 0x75, 0x08, /* 8 (padding bits) */
|
---|
249 | /* Input */ 0x81, 0x01, /* Constant, Array, Absolute, Bit field */
|
---|
250 | /* Report Count */ 0x95, 0x05, /* 5 */
|
---|
251 | /* Report Size */ 0x75, 0x01, /* 1 */
|
---|
252 | /* Usage Page */ 0x05, 0x08, /* LEDs */
|
---|
253 | /* Usage Minimum */ 0x19, 0x01, /* Num Lock */
|
---|
254 | /* Usage Maximum */ 0x29, 0x05, /* Kana */
|
---|
255 | /* Output */ 0x91, 0x02, /* Data, Value, Absolute, Non-volatile,Bit field */
|
---|
256 | /* Report Count */ 0x95, 0x01, /* 1 */
|
---|
257 | /* Report Size */ 0x75, 0x03, /* 3 */
|
---|
258 | /* Output */ 0x91, 0x01, /* Constant, Value, Absolute, Non-volatile, Bit field */
|
---|
259 | /* Report Count */ 0x95, 0x06, /* 6 */
|
---|
260 | /* Report Size */ 0x75, 0x08, /* 8 */
|
---|
261 | /* Logical Minimum */ 0x15, 0x00, /* 0 */
|
---|
262 | /* Logical Maximum */ 0x26, 0xFF,0x00,/* 255 */
|
---|
263 | /* Usage Page */ 0x05, 0x07, /* Keyboard */
|
---|
264 | /* Usage Minimum */ 0x19, 0x00, /* 0 */
|
---|
265 | /* Usage Maximum */ 0x29, 0xFF, /* 255 */
|
---|
266 | /* Input */ 0x81, 0x00, /* Data, Array, Absolute, Bit field */
|
---|
267 | /* End Collection */ 0xC0,
|
---|
268 | };
|
---|
269 |
|
---|
270 | /** Additional HID class interface descriptor. */
|
---|
271 | static const uint8_t g_UsbHidIfHidDesc[] =
|
---|
272 | {
|
---|
273 | /* .bLength = */ 0x09,
|
---|
274 | /* .bDescriptorType = */ 0x21, /* HID */
|
---|
275 | /* .bcdHID = */ 0x10, 0x01, /* 1.1 */
|
---|
276 | /* .bCountryCode = */ 0x0D, /* International (ISO) */
|
---|
277 | /* .bNumDescriptors = */ 1,
|
---|
278 | /* .bDescriptorType = */ 0x22, /* Report */
|
---|
279 | /* .wDescriptorLength = */ sizeof(g_UsbHidReportDesc), 0x00
|
---|
280 | };
|
---|
281 |
|
---|
282 | static const VUSBDESCINTERFACEEX g_UsbHidInterfaceDesc =
|
---|
283 | {
|
---|
284 | {
|
---|
285 | /* .bLength = */ sizeof(VUSBDESCINTERFACE),
|
---|
286 | /* .bDescriptorType = */ VUSB_DT_INTERFACE,
|
---|
287 | /* .bInterfaceNumber = */ 0,
|
---|
288 | /* .bAlternateSetting = */ 0,
|
---|
289 | /* .bNumEndpoints = */ 1,
|
---|
290 | /* .bInterfaceClass = */ 3 /* HID */,
|
---|
291 | /* .bInterfaceSubClass = */ 1 /* Boot Interface */,
|
---|
292 | /* .bInterfaceProtocol = */ 1 /* Keyboard */,
|
---|
293 | /* .iInterface = */ 0
|
---|
294 | },
|
---|
295 | /* .pvMore = */ NULL,
|
---|
296 | /* .pvClass = */ &g_UsbHidIfHidDesc,
|
---|
297 | /* .cbClass = */ sizeof(g_UsbHidIfHidDesc),
|
---|
298 | &g_aUsbHidEndpointDescs[0]
|
---|
299 | };
|
---|
300 |
|
---|
301 | static const VUSBINTERFACE g_aUsbHidInterfaces[] =
|
---|
302 | {
|
---|
303 | { &g_UsbHidInterfaceDesc, /* .cSettings = */ 1 },
|
---|
304 | };
|
---|
305 |
|
---|
306 | static const VUSBDESCCONFIGEX g_UsbHidConfigDesc =
|
---|
307 | {
|
---|
308 | {
|
---|
309 | /* .bLength = */ sizeof(VUSBDESCCONFIG),
|
---|
310 | /* .bDescriptorType = */ VUSB_DT_CONFIG,
|
---|
311 | /* .wTotalLength = */ 0 /* recalculated on read */,
|
---|
312 | /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidInterfaces),
|
---|
313 | /* .bConfigurationValue =*/ 1,
|
---|
314 | /* .iConfiguration = */ 0,
|
---|
315 | /* .bmAttributes = */ RT_BIT(7),
|
---|
316 | /* .MaxPower = */ 50 /* 100mA */
|
---|
317 | },
|
---|
318 | NULL,
|
---|
319 | &g_aUsbHidInterfaces[0]
|
---|
320 | };
|
---|
321 |
|
---|
322 | static const VUSBDESCDEVICE g_UsbHidDeviceDesc =
|
---|
323 | {
|
---|
324 | /* .bLength = */ sizeof(g_UsbHidDeviceDesc),
|
---|
325 | /* .bDescriptorType = */ VUSB_DT_DEVICE,
|
---|
326 | /* .bcdUsb = */ 0x110, /* 1.1 */
|
---|
327 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
328 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
329 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
330 | /* .bMaxPacketSize0 = */ 8,
|
---|
331 | /* .idVendor = */ VBOX_USB_VENDOR,
|
---|
332 | /* .idProduct = */ USBHID_PID_KEYBOARD,
|
---|
333 | /* .bcdDevice = */ 0x0100, /* 1.0 */
|
---|
334 | /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
|
---|
335 | /* .iProduct = */ USBHID_STR_ID_PRODUCT,
|
---|
336 | /* .iSerialNumber = */ 0,
|
---|
337 | /* .bNumConfigurations = */ 1
|
---|
338 | };
|
---|
339 |
|
---|
340 | static const PDMUSBDESCCACHE g_UsbHidDescCache =
|
---|
341 | {
|
---|
342 | /* .pDevice = */ &g_UsbHidDeviceDesc,
|
---|
343 | /* .paConfigs = */ &g_UsbHidConfigDesc,
|
---|
344 | /* .paLanguages = */ g_aUsbHidLanguages,
|
---|
345 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
|
---|
346 | /* .fUseCachedDescriptors = */ true,
|
---|
347 | /* .fUseCachedStringsDescriptors = */ true
|
---|
348 | };
|
---|
349 |
|
---|
350 |
|
---|
351 | /*
|
---|
352 | * Because of historical reasons and poor design, VirtualBox internally uses BIOS
|
---|
353 | * PC/XT style scan codes to represent keyboard events. Each key press and release is
|
---|
354 | * represented as a stream of bytes, typically only one byte but up to four-byte
|
---|
355 | * sequences are possible. In the typical case, the GUI front end generates the stream
|
---|
356 | * of scan codes which we need to translate back to a single up/down event.
|
---|
357 | *
|
---|
358 | * This function could possibly live somewhere else.
|
---|
359 | */
|
---|
360 |
|
---|
361 | /** Lookup table for converting PC/XT scan codes to USB HID usage codes. */
|
---|
362 | static uint8_t aScancode2Hid[] =
|
---|
363 | {
|
---|
364 | 0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, /* 00-07 */
|
---|
365 | 0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b, /* 08-1F */
|
---|
366 | 0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c, /* 10-17 */
|
---|
367 | 0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16, /* 18-1F */
|
---|
368 | 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33, /* 20-27 */
|
---|
369 | 0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19, /* 28-2F */
|
---|
370 | 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55, /* 30-37 */
|
---|
371 | 0xe2, 0x2c, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, /* 38-3F */
|
---|
372 | 0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f, /* 40-47 */
|
---|
373 | 0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59, /* 48-4F */
|
---|
374 | 0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x64, 0x44, /* 50-57 */
|
---|
375 | 0x45, 0x67, 0x00, 0x00, 0x8c, 0x00, 0x00, 0x00, /* 58-5F */
|
---|
376 | 0x00, 0x00, 0x00, 0x00, 0x68, 0x69, 0x6a, 0x6b, /* 60-67 */
|
---|
377 | 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, /* 68-6F */
|
---|
378 | 0x88, 0x00, 0x00, 0x87, 0x00, 0x00, 0x00, 0x00, /* 70-77 */
|
---|
379 | 0x00, 0x8a, 0x00, 0x8b, 0x00, 0x89, 0x85, 0x00 /* 78-7F */
|
---|
380 | };
|
---|
381 |
|
---|
382 | /** Lookup table for extended scancodes (arrow keys etc.). */
|
---|
383 | static uint8_t aExtScan2Hid[] =
|
---|
384 | {
|
---|
385 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00-07 */
|
---|
386 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 08-1F */
|
---|
387 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10-17 */
|
---|
388 | 0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00, /* 18-1F */
|
---|
389 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 20-27 */
|
---|
390 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28-2F */
|
---|
391 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46, /* 30-37 */
|
---|
392 | /* Sun-specific keys. Most of the XT codes are made up */
|
---|
393 | 0xe6, 0x00, 0x00, 0x75, 0x76, 0x77, 0xA3, 0x78, /* 38-3F */
|
---|
394 | 0x80, 0x81, 0x82, 0x79, 0x00, 0x48, 0x00, 0x4a, /* 40-47 */
|
---|
395 | 0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d, /* 48-4F */
|
---|
396 | 0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00, /* 50-57 */
|
---|
397 | 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x66, 0x00, /* 58-5F */
|
---|
398 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60-67 */
|
---|
399 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 68-6F */
|
---|
400 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70-77 */
|
---|
401 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* 78-7F */
|
---|
402 | };
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Convert a PC scan code to a USB HID usage byte.
|
---|
406 | *
|
---|
407 | * @param state Current state of the translator (scan_state_t).
|
---|
408 | * @param scanCode Incoming scan code.
|
---|
409 | * @param pUsage Pointer to usage; high bit set for key up events. The
|
---|
410 | * contents are only valid if returned state is SS_IDLE.
|
---|
411 | *
|
---|
412 | * @return scan_state_t New state of the translator.
|
---|
413 | */
|
---|
414 | static scan_state_t ScancodeToHidUsage(scan_state_t state, uint8_t scanCode, uint32_t *pUsage)
|
---|
415 | {
|
---|
416 | uint32_t keyUp;
|
---|
417 | uint8_t usage;
|
---|
418 |
|
---|
419 | Assert(pUsage);
|
---|
420 |
|
---|
421 | /* Isolate the scan code and key break flag. */
|
---|
422 | keyUp = (scanCode & 0x80) << 24;
|
---|
423 |
|
---|
424 | switch (state) {
|
---|
425 | case SS_IDLE:
|
---|
426 | if (scanCode == 0xE0) {
|
---|
427 | state = SS_EXT;
|
---|
428 | } else if (scanCode == 0xE1) {
|
---|
429 | state = SS_EXT1;
|
---|
430 | } else {
|
---|
431 | usage = aScancode2Hid[scanCode & 0x7F];
|
---|
432 | *pUsage = usage | keyUp;
|
---|
433 | /* Remain in SS_IDLE state. */
|
---|
434 | }
|
---|
435 | break;
|
---|
436 | case SS_EXT:
|
---|
437 | usage = aExtScan2Hid[scanCode & 0x7F];
|
---|
438 | *pUsage = usage | keyUp;
|
---|
439 | state = SS_IDLE;
|
---|
440 | break;
|
---|
441 | case SS_EXT1:
|
---|
442 | Assert(0); //@todo - sort out the Pause key
|
---|
443 | *pUsage = 0;
|
---|
444 | state = SS_IDLE;
|
---|
445 | break;
|
---|
446 | }
|
---|
447 | return state;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /*******************************************************************************
|
---|
451 | * Internal Functions *
|
---|
452 | *******************************************************************************/
|
---|
453 |
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Initializes an URB queue.
|
---|
457 | *
|
---|
458 | * @param pQueue The URB queue.
|
---|
459 | */
|
---|
460 | static void usbHidQueueInit(PUSBHIDURBQUEUE pQueue)
|
---|
461 | {
|
---|
462 | pQueue->pHead = NULL;
|
---|
463 | pQueue->ppTail = &pQueue->pHead;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /**
|
---|
467 | * Inserts an URB at the end of the queue.
|
---|
468 | *
|
---|
469 | * @param pQueue The URB queue.
|
---|
470 | * @param pUrb The URB to insert.
|
---|
471 | */
|
---|
472 | DECLINLINE(void) usbHidQueueAddTail(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
|
---|
473 | {
|
---|
474 | pUrb->Dev.pNext = NULL;
|
---|
475 | *pQueue->ppTail = pUrb;
|
---|
476 | pQueue->ppTail = &pUrb->Dev.pNext;
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 | /**
|
---|
481 | * Unlinks the head of the queue and returns it.
|
---|
482 | *
|
---|
483 | * @returns The head entry.
|
---|
484 | * @param pQueue The URB queue.
|
---|
485 | */
|
---|
486 | DECLINLINE(PVUSBURB) usbHidQueueRemoveHead(PUSBHIDURBQUEUE pQueue)
|
---|
487 | {
|
---|
488 | PVUSBURB pUrb = pQueue->pHead;
|
---|
489 | if (pUrb)
|
---|
490 | {
|
---|
491 | PVUSBURB pNext = pUrb->Dev.pNext;
|
---|
492 | pQueue->pHead = pNext;
|
---|
493 | if (!pNext)
|
---|
494 | pQueue->ppTail = &pQueue->pHead;
|
---|
495 | else
|
---|
496 | pUrb->Dev.pNext = NULL;
|
---|
497 | }
|
---|
498 | return pUrb;
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | /**
|
---|
503 | * Removes an URB from anywhere in the queue.
|
---|
504 | *
|
---|
505 | * @returns true if found, false if not.
|
---|
506 | * @param pQueue The URB queue.
|
---|
507 | * @param pUrb The URB to remove.
|
---|
508 | */
|
---|
509 | DECLINLINE(bool) usbHidQueueRemove(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
|
---|
510 | {
|
---|
511 | PVUSBURB pCur = pQueue->pHead;
|
---|
512 | if (pCur == pUrb)
|
---|
513 | pQueue->pHead = pUrb->Dev.pNext;
|
---|
514 | else
|
---|
515 | {
|
---|
516 | while (pCur)
|
---|
517 | {
|
---|
518 | if (pCur->Dev.pNext == pUrb)
|
---|
519 | {
|
---|
520 | pCur->Dev.pNext = pUrb->Dev.pNext;
|
---|
521 | break;
|
---|
522 | }
|
---|
523 | pCur = pCur->Dev.pNext;
|
---|
524 | }
|
---|
525 | if (!pCur)
|
---|
526 | return false;
|
---|
527 | }
|
---|
528 | if (!pUrb->Dev.pNext)
|
---|
529 | pQueue->ppTail = &pQueue->pHead;
|
---|
530 | return true;
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Checks if the queue is empty or not.
|
---|
536 | *
|
---|
537 | * @returns true if it is, false if it isn't.
|
---|
538 | * @param pQueue The URB queue.
|
---|
539 | */
|
---|
540 | DECLINLINE(bool) usbHidQueueIsEmpty(PCUSBHIDURBQUEUE pQueue)
|
---|
541 | {
|
---|
542 | return pQueue->pHead == NULL;
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | /**
|
---|
547 | * Links an URB into the done queue.
|
---|
548 | *
|
---|
549 | * @param pThis The HID instance.
|
---|
550 | * @param pUrb The URB.
|
---|
551 | */
|
---|
552 | static void usbHidLinkDone(PUSBHID pThis, PVUSBURB pUrb)
|
---|
553 | {
|
---|
554 | usbHidQueueAddTail(&pThis->DoneQueue, pUrb);
|
---|
555 |
|
---|
556 | if (pThis->fHaveDoneQueueWaiter)
|
---|
557 | {
|
---|
558 | int rc = RTSemEventSignal(pThis->hEvtDoneQueue);
|
---|
559 | AssertRC(rc);
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 |
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Completes the URB with a stalled state, halting the pipe.
|
---|
567 | */
|
---|
568 | static int usbHidCompleteStall(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb, const char *pszWhy)
|
---|
569 | {
|
---|
570 | Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
|
---|
571 |
|
---|
572 | pUrb->enmStatus = VUSBSTATUS_STALL;
|
---|
573 |
|
---|
574 | /** @todo figure out if the stall is global or pipe-specific or both. */
|
---|
575 | if (pEp)
|
---|
576 | pEp->fHalted = true;
|
---|
577 | else
|
---|
578 | {
|
---|
579 | pThis->aEps[1].fHalted = true;
|
---|
580 | pThis->aEps[2].fHalted = true;
|
---|
581 | }
|
---|
582 |
|
---|
583 | usbHidLinkDone(pThis, pUrb);
|
---|
584 | return VINF_SUCCESS;
|
---|
585 | }
|
---|
586 |
|
---|
587 |
|
---|
588 | /**
|
---|
589 | * Completes the URB with a OK state.
|
---|
590 | */
|
---|
591 | static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
|
---|
592 | {
|
---|
593 | Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
|
---|
594 |
|
---|
595 | pUrb->enmStatus = VUSBSTATUS_OK;
|
---|
596 | pUrb->cbData = (uint32_t)cbData;
|
---|
597 |
|
---|
598 | usbHidLinkDone(pThis, pUrb);
|
---|
599 | return VINF_SUCCESS;
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | /**
|
---|
604 | * Reset worker for usbHidUsbReset, usbHidUsbSetConfiguration and
|
---|
605 | * usbHidUrbHandleDefaultPipe.
|
---|
606 | *
|
---|
607 | * @returns VBox status code.
|
---|
608 | * @param pThis The HID instance.
|
---|
609 | * @param pUrb Set when usbHidUrbHandleDefaultPipe is the
|
---|
610 | * caller.
|
---|
611 | * @param fSetConfig Set when usbHidUsbSetConfiguration is the
|
---|
612 | * caller.
|
---|
613 | */
|
---|
614 | static int usbHidResetWorker(PUSBHID pThis, PVUSBURB pUrb, bool fSetConfig)
|
---|
615 | {
|
---|
616 | /*
|
---|
617 | * Deactivate the keyboard.
|
---|
618 | */
|
---|
619 | pThis->Lun0.pDrv->pfnSetActive(pThis->Lun0.pDrv, false);
|
---|
620 |
|
---|
621 | /*
|
---|
622 | * Reset the device state.
|
---|
623 | */
|
---|
624 | pThis->enmState = USBHIDREQSTATE_READY;
|
---|
625 | pThis->bIdle = 0;
|
---|
626 | pThis->fHasPendingChanges = false;
|
---|
627 |
|
---|
628 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
|
---|
629 | pThis->aEps[i].fHalted = false;
|
---|
630 |
|
---|
631 | if (!pUrb && !fSetConfig) /* (only device reset) */
|
---|
632 | pThis->bConfigurationValue = 0; /* default */
|
---|
633 |
|
---|
634 | /*
|
---|
635 | * Ditch all pending URBs.
|
---|
636 | */
|
---|
637 | PVUSBURB pCurUrb;
|
---|
638 | while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
|
---|
639 | {
|
---|
640 | pCurUrb->enmStatus = VUSBSTATUS_CRC;
|
---|
641 | usbHidLinkDone(pThis, pCurUrb);
|
---|
642 | }
|
---|
643 |
|
---|
644 | if (pUrb)
|
---|
645 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
646 | return VINF_SUCCESS;
|
---|
647 | }
|
---|
648 |
|
---|
649 | #ifdef DEBUG
|
---|
650 | # define HEX_DIGIT(x) (((x) < 0xa) ? ((x) + '0') : ((x) - 0xa + 'a'))
|
---|
651 | static void usbHidComputePressed(PUSBHIDK_REPORT pReport, char* pszBuf, unsigned cbBuf)
|
---|
652 | {
|
---|
653 | unsigned offBuf = 0;
|
---|
654 | unsigned i;
|
---|
655 | for (i = 0; i < RT_ELEMENTS(pReport->aKeys); ++i)
|
---|
656 | {
|
---|
657 | uint8_t uCode = pReport->aKeys[i];
|
---|
658 | if (uCode != 0)
|
---|
659 | {
|
---|
660 | if (offBuf + 4 >= cbBuf)
|
---|
661 | break;
|
---|
662 | pszBuf[offBuf++] = HEX_DIGIT(uCode >> 4);
|
---|
663 | pszBuf[offBuf++] = HEX_DIGIT(uCode & 0xf);
|
---|
664 | pszBuf[offBuf++] = ' ';
|
---|
665 | }
|
---|
666 | }
|
---|
667 | pszBuf[offBuf++] = '\0';
|
---|
668 | }
|
---|
669 | # undef HEX_DIGIT
|
---|
670 | #endif
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Returns true if the usage code corresponds to a keyboard modifier key
|
---|
674 | * (left or right ctrl, shift, alt or GUI). The usage codes for these keys
|
---|
675 | * are the range 0xe0 to 0xe7.
|
---|
676 | */
|
---|
677 | static bool usbHidUsageCodeIsModifier(uint8_t u8Usage)
|
---|
678 | {
|
---|
679 | return u8Usage >= 0xe0 && u8Usage <= 0xe7;
|
---|
680 | }
|
---|
681 |
|
---|
682 | /**
|
---|
683 | * Convert a USB HID usage code to a keyboard modifier flag. The arithmetic
|
---|
684 | * is simple: the modifier keys have usage codes from 0xe0 to 0xe7, and the
|
---|
685 | * lower nibble is the bit number of the flag.
|
---|
686 | */
|
---|
687 | static uint8_t usbHidModifierToFlag(uint8_t u8Usage)
|
---|
688 | {
|
---|
689 | Assert(usbHidUsageCodeIsModifier(u8Usage));
|
---|
690 | return RT_BIT(u8Usage & 0xf);
|
---|
691 | }
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Create a USB HID keyboard report based on a vector of keys which have been
|
---|
695 | * pressed since the last report was created (so that we don't miss keys that
|
---|
696 | * are only pressed briefly) and a vector of currently depressed keys.
|
---|
697 | * The keys in the report aKeys array are in increasing order (important for
|
---|
698 | * the test case).
|
---|
699 | */
|
---|
700 | static int usbHidFillReport(PUSBHIDK_REPORT pReport,
|
---|
701 | uint8_t *pabUnreportedKeys,
|
---|
702 | uint8_t *pabDepressedKeys)
|
---|
703 | {
|
---|
704 | unsigned iBuf = 0;
|
---|
705 | RT_ZERO(*pReport);
|
---|
706 | for (unsigned iKey = 0; iKey < VBOX_USB_MAX_USAGE_KBD; ++iKey)
|
---|
707 | {
|
---|
708 | AssertReturn(iBuf <= RT_ELEMENTS(pReport->aKeys),
|
---|
709 | VERR_INTERNAL_ERROR);
|
---|
710 | if (pabUnreportedKeys[iKey] || pabDepressedKeys[iKey])
|
---|
711 | {
|
---|
712 | if (usbHidUsageCodeIsModifier(iKey))
|
---|
713 | pReport->ShiftState |= usbHidModifierToFlag(iKey);
|
---|
714 | else if (iBuf == RT_ELEMENTS(pReport->aKeys))
|
---|
715 | {
|
---|
716 | /* The USB HID spec says that the entire vector should be
|
---|
717 | * set to ErrorRollOver on overflow. We don't mind if this
|
---|
718 | * path is taken several times for one report. */
|
---|
719 | for (unsigned iBuf2 = 0;
|
---|
720 | iBuf2 < RT_ELEMENTS(pReport->aKeys); ++iBuf2)
|
---|
721 | pReport->aKeys[iBuf2] = USBHID_USAGE_ROLL_OVER;
|
---|
722 | }
|
---|
723 | else
|
---|
724 | {
|
---|
725 | pReport->aKeys[iBuf] = iKey;
|
---|
726 | ++iBuf;
|
---|
727 | }
|
---|
728 | pabUnreportedKeys[iKey] = 0;
|
---|
729 | }
|
---|
730 | }
|
---|
731 | return VINF_SUCCESS;
|
---|
732 | }
|
---|
733 |
|
---|
734 | #ifdef DEBUG
|
---|
735 | /** Test data for testing usbHidFillReport(). The format is:
|
---|
736 | * - Unreported keys (zero terminated array)
|
---|
737 | * - Depressed keys (zero terminated array)
|
---|
738 | * - Expected shift state in the report (single byte inside array)
|
---|
739 | * - Expected keys buffer contents (array of six bytes)
|
---|
740 | */
|
---|
741 | static const uint8_t testUsbHidFillReportData[][4][10] = {
|
---|
742 | /* Just unreported, no modifiers */
|
---|
743 | {{4, 9, 0}, {0}, {0}, {4, 9, 0, 0, 0, 0}},
|
---|
744 | /* Just unreported, one modifier */
|
---|
745 | {{4, 9, 0xe2, 0}, {0}, {4}, {4, 9, 0, 0, 0, 0}},
|
---|
746 | /* Just unreported, two modifiers */
|
---|
747 | {{4, 9, 0xe2, 0xe4, 0}, {0}, {20}, {4, 9, 0, 0, 0, 0}},
|
---|
748 | /* Just depressed, no modifiers */
|
---|
749 | {{0}, {7, 20, 0}, {0}, {7, 20, 0, 0, 0, 0}},
|
---|
750 | /* Just depressed, one modifier */
|
---|
751 | {{0}, {7, 20, 0xe3, 0}, {8}, {7, 20, 0, 0, 0, 0}},
|
---|
752 | /* Just depressed, two modifiers */
|
---|
753 | {{0}, {7, 20, 0xe3, 0xe6, 0}, {72}, {7, 20, 0, 0, 0, 0}},
|
---|
754 | /* Unreported and depressed, no overlap, no modifiers */
|
---|
755 | {{5, 10, 0}, {8, 21, 0}, {0}, {5, 8, 10, 21, 0, 0}},
|
---|
756 | /* Unreported and depressed, one overlap, no modifiers */
|
---|
757 | {{5, 10, 0}, {8, 10, 21, 0}, {0}, {5, 8, 10, 21, 0, 0}},
|
---|
758 | /* Unreported and depressed, no overlap, non-overlapping modifiers */
|
---|
759 | {{5, 10, 0xe2, 0xe4, 0}, {8, 21, 0xe3, 0xe6, 0}, {92},
|
---|
760 | {5, 8, 10, 21, 0, 0}},
|
---|
761 | /* Unreported and depressed, one overlap, non-overlapping modifiers */
|
---|
762 | {{5, 10, 21, 0xe2, 0xe4, 0}, {8, 21, 0xe3, 0xe6, 0}, {92},
|
---|
763 | {5, 8, 10, 21, 0, 0}},
|
---|
764 | /* Unreported and depressed, no overlap, overlapping modifiers */
|
---|
765 | {{5, 10, 0xe2, 0xe4, 0}, {8, 21, 0xe3, 0xe4, 0}, {28},
|
---|
766 | {5, 8, 10, 21, 0, 0}},
|
---|
767 | /* Unreported and depressed, one overlap, overlapping modifiers */
|
---|
768 | {{5, 10, 0xe2, 0xe4, 0}, {5, 8, 21, 0xe3, 0xe4, 0}, {28},
|
---|
769 | {5, 8, 10, 21, 0, 0}},
|
---|
770 | /* Just too many unreported, no modifiers */
|
---|
771 | {{4, 9, 11, 12, 16, 18, 20, 0}, {0}, {0}, {1, 1, 1, 1, 1, 1}},
|
---|
772 | /* Just too many unreported, two modifiers */
|
---|
773 | {{4, 9, 11, 12, 16, 18, 20, 0xe2, 0xe4, 0}, {0}, {20},
|
---|
774 | {1, 1, 1, 1, 1, 1}},
|
---|
775 | /* Just too many depressed, no modifiers */
|
---|
776 | {{0}, {7, 20, 22, 25, 27, 29, 34, 0}, {0}, {1, 1, 1, 1, 1, 1}},
|
---|
777 | /* Just too many depressed, two modifiers */
|
---|
778 | {{0}, {7, 20, 22, 25, 27, 29, 34, 0xe3, 0xe5, 0}, {40},
|
---|
779 | {1, 1, 1, 1, 1, 1}},
|
---|
780 | /* Too many unreported and depressed, no overlap, no modifiers */
|
---|
781 | {{5, 10, 12, 13, 0}, {8, 9, 21, 0}, {0}, {1, 1, 1, 1, 1, 1}},
|
---|
782 | /* Eight unreported and depressed total, one overlap, no modifiers */
|
---|
783 | {{5, 10, 12, 13, 0}, {8, 10, 21, 22, 0}, {0}, {1, 1, 1, 1, 1, 1}},
|
---|
784 | /* Seven unreported and depressed total, one overlap, no modifiers */
|
---|
785 | {{5, 10, 12, 13, 0}, {8, 10, 21, 0}, {0}, {5, 8, 10, 12, 13, 21}},
|
---|
786 | /* Too many unreported and depressed, no overlap, two modifiers */
|
---|
787 | {{5, 10, 12, 13, 0xe2, 0}, {8, 9, 21, 0xe4, 0}, {20},
|
---|
788 | {1, 1, 1, 1, 1, 1}},
|
---|
789 | /* Eight unreported and depressed total, one overlap, two modifiers */
|
---|
790 | {{5, 10, 12, 13, 0xe1, 0}, {8, 10, 21, 22, 0xe2, 0}, {6},
|
---|
791 | {1, 1, 1, 1, 1, 1}},
|
---|
792 | /* Seven unreported and depressed total, one overlap, two modifiers */
|
---|
793 | {{5, 10, 12, 13, 0xe2, 0}, {8, 10, 21, 0xe3, 0}, {12},
|
---|
794 | {5, 8, 10, 12, 13, 21}}
|
---|
795 | };
|
---|
796 |
|
---|
797 | /** Test case for usbHidFillReport() */
|
---|
798 | class testUsbHidFillReport
|
---|
799 | {
|
---|
800 | USBHIDK_REPORT mReport;
|
---|
801 | uint8_t mabUnreportedKeys[VBOX_USB_MAX_USAGE_KBD];
|
---|
802 | uint8_t mabDepressedKeys[VBOX_USB_MAX_USAGE_KBD];
|
---|
803 | const uint8_t (*mTests)[4][10];
|
---|
804 |
|
---|
805 | void doTest(unsigned cTest, const uint8_t *paiUnreportedKeys,
|
---|
806 | const uint8_t *paiDepressedKeys, uint8_t aExpShiftState,
|
---|
807 | const uint8_t *pabExpKeys)
|
---|
808 | {
|
---|
809 | RT_ZERO(mReport);
|
---|
810 | RT_ZERO(mabUnreportedKeys);
|
---|
811 | RT_ZERO(mabDepressedKeys);
|
---|
812 | for (unsigned i = 0; paiUnreportedKeys[i] != 0; ++i)
|
---|
813 | mabUnreportedKeys[paiUnreportedKeys[i]] = 1;
|
---|
814 | for (unsigned i = 0; paiDepressedKeys[i] != 0; ++i)
|
---|
815 | mabUnreportedKeys[paiDepressedKeys[i]] = 1;
|
---|
816 | int rc = usbHidFillReport(&mReport, mabUnreportedKeys, mabDepressedKeys);
|
---|
817 | AssertMsgRC(rc, ("test %u\n", cTest));
|
---|
818 | AssertMsg(mReport.ShiftState == aExpShiftState, ("test %u\n", cTest));
|
---|
819 | for (unsigned i = 0; i < RT_ELEMENTS(mReport.aKeys); ++i)
|
---|
820 | AssertMsg(mReport.aKeys[i] == pabExpKeys[i], ("test %u\n", cTest));
|
---|
821 | }
|
---|
822 |
|
---|
823 | public:
|
---|
824 | testUsbHidFillReport(void) : mTests(&testUsbHidFillReportData[0])
|
---|
825 | {
|
---|
826 | for (unsigned i = 0; i < RT_ELEMENTS(testUsbHidFillReportData); ++i)
|
---|
827 | doTest(i, mTests[i][0], mTests[i][1], mTests[i][2][0],
|
---|
828 | mTests[i][3]);
|
---|
829 | }
|
---|
830 | };
|
---|
831 |
|
---|
832 | static testUsbHidFillReport gsTestUsbHidFillReport;
|
---|
833 | #endif
|
---|
834 |
|
---|
835 | /**
|
---|
836 | * Sends a state report to the host if there is a pending URB.
|
---|
837 | */
|
---|
838 | static int usbHidSendReport(PUSBHID pThis)
|
---|
839 | {
|
---|
840 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
|
---|
841 | if (pUrb)
|
---|
842 | {
|
---|
843 | PUSBHIDK_REPORT pReport = (PUSBHIDK_REPORT)&pUrb->abData[0];
|
---|
844 |
|
---|
845 | int rc = usbHidFillReport(pReport, pThis->abUnreportedKeys,
|
---|
846 | pThis->abDepressedKeys);
|
---|
847 | pThis->fHasPendingChanges = false;
|
---|
848 | AssertRCReturn(rc, rc);
|
---|
849 | return usbHidCompleteOk(pThis, pUrb, sizeof(*pReport));
|
---|
850 | }
|
---|
851 | else
|
---|
852 | {
|
---|
853 | Log2(("No available URB for USB kbd\n"));
|
---|
854 | pThis->fHasPendingChanges = true;
|
---|
855 | }
|
---|
856 | return VINF_EOF;
|
---|
857 | }
|
---|
858 |
|
---|
859 | /**
|
---|
860 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
861 | */
|
---|
862 | static DECLCALLBACK(void *) usbHidKeyboardQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
863 | {
|
---|
864 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
|
---|
865 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
|
---|
866 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDPORT, &pThis->Lun0.IPort);
|
---|
867 | return NULL;
|
---|
868 | }
|
---|
869 |
|
---|
870 | /**
|
---|
871 | * Keyboard event handler.
|
---|
872 | *
|
---|
873 | * @returns VBox status code.
|
---|
874 | * @param pInterface Pointer to the keyboard port interface (KBDState::Keyboard.IPort).
|
---|
875 | * @param u8KeyCode The keycode.
|
---|
876 | */
|
---|
877 | static DECLCALLBACK(int) usbHidKeyboardPutEvent(PPDMIKEYBOARDPORT pInterface, uint8_t u8KeyCode)
|
---|
878 | {
|
---|
879 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
|
---|
880 | uint32_t u32Usage = 0;
|
---|
881 | uint8_t u8HidCode;
|
---|
882 | int fKeyDown;
|
---|
883 | bool fHaveEvent = true;
|
---|
884 | unsigned i;
|
---|
885 |
|
---|
886 | RTCritSectEnter(&pThis->CritSect);
|
---|
887 |
|
---|
888 | pThis->XlatState = ScancodeToHidUsage(pThis->XlatState, u8KeyCode, &u32Usage);
|
---|
889 |
|
---|
890 | if (pThis->XlatState == SS_IDLE)
|
---|
891 | {
|
---|
892 | /* The usage code is valid. */
|
---|
893 | fKeyDown = !(u32Usage & 0x80000000);
|
---|
894 | u8HidCode = u32Usage & 0xFF;
|
---|
895 | AssertReturn(u8HidCode <= VBOX_USB_MAX_USAGE_KBD, VERR_INTERNAL_ERROR);
|
---|
896 |
|
---|
897 | LogRelFlowFunc(("key %s: 0x%x->0x%x\n",
|
---|
898 | fKeyDown ? "down" : "up", u8KeyCode, u8HidCode));
|
---|
899 |
|
---|
900 | if (fKeyDown)
|
---|
901 | {
|
---|
902 | /* Due to host key repeat, we can get key events for keys which are
|
---|
903 | * already depressed. */
|
---|
904 | if (!pThis->abDepressedKeys[u8HidCode])
|
---|
905 | pThis->abUnreportedKeys[u8HidCode] = 1;
|
---|
906 | else
|
---|
907 | fHaveEvent = false;
|
---|
908 | pThis->abDepressedKeys[u8HidCode] = 1;
|
---|
909 | }
|
---|
910 | else
|
---|
911 | pThis->abDepressedKeys[u8HidCode] = 0;
|
---|
912 |
|
---|
913 | /* Send a report if the host is already waiting for it. */
|
---|
914 | if (fHaveEvent)
|
---|
915 | usbHidSendReport(pThis);
|
---|
916 | }
|
---|
917 |
|
---|
918 | RTCritSectLeave(&pThis->CritSect);
|
---|
919 |
|
---|
920 | return VINF_SUCCESS;
|
---|
921 | }
|
---|
922 |
|
---|
923 | /**
|
---|
924 | * @copydoc PDMUSBREG::pfnUrbReap
|
---|
925 | */
|
---|
926 | static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
|
---|
927 | {
|
---|
928 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
929 | //LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
|
---|
930 |
|
---|
931 | RTCritSectEnter(&pThis->CritSect);
|
---|
932 |
|
---|
933 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
|
---|
934 | if (!pUrb && cMillies)
|
---|
935 | {
|
---|
936 | /* Wait */
|
---|
937 | pThis->fHaveDoneQueueWaiter = true;
|
---|
938 | RTCritSectLeave(&pThis->CritSect);
|
---|
939 |
|
---|
940 | RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
|
---|
941 |
|
---|
942 | RTCritSectEnter(&pThis->CritSect);
|
---|
943 | pThis->fHaveDoneQueueWaiter = false;
|
---|
944 |
|
---|
945 | pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
|
---|
946 | }
|
---|
947 |
|
---|
948 | RTCritSectLeave(&pThis->CritSect);
|
---|
949 |
|
---|
950 | if (pUrb)
|
---|
951 | Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
952 | return pUrb;
|
---|
953 | }
|
---|
954 |
|
---|
955 |
|
---|
956 | /**
|
---|
957 | * @copydoc PDMUSBREG::pfnUrbCancel
|
---|
958 | */
|
---|
959 | static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
960 | {
|
---|
961 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
962 | LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
963 | RTCritSectEnter(&pThis->CritSect);
|
---|
964 |
|
---|
965 | /*
|
---|
966 | * Remove the URB from the to-host queue and move it onto the done queue.
|
---|
967 | */
|
---|
968 | if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
|
---|
969 | usbHidLinkDone(pThis, pUrb);
|
---|
970 |
|
---|
971 | RTCritSectLeave(&pThis->CritSect);
|
---|
972 | return VINF_SUCCESS;
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | /**
|
---|
977 | * Handles request sent to the inbound (device to host) interrupt pipe. This is
|
---|
978 | * rather different from bulk requests because an interrupt read URB may complete
|
---|
979 | * after arbitrarily long time.
|
---|
980 | */
|
---|
981 | static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
|
---|
982 | {
|
---|
983 | /*
|
---|
984 | * Stall the request if the pipe is halted.
|
---|
985 | */
|
---|
986 | if (RT_UNLIKELY(pEp->fHalted))
|
---|
987 | return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
|
---|
988 |
|
---|
989 | /*
|
---|
990 | * Deal with the URB according to the state.
|
---|
991 | */
|
---|
992 | switch (pThis->enmState)
|
---|
993 | {
|
---|
994 | /*
|
---|
995 | * We've data left to transfer to the host.
|
---|
996 | */
|
---|
997 | case USBHIDREQSTATE_DATA_TO_HOST:
|
---|
998 | {
|
---|
999 | AssertFailed();
|
---|
1000 | Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
|
---|
1001 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /*
|
---|
1005 | * Status transfer.
|
---|
1006 | */
|
---|
1007 | case USBHIDREQSTATE_STATUS:
|
---|
1008 | {
|
---|
1009 | AssertFailed();
|
---|
1010 | Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
|
---|
1011 | pThis->enmState = USBHIDREQSTATE_READY;
|
---|
1012 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | case USBHIDREQSTATE_READY:
|
---|
1016 | usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
|
---|
1017 | /* If device was not set idle, sent the current report right away. */
|
---|
1018 | if (pThis->bIdle != 0 || pThis->fHasPendingChanges)
|
---|
1019 | usbHidSendReport(pThis);
|
---|
1020 | LogFlow(("usbHidHandleIntrDevToHost: Sent report via %p:%s\n", pUrb, pUrb->pszDesc));
|
---|
1021 | return VINF_SUCCESS;
|
---|
1022 |
|
---|
1023 | /*
|
---|
1024 | * Bad states, stall.
|
---|
1025 | */
|
---|
1026 | default:
|
---|
1027 | Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
|
---|
1028 | return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 |
|
---|
1033 | /**
|
---|
1034 | * Handles request sent to the default control pipe.
|
---|
1035 | */
|
---|
1036 | static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
|
---|
1037 | {
|
---|
1038 | PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
|
---|
1039 | LogFlow(("usbHidHandleDefaultPipe: cbData=%d\n", pUrb->cbData));
|
---|
1040 |
|
---|
1041 | AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
|
---|
1042 |
|
---|
1043 | if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
|
---|
1044 | {
|
---|
1045 | switch (pSetup->bRequest)
|
---|
1046 | {
|
---|
1047 | case VUSB_REQ_GET_DESCRIPTOR:
|
---|
1048 | {
|
---|
1049 | switch (pSetup->bmRequestType)
|
---|
1050 | {
|
---|
1051 | case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1052 | {
|
---|
1053 | switch (pSetup->wValue >> 8)
|
---|
1054 | {
|
---|
1055 | case VUSB_DT_STRING:
|
---|
1056 | Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1057 | break;
|
---|
1058 | default:
|
---|
1059 | Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1060 | break;
|
---|
1061 | }
|
---|
1062 | break;
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1066 | {
|
---|
1067 | switch (pSetup->wValue >> 8)
|
---|
1068 | {
|
---|
1069 | case DT_IF_HID_REPORT:
|
---|
1070 | uint32_t cbCopy;
|
---|
1071 |
|
---|
1072 | /* Returned data is written after the setup message. */
|
---|
1073 | cbCopy = pUrb->cbData - sizeof(*pSetup);
|
---|
1074 | cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidReportDesc));
|
---|
1075 | Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
|
---|
1076 | memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidReportDesc, cbCopy);
|
---|
1077 | return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
|
---|
1078 | default:
|
---|
1079 | Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1080 | break;
|
---|
1081 | }
|
---|
1082 | break;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | default:
|
---|
1086 | Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
|
---|
1087 | return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
|
---|
1088 | }
|
---|
1089 | break;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | case VUSB_REQ_GET_STATUS:
|
---|
1093 | {
|
---|
1094 | uint16_t wRet = 0;
|
---|
1095 |
|
---|
1096 | if (pSetup->wLength != 2)
|
---|
1097 | {
|
---|
1098 | Log(("usbHid: Bad GET_STATUS req: wLength=%#x\n", pSetup->wLength));
|
---|
1099 | break;
|
---|
1100 | }
|
---|
1101 | Assert(pSetup->wValue == 0);
|
---|
1102 | switch (pSetup->bmRequestType)
|
---|
1103 | {
|
---|
1104 | case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1105 | {
|
---|
1106 | Assert(pSetup->wIndex == 0);
|
---|
1107 | Log(("usbHid: GET_STATUS (device)\n"));
|
---|
1108 | wRet = 0; /* Not self-powered, no remote wakeup. */
|
---|
1109 | memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
|
---|
1110 | return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1114 | {
|
---|
1115 | if (pSetup->wIndex == 0)
|
---|
1116 | {
|
---|
1117 | memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
|
---|
1118 | return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
|
---|
1119 | }
|
---|
1120 | else
|
---|
1121 | {
|
---|
1122 | Log(("usbHid: GET_STATUS (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
|
---|
1123 | }
|
---|
1124 | break;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1128 | {
|
---|
1129 | if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
|
---|
1130 | {
|
---|
1131 | wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
|
---|
1132 | memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
|
---|
1133 | return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
|
---|
1134 | }
|
---|
1135 | else
|
---|
1136 | {
|
---|
1137 | Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
|
---|
1138 | }
|
---|
1139 | break;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | default:
|
---|
1143 | Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
|
---|
1144 | return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
|
---|
1145 | }
|
---|
1146 | break;
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | case VUSB_REQ_CLEAR_FEATURE:
|
---|
1150 | break;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /** @todo implement this. */
|
---|
1154 | Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1155 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1156 |
|
---|
1157 | usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
|
---|
1158 | }
|
---|
1159 | else if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_CLASS)
|
---|
1160 | {
|
---|
1161 | switch (pSetup->bRequest)
|
---|
1162 | {
|
---|
1163 | case HID_REQ_SET_IDLE:
|
---|
1164 | {
|
---|
1165 | switch (pSetup->bmRequestType)
|
---|
1166 | {
|
---|
1167 | case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
|
---|
1168 | {
|
---|
1169 | Log(("usbHid: SET_IDLE wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1170 | pThis->bIdle = pSetup->wValue >> 8;
|
---|
1171 | /* Consider 24ms to mean zero for keyboards (see IOUSBHIDDriver) */
|
---|
1172 | if (pThis->bIdle == 6) pThis->bIdle = 0;
|
---|
1173 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
1174 | }
|
---|
1175 | break;
|
---|
1176 | }
|
---|
1177 | break;
|
---|
1178 | }
|
---|
1179 | case HID_REQ_GET_IDLE:
|
---|
1180 | {
|
---|
1181 | switch (pSetup->bmRequestType)
|
---|
1182 | {
|
---|
1183 | case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_HOST:
|
---|
1184 | {
|
---|
1185 | Log(("usbHid: GET_IDLE wValue=%#x wIndex=%#x, returning %#x\n", pSetup->wValue, pSetup->wIndex, pThis->bIdle));
|
---|
1186 | pUrb->abData[sizeof(*pSetup)] = pThis->bIdle;
|
---|
1187 | return usbHidCompleteOk(pThis, pUrb, 1);
|
---|
1188 | }
|
---|
1189 | break;
|
---|
1190 | }
|
---|
1191 | break;
|
---|
1192 | }
|
---|
1193 | }
|
---|
1194 | Log(("usbHid: Unimplemented class request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1195 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1196 |
|
---|
1197 | usbHidCompleteStall(pThis, pEp, pUrb, "TODO: class request stuff");
|
---|
1198 | }
|
---|
1199 | else
|
---|
1200 | {
|
---|
1201 | Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1202 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1203 | return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | return VINF_SUCCESS;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 |
|
---|
1210 | /**
|
---|
1211 | * @copydoc PDMUSBREG::pfnQueue
|
---|
1212 | */
|
---|
1213 | static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
1214 | {
|
---|
1215 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1216 | LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
|
---|
1217 | RTCritSectEnter(&pThis->CritSect);
|
---|
1218 |
|
---|
1219 | /*
|
---|
1220 | * Parse on a per end-point basis.
|
---|
1221 | */
|
---|
1222 | int rc;
|
---|
1223 | switch (pUrb->EndPt)
|
---|
1224 | {
|
---|
1225 | case 0:
|
---|
1226 | rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
|
---|
1227 | break;
|
---|
1228 |
|
---|
1229 | case 0x81:
|
---|
1230 | AssertFailed();
|
---|
1231 | case 0x01:
|
---|
1232 | rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
|
---|
1233 | break;
|
---|
1234 |
|
---|
1235 | default:
|
---|
1236 | AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
|
---|
1237 | rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
|
---|
1238 | break;
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | RTCritSectLeave(&pThis->CritSect);
|
---|
1242 | return rc;
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 |
|
---|
1246 | /**
|
---|
1247 | * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
|
---|
1248 | */
|
---|
1249 | static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
|
---|
1250 | {
|
---|
1251 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1252 | LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
|
---|
1253 |
|
---|
1254 | if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
|
---|
1255 | {
|
---|
1256 | RTCritSectEnter(&pThis->CritSect);
|
---|
1257 | pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
|
---|
1258 | RTCritSectLeave(&pThis->CritSect);
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | return VINF_SUCCESS;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 |
|
---|
1265 | /**
|
---|
1266 | * @copydoc PDMUSBREG::pfnUsbSetInterface
|
---|
1267 | */
|
---|
1268 | static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
|
---|
1269 | {
|
---|
1270 | LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
|
---|
1271 | Assert(bAlternateSetting == 0);
|
---|
1272 | return VINF_SUCCESS;
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 |
|
---|
1276 | /**
|
---|
1277 | * @copydoc PDMUSBREG::pfnUsbSetConfiguration
|
---|
1278 | */
|
---|
1279 | static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
|
---|
1280 | const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
|
---|
1281 | {
|
---|
1282 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1283 | LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
|
---|
1284 | Assert(bConfigurationValue == 1);
|
---|
1285 | RTCritSectEnter(&pThis->CritSect);
|
---|
1286 |
|
---|
1287 | /*
|
---|
1288 | * If the same config is applied more than once, it's a kind of reset.
|
---|
1289 | */
|
---|
1290 | if (pThis->bConfigurationValue == bConfigurationValue)
|
---|
1291 | usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
|
---|
1292 | pThis->bConfigurationValue = bConfigurationValue;
|
---|
1293 |
|
---|
1294 | /*
|
---|
1295 | * Tell the other end that the keyboard is now enabled and wants
|
---|
1296 | * to receive keystrokes.
|
---|
1297 | */
|
---|
1298 | pThis->Lun0.pDrv->pfnSetActive(pThis->Lun0.pDrv, true);
|
---|
1299 |
|
---|
1300 | RTCritSectLeave(&pThis->CritSect);
|
---|
1301 | return VINF_SUCCESS;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 |
|
---|
1305 | /**
|
---|
1306 | * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
|
---|
1307 | */
|
---|
1308 | static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
|
---|
1309 | {
|
---|
1310 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1311 | LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
|
---|
1312 | return &g_UsbHidDescCache;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 |
|
---|
1316 | /**
|
---|
1317 | * @copydoc PDMUSBREG::pfnUsbReset
|
---|
1318 | */
|
---|
1319 | static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
|
---|
1320 | {
|
---|
1321 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1322 | LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
|
---|
1323 | RTCritSectEnter(&pThis->CritSect);
|
---|
1324 |
|
---|
1325 | int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
|
---|
1326 |
|
---|
1327 | RTCritSectLeave(&pThis->CritSect);
|
---|
1328 | return rc;
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 |
|
---|
1332 | /**
|
---|
1333 | * @copydoc PDMUSBREG::pfnDestruct
|
---|
1334 | */
|
---|
1335 | static void usbHidDestruct(PPDMUSBINS pUsbIns)
|
---|
1336 | {
|
---|
1337 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1338 | LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
|
---|
1339 |
|
---|
1340 | if (RTCritSectIsInitialized(&pThis->CritSect))
|
---|
1341 | {
|
---|
1342 | /* Let whoever runs in this critical section complete. */
|
---|
1343 | RTCritSectEnter(&pThis->CritSect);
|
---|
1344 | RTCritSectLeave(&pThis->CritSect);
|
---|
1345 | RTCritSectDelete(&pThis->CritSect);
|
---|
1346 | }
|
---|
1347 |
|
---|
1348 | if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
|
---|
1349 | {
|
---|
1350 | RTSemEventDestroy(pThis->hEvtDoneQueue);
|
---|
1351 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
1352 | }
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 |
|
---|
1356 | /**
|
---|
1357 | * @copydoc PDMUSBREG::pfnConstruct
|
---|
1358 | */
|
---|
1359 | static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
|
---|
1360 | {
|
---|
1361 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1362 | Log(("usbHidConstruct/#%u:\n", iInstance));
|
---|
1363 |
|
---|
1364 | /*
|
---|
1365 | * Perform the basic structure initialization first so the destructor
|
---|
1366 | * will not misbehave.
|
---|
1367 | */
|
---|
1368 | pThis->pUsbIns = pUsbIns;
|
---|
1369 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
1370 | pThis->XlatState = SS_IDLE;
|
---|
1371 | usbHidQueueInit(&pThis->ToHostQueue);
|
---|
1372 | usbHidQueueInit(&pThis->DoneQueue);
|
---|
1373 |
|
---|
1374 | int rc = RTCritSectInit(&pThis->CritSect);
|
---|
1375 | AssertRCReturn(rc, rc);
|
---|
1376 |
|
---|
1377 | rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
|
---|
1378 | AssertRCReturn(rc, rc);
|
---|
1379 |
|
---|
1380 | /*
|
---|
1381 | * Validate and read the configuration.
|
---|
1382 | */
|
---|
1383 | rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbHid", iInstance);
|
---|
1384 | if (RT_FAILURE(rc))
|
---|
1385 | return rc;
|
---|
1386 |
|
---|
1387 | pThis->Lun0.IBase.pfnQueryInterface = usbHidKeyboardQueryInterface;
|
---|
1388 | pThis->Lun0.IPort.pfnPutEvent = usbHidKeyboardPutEvent;
|
---|
1389 |
|
---|
1390 | /*
|
---|
1391 | * Attach the keyboard driver.
|
---|
1392 | */
|
---|
1393 | rc = pUsbIns->pHlpR3->pfnDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Keyboard Port");
|
---|
1394 | if (RT_FAILURE(rc))
|
---|
1395 | return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach keyboard driver"));
|
---|
1396 |
|
---|
1397 | pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIKEYBOARDCONNECTOR);
|
---|
1398 | if (!pThis->Lun0.pDrv)
|
---|
1399 | return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query keyboard interface"));
|
---|
1400 |
|
---|
1401 | return VINF_SUCCESS;
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 |
|
---|
1405 | /**
|
---|
1406 | * The USB Human Interface Device (HID) Keyboard registration record.
|
---|
1407 | */
|
---|
1408 | const PDMUSBREG g_UsbHidKbd =
|
---|
1409 | {
|
---|
1410 | /* u32Version */
|
---|
1411 | PDM_USBREG_VERSION,
|
---|
1412 | /* szName */
|
---|
1413 | "HidKeyboard",
|
---|
1414 | /* pszDescription */
|
---|
1415 | "USB HID Keyboard.",
|
---|
1416 | /* fFlags */
|
---|
1417 | 0,
|
---|
1418 | /* cMaxInstances */
|
---|
1419 | ~0,
|
---|
1420 | /* cbInstance */
|
---|
1421 | sizeof(USBHID),
|
---|
1422 | /* pfnConstruct */
|
---|
1423 | usbHidConstruct,
|
---|
1424 | /* pfnDestruct */
|
---|
1425 | usbHidDestruct,
|
---|
1426 | /* pfnVMInitComplete */
|
---|
1427 | NULL,
|
---|
1428 | /* pfnVMPowerOn */
|
---|
1429 | NULL,
|
---|
1430 | /* pfnVMReset */
|
---|
1431 | NULL,
|
---|
1432 | /* pfnVMSuspend */
|
---|
1433 | NULL,
|
---|
1434 | /* pfnVMResume */
|
---|
1435 | NULL,
|
---|
1436 | /* pfnVMPowerOff */
|
---|
1437 | NULL,
|
---|
1438 | /* pfnHotPlugged */
|
---|
1439 | NULL,
|
---|
1440 | /* pfnHotUnplugged */
|
---|
1441 | NULL,
|
---|
1442 | /* pfnDriverAttach */
|
---|
1443 | NULL,
|
---|
1444 | /* pfnDriverDetach */
|
---|
1445 | NULL,
|
---|
1446 | /* pfnQueryInterface */
|
---|
1447 | NULL,
|
---|
1448 | /* pfnUsbReset */
|
---|
1449 | usbHidUsbReset,
|
---|
1450 | /* pfnUsbGetCachedDescriptors */
|
---|
1451 | usbHidUsbGetDescriptorCache,
|
---|
1452 | /* pfnUsbSetConfiguration */
|
---|
1453 | usbHidUsbSetConfiguration,
|
---|
1454 | /* pfnUsbSetInterface */
|
---|
1455 | usbHidUsbSetInterface,
|
---|
1456 | /* pfnUsbClearHaltedEndpoint */
|
---|
1457 | usbHidUsbClearHaltedEndpoint,
|
---|
1458 | /* pfnUrbNew */
|
---|
1459 | NULL/*usbHidUrbNew*/,
|
---|
1460 | /* pfnQueue */
|
---|
1461 | usbHidQueue,
|
---|
1462 | /* pfnUrbCancel */
|
---|
1463 | usbHidUrbCancel,
|
---|
1464 | /* pfnUrbReap */
|
---|
1465 | usbHidUrbReap,
|
---|
1466 | /* u32TheEnd */
|
---|
1467 | PDM_USBREG_VERSION
|
---|
1468 | };
|
---|