VirtualBox

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

Last change on this file since 46923 was 46923, checked in by vboxsync, 11 years ago

Devices/Input: add USB multi-touch device, not yet active.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.4 KB
Line 
1/** @file
2 * UsbMouse - USB Human Interface Device Emulation (Mouse).
3 */
4
5/*
6 * Copyright (C) 2007-2012 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17/*******************************************************************************
18* Header Files *
19*******************************************************************************/
20#define LOG_GROUP LOG_GROUP_USB_MOUSE
21#include <VBox/vmm/pdmusb.h>
22#include <VBox/log.h>
23#include <VBox/err.h>
24#include <iprt/assert.h>
25#include <iprt/critsect.h>
26#include <iprt/mem.h>
27#include <iprt/semaphore.h>
28#include <iprt/string.h>
29#include <iprt/uuid.h>
30#include "VBoxDD.h"
31
32
33/*******************************************************************************
34* Defined Constants And Macros *
35*******************************************************************************/
36/** @name USB HID string IDs
37 * @{ */
38#define USBHID_STR_ID_MANUFACTURER 1
39#define USBHID_STR_ID_PRODUCT_M 2
40#define USBHID_STR_ID_PRODUCT_T 3
41/** @} */
42
43/** @name USB HID specific descriptor types
44 * @{ */
45#define DT_IF_HID_DESCRIPTOR 0x21
46#define DT_IF_HID_REPORT 0x22
47/** @} */
48
49/** @name USB HID vendor and product IDs
50 * @{ */
51#define VBOX_USB_VENDOR 0x80EE
52#define USBHID_PID_MOUSE 0x0020
53#define USBHID_PID_TABLET 0x0021
54/** @} */
55
56/*******************************************************************************
57* Structures and Typedefs *
58*******************************************************************************/
59
60/**
61 * The USB HID request state.
62 */
63typedef enum USBHIDREQSTATE
64{
65 /** Invalid status. */
66 USBHIDREQSTATE_INVALID = 0,
67 /** Ready to receive a new read request. */
68 USBHIDREQSTATE_READY,
69 /** Have (more) data for the host. */
70 USBHIDREQSTATE_DATA_TO_HOST,
71 /** Waiting to supply status information to the host. */
72 USBHIDREQSTATE_STATUS,
73 /** The end of the valid states. */
74 USBHIDREQSTATE_END
75} USBHIDREQSTATE;
76
77/**
78 * 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 Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
791
792 pUrb->enmStatus = VUSBSTATUS_STALL;
793
794 /** @todo figure out if the stall is global or pipe-specific or both. */
795 if (pEp)
796 pEp->fHalted = true;
797 else
798 {
799 pThis->aEps[0].fHalted = true;
800 pThis->aEps[1].fHalted = true;
801 }
802
803 usbHidLinkDone(pThis, pUrb);
804 return VINF_SUCCESS;
805}
806
807
808/**
809 * Completes the URB with a OK state.
810 */
811static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
812{
813 Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
814
815 pUrb->enmStatus = VUSBSTATUS_OK;
816 pUrb->cbData = (uint32_t)cbData;
817
818 usbHidLinkDone(pThis, pUrb);
819 return VINF_SUCCESS;
820}
821
822
823/**
824 * Reset worker for usbHidUsbReset, usbHidUsbSetConfiguration and
825 * usbHidHandleDefaultPipe.
826 *
827 * @returns VBox status code.
828 * @param pThis The HID instance.
829 * @param pUrb Set when usbHidHandleDefaultPipe is the
830 * caller.
831 * @param fSetConfig Set when usbHidUsbSetConfiguration is the
832 * caller.
833 */
834static int usbHidResetWorker(PUSBHID pThis, PVUSBURB pUrb, bool fSetConfig)
835{
836 /*
837 * Wait for the any command currently executing to complete before
838 * resetting. (We cannot cancel its execution.) How we do this depends
839 * on the reset method.
840 */
841
842 /*
843 * Reset the device state.
844 */
845 pThis->enmState = USBHIDREQSTATE_READY;
846 pThis->fHasPendingChanges = false;
847
848 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
849 pThis->aEps[i].fHalted = false;
850
851 if (!pUrb && !fSetConfig) /* (only device reset) */
852 pThis->bConfigurationValue = 0; /* default */
853
854 /*
855 * Ditch all pending URBs.
856 */
857 PVUSBURB pCurUrb;
858 while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
859 {
860 pCurUrb->enmStatus = VUSBSTATUS_CRC;
861 usbHidLinkDone(pThis, pCurUrb);
862 }
863
864 if (pUrb)
865 return usbHidCompleteOk(pThis, pUrb, 0);
866 return VINF_SUCCESS;
867}
868
869static int8_t clamp_i8(int32_t val)
870{
871 if (val > 127) {
872 val = 127;
873 } else if (val < -127) {
874 val = -127;
875 }
876 return val;
877}
878
879/**
880 * Create a USB HID report report based on the currently accumulated data.
881 */
882static size_t usbHidFillReport(PUSBHIDTM_REPORT pReport,
883 PUSBHIDM_ACCUM pAccumulated, USBHIDMODE enmMode)
884{
885 size_t cbCopy;
886
887 switch (enmMode)
888 {
889 case USBHIDMODE_ABSOLUTE:
890 {
891 pReport->t.rid = REPORTID_MOUSE;
892 pReport->t.btn = pAccumulated->btn;
893 pReport->t.cx = pAccumulated->dX;
894 pReport->t.cy = pAccumulated->dY;
895 pReport->t.dz = clamp_i8(pAccumulated->dZ);
896
897 cbCopy = sizeof(pReport->t);
898// LogRel(("Abs movement, X=%d, Y=%d, dZ=%d, btn=%02x, report size %d\n", pReport->t.cx, pReport->t.cy, pReport->t.dz, pReport->t.btn, cbCopy));
899 break;
900 }
901 case USBHIDMODE_RELATIVE:
902 {
903 pReport->m.btn = pAccumulated->btn;
904 pReport->m.dx = clamp_i8(pAccumulated->dX);
905 pReport->m.dy = clamp_i8(pAccumulated->dY);
906 pReport->m.dz = clamp_i8(pAccumulated->dZ);
907
908 cbCopy = sizeof(pReport->m);
909// LogRel(("Rel movement, dX=%d, dY=%d, dZ=%d, btn=%02x, report size %d\n", pReport->m.dx, pReport->m.dy, pReport->m.dz, pReport->m.btn, cbCopy));
910 break;
911 }
912 case USBHIDMODE_MULTI_TOUCH:
913 {
914 pReport->mt.idReport = REPORTID_MOUSE;
915 pReport->mt.idContact = 1;
916 pReport->mt.x = pAccumulated->dX;
917 pReport->mt.y = pAccumulated->dY;
918 pReport->mt.fButton = 0x1; /* We only send events when there
919 * is contact. */
920
921 cbCopy = sizeof(pReport->t);
922 LogRel3(("Multi-touch event, X=%d, Y=%d, report size %d\n",
923 pAccumulated->dX, pAccumulated->dY, cbCopy));
924 break;
925 }
926 }
927
928 /* Clear the accumulated movement. */
929 RT_ZERO(*pAccumulated);
930
931 return cbCopy;
932}
933
934/**
935 * Sends a state report to the host if there is a pending URB.
936 */
937static int usbHidSendReport(PUSBHID pThis)
938{
939 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
940
941 if (pUrb)
942 {
943 PUSBHIDTM_REPORT pReport = (PUSBHIDTM_REPORT)&pUrb->abData[0];
944 size_t cbCopy;
945
946 cbCopy = usbHidFillReport(pReport, &pThis->PtrDelta, pThis->enmMode);
947 pThis->fHasPendingChanges = false;
948 return usbHidCompleteOk(pThis, pUrb, cbCopy);
949 }
950 else
951 {
952 Log2(("No available URB for USB mouse\n"));
953 pThis->fHasPendingChanges = true;
954 }
955 return VINF_EOF;
956}
957
958/**
959 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
960 */
961static DECLCALLBACK(void *) usbHidMouseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
962{
963 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
964 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
965 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Lun0.IPort);
966 return NULL;
967}
968
969/**
970 * Relative mouse event handler.
971 *
972 * @returns VBox status code.
973 * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
974 * @param i32DeltaX The X delta.
975 * @param i32DeltaY The Y delta.
976 * @param i32DeltaZ The Z delta.
977 * @param i32DeltaW The W delta.
978 * @param fButtonStates The button states.
979 */
980static DECLCALLBACK(int) usbHidMousePutEvent(PPDMIMOUSEPORT pInterface, int32_t i32DeltaX, int32_t i32DeltaY, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
981{
982 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
983 RTCritSectEnter(&pThis->CritSect);
984
985 /* Accumulate movement - the events from the front end may arrive
986 * at a much higher rate than USB can handle.
987 */
988 pThis->PtrDelta.btn = fButtonStates;
989 pThis->PtrDelta.dX += i32DeltaX;
990 pThis->PtrDelta.dY += i32DeltaY;
991 pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
992
993 /* Send a report if possible. */
994 usbHidSendReport(pThis);
995
996 RTCritSectLeave(&pThis->CritSect);
997 return VINF_SUCCESS;
998}
999
1000/**
1001 * Absolute mouse event handler.
1002 *
1003 * @returns VBox status code.
1004 * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
1005 * @param u32X The X coordinate.
1006 * @param u32Y The Y coordinate.
1007 * @param i32DeltaZ The Z delta.
1008 * @param i32DeltaW The W delta.
1009 * @param fButtonStates The button states.
1010 */
1011static DECLCALLBACK(int) usbHidMousePutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t u32X, uint32_t u32Y, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
1012{
1013 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
1014 RTCritSectEnter(&pThis->CritSect);
1015
1016 Assert(pThis->enmMode == USBHIDMODE_ABSOLUTE);
1017
1018 /* Accumulate movement - the events from the front end may arrive
1019 * at a much higher rate than USB can handle. Probably not a real issue
1020 * when only the Z axis is relative (X/Y movement isn't technically
1021 * accumulated and only the last value is used).
1022 */
1023 pThis->PtrDelta.btn = fButtonStates;
1024 pThis->PtrDelta.dX = u32X >> pThis->u8CoordShift;
1025 pThis->PtrDelta.dY = u32Y >> pThis->u8CoordShift;
1026 pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
1027
1028 /* Send a report if possible. */
1029 usbHidSendReport(pThis);
1030
1031 RTCritSectLeave(&pThis->CritSect);
1032 return VINF_SUCCESS;
1033}
1034
1035/**
1036 * @copydoc PDMUSBREG::pfnUrbReap
1037 */
1038static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
1039{
1040 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1041 LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
1042
1043 RTCritSectEnter(&pThis->CritSect);
1044
1045 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
1046 if (!pUrb && cMillies)
1047 {
1048 /* Wait */
1049 pThis->fHaveDoneQueueWaiter = true;
1050 RTCritSectLeave(&pThis->CritSect);
1051
1052 RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
1053
1054 RTCritSectEnter(&pThis->CritSect);
1055 pThis->fHaveDoneQueueWaiter = false;
1056
1057 pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
1058 }
1059
1060 RTCritSectLeave(&pThis->CritSect);
1061
1062 if (pUrb)
1063 Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
1064 return pUrb;
1065}
1066
1067
1068/**
1069 * @copydoc PDMUSBREG::pfnUrbCancel
1070 */
1071static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
1072{
1073 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1074 LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
1075 RTCritSectEnter(&pThis->CritSect);
1076
1077 /*
1078 * Remove the URB from the to-host queue and move it onto the done queue.
1079 */
1080 if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
1081 usbHidLinkDone(pThis, pUrb);
1082
1083 RTCritSectLeave(&pThis->CritSect);
1084 return VINF_SUCCESS;
1085}
1086
1087
1088/**
1089 * Handles request sent to the inbound (device to host) interrupt pipe. This is
1090 * rather different from bulk requests because an interrupt read URB may complete
1091 * after arbitrarily long time.
1092 */
1093static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
1094{
1095 /*
1096 * Stall the request if the pipe is halted.
1097 */
1098 if (RT_UNLIKELY(pEp->fHalted))
1099 return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
1100
1101 /*
1102 * Deal with the URB according to the state.
1103 */
1104 switch (pThis->enmState)
1105 {
1106 /*
1107 * We've data left to transfer to the host.
1108 */
1109 case USBHIDREQSTATE_DATA_TO_HOST:
1110 {
1111 AssertFailed();
1112 Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
1113 return usbHidCompleteOk(pThis, pUrb, 0);
1114 }
1115
1116 /*
1117 * Status transfer.
1118 */
1119 case USBHIDREQSTATE_STATUS:
1120 {
1121 AssertFailed();
1122 Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
1123 pThis->enmState = USBHIDREQSTATE_READY;
1124 return usbHidCompleteOk(pThis, pUrb, 0);
1125 }
1126
1127 case USBHIDREQSTATE_READY:
1128 usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
1129 /* If a report is pending, send it right away. */
1130 if (pThis->fHasPendingChanges)
1131 usbHidSendReport(pThis);
1132 LogFlow(("usbHidHandleIntrDevToHost: Added %p:%s to the queue\n", pUrb, pUrb->pszDesc));
1133 return VINF_SUCCESS;
1134
1135 /*
1136 * Bad states, stall.
1137 */
1138 default:
1139 Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
1140 return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
1141 }
1142}
1143
1144
1145/**
1146 * Handles request sent to the default control pipe.
1147 */
1148static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
1149{
1150 PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
1151 AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
1152
1153 if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
1154 {
1155 switch (pSetup->bRequest)
1156 {
1157 case VUSB_REQ_GET_DESCRIPTOR:
1158 {
1159 switch (pSetup->bmRequestType)
1160 {
1161 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1162 {
1163 switch (pSetup->wValue >> 8)
1164 {
1165 case VUSB_DT_STRING:
1166 Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1167 break;
1168 default:
1169 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1170 break;
1171 }
1172 break;
1173 }
1174
1175 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1176 {
1177 switch (pSetup->wValue >> 8)
1178 {
1179 uint32_t cbCopy;
1180 uint32_t cbDesc;
1181 const uint8_t *pDesc;
1182
1183 case DT_IF_HID_DESCRIPTOR:
1184 {
1185 switch (pThis->enmMode)
1186 {
1187 case USBHIDMODE_ABSOLUTE:
1188 {
1189 cbDesc = sizeof(g_UsbHidTIfHidDesc);
1190 pDesc = (const uint8_t *)&g_UsbHidTIfHidDesc;
1191 break;
1192 }
1193 case USBHIDMODE_RELATIVE:
1194 {
1195 cbDesc = sizeof(g_UsbHidMIfHidDesc);
1196 pDesc = (const uint8_t *)&g_UsbHidMIfHidDesc;
1197 break;
1198 }
1199 case USBHIDMODE_MULTI_TOUCH:
1200 {
1201 cbDesc = sizeof(g_UsbHidMTIfHidDesc);
1202 pDesc = (const uint8_t *)&g_UsbHidMTIfHidDesc;
1203 break;
1204 }
1205 }
1206 /* Returned data is written after the setup message. */
1207 cbCopy = pUrb->cbData - sizeof(*pSetup);
1208 cbCopy = RT_MIN(cbCopy, cbDesc);
1209 Log(("usbHidMouse: GET_DESCRIPTOR DT_IF_HID_DESCRIPTOR wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
1210 memcpy(&pUrb->abData[sizeof(*pSetup)], pDesc, cbCopy);
1211 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
1212 }
1213
1214 case DT_IF_HID_REPORT:
1215 {
1216 switch (pThis->enmMode)
1217 {
1218 case USBHIDMODE_ABSOLUTE:
1219 {
1220 cbDesc = sizeof(g_UsbHidTReportDesc);
1221 pDesc = (const uint8_t *)&g_UsbHidTReportDesc;
1222 break;
1223 }
1224 case USBHIDMODE_RELATIVE:
1225 {
1226 cbDesc = sizeof(g_UsbHidMReportDesc);
1227 pDesc = (const uint8_t *)&g_UsbHidMReportDesc;
1228 break;
1229 }
1230 case USBHIDMODE_MULTI_TOUCH:
1231 {
1232 cbDesc = sizeof(g_UsbHidMTReportDesc);
1233 pDesc = (const uint8_t *)&g_UsbHidMTReportDesc;
1234 break;
1235 }
1236 }
1237 /* Returned data is written after the setup message. */
1238 cbCopy = pUrb->cbData - sizeof(*pSetup);
1239 cbCopy = RT_MIN(cbCopy, cbDesc);
1240 Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
1241 memcpy(&pUrb->abData[sizeof(*pSetup)], pDesc, cbCopy);
1242 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
1243 }
1244
1245 default:
1246 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1247 break;
1248 }
1249 break;
1250 }
1251
1252 default:
1253 Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
1254 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
1255 }
1256 break;
1257 }
1258
1259 case VUSB_REQ_GET_STATUS:
1260 {
1261 uint16_t wRet = 0;
1262
1263 if (pSetup->wLength != 2)
1264 {
1265 Log(("usbHid: Bad GET_STATUS req: wLength=%#x\n", pSetup->wLength));
1266 break;
1267 }
1268 Assert(pSetup->wValue == 0);
1269 switch (pSetup->bmRequestType)
1270 {
1271 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1272 {
1273 Assert(pSetup->wIndex == 0);
1274 Log(("usbHid: GET_STATUS (device)\n"));
1275 wRet = 0; /* Not self-powered, no remote wakeup. */
1276 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1277 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1278 }
1279
1280 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1281 {
1282 if (pSetup->wIndex == 0)
1283 {
1284 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1285 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1286 }
1287 else
1288 {
1289 Log(("usbHid: GET_STATUS (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
1290 }
1291 break;
1292 }
1293
1294 case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1295 {
1296 if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
1297 {
1298 wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
1299 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1300 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1301 }
1302 else
1303 {
1304 Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
1305 }
1306 break;
1307 }
1308
1309 default:
1310 Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
1311 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
1312 }
1313 break;
1314 }
1315
1316 case VUSB_REQ_CLEAR_FEATURE:
1317 break;
1318 }
1319
1320 /** @todo implement this. */
1321 Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1322 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1323
1324 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
1325 }
1326 /* 3.1 Bulk-Only Mass Storage Reset */
1327 else if ( pSetup->bmRequestType == (VUSB_REQ_CLASS | VUSB_TO_INTERFACE)
1328 && pSetup->bRequest == 0xff
1329 && !pSetup->wValue
1330 && !pSetup->wLength
1331 && pSetup->wIndex == 0)
1332 {
1333 Log(("usbHidHandleDefaultPipe: Bulk-Only Mass Storage Reset\n"));
1334 return usbHidResetWorker(pThis, pUrb, false /*fSetConfig*/);
1335 }
1336 else
1337 {
1338 Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1339 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1340 return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
1341 }
1342
1343 return VINF_SUCCESS;
1344}
1345
1346
1347/**
1348 * @copydoc PDMUSBREG::pfnUrbQueue
1349 */
1350static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
1351{
1352 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1353 LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
1354 RTCritSectEnter(&pThis->CritSect);
1355
1356 /*
1357 * Parse on a per end-point basis.
1358 */
1359 int rc;
1360 switch (pUrb->EndPt)
1361 {
1362 case 0:
1363 rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
1364 break;
1365
1366 case 0x81:
1367 AssertFailed();
1368 case 0x01:
1369 rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
1370 break;
1371
1372 default:
1373 AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
1374 rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
1375 break;
1376 }
1377
1378 RTCritSectLeave(&pThis->CritSect);
1379 return rc;
1380}
1381
1382
1383/**
1384 * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
1385 */
1386static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
1387{
1388 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1389 LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
1390
1391 if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
1392 {
1393 RTCritSectEnter(&pThis->CritSect);
1394 pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
1395 RTCritSectLeave(&pThis->CritSect);
1396 }
1397
1398 return VINF_SUCCESS;
1399}
1400
1401
1402/**
1403 * @copydoc PDMUSBREG::pfnUsbSetInterface
1404 */
1405static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
1406{
1407 LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
1408 Assert(bAlternateSetting == 0);
1409 return VINF_SUCCESS;
1410}
1411
1412
1413/**
1414 * @copydoc PDMUSBREG::pfnUsbSetConfiguration
1415 */
1416static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
1417 const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
1418{
1419 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1420 LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
1421 Assert(bConfigurationValue == 1);
1422 RTCritSectEnter(&pThis->CritSect);
1423
1424 /*
1425 * If the same config is applied more than once, it's a kind of reset.
1426 */
1427 if (pThis->bConfigurationValue == bConfigurationValue)
1428 usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
1429 pThis->bConfigurationValue = bConfigurationValue;
1430
1431 /*
1432 * Set received event type to absolute or relative.
1433 */
1434 pThis->Lun0.pDrv->pfnReportModes(pThis->Lun0.pDrv,
1435 pThis->enmMode == USBHIDMODE_RELATIVE,
1436 pThis->enmMode == USBHIDMODE_ABSOLUTE);
1437
1438 RTCritSectLeave(&pThis->CritSect);
1439 return VINF_SUCCESS;
1440}
1441
1442
1443/**
1444 * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
1445 */
1446static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
1447{
1448 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1449 LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
1450 switch (pThis->enmMode)
1451 {
1452 case USBHIDMODE_ABSOLUTE:
1453 return &g_UsbHidTDescCache;
1454 case USBHIDMODE_RELATIVE:
1455 return &g_UsbHidMDescCache;
1456 case USBHIDMODE_MULTI_TOUCH:
1457 return &g_UsbHidMTDescCache;
1458 default:
1459 return NULL;
1460 }
1461}
1462
1463
1464/**
1465 * @copydoc PDMUSBREG::pfnUsbReset
1466 */
1467static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
1468{
1469 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1470 LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
1471 RTCritSectEnter(&pThis->CritSect);
1472
1473 int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
1474
1475 RTCritSectLeave(&pThis->CritSect);
1476 return rc;
1477}
1478
1479
1480/**
1481 * @copydoc PDMUSBREG::pfnDestruct
1482 */
1483static void usbHidDestruct(PPDMUSBINS pUsbIns)
1484{
1485 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1486 LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
1487
1488 if (RTCritSectIsInitialized(&pThis->CritSect))
1489 {
1490 RTCritSectEnter(&pThis->CritSect);
1491 RTCritSectLeave(&pThis->CritSect);
1492 RTCritSectDelete(&pThis->CritSect);
1493 }
1494
1495 if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
1496 {
1497 RTSemEventDestroy(pThis->hEvtDoneQueue);
1498 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1499 }
1500}
1501
1502
1503/**
1504 * @copydoc PDMUSBREG::pfnConstruct
1505 */
1506static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
1507{
1508 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1509 bool isAbsolute;
1510 Log(("usbHidConstruct/#%u:\n", iInstance));
1511
1512 /*
1513 * Perform the basic structure initialization first so the destructor
1514 * will not misbehave.
1515 */
1516 pThis->pUsbIns = pUsbIns;
1517 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1518 usbHidQueueInit(&pThis->ToHostQueue);
1519 usbHidQueueInit(&pThis->DoneQueue);
1520
1521 int rc = RTCritSectInit(&pThis->CritSect);
1522 AssertRCReturn(rc, rc);
1523
1524 rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
1525 AssertRCReturn(rc, rc);
1526
1527 /*
1528 * Validate and read the configuration.
1529 */
1530 rc = CFGMR3ValidateConfig(pCfg, "/", "Absolute|CoordShift", "Config", "UsbHid", iInstance);
1531 if (RT_FAILURE(rc))
1532 return rc;
1533 rc = CFGMR3QueryBoolDef(pCfg, "Absolute", &isAbsolute, false);
1534 if (RT_FAILURE(rc))
1535 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query settings"));
1536 pThis->enmMode = isAbsolute ? USBHIDMODE_ABSOLUTE : USBHIDMODE_RELATIVE;
1537
1538 pThis->Lun0.IBase.pfnQueryInterface = usbHidMouseQueryInterface;
1539 pThis->Lun0.IPort.pfnPutEvent = usbHidMousePutEvent;
1540 pThis->Lun0.IPort.pfnPutEventAbs = usbHidMousePutEventAbs;
1541
1542 /*
1543 * Attach the mouse driver.
1544 */
1545 rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Mouse Port");
1546 if (RT_FAILURE(rc))
1547 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach mouse driver"));
1548
1549 pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIMOUSECONNECTOR);
1550 if (!pThis->Lun0.pDrv)
1551 return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query mouse interface"));
1552
1553 rc = CFGMR3QueryU8Def(pCfg, "CoordShift", &pThis->u8CoordShift, 1);
1554 if (RT_FAILURE(rc))
1555 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query shift factor"));
1556
1557 return VINF_SUCCESS;
1558}
1559
1560
1561/**
1562 * The USB Human Interface Device (HID) Mouse registration record.
1563 */
1564const PDMUSBREG g_UsbHidMou =
1565{
1566 /* u32Version */
1567 PDM_USBREG_VERSION,
1568 /* szName */
1569 "HidMouse",
1570 /* pszDescription */
1571 "USB HID Mouse.",
1572 /* fFlags */
1573 0,
1574 /* cMaxInstances */
1575 ~0U,
1576 /* cbInstance */
1577 sizeof(USBHID),
1578 /* pfnConstruct */
1579 usbHidConstruct,
1580 /* pfnDestruct */
1581 usbHidDestruct,
1582 /* pfnVMInitComplete */
1583 NULL,
1584 /* pfnVMPowerOn */
1585 NULL,
1586 /* pfnVMReset */
1587 NULL,
1588 /* pfnVMSuspend */
1589 NULL,
1590 /* pfnVMResume */
1591 NULL,
1592 /* pfnVMPowerOff */
1593 NULL,
1594 /* pfnHotPlugged */
1595 NULL,
1596 /* pfnHotUnplugged */
1597 NULL,
1598 /* pfnDriverAttach */
1599 NULL,
1600 /* pfnDriverDetach */
1601 NULL,
1602 /* pfnQueryInterface */
1603 NULL,
1604 /* pfnUsbReset */
1605 usbHidUsbReset,
1606 /* pfnUsbGetDescriptorCache */
1607 usbHidUsbGetDescriptorCache,
1608 /* pfnUsbSetConfiguration */
1609 usbHidUsbSetConfiguration,
1610 /* pfnUsbSetInterface */
1611 usbHidUsbSetInterface,
1612 /* pfnUsbClearHaltedEndpoint */
1613 usbHidUsbClearHaltedEndpoint,
1614 /* pfnUrbNew */
1615 NULL/*usbHidUrbNew*/,
1616 /* pfnUrbQueue */
1617 usbHidQueue,
1618 /* pfnUrbCancel */
1619 usbHidUrbCancel,
1620 /* pfnUrbReap */
1621 usbHidUrbReap,
1622 /* u32TheEnd */
1623 PDM_USBREG_VERSION
1624};
Note: See TracBrowser for help on using the repository browser.

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