VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.3 KB
Line 
1/* $Id: UsbKbd.cpp 28800 2010-04-27 08:22:32Z 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 * Reset the device state.
612 */
613 pThis->enmState = USBHIDREQSTATE_READY;
614 pThis->bIdle = 0;
615 memset(&pThis->Report, 0, sizeof(pThis->Report));
616 pThis->fNoUrbSinceLastPress = false;
617 pThis->fHasPendingChanges = false;
618 memset(&pThis->abReleasedKeys[0], 0, sizeof(pThis->abReleasedKeys));
619
620 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
621 pThis->aEps[i].fHalted = false;
622
623 if (!pUrb && !fSetConfig) /* (only device reset) */
624 pThis->bConfigurationValue = 0; /* default */
625
626 /*
627 * Ditch all pending URBs.
628 */
629 PVUSBURB pCurUrb;
630 while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
631 {
632 pCurUrb->enmStatus = VUSBSTATUS_CRC;
633 usbHidLinkDone(pThis, pCurUrb);
634 }
635
636 if (pUrb)
637 return usbHidCompleteOk(pThis, pUrb, 0);
638 return VINF_SUCCESS;
639}
640
641static void usbHidUpdateReportReleased(PUSBHID pThis, uint8_t u8HidCode)
642{
643 unsigned i;
644
645 for (i = 0; i < RT_ELEMENTS(pThis->Report.aKeys); ++i)
646 {
647 if (pThis->Report.aKeys[i] == u8HidCode)
648 {
649 /* Remove key down. */
650 pThis->Report.aKeys[i] = 0;
651 }
652 }
653
654#if 0
655 if (i == RT_ELEMENTS(pThis->Report.aKeys))
656 {
657 Log(("Key 0x%x is up but was never down!?", u8HidCode));
658 }
659#endif
660}
661
662static void usbHidCommitReportReleased(PUSBHID pThis)
663{
664 unsigned i;
665 for (i = 0; i < RT_ELEMENTS(pThis->abReleasedKeys); ++i)
666 {
667 if (pThis->abReleasedKeys[i] != 0)
668 {
669 usbHidUpdateReportReleased(pThis, pThis->abReleasedKeys[i]);
670 pThis->abReleasedKeys[i] = 0;
671 }
672 }
673}
674
675#ifdef DEBUG
676# define HEX_DIGIT(x) (((x) < 0xa) ? ((x) + '0') : ((x) - 0xa + 'a'))
677static void usbHidComputePressed(PUSBHIDK_REPORT pReport, char* pszBuf, unsigned cbBuf)
678{
679 unsigned offBuf = 0;
680 unsigned i;
681 for (i = 0; i < RT_ELEMENTS(pReport->aKeys); ++i)
682 {
683 uint8_t uCode = pReport->aKeys[i];
684 if (uCode != 0)
685 {
686 if (offBuf + 4 >= cbBuf)
687 break;
688 pszBuf[offBuf++] = HEX_DIGIT(uCode >> 4);
689 pszBuf[offBuf++] = HEX_DIGIT(uCode & 0xf);
690 pszBuf[offBuf++] = ' ';
691 }
692 }
693 pszBuf[offBuf++] = '\0';
694}
695# undef HEX_DIGIT
696#endif
697
698/**
699 * Sends a state report to the host if there is a pending URB.
700 */
701static int usbHidSendReport(PUSBHID pThis)
702{
703 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
704 if (pUrb)
705 {
706 PUSBHIDK_REPORT pReport = &pThis->Report;
707 size_t cbCopy;
708
709#ifdef DEBUG
710 char szActiveBuf[128];
711 usbHidComputePressed(pReport, szActiveBuf, sizeof szActiveBuf);
712 Log2(("Sending report with pressed keys: %s\n", szActiveBuf));
713#endif
714 cbCopy = sizeof(*pReport);
715 memcpy(&pUrb->abData[0], pReport, cbCopy);
716 pThis->fNoUrbSinceLastPress = false;
717 pThis->fHasPendingChanges = false;
718 usbHidCommitReportReleased(pThis);
719#ifdef DEBUG
720 usbHidComputePressed(pReport, szActiveBuf, sizeof szActiveBuf);
721 Log2(("New state: %s\n", szActiveBuf));
722#endif
723// LogRel(("Sent report: %x : %x %x, size %d\n", pReport->ShiftState, pReport->aKeys[0], pReport->aKeys[1], cbCopy));
724 return usbHidCompleteOk(pThis, pUrb, cbCopy);
725 }
726 else
727 {
728 Log2(("No available URB for USB kbd\n"));
729 pThis->fHasPendingChanges = true;
730 }
731 return VINF_EOF;
732}
733
734/**
735 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
736 */
737static DECLCALLBACK(void *) usbHidKeyboardQueryInterface(PPDMIBASE pInterface, const char *pszIID)
738{
739 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
740 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
741 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDPORT, &pThis->Lun0.IPort);
742 return NULL;
743}
744
745/**
746 * Keyboard event handler.
747 *
748 * @returns VBox status code.
749 * @param pInterface Pointer to the keyboard port interface (KBDState::Keyboard.IPort).
750 * @param u8KeyCode The keycode.
751 */
752static DECLCALLBACK(int) usbHidKeyboardPutEvent(PPDMIKEYBOARDPORT pInterface, uint8_t u8KeyCode)
753{
754 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
755 PUSBHIDK_REPORT pReport = &pThis->Report;
756 uint32_t u32Usage = 0;
757 uint8_t u8HidCode;
758 int fKeyDown;
759 bool fHaveEvent = true;
760 unsigned i;
761
762 RTCritSectEnter(&pThis->CritSect);
763
764 pThis->XlatState = ScancodeToHidUsage(pThis->XlatState, u8KeyCode, &u32Usage);
765
766 if (pThis->XlatState == SS_IDLE)
767 {
768 /* The usage code is valid. */
769 fKeyDown = !(u32Usage & 0x80000000);
770 u8HidCode = u32Usage & 0xFF;
771
772 Log(("key %s: 0x%x->0x%x\n",
773 fKeyDown ? "down" : "up", u8KeyCode, u8HidCode));
774
775 if (fKeyDown)
776 {
777 for (i = 0; i < RT_ELEMENTS(pReport->aKeys); ++i)
778 {
779 if (pReport->aKeys[i] == u8HidCode)
780 {
781 /* Skip repeat events. */
782 fHaveEvent = false;
783 break;
784 }
785 }
786
787 if (fHaveEvent)
788 {
789 for (i = 0; i < RT_ELEMENTS(pReport->aKeys); ++i)
790 {
791 if (pReport->aKeys[i] == 0)
792 {
793 pReport->aKeys[i] = u8HidCode; /* Report key down. */
794 break;
795 }
796 }
797
798 pThis->fNoUrbSinceLastPress = true;
799
800 if (i == RT_ELEMENTS(pReport->aKeys))
801 {
802 /* We ran out of room. Report error. */
803 Log(("no more room in usbHidKeyboardPutEvent\n"));
804 /// @todo!!
805 }
806 }
807 }
808 else
809 {
810 /*
811 * We have to avoid coalescing key presses and releases,
812 * so put all releases somewhere else if press wasn't seen
813 * by the guest.
814 */
815 if (pThis->fNoUrbSinceLastPress)
816 {
817 for (i = 0; i < RT_ELEMENTS(pThis->abReleasedKeys); ++i)
818 {
819 if (pThis->abReleasedKeys[i] == u8HidCode)
820 break;
821
822 if (pThis->abReleasedKeys[i] == 0)
823 {
824 pThis->abReleasedKeys[i] = u8HidCode;
825 break;
826 }
827 }
828 }
829 else
830 usbHidUpdateReportReleased(pThis, u8HidCode);
831 }
832
833 /* Send a report if the host is already waiting for it. */
834 if (fHaveEvent)
835 usbHidSendReport(pThis);
836 }
837
838 RTCritSectLeave(&pThis->CritSect);
839
840 return VINF_SUCCESS;
841}
842
843/**
844 * @copydoc PDMUSBREG::pfnUrbReap
845 */
846static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
847{
848 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
849 //LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
850
851 RTCritSectEnter(&pThis->CritSect);
852
853 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
854 if (!pUrb && cMillies)
855 {
856 /* Wait */
857 pThis->fHaveDoneQueueWaiter = true;
858 RTCritSectLeave(&pThis->CritSect);
859
860 RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
861
862 RTCritSectEnter(&pThis->CritSect);
863 pThis->fHaveDoneQueueWaiter = false;
864
865 pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
866 }
867
868 RTCritSectLeave(&pThis->CritSect);
869
870 if (pUrb)
871 Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
872 return pUrb;
873}
874
875
876/**
877 * @copydoc PDMUSBREG::pfnUrbCancel
878 */
879static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
880{
881 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
882 LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
883 RTCritSectEnter(&pThis->CritSect);
884
885 /*
886 * Remove the URB from the to-host queue and move it onto the done queue.
887 */
888 if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
889 usbHidLinkDone(pThis, pUrb);
890
891 RTCritSectLeave(&pThis->CritSect);
892 return VINF_SUCCESS;
893}
894
895
896/**
897 * Handles request sent to the inbound (device to host) interrupt pipe. This is
898 * rather different from bulk requests because an interrupt read URB may complete
899 * after arbitrarily long time.
900 */
901static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
902{
903 /*
904 * Stall the request if the pipe is halted.
905 */
906 if (RT_UNLIKELY(pEp->fHalted))
907 return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
908
909 /*
910 * Deal with the URB according to the state.
911 */
912 switch (pThis->enmState)
913 {
914 /*
915 * We've data left to transfer to the host.
916 */
917 case USBHIDREQSTATE_DATA_TO_HOST:
918 {
919 AssertFailed();
920 Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
921 return usbHidCompleteOk(pThis, pUrb, 0);
922 }
923
924 /*
925 * Status transfer.
926 */
927 case USBHIDREQSTATE_STATUS:
928 {
929 AssertFailed();
930 Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
931 pThis->enmState = USBHIDREQSTATE_READY;
932 return usbHidCompleteOk(pThis, pUrb, 0);
933 }
934
935 case USBHIDREQSTATE_READY:
936 usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
937 /* If device was not set idle, sent the current report right away. */
938 if (pThis->bIdle != 0 || pThis->fHasPendingChanges)
939 usbHidSendReport(pThis);
940 LogFlow(("usbHidHandleIntrDevToHost: Sent report via %p:%s\n", pUrb, pUrb->pszDesc));
941 return VINF_SUCCESS;
942
943 /*
944 * Bad states, stall.
945 */
946 default:
947 Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
948 return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
949 }
950}
951
952
953/**
954 * Handles request sent to the default control pipe.
955 */
956static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
957{
958 PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
959 LogFlow(("usbHidHandleDefaultPipe: cbData=%d\n", pUrb->cbData));
960
961 AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
962
963 if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
964 {
965 switch (pSetup->bRequest)
966 {
967 case VUSB_REQ_GET_DESCRIPTOR:
968 {
969 switch (pSetup->bmRequestType)
970 {
971 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
972 {
973 switch (pSetup->wValue >> 8)
974 {
975 case VUSB_DT_STRING:
976 Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
977 break;
978 default:
979 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
980 break;
981 }
982 break;
983 }
984
985 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
986 {
987 switch (pSetup->wValue >> 8)
988 {
989 case DT_IF_HID_REPORT:
990 uint32_t cbCopy;
991
992 /* Returned data is written after the setup message. */
993 cbCopy = pUrb->cbData - sizeof(*pSetup);
994 cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidReportDesc));
995 Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
996 memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidReportDesc, cbCopy);
997 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
998 default:
999 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1000 break;
1001 }
1002 break;
1003 }
1004
1005 default:
1006 Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
1007 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
1008 }
1009 break;
1010 }
1011
1012 case VUSB_REQ_GET_STATUS:
1013 {
1014 uint16_t wRet = 0;
1015
1016 if (pSetup->wLength != 2)
1017 {
1018 Log(("usbHid: Bad GET_STATUS req: wLength=%#x\n", pSetup->wLength));
1019 break;
1020 }
1021 Assert(pSetup->wValue == 0);
1022 switch (pSetup->bmRequestType)
1023 {
1024 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1025 {
1026 Assert(pSetup->wIndex == 0);
1027 Log(("usbHid: GET_STATUS (device)\n"));
1028 wRet = 0; /* Not self-powered, no remote wakeup. */
1029 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1030 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1031 }
1032
1033 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1034 {
1035 if (pSetup->wIndex == 0)
1036 {
1037 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1038 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1039 }
1040 else
1041 {
1042 Log(("usbHid: GET_STATUS (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
1043 }
1044 break;
1045 }
1046
1047 case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1048 {
1049 if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
1050 {
1051 wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
1052 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1053 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1054 }
1055 else
1056 {
1057 Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
1058 }
1059 break;
1060 }
1061
1062 default:
1063 Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
1064 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
1065 }
1066 break;
1067 }
1068
1069 case VUSB_REQ_CLEAR_FEATURE:
1070 break;
1071 }
1072
1073 /** @todo implement this. */
1074 Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1075 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1076
1077 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
1078 }
1079 else if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_CLASS)
1080 {
1081 switch (pSetup->bRequest)
1082 {
1083 case HID_REQ_SET_IDLE:
1084 {
1085 switch (pSetup->bmRequestType)
1086 {
1087 case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_DEVICE:
1088 {
1089 Log(("usbHid: SET_IDLE wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1090 pThis->bIdle = pSetup->wValue >> 8;
1091 /* Consider 24ms to mean zero for keyboards (see IOUSBHIDDriver) */
1092 if (pThis->bIdle == 6) pThis->bIdle = 0;
1093 return usbHidCompleteOk(pThis, pUrb, 0);
1094 }
1095 break;
1096 }
1097 break;
1098 }
1099 case HID_REQ_GET_IDLE:
1100 {
1101 switch (pSetup->bmRequestType)
1102 {
1103 case VUSB_TO_INTERFACE | VUSB_REQ_CLASS | VUSB_DIR_TO_HOST:
1104 {
1105 Log(("usbHid: GET_IDLE wValue=%#x wIndex=%#x, returning %#x\n", pSetup->wValue, pSetup->wIndex, pThis->bIdle));
1106 pUrb->abData[sizeof(*pSetup)] = pThis->bIdle;
1107 return usbHidCompleteOk(pThis, pUrb, 1);
1108 }
1109 break;
1110 }
1111 break;
1112 }
1113 }
1114 Log(("usbHid: Unimplemented class request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1115 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1116
1117 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: class request stuff");
1118 }
1119 else
1120 {
1121 Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1122 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1123 return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
1124 }
1125
1126 return VINF_SUCCESS;
1127}
1128
1129
1130/**
1131 * @copydoc PDMUSBREG::pfnQueue
1132 */
1133static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
1134{
1135 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1136 LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
1137 RTCritSectEnter(&pThis->CritSect);
1138
1139 /*
1140 * Parse on a per end-point basis.
1141 */
1142 int rc;
1143 switch (pUrb->EndPt)
1144 {
1145 case 0:
1146 rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
1147 break;
1148
1149 case 0x81:
1150 AssertFailed();
1151 case 0x01:
1152 rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
1153 break;
1154
1155 default:
1156 AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
1157 rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
1158 break;
1159 }
1160
1161 RTCritSectLeave(&pThis->CritSect);
1162 return rc;
1163}
1164
1165
1166/**
1167 * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
1168 */
1169static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
1170{
1171 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1172 LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
1173
1174 if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
1175 {
1176 RTCritSectEnter(&pThis->CritSect);
1177 pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
1178 RTCritSectLeave(&pThis->CritSect);
1179 }
1180
1181 return VINF_SUCCESS;
1182}
1183
1184
1185/**
1186 * @copydoc PDMUSBREG::pfnUsbSetInterface
1187 */
1188static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
1189{
1190 LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
1191 Assert(bAlternateSetting == 0);
1192 return VINF_SUCCESS;
1193}
1194
1195
1196/**
1197 * @copydoc PDMUSBREG::pfnUsbSetConfiguration
1198 */
1199static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
1200 const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
1201{
1202 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1203 LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
1204 Assert(bConfigurationValue == 1);
1205 RTCritSectEnter(&pThis->CritSect);
1206
1207 /*
1208 * If the same config is applied more than once, it's a kind of reset.
1209 */
1210 if (pThis->bConfigurationValue == bConfigurationValue)
1211 usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
1212 pThis->bConfigurationValue = bConfigurationValue;
1213
1214 RTCritSectLeave(&pThis->CritSect);
1215 return VINF_SUCCESS;
1216}
1217
1218
1219/**
1220 * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
1221 */
1222static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
1223{
1224 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1225 LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
1226 return &g_UsbHidDescCache;
1227}
1228
1229
1230/**
1231 * @copydoc PDMUSBREG::pfnUsbReset
1232 */
1233static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
1234{
1235 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1236 LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
1237 RTCritSectEnter(&pThis->CritSect);
1238
1239 int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
1240
1241 RTCritSectLeave(&pThis->CritSect);
1242 return rc;
1243}
1244
1245
1246/**
1247 * @copydoc PDMUSBREG::pfnDestruct
1248 */
1249static void usbHidDestruct(PPDMUSBINS pUsbIns)
1250{
1251 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1252 LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
1253
1254 if (RTCritSectIsInitialized(&pThis->CritSect))
1255 {
1256 /* Let whoever runs in this critical section complete. */
1257 RTCritSectEnter(&pThis->CritSect);
1258 RTCritSectLeave(&pThis->CritSect);
1259 RTCritSectDelete(&pThis->CritSect);
1260 }
1261
1262 if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
1263 {
1264 RTSemEventDestroy(pThis->hEvtDoneQueue);
1265 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1266 }
1267}
1268
1269
1270/**
1271 * @copydoc PDMUSBREG::pfnConstruct
1272 */
1273static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
1274{
1275 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1276 Log(("usbHidConstruct/#%u:\n", iInstance));
1277
1278 /*
1279 * Perform the basic structure initialization first so the destructor
1280 * will not misbehave.
1281 */
1282 pThis->pUsbIns = pUsbIns;
1283 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1284 pThis->XlatState = SS_IDLE;
1285 usbHidQueueInit(&pThis->ToHostQueue);
1286 usbHidQueueInit(&pThis->DoneQueue);
1287
1288 int rc = RTCritSectInit(&pThis->CritSect);
1289 AssertRCReturn(rc, rc);
1290
1291 rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
1292 AssertRCReturn(rc, rc);
1293
1294 /*
1295 * Validate and read the configuration.
1296 */
1297 rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbHid", iInstance);
1298 if (RT_FAILURE(rc))
1299 return rc;
1300
1301 pThis->Lun0.IBase.pfnQueryInterface = usbHidKeyboardQueryInterface;
1302 pThis->Lun0.IPort.pfnPutEvent = usbHidKeyboardPutEvent;
1303
1304 /*
1305 * Attach the keyboard driver.
1306 */
1307 rc = pUsbIns->pHlpR3->pfnDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Keyboard Port");
1308 if (RT_FAILURE(rc))
1309 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach keyboard driver"));
1310
1311 return VINF_SUCCESS;
1312}
1313
1314
1315/**
1316 * The USB Human Interface Device (HID) Keyboard registration record.
1317 */
1318const PDMUSBREG g_UsbHidKbd =
1319{
1320 /* u32Version */
1321 PDM_USBREG_VERSION,
1322 /* szName */
1323 "HidKeyboard",
1324 /* pszDescription */
1325 "USB HID Keyboard.",
1326 /* fFlags */
1327 0,
1328 /* cMaxInstances */
1329 ~0,
1330 /* cbInstance */
1331 sizeof(USBHID),
1332 /* pfnConstruct */
1333 usbHidConstruct,
1334 /* pfnDestruct */
1335 usbHidDestruct,
1336 /* pfnVMInitComplete */
1337 NULL,
1338 /* pfnVMPowerOn */
1339 NULL,
1340 /* pfnVMReset */
1341 NULL,
1342 /* pfnVMSuspend */
1343 NULL,
1344 /* pfnVMResume */
1345 NULL,
1346 /* pfnVMPowerOff */
1347 NULL,
1348 /* pfnHotPlugged */
1349 NULL,
1350 /* pfnHotUnplugged */
1351 NULL,
1352 /* pfnDriverAttach */
1353 NULL,
1354 /* pfnDriverDetach */
1355 NULL,
1356 /* pfnQueryInterface */
1357 NULL,
1358 /* pfnUsbReset */
1359 usbHidUsbReset,
1360 /* pfnUsbGetCachedDescriptors */
1361 usbHidUsbGetDescriptorCache,
1362 /* pfnUsbSetConfiguration */
1363 usbHidUsbSetConfiguration,
1364 /* pfnUsbSetInterface */
1365 usbHidUsbSetInterface,
1366 /* pfnUsbClearHaltedEndpoint */
1367 usbHidUsbClearHaltedEndpoint,
1368 /* pfnUrbNew */
1369 NULL/*usbHidUrbNew*/,
1370 /* pfnQueue */
1371 usbHidQueue,
1372 /* pfnUrbCancel */
1373 usbHidUrbCancel,
1374 /* pfnUrbReap */
1375 usbHidUrbReap,
1376 /* u32TheEnd */
1377 PDM_USBREG_VERSION
1378};
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