1 | /* $Id: UsbKbd.cpp 34001 2010-11-11 16:31:11Z 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, 0x91, 0x90, 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 | int rc = false;
|
---|
705 | unsigned iBuf = 0;
|
---|
706 | RT_ZERO(*pReport);
|
---|
707 | for (unsigned iKey = 0; iKey < VBOX_USB_MAX_USAGE_KBD; ++iKey)
|
---|
708 | {
|
---|
709 | AssertReturn(iBuf <= RT_ELEMENTS(pReport->aKeys),
|
---|
710 | VERR_INTERNAL_ERROR);
|
---|
711 | if (pabUnreportedKeys[iKey] || pabDepressedKeys[iKey])
|
---|
712 | {
|
---|
713 | if (usbHidUsageCodeIsModifier(iKey))
|
---|
714 | pReport->ShiftState |= usbHidModifierToFlag(iKey);
|
---|
715 | else if (iBuf == RT_ELEMENTS(pReport->aKeys))
|
---|
716 | {
|
---|
717 | /* The USB HID spec says that the entire vector should be
|
---|
718 | * set to ErrorRollOver on overflow. We don't mind if this
|
---|
719 | * path is taken several times for one report. */
|
---|
720 | for (unsigned iBuf2 = 0;
|
---|
721 | iBuf2 < RT_ELEMENTS(pReport->aKeys); ++iBuf2)
|
---|
722 | pReport->aKeys[iBuf2] = USBHID_USAGE_ROLL_OVER;
|
---|
723 | }
|
---|
724 | else
|
---|
725 | {
|
---|
726 | pReport->aKeys[iBuf] = iKey;
|
---|
727 | ++iBuf;
|
---|
728 | /* More Korean keyboard hackery: Give the caller a hint that
|
---|
729 | * a key release event needs reporting.
|
---|
730 | */
|
---|
731 | if (iKey == 0x90 || iKey == 0x91)
|
---|
732 | rc = true;
|
---|
733 | }
|
---|
734 | pabUnreportedKeys[iKey] = 0;
|
---|
735 | }
|
---|
736 | }
|
---|
737 | return rc;
|
---|
738 | }
|
---|
739 |
|
---|
740 | #ifdef DEBUG
|
---|
741 | /** Test data for testing usbHidFillReport(). The format is:
|
---|
742 | * - Unreported keys (zero terminated array)
|
---|
743 | * - Depressed keys (zero terminated array)
|
---|
744 | * - Expected shift state in the report (single byte inside array)
|
---|
745 | * - Expected keys buffer contents (array of six bytes)
|
---|
746 | */
|
---|
747 | static const uint8_t testUsbHidFillReportData[][4][10] = {
|
---|
748 | /* Just unreported, no modifiers */
|
---|
749 | {{4, 9, 0}, {0}, {0}, {4, 9, 0, 0, 0, 0}},
|
---|
750 | /* Just unreported, one modifier */
|
---|
751 | {{4, 9, 0xe2, 0}, {0}, {4}, {4, 9, 0, 0, 0, 0}},
|
---|
752 | /* Just unreported, two modifiers */
|
---|
753 | {{4, 9, 0xe2, 0xe4, 0}, {0}, {20}, {4, 9, 0, 0, 0, 0}},
|
---|
754 | /* Just depressed, no modifiers */
|
---|
755 | {{0}, {7, 20, 0}, {0}, {7, 20, 0, 0, 0, 0}},
|
---|
756 | /* Just depressed, one modifier */
|
---|
757 | {{0}, {7, 20, 0xe3, 0}, {8}, {7, 20, 0, 0, 0, 0}},
|
---|
758 | /* Just depressed, two modifiers */
|
---|
759 | {{0}, {7, 20, 0xe3, 0xe6, 0}, {72}, {7, 20, 0, 0, 0, 0}},
|
---|
760 | /* Unreported and depressed, no overlap, no modifiers */
|
---|
761 | {{5, 10, 0}, {8, 21, 0}, {0}, {5, 8, 10, 21, 0, 0}},
|
---|
762 | /* Unreported and depressed, one overlap, no modifiers */
|
---|
763 | {{5, 10, 0}, {8, 10, 21, 0}, {0}, {5, 8, 10, 21, 0, 0}},
|
---|
764 | /* Unreported and depressed, no overlap, non-overlapping modifiers */
|
---|
765 | {{5, 10, 0xe2, 0xe4, 0}, {8, 21, 0xe3, 0xe6, 0}, {92},
|
---|
766 | {5, 8, 10, 21, 0, 0}},
|
---|
767 | /* Unreported and depressed, one overlap, non-overlapping modifiers */
|
---|
768 | {{5, 10, 21, 0xe2, 0xe4, 0}, {8, 21, 0xe3, 0xe6, 0}, {92},
|
---|
769 | {5, 8, 10, 21, 0, 0}},
|
---|
770 | /* Unreported and depressed, no overlap, overlapping modifiers */
|
---|
771 | {{5, 10, 0xe2, 0xe4, 0}, {8, 21, 0xe3, 0xe4, 0}, {28},
|
---|
772 | {5, 8, 10, 21, 0, 0}},
|
---|
773 | /* Unreported and depressed, one overlap, overlapping modifiers */
|
---|
774 | {{5, 10, 0xe2, 0xe4, 0}, {5, 8, 21, 0xe3, 0xe4, 0}, {28},
|
---|
775 | {5, 8, 10, 21, 0, 0}},
|
---|
776 | /* Just too many unreported, no modifiers */
|
---|
777 | {{4, 9, 11, 12, 16, 18, 20, 0}, {0}, {0}, {1, 1, 1, 1, 1, 1}},
|
---|
778 | /* Just too many unreported, two modifiers */
|
---|
779 | {{4, 9, 11, 12, 16, 18, 20, 0xe2, 0xe4, 0}, {0}, {20},
|
---|
780 | {1, 1, 1, 1, 1, 1}},
|
---|
781 | /* Just too many depressed, no modifiers */
|
---|
782 | {{0}, {7, 20, 22, 25, 27, 29, 34, 0}, {0}, {1, 1, 1, 1, 1, 1}},
|
---|
783 | /* Just too many depressed, two modifiers */
|
---|
784 | {{0}, {7, 20, 22, 25, 27, 29, 34, 0xe3, 0xe5, 0}, {40},
|
---|
785 | {1, 1, 1, 1, 1, 1}},
|
---|
786 | /* Too many unreported and depressed, no overlap, no modifiers */
|
---|
787 | {{5, 10, 12, 13, 0}, {8, 9, 21, 0}, {0}, {1, 1, 1, 1, 1, 1}},
|
---|
788 | /* Eight unreported and depressed total, one overlap, no modifiers */
|
---|
789 | {{5, 10, 12, 13, 0}, {8, 10, 21, 22, 0}, {0}, {1, 1, 1, 1, 1, 1}},
|
---|
790 | /* Seven unreported and depressed total, one overlap, no modifiers */
|
---|
791 | {{5, 10, 12, 13, 0}, {8, 10, 21, 0}, {0}, {5, 8, 10, 12, 13, 21}},
|
---|
792 | /* Too many unreported and depressed, no overlap, two modifiers */
|
---|
793 | {{5, 10, 12, 13, 0xe2, 0}, {8, 9, 21, 0xe4, 0}, {20},
|
---|
794 | {1, 1, 1, 1, 1, 1}},
|
---|
795 | /* Eight unreported and depressed total, one overlap, two modifiers */
|
---|
796 | {{5, 10, 12, 13, 0xe1, 0}, {8, 10, 21, 22, 0xe2, 0}, {6},
|
---|
797 | {1, 1, 1, 1, 1, 1}},
|
---|
798 | /* Seven unreported and depressed total, one overlap, two modifiers */
|
---|
799 | {{5, 10, 12, 13, 0xe2, 0}, {8, 10, 21, 0xe3, 0}, {12},
|
---|
800 | {5, 8, 10, 12, 13, 21}}
|
---|
801 | };
|
---|
802 |
|
---|
803 | /** Test case for usbHidFillReport() */
|
---|
804 | class testUsbHidFillReport
|
---|
805 | {
|
---|
806 | USBHIDK_REPORT mReport;
|
---|
807 | uint8_t mabUnreportedKeys[VBOX_USB_MAX_USAGE_KBD];
|
---|
808 | uint8_t mabDepressedKeys[VBOX_USB_MAX_USAGE_KBD];
|
---|
809 | const uint8_t (*mTests)[4][10];
|
---|
810 |
|
---|
811 | void doTest(unsigned cTest, const uint8_t *paiUnreportedKeys,
|
---|
812 | const uint8_t *paiDepressedKeys, uint8_t aExpShiftState,
|
---|
813 | const uint8_t *pabExpKeys)
|
---|
814 | {
|
---|
815 | RT_ZERO(mReport);
|
---|
816 | RT_ZERO(mabUnreportedKeys);
|
---|
817 | RT_ZERO(mabDepressedKeys);
|
---|
818 | for (unsigned i = 0; paiUnreportedKeys[i] != 0; ++i)
|
---|
819 | mabUnreportedKeys[paiUnreportedKeys[i]] = 1;
|
---|
820 | for (unsigned i = 0; paiDepressedKeys[i] != 0; ++i)
|
---|
821 | mabUnreportedKeys[paiDepressedKeys[i]] = 1;
|
---|
822 | int rc = usbHidFillReport(&mReport, mabUnreportedKeys, mabDepressedKeys);
|
---|
823 | AssertMsgRC(rc, ("test %u\n", cTest));
|
---|
824 | AssertMsg(mReport.ShiftState == aExpShiftState, ("test %u\n", cTest));
|
---|
825 | for (unsigned i = 0; i < RT_ELEMENTS(mReport.aKeys); ++i)
|
---|
826 | AssertMsg(mReport.aKeys[i] == pabExpKeys[i], ("test %u\n", cTest));
|
---|
827 | }
|
---|
828 |
|
---|
829 | public:
|
---|
830 | testUsbHidFillReport(void) : mTests(&testUsbHidFillReportData[0])
|
---|
831 | {
|
---|
832 | for (unsigned i = 0; i < RT_ELEMENTS(testUsbHidFillReportData); ++i)
|
---|
833 | doTest(i, mTests[i][0], mTests[i][1], mTests[i][2][0],
|
---|
834 | mTests[i][3]);
|
---|
835 | }
|
---|
836 | };
|
---|
837 |
|
---|
838 | static testUsbHidFillReport gsTestUsbHidFillReport;
|
---|
839 | #endif
|
---|
840 |
|
---|
841 | /**
|
---|
842 | * Sends a state report to the host if there is a pending URB.
|
---|
843 | */
|
---|
844 | static int usbHidSendReport(PUSBHID pThis)
|
---|
845 | {
|
---|
846 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
|
---|
847 | if (pUrb)
|
---|
848 | {
|
---|
849 | PUSBHIDK_REPORT pReport = (PUSBHIDK_REPORT)&pUrb->abData[0];
|
---|
850 |
|
---|
851 | int again = usbHidFillReport(pReport, pThis->abUnreportedKeys,
|
---|
852 | pThis->abDepressedKeys);
|
---|
853 | if (again)
|
---|
854 | pThis->fHasPendingChanges = true;
|
---|
855 | else
|
---|
856 | pThis->fHasPendingChanges = false;
|
---|
857 | return usbHidCompleteOk(pThis, pUrb, sizeof(*pReport));
|
---|
858 | }
|
---|
859 | else
|
---|
860 | {
|
---|
861 | Log2(("No available URB for USB kbd\n"));
|
---|
862 | pThis->fHasPendingChanges = true;
|
---|
863 | }
|
---|
864 | return VINF_EOF;
|
---|
865 | }
|
---|
866 |
|
---|
867 | /**
|
---|
868 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
869 | */
|
---|
870 | static DECLCALLBACK(void *) usbHidKeyboardQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
871 | {
|
---|
872 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
|
---|
873 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
|
---|
874 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDPORT, &pThis->Lun0.IPort);
|
---|
875 | return NULL;
|
---|
876 | }
|
---|
877 |
|
---|
878 | /**
|
---|
879 | * Keyboard event handler.
|
---|
880 | *
|
---|
881 | * @returns VBox status code.
|
---|
882 | * @param pInterface Pointer to the keyboard port interface (KBDState::Keyboard.IPort).
|
---|
883 | * @param u8KeyCode The keycode.
|
---|
884 | */
|
---|
885 | static DECLCALLBACK(int) usbHidKeyboardPutEvent(PPDMIKEYBOARDPORT pInterface, uint8_t u8KeyCode)
|
---|
886 | {
|
---|
887 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
|
---|
888 | uint32_t u32Usage = 0;
|
---|
889 | uint8_t u8HidCode;
|
---|
890 | int fKeyDown;
|
---|
891 | bool fHaveEvent = true;
|
---|
892 |
|
---|
893 | RTCritSectEnter(&pThis->CritSect);
|
---|
894 |
|
---|
895 | pThis->XlatState = ScancodeToHidUsage(pThis->XlatState, u8KeyCode, &u32Usage);
|
---|
896 |
|
---|
897 | if (pThis->XlatState == SS_IDLE)
|
---|
898 | {
|
---|
899 | /* The usage code is valid. */
|
---|
900 | fKeyDown = !(u32Usage & 0x80000000);
|
---|
901 | u8HidCode = u32Usage & 0xFF;
|
---|
902 | AssertReturn(u8HidCode <= VBOX_USB_MAX_USAGE_KBD, VERR_INTERNAL_ERROR);
|
---|
903 |
|
---|
904 | LogRelFlowFunc(("key %s: 0x%x->0x%x\n",
|
---|
905 | fKeyDown ? "down" : "up", u8KeyCode, u8HidCode));
|
---|
906 |
|
---|
907 | if (fKeyDown)
|
---|
908 | {
|
---|
909 | /* Due to host key repeat, we can get key events for keys which are
|
---|
910 | * already depressed. */
|
---|
911 | if (!pThis->abDepressedKeys[u8HidCode])
|
---|
912 | pThis->abUnreportedKeys[u8HidCode] = 1;
|
---|
913 | else
|
---|
914 | fHaveEvent = false;
|
---|
915 | pThis->abDepressedKeys[u8HidCode] = 1;
|
---|
916 | }
|
---|
917 | else
|
---|
918 | {
|
---|
919 | /* For stupid Korean keyboards, we have to fake a key up/down sequence
|
---|
920 | * because they only send break codes for Hangul/Hanja keys.
|
---|
921 | */
|
---|
922 | if (u8HidCode == 0x90 || u8HidCode == 0x91)
|
---|
923 | pThis->abUnreportedKeys[u8HidCode] = 1;
|
---|
924 | pThis->abDepressedKeys[u8HidCode] = 0;
|
---|
925 | }
|
---|
926 |
|
---|
927 |
|
---|
928 | /* Send a report if the host is already waiting for it. */
|
---|
929 | if (fHaveEvent)
|
---|
930 | usbHidSendReport(pThis);
|
---|
931 | }
|
---|
932 |
|
---|
933 | RTCritSectLeave(&pThis->CritSect);
|
---|
934 |
|
---|
935 | return VINF_SUCCESS;
|
---|
936 | }
|
---|
937 |
|
---|
938 | /**
|
---|
939 | * @copydoc PDMUSBREG::pfnUrbReap
|
---|
940 | */
|
---|
941 | static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
|
---|
942 | {
|
---|
943 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
944 | //LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
|
---|
945 |
|
---|
946 | RTCritSectEnter(&pThis->CritSect);
|
---|
947 |
|
---|
948 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
|
---|
949 | if (!pUrb && cMillies)
|
---|
950 | {
|
---|
951 | /* Wait */
|
---|
952 | pThis->fHaveDoneQueueWaiter = true;
|
---|
953 | RTCritSectLeave(&pThis->CritSect);
|
---|
954 |
|
---|
955 | RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
|
---|
956 |
|
---|
957 | RTCritSectEnter(&pThis->CritSect);
|
---|
958 | pThis->fHaveDoneQueueWaiter = false;
|
---|
959 |
|
---|
960 | pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
|
---|
961 | }
|
---|
962 |
|
---|
963 | RTCritSectLeave(&pThis->CritSect);
|
---|
964 |
|
---|
965 | if (pUrb)
|
---|
966 | Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
967 | return pUrb;
|
---|
968 | }
|
---|
969 |
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * @copydoc PDMUSBREG::pfnUrbCancel
|
---|
973 | */
|
---|
974 | static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
975 | {
|
---|
976 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
977 | LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
978 | RTCritSectEnter(&pThis->CritSect);
|
---|
979 |
|
---|
980 | /*
|
---|
981 | * Remove the URB from the to-host queue and move it onto the done queue.
|
---|
982 | */
|
---|
983 | if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
|
---|
984 | usbHidLinkDone(pThis, pUrb);
|
---|
985 |
|
---|
986 | RTCritSectLeave(&pThis->CritSect);
|
---|
987 | return VINF_SUCCESS;
|
---|
988 | }
|
---|
989 |
|
---|
990 |
|
---|
991 | /**
|
---|
992 | * Handles request sent to the inbound (device to host) interrupt pipe. This is
|
---|
993 | * rather different from bulk requests because an interrupt read URB may complete
|
---|
994 | * after arbitrarily long time.
|
---|
995 | */
|
---|
996 | static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
|
---|
997 | {
|
---|
998 | /*
|
---|
999 | * Stall the request if the pipe is halted.
|
---|
1000 | */
|
---|
1001 | if (RT_UNLIKELY(pEp->fHalted))
|
---|
1002 | return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
|
---|
1003 |
|
---|
1004 | /*
|
---|
1005 | * Deal with the URB according to the state.
|
---|
1006 | */
|
---|
1007 | switch (pThis->enmState)
|
---|
1008 | {
|
---|
1009 | /*
|
---|
1010 | * We've data left to transfer to the host.
|
---|
1011 | */
|
---|
1012 | case USBHIDREQSTATE_DATA_TO_HOST:
|
---|
1013 | {
|
---|
1014 | AssertFailed();
|
---|
1015 | Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
|
---|
1016 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /*
|
---|
1020 | * Status transfer.
|
---|
1021 | */
|
---|
1022 | case USBHIDREQSTATE_STATUS:
|
---|
1023 | {
|
---|
1024 | AssertFailed();
|
---|
1025 | Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
|
---|
1026 | pThis->enmState = USBHIDREQSTATE_READY;
|
---|
1027 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | case USBHIDREQSTATE_READY:
|
---|
1031 | usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
|
---|
1032 | /* If device was not set idle, sent the current report right away. */
|
---|
1033 | if (pThis->bIdle != 0 || pThis->fHasPendingChanges)
|
---|
1034 | usbHidSendReport(pThis);
|
---|
1035 | LogFlow(("usbHidHandleIntrDevToHost: Sent report via %p:%s\n", pUrb, pUrb->pszDesc));
|
---|
1036 | return VINF_SUCCESS;
|
---|
1037 |
|
---|
1038 | /*
|
---|
1039 | * Bad states, stall.
|
---|
1040 | */
|
---|
1041 | default:
|
---|
1042 | Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
|
---|
1043 | return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 |
|
---|
1048 | /**
|
---|
1049 | * Handles request sent to the default control pipe.
|
---|
1050 | */
|
---|
1051 | static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
|
---|
1052 | {
|
---|
1053 | PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
|
---|
1054 | LogFlow(("usbHidHandleDefaultPipe: cbData=%d\n", pUrb->cbData));
|
---|
1055 |
|
---|
1056 | AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
|
---|
1057 |
|
---|
1058 | if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
|
---|
1059 | {
|
---|
1060 | switch (pSetup->bRequest)
|
---|
1061 | {
|
---|
1062 | case VUSB_REQ_GET_DESCRIPTOR:
|
---|
1063 | {
|
---|
1064 | switch (pSetup->bmRequestType)
|
---|
1065 | {
|
---|
1066 | case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1067 | {
|
---|
1068 | switch (pSetup->wValue >> 8)
|
---|
1069 | {
|
---|
1070 | case VUSB_DT_STRING:
|
---|
1071 | Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1072 | break;
|
---|
1073 | default:
|
---|
1074 | Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1075 | break;
|
---|
1076 | }
|
---|
1077 | break;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1081 | {
|
---|
1082 | switch (pSetup->wValue >> 8)
|
---|
1083 | {
|
---|
1084 | case DT_IF_HID_REPORT:
|
---|
1085 | uint32_t cbCopy;
|
---|
1086 |
|
---|
1087 | /* Returned data is written after the setup message. */
|
---|
1088 | cbCopy = pUrb->cbData - sizeof(*pSetup);
|
---|
1089 | cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidReportDesc));
|
---|
1090 | Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
|
---|
1091 | memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidReportDesc, cbCopy);
|
---|
1092 | return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
|
---|
1093 | default:
|
---|
1094 | Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1095 | break;
|
---|
1096 | }
|
---|
1097 | break;
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | default:
|
---|
1101 | Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
|
---|
1102 | return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
|
---|
1103 | }
|
---|
1104 | break;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | case VUSB_REQ_GET_STATUS:
|
---|
1108 | {
|
---|
1109 | uint16_t wRet = 0;
|
---|
1110 |
|
---|
1111 | if (pSetup->wLength != 2)
|
---|
1112 | {
|
---|
1113 | Log(("usbHid: Bad GET_STATUS req: wLength=%#x\n", pSetup->wLength));
|
---|
1114 | break;
|
---|
1115 | }
|
---|
1116 | Assert(pSetup->wValue == 0);
|
---|
1117 | switch (pSetup->bmRequestType)
|
---|
1118 | {
|
---|
1119 | case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1120 | {
|
---|
1121 | Assert(pSetup->wIndex == 0);
|
---|
1122 | Log(("usbHid: GET_STATUS (device)\n"));
|
---|
1123 | wRet = 0; /* Not self-powered, no remote wakeup. */
|
---|
1124 | memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
|
---|
1125 | return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1129 | {
|
---|
1130 | if (pSetup->wIndex == 0)
|
---|
1131 | {
|
---|
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 (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
|
---|
1138 | }
|
---|
1139 | break;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
1143 | {
|
---|
1144 | if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
|
---|
1145 | {
|
---|
1146 | wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
|
---|
1147 | memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
|
---|
1148 | return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
|
---|
1149 | }
|
---|
1150 | else
|
---|
1151 | {
|
---|
1152 | Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
|
---|
1153 | }
|
---|
1154 | break;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | default:
|
---|
1158 | Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
|
---|
1159 | return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
|
---|
1160 | }
|
---|
1161 | break;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | case VUSB_REQ_CLEAR_FEATURE:
|
---|
1165 | break;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | /** @todo implement this. */
|
---|
1169 | Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1170 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1171 |
|
---|
1172 | usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
|
---|
1173 | }
|
---|
1174 | else if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_CLASS)
|
---|
1175 | {
|
---|
1176 | switch (pSetup->bRequest)
|
---|
1177 | {
|
---|
1178 | case HID_REQ_SET_IDLE:
|
---|
1179 | {
|
---|
1180 | switch (pSetup->bmRequestType)
|
---|
1181 | {
|
---|
1182 | case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
|
---|
1183 | {
|
---|
1184 | Log(("usbHid: SET_IDLE wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1185 | pThis->bIdle = pSetup->wValue >> 8;
|
---|
1186 | /* Consider 24ms to mean zero for keyboards (see IOUSBHIDDriver) */
|
---|
1187 | if (pThis->bIdle == 6) pThis->bIdle = 0;
|
---|
1188 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
1189 | }
|
---|
1190 | break;
|
---|
1191 | }
|
---|
1192 | break;
|
---|
1193 | }
|
---|
1194 | case HID_REQ_GET_IDLE:
|
---|
1195 | {
|
---|
1196 | switch (pSetup->bmRequestType)
|
---|
1197 | {
|
---|
1198 | case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_HOST:
|
---|
1199 | {
|
---|
1200 | Log(("usbHid: GET_IDLE wValue=%#x wIndex=%#x, returning %#x\n", pSetup->wValue, pSetup->wIndex, pThis->bIdle));
|
---|
1201 | pUrb->abData[sizeof(*pSetup)] = pThis->bIdle;
|
---|
1202 | return usbHidCompleteOk(pThis, pUrb, 1);
|
---|
1203 | }
|
---|
1204 | break;
|
---|
1205 | }
|
---|
1206 | break;
|
---|
1207 | }
|
---|
1208 | }
|
---|
1209 | Log(("usbHid: Unimplemented class request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1210 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1211 |
|
---|
1212 | usbHidCompleteStall(pThis, pEp, pUrb, "TODO: class request stuff");
|
---|
1213 | }
|
---|
1214 | else
|
---|
1215 | {
|
---|
1216 | Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1217 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1218 | return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | return VINF_SUCCESS;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 |
|
---|
1225 | /**
|
---|
1226 | * @copydoc PDMUSBREG::pfnQueue
|
---|
1227 | */
|
---|
1228 | static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
1229 | {
|
---|
1230 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1231 | LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
|
---|
1232 | RTCritSectEnter(&pThis->CritSect);
|
---|
1233 |
|
---|
1234 | /*
|
---|
1235 | * Parse on a per end-point basis.
|
---|
1236 | */
|
---|
1237 | int rc;
|
---|
1238 | switch (pUrb->EndPt)
|
---|
1239 | {
|
---|
1240 | case 0:
|
---|
1241 | rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
|
---|
1242 | break;
|
---|
1243 |
|
---|
1244 | case 0x81:
|
---|
1245 | AssertFailed();
|
---|
1246 | case 0x01:
|
---|
1247 | rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
|
---|
1248 | break;
|
---|
1249 |
|
---|
1250 | default:
|
---|
1251 | AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
|
---|
1252 | rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
|
---|
1253 | break;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | RTCritSectLeave(&pThis->CritSect);
|
---|
1257 | return rc;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 |
|
---|
1261 | /**
|
---|
1262 | * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
|
---|
1263 | */
|
---|
1264 | static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
|
---|
1265 | {
|
---|
1266 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1267 | LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
|
---|
1268 |
|
---|
1269 | if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
|
---|
1270 | {
|
---|
1271 | RTCritSectEnter(&pThis->CritSect);
|
---|
1272 | pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
|
---|
1273 | RTCritSectLeave(&pThis->CritSect);
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | return VINF_SUCCESS;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 |
|
---|
1280 | /**
|
---|
1281 | * @copydoc PDMUSBREG::pfnUsbSetInterface
|
---|
1282 | */
|
---|
1283 | static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
|
---|
1284 | {
|
---|
1285 | LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
|
---|
1286 | Assert(bAlternateSetting == 0);
|
---|
1287 | return VINF_SUCCESS;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 |
|
---|
1291 | /**
|
---|
1292 | * @copydoc PDMUSBREG::pfnUsbSetConfiguration
|
---|
1293 | */
|
---|
1294 | static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
|
---|
1295 | const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
|
---|
1296 | {
|
---|
1297 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1298 | LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
|
---|
1299 | Assert(bConfigurationValue == 1);
|
---|
1300 | RTCritSectEnter(&pThis->CritSect);
|
---|
1301 |
|
---|
1302 | /*
|
---|
1303 | * If the same config is applied more than once, it's a kind of reset.
|
---|
1304 | */
|
---|
1305 | if (pThis->bConfigurationValue == bConfigurationValue)
|
---|
1306 | usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
|
---|
1307 | pThis->bConfigurationValue = bConfigurationValue;
|
---|
1308 |
|
---|
1309 | /*
|
---|
1310 | * Tell the other end that the keyboard is now enabled and wants
|
---|
1311 | * to receive keystrokes.
|
---|
1312 | */
|
---|
1313 | pThis->Lun0.pDrv->pfnSetActive(pThis->Lun0.pDrv, true);
|
---|
1314 |
|
---|
1315 | RTCritSectLeave(&pThis->CritSect);
|
---|
1316 | return VINF_SUCCESS;
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 |
|
---|
1320 | /**
|
---|
1321 | * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
|
---|
1322 | */
|
---|
1323 | static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
|
---|
1324 | {
|
---|
1325 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1326 | LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
|
---|
1327 | return &g_UsbHidDescCache;
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 |
|
---|
1331 | /**
|
---|
1332 | * @copydoc PDMUSBREG::pfnUsbReset
|
---|
1333 | */
|
---|
1334 | static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
|
---|
1335 | {
|
---|
1336 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1337 | LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
|
---|
1338 | RTCritSectEnter(&pThis->CritSect);
|
---|
1339 |
|
---|
1340 | int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
|
---|
1341 |
|
---|
1342 | RTCritSectLeave(&pThis->CritSect);
|
---|
1343 | return rc;
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 |
|
---|
1347 | /**
|
---|
1348 | * @copydoc PDMUSBREG::pfnDestruct
|
---|
1349 | */
|
---|
1350 | static void usbHidDestruct(PPDMUSBINS pUsbIns)
|
---|
1351 | {
|
---|
1352 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1353 | LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
|
---|
1354 |
|
---|
1355 | if (RTCritSectIsInitialized(&pThis->CritSect))
|
---|
1356 | {
|
---|
1357 | /* Let whoever runs in this critical section complete. */
|
---|
1358 | RTCritSectEnter(&pThis->CritSect);
|
---|
1359 | RTCritSectLeave(&pThis->CritSect);
|
---|
1360 | RTCritSectDelete(&pThis->CritSect);
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
|
---|
1364 | {
|
---|
1365 | RTSemEventDestroy(pThis->hEvtDoneQueue);
|
---|
1366 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
1367 | }
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 |
|
---|
1371 | /**
|
---|
1372 | * @copydoc PDMUSBREG::pfnConstruct
|
---|
1373 | */
|
---|
1374 | static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
|
---|
1375 | {
|
---|
1376 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1377 | Log(("usbHidConstruct/#%u:\n", iInstance));
|
---|
1378 |
|
---|
1379 | /*
|
---|
1380 | * Perform the basic structure initialization first so the destructor
|
---|
1381 | * will not misbehave.
|
---|
1382 | */
|
---|
1383 | pThis->pUsbIns = pUsbIns;
|
---|
1384 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
1385 | pThis->XlatState = SS_IDLE;
|
---|
1386 | usbHidQueueInit(&pThis->ToHostQueue);
|
---|
1387 | usbHidQueueInit(&pThis->DoneQueue);
|
---|
1388 |
|
---|
1389 | int rc = RTCritSectInit(&pThis->CritSect);
|
---|
1390 | AssertRCReturn(rc, rc);
|
---|
1391 |
|
---|
1392 | rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
|
---|
1393 | AssertRCReturn(rc, rc);
|
---|
1394 |
|
---|
1395 | /*
|
---|
1396 | * Validate and read the configuration.
|
---|
1397 | */
|
---|
1398 | rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbHid", iInstance);
|
---|
1399 | if (RT_FAILURE(rc))
|
---|
1400 | return rc;
|
---|
1401 |
|
---|
1402 | pThis->Lun0.IBase.pfnQueryInterface = usbHidKeyboardQueryInterface;
|
---|
1403 | pThis->Lun0.IPort.pfnPutEvent = usbHidKeyboardPutEvent;
|
---|
1404 |
|
---|
1405 | /*
|
---|
1406 | * Attach the keyboard driver.
|
---|
1407 | */
|
---|
1408 | rc = pUsbIns->pHlpR3->pfnDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Keyboard Port");
|
---|
1409 | if (RT_FAILURE(rc))
|
---|
1410 | return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach keyboard driver"));
|
---|
1411 |
|
---|
1412 | pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIKEYBOARDCONNECTOR);
|
---|
1413 | if (!pThis->Lun0.pDrv)
|
---|
1414 | return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query keyboard interface"));
|
---|
1415 |
|
---|
1416 | return VINF_SUCCESS;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 |
|
---|
1420 | /**
|
---|
1421 | * The USB Human Interface Device (HID) Keyboard registration record.
|
---|
1422 | */
|
---|
1423 | const PDMUSBREG g_UsbHidKbd =
|
---|
1424 | {
|
---|
1425 | /* u32Version */
|
---|
1426 | PDM_USBREG_VERSION,
|
---|
1427 | /* szName */
|
---|
1428 | "HidKeyboard",
|
---|
1429 | /* pszDescription */
|
---|
1430 | "USB HID Keyboard.",
|
---|
1431 | /* fFlags */
|
---|
1432 | 0,
|
---|
1433 | /* cMaxInstances */
|
---|
1434 | ~0,
|
---|
1435 | /* cbInstance */
|
---|
1436 | sizeof(USBHID),
|
---|
1437 | /* pfnConstruct */
|
---|
1438 | usbHidConstruct,
|
---|
1439 | /* pfnDestruct */
|
---|
1440 | usbHidDestruct,
|
---|
1441 | /* pfnVMInitComplete */
|
---|
1442 | NULL,
|
---|
1443 | /* pfnVMPowerOn */
|
---|
1444 | NULL,
|
---|
1445 | /* pfnVMReset */
|
---|
1446 | NULL,
|
---|
1447 | /* pfnVMSuspend */
|
---|
1448 | NULL,
|
---|
1449 | /* pfnVMResume */
|
---|
1450 | NULL,
|
---|
1451 | /* pfnVMPowerOff */
|
---|
1452 | NULL,
|
---|
1453 | /* pfnHotPlugged */
|
---|
1454 | NULL,
|
---|
1455 | /* pfnHotUnplugged */
|
---|
1456 | NULL,
|
---|
1457 | /* pfnDriverAttach */
|
---|
1458 | NULL,
|
---|
1459 | /* pfnDriverDetach */
|
---|
1460 | NULL,
|
---|
1461 | /* pfnQueryInterface */
|
---|
1462 | NULL,
|
---|
1463 | /* pfnUsbReset */
|
---|
1464 | usbHidUsbReset,
|
---|
1465 | /* pfnUsbGetCachedDescriptors */
|
---|
1466 | usbHidUsbGetDescriptorCache,
|
---|
1467 | /* pfnUsbSetConfiguration */
|
---|
1468 | usbHidUsbSetConfiguration,
|
---|
1469 | /* pfnUsbSetInterface */
|
---|
1470 | usbHidUsbSetInterface,
|
---|
1471 | /* pfnUsbClearHaltedEndpoint */
|
---|
1472 | usbHidUsbClearHaltedEndpoint,
|
---|
1473 | /* pfnUrbNew */
|
---|
1474 | NULL/*usbHidUrbNew*/,
|
---|
1475 | /* pfnQueue */
|
---|
1476 | usbHidQueue,
|
---|
1477 | /* pfnUrbCancel */
|
---|
1478 | usbHidUrbCancel,
|
---|
1479 | /* pfnUrbReap */
|
---|
1480 | usbHidUrbReap,
|
---|
1481 | /* u32TheEnd */
|
---|
1482 | PDM_USBREG_VERSION
|
---|
1483 | };
|
---|