1 | /* $Id: USBTest.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox host drivers - USB drivers - Filter & driver installation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 pszVendor Vendor filter string
|
---|
160 | * @param pszProduct Product filter string
|
---|
161 | * @param pszRevision Revision filter string
|
---|
162 | * @param ppID Pointer to filter id
|
---|
163 | */
|
---|
164 | int usbMonInsertFilter(const char *pszVendor, const char *pszProduct, const char *pszRevision, void **ppID)
|
---|
165 | {
|
---|
166 | USBFILTER filter;
|
---|
167 | USBSUP_FLTADDOUT flt_add;
|
---|
168 | DWORD cbReturned = 0;
|
---|
169 |
|
---|
170 | Assert(g_hUSBMonitor);
|
---|
171 |
|
---|
172 | printf("usblibInsertFilter %s %s %s\n", pszVendor, pszProduct, pszRevision);
|
---|
173 |
|
---|
174 | // strncpy(filter.szVendor, pszVendor, sizeof(filter.szVendor));
|
---|
175 | // strncpy(filter.szProduct, pszProduct, sizeof(filter.szProduct));
|
---|
176 | // strncpy(filter.szRevision, pszRevision, sizeof(filter.szRevision));
|
---|
177 |
|
---|
178 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_ADD_FILTER, &filter, sizeof(filter), &flt_add, sizeof(flt_add), &cbReturned, NULL))
|
---|
179 | {
|
---|
180 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
181 | return RTErrConvertFromWin32(GetLastError());
|
---|
182 | }
|
---|
183 | *ppID = (void *)flt_add.uId;
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Remove USB device filter
|
---|
189 | *
|
---|
190 | * @returns VBox status code.
|
---|
191 | * @param aID Filter id
|
---|
192 | */
|
---|
193 | int usbMonRemoveFilter (void *aID)
|
---|
194 | {
|
---|
195 | uintptr_t uId;
|
---|
196 | DWORD cbReturned = 0;
|
---|
197 |
|
---|
198 | Assert(g_hUSBMonitor);
|
---|
199 |
|
---|
200 | printf("usblibRemoveFilter %p\n", aID);
|
---|
201 |
|
---|
202 | uId = (uintptr_t)aID;
|
---|
203 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_REMOVE_FILTER, &uId, sizeof(uId), NULL, 0,&cbReturned, NULL))
|
---|
204 | {
|
---|
205 | AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
|
---|
206 | return RTErrConvertFromWin32(GetLastError());
|
---|
207 | }
|
---|
208 | return VINF_SUCCESS;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Initialize the USB monitor
|
---|
213 | *
|
---|
214 | * @returns VBox status code.
|
---|
215 | */
|
---|
216 | int usbMonitorInit()
|
---|
217 | {
|
---|
218 | int rc;
|
---|
219 | USBSUP_VERSION version = {0};
|
---|
220 | DWORD cbReturned;
|
---|
221 |
|
---|
222 | printf("usbproxy: usbLibInit\n");
|
---|
223 |
|
---|
224 | g_hUSBMonitor = CreateFile (USBMON_DEVICE_NAME,
|
---|
225 | GENERIC_READ | GENERIC_WRITE,
|
---|
226 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
227 | NULL, // no SECURITY_ATTRIBUTES structure
|
---|
228 | OPEN_EXISTING, // No special create flags
|
---|
229 | FILE_ATTRIBUTE_SYSTEM,
|
---|
230 | NULL); // No template file
|
---|
231 |
|
---|
232 | if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
|
---|
233 | {
|
---|
234 | usbMonStartService();
|
---|
235 |
|
---|
236 | g_hUSBMonitor = CreateFile (USBMON_DEVICE_NAME,
|
---|
237 | GENERIC_READ | GENERIC_WRITE,
|
---|
238 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
239 | NULL, // no SECURITY_ATTRIBUTES structure
|
---|
240 | OPEN_EXISTING, // No special create flags
|
---|
241 | FILE_ATTRIBUTE_SYSTEM,
|
---|
242 | NULL); // No template file
|
---|
243 |
|
---|
244 | if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
|
---|
245 | {
|
---|
246 | /* AssertFailed(); */
|
---|
247 | printf("usbproxy: Unable to open filter driver!! (rc=%d)\n", GetLastError());
|
---|
248 | rc = VERR_FILE_NOT_FOUND;
|
---|
249 | goto failure;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Check the version
|
---|
255 | */
|
---|
256 | cbReturned = 0;
|
---|
257 | if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_GET_VERSION, NULL, 0,&version, sizeof(version), &cbReturned, NULL))
|
---|
258 | {
|
---|
259 | printf("usbproxy: Unable to query filter version!! (rc=%d)\n", GetLastError());
|
---|
260 | rc = VERR_VERSION_MISMATCH;
|
---|
261 | goto failure;
|
---|
262 | }
|
---|
263 |
|
---|
264 | if ( version.u32Major != USBMON_MAJOR_VERSION
|
---|
265 | #if USBMON_MINOR_VERSION != 0
|
---|
266 | || version.u32Minor < USBMON_MINOR_VERSION
|
---|
267 | #endif
|
---|
268 | )
|
---|
269 | {
|
---|
270 | printf("usbproxy: Filter driver version mismatch!!\n");
|
---|
271 | rc = VERR_VERSION_MISMATCH;
|
---|
272 | goto failure;
|
---|
273 | }
|
---|
274 |
|
---|
275 | return VINF_SUCCESS;
|
---|
276 |
|
---|
277 | failure:
|
---|
278 | if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
|
---|
279 | {
|
---|
280 | CloseHandle(g_hUSBMonitor);
|
---|
281 | g_hUSBMonitor = INVALID_HANDLE_VALUE;
|
---|
282 | }
|
---|
283 | return rc;
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Terminate the USB monitor
|
---|
290 | *
|
---|
291 | * @returns VBox status code.
|
---|
292 | */
|
---|
293 | int usbMonitorTerm()
|
---|
294 | {
|
---|
295 | if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
|
---|
296 | {
|
---|
297 | CloseHandle(g_hUSBMonitor);
|
---|
298 | g_hUSBMonitor = INVALID_HANDLE_VALUE;
|
---|
299 | }
|
---|
300 | /*
|
---|
301 | * If we started the service we might consider stopping it too.
|
---|
302 | *
|
---|
303 | * Since this won't work unless the process starting it is the
|
---|
304 | * last user we might wanna skip this...
|
---|
305 | */
|
---|
306 | if (g_fStartedService)
|
---|
307 | {
|
---|
308 | usbMonStopService();
|
---|
309 | g_fStartedService = false;
|
---|
310 | }
|
---|
311 |
|
---|
312 | return VINF_SUCCESS;
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | int __cdecl main(int argc, char **argv)
|
---|
317 | {
|
---|
318 | int rc;
|
---|
319 | RT_NOREF2(argc, argv);
|
---|
320 |
|
---|
321 | printf("USB test\n");
|
---|
322 |
|
---|
323 | rc = usbMonitorInit();
|
---|
324 | AssertRC(rc);
|
---|
325 |
|
---|
326 | void *pId1, *pId2;
|
---|
327 |
|
---|
328 | usbMonInsertFilter("0529", "0514", "0100", &pId1);
|
---|
329 | usbMonInsertFilter("0A16", "2499", "0100", &pId2);
|
---|
330 |
|
---|
331 | printf("Waiting to capture device\n");
|
---|
332 | getchar();
|
---|
333 |
|
---|
334 | printf("Releasing device\n");
|
---|
335 | usbMonReleaseDevice(0xA16, 0x2499, 0x100);
|
---|
336 |
|
---|
337 | usbMonRemoveFilter(pId1);
|
---|
338 | usbMonRemoveFilter(pId2);
|
---|
339 |
|
---|
340 | rc = usbMonitorTerm();
|
---|
341 |
|
---|
342 | return 0;
|
---|
343 | }
|
---|