VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.3 KB
Line 
1/* $Id: UsbKbd.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * UsbKbd - USB Human Interface Device Emulation, Keyboard.
4 */
5
6/*
7 * Copyright (C) 2007-2019 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 * Keyboard event handler.
703 *
704 * @returns VBox status code.
705 * @param pInterface Pointer to the keyboard port interface (KBDState::Keyboard.IPort).
706 * @param u32UsageCode The key usage ID.
707 */
708static DECLCALLBACK(int) usbHidKeyboardPutEvent(PPDMIKEYBOARDPORT pInterface, uint32_t u32UsageCode)
709{
710 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
711 uint8_t u8HidCode;
712 bool fKeyDown;
713 bool fHaveEvent = true;
714 int rc = VINF_SUCCESS;
715
716 RTCritSectEnter(&pThis->CritSect);
717
718 /* Let's see what we got... */
719 fKeyDown = !(u32UsageCode & 0x80000000);
720 u8HidCode = u32UsageCode & 0xFF;
721 AssertReturn(u8HidCode <= VBOX_USB_MAX_USAGE_CODE, VERR_INTERNAL_ERROR);
722
723 LogFlowFunc(("key %s: 0x%x\n", fKeyDown ? "down" : "up", u8HidCode));
724
725 /*
726 * Due to host key repeat, we can get key events for keys which are
727 * already depressed. Drop those right here.
728 */
729 if (fKeyDown && pThis->abDepressedKeys[u8HidCode])
730 fHaveEvent = false;
731
732 /* If there is already a pending event, we won't accept a new one yet. */
733 if (pThis->fHasPendingChanges && fHaveEvent)
734 {
735 rc = VERR_TRY_AGAIN;
736 }
737 else if (fHaveEvent)
738 {
739 if (RT_UNLIKELY(u32UsageCode == KRSP_BAT_FAIL))
740 {
741 /* Clear all currently depressed and unreported keys. */
742 RT_ZERO(pThis->abDepressedKeys);
743 }
744 else
745 {
746 /* Regular key event - update keyboard state. */
747 if (fKeyDown)
748 pThis->abDepressedKeys[u8HidCode] = 1;
749 else
750 pThis->abDepressedKeys[u8HidCode] = 0;
751 }
752
753 /*
754 * Try sending a report. Note that we already decided to consume the
755 * event regardless of whether a URB is available or not. If it's not,
756 * we will simply not accept any further events.
757 */
758 usbHidSendReport(pThis);
759 }
760
761 RTCritSectLeave(&pThis->CritSect);
762
763 return rc;
764}
765
766/**
767 * @interface_method_impl{PDMUSBREG,pfnUrbReap}
768 */
769static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
770{
771 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
772 //LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
773
774 RTCritSectEnter(&pThis->CritSect);
775
776 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
777 if (!pUrb && cMillies)
778 {
779 /* Wait */
780 pThis->fHaveDoneQueueWaiter = true;
781 RTCritSectLeave(&pThis->CritSect);
782
783 RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
784
785 RTCritSectEnter(&pThis->CritSect);
786 pThis->fHaveDoneQueueWaiter = false;
787
788 pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
789 }
790
791 RTCritSectLeave(&pThis->CritSect);
792
793 if (pUrb)
794 Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
795 return pUrb;
796}
797
798
799/**
800 * @interface_method_impl{PDMUSBREG,pfnWakeup}
801 */
802static DECLCALLBACK(int) usbHidWakeup(PPDMUSBINS pUsbIns)
803{
804 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
805
806 return RTSemEventSignal(pThis->hEvtDoneQueue);
807}
808
809
810/**
811 * @interface_method_impl{PDMUSBREG,pfnUrbCancel}
812 */
813static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
814{
815 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
816 LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
817 RTCritSectEnter(&pThis->CritSect);
818
819 /*
820 * Remove the URB from the to-host queue and move it onto the done queue.
821 */
822 if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
823 usbHidLinkDone(pThis, pUrb);
824
825 RTCritSectLeave(&pThis->CritSect);
826 return VINF_SUCCESS;
827}
828
829
830/**
831 * Handles request sent to the inbound (device to host) interrupt pipe. This is
832 * rather different from bulk requests because an interrupt read URB may complete
833 * after arbitrarily long time.
834 */
835static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
836{
837 /*
838 * Stall the request if the pipe is halted.
839 */
840 if (RT_UNLIKELY(pEp->fHalted))
841 return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
842
843 /*
844 * Deal with the URB according to the state.
845 */
846 switch (pThis->enmState)
847 {
848 /*
849 * We've data left to transfer to the host.
850 */
851 case USBHIDREQSTATE_DATA_TO_HOST:
852 {
853 AssertFailed();
854 Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
855 return usbHidCompleteOk(pThis, pUrb, 0);
856 }
857
858 /*
859 * Status transfer.
860 */
861 case USBHIDREQSTATE_STATUS:
862 {
863 AssertFailed();
864 Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
865 pThis->enmState = USBHIDREQSTATE_READY;
866 return usbHidCompleteOk(pThis, pUrb, 0);
867 }
868
869 case USBHIDREQSTATE_READY:
870 usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
871 /* If device was not set idle, send the current report right away. */
872 if (pThis->bIdle != 0 || pThis->fHasPendingChanges)
873 {
874 usbHidSendReport(pThis);
875 LogFlow(("usbHidHandleIntrDevToHost: Sent report via %p:%s\n", pUrb, pUrb->pszDesc));
876 Assert(!pThis->fHasPendingChanges); /* Since we just got a URB... */
877 /* There may be more input queued up. Ask for it now. */
878 pThis->Lun0.pDrv->pfnFlushQueue(pThis->Lun0.pDrv);
879 }
880 return VINF_SUCCESS;
881
882 /*
883 * Bad states, stall.
884 */
885 default:
886 Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
887 return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
888 }
889}
890
891
892/**
893 * Handles request sent to the default control pipe.
894 */
895static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
896{
897 PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
898 LogFlow(("usbHidHandleDefaultPipe: cbData=%d\n", pUrb->cbData));
899
900 AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
901
902 if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
903 {
904 switch (pSetup->bRequest)
905 {
906 case VUSB_REQ_GET_DESCRIPTOR:
907 {
908 switch (pSetup->bmRequestType)
909 {
910 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
911 {
912 switch (pSetup->wValue >> 8)
913 {
914 case VUSB_DT_STRING:
915 Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
916 break;
917 default:
918 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
919 break;
920 }
921 break;
922 }
923
924 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
925 {
926 switch (pSetup->wValue >> 8)
927 {
928 case DT_IF_HID_DESCRIPTOR:
929 {
930 uint32_t cbCopy;
931
932 /* Returned data is written after the setup message. */
933 cbCopy = pUrb->cbData - sizeof(*pSetup);
934 cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidIfHidDesc));
935 Log(("usbHidKbd: GET_DESCRIPTOR DT_IF_HID_DESCRIPTOR wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
936 memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidIfHidDesc, cbCopy);
937 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
938 }
939
940 case DT_IF_HID_REPORT:
941 {
942 uint32_t cbCopy;
943
944 /* Returned data is written after the setup message. */
945 cbCopy = pUrb->cbData - sizeof(*pSetup);
946 cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidReportDesc));
947 Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
948 memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidReportDesc, cbCopy);
949 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
950 }
951
952 default:
953 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
954 break;
955 }
956 break;
957 }
958
959 default:
960 Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
961 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
962 }
963 break;
964 }
965
966 case VUSB_REQ_GET_STATUS:
967 {
968 uint16_t wRet = 0;
969
970 if (pSetup->wLength != 2)
971 {
972 Log(("usbHid: Bad GET_STATUS req: wLength=%#x\n", pSetup->wLength));
973 break;
974 }
975 Assert(pSetup->wValue == 0);
976 switch (pSetup->bmRequestType)
977 {
978 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
979 {
980 Assert(pSetup->wIndex == 0);
981 Log(("usbHid: GET_STATUS (device)\n"));
982 wRet = 0; /* Not self-powered, no remote wakeup. */
983 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
984 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
985 }
986
987 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
988 {
989 if (pSetup->wIndex == 0)
990 {
991 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
992 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
993 }
994 Log(("usbHid: GET_STATUS (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
995 break;
996 }
997
998 case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
999 {
1000 if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
1001 {
1002 wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
1003 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1004 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1005 }
1006 Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
1007 break;
1008 }
1009
1010 default:
1011 Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
1012 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
1013 }
1014 break;
1015 }
1016
1017 case VUSB_REQ_CLEAR_FEATURE:
1018 break;
1019 }
1020
1021 /** @todo implement this. */
1022 Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1023 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1024
1025 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
1026 }
1027 else if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_CLASS)
1028 {
1029 switch (pSetup->bRequest)
1030 {
1031 case HID_REQ_SET_IDLE:
1032 {
1033 switch (pSetup->bmRequestType)
1034 {
1035 case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
1036 {
1037 Log(("usbHid: SET_IDLE wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1038 pThis->bIdle = pSetup->wValue >> 8;
1039 /* Consider 24ms to mean zero for keyboards (see IOUSBHIDDriver) */
1040 if (pThis->bIdle == 6) pThis->bIdle = 0;
1041 return usbHidCompleteOk(pThis, pUrb, 0);
1042 }
1043 break;
1044 }
1045 break;
1046 }
1047 case HID_REQ_GET_IDLE:
1048 {
1049 switch (pSetup->bmRequestType)
1050 {
1051 case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_HOST:
1052 {
1053 Log(("usbHid: GET_IDLE wValue=%#x wIndex=%#x, returning %#x\n", pSetup->wValue, pSetup->wIndex, pThis->bIdle));
1054 pUrb->abData[sizeof(*pSetup)] = pThis->bIdle;
1055 return usbHidCompleteOk(pThis, pUrb, 1);
1056 }
1057 break;
1058 }
1059 break;
1060 }
1061 case HID_REQ_SET_REPORT:
1062 {
1063 switch (pSetup->bmRequestType)
1064 {
1065 case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
1066 {
1067 Log(("usbHid: SET_REPORT wValue=%#x wIndex=%#x wLength=%#x\n", pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1068 usbHidSetReport(pThis, pUrb);
1069 return usbHidCompleteOk(pThis, pUrb, 0);
1070 }
1071 break;
1072 }
1073 break;
1074 }
1075 }
1076 Log(("usbHid: Unimplemented class request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1077 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1078
1079 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: class request stuff");
1080 }
1081 else
1082 {
1083 Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1084 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1085 return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
1086 }
1087
1088 return VINF_SUCCESS;
1089}
1090
1091
1092/**
1093 * @interface_method_impl{PDMUSBREG,pfnUrbQueue}
1094 */
1095static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
1096{
1097 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1098 LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
1099 RTCritSectEnter(&pThis->CritSect);
1100
1101 /*
1102 * Parse on a per end-point basis.
1103 */
1104 int rc;
1105 switch (pUrb->EndPt)
1106 {
1107 case 0:
1108 rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
1109 break;
1110
1111 case 0x81:
1112 AssertFailed();
1113 RT_FALL_THRU();
1114 case 0x01:
1115 rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
1116 break;
1117
1118 default:
1119 AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
1120 rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
1121 break;
1122 }
1123
1124 RTCritSectLeave(&pThis->CritSect);
1125 return rc;
1126}
1127
1128
1129/**
1130 * @interface_method_impl{PDMUSBREG,pfnUsbClearHaltedEndpoint}
1131 */
1132static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
1133{
1134 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1135 LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
1136
1137 if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
1138 {
1139 RTCritSectEnter(&pThis->CritSect);
1140 pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
1141 RTCritSectLeave(&pThis->CritSect);
1142 }
1143
1144 return VINF_SUCCESS;
1145}
1146
1147
1148/**
1149 * @interface_method_impl{PDMUSBREG,pfnUsbSetInterface}
1150 */
1151static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
1152{
1153 RT_NOREF3(pUsbIns, bInterfaceNumber, bAlternateSetting);
1154 LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
1155 Assert(bAlternateSetting == 0);
1156 return VINF_SUCCESS;
1157}
1158
1159
1160/**
1161 * @interface_method_impl{PDMUSBREG,pfnUsbSetConfiguration}
1162 */
1163static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
1164 const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
1165{
1166 RT_NOREF3(pvOldCfgDesc, pvOldIfState, pvNewCfgDesc);
1167 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1168 LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
1169 Assert(bConfigurationValue == 1);
1170 RTCritSectEnter(&pThis->CritSect);
1171
1172 /*
1173 * If the same config is applied more than once, it's a kind of reset.
1174 */
1175 if (pThis->bConfigurationValue == bConfigurationValue)
1176 usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
1177 pThis->bConfigurationValue = bConfigurationValue;
1178
1179 /*
1180 * Tell the other end that the keyboard is now enabled and wants
1181 * to receive keystrokes.
1182 */
1183 pThis->Lun0.pDrv->pfnSetActive(pThis->Lun0.pDrv, true);
1184
1185 RTCritSectLeave(&pThis->CritSect);
1186 return VINF_SUCCESS;
1187}
1188
1189
1190/**
1191 * @interface_method_impl{PDMUSBREG,pfnUsbGetDescriptorCache}
1192 */
1193static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
1194{
1195 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID); RT_NOREF_PV(pThis);
1196 LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
1197 return &g_UsbHidDescCache;
1198}
1199
1200
1201/**
1202 * @interface_method_impl{PDMUSBREG,pfnUsbReset}
1203 */
1204static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
1205{
1206 RT_NOREF1(fResetOnLinux);
1207 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1208 LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
1209 RTCritSectEnter(&pThis->CritSect);
1210
1211 int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
1212
1213 RTCritSectLeave(&pThis->CritSect);
1214 return rc;
1215}
1216
1217
1218/**
1219 * @interface_method_impl{PDMUSBREG,pfnDestruct}
1220 */
1221static DECLCALLBACK(void) usbHidDestruct(PPDMUSBINS pUsbIns)
1222{
1223 PDMUSB_CHECK_VERSIONS_RETURN_VOID(pUsbIns);
1224 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1225 LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
1226
1227 if (RTCritSectIsInitialized(&pThis->CritSect))
1228 {
1229 /* Let whoever runs in this critical section complete. */
1230 RTCritSectEnter(&pThis->CritSect);
1231 RTCritSectLeave(&pThis->CritSect);
1232 RTCritSectDelete(&pThis->CritSect);
1233 }
1234
1235 if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
1236 {
1237 RTSemEventDestroy(pThis->hEvtDoneQueue);
1238 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1239 }
1240}
1241
1242
1243/**
1244 * @interface_method_impl{PDMUSBREG,pfnConstruct}
1245 */
1246static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
1247{
1248 RT_NOREF1(pCfgGlobal);
1249 PDMUSB_CHECK_VERSIONS_RETURN(pUsbIns);
1250 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1251 Log(("usbHidConstruct/#%u:\n", iInstance));
1252
1253 /*
1254 * Perform the basic structure initialization first so the destructor
1255 * will not misbehave.
1256 */
1257 pThis->pUsbIns = pUsbIns;
1258 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1259 usbHidQueueInit(&pThis->ToHostQueue);
1260 usbHidQueueInit(&pThis->DoneQueue);
1261
1262 int rc = RTCritSectInit(&pThis->CritSect);
1263 AssertRCReturn(rc, rc);
1264
1265 rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
1266 AssertRCReturn(rc, rc);
1267
1268 /*
1269 * Validate and read the configuration.
1270 */
1271 rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbHid", iInstance);
1272 if (RT_FAILURE(rc))
1273 return rc;
1274
1275 pThis->Lun0.IBase.pfnQueryInterface = usbHidKeyboardQueryInterface;
1276 pThis->Lun0.IPort.pfnPutEventHid = usbHidKeyboardPutEvent;
1277
1278 /*
1279 * Attach the keyboard driver.
1280 */
1281 rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Keyboard Port");
1282 if (RT_FAILURE(rc))
1283 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach keyboard driver"));
1284
1285 pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIKEYBOARDCONNECTOR);
1286 if (!pThis->Lun0.pDrv)
1287 return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query keyboard interface"));
1288
1289 return VINF_SUCCESS;
1290}
1291
1292
1293/**
1294 * The USB Human Interface Device (HID) Keyboard registration record.
1295 */
1296const PDMUSBREG g_UsbHidKbd =
1297{
1298 /* u32Version */
1299 PDM_USBREG_VERSION,
1300 /* szName */
1301 "HidKeyboard",
1302 /* pszDescription */
1303 "USB HID Keyboard.",
1304 /* fFlags */
1305 0,
1306 /* cMaxInstances */
1307 ~0U,
1308 /* cbInstance */
1309 sizeof(USBHID),
1310 /* pfnConstruct */
1311 usbHidConstruct,
1312 /* pfnDestruct */
1313 usbHidDestruct,
1314 /* pfnVMInitComplete */
1315 NULL,
1316 /* pfnVMPowerOn */
1317 NULL,
1318 /* pfnVMReset */
1319 NULL,
1320 /* pfnVMSuspend */
1321 NULL,
1322 /* pfnVMResume */
1323 NULL,
1324 /* pfnVMPowerOff */
1325 NULL,
1326 /* pfnHotPlugged */
1327 NULL,
1328 /* pfnHotUnplugged */
1329 NULL,
1330 /* pfnDriverAttach */
1331 NULL,
1332 /* pfnDriverDetach */
1333 NULL,
1334 /* pfnQueryInterface */
1335 NULL,
1336 /* pfnUsbReset */
1337 usbHidUsbReset,
1338 /* pfnUsbGetDescriptorCache */
1339 usbHidUsbGetDescriptorCache,
1340 /* pfnUsbSetConfiguration */
1341 usbHidUsbSetConfiguration,
1342 /* pfnUsbSetInterface */
1343 usbHidUsbSetInterface,
1344 /* pfnUsbClearHaltedEndpoint */
1345 usbHidUsbClearHaltedEndpoint,
1346 /* pfnUrbNew */
1347 NULL/*usbHidUrbNew*/,
1348 /* pfnUrbQueue */
1349 usbHidQueue,
1350 /* pfnUrbCancel */
1351 usbHidUrbCancel,
1352 /* pfnUrbReap */
1353 usbHidUrbReap,
1354 /* pfnWakeup */
1355 usbHidWakeup,
1356 /* u32TheEnd */
1357 PDM_USBREG_VERSION
1358};
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