1 | /* $Id: USBTest.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox host drivers - USB drivers - Filter & driver installation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/win/windows.h>
|
---|
42 | #include <iprt/win/setupapi.h>
|
---|
43 | #include <newdev.h>
|
---|
44 |
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <VBox/err.h>
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/path.h>
|
---|
49 | #include <iprt/stream.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <VBox/usblib.h>
|
---|
52 | #include <VBox/VBoxDrvCfg-win.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************************************************************************
|
---|
56 | * Global Variables *
|
---|
57 | *********************************************************************************************************************************/
|
---|
58 | /** Handle to the open device. */
|
---|
59 | static HANDLE g_hUSBMonitor = INVALID_HANDLE_VALUE;
|
---|
60 | /** Flags whether or not we started the service. */
|
---|
61 | static bool g_fStartedService = false;
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Attempts to start the service, creating it if necessary.
|
---|
66 | *
|
---|
67 | * @returns 0 on success.
|
---|
68 | * @returns -1 on failure.
|
---|
69 | * @param fRetry Indicates retry call.
|
---|
70 | */
|
---|
71 | int usbMonStartService(void)
|
---|
72 | {
|
---|
73 | HRESULT hr = VBoxDrvCfgSvcStart(USBMON_SERVICE_NAME_W);
|
---|
74 | if (hr != S_OK)
|
---|
75 | {
|
---|
76 | AssertMsgFailed(("couldn't start service, hr (0x%x)\n", hr));
|
---|
77 | return -1;
|
---|
78 | }
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Stops a possibly running service.
|
---|
84 | *
|
---|
85 | * @returns 0 on success.
|
---|
86 | * @returns -1 on failure.
|
---|
87 | */
|
---|
88 | int usbMonStopService(void)
|
---|
89 | {
|
---|
90 | RTPrintf("usbMonStopService\n");
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Assume it didn't exist, so we'll create the service.
|
---|
94 | */
|
---|
95 | int rc = -1;
|
---|
96 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
97 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", GetLastError()));
|
---|
98 | if (hSMgr)
|
---|
99 | {
|
---|
100 | SC_HANDLE hService = OpenServiceW(hSMgr, USBMON_SERVICE_NAME_W, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
101 | if (hService)
|
---|
102 | {
|
---|
103 | /*
|
---|
104 | * Stop the service.
|
---|
105 | */
|
---|
106 | SERVICE_STATUS Status;
|
---|
107 | QueryServiceStatus(hService, &Status);
|
---|
108 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
109 | rc = 0;
|
---|
110 | else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
|
---|
111 | {
|
---|
112 | int iWait = 100;
|
---|
113 | while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
|
---|
114 | {
|
---|
115 | Sleep(100);
|
---|
116 | QueryServiceStatus(hService, &Status);
|
---|
117 | }
|
---|
118 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
119 | rc = 0;
|
---|
120 | else
|
---|
121 | AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
|
---|
122 | }
|
---|
123 | else
|
---|
124 | AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", GetLastError(), Status.dwCurrentState));
|
---|
125 | CloseServiceHandle(hService);
|
---|
126 | }
|
---|
127 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
128 | rc = 0;
|
---|
129 | else
|
---|
130 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", GetLastError()));
|
---|
131 | CloseServiceHandle(hSMgr);
|
---|
132 | }
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Release specified USB device to the host.
|
---|
138 | *
|
---|
139 | * @returns VBox status code
|
---|
140 | * @param usVendorId Vendor id
|
---|
141 | * @param usProductId Product id
|
---|
142 | * @param usRevision Revision
|
---|
143 | */
|
---|
144 | int usbMonReleaseDevice(USHORT usVendorId, USHORT usProductId, USHORT usRevision)
|
---|
145 | {
|
---|
146 | USBSUP_RELEASE release;
|
---|
147 | DWORD cbReturned = 0;
|
---|
148 |
|
---|
149 | RTPrintf("usbLibReleaseDevice %x %x %x\n", usVendorId, usProductId, usRevision);
|
---|
150 |
|
---|
151 | release.usVendorId = usVendorId;
|
---|
152 | release.usProductId = usProductId;
|
---|
153 | release.usRevision = usRevision;
|
---|
154 |
|
---|
155 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_RELEASE_DEVICE, &release, sizeof(release), NULL, 0, &cbReturned, NULL))
|
---|
156 | {
|
---|
157 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
158 | return RTErrConvertFromWin32(GetLastError());
|
---|
159 | }
|
---|
160 |
|
---|
161 | return VINF_SUCCESS;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Add USB device filter
|
---|
167 | *
|
---|
168 | * @returns VBox status code.
|
---|
169 | * @param usVendorId Vendor id
|
---|
170 | * @param usProductId Product id
|
---|
171 | * @param usRevision Revision
|
---|
172 | * @param ppID Pointer to filter id
|
---|
173 | */
|
---|
174 | int usbMonInsertFilter(USHORT usVendorId, USHORT usProductId, USHORT usRevision, void **ppID)
|
---|
175 | {
|
---|
176 | USBFILTER filter;
|
---|
177 | USBSUP_FLTADDOUT flt_add;
|
---|
178 | DWORD cbReturned = 0;
|
---|
179 |
|
---|
180 | Assert(g_hUSBMonitor);
|
---|
181 |
|
---|
182 | RTPrintf("usblibInsertFilter %04X %04X %04X\n", usVendorId, usProductId, usRevision);
|
---|
183 |
|
---|
184 | USBFilterInit(&filter, USBFILTERTYPE_CAPTURE);
|
---|
185 | USBFilterSetNumExact(&filter, USBFILTERIDX_VENDOR_ID, usVendorId, true);
|
---|
186 | USBFilterSetNumExact(&filter, USBFILTERIDX_PRODUCT_ID, usProductId, true);
|
---|
187 | USBFilterSetNumExact(&filter, USBFILTERIDX_DEVICE_REV, usRevision, true);
|
---|
188 |
|
---|
189 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_ADD_FILTER, &filter, sizeof(filter), &flt_add, sizeof(flt_add), &cbReturned, NULL))
|
---|
190 | {
|
---|
191 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
192 | return RTErrConvertFromWin32(GetLastError());
|
---|
193 | }
|
---|
194 | *ppID = (void *)flt_add.uId;
|
---|
195 | return VINF_SUCCESS;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Applies existing filters to currently plugged-in USB devices
|
---|
200 | *
|
---|
201 | * @returns VBox status code.
|
---|
202 | */
|
---|
203 | int usbMonRunFilters(void)
|
---|
204 | {
|
---|
205 | DWORD cbReturned = 0;
|
---|
206 |
|
---|
207 | Assert(g_hUSBMonitor);
|
---|
208 |
|
---|
209 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_RUN_FILTERS, NULL, 0, NULL, 0, &cbReturned, NULL))
|
---|
210 | {
|
---|
211 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
212 | return RTErrConvertFromWin32(GetLastError());
|
---|
213 | }
|
---|
214 | return VINF_SUCCESS;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Remove USB device filter
|
---|
219 | *
|
---|
220 | * @returns VBox status code.
|
---|
221 | * @param aID Filter id
|
---|
222 | */
|
---|
223 | int usbMonRemoveFilter (void *aID)
|
---|
224 | {
|
---|
225 | uintptr_t uId;
|
---|
226 | DWORD cbReturned = 0;
|
---|
227 |
|
---|
228 | Assert(g_hUSBMonitor);
|
---|
229 |
|
---|
230 | RTPrintf("usblibRemoveFilter %p\n", aID);
|
---|
231 |
|
---|
232 | uId = (uintptr_t)aID;
|
---|
233 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_REMOVE_FILTER, &uId, sizeof(uId), NULL, 0,&cbReturned, NULL))
|
---|
234 | {
|
---|
235 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
236 | return RTErrConvertFromWin32(GetLastError());
|
---|
237 | }
|
---|
238 | return VINF_SUCCESS;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Initialize the USB monitor
|
---|
243 | *
|
---|
244 | * @returns VBox status code.
|
---|
245 | */
|
---|
246 | int usbMonitorInit()
|
---|
247 | {
|
---|
248 | int rc;
|
---|
249 | USBSUP_VERSION version = {0};
|
---|
250 | DWORD cbReturned;
|
---|
251 |
|
---|
252 | RTPrintf("usbproxy: usbLibInit\n");
|
---|
253 |
|
---|
254 | g_hUSBMonitor = CreateFile (USBMON_DEVICE_NAME,
|
---|
255 | GENERIC_READ | GENERIC_WRITE,
|
---|
256 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
257 | NULL, // no SECURITY_ATTRIBUTES structure
|
---|
258 | OPEN_EXISTING, // No special create flags
|
---|
259 | FILE_ATTRIBUTE_SYSTEM,
|
---|
260 | NULL); // No template file
|
---|
261 |
|
---|
262 | if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
|
---|
263 | {
|
---|
264 | usbMonStartService();
|
---|
265 |
|
---|
266 | g_hUSBMonitor = CreateFile (USBMON_DEVICE_NAME,
|
---|
267 | GENERIC_READ | GENERIC_WRITE,
|
---|
268 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
269 | NULL, // no SECURITY_ATTRIBUTES structure
|
---|
270 | OPEN_EXISTING, // No special create flags
|
---|
271 | FILE_ATTRIBUTE_SYSTEM,
|
---|
272 | NULL); // No template file
|
---|
273 |
|
---|
274 | if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
|
---|
275 | {
|
---|
276 | /* AssertFailed(); */
|
---|
277 | RTPrintf("usbproxy: Unable to open filter driver!! (rc=%lu)\n", GetLastError());
|
---|
278 | rc = VERR_FILE_NOT_FOUND;
|
---|
279 | goto failure;
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Check the version
|
---|
285 | */
|
---|
286 | cbReturned = 0;
|
---|
287 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_GET_VERSION, NULL, 0,&version, sizeof(version), &cbReturned, NULL))
|
---|
288 | {
|
---|
289 | RTPrintf("usbproxy: Unable to query filter version!! (rc=%lu)\n", GetLastError());
|
---|
290 | rc = VERR_VERSION_MISMATCH;
|
---|
291 | goto failure;
|
---|
292 | }
|
---|
293 |
|
---|
294 | if ( version.u32Major != USBMON_MAJOR_VERSION
|
---|
295 | #if USBMON_MINOR_VERSION != 0
|
---|
296 | || version.u32Minor < USBMON_MINOR_VERSION
|
---|
297 | #endif
|
---|
298 | )
|
---|
299 | {
|
---|
300 | RTPrintf("usbproxy: Filter driver version mismatch!!\n");
|
---|
301 | rc = VERR_VERSION_MISMATCH;
|
---|
302 | goto failure;
|
---|
303 | }
|
---|
304 |
|
---|
305 | return VINF_SUCCESS;
|
---|
306 |
|
---|
307 | failure:
|
---|
308 | if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
|
---|
309 | {
|
---|
310 | CloseHandle(g_hUSBMonitor);
|
---|
311 | g_hUSBMonitor = INVALID_HANDLE_VALUE;
|
---|
312 | }
|
---|
313 | return rc;
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Terminate the USB monitor
|
---|
320 | *
|
---|
321 | * @returns VBox status code.
|
---|
322 | */
|
---|
323 | int usbMonitorTerm()
|
---|
324 | {
|
---|
325 | if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
|
---|
326 | {
|
---|
327 | CloseHandle(g_hUSBMonitor);
|
---|
328 | g_hUSBMonitor = INVALID_HANDLE_VALUE;
|
---|
329 | }
|
---|
330 | /*
|
---|
331 | * If we started the service we might consider stopping it too.
|
---|
332 | *
|
---|
333 | * Since this won't work unless the process starting it is the
|
---|
334 | * last user we might wanna skip this...
|
---|
335 | */
|
---|
336 | if (g_fStartedService)
|
---|
337 | {
|
---|
338 | usbMonStopService();
|
---|
339 | g_fStartedService = false;
|
---|
340 | }
|
---|
341 |
|
---|
342 | return VINF_SUCCESS;
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | int __cdecl main(int argc, char **argv)
|
---|
347 | {
|
---|
348 | int rc;
|
---|
349 | int c;
|
---|
350 | RT_NOREF2(argc, argv);
|
---|
351 |
|
---|
352 | RTPrintf("USB test\n");
|
---|
353 |
|
---|
354 | rc = usbMonitorInit();
|
---|
355 | AssertRC(rc);
|
---|
356 |
|
---|
357 | void *pId1, *pId2, *pId3;
|
---|
358 |
|
---|
359 | usbMonInsertFilter(0x0529, 0x0514, 0x0100, &pId1);
|
---|
360 | usbMonInsertFilter(0x0A16, 0x2499, 0x0100, &pId2);
|
---|
361 | usbMonInsertFilter(0x80EE, 0x0030, 0x0110, &pId3);
|
---|
362 |
|
---|
363 | RTPrintf("Waiting to capture devices... enter 'r' to run filters\n");
|
---|
364 | c = RTStrmGetCh(g_pStdIn);
|
---|
365 | if (c == 'r')
|
---|
366 | {
|
---|
367 | usbMonRunFilters();
|
---|
368 | RTPrintf("Waiting to capture devices...\n");
|
---|
369 | RTStrmGetCh(g_pStdIn); /* eat the '\n' */
|
---|
370 | RTStrmGetCh(g_pStdIn); /* wait for more input */
|
---|
371 | }
|
---|
372 |
|
---|
373 | RTPrintf("Releasing device\n");
|
---|
374 | usbMonReleaseDevice(0xA16, 0x2499, 0x100);
|
---|
375 |
|
---|
376 | usbMonRemoveFilter(pId1);
|
---|
377 | usbMonRemoveFilter(pId2);
|
---|
378 | usbMonRemoveFilter(pId3);
|
---|
379 |
|
---|
380 | rc = usbMonitorTerm();
|
---|
381 |
|
---|
382 | return 0;
|
---|
383 | }
|
---|
384 |
|
---|