VirtualBox

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

Last change on this file since 68080 was 66989, checked in by vboxsync, 8 years ago

VUSB: Collect opaque class-specific data between config and interface descriptors. See bugref:8769

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.5 KB
Line 
1/* $Id: UsbKbd.cpp 66989 2017-05-19 14:42:59Z vboxsync $ */
2/** @file
3 * UsbKbd - USB Human Interface Device Emulation, Keyboard.
4 */
5
6/*
7 * Copyright (C) 2007-2016 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 else
995 {
996 Log(("usbHid: GET_STATUS (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
997 }
998 break;
999 }
1000
1001 case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1002 {
1003 if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
1004 {
1005 wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
1006 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1007 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1008 }
1009 else
1010 {
1011 Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
1012 }
1013 break;
1014 }
1015
1016 default:
1017 Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
1018 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
1019 }
1020 break;
1021 }
1022
1023 case VUSB_REQ_CLEAR_FEATURE:
1024 break;
1025 }
1026
1027 /** @todo implement this. */
1028 Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1029 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1030
1031 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
1032 }
1033 else if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_CLASS)
1034 {
1035 switch (pSetup->bRequest)
1036 {
1037 case HID_REQ_SET_IDLE:
1038 {
1039 switch (pSetup->bmRequestType)
1040 {
1041 case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
1042 {
1043 Log(("usbHid: SET_IDLE wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1044 pThis->bIdle = pSetup->wValue >> 8;
1045 /* Consider 24ms to mean zero for keyboards (see IOUSBHIDDriver) */
1046 if (pThis->bIdle == 6) pThis->bIdle = 0;
1047 return usbHidCompleteOk(pThis, pUrb, 0);
1048 }
1049 break;
1050 }
1051 break;
1052 }
1053 case HID_REQ_GET_IDLE:
1054 {
1055 switch (pSetup->bmRequestType)
1056 {
1057 case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_HOST:
1058 {
1059 Log(("usbHid: GET_IDLE wValue=%#x wIndex=%#x, returning %#x\n", pSetup->wValue, pSetup->wIndex, pThis->bIdle));
1060 pUrb->abData[sizeof(*pSetup)] = pThis->bIdle;
1061 return usbHidCompleteOk(pThis, pUrb, 1);
1062 }
1063 break;
1064 }
1065 break;
1066 }
1067 case HID_REQ_SET_REPORT:
1068 {
1069 switch (pSetup->bmRequestType)
1070 {
1071 case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
1072 {
1073 Log(("usbHid: SET_REPORT wValue=%#x wIndex=%#x wLength=%#x\n", pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1074 usbHidSetReport(pThis, pUrb);
1075 return usbHidCompleteOk(pThis, pUrb, 0);
1076 }
1077 break;
1078 }
1079 break;
1080 }
1081 }
1082 Log(("usbHid: Unimplemented class request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1083 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1084
1085 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: class request stuff");
1086 }
1087 else
1088 {
1089 Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1090 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1091 return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
1092 }
1093
1094 return VINF_SUCCESS;
1095}
1096
1097
1098/**
1099 * @interface_method_impl{PDMUSBREG,pfnUrbQueue}
1100 */
1101static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
1102{
1103 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1104 LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
1105 RTCritSectEnter(&pThis->CritSect);
1106
1107 /*
1108 * Parse on a per end-point basis.
1109 */
1110 int rc;
1111 switch (pUrb->EndPt)
1112 {
1113 case 0:
1114 rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
1115 break;
1116
1117 case 0x81:
1118 AssertFailed();
1119 /* fall thru */
1120 case 0x01:
1121 rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
1122 break;
1123
1124 default:
1125 AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
1126 rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
1127 break;
1128 }
1129
1130 RTCritSectLeave(&pThis->CritSect);
1131 return rc;
1132}
1133
1134
1135/**
1136 * @interface_method_impl{PDMUSBREG,pfnUsbClearHaltedEndpoint}
1137 */
1138static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
1139{
1140 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1141 LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
1142
1143 if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
1144 {
1145 RTCritSectEnter(&pThis->CritSect);
1146 pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
1147 RTCritSectLeave(&pThis->CritSect);
1148 }
1149
1150 return VINF_SUCCESS;
1151}
1152
1153
1154/**
1155 * @interface_method_impl{PDMUSBREG,pfnUsbSetInterface}
1156 */
1157static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
1158{
1159 RT_NOREF3(pUsbIns, bInterfaceNumber, bAlternateSetting);
1160 LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
1161 Assert(bAlternateSetting == 0);
1162 return VINF_SUCCESS;
1163}
1164
1165
1166/**
1167 * @interface_method_impl{PDMUSBREG,pfnUsbSetConfiguration}
1168 */
1169static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
1170 const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
1171{
1172 RT_NOREF3(pvOldCfgDesc, pvOldIfState, pvNewCfgDesc);
1173 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1174 LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
1175 Assert(bConfigurationValue == 1);
1176 RTCritSectEnter(&pThis->CritSect);
1177
1178 /*
1179 * If the same config is applied more than once, it's a kind of reset.
1180 */
1181 if (pThis->bConfigurationValue == bConfigurationValue)
1182 usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
1183 pThis->bConfigurationValue = bConfigurationValue;
1184
1185 /*
1186 * Tell the other end that the keyboard is now enabled and wants
1187 * to receive keystrokes.
1188 */
1189 pThis->Lun0.pDrv->pfnSetActive(pThis->Lun0.pDrv, true);
1190
1191 RTCritSectLeave(&pThis->CritSect);
1192 return VINF_SUCCESS;
1193}
1194
1195
1196/**
1197 * @interface_method_impl{PDMUSBREG,pfnUsbGetDescriptorCache}
1198 */
1199static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
1200{
1201 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID); RT_NOREF_PV(pThis);
1202 LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
1203 return &g_UsbHidDescCache;
1204}
1205
1206
1207/**
1208 * @interface_method_impl{PDMUSBREG,pfnUsbReset}
1209 */
1210static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
1211{
1212 RT_NOREF1(fResetOnLinux);
1213 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1214 LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
1215 RTCritSectEnter(&pThis->CritSect);
1216
1217 int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
1218
1219 RTCritSectLeave(&pThis->CritSect);
1220 return rc;
1221}
1222
1223
1224/**
1225 * @interface_method_impl{PDMUSBREG,pfnDestruct}
1226 */
1227static DECLCALLBACK(void) usbHidDestruct(PPDMUSBINS pUsbIns)
1228{
1229 PDMUSB_CHECK_VERSIONS_RETURN_VOID(pUsbIns);
1230 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1231 LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
1232
1233 if (RTCritSectIsInitialized(&pThis->CritSect))
1234 {
1235 /* Let whoever runs in this critical section complete. */
1236 RTCritSectEnter(&pThis->CritSect);
1237 RTCritSectLeave(&pThis->CritSect);
1238 RTCritSectDelete(&pThis->CritSect);
1239 }
1240
1241 if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
1242 {
1243 RTSemEventDestroy(pThis->hEvtDoneQueue);
1244 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1245 }
1246}
1247
1248
1249/**
1250 * @interface_method_impl{PDMUSBREG,pfnConstruct}
1251 */
1252static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
1253{
1254 RT_NOREF1(pCfgGlobal);
1255 PDMUSB_CHECK_VERSIONS_RETURN(pUsbIns);
1256 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1257 Log(("usbHidConstruct/#%u:\n", iInstance));
1258
1259 /*
1260 * Perform the basic structure initialization first so the destructor
1261 * will not misbehave.
1262 */
1263 pThis->pUsbIns = pUsbIns;
1264 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1265 usbHidQueueInit(&pThis->ToHostQueue);
1266 usbHidQueueInit(&pThis->DoneQueue);
1267
1268 int rc = RTCritSectInit(&pThis->CritSect);
1269 AssertRCReturn(rc, rc);
1270
1271 rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
1272 AssertRCReturn(rc, rc);
1273
1274 /*
1275 * Validate and read the configuration.
1276 */
1277 rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbHid", iInstance);
1278 if (RT_FAILURE(rc))
1279 return rc;
1280
1281 pThis->Lun0.IBase.pfnQueryInterface = usbHidKeyboardQueryInterface;
1282 pThis->Lun0.IPort.pfnPutEventHid = usbHidKeyboardPutEvent;
1283
1284 /*
1285 * Attach the keyboard driver.
1286 */
1287 rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Keyboard Port");
1288 if (RT_FAILURE(rc))
1289 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach keyboard driver"));
1290
1291 pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIKEYBOARDCONNECTOR);
1292 if (!pThis->Lun0.pDrv)
1293 return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query keyboard interface"));
1294
1295 return VINF_SUCCESS;
1296}
1297
1298
1299/**
1300 * The USB Human Interface Device (HID) Keyboard registration record.
1301 */
1302const PDMUSBREG g_UsbHidKbd =
1303{
1304 /* u32Version */
1305 PDM_USBREG_VERSION,
1306 /* szName */
1307 "HidKeyboard",
1308 /* pszDescription */
1309 "USB HID Keyboard.",
1310 /* fFlags */
1311 0,
1312 /* cMaxInstances */
1313 ~0U,
1314 /* cbInstance */
1315 sizeof(USBHID),
1316 /* pfnConstruct */
1317 usbHidConstruct,
1318 /* pfnDestruct */
1319 usbHidDestruct,
1320 /* pfnVMInitComplete */
1321 NULL,
1322 /* pfnVMPowerOn */
1323 NULL,
1324 /* pfnVMReset */
1325 NULL,
1326 /* pfnVMSuspend */
1327 NULL,
1328 /* pfnVMResume */
1329 NULL,
1330 /* pfnVMPowerOff */
1331 NULL,
1332 /* pfnHotPlugged */
1333 NULL,
1334 /* pfnHotUnplugged */
1335 NULL,
1336 /* pfnDriverAttach */
1337 NULL,
1338 /* pfnDriverDetach */
1339 NULL,
1340 /* pfnQueryInterface */
1341 NULL,
1342 /* pfnUsbReset */
1343 usbHidUsbReset,
1344 /* pfnUsbGetDescriptorCache */
1345 usbHidUsbGetDescriptorCache,
1346 /* pfnUsbSetConfiguration */
1347 usbHidUsbSetConfiguration,
1348 /* pfnUsbSetInterface */
1349 usbHidUsbSetInterface,
1350 /* pfnUsbClearHaltedEndpoint */
1351 usbHidUsbClearHaltedEndpoint,
1352 /* pfnUrbNew */
1353 NULL/*usbHidUrbNew*/,
1354 /* pfnUrbQueue */
1355 usbHidQueue,
1356 /* pfnUrbCancel */
1357 usbHidUrbCancel,
1358 /* pfnUrbReap */
1359 usbHidUrbReap,
1360 /* pfnWakeup */
1361 usbHidWakeup,
1362 /* u32TheEnd */
1363 PDM_USBREG_VERSION
1364};
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