1 | /* $Id: USBProxyServiceWindows.cpp 37599 2011-06-22 21:06:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Service, Windows Specialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include "USBProxyService.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 | USBProxyServiceWindows::USBProxyServiceWindows(Host *aHost)
|
---|
41 | : USBProxyService(aHost), mhEventInterrupt(INVALID_HANDLE_VALUE)
|
---|
42 | {
|
---|
43 | LogFlowThisFunc(("aHost=%p\n", aHost));
|
---|
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 | HRESULT USBProxyServiceWindows::init(void)
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Create the semaphore (considered fatal).
|
---|
56 | */
|
---|
57 | mhEventInterrupt = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
58 | AssertReturn(mhEventInterrupt != INVALID_HANDLE_VALUE, E_FAIL);
|
---|
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 S_OK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | USBLibTerm();
|
---|
77 | }
|
---|
78 |
|
---|
79 | CloseHandle(mhEventInterrupt);
|
---|
80 | mhEventInterrupt = INVALID_HANDLE_VALUE;
|
---|
81 |
|
---|
82 | LogFlowThisFunc(("returns failure!!! (rc=%Rrc)\n", rc));
|
---|
83 | mLastError = rc;
|
---|
84 | return S_OK;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Stop all service threads and free the device chain.
|
---|
90 | */
|
---|
91 | USBProxyServiceWindows::~USBProxyServiceWindows()
|
---|
92 | {
|
---|
93 | LogFlowThisFunc(("\n"));
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Stop the service.
|
---|
97 | */
|
---|
98 | if (isActive())
|
---|
99 | stop();
|
---|
100 |
|
---|
101 | if (mhEventInterrupt != INVALID_HANDLE_VALUE)
|
---|
102 | CloseHandle(mhEventInterrupt);
|
---|
103 | mhEventInterrupt = INVALID_HANDLE_VALUE;
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Terminate the library...
|
---|
107 | */
|
---|
108 | int rc = USBLibTerm();
|
---|
109 | AssertRC(rc);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | void *USBProxyServiceWindows::insertFilter(PCUSBFILTER aFilter)
|
---|
114 | {
|
---|
115 | AssertReturn(aFilter, NULL);
|
---|
116 |
|
---|
117 | LogFlow(("USBProxyServiceWindows::insertFilter()\n"));
|
---|
118 |
|
---|
119 | void *pvId = USBLibAddFilter(aFilter);
|
---|
120 |
|
---|
121 | LogFlow(("USBProxyServiceWindows::insertFilter(): returning pvId=%p\n", pvId));
|
---|
122 |
|
---|
123 | return pvId;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | void USBProxyServiceWindows::removeFilter(void *aID)
|
---|
128 | {
|
---|
129 | LogFlow(("USBProxyServiceWindows::removeFilter(): id=%p\n", aID));
|
---|
130 |
|
---|
131 | AssertReturnVoid(aID);
|
---|
132 |
|
---|
133 | USBLibRemoveFilter(aID);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | int USBProxyServiceWindows::captureDevice(HostUSBDevice *aDevice)
|
---|
138 | {
|
---|
139 | /*
|
---|
140 | * Create a one-shot ignore filter for the device
|
---|
141 | * and trigger a re-enumeration of it.
|
---|
142 | */
|
---|
143 | USBFILTER Filter;
|
---|
144 | USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_CAPTURE);
|
---|
145 | initFilterFromDevice(&Filter, aDevice);
|
---|
146 | Log(("USBFILTERIDX_PORT=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_PORT)));
|
---|
147 | Log(("USBFILTERIDX_BUS=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_BUS)));
|
---|
148 |
|
---|
149 | void *pvId = USBLibAddFilter(&Filter);
|
---|
150 | if (!pvId)
|
---|
151 | {
|
---|
152 | AssertMsgFailed(("Add one-shot Filter failed\n"));
|
---|
153 | return VERR_GENERAL_FAILURE;
|
---|
154 | }
|
---|
155 |
|
---|
156 | int rc = USBLibRunFilters();
|
---|
157 | if (!RT_SUCCESS(rc))
|
---|
158 | {
|
---|
159 | AssertMsgFailed(("Run Filters failed\n"));
|
---|
160 | USBLibRemoveFilter(pvId);
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 | return VINF_SUCCESS;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | int USBProxyServiceWindows::releaseDevice(HostUSBDevice *aDevice)
|
---|
169 | {
|
---|
170 | /*
|
---|
171 | * Create a one-shot ignore filter for the device
|
---|
172 | * and trigger a re-enumeration of it.
|
---|
173 | */
|
---|
174 | USBFILTER Filter;
|
---|
175 | USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_IGNORE);
|
---|
176 | initFilterFromDevice(&Filter, aDevice);
|
---|
177 | Log(("USBFILTERIDX_PORT=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_PORT)));
|
---|
178 | Log(("USBFILTERIDX_BUS=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_BUS)));
|
---|
179 |
|
---|
180 | void *pvId = USBLibAddFilter(&Filter);
|
---|
181 | if (!pvId)
|
---|
182 | {
|
---|
183 | AssertMsgFailed(("Add one-shot Filter failed\n"));
|
---|
184 | return VERR_GENERAL_FAILURE;
|
---|
185 | }
|
---|
186 |
|
---|
187 | int rc = USBLibRunFilters();
|
---|
188 | if (!RT_SUCCESS(rc))
|
---|
189 | {
|
---|
190 | AssertMsgFailed(("Run Filters failed\n"));
|
---|
191 | USBLibRemoveFilter(pvId);
|
---|
192 | return rc;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | return VINF_SUCCESS;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | bool USBProxyServiceWindows::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine)
|
---|
201 | {
|
---|
202 | /* Nothing special here so far, so fall back on parent */
|
---|
203 | return USBProxyService::updateDeviceState(aDevice, aUSBDevice, aRunFilters, aIgnoreMachine);
|
---|
204 |
|
---|
205 | /// @todo remove?
|
---|
206 | #if 0
|
---|
207 | AssertReturn(aDevice, false);
|
---|
208 | AssertReturn(aDevice->isWriteLockOnCurrentThread(), false);
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * We're only called in the 'existing device' state, so if there is a pending async
|
---|
212 | * operation we can check if it completed here and suppress state changes if it hasn't.
|
---|
213 | */
|
---|
214 | /* TESTME */
|
---|
215 | if (aDevice->isStatePending())
|
---|
216 | {
|
---|
217 | bool fRc = aDevice->updateState(aUSBDevice);
|
---|
218 | if (fRc)
|
---|
219 | {
|
---|
220 | if (aDevice->state() != aDevice->pendingState())
|
---|
221 | fRc = false;
|
---|
222 | }
|
---|
223 | return fRc;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /* fall back on parent. */
|
---|
227 | return USBProxyService::updateDeviceState(aDevice, aUSBDevice, aRunFilters, aIgnoreMachine);
|
---|
228 | #endif
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | int USBProxyServiceWindows::wait(unsigned aMillies)
|
---|
233 | {
|
---|
234 | return USBLibWaitChange(aMillies);
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | int USBProxyServiceWindows::interruptWait(void)
|
---|
239 | {
|
---|
240 | return USBLibInterruptWaitChange();
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Gets a list of all devices the VM can grab
|
---|
245 | */
|
---|
246 | PUSBDEVICE USBProxyServiceWindows::getDevices(void)
|
---|
247 | {
|
---|
248 | PUSBDEVICE pDevices = NULL;
|
---|
249 | uint32_t cDevices = 0;
|
---|
250 |
|
---|
251 | Log(("USBProxyServiceWindows::getDevices\n"));
|
---|
252 | USBLibGetDevices(&pDevices, &cDevices);
|
---|
253 | return pDevices;
|
---|
254 | }
|
---|
255 |
|
---|