1 | /** @file
|
---|
2 | * VirtualBox USB Proxy Service (base) class.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * If you received this file as part of a commercial VirtualBox
|
---|
17 | * distribution, then only the terms of your commercial VirtualBox
|
---|
18 | * license agreement apply instead of the previous paragraph.
|
---|
19 | */
|
---|
20 |
|
---|
21 |
|
---|
22 | #ifndef ____H_USBPROXYSERVICE
|
---|
23 | #define ____H_USBPROXYSERVICE
|
---|
24 |
|
---|
25 | #include "HostImpl.h"
|
---|
26 | #include "HostUSBDeviceImpl.h"
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Base class for the USB Proxy service.
|
---|
30 | */
|
---|
31 | class USBProxyService
|
---|
32 | {
|
---|
33 | public:
|
---|
34 | USBProxyService (Host *aHost);
|
---|
35 | virtual ~USBProxyService();
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * A filter was inserted / loaded.
|
---|
39 | *
|
---|
40 | * @param aFilter Pointer to the inserted filter.
|
---|
41 | * @return ID of the inserted filter
|
---|
42 | */
|
---|
43 | virtual void *insertFilter (IUSBDeviceFilter *aFilter);
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * A filter was removed.
|
---|
47 | *
|
---|
48 | * @param aID ID of the filter to remove
|
---|
49 | */
|
---|
50 | virtual void removeFilter (void *aID);
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * A VM is trying to capture a device, do necessary preperations.
|
---|
54 | *
|
---|
55 | * @returns VBox status code.
|
---|
56 | * @param pDevice The device in question.
|
---|
57 | */
|
---|
58 | virtual int captureDevice (HostUSBDevice *pDevice);
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * The device is to be held so that the host OS will not start using it.
|
---|
62 | *
|
---|
63 | * @returns VBox status code.
|
---|
64 | * @param pDevice The device in question.
|
---|
65 | */
|
---|
66 | virtual int holdDevice (HostUSBDevice *pDevice);
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * A VM is releasing a device back to the host.
|
---|
70 | *
|
---|
71 | * @returns VBox status code.
|
---|
72 | * @param pDevice The device in question.
|
---|
73 | */
|
---|
74 | virtual int releaseDevice (HostUSBDevice *pDevice);
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * A VM is releaseing a device back to be held or assigned to another VM.
|
---|
78 | * A port reset should be performed.
|
---|
79 | *
|
---|
80 | * @returns VBox status code.
|
---|
81 | * @param pDevice The device in question.
|
---|
82 | */
|
---|
83 | virtual int resetDevice (HostUSBDevice *pDevice);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Updates the device state.
|
---|
87 | * This is responsible for calling HostUSBDevice::updateState() and check for async completion.
|
---|
88 | *
|
---|
89 | * @returns true if there is a state change.
|
---|
90 | * @param pDevice The device in question.
|
---|
91 | * @param pUSBDevice The USB device structure for the last enumeration.
|
---|
92 | */
|
---|
93 | virtual bool updateDeviceState (HostUSBDevice *pDevice, PUSBDEVICE pUSBDevice);
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Query if the service is active and working.
|
---|
97 | *
|
---|
98 | * @returns true if the service is up running.
|
---|
99 | * @returns false if the service isn't running.
|
---|
100 | */
|
---|
101 | bool isActive (void);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Get last error.
|
---|
105 | * Can be used to check why the proxy !isActive() upon construction.
|
---|
106 | *
|
---|
107 | * @returns VBox status code.
|
---|
108 | */
|
---|
109 | int getLastError (void);
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Calculate the hash of the serial string.
|
---|
113 | *
|
---|
114 | * 64bit FNV1a, chosen because it is designed to hash in to a power of two
|
---|
115 | * space, and is much quicker and simpler than, say, a half MD4.
|
---|
116 | *
|
---|
117 | * @returns the hash.
|
---|
118 | * @param aSerial The serial string.
|
---|
119 | */
|
---|
120 | static uint64_t calcSerialHash (const char *aSerial);
|
---|
121 |
|
---|
122 | protected:
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Starts the service.
|
---|
126 | *
|
---|
127 | * @returns VBox status.
|
---|
128 | */
|
---|
129 | int start (void);
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Stops the service.
|
---|
133 | *
|
---|
134 | * @returns VBox status.
|
---|
135 | */
|
---|
136 | int stop (void);
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Wait for a change in the USB devices attached to the host.
|
---|
140 | *
|
---|
141 | * @returns VBox status (ignored).
|
---|
142 | * @param aMillies Number of milliseconds to wait.
|
---|
143 | */
|
---|
144 | virtual int wait (unsigned aMillies);
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Interrupt any wait() call in progress.
|
---|
148 | *
|
---|
149 | * @returns VBox status.
|
---|
150 | */
|
---|
151 | virtual int interruptWait (void);
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Get a list of USB device currently attached to the host.
|
---|
155 | *
|
---|
156 | * @returns Pointer to a list of USB devices.
|
---|
157 | * The list nodes are freed individually by calling freeDevice().
|
---|
158 | */
|
---|
159 | virtual PUSBDEVICE getDevices (void);
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * First call made on the service thread, use it to do
|
---|
163 | * thread initialization.
|
---|
164 | */
|
---|
165 | virtual void serviceThreadInit (void);
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * First call made on the service thread, use it to do
|
---|
169 | * thread termination.
|
---|
170 | */
|
---|
171 | virtual void serviceThreadTerm (void);
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Implement fake capture, ++.
|
---|
175 | *
|
---|
176 | * @returns true if there is a state change.
|
---|
177 | * @param pDevice The device in question.
|
---|
178 | * @param pUSBDevice The USB device structure for the last enumeration.
|
---|
179 | */
|
---|
180 | bool updateDeviceStateFake (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
181 |
|
---|
182 |
|
---|
183 | public:
|
---|
184 | /**
|
---|
185 | * Free one USB device returned by getDevice().
|
---|
186 | *
|
---|
187 | * @param pDevice Pointer to the device.
|
---|
188 | */
|
---|
189 | static void freeDevice (PUSBDEVICE pDevice);
|
---|
190 |
|
---|
191 | private:
|
---|
192 | /**
|
---|
193 | * Process any relevant changes in the attached USB devices.
|
---|
194 | */
|
---|
195 | void processChanges (void);
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * The service thread created by start().
|
---|
199 | *
|
---|
200 | * @param Thread The thread handle.
|
---|
201 | * @param pvUser Pointer to the USBProxyService instance.
|
---|
202 | */
|
---|
203 | static DECLCALLBACK (int) serviceThread (RTTHREAD Thread, void *pvUser);
|
---|
204 |
|
---|
205 | protected:
|
---|
206 | /** Pointer to the Host object. */
|
---|
207 | Host *mHost;
|
---|
208 | /** Thread handle of the service thread. */
|
---|
209 | RTTHREAD mThread;
|
---|
210 | /** Flag which stop() sets to cause serviceThread to return. */
|
---|
211 | bool volatile mTerminate;
|
---|
212 | /** List of smart HostUSBDevice pointers. */
|
---|
213 | typedef std::list <ComObjPtr <HostUSBDevice> > HostUSBDeviceList;
|
---|
214 | /** List of the known USB devices. */
|
---|
215 | HostUSBDeviceList mDevices;
|
---|
216 | /** VBox status code of the last failure.
|
---|
217 | * (Only used by start(), stop() and the child constructors.) */
|
---|
218 | int mLastError;
|
---|
219 | };
|
---|
220 |
|
---|
221 |
|
---|
222 | #ifdef VBOX_WITH_USB
|
---|
223 |
|
---|
224 | # ifdef __DARWIN__
|
---|
225 | # include <VBox/param.h>
|
---|
226 | # undef PAGE_SHIFT
|
---|
227 | # undef PAGE_SIZE
|
---|
228 | # define OSType Carbon_OSType
|
---|
229 | # include <Carbon/Carbon.h>
|
---|
230 | # undef OSType
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * The Darwin hosted USB Proxy Service.
|
---|
234 | */
|
---|
235 | class USBProxyServiceDarwin : public USBProxyService
|
---|
236 | {
|
---|
237 | public:
|
---|
238 | USBProxyServiceDarwin (Host *aHost);
|
---|
239 | ~USBProxyServiceDarwin();
|
---|
240 |
|
---|
241 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
242 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
243 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
244 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
245 | virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
246 |
|
---|
247 | protected:
|
---|
248 | virtual int wait (unsigned aMillies);
|
---|
249 | virtual int interruptWait (void);
|
---|
250 | virtual PUSBDEVICE getDevices (void);
|
---|
251 | virtual void serviceThreadInit (void);
|
---|
252 | virtual void serviceThreadTerm (void);
|
---|
253 |
|
---|
254 | private:
|
---|
255 | /** Reference to the runloop of the service thread.
|
---|
256 | * This is NULL if the service thread isn't running. */
|
---|
257 | CFRunLoopRef mServiceRunLoopRef;
|
---|
258 | /** The opaque value returned by DarwinSubscribeUSBNotifications. */
|
---|
259 | void *mNotifyOpaque;
|
---|
260 | /** A hack to work around the problem with the usb device enumeration
|
---|
261 | * not including newly attached devices. */
|
---|
262 | bool mWaitABitNextTime;
|
---|
263 | };
|
---|
264 | # endif /* __DARWIN__ */
|
---|
265 |
|
---|
266 |
|
---|
267 | # ifdef __LINUX__
|
---|
268 | # include <stdio.h>
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * The Linux hosted USB Proxy Service.
|
---|
272 | */
|
---|
273 | class USBProxyServiceLinux : public USBProxyService
|
---|
274 | {
|
---|
275 | public:
|
---|
276 | USBProxyServiceLinux (Host *aHost, const char *aUsbfsRoot = "/proc/bus/usb");
|
---|
277 | ~USBProxyServiceLinux();
|
---|
278 |
|
---|
279 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
280 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
281 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
282 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
283 | virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
284 |
|
---|
285 | protected:
|
---|
286 | virtual int wait (unsigned aMillies);
|
---|
287 | virtual int interruptWait (void);
|
---|
288 | virtual PUSBDEVICE getDevices (void);
|
---|
289 |
|
---|
290 | private:
|
---|
291 | /** File handle to the '/proc/bus/usb/devices' file. */
|
---|
292 | RTFILE mFile;
|
---|
293 | /** Stream for mFile. */
|
---|
294 | FILE *mStream;
|
---|
295 | /** Pipe used to interrupt wait(), the read end. */
|
---|
296 | RTFILE mWakeupPipeR;
|
---|
297 | /** Pipe used to interrupt wait(), the write end. */
|
---|
298 | RTFILE mWakeupPipeW;
|
---|
299 | /** The root of usbfs. */
|
---|
300 | Utf8Str mUsbfsRoot;
|
---|
301 | };
|
---|
302 | # endif /* __LINUX__ */
|
---|
303 |
|
---|
304 |
|
---|
305 | # ifdef __WIN__
|
---|
306 | /**
|
---|
307 | * The Win32 hosted USB Proxy Service.
|
---|
308 | */
|
---|
309 | class USBProxyServiceWin32 : public USBProxyService
|
---|
310 | {
|
---|
311 | public:
|
---|
312 | USBProxyServiceWin32 (Host *aHost);
|
---|
313 | ~USBProxyServiceWin32();
|
---|
314 |
|
---|
315 | virtual void *insertFilter (IUSBDeviceFilter *aFilter);
|
---|
316 | virtual void removeFilter (void *aID);
|
---|
317 |
|
---|
318 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
319 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
320 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
321 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
322 | virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
323 |
|
---|
324 | protected:
|
---|
325 | virtual int wait (unsigned aMillies);
|
---|
326 | virtual int interruptWait (void);
|
---|
327 | virtual PUSBDEVICE getDevices (void);
|
---|
328 |
|
---|
329 | private:
|
---|
330 |
|
---|
331 | HANDLE hEventInterrupt;
|
---|
332 | };
|
---|
333 | # endif /* __WIN__ */
|
---|
334 |
|
---|
335 | #endif /* VBOX_WITH_USB */
|
---|
336 |
|
---|
337 |
|
---|
338 | #endif /* !____H_USBPROXYSERVICE */
|
---|