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