1 | /** $Id: USBLib-solaris.cpp 37151 2011-05-19 12:12:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * USBLib - Library for wrapping up the VBoxUSB functionality, Solaris flavor.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 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 <VBox/usblib.h>
|
---|
23 | #include <VBox/err.h>
|
---|
24 | #include <VBox/log.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/asm.h>
|
---|
27 | #include <iprt/file.h>
|
---|
28 | #include <iprt/mem.h>
|
---|
29 | #include <iprt/process.h>
|
---|
30 | #include <iprt/env.h>
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 |
|
---|
34 | # include <sys/types.h>
|
---|
35 | # include <sys/stat.h>
|
---|
36 | # include <errno.h>
|
---|
37 | # include <unistd.h>
|
---|
38 | # include <string.h>
|
---|
39 | # include <limits.h>
|
---|
40 | # include <strings.h>
|
---|
41 |
|
---|
42 | /** -XXX- Remove this hackery eventually */
|
---|
43 | #ifdef DEBUG_ramshankar
|
---|
44 | # undef Log
|
---|
45 | # undef LogFlow
|
---|
46 | # define Log LogRel
|
---|
47 | # define LogFlow LogRel
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Defined Constants And Macros *
|
---|
52 | *******************************************************************************/
|
---|
53 | /** Logging class. */
|
---|
54 | #define USBLIBR3 "USBLibR3"
|
---|
55 |
|
---|
56 |
|
---|
57 | /*******************************************************************************
|
---|
58 | * Global Variables *
|
---|
59 | *******************************************************************************/
|
---|
60 | /** Reference counter. */
|
---|
61 | static uint32_t volatile g_cUsers = 0;
|
---|
62 | /** VBoxUSB Device handle. */
|
---|
63 | static RTFILE g_File = NIL_RTFILE;
|
---|
64 | /** List of tasks handled by the USB helper. */
|
---|
65 | typedef enum USBHELPER_OP
|
---|
66 | {
|
---|
67 | ADD_ALIAS = 0,
|
---|
68 | DEL_ALIAS,
|
---|
69 | RESET
|
---|
70 | };
|
---|
71 |
|
---|
72 | /*******************************************************************************
|
---|
73 | * Internal Functions *
|
---|
74 | *******************************************************************************/
|
---|
75 | static int usblibDoIOCtl(unsigned iFunction, void *pvData, size_t cbData);
|
---|
76 |
|
---|
77 |
|
---|
78 | USBLIB_DECL(int) USBLibInit(void)
|
---|
79 | {
|
---|
80 | LogFlow((USBLIBR3 ":USBLibInit\n"));
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Already open?
|
---|
84 | * This isn't properly serialized, but we'll be fine with the current usage.
|
---|
85 | */
|
---|
86 | if (g_cUsers)
|
---|
87 | {
|
---|
88 | ASMAtomicIncU32(&g_cUsers);
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 |
|
---|
92 | RTFILE File;
|
---|
93 | int rc = RTFileOpen(&File, VBOXUSB_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
94 | if (RT_FAILURE(rc))
|
---|
95 | {
|
---|
96 | LogRel((USBLIBR3 ":RTFileOpen failed to open VBoxUSB device.rc=%d\n", rc));
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 | g_File = File;
|
---|
100 |
|
---|
101 | ASMAtomicIncU32(&g_cUsers);
|
---|
102 | /*
|
---|
103 | * Check the USBMonitor version.
|
---|
104 | */
|
---|
105 | VBOXUSBREQ_GET_VERSION Req;
|
---|
106 | bzero(&Req, sizeof(Req));
|
---|
107 | rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_GET_VERSION, &Req, sizeof(Req));
|
---|
108 | if (RT_SUCCESS(rc))
|
---|
109 | {
|
---|
110 | if ( Req.u32Major != VBOXUSBMON_VERSION_MAJOR
|
---|
111 | || Req.u32Minor < VBOXUSBMON_VERSION_MINOR)
|
---|
112 | {
|
---|
113 | rc = VERR_VERSION_MISMATCH;
|
---|
114 | LogRel((USBLIBR3 ":USBMonitor version mismatch! driver v%d.%d, expecting ~v%d.%d\n",
|
---|
115 | Req.u32Major, Req.u32Minor, VBOXUSBMON_VERSION_MAJOR, VBOXUSBMON_VERSION_MINOR));
|
---|
116 |
|
---|
117 | RTFileClose(File);
|
---|
118 | g_File = NIL_RTFILE;
|
---|
119 | ASMAtomicDecU32(&g_cUsers);
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | else
|
---|
124 | {
|
---|
125 | LogRel((USBLIBR3 ":USBMonitor driver version query failed. rc=%Rrc\n", rc));
|
---|
126 | RTFileClose(File);
|
---|
127 | g_File = NIL_RTFILE;
|
---|
128 | ASMAtomicDecU32(&g_cUsers);
|
---|
129 | return rc;
|
---|
130 | }
|
---|
131 |
|
---|
132 | return VINF_SUCCESS;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | USBLIB_DECL(int) USBLibTerm(void)
|
---|
137 | {
|
---|
138 | LogFlow((USBLIBR3 ":USBLibTerm\n"));
|
---|
139 |
|
---|
140 | if (!g_cUsers)
|
---|
141 | return VERR_WRONG_ORDER;
|
---|
142 | if (ASMAtomicDecU32(&g_cUsers) != 0)
|
---|
143 | return VINF_SUCCESS;
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * We're the last guy, close down the connection.
|
---|
147 | */
|
---|
148 | RTFILE File = g_File;
|
---|
149 | g_File = NIL_RTFILE;
|
---|
150 | if (File == NIL_RTFILE)
|
---|
151 | return VERR_INTERNAL_ERROR;
|
---|
152 |
|
---|
153 | int rc = RTFileClose(File);
|
---|
154 | AssertRC(rc);
|
---|
155 | return rc;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | USBLIB_DECL(void *) USBLibAddFilter(PCUSBFILTER pFilter)
|
---|
160 | {
|
---|
161 | LogFlow((USBLIBR3 ":USBLibAddFilter pFilter=%p\n", pFilter));
|
---|
162 |
|
---|
163 | VBOXUSBREQ_ADD_FILTER Req;
|
---|
164 | Req.Filter = *pFilter;
|
---|
165 | Req.uId = 0;
|
---|
166 |
|
---|
167 | int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_ADD_FILTER, &Req, sizeof(Req));
|
---|
168 | if (RT_SUCCESS(rc))
|
---|
169 | return (void *)Req.uId;
|
---|
170 |
|
---|
171 | AssertMsgFailed((USBLIBR3 ":VBOXUSBMON_IOCTL_ADD_FILTER failed! rc=%Rrc\n", rc));
|
---|
172 | return NULL;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | USBLIB_DECL(void) USBLibRemoveFilter(void *pvId)
|
---|
177 | {
|
---|
178 | LogFlow((USBLIBR3 ":USBLibRemoveFilter pvId=%p\n", pvId));
|
---|
179 |
|
---|
180 | VBOXUSBREQ_REMOVE_FILTER Req;
|
---|
181 | Req.uId = (uintptr_t)pvId;
|
---|
182 |
|
---|
183 | int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_REMOVE_FILTER, &Req, sizeof(Req));
|
---|
184 | if (RT_SUCCESS(rc))
|
---|
185 | return;
|
---|
186 |
|
---|
187 | AssertMsgFailed((USBLIBR3 ":VBOXUSBMON_IOCTL_REMOVE_FILTER failed! rc=%Rrc\n", rc));
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | USBLIB_DECL(int) USBLibGetClientInfo(char *pszDeviceIdent, char **ppszClientPath, int *pInstance)
|
---|
192 | {
|
---|
193 | LogFlow((USBLIBR3 ":USBLibGetClientInfo pszDeviceIdent=%s ppszClientPath=%p pInstance=%p\n",
|
---|
194 | pszDeviceIdent, ppszClientPath, pInstance));
|
---|
195 |
|
---|
196 | AssertPtrReturn(pInstance, VERR_INVALID_PARAMETER);
|
---|
197 | AssertPtrReturn(ppszClientPath, VERR_INVALID_PARAMETER);
|
---|
198 | AssertPtrReturn(pszDeviceIdent, VERR_INVALID_PARAMETER);
|
---|
199 |
|
---|
200 | VBOXUSBREQ_CLIENT_INFO Req;
|
---|
201 | bzero(&Req, sizeof(Req));
|
---|
202 | RTStrPrintf(Req.szDeviceIdent, sizeof(Req.szDeviceIdent), "%s", pszDeviceIdent);
|
---|
203 |
|
---|
204 | int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_CLIENT_INFO, &Req, sizeof(Req));
|
---|
205 | if (RT_SUCCESS(rc))
|
---|
206 | {
|
---|
207 | *pInstance = Req.Instance;
|
---|
208 | rc = RTStrDupEx(ppszClientPath, Req.szClientPath);
|
---|
209 | if (RT_SUCCESS(rc))
|
---|
210 | return VINF_SUCCESS;
|
---|
211 |
|
---|
212 | LogRel((USBLIBR3 ":USBLibGetClientInfo RTStrDupEx failed! rc=%Rrc szClientPath=%s\n", rc, Req.szClientPath));
|
---|
213 | }
|
---|
214 | else
|
---|
215 | LogRel((USBLIBR3 ":USBLibGetClientInfo VBOXUSBMON_IOCTL_CLIENTPATH failed! rc=%Rrc\n", rc));
|
---|
216 |
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | USBLIB_DECL(int) USBLibResetDevice(char *pszDevicePath, bool fReattach)
|
---|
222 | {
|
---|
223 | LogFlow((USBLIBR3 ":USBLibResetDevice pszDevicePath=%s\n", pszDevicePath));
|
---|
224 |
|
---|
225 | size_t cbReq = sizeof(VBOXUSBREQ_RESET_DEVICE) + strlen(pszDevicePath);
|
---|
226 | VBOXUSBREQ_RESET_DEVICE *pReq = (VBOXUSBREQ_RESET_DEVICE *)RTMemTmpAllocZ(cbReq);
|
---|
227 | if (RT_UNLIKELY(!pReq))
|
---|
228 | return VERR_NO_MEMORY;
|
---|
229 |
|
---|
230 | pReq->fReattach = fReattach;
|
---|
231 | strcpy(pReq->szDevicePath, pszDevicePath);
|
---|
232 |
|
---|
233 | int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_RESET_DEVICE, pReq, cbReq);
|
---|
234 | if (RT_FAILURE(rc))
|
---|
235 | LogRel((USBLIBR3 ":VBOXUSBMON_IOCTL_RESET_DEVICE failed! rc=%Rrc\n", rc));
|
---|
236 |
|
---|
237 | RTMemFree(pReq);
|
---|
238 | return rc;
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | static int usblibDoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
|
---|
243 | {
|
---|
244 | if (g_File == NIL_RTFILE)
|
---|
245 | {
|
---|
246 | LogRel((USBLIBR3 ":IOCtl failed, device not open.\n"));
|
---|
247 | return VERR_FILE_NOT_FOUND;
|
---|
248 | }
|
---|
249 |
|
---|
250 | VBOXUSBREQ Hdr;
|
---|
251 | Hdr.u32Magic = VBOXUSBMON_MAGIC;
|
---|
252 | Hdr.cbData = cbData; /* Don't include full size because the header size is fixed. */
|
---|
253 | Hdr.pvDataR3 = pvData;
|
---|
254 |
|
---|
255 | int rc = ioctl((int)g_File, iFunction, &Hdr);
|
---|
256 | if (rc < 0)
|
---|
257 | {
|
---|
258 | rc = errno;
|
---|
259 | LogRel((USBLIBR3 ":IOCtl failed iFunction=%x errno=%d g_file=%d\n", iFunction, rc, (int)g_File));
|
---|
260 | return RTErrConvertFromErrno(rc);
|
---|
261 | }
|
---|
262 |
|
---|
263 | rc = Hdr.rc;
|
---|
264 | if (RT_UNLIKELY(RT_FAILURE(rc)))
|
---|
265 | LogRel((USBLIBR3 ":Function (%x) failed. rc=%Rrc\n", iFunction, rc));
|
---|
266 |
|
---|
267 | return rc;
|
---|
268 | }
|
---|