1 | /* $Id: USBProxyBackendWindows.cpp 59120 2015-12-14 14:34:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Service, Windows Specialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. 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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include "USBProxyBackend.h"
|
---|
23 | #include "Logging.h"
|
---|
24 |
|
---|
25 | #include <VBox/usb.h>
|
---|
26 | #include <VBox/err.h>
|
---|
27 |
|
---|
28 | #include <iprt/string.h>
|
---|
29 | #include <iprt/alloc.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/file.h>
|
---|
32 | #include <iprt/err.h>
|
---|
33 |
|
---|
34 | #include <VBox/usblib.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Initialize data members.
|
---|
39 | */
|
---|
40 | USBProxyBackendWindows::USBProxyBackendWindows(USBProxyService *aUsbProxyService)
|
---|
41 | : USBProxyBackend(aUsbProxyService), mhEventInterrupt(INVALID_HANDLE_VALUE)
|
---|
42 | {
|
---|
43 | LogFlowThisFunc(("aUsbProxyService=%p\n", aUsbProxyService));
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Initializes the object (called right after construction).
|
---|
49 | *
|
---|
50 | * @returns S_OK on success and non-fatal failures, some COM error otherwise.
|
---|
51 | */
|
---|
52 | int USBProxyBackendWindows::init(void)
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Create the semaphore (considered fatal).
|
---|
56 | */
|
---|
57 | mhEventInterrupt = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
58 | AssertReturn(mhEventInterrupt != INVALID_HANDLE_VALUE, VERR_OUT_OF_RESOURCES);
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * Initialize the USB lib and stuff.
|
---|
62 | */
|
---|
63 | int rc = USBLibInit();
|
---|
64 | if (RT_SUCCESS(rc))
|
---|
65 | {
|
---|
66 | /*
|
---|
67 | * Start the poller thread.
|
---|
68 | */
|
---|
69 | rc = start();
|
---|
70 | if (RT_SUCCESS(rc))
|
---|
71 | {
|
---|
72 | LogFlowThisFunc(("returns successfully\n"));
|
---|
73 | return VINF_SUCCESS;
|
---|
74 | }
|
---|
75 |
|
---|
76 | USBLibTerm();
|
---|
77 | }
|
---|
78 |
|
---|
79 | CloseHandle(mhEventInterrupt);
|
---|
80 | mhEventInterrupt = INVALID_HANDLE_VALUE;
|
---|
81 |
|
---|
82 | LogFlowThisFunc(("returns failure!!! (rc=%Rrc)\n", rc));
|
---|
83 | return rc;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Stop all service threads and free the device chain.
|
---|
89 | */
|
---|
90 | USBProxyBackendWindows::~USBProxyBackendWindows()
|
---|
91 | {
|
---|
92 | LogFlowThisFunc(("\n"));
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Stop the service.
|
---|
96 | */
|
---|
97 | if (isActive())
|
---|
98 | stop();
|
---|
99 |
|
---|
100 | if (mhEventInterrupt != INVALID_HANDLE_VALUE)
|
---|
101 | CloseHandle(mhEventInterrupt);
|
---|
102 | mhEventInterrupt = INVALID_HANDLE_VALUE;
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Terminate the library...
|
---|
106 | */
|
---|
107 | int rc = USBLibTerm();
|
---|
108 | AssertRC(rc);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | void *USBProxyBackendWindows::insertFilter(PCUSBFILTER aFilter)
|
---|
113 | {
|
---|
114 | AssertReturn(aFilter, NULL);
|
---|
115 |
|
---|
116 | LogFlow(("USBProxyBackendWindows::insertFilter()\n"));
|
---|
117 |
|
---|
118 | void *pvId = USBLibAddFilter(aFilter);
|
---|
119 |
|
---|
120 | LogFlow(("USBProxyBackendWindows::insertFilter(): returning pvId=%p\n", pvId));
|
---|
121 |
|
---|
122 | return pvId;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | void USBProxyBackendWindows::removeFilter(void *aID)
|
---|
127 | {
|
---|
128 | LogFlow(("USBProxyBackendWindows::removeFilter(): id=%p\n", aID));
|
---|
129 |
|
---|
130 | AssertReturnVoid(aID);
|
---|
131 |
|
---|
132 | USBLibRemoveFilter(aID);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | int USBProxyBackendWindows::captureDevice(HostUSBDevice *aDevice)
|
---|
137 | {
|
---|
138 | /*
|
---|
139 | * Check preconditions.
|
---|
140 | */
|
---|
141 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
142 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
143 |
|
---|
144 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
145 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
146 |
|
---|
147 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Create a one-shot ignore filter for the device
|
---|
151 | * and trigger a re-enumeration of it.
|
---|
152 | */
|
---|
153 | USBFILTER Filter;
|
---|
154 | USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_CAPTURE);
|
---|
155 | initFilterFromDevice(&Filter, aDevice);
|
---|
156 | Log(("USBFILTERIDX_PORT=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_PORT)));
|
---|
157 | Log(("USBFILTERIDX_BUS=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_BUS)));
|
---|
158 |
|
---|
159 | void *pvId = USBLibAddFilter(&Filter);
|
---|
160 | if (!pvId)
|
---|
161 | {
|
---|
162 | AssertMsgFailed(("Add one-shot Filter failed\n"));
|
---|
163 | return VERR_GENERAL_FAILURE;
|
---|
164 | }
|
---|
165 |
|
---|
166 | int rc = USBLibRunFilters();
|
---|
167 | if (!RT_SUCCESS(rc))
|
---|
168 | {
|
---|
169 | AssertMsgFailed(("Run Filters failed\n"));
|
---|
170 | USBLibRemoveFilter(pvId);
|
---|
171 | return rc;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return VINF_SUCCESS;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | int USBProxyBackendWindows::releaseDevice(HostUSBDevice *aDevice)
|
---|
179 | {
|
---|
180 | /*
|
---|
181 | * Check preconditions.
|
---|
182 | */
|
---|
183 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
184 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
185 |
|
---|
186 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
187 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
188 |
|
---|
189 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * Create a one-shot ignore filter for the device
|
---|
193 | * and trigger a re-enumeration of it.
|
---|
194 | */
|
---|
195 | USBFILTER Filter;
|
---|
196 | USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_IGNORE);
|
---|
197 | initFilterFromDevice(&Filter, aDevice);
|
---|
198 | Log(("USBFILTERIDX_PORT=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_PORT)));
|
---|
199 | Log(("USBFILTERIDX_BUS=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_BUS)));
|
---|
200 |
|
---|
201 | void *pvId = USBLibAddFilter(&Filter);
|
---|
202 | if (!pvId)
|
---|
203 | {
|
---|
204 | AssertMsgFailed(("Add one-shot Filter failed\n"));
|
---|
205 | return VERR_GENERAL_FAILURE;
|
---|
206 | }
|
---|
207 |
|
---|
208 | int rc = USBLibRunFilters();
|
---|
209 | if (!RT_SUCCESS(rc))
|
---|
210 | {
|
---|
211 | AssertMsgFailed(("Run Filters failed\n"));
|
---|
212 | USBLibRemoveFilter(pvId);
|
---|
213 | return rc;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | return VINF_SUCCESS;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | bool USBProxyBackendWindows::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters,
|
---|
222 | SessionMachine **aIgnoreMachine)
|
---|
223 | {
|
---|
224 | AssertReturn(aDevice, false);
|
---|
225 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), false);
|
---|
226 | /* Nothing special here so far, so fall back on parent */
|
---|
227 | return USBProxyBackend::updateDeviceState(aDevice, aUSBDevice, aRunFilters, aIgnoreMachine);
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | int USBProxyBackendWindows::wait(unsigned aMillies)
|
---|
232 | {
|
---|
233 | return USBLibWaitChange(aMillies);
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | int USBProxyBackendWindows::interruptWait(void)
|
---|
238 | {
|
---|
239 | return USBLibInterruptWaitChange();
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Gets a list of all devices the VM can grab
|
---|
244 | */
|
---|
245 | PUSBDEVICE USBProxyBackendWindows::getDevices(void)
|
---|
246 | {
|
---|
247 | PUSBDEVICE pDevices = NULL;
|
---|
248 | uint32_t cDevices = 0;
|
---|
249 |
|
---|
250 | Log(("USBProxyBackendWindows::getDevices\n"));
|
---|
251 | USBLibGetDevices(&pDevices, &cDevices);
|
---|
252 | return pDevices;
|
---|
253 | }
|
---|
254 |
|
---|