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 aDevice The device in question.
|
---|
91 | * @param aUSBDevice The USB device structure for the last enumeration.
|
---|
92 | */
|
---|
93 | virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Add device notification hook for the OS specific code.
|
---|
97 | *
|
---|
98 | * @param aDevice The device in question.
|
---|
99 | * @param aUSBDevice The USB device structure.
|
---|
100 | */
|
---|
101 | virtual void deviceAdded (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Remove device notification hook for the OS specific code.
|
---|
105 | *
|
---|
106 | * @param aDevice The device in question.
|
---|
107 | */
|
---|
108 | virtual void deviceRemoved (HostUSBDevice *aDevice);
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Query if the service is active and working.
|
---|
112 | *
|
---|
113 | * @returns true if the service is up running.
|
---|
114 | * @returns false if the service isn't running.
|
---|
115 | */
|
---|
116 | bool isActive (void);
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Get last error.
|
---|
120 | * Can be used to check why the proxy !isActive() upon construction.
|
---|
121 | *
|
---|
122 | * @returns VBox status code.
|
---|
123 | */
|
---|
124 | int getLastError (void);
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Calculate the hash of the serial string.
|
---|
128 | *
|
---|
129 | * 64bit FNV1a, chosen because it is designed to hash in to a power of two
|
---|
130 | * space, and is much quicker and simpler than, say, a half MD4.
|
---|
131 | *
|
---|
132 | * @returns the hash.
|
---|
133 | * @param aSerial The serial string.
|
---|
134 | */
|
---|
135 | static uint64_t calcSerialHash (const char *aSerial);
|
---|
136 |
|
---|
137 | protected:
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Starts the service.
|
---|
141 | *
|
---|
142 | * @returns VBox status.
|
---|
143 | */
|
---|
144 | int start (void);
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Stops the service.
|
---|
148 | *
|
---|
149 | * @returns VBox status.
|
---|
150 | */
|
---|
151 | int stop (void);
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Wait for a change in the USB devices attached to the host.
|
---|
155 | *
|
---|
156 | * @returns VBox status (ignored).
|
---|
157 | * @param aMillies Number of milliseconds to wait.
|
---|
158 | */
|
---|
159 | virtual int wait (unsigned aMillies);
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Interrupt any wait() call in progress.
|
---|
163 | *
|
---|
164 | * @returns VBox status.
|
---|
165 | */
|
---|
166 | virtual int interruptWait (void);
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Get a list of USB device currently attached to the host.
|
---|
170 | *
|
---|
171 | * @returns Pointer to a list of USB devices.
|
---|
172 | * The list nodes are freed individually by calling freeDevice().
|
---|
173 | */
|
---|
174 | virtual PUSBDEVICE getDevices (void);
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * First call made on the service thread, use it to do
|
---|
178 | * thread initialization.
|
---|
179 | */
|
---|
180 | virtual void serviceThreadInit (void);
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * First call made on the service thread, use it to do
|
---|
184 | * thread termination.
|
---|
185 | */
|
---|
186 | virtual void serviceThreadTerm (void);
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Implement fake capture, ++.
|
---|
190 | *
|
---|
191 | * @returns true if there is a state change.
|
---|
192 | * @param pDevice The device in question.
|
---|
193 | * @param pUSBDevice The USB device structure for the last enumeration.
|
---|
194 | */
|
---|
195 | bool updateDeviceStateFake (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
196 |
|
---|
197 |
|
---|
198 | public:
|
---|
199 | /**
|
---|
200 | * Free all the members of a USB device returned by getDevice().
|
---|
201 | *
|
---|
202 | * @param pDevice Pointer to the device.
|
---|
203 | */
|
---|
204 | static void freeDeviceMembers (PUSBDEVICE pDevice);
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Free one USB device returned by getDevice().
|
---|
208 | *
|
---|
209 | * @param pDevice Pointer to the device.
|
---|
210 | */
|
---|
211 | static void freeDevice (PUSBDEVICE pDevice);
|
---|
212 |
|
---|
213 | private:
|
---|
214 | /**
|
---|
215 | * Process any relevant changes in the attached USB devices.
|
---|
216 | */
|
---|
217 | void processChanges (void);
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * The service thread created by start().
|
---|
221 | *
|
---|
222 | * @param Thread The thread handle.
|
---|
223 | * @param pvUser Pointer to the USBProxyService instance.
|
---|
224 | */
|
---|
225 | static DECLCALLBACK (int) serviceThread (RTTHREAD Thread, void *pvUser);
|
---|
226 |
|
---|
227 | protected:
|
---|
228 | /** Pointer to the Host object. */
|
---|
229 | Host *mHost;
|
---|
230 | /** Thread handle of the service thread. */
|
---|
231 | RTTHREAD mThread;
|
---|
232 | /** Flag which stop() sets to cause serviceThread to return. */
|
---|
233 | bool volatile mTerminate;
|
---|
234 | /** List of smart HostUSBDevice pointers. */
|
---|
235 | typedef std::list <ComObjPtr <HostUSBDevice> > HostUSBDeviceList;
|
---|
236 | /** List of the known USB devices. */
|
---|
237 | HostUSBDeviceList mDevices;
|
---|
238 | /** VBox status code of the last failure.
|
---|
239 | * (Only used by start(), stop() and the child constructors.) */
|
---|
240 | int mLastError;
|
---|
241 | };
|
---|
242 |
|
---|
243 |
|
---|
244 | #ifdef VBOX_WITH_USB
|
---|
245 |
|
---|
246 | # ifdef __DARWIN__
|
---|
247 | # include <VBox/param.h>
|
---|
248 | # undef PAGE_SHIFT
|
---|
249 | # undef PAGE_SIZE
|
---|
250 | # define OSType Carbon_OSType
|
---|
251 | # include <Carbon/Carbon.h>
|
---|
252 | # undef OSType
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * The Darwin hosted USB Proxy Service.
|
---|
256 | */
|
---|
257 | class USBProxyServiceDarwin : public USBProxyService
|
---|
258 | {
|
---|
259 | public:
|
---|
260 | USBProxyServiceDarwin (Host *aHost);
|
---|
261 | ~USBProxyServiceDarwin();
|
---|
262 |
|
---|
263 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
264 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
265 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
266 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
267 | virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
268 |
|
---|
269 | protected:
|
---|
270 | virtual int wait (unsigned aMillies);
|
---|
271 | virtual int interruptWait (void);
|
---|
272 | virtual PUSBDEVICE getDevices (void);
|
---|
273 | virtual void serviceThreadInit (void);
|
---|
274 | virtual void serviceThreadTerm (void);
|
---|
275 |
|
---|
276 | private:
|
---|
277 | /** Reference to the runloop of the service thread.
|
---|
278 | * This is NULL if the service thread isn't running. */
|
---|
279 | CFRunLoopRef mServiceRunLoopRef;
|
---|
280 | /** The opaque value returned by DarwinSubscribeUSBNotifications. */
|
---|
281 | void *mNotifyOpaque;
|
---|
282 | /** A hack to work around the problem with the usb device enumeration
|
---|
283 | * not including newly attached devices. */
|
---|
284 | bool mWaitABitNextTime;
|
---|
285 | };
|
---|
286 | # endif /* __DARWIN__ */
|
---|
287 |
|
---|
288 |
|
---|
289 | # ifdef __LINUX__
|
---|
290 | # include <stdio.h>
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * The Linux hosted USB Proxy Service.
|
---|
294 | */
|
---|
295 | class USBProxyServiceLinux : public USBProxyService
|
---|
296 | {
|
---|
297 | public:
|
---|
298 | USBProxyServiceLinux (Host *aHost, const char *aUsbfsRoot = "/proc/bus/usb");
|
---|
299 | ~USBProxyServiceLinux();
|
---|
300 |
|
---|
301 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
302 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
303 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
304 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
305 | virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
306 | virtual void deviceAdded (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
307 |
|
---|
308 | protected:
|
---|
309 | virtual int wait (unsigned aMillies);
|
---|
310 | virtual int interruptWait (void);
|
---|
311 | virtual PUSBDEVICE getDevices (void);
|
---|
312 | int addDeviceToChain (PUSBDEVICE pDev, PUSBDEVICE *ppFirst, PUSBDEVICE **pppNext, int rc);
|
---|
313 |
|
---|
314 | private:
|
---|
315 | /** File handle to the '/proc/bus/usb/devices' file. */
|
---|
316 | RTFILE mFile;
|
---|
317 | /** Stream for mFile. */
|
---|
318 | FILE *mStream;
|
---|
319 | /** Pipe used to interrupt wait(), the read end. */
|
---|
320 | RTFILE mWakeupPipeR;
|
---|
321 | /** Pipe used to interrupt wait(), the write end. */
|
---|
322 | RTFILE mWakeupPipeW;
|
---|
323 | /** The root of usbfs. */
|
---|
324 | Utf8Str mUsbfsRoot;
|
---|
325 | /** Number of 500ms polls left to do. See usbDeterminState for details. */
|
---|
326 | unsigned mUdevPolls;
|
---|
327 | };
|
---|
328 | # endif /* __LINUX__ */
|
---|
329 |
|
---|
330 |
|
---|
331 | # ifdef __WIN__
|
---|
332 | /**
|
---|
333 | * The Win32 hosted USB Proxy Service.
|
---|
334 | */
|
---|
335 | class USBProxyServiceWin32 : public USBProxyService
|
---|
336 | {
|
---|
337 | public:
|
---|
338 | USBProxyServiceWin32 (Host *aHost);
|
---|
339 | ~USBProxyServiceWin32();
|
---|
340 |
|
---|
341 | virtual void *insertFilter (IUSBDeviceFilter *aFilter);
|
---|
342 | virtual void removeFilter (void *aID);
|
---|
343 |
|
---|
344 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
345 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
346 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
347 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
348 | virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
|
---|
349 |
|
---|
350 | protected:
|
---|
351 | virtual int wait (unsigned aMillies);
|
---|
352 | virtual int interruptWait (void);
|
---|
353 | virtual PUSBDEVICE getDevices (void);
|
---|
354 |
|
---|
355 | private:
|
---|
356 |
|
---|
357 | HANDLE hEventInterrupt;
|
---|
358 | };
|
---|
359 | # endif /* __WIN__ */
|
---|
360 |
|
---|
361 | #endif /* VBOX_WITH_USB */
|
---|
362 |
|
---|
363 |
|
---|
364 | #endif /* !____H_USBPROXYSERVICE */
|
---|