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