VirtualBox

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

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

Devices/Input/UsbMouse: add multi-touch mode to pfnReportModes in PDM.

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