1 | /* $Id: USBTest.cpp 81023 2019-09-26 14:52:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox host drivers - USB drivers - Filter & driver installation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
82 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
|
---|
83 | if (hSMgr)
|
---|
84 | {
|
---|
85 | SC_HANDLE hService = OpenServiceW(hSMgr, USBMON_SERVICE_NAME_W, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
86 | if (hService)
|
---|
87 | {
|
---|
88 | /*
|
---|
89 | * Stop the service.
|
---|
90 | */
|
---|
91 | SERVICE_STATUS Status;
|
---|
92 | QueryServiceStatus(hService, &Status);
|
---|
93 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
94 | rc = 0;
|
---|
95 | else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
|
---|
96 | {
|
---|
97 | int iWait = 100;
|
---|
98 | while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
|
---|
99 | {
|
---|
100 | Sleep(100);
|
---|
101 | QueryServiceStatus(hService, &Status);
|
---|
102 | }
|
---|
103 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
104 | rc = 0;
|
---|
105 | else
|
---|
106 | AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
111 | AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
|
---|
112 | }
|
---|
113 | CloseServiceHandle(hService);
|
---|
114 | }
|
---|
115 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
116 | rc = 0;
|
---|
117 | else
|
---|
118 | {
|
---|
119 | DWORD LastError = GetLastError(); NOREF(LastError);
|
---|
120 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
|
---|
121 | }
|
---|
122 | CloseServiceHandle(hSMgr);
|
---|
123 | }
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 | /**
|
---|
127 | * Release specified USB device to the host.
|
---|
128 | *
|
---|
129 | * @returns VBox status code
|
---|
130 | * @param usVendorId Vendor id
|
---|
131 | * @param usProductId Product id
|
---|
132 | * @param usRevision Revision
|
---|
133 | */
|
---|
134 | int usbMonReleaseDevice(USHORT usVendorId, USHORT usProductId, USHORT usRevision)
|
---|
135 | {
|
---|
136 | USBSUP_RELEASE release;
|
---|
137 | DWORD cbReturned = 0;
|
---|
138 |
|
---|
139 | printf("usbLibReleaseDevice %x %x %x\n", usVendorId, usProductId, usRevision);
|
---|
140 |
|
---|
141 | release.usVendorId = usVendorId;
|
---|
142 | release.usProductId = usProductId;
|
---|
143 | release.usRevision = usRevision;
|
---|
144 |
|
---|
145 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_RELEASE_DEVICE, &release, sizeof(release), NULL, 0, &cbReturned, NULL))
|
---|
146 | {
|
---|
147 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
148 | return RTErrConvertFromWin32(GetLastError());
|
---|
149 | }
|
---|
150 |
|
---|
151 | return VINF_SUCCESS;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Add USB device filter
|
---|
157 | *
|
---|
158 | * @returns VBox status code.
|
---|
159 | * @param usVendorId Vendor id
|
---|
160 | * @param usProductId Product id
|
---|
161 | * @param usRevision Revision
|
---|
162 | * @param ppID Pointer to filter id
|
---|
163 | */
|
---|
164 | int usbMonInsertFilter(USHORT usVendorId, USHORT usProductId, USHORT usRevision, void **ppID)
|
---|
165 | {
|
---|
166 | USBFILTER filter;
|
---|
167 | USBSUP_FLTADDOUT flt_add;
|
---|
168 | DWORD cbReturned = 0;
|
---|
169 |
|
---|
170 | Assert(g_hUSBMonitor);
|
---|
171 |
|
---|
172 | printf("usblibInsertFilter %04X %04X %04X\n", usVendorId, usProductId, usRevision);
|
---|
173 |
|
---|
174 | USBFilterInit(&filter, USBFILTERTYPE_CAPTURE);
|
---|
175 | USBFilterSetNumExact(&filter, USBFILTERIDX_VENDOR_ID, usVendorId, true);
|
---|
176 | USBFilterSetNumExact(&filter, USBFILTERIDX_PRODUCT_ID, usProductId, true);
|
---|
177 | USBFilterSetNumExact(&filter, USBFILTERIDX_DEVICE_REV, usRevision, true);
|
---|
178 |
|
---|
179 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_ADD_FILTER, &filter, sizeof(filter), &flt_add, sizeof(flt_add), &cbReturned, NULL))
|
---|
180 | {
|
---|
181 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
182 | return RTErrConvertFromWin32(GetLastError());
|
---|
183 | }
|
---|
184 | *ppID = (void *)flt_add.uId;
|
---|
185 | return VINF_SUCCESS;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Applies existing filters to currently plugged-in USB devices
|
---|
190 | *
|
---|
191 | * @returns VBox status code.
|
---|
192 | */
|
---|
193 | int usbMonRunFilters(void)
|
---|
194 | {
|
---|
195 | DWORD cbReturned = 0;
|
---|
196 |
|
---|
197 | Assert(g_hUSBMonitor);
|
---|
198 |
|
---|
199 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_RUN_FILTERS, NULL, 0, NULL, 0, &cbReturned, NULL))
|
---|
200 | {
|
---|
201 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
202 | return RTErrConvertFromWin32(GetLastError());
|
---|
203 | }
|
---|
204 | return VINF_SUCCESS;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Remove USB device filter
|
---|
209 | *
|
---|
210 | * @returns VBox status code.
|
---|
211 | * @param aID Filter id
|
---|
212 | */
|
---|
213 | int usbMonRemoveFilter (void *aID)
|
---|
214 | {
|
---|
215 | uintptr_t uId;
|
---|
216 | DWORD cbReturned = 0;
|
---|
217 |
|
---|
218 | Assert(g_hUSBMonitor);
|
---|
219 |
|
---|
220 | printf("usblibRemoveFilter %p\n", aID);
|
---|
221 |
|
---|
222 | uId = (uintptr_t)aID;
|
---|
223 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_REMOVE_FILTER, &uId, sizeof(uId), NULL, 0,&cbReturned, NULL))
|
---|
224 | {
|
---|
225 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
226 | return RTErrConvertFromWin32(GetLastError());
|
---|
227 | }
|
---|
228 | return VINF_SUCCESS;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Initialize the USB monitor
|
---|
233 | *
|
---|
234 | * @returns VBox status code.
|
---|
235 | */
|
---|
236 | int usbMonitorInit()
|
---|
237 | {
|
---|
238 | int rc;
|
---|
239 | USBSUP_VERSION version = {0};
|
---|
240 | DWORD cbReturned;
|
---|
241 |
|
---|
242 | printf("usbproxy: usbLibInit\n");
|
---|
243 |
|
---|
244 | g_hUSBMonitor = CreateFile (USBMON_DEVICE_NAME,
|
---|
245 | GENERIC_READ | GENERIC_WRITE,
|
---|
246 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
247 | NULL, // no SECURITY_ATTRIBUTES structure
|
---|
248 | OPEN_EXISTING, // No special create flags
|
---|
249 | FILE_ATTRIBUTE_SYSTEM,
|
---|
250 | NULL); // No template file
|
---|
251 |
|
---|
252 | if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
|
---|
253 | {
|
---|
254 | usbMonStartService();
|
---|
255 |
|
---|
256 | g_hUSBMonitor = CreateFile (USBMON_DEVICE_NAME,
|
---|
257 | GENERIC_READ | GENERIC_WRITE,
|
---|
258 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
259 | NULL, // no SECURITY_ATTRIBUTES structure
|
---|
260 | OPEN_EXISTING, // No special create flags
|
---|
261 | FILE_ATTRIBUTE_SYSTEM,
|
---|
262 | NULL); // No template file
|
---|
263 |
|
---|
264 | if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
|
---|
265 | {
|
---|
266 | /* AssertFailed(); */
|
---|
267 | printf("usbproxy: Unable to open filter driver!! (rc=%d)\n", GetLastError());
|
---|
268 | rc = VERR_FILE_NOT_FOUND;
|
---|
269 | goto failure;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*
|
---|
274 | * Check the version
|
---|
275 | */
|
---|
276 | cbReturned = 0;
|
---|
277 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_GET_VERSION, NULL, 0,&version, sizeof(version), &cbReturned, NULL))
|
---|
278 | {
|
---|
279 | printf("usbproxy: Unable to query filter version!! (rc=%d)\n", GetLastError());
|
---|
280 | rc = VERR_VERSION_MISMATCH;
|
---|
281 | goto failure;
|
---|
282 | }
|
---|
283 |
|
---|
284 | if ( version.u32Major != USBMON_MAJOR_VERSION
|
---|
285 | #if USBMON_MINOR_VERSION != 0
|
---|
286 | || version.u32Minor < USBMON_MINOR_VERSION
|
---|
287 | #endif
|
---|
288 | )
|
---|
289 | {
|
---|
290 | printf("usbproxy: Filter driver version mismatch!!\n");
|
---|
291 | rc = VERR_VERSION_MISMATCH;
|
---|
292 | goto failure;
|
---|
293 | }
|
---|
294 |
|
---|
295 | return VINF_SUCCESS;
|
---|
296 |
|
---|
297 | failure:
|
---|
298 | if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
|
---|
299 | {
|
---|
300 | CloseHandle(g_hUSBMonitor);
|
---|
301 | g_hUSBMonitor = INVALID_HANDLE_VALUE;
|
---|
302 | }
|
---|
303 | return rc;
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Terminate the USB monitor
|
---|
310 | *
|
---|
311 | * @returns VBox status code.
|
---|
312 | */
|
---|
313 | int usbMonitorTerm()
|
---|
314 | {
|
---|
315 | if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
|
---|
316 | {
|
---|
317 | CloseHandle(g_hUSBMonitor);
|
---|
318 | g_hUSBMonitor = INVALID_HANDLE_VALUE;
|
---|
319 | }
|
---|
320 | /*
|
---|
321 | * If we started the service we might consider stopping it too.
|
---|
322 | *
|
---|
323 | * Since this won't work unless the process starting it is the
|
---|
324 | * last user we might wanna skip this...
|
---|
325 | */
|
---|
326 | if (g_fStartedService)
|
---|
327 | {
|
---|
328 | usbMonStopService();
|
---|
329 | g_fStartedService = false;
|
---|
330 | }
|
---|
331 |
|
---|
332 | return VINF_SUCCESS;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | int __cdecl main(int argc, char **argv)
|
---|
337 | {
|
---|
338 | int rc;
|
---|
339 | int c;
|
---|
340 | RT_NOREF2(argc, argv);
|
---|
341 |
|
---|
342 | printf("USB test\n");
|
---|
343 |
|
---|
344 | rc = usbMonitorInit();
|
---|
345 | AssertRC(rc);
|
---|
346 |
|
---|
347 | void *pId1, *pId2, *pId3;
|
---|
348 |
|
---|
349 | usbMonInsertFilter(0x0529, 0x0514, 0x0100, &pId1);
|
---|
350 | usbMonInsertFilter(0x0A16, 0x2499, 0x0100, &pId2);
|
---|
351 | usbMonInsertFilter(0x80EE, 0x0030, 0x0110, &pId3);
|
---|
352 |
|
---|
353 | printf("Waiting to capture devices... enter 'r' to run filters\n");
|
---|
354 | c = getchar();
|
---|
355 | if (c == 'r')
|
---|
356 | {
|
---|
357 | usbMonRunFilters();
|
---|
358 | printf("Waiting to capture devices...\n");
|
---|
359 | getchar(); /* eat the '\n' */
|
---|
360 | getchar(); /* wait for more input */
|
---|
361 | }
|
---|
362 |
|
---|
363 | printf("Releasing device\n");
|
---|
364 | usbMonReleaseDevice(0xA16, 0x2499, 0x100);
|
---|
365 |
|
---|
366 | usbMonRemoveFilter(pId1);
|
---|
367 | usbMonRemoveFilter(pId2);
|
---|
368 | usbMonRemoveFilter(pId3);
|
---|
369 |
|
---|
370 | rc = usbMonitorTerm();
|
---|
371 |
|
---|
372 | return 0;
|
---|
373 | }
|
---|