VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/UsbMouse.cpp@ 46818

Last change on this file since 46818 was 46818, checked in by vboxsync, 12 years ago

Devices/Input/UsbMouse: added the specification multi-touch descriptor, not yet active.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 54.9 KB
Line 
1/** @file
2 * UsbMouse - USB Human Interface Device Emulation (Mouse).
3 */
4
5/*
6 * Copyright (C) 2007-2012 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17/*******************************************************************************
18* Header Files *
19*******************************************************************************/
20#define LOG_GROUP LOG_GROUP_USB_MOUSE
21#include <VBox/vmm/pdmusb.h>
22#include <VBox/log.h>
23#include <VBox/err.h>
24#include <iprt/assert.h>
25#include <iprt/critsect.h>
26#include <iprt/mem.h>
27#include <iprt/semaphore.h>
28#include <iprt/string.h>
29#include <iprt/uuid.h>
30#include "VBoxDD.h"
31
32
33/*******************************************************************************
34* Defined Constants And Macros *
35*******************************************************************************/
36/** @name USB HID string IDs
37 * @{ */
38#define USBHID_STR_ID_MANUFACTURER 1
39#define USBHID_STR_ID_PRODUCT_M 2
40#define USBHID_STR_ID_PRODUCT_T 3
41/** @} */
42
43/** @name USB HID specific descriptor types
44 * @{ */
45#define DT_IF_HID_DESCRIPTOR 0x21
46#define DT_IF_HID_REPORT 0x22
47/** @} */
48
49/** @name USB HID vendor and product IDs
50 * @{ */
51#define VBOX_USB_VENDOR 0x80EE
52#define USBHID_PID_MOUSE 0x0020
53#define USBHID_PID_TABLET 0x0021
54/** @} */
55
56/*******************************************************************************
57* Structures and Typedefs *
58*******************************************************************************/
59
60/**
61 * The USB HID request state.
62 */
63typedef enum USBHIDREQSTATE
64{
65 /** Invalid status. */
66 USBHIDREQSTATE_INVALID = 0,
67 /** Ready to receive a new read request. */
68 USBHIDREQSTATE_READY,
69 /** Have (more) data for the host. */
70 USBHIDREQSTATE_DATA_TO_HOST,
71 /** Waiting to supply status information to the host. */
72 USBHIDREQSTATE_STATUS,
73 /** The end of the valid states. */
74 USBHIDREQSTATE_END
75} USBHIDREQSTATE;
76
77
78/**
79 * Endpoint status data.
80 */
81typedef struct USBHIDEP
82{
83 bool fHalted;
84} USBHIDEP;
85/** Pointer to the endpoint status. */
86typedef USBHIDEP *PUSBHIDEP;
87
88
89/**
90 * A URB queue.
91 */
92typedef struct USBHIDURBQUEUE
93{
94 /** The head pointer. */
95 PVUSBURB pHead;
96 /** Where to insert the next entry. */
97 PVUSBURB *ppTail;
98} USBHIDURBQUEUE;
99/** Pointer to a URB queue. */
100typedef USBHIDURBQUEUE *PUSBHIDURBQUEUE;
101/** Pointer to a const URB queue. */
102typedef USBHIDURBQUEUE const *PCUSBHIDURBQUEUE;
103
104
105/**
106 * Mouse movement accumulator.
107 */
108typedef struct USBHIDM_ACCUM
109{
110 uint32_t btn;
111 int32_t dX;
112 int32_t dY;
113 int32_t dZ;
114} USBHIDM_ACCUM, *PUSBHIDM_ACCUM;
115
116
117/**
118 * The USB HID instance data.
119 */
120typedef struct USBHID
121{
122 /** Pointer back to the PDM USB Device instance structure. */
123 PPDMUSBINS pUsbIns;
124 /** Critical section protecting the device state. */
125 RTCRITSECT CritSect;
126
127 /** The current configuration.
128 * (0 - default, 1 - the one supported configuration, i.e configured.) */
129 uint8_t bConfigurationValue;
130 /** Endpoint 0 is the default control pipe, 1 is the dev->host interrupt one. */
131 USBHIDEP aEps[2];
132 /** The state of the HID (state machine).*/
133 USBHIDREQSTATE enmState;
134
135 /** Pointer movement accumulator. */
136 USBHIDM_ACCUM PtrDelta;
137
138 /** Pending to-host queue.
139 * The URBs waiting here are waiting for data to become available.
140 */
141 USBHIDURBQUEUE ToHostQueue;
142
143 /** Done queue
144 * The URBs stashed here are waiting to be reaped. */
145 USBHIDURBQUEUE DoneQueue;
146 /** Signalled when adding an URB to the done queue and fHaveDoneQueueWaiter
147 * is set. */
148 RTSEMEVENT hEvtDoneQueue;
149
150 /** Someone is waiting on the done queue. */
151 bool fHaveDoneQueueWaiter;
152 /** If device has pending changes. */
153 bool fHasPendingChanges;
154 /** Is this an absolute pointing device (tablet)? Relative (mouse) otherwise. */
155 bool isAbsolute;
156 /** Tablet coordinate shift factor for old and broken operating systems. */
157 uint8_t u8CoordShift;
158
159 /**
160 * Mouse port - LUN#0.
161 *
162 * @implements PDMIBASE
163 * @implements PDMIMOUSEPORT
164 */
165 struct
166 {
167 /** The base interface for the mouse port. */
168 PDMIBASE IBase;
169 /** The mouse port base interface. */
170 PDMIMOUSEPORT IPort;
171
172 /** The base interface of the attached mouse driver. */
173 R3PTRTYPE(PPDMIBASE) pDrvBase;
174 /** The mouse interface of the attached mouse driver. */
175 R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
176 } Lun0;
177
178} USBHID;
179/** Pointer to the USB HID instance data. */
180typedef USBHID *PUSBHID;
181
182/**
183 * The USB HID report structure for relative device.
184 */
185typedef struct USBHIDM_REPORT
186{
187 uint8_t btn;
188 int8_t dx;
189 int8_t dy;
190 int8_t dz;
191} USBHIDM_REPORT, *PUSBHIDM_REPORT;
192
193/**
194 * The USB HID report structure for absolute device.
195 */
196
197typedef struct USBHIDT_REPORT
198{
199 uint8_t rid;
200 uint8_t btn;
201 int8_t dz;
202 int8_t dummy;
203 uint16_t cx;
204 uint16_t cy;
205} USBHIDT_REPORT, *PUSBHIDT_REPORT;
206
207/**
208 * The combined USB HID report union for relative and absolute device.
209 */
210typedef union USBHIDTM_REPORT
211{
212 USBHIDT_REPORT t;
213 USBHIDM_REPORT m;
214} USBHIDTM_REPORT, *PUSBHIDTM_REPORT;
215
216/*******************************************************************************
217* Global Variables *
218*******************************************************************************/
219static const PDMUSBDESCCACHESTRING g_aUsbHidStrings_en_US[] =
220{
221 { USBHID_STR_ID_MANUFACTURER, "VirtualBox" },
222 { USBHID_STR_ID_PRODUCT_M, "USB Mouse" },
223 { USBHID_STR_ID_PRODUCT_T, "USB Tablet" },
224};
225
226static const PDMUSBDESCCACHELANG g_aUsbHidLanguages[] =
227{
228 { 0x0409, RT_ELEMENTS(g_aUsbHidStrings_en_US), g_aUsbHidStrings_en_US }
229};
230
231static const VUSBDESCENDPOINTEX g_aUsbHidMEndpointDescs[] =
232{
233 {
234 {
235 /* .bLength = */ sizeof(VUSBDESCENDPOINT),
236 /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
237 /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
238 /* .bmAttributes = */ 3 /* interrupt */,
239 /* .wMaxPacketSize = */ 4,
240 /* .bInterval = */ 10,
241 },
242 /* .pvMore = */ NULL,
243 /* .pvClass = */ NULL,
244 /* .cbClass = */ 0
245 },
246};
247
248static const VUSBDESCENDPOINTEX g_aUsbHidTEndpointDescs[] =
249{
250 {
251 {
252 /* .bLength = */ sizeof(VUSBDESCENDPOINT),
253 /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
254 /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
255 /* .bmAttributes = */ 3 /* interrupt */,
256 /* .wMaxPacketSize = */ 6,
257 /* .bInterval = */ 10,
258 },
259 /* .pvMore = */ NULL,
260 /* .pvClass = */ NULL,
261 /* .cbClass = */ 0
262 },
263};
264
265/* HID report descriptor (mouse). */
266static const uint8_t g_UsbHidMReportDesc[] =
267{
268 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
269 /* Usage */ 0x09, 0x02, /* Mouse */
270 /* Collection */ 0xA1, 0x01, /* Application */
271 /* Usage */ 0x09, 0x01, /* Pointer */
272 /* Collection */ 0xA1, 0x00, /* Physical */
273 /* Usage Page */ 0x05, 0x09, /* Button */
274 /* Usage Minimum */ 0x19, 0x01, /* Button 1 */
275 /* Usage Maximum */ 0x29, 0x05, /* Button 5 */
276 /* Logical Minimum */ 0x15, 0x00, /* 0 */
277 /* Logical Maximum */ 0x25, 0x01, /* 1 */
278 /* Report Count */ 0x95, 0x05, /* 5 */
279 /* Report Size */ 0x75, 0x01, /* 1 */
280 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
281 /* Report Count */ 0x95, 0x01, /* 1 */
282 /* Report Size */ 0x75, 0x03, /* 3 (padding bits) */
283 /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
284 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
285 /* Usage */ 0x09, 0x30, /* X */
286 /* Usage */ 0x09, 0x31, /* Y */
287 /* Usage */ 0x09, 0x38, /* Z (wheel) */
288 /* Logical Minimum */ 0x15, 0x81, /* -127 */
289 /* Logical Maximum */ 0x25, 0x7F, /* +127 */
290 /* Report Size */ 0x75, 0x08, /* 8 */
291 /* Report Count */ 0x95, 0x03, /* 3 */
292 /* Input */ 0x81, 0x06, /* Data, Value, Relative, Bit field */
293 /* End Collection */ 0xC0,
294 /* End Collection */ 0xC0,
295};
296
297/* HID report descriptor (tablet). */
298/* NB: The layout is far from random. Having the buttons and Z axis grouped
299 * together avoids alignment issues. Also, if X/Y is reported first, followed
300 * by buttons/Z, Windows gets phantom Z movement. That is likely a bug in Windows
301 * as OS X shows no such problem. When X/Y is reported last, Windows behaves
302 * properly.
303 */
304#define REPORTID_MOUSE 1
305#define REPORTID_MTOUCH 2
306#define REPORTID_MAX_COUNT 3
307#define REPORTID_FEATURE 4
308
309static const uint8_t g_UsbHidTReportDesc[] =
310{
311 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
312 /* Usage */ 0x09, 0x02, /* Mouse */
313 /* Collection */ 0xA1, 0x01, /* Application */
314 /* Report ID */ 0x85, REPORTID_MOUSE,
315 /* Usage */ 0x09, 0x01, /* Pointer */
316 /* Collection */ 0xA1, 0x00, /* Physical */
317 /* Usage Page */ 0x05, 0x09, /* Button */
318 /* Usage Minimum */ 0x19, 0x01, /* Button 1 */
319 /* Usage Maximum */ 0x29, 0x05, /* Button 5 */
320 /* Logical Minimum */ 0x15, 0x00, /* 0 */
321 /* Logical Maximum */ 0x25, 0x01, /* 1 */
322 /* Report Count */ 0x95, 0x05, /* 5 */
323 /* Report Size */ 0x75, 0x01, /* 1 */
324 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
325 /* Report Count */ 0x95, 0x01, /* 1 */
326 /* Report Size */ 0x75, 0x03, /* 3 (padding bits) */
327 /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
328 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
329 /* Usage */ 0x09, 0x38, /* Z (wheel) */
330 /* Logical Minimum */ 0x15, 0x81, /* -127 */
331 /* Logical Maximum */ 0x25, 0x7F, /* +127 */
332 /* Report Size */ 0x75, 0x08, /* 8 */
333 /* Report Count */ 0x95, 0x01, /* 1 */
334 /* Input */ 0x81, 0x06, /* Data, Value, Relative, Bit field */
335 /* Report Count */ 0x95, 0x01, /* 1 (padding byte) */
336 /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
337 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
338 /* Usage */ 0x09, 0x30, /* X */
339 /* Usage */ 0x09, 0x31, /* Y */
340 /* Logical Minimum */ 0x15, 0x00, /* 0 */
341 /* Logical Maximum */ 0x26, 0xFF,0x7F,/* 0x7fff */
342 /* Physical Minimum */ 0x35, 0x00, /* 0 */
343 /* Physical Maximum */ 0x46, 0xFF,0x7F,/* 0x7fff */
344 /* Report Size */ 0x75, 0x10, /* 16 */
345 /* Report Count */ 0x95, 0x02, /* 2 */
346 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
347 /* End Collection */ 0xC0,
348 /* End Collection */ 0xC0,
349};
350
351static const uint8_t g_UsbHidMTReportDesc[] =
352{
353 /* Usage Page */ 0x05, 0x0D, /* Digitisers */
354 /* Usage */ 0x09, 0x04, /* Touch Screen */
355 /* Collection */ 0xA1, 0x01, /* Application */
356 /* Report ID */ 0x85, REPORTID_MTOUCH,
357 /* Usage */ 0x09, 0x22, /* Finger */
358 /* Collection */ 0xA1, 0x02, /* Logical */
359 /* Usage */ 0x09, 0x42, /* Tip Switch */
360 /* Logical Minimum */ 0x15, 0x00, /* 0 */
361 /* Logical Maximum */ 0x25, 0x01, /* 1 */
362 /* Report Size */ 0x75, 0x01, /* 1 */
363 /* Report Count */ 0x95, 0x01, /* 1 */
364 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
365 /* Usage */ 0x09, 0x32, /* In Range */
366 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
367 /* Report Count */ 0x95, 0x06, /* 6 */
368 /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
369 /* Report Size */ 0x75, 0x08, /* 8 */
370 /* Usage */ 0x09, 0x51, /* Contact Identifier */
371 /* Report Count */ 0x95, 0x01, /* 1 */
372 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
373 /* Collection */ 0xA1, 0x00, /* Physical */
374 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
375 /* Logical Maximum */ 0x26, 0xff,0x0f,/* 4095 */
376 /* Report Size */ 0x75, 0x10, /* 16 */
377 /* Unit Exponent */ 0x55, 0x0e, /* -2 */
378 /* Unit */ 0x65, 0x33, /* Inch,EngLinear */
379 /* Usage */ 0x09, 0x30, /* X */
380 /* Physical Minimum */ 0x35, 0x00, /* 0 */
381 /* Physical Maximum */ 0x46, 0xb5,0x04,/* 1205 */
382 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
383 /* Physical Maximum */ 0x46, 0x8a,0x03,/* 906 */
384 /* Usage */ 0x09, 0x31, /* Y */
385 /* Physical Maximum */ 0x45, 0x00, /* 0 (reset) */
386 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
387 /* End Collection */ 0xC0,
388 /* End Collection */ 0xC0,
389 /* Usage Page */ 0x05, 0x0d, /* Digitisers */
390 /* Usage */ 0x09, 0x22, /* Finger */
391 /* Collection */ 0xA1, 0x02, /* Logical */
392 /* Usage */ 0x09, 0x42, /* Tip Switch */
393 /* Logical Minimum */ 0x15, 0x00, /* 0 */
394 /* Logical Maximum */ 0x25, 0x01, /* 1 */
395 /* Report Size */ 0x75, 0x01, /* 1 */
396 /* Report Count */ 0x95, 0x01, /* 1 */
397 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
398 /* Usage */ 0x09, 0x32, /* In Range */
399 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
400 /* Report Count */ 0x95, 0x06, /* 6 */
401 /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
402 /* Report Size */ 0x75, 0x08, /* 8 */
403 /* Usage */ 0x09, 0x51, /* Contact Identifier */
404 /* Report Count */ 0x95, 0x01, /* 1 */
405 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
406 /* Collection */ 0xA1, 0x00, /* Physical */
407 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
408 /* Logical Maximum */ 0x26, 0xff,0x0f,/* 4095 */
409 /* Report Size */ 0x75, 0x10, /* 16 */
410 /* Unit Exponent */ 0x55, 0x0e, /* -2 */
411 /* Unit */ 0x65, 0x33, /* Inch,EngLinear */
412 /* Usage */ 0x09, 0x30, /* X */
413 /* Physical Minimum */ 0x35, 0x00, /* 0 */
414 /* Physical Maximum */ 0x46, 0xb5,0x04,/* 1205 */
415 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
416 /* Physical Maximum */ 0x46, 0x8a,0x03,/* 906 */
417 /* Usage */ 0x09, 0x31, /* Y */
418 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
419 /* End Collection */ 0xC0,
420 /* End Collection */ 0xC0,
421 /* Usage Page */ 0x05, 0x0d, /* Digitizers */
422 /* Usage */ 0x09, 0x54, /* Contact Count */
423 /* Report Count */ 0x95, 0x01, /* 1 */
424 /* Report Size */ 0x75, 0x08, /* 8 */
425 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
426 /* Report ID */ 0x85, REPORTID_MAX_COUNT,
427 /* Usage */ 0x09, 0x55, /* Contact Count Maximum */
428 /* Report Count */ 0x95, 0x01, /* 1 */
429 /* Logical Maximum */ 0x25, 0x02, /* 2 */
430 /* Feature */ 0xB1, 0x03, /* Constant, Value, Absolute, Bit field */
431 /* End Collection */ 0xC0,
432 /* Usage */ 0x09, 0x0E, /* Configuration */
433 /* Collection */ 0xA1, 0x01, /* Application */
434 /* Report ID */ 0x85, REPORTID_FEATURE,
435 /* Usage */ 0x09, 0x23, /* Device Settings */
436 /* Collection */ 0xA1, 0x02, /* logical */
437 /* Usage */ 0x09, 0x52, /* Input Mode */
438 /* Usage */ 0x09, 0x53, /* Device Index */
439 /* Logical Minimum */ 0x15, 0x00, /* 0 */
440 /* Logical Maximum */ 0x25, 0x0a, /* 10 */
441 /* Report Size */ 0x75, 0x08, /* 8 */
442 /* Report Count */ 0x95, 0x02, /* 2 */
443 /* Feature */ 0xB1, 0x02, /* Data, Value, Absolute, Bit field */
444 /* End Collection */ 0xC0,
445 /* End Collection */ 0xC0,
446 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
447 /* Usage */ 0x09, 0x02, /* Mouse */
448 /* Collection */ 0xA1, 0x01, /* Application */
449 /* Report ID */ 0x85, REPORTID_MOUSE,
450 /* Usage */ 0x09, 0x01, /* Pointer */
451 /* Collection */ 0xA1, 0x00, /* Physical */
452 /* Usage Page */ 0x05, 0x09, /* Button */
453 /* Usage Minimum */ 0x19, 0x01, /* Button 1 */
454 /* Usage Maximum */ 0x29, 0x02, /* Button 2 */
455 /* Logical Minimum */ 0x15, 0x00, /* 0 */
456 /* Logical Maximum */ 0x25, 0x01, /* 1 */
457 /* Report Size */ 0x75, 0x01, /* 1 */
458 /* Report Count */ 0x95, 0x02, /* 2 */
459 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
460 /* Report Count */ 0x95, 0x06, /* 6 */
461 /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
462 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
463 /* Usage */ 0x09, 0x30, /* X */
464 /* Usage */ 0x09, 0x31, /* Y */
465 /* Report Size */ 0x75, 0x10, /* 16 */
466 /* Report Count */ 0x95, 0x02, /* 2 */
467 /* Logical Minimum */ 0x15, 0x00, /* 0 */
468 /* Logical Maximum */ 0x26, 0xff,0x7f,/* 32767 */
469 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
470 /* End Collection */ 0xC0,
471 /* End Collection */ 0xC0
472};
473
474/* Additional HID class interface descriptor. */
475static const uint8_t g_UsbHidMIfHidDesc[] =
476{
477 /* .bLength = */ 0x09,
478 /* .bDescriptorType = */ 0x21, /* HID */
479 /* .bcdHID = */ 0x10, 0x01, /* 1.1 */
480 /* .bCountryCode = */ 0,
481 /* .bNumDescriptors = */ 1,
482 /* .bDescriptorType = */ 0x22, /* Report */
483 /* .wDescriptorLength = */ sizeof(g_UsbHidMReportDesc), 0x00
484};
485
486/* Additional HID class interface descriptor. */
487static const uint8_t g_UsbHidTIfHidDesc[] =
488{
489 /* .bLength = */ 0x09,
490 /* .bDescriptorType = */ 0x21, /* HID */
491 /* .bcdHID = */ 0x10, 0x01, /* 1.1 */
492 /* .bCountryCode = */ 0,
493 /* .bNumDescriptors = */ 1,
494 /* .bDescriptorType = */ 0x22, /* Report */
495 /* .wDescriptorLength = */ sizeof(g_UsbHidTReportDesc), 0x00
496};
497
498static const VUSBDESCINTERFACEEX g_UsbHidMInterfaceDesc =
499{
500 {
501 /* .bLength = */ sizeof(VUSBDESCINTERFACE),
502 /* .bDescriptorType = */ VUSB_DT_INTERFACE,
503 /* .bInterfaceNumber = */ 0,
504 /* .bAlternateSetting = */ 0,
505 /* .bNumEndpoints = */ 1,
506 /* .bInterfaceClass = */ 3 /* HID */,
507 /* .bInterfaceSubClass = */ 1 /* Boot Interface */,
508 /* .bInterfaceProtocol = */ 2 /* Mouse */,
509 /* .iInterface = */ 0
510 },
511 /* .pvMore = */ NULL,
512 /* .pvClass = */ &g_UsbHidMIfHidDesc,
513 /* .cbClass = */ sizeof(g_UsbHidMIfHidDesc),
514 &g_aUsbHidMEndpointDescs[0],
515 /* .pIAD = */ NULL,
516 /* .cbIAD = */ 0
517};
518
519static const VUSBDESCINTERFACEEX g_UsbHidTInterfaceDesc =
520{
521 {
522 /* .bLength = */ sizeof(VUSBDESCINTERFACE),
523 /* .bDescriptorType = */ VUSB_DT_INTERFACE,
524 /* .bInterfaceNumber = */ 0,
525 /* .bAlternateSetting = */ 0,
526 /* .bNumEndpoints = */ 1,
527 /* .bInterfaceClass = */ 3 /* HID */,
528 /* .bInterfaceSubClass = */ 0 /* No subclass - no boot interface. */,
529 /* .bInterfaceProtocol = */ 0 /* No protocol - no boot interface. */,
530 /* .iInterface = */ 0
531 },
532 /* .pvMore = */ NULL,
533 /* .pvClass = */ &g_UsbHidTIfHidDesc,
534 /* .cbClass = */ sizeof(g_UsbHidTIfHidDesc),
535 &g_aUsbHidTEndpointDescs[0],
536 /* .pIAD = */ NULL,
537 /* .cbIAD = */ 0
538};
539
540static const VUSBINTERFACE g_aUsbHidMInterfaces[] =
541{
542 { &g_UsbHidMInterfaceDesc, /* .cSettings = */ 1 },
543};
544
545static const VUSBINTERFACE g_aUsbHidTInterfaces[] =
546{
547 { &g_UsbHidTInterfaceDesc, /* .cSettings = */ 1 },
548};
549
550static const VUSBDESCCONFIGEX g_UsbHidMConfigDesc =
551{
552 {
553 /* .bLength = */ sizeof(VUSBDESCCONFIG),
554 /* .bDescriptorType = */ VUSB_DT_CONFIG,
555 /* .wTotalLength = */ 0 /* recalculated on read */,
556 /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidMInterfaces),
557 /* .bConfigurationValue =*/ 1,
558 /* .iConfiguration = */ 0,
559 /* .bmAttributes = */ RT_BIT(7),
560 /* .MaxPower = */ 50 /* 100mA */
561 },
562 NULL, /* pvMore */
563 &g_aUsbHidMInterfaces[0],
564 NULL /* pvOriginal */
565};
566
567static const VUSBDESCCONFIGEX g_UsbHidTConfigDesc =
568{
569 {
570 /* .bLength = */ sizeof(VUSBDESCCONFIG),
571 /* .bDescriptorType = */ VUSB_DT_CONFIG,
572 /* .wTotalLength = */ 0 /* recalculated on read */,
573 /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidTInterfaces),
574 /* .bConfigurationValue =*/ 1,
575 /* .iConfiguration = */ 0,
576 /* .bmAttributes = */ RT_BIT(7),
577 /* .MaxPower = */ 50 /* 100mA */
578 },
579 NULL, /* pvMore */
580 &g_aUsbHidTInterfaces[0],
581 NULL /* pvOriginal */
582};
583
584static const VUSBDESCDEVICE g_UsbHidMDeviceDesc =
585{
586 /* .bLength = */ sizeof(g_UsbHidMDeviceDesc),
587 /* .bDescriptorType = */ VUSB_DT_DEVICE,
588 /* .bcdUsb = */ 0x110, /* 1.1 */
589 /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
590 /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
591 /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
592 /* .bMaxPacketSize0 = */ 8,
593 /* .idVendor = */ VBOX_USB_VENDOR,
594 /* .idProduct = */ USBHID_PID_MOUSE,
595 /* .bcdDevice = */ 0x0100, /* 1.0 */
596 /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
597 /* .iProduct = */ USBHID_STR_ID_PRODUCT_M,
598 /* .iSerialNumber = */ 0,
599 /* .bNumConfigurations = */ 1
600};
601
602static const VUSBDESCDEVICE g_UsbHidTDeviceDesc =
603{
604 /* .bLength = */ sizeof(g_UsbHidTDeviceDesc),
605 /* .bDescriptorType = */ VUSB_DT_DEVICE,
606 /* .bcdUsb = */ 0x110, /* 1.1 */
607 /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
608 /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
609 /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
610 /* .bMaxPacketSize0 = */ 8,
611 /* .idVendor = */ VBOX_USB_VENDOR,
612 /* .idProduct = */ USBHID_PID_TABLET,
613 /* .bcdDevice = */ 0x0100, /* 1.0 */
614 /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
615 /* .iProduct = */ USBHID_STR_ID_PRODUCT_T,
616 /* .iSerialNumber = */ 0,
617 /* .bNumConfigurations = */ 1
618};
619
620static const PDMUSBDESCCACHE g_UsbHidMDescCache =
621{
622 /* .pDevice = */ &g_UsbHidMDeviceDesc,
623 /* .paConfigs = */ &g_UsbHidMConfigDesc,
624 /* .paLanguages = */ g_aUsbHidLanguages,
625 /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
626 /* .fUseCachedDescriptors = */ true,
627 /* .fUseCachedStringsDescriptors = */ true
628};
629
630static const PDMUSBDESCCACHE g_UsbHidTDescCache =
631{
632 /* .pDevice = */ &g_UsbHidTDeviceDesc,
633 /* .paConfigs = */ &g_UsbHidTConfigDesc,
634 /* .paLanguages = */ g_aUsbHidLanguages,
635 /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
636 /* .fUseCachedDescriptors = */ true,
637 /* .fUseCachedStringsDescriptors = */ true
638};
639
640
641/*******************************************************************************
642* Internal Functions *
643*******************************************************************************/
644
645/**
646 * Initializes an URB queue.
647 *
648 * @param pQueue The URB queue.
649 */
650static void usbHidQueueInit(PUSBHIDURBQUEUE pQueue)
651{
652 pQueue->pHead = NULL;
653 pQueue->ppTail = &pQueue->pHead;
654}
655
656
657
658/**
659 * Inserts an URB at the end of the queue.
660 *
661 * @param pQueue The URB queue.
662 * @param pUrb The URB to insert.
663 */
664DECLINLINE(void) usbHidQueueAddTail(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
665{
666 pUrb->Dev.pNext = NULL;
667 *pQueue->ppTail = pUrb;
668 pQueue->ppTail = &pUrb->Dev.pNext;
669}
670
671
672/**
673 * Unlinks the head of the queue and returns it.
674 *
675 * @returns The head entry.
676 * @param pQueue The URB queue.
677 */
678DECLINLINE(PVUSBURB) usbHidQueueRemoveHead(PUSBHIDURBQUEUE pQueue)
679{
680 PVUSBURB pUrb = pQueue->pHead;
681 if (pUrb)
682 {
683 PVUSBURB pNext = pUrb->Dev.pNext;
684 pQueue->pHead = pNext;
685 if (!pNext)
686 pQueue->ppTail = &pQueue->pHead;
687 else
688 pUrb->Dev.pNext = NULL;
689 }
690 return pUrb;
691}
692
693
694/**
695 * Removes an URB from anywhere in the queue.
696 *
697 * @returns true if found, false if not.
698 * @param pQueue The URB queue.
699 * @param pUrb The URB to remove.
700 */
701DECLINLINE(bool) usbHidQueueRemove(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
702{
703 PVUSBURB pCur = pQueue->pHead;
704 if (pCur == pUrb)
705 pQueue->pHead = pUrb->Dev.pNext;
706 else
707 {
708 while (pCur)
709 {
710 if (pCur->Dev.pNext == pUrb)
711 {
712 pCur->Dev.pNext = pUrb->Dev.pNext;
713 break;
714 }
715 pCur = pCur->Dev.pNext;
716 }
717 if (!pCur)
718 return false;
719 }
720 if (!pUrb->Dev.pNext)
721 pQueue->ppTail = &pQueue->pHead;
722 return true;
723}
724
725
726/**
727 * Checks if the queue is empty or not.
728 *
729 * @returns true if it is, false if it isn't.
730 * @param pQueue The URB queue.
731 */
732DECLINLINE(bool) usbHidQueueIsEmpty(PCUSBHIDURBQUEUE pQueue)
733{
734 return pQueue->pHead == NULL;
735}
736
737
738/**
739 * Links an URB into the done queue.
740 *
741 * @param pThis The HID instance.
742 * @param pUrb The URB.
743 */
744static void usbHidLinkDone(PUSBHID pThis, PVUSBURB pUrb)
745{
746 usbHidQueueAddTail(&pThis->DoneQueue, pUrb);
747
748 if (pThis->fHaveDoneQueueWaiter)
749 {
750 int rc = RTSemEventSignal(pThis->hEvtDoneQueue);
751 AssertRC(rc);
752 }
753}
754
755
756
757/**
758 * Completes the URB with a stalled state, halting the pipe.
759 */
760static int usbHidCompleteStall(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb, const char *pszWhy)
761{
762 Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
763
764 pUrb->enmStatus = VUSBSTATUS_STALL;
765
766 /** @todo figure out if the stall is global or pipe-specific or both. */
767 if (pEp)
768 pEp->fHalted = true;
769 else
770 {
771 pThis->aEps[0].fHalted = true;
772 pThis->aEps[1].fHalted = true;
773 }
774
775 usbHidLinkDone(pThis, pUrb);
776 return VINF_SUCCESS;
777}
778
779
780/**
781 * Completes the URB with a OK state.
782 */
783static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
784{
785 Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
786
787 pUrb->enmStatus = VUSBSTATUS_OK;
788 pUrb->cbData = (uint32_t)cbData;
789
790 usbHidLinkDone(pThis, pUrb);
791 return VINF_SUCCESS;
792}
793
794
795/**
796 * Reset worker for usbHidUsbReset, usbHidUsbSetConfiguration and
797 * usbHidHandleDefaultPipe.
798 *
799 * @returns VBox status code.
800 * @param pThis The HID instance.
801 * @param pUrb Set when usbHidHandleDefaultPipe is the
802 * caller.
803 * @param fSetConfig Set when usbHidUsbSetConfiguration is the
804 * caller.
805 */
806static int usbHidResetWorker(PUSBHID pThis, PVUSBURB pUrb, bool fSetConfig)
807{
808 /*
809 * Wait for the any command currently executing to complete before
810 * resetting. (We cannot cancel its execution.) How we do this depends
811 * on the reset method.
812 */
813
814 /*
815 * Reset the device state.
816 */
817 pThis->enmState = USBHIDREQSTATE_READY;
818 pThis->fHasPendingChanges = false;
819
820 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
821 pThis->aEps[i].fHalted = false;
822
823 if (!pUrb && !fSetConfig) /* (only device reset) */
824 pThis->bConfigurationValue = 0; /* default */
825
826 /*
827 * Ditch all pending URBs.
828 */
829 PVUSBURB pCurUrb;
830 while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
831 {
832 pCurUrb->enmStatus = VUSBSTATUS_CRC;
833 usbHidLinkDone(pThis, pCurUrb);
834 }
835
836 if (pUrb)
837 return usbHidCompleteOk(pThis, pUrb, 0);
838 return VINF_SUCCESS;
839}
840
841static int8_t clamp_i8(int32_t val)
842{
843 if (val > 127) {
844 val = 127;
845 } else if (val < -127) {
846 val = -127;
847 }
848 return val;
849}
850
851/**
852 * Create a USB HID report report based on the currently accumulated data.
853 */
854static size_t usbHidFillReport(PUSBHIDTM_REPORT pReport, PUSBHIDM_ACCUM pAccumulated, bool isAbsolute)
855{
856 size_t cbCopy;
857
858 if (isAbsolute)
859 {
860 pReport->t.rid = REPORTID_MOUSE;
861 pReport->t.btn = pAccumulated->btn;
862 pReport->t.cx = pAccumulated->dX;
863 pReport->t.cy = pAccumulated->dY;
864 pReport->t.dz = clamp_i8(pAccumulated->dZ);
865
866 cbCopy = sizeof(pReport->t);
867// LogRel(("Abs movement, X=%d, Y=%d, dZ=%d, btn=%02x, report size %d\n", pReport->t.cx, pReport->t.cy, pReport->t.dz, pReport->t.btn, cbCopy));
868 }
869 else
870 {
871 pReport->m.btn = pAccumulated->btn;
872 pReport->m.dx = clamp_i8(pAccumulated->dX);
873 pReport->m.dy = clamp_i8(pAccumulated->dY);
874 pReport->m.dz = clamp_i8(pAccumulated->dZ);
875
876 cbCopy = sizeof(pReport->m);
877// LogRel(("Rel movement, dX=%d, dY=%d, dZ=%d, btn=%02x, report size %d\n", pReport->m.dx, pReport->m.dy, pReport->m.dz, pReport->m.btn, cbCopy));
878 }
879
880 /* Clear the accumulated movement. */
881 RT_ZERO(*pAccumulated);
882
883 return cbCopy;
884}
885
886/**
887 * Sends a state report to the host if there is a pending URB.
888 */
889static int usbHidSendReport(PUSBHID pThis)
890{
891 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
892
893 if (pUrb)
894 {
895 PUSBHIDTM_REPORT pReport = (PUSBHIDTM_REPORT)&pUrb->abData[0];
896 size_t cbCopy;
897
898 cbCopy = usbHidFillReport(pReport, &pThis->PtrDelta, pThis->isAbsolute);
899 pThis->fHasPendingChanges = false;
900 return usbHidCompleteOk(pThis, pUrb, cbCopy);
901 }
902 else
903 {
904 Log2(("No available URB for USB mouse\n"));
905 pThis->fHasPendingChanges = true;
906 }
907 return VINF_EOF;
908}
909
910/**
911 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
912 */
913static DECLCALLBACK(void *) usbHidMouseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
914{
915 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
916 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
917 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Lun0.IPort);
918 return NULL;
919}
920
921/**
922 * Relative mouse event handler.
923 *
924 * @returns VBox status code.
925 * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
926 * @param i32DeltaX The X delta.
927 * @param i32DeltaY The Y delta.
928 * @param i32DeltaZ The Z delta.
929 * @param i32DeltaW The W delta.
930 * @param fButtonStates The button states.
931 */
932static DECLCALLBACK(int) usbHidMousePutEvent(PPDMIMOUSEPORT pInterface, int32_t i32DeltaX, int32_t i32DeltaY, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
933{
934 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
935 RTCritSectEnter(&pThis->CritSect);
936
937 /* Accumulate movement - the events from the front end may arrive
938 * at a much higher rate than USB can handle.
939 */
940 pThis->PtrDelta.btn = fButtonStates;
941 pThis->PtrDelta.dX += i32DeltaX;
942 pThis->PtrDelta.dY += i32DeltaY;
943 pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
944
945 /* Send a report if possible. */
946 usbHidSendReport(pThis);
947
948 RTCritSectLeave(&pThis->CritSect);
949 return VINF_SUCCESS;
950}
951
952/**
953 * Absolute mouse event handler.
954 *
955 * @returns VBox status code.
956 * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
957 * @param u32X The X coordinate.
958 * @param u32Y The Y coordinate.
959 * @param i32DeltaZ The Z delta.
960 * @param i32DeltaW The W delta.
961 * @param fButtonStates The button states.
962 */
963static DECLCALLBACK(int) usbHidMousePutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t u32X, uint32_t u32Y, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
964{
965 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
966 RTCritSectEnter(&pThis->CritSect);
967
968 Assert(pThis->isAbsolute);
969
970 /* Accumulate movement - the events from the front end may arrive
971 * at a much higher rate than USB can handle. Probably not a real issue
972 * when only the Z axis is relative (X/Y movement isn't technically
973 * accumulated and only the last value is used).
974 */
975 pThis->PtrDelta.btn = fButtonStates;
976 pThis->PtrDelta.dX = u32X >> pThis->u8CoordShift;
977 pThis->PtrDelta.dY = u32Y >> pThis->u8CoordShift;
978 pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
979
980 /* Send a report if possible. */
981 usbHidSendReport(pThis);
982
983 RTCritSectLeave(&pThis->CritSect);
984 return VINF_SUCCESS;
985}
986
987/**
988 * @copydoc PDMUSBREG::pfnUrbReap
989 */
990static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
991{
992 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
993 LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
994
995 RTCritSectEnter(&pThis->CritSect);
996
997 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
998 if (!pUrb && cMillies)
999 {
1000 /* Wait */
1001 pThis->fHaveDoneQueueWaiter = true;
1002 RTCritSectLeave(&pThis->CritSect);
1003
1004 RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
1005
1006 RTCritSectEnter(&pThis->CritSect);
1007 pThis->fHaveDoneQueueWaiter = false;
1008
1009 pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
1010 }
1011
1012 RTCritSectLeave(&pThis->CritSect);
1013
1014 if (pUrb)
1015 Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
1016 return pUrb;
1017}
1018
1019
1020/**
1021 * @copydoc PDMUSBREG::pfnUrbCancel
1022 */
1023static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
1024{
1025 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1026 LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
1027 RTCritSectEnter(&pThis->CritSect);
1028
1029 /*
1030 * Remove the URB from the to-host queue and move it onto the done queue.
1031 */
1032 if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
1033 usbHidLinkDone(pThis, pUrb);
1034
1035 RTCritSectLeave(&pThis->CritSect);
1036 return VINF_SUCCESS;
1037}
1038
1039
1040/**
1041 * Handles request sent to the inbound (device to host) interrupt pipe. This is
1042 * rather different from bulk requests because an interrupt read URB may complete
1043 * after arbitrarily long time.
1044 */
1045static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
1046{
1047 /*
1048 * Stall the request if the pipe is halted.
1049 */
1050 if (RT_UNLIKELY(pEp->fHalted))
1051 return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
1052
1053 /*
1054 * Deal with the URB according to the state.
1055 */
1056 switch (pThis->enmState)
1057 {
1058 /*
1059 * We've data left to transfer to the host.
1060 */
1061 case USBHIDREQSTATE_DATA_TO_HOST:
1062 {
1063 AssertFailed();
1064 Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
1065 return usbHidCompleteOk(pThis, pUrb, 0);
1066 }
1067
1068 /*
1069 * Status transfer.
1070 */
1071 case USBHIDREQSTATE_STATUS:
1072 {
1073 AssertFailed();
1074 Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
1075 pThis->enmState = USBHIDREQSTATE_READY;
1076 return usbHidCompleteOk(pThis, pUrb, 0);
1077 }
1078
1079 case USBHIDREQSTATE_READY:
1080 usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
1081 /* If a report is pending, send it right away. */
1082 if (pThis->fHasPendingChanges)
1083 usbHidSendReport(pThis);
1084 LogFlow(("usbHidHandleIntrDevToHost: Added %p:%s to the queue\n", pUrb, pUrb->pszDesc));
1085 return VINF_SUCCESS;
1086
1087 /*
1088 * Bad states, stall.
1089 */
1090 default:
1091 Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
1092 return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
1093 }
1094}
1095
1096
1097/**
1098 * Handles request sent to the default control pipe.
1099 */
1100static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
1101{
1102 PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
1103 AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
1104
1105 if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
1106 {
1107 switch (pSetup->bRequest)
1108 {
1109 case VUSB_REQ_GET_DESCRIPTOR:
1110 {
1111 switch (pSetup->bmRequestType)
1112 {
1113 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1114 {
1115 switch (pSetup->wValue >> 8)
1116 {
1117 case VUSB_DT_STRING:
1118 Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1119 break;
1120 default:
1121 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1122 break;
1123 }
1124 break;
1125 }
1126
1127 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1128 {
1129 switch (pSetup->wValue >> 8)
1130 {
1131 uint32_t cbCopy;
1132 uint32_t cbDesc;
1133 const uint8_t *pDesc;
1134
1135 case DT_IF_HID_DESCRIPTOR:
1136 {
1137 if (pThis->isAbsolute)
1138 {
1139 cbDesc = sizeof(g_UsbHidTIfHidDesc);
1140 pDesc = (const uint8_t *)&g_UsbHidTIfHidDesc;
1141 }
1142 else
1143 {
1144 cbDesc = sizeof(g_UsbHidMIfHidDesc);
1145 pDesc = (const uint8_t *)&g_UsbHidMIfHidDesc;
1146 }
1147 /* Returned data is written after the setup message. */
1148 cbCopy = pUrb->cbData - sizeof(*pSetup);
1149 cbCopy = RT_MIN(cbCopy, cbDesc);
1150 Log(("usbHidMouse: GET_DESCRIPTOR DT_IF_HID_DESCRIPTOR wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
1151 memcpy(&pUrb->abData[sizeof(*pSetup)], pDesc, cbCopy);
1152 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
1153 }
1154
1155 case DT_IF_HID_REPORT:
1156 {
1157 if (pThis->isAbsolute)
1158 {
1159 cbDesc = sizeof(g_UsbHidTReportDesc);
1160 pDesc = (const uint8_t *)&g_UsbHidTReportDesc;
1161 }
1162 else
1163 {
1164 cbDesc = sizeof(g_UsbHidMReportDesc);
1165 pDesc = (const uint8_t *)&g_UsbHidMReportDesc;
1166 }
1167 /* Returned data is written after the setup message. */
1168 cbCopy = pUrb->cbData - sizeof(*pSetup);
1169 cbCopy = RT_MIN(cbCopy, cbDesc);
1170 Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
1171 memcpy(&pUrb->abData[sizeof(*pSetup)], pDesc, cbCopy);
1172 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
1173 }
1174
1175 default:
1176 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1177 break;
1178 }
1179 break;
1180 }
1181
1182 default:
1183 Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
1184 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
1185 }
1186 break;
1187 }
1188
1189 case VUSB_REQ_GET_STATUS:
1190 {
1191 uint16_t wRet = 0;
1192
1193 if (pSetup->wLength != 2)
1194 {
1195 Log(("usbHid: Bad GET_STATUS req: wLength=%#x\n", pSetup->wLength));
1196 break;
1197 }
1198 Assert(pSetup->wValue == 0);
1199 switch (pSetup->bmRequestType)
1200 {
1201 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1202 {
1203 Assert(pSetup->wIndex == 0);
1204 Log(("usbHid: GET_STATUS (device)\n"));
1205 wRet = 0; /* Not self-powered, no remote wakeup. */
1206 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1207 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1208 }
1209
1210 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1211 {
1212 if (pSetup->wIndex == 0)
1213 {
1214 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1215 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1216 }
1217 else
1218 {
1219 Log(("usbHid: GET_STATUS (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
1220 }
1221 break;
1222 }
1223
1224 case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1225 {
1226 if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
1227 {
1228 wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
1229 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1230 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1231 }
1232 else
1233 {
1234 Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
1235 }
1236 break;
1237 }
1238
1239 default:
1240 Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
1241 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
1242 }
1243 break;
1244 }
1245
1246 case VUSB_REQ_CLEAR_FEATURE:
1247 break;
1248 }
1249
1250 /** @todo implement this. */
1251 Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1252 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1253
1254 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
1255 }
1256 /* 3.1 Bulk-Only Mass Storage Reset */
1257 else if ( pSetup->bmRequestType == (VUSB_REQ_CLASS | VUSB_TO_INTERFACE)
1258 && pSetup->bRequest == 0xff
1259 && !pSetup->wValue
1260 && !pSetup->wLength
1261 && pSetup->wIndex == 0)
1262 {
1263 Log(("usbHidHandleDefaultPipe: Bulk-Only Mass Storage Reset\n"));
1264 return usbHidResetWorker(pThis, pUrb, false /*fSetConfig*/);
1265 }
1266 else
1267 {
1268 Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1269 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1270 return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
1271 }
1272
1273 return VINF_SUCCESS;
1274}
1275
1276
1277/**
1278 * @copydoc PDMUSBREG::pfnUrbQueue
1279 */
1280static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
1281{
1282 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1283 LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
1284 RTCritSectEnter(&pThis->CritSect);
1285
1286 /*
1287 * Parse on a per end-point basis.
1288 */
1289 int rc;
1290 switch (pUrb->EndPt)
1291 {
1292 case 0:
1293 rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
1294 break;
1295
1296 case 0x81:
1297 AssertFailed();
1298 case 0x01:
1299 rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
1300 break;
1301
1302 default:
1303 AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
1304 rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
1305 break;
1306 }
1307
1308 RTCritSectLeave(&pThis->CritSect);
1309 return rc;
1310}
1311
1312
1313/**
1314 * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
1315 */
1316static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
1317{
1318 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1319 LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
1320
1321 if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
1322 {
1323 RTCritSectEnter(&pThis->CritSect);
1324 pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
1325 RTCritSectLeave(&pThis->CritSect);
1326 }
1327
1328 return VINF_SUCCESS;
1329}
1330
1331
1332/**
1333 * @copydoc PDMUSBREG::pfnUsbSetInterface
1334 */
1335static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
1336{
1337 LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
1338 Assert(bAlternateSetting == 0);
1339 return VINF_SUCCESS;
1340}
1341
1342
1343/**
1344 * @copydoc PDMUSBREG::pfnUsbSetConfiguration
1345 */
1346static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
1347 const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
1348{
1349 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1350 LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
1351 Assert(bConfigurationValue == 1);
1352 RTCritSectEnter(&pThis->CritSect);
1353
1354 /*
1355 * If the same config is applied more than once, it's a kind of reset.
1356 */
1357 if (pThis->bConfigurationValue == bConfigurationValue)
1358 usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
1359 pThis->bConfigurationValue = bConfigurationValue;
1360
1361 /*
1362 * Set received event type to absolute or relative.
1363 */
1364 pThis->Lun0.pDrv->pfnReportModes(pThis->Lun0.pDrv, !pThis->isAbsolute,
1365 pThis->isAbsolute);
1366
1367 RTCritSectLeave(&pThis->CritSect);
1368 return VINF_SUCCESS;
1369}
1370
1371
1372/**
1373 * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
1374 */
1375static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
1376{
1377 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1378 LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
1379 if (pThis->isAbsolute) {
1380 return &g_UsbHidTDescCache;
1381 } else {
1382 return &g_UsbHidMDescCache;
1383 }
1384}
1385
1386
1387/**
1388 * @copydoc PDMUSBREG::pfnUsbReset
1389 */
1390static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
1391{
1392 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1393 LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
1394 RTCritSectEnter(&pThis->CritSect);
1395
1396 int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
1397
1398 RTCritSectLeave(&pThis->CritSect);
1399 return rc;
1400}
1401
1402
1403/**
1404 * @copydoc PDMUSBREG::pfnDestruct
1405 */
1406static void usbHidDestruct(PPDMUSBINS pUsbIns)
1407{
1408 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1409 LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
1410
1411 if (RTCritSectIsInitialized(&pThis->CritSect))
1412 {
1413 RTCritSectEnter(&pThis->CritSect);
1414 RTCritSectLeave(&pThis->CritSect);
1415 RTCritSectDelete(&pThis->CritSect);
1416 }
1417
1418 if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
1419 {
1420 RTSemEventDestroy(pThis->hEvtDoneQueue);
1421 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1422 }
1423}
1424
1425
1426/**
1427 * @copydoc PDMUSBREG::pfnConstruct
1428 */
1429static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
1430{
1431 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1432 Log(("usbHidConstruct/#%u:\n", iInstance));
1433
1434 /*
1435 * Perform the basic structure initialization first so the destructor
1436 * will not misbehave.
1437 */
1438 pThis->pUsbIns = pUsbIns;
1439 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1440 usbHidQueueInit(&pThis->ToHostQueue);
1441 usbHidQueueInit(&pThis->DoneQueue);
1442
1443 int rc = RTCritSectInit(&pThis->CritSect);
1444 AssertRCReturn(rc, rc);
1445
1446 rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
1447 AssertRCReturn(rc, rc);
1448
1449 /*
1450 * Validate and read the configuration.
1451 */
1452 rc = CFGMR3ValidateConfig(pCfg, "/", "Absolute|CoordShift", "Config", "UsbHid", iInstance);
1453 if (RT_FAILURE(rc))
1454 return rc;
1455 rc = CFGMR3QueryBoolDef(pCfg, "Absolute", &pThis->isAbsolute, false);
1456 if (RT_FAILURE(rc))
1457 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query settings"));
1458
1459 pThis->Lun0.IBase.pfnQueryInterface = usbHidMouseQueryInterface;
1460 pThis->Lun0.IPort.pfnPutEvent = usbHidMousePutEvent;
1461 pThis->Lun0.IPort.pfnPutEventAbs = usbHidMousePutEventAbs;
1462
1463 /*
1464 * Attach the mouse driver.
1465 */
1466 rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Mouse Port");
1467 if (RT_FAILURE(rc))
1468 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach mouse driver"));
1469
1470 pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIMOUSECONNECTOR);
1471 if (!pThis->Lun0.pDrv)
1472 return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query mouse interface"));
1473
1474 rc = CFGMR3QueryU8Def(pCfg, "CoordShift", &pThis->u8CoordShift, 1);
1475 if (RT_FAILURE(rc))
1476 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query shift factor"));
1477
1478 return VINF_SUCCESS;
1479}
1480
1481
1482/**
1483 * The USB Human Interface Device (HID) Mouse registration record.
1484 */
1485const PDMUSBREG g_UsbHidMou =
1486{
1487 /* u32Version */
1488 PDM_USBREG_VERSION,
1489 /* szName */
1490 "HidMouse",
1491 /* pszDescription */
1492 "USB HID Mouse.",
1493 /* fFlags */
1494 0,
1495 /* cMaxInstances */
1496 ~0U,
1497 /* cbInstance */
1498 sizeof(USBHID),
1499 /* pfnConstruct */
1500 usbHidConstruct,
1501 /* pfnDestruct */
1502 usbHidDestruct,
1503 /* pfnVMInitComplete */
1504 NULL,
1505 /* pfnVMPowerOn */
1506 NULL,
1507 /* pfnVMReset */
1508 NULL,
1509 /* pfnVMSuspend */
1510 NULL,
1511 /* pfnVMResume */
1512 NULL,
1513 /* pfnVMPowerOff */
1514 NULL,
1515 /* pfnHotPlugged */
1516 NULL,
1517 /* pfnHotUnplugged */
1518 NULL,
1519 /* pfnDriverAttach */
1520 NULL,
1521 /* pfnDriverDetach */
1522 NULL,
1523 /* pfnQueryInterface */
1524 NULL,
1525 /* pfnUsbReset */
1526 usbHidUsbReset,
1527 /* pfnUsbGetDescriptorCache */
1528 usbHidUsbGetDescriptorCache,
1529 /* pfnUsbSetConfiguration */
1530 usbHidUsbSetConfiguration,
1531 /* pfnUsbSetInterface */
1532 usbHidUsbSetInterface,
1533 /* pfnUsbClearHaltedEndpoint */
1534 usbHidUsbClearHaltedEndpoint,
1535 /* pfnUrbNew */
1536 NULL/*usbHidUrbNew*/,
1537 /* pfnUrbQueue */
1538 usbHidQueue,
1539 /* pfnUrbCancel */
1540 usbHidUrbCancel,
1541 /* pfnUrbReap */
1542 usbHidUrbReap,
1543 /* u32TheEnd */
1544 PDM_USBREG_VERSION
1545};
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette