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