VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/UsbKbd.cpp@ 29749

Last change on this file since 29749 was 29749, checked in by vboxsync, 15 years ago

Devices and FE/Common: keyboard table cleanup and a hack to support Sun keys on the emulated USB keyboard (Solaris host only)

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette