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