VirtualBox

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

Last change on this file since 87773 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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