VirtualBox

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

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

UsbKbd.cpp: Use doxygen comment where applicable; hungarian typos; don't wrap RTCritSectEnter/Leave or the lock validator doesn't get the best data to work on.

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