VirtualBox

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

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

Route input to PS/2 keyboard unless USB keyboard was explicitly activated.

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