VirtualBox

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

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

Devices/Input: machine configuration for multi-touch device.

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