VirtualBox

source: vbox/trunk/src/VBox/Main/include/USBProxyService.h@ 3719

Last change on this file since 3719 was 3668, checked in by vboxsync, 18 years ago

replace underscore symbols in Main/

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/* $Id: USBProxyService.h 3668 2007-07-17 08:56: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 */
32class USBProxyService
33{
34public:
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 virtual void *insertFilter (IUSBDeviceFilter *aFilter);
45
46 /**
47 * A filter was removed.
48 *
49 * @param aID ID of the filter to remove
50 */
51 virtual void removeFilter (void *aID);
52
53 /**
54 * A VM is trying to capture a device, do necessary preperations.
55 *
56 * @returns VBox status code.
57 * @param aDevice The device in question.
58 */
59 virtual int captureDevice (HostUSBDevice *aDevice);
60
61 /**
62 * The device is to be held so that the host OS will not start using it.
63 *
64 * @returns VBox status code.
65 * @param aDevice The device in question.
66 */
67 virtual int holdDevice (HostUSBDevice *aDevice);
68
69 /**
70 * The device is going to be detached from a VM.
71 *
72 * @param aDevice The device in question.
73 */
74 virtual void detachingDevice (HostUSBDevice *aDevice);
75
76 /**
77 * A VM is releasing a device back to the host.
78 *
79 * @returns VBox status code.
80 * @param aDevice The device in question.
81 */
82 virtual int releaseDevice (HostUSBDevice *aDevice);
83
84 /**
85 * Updates the device state.
86 * This is responsible for calling HostUSBDevice::updateState() and check for async completion.
87 *
88 * @returns true if there is a state change.
89 * @param aDevice The device in question.
90 * @param aUSBDevice The USB device structure for the last enumeration.
91 */
92 virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
93
94 /**
95 * Add device notification hook for the OS specific code.
96 *
97 * @param aDevice The device in question.
98 * @param aUSBDevice The USB device structure.
99 */
100 virtual void deviceAdded (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
101
102 /**
103 * Remove device notification hook for the OS specific code.
104 *
105 * @param aDevice The device in question.
106 */
107 virtual void deviceRemoved (HostUSBDevice *aDevice);
108
109 /**
110 * Query if the service is active and working.
111 *
112 * @returns true if the service is up running.
113 * @returns false if the service isn't running.
114 */
115 bool isActive (void);
116
117 /**
118 * Get last error.
119 * Can be used to check why the proxy !isActive() upon construction.
120 *
121 * @returns VBox status code.
122 */
123 int getLastError (void);
124
125 /**
126 * Calculate the hash of the serial string.
127 *
128 * 64bit FNV1a, chosen because it is designed to hash in to a power of two
129 * space, and is much quicker and simpler than, say, a half MD4.
130 *
131 * @returns the hash.
132 * @param aSerial The serial string.
133 */
134 static uint64_t calcSerialHash (const char *aSerial);
135
136protected:
137
138 /**
139 * Starts the service.
140 *
141 * @returns VBox status.
142 */
143 int start (void);
144
145 /**
146 * Stops the service.
147 *
148 * @returns VBox status.
149 */
150 int stop (void);
151
152 /**
153 * Wait for a change in the USB devices attached to the host.
154 *
155 * @returns VBox status (ignored).
156 * @param aMillies Number of milliseconds to wait.
157 */
158 virtual int wait (unsigned aMillies);
159
160 /**
161 * Interrupt any wait() call in progress.
162 *
163 * @returns VBox status.
164 */
165 virtual int interruptWait (void);
166
167 /**
168 * Get a list of USB device currently attached to the host.
169 *
170 * @returns Pointer to a list of USB devices.
171 * The list nodes are freed individually by calling freeDevice().
172 */
173 virtual PUSBDEVICE getDevices (void);
174
175 /**
176 * First call made on the service thread, use it to do
177 * thread initialization.
178 */
179 virtual void serviceThreadInit (void);
180
181 /**
182 * First call made on the service thread, use it to do
183 * thread termination.
184 */
185 virtual void serviceThreadTerm (void);
186
187 /**
188 * Implement fake capture, ++.
189 *
190 * @returns true if there is a state change.
191 * @param pDevice The device in question.
192 * @param pUSBDevice The USB device structure for the last enumeration.
193 */
194 bool updateDeviceStateFake (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
195
196
197public:
198 /**
199 * Free all the members of a USB device returned by getDevice().
200 *
201 * @param pDevice Pointer to the device.
202 */
203 static void freeDeviceMembers (PUSBDEVICE pDevice);
204
205 /**
206 * Free one USB device returned by getDevice().
207 *
208 * @param pDevice Pointer to the device.
209 */
210 static void freeDevice (PUSBDEVICE pDevice);
211
212private:
213 /**
214 * Process any relevant changes in the attached USB devices.
215 */
216 void processChanges (void);
217
218 /**
219 * The service thread created by start().
220 *
221 * @param Thread The thread handle.
222 * @param pvUser Pointer to the USBProxyService instance.
223 */
224 static DECLCALLBACK (int) serviceThread (RTTHREAD Thread, void *pvUser);
225
226protected:
227 /** Pointer to the Host object. */
228 Host *mHost;
229 /** Thread handle of the service thread. */
230 RTTHREAD mThread;
231 /** Flag which stop() sets to cause serviceThread to return. */
232 bool volatile mTerminate;
233 /** List of smart HostUSBDevice pointers. */
234 typedef std::list <ComObjPtr <HostUSBDevice> > HostUSBDeviceList;
235 /** List of the known USB devices. */
236 HostUSBDeviceList mDevices;
237 /** VBox status code of the last failure.
238 * (Only used by start(), stop() and the child constructors.) */
239 int mLastError;
240};
241
242
243#ifdef VBOX_WITH_USB
244
245# ifdef RT_OS_DARWIN
246# include <VBox/param.h>
247# undef PAGE_SHIFT
248# undef PAGE_SIZE
249# define OSType Carbon_OSType
250# include <Carbon/Carbon.h>
251# undef OSType
252
253/**
254 * The Darwin hosted USB Proxy Service.
255 */
256class USBProxyServiceDarwin : public USBProxyService
257{
258public:
259 USBProxyServiceDarwin (Host *aHost);
260 ~USBProxyServiceDarwin();
261
262 virtual int captureDevice (HostUSBDevice *aDevice);
263 virtual int holdDevice (HostUSBDevice *aDevice);
264 virtual void detachingDevice (HostUSBDevice *aDevice);
265 virtual int releaseDevice (HostUSBDevice *aDevice);
266 virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
267
268protected:
269 virtual int wait (unsigned aMillies);
270 virtual int interruptWait (void);
271 virtual PUSBDEVICE getDevices (void);
272 virtual void serviceThreadInit (void);
273 virtual void serviceThreadTerm (void);
274
275private:
276 /** Reference to the runloop of the service thread.
277 * This is NULL if the service thread isn't running. */
278 CFRunLoopRef mServiceRunLoopRef;
279 /** The opaque value returned by DarwinSubscribeUSBNotifications. */
280 void *mNotifyOpaque;
281 /** A hack to work around the problem with the usb device enumeration
282 * not including newly attached devices. */
283 bool mWaitABitNextTime;
284 /** Whether we've got a fake async event and should return without entering the runloop. */
285 bool volatile mFakeAsync;
286};
287# endif /* RT_OS_DARWIN */
288
289
290# ifdef RT_OS_LINUX
291# include <stdio.h>
292
293/**
294 * The Linux hosted USB Proxy Service.
295 */
296class USBProxyServiceLinux : public USBProxyService
297{
298public:
299 USBProxyServiceLinux (Host *aHost, const char *aUsbfsRoot = "/proc/bus/usb");
300 ~USBProxyServiceLinux();
301
302 virtual int captureDevice (HostUSBDevice *aDevice);
303 virtual int holdDevice (HostUSBDevice *aDevice);
304 virtual int releaseDevice (HostUSBDevice *aDevice);
305 virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
306 virtual void deviceAdded (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
307
308protected:
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
314private:
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 /* RT_OS_LINUX */
329
330
331# ifdef RT_OS_WINDOWS
332/**
333 * The Win32 hosted USB Proxy Service.
334 */
335class USBProxyServiceWin32 : public USBProxyService
336{
337public:
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 bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice);
348
349protected:
350 virtual int wait (unsigned aMillies);
351 virtual int interruptWait (void);
352 virtual PUSBDEVICE getDevices (void);
353
354private:
355
356 HANDLE hEventInterrupt;
357};
358# endif /* RT_OS_WINDOWS */
359
360#endif /* VBOX_WITH_USB */
361
362
363#endif /* !____H_USBPROXYSERVICE */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette