VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/solaris/USBLib-solaris.cpp@ 103242

Last change on this file since 103242 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/** $Id: USBLib-solaris.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * USBLib - Library for wrapping up the VBoxUSB functionality, Solaris flavor.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <VBox/usblib.h>
42#include <VBox/err.h>
43#include <VBox/log.h>
44#include <iprt/assert.h>
45#include <iprt/asm.h>
46#include <iprt/file.h>
47#include <iprt/mem.h>
48#include <iprt/process.h>
49#include <iprt/env.h>
50#include <iprt/path.h>
51#include <iprt/string.h>
52
53# include <sys/types.h>
54# include <sys/stat.h>
55# include <errno.h>
56# include <unistd.h>
57# include <string.h>
58# include <limits.h>
59# include <strings.h>
60
61
62/*********************************************************************************************************************************
63* Defined Constants And Macros *
64*********************************************************************************************************************************/
65/** Logging class. */
66#define USBLIBR3 "USBLibR3"
67
68
69/*********************************************************************************************************************************
70* Global Variables *
71*********************************************************************************************************************************/
72/** Reference counter. */
73static uint32_t volatile g_cUsers = 0;
74/** VBoxUSB Device handle. */
75static RTFILE g_hFile = NIL_RTFILE;
76
77
78/*********************************************************************************************************************************
79* Internal Functions *
80*********************************************************************************************************************************/
81static int usblibDoIOCtl(unsigned iFunction, void *pvData, size_t cbData);
82
83
84USBLIB_DECL(int) USBLibInit(void)
85{
86 LogFlow((USBLIBR3 ":USBLibInit\n"));
87
88 /*
89 * Already open?
90 * This isn't properly serialized, but we'll be fine with the current usage.
91 */
92 if (g_cUsers)
93 {
94 ASMAtomicIncU32(&g_cUsers);
95 return VINF_SUCCESS;
96 }
97
98 RTFILE File;
99 int rc = RTFileOpen(&File, VBOXUSB_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
100 if (RT_FAILURE(rc))
101 {
102 LogRel((USBLIBR3 ":failed to open the VBoxUSB monitor device node '%s' rc=%Rrc\n", VBOXUSB_DEVICE_NAME, rc));
103 return rc;
104 }
105 g_hFile = File;
106
107 ASMAtomicIncU32(&g_cUsers);
108 /*
109 * Check the USBMonitor version.
110 */
111 VBOXUSBREQ_GET_VERSION Req;
112 bzero(&Req, sizeof(Req));
113 rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_GET_VERSION, &Req, sizeof(Req));
114 if (RT_SUCCESS(rc))
115 {
116 if ( Req.u32Major != VBOXUSBMON_VERSION_MAJOR
117 || Req.u32Minor < VBOXUSBMON_VERSION_MINOR)
118 {
119 rc = VERR_VERSION_MISMATCH;
120 LogRel((USBLIBR3 ":USBMonitor version mismatch! driver v%d.%d, expecting ~v%d.%d\n",
121 Req.u32Major, Req.u32Minor, VBOXUSBMON_VERSION_MAJOR, VBOXUSBMON_VERSION_MINOR));
122
123 RTFileClose(File);
124 g_hFile = NIL_RTFILE;
125 ASMAtomicDecU32(&g_cUsers);
126 return rc;
127 }
128 }
129 else
130 {
131 LogRel((USBLIBR3 ":USBMonitor driver version query failed. rc=%Rrc\n", rc));
132 RTFileClose(File);
133 g_hFile = NIL_RTFILE;
134 ASMAtomicDecU32(&g_cUsers);
135 return rc;
136 }
137
138 return VINF_SUCCESS;
139}
140
141
142USBLIB_DECL(int) USBLibTerm(void)
143{
144 LogFlow((USBLIBR3 ":USBLibTerm\n"));
145
146 if (!g_cUsers)
147 return VERR_WRONG_ORDER;
148 if (ASMAtomicDecU32(&g_cUsers) != 0)
149 return VINF_SUCCESS;
150
151 /*
152 * We're the last guy, close down the connection.
153 */
154 RTFILE File = g_hFile;
155 g_hFile = NIL_RTFILE;
156 if (File == NIL_RTFILE)
157 return VERR_INTERNAL_ERROR;
158
159 int rc = RTFileClose(File);
160 AssertRC(rc);
161 return rc;
162}
163
164
165USBLIB_DECL(void *) USBLibAddFilter(PCUSBFILTER pFilter)
166{
167 LogFlow((USBLIBR3 ":USBLibAddFilter pFilter=%p\n", pFilter));
168
169 VBOXUSBREQ_ADD_FILTER Req;
170 Req.Filter = *pFilter;
171 Req.uId = 0;
172
173 int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_ADD_FILTER, &Req, sizeof(Req));
174 if (RT_SUCCESS(rc))
175 return (void *)Req.uId;
176
177 AssertMsgFailed((USBLIBR3 ":VBOXUSBMON_IOCTL_ADD_FILTER failed! rc=%Rrc\n", rc));
178 return NULL;
179}
180
181
182USBLIB_DECL(void) USBLibRemoveFilter(void *pvId)
183{
184 LogFlow((USBLIBR3 ":USBLibRemoveFilter pvId=%p\n", pvId));
185
186 VBOXUSBREQ_REMOVE_FILTER Req;
187 Req.uId = (uintptr_t)pvId;
188
189 int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_REMOVE_FILTER, &Req, sizeof(Req));
190 if (RT_SUCCESS(rc))
191 return;
192
193 AssertMsgFailed((USBLIBR3 ":VBOXUSBMON_IOCTL_REMOVE_FILTER failed! rc=%Rrc\n", rc));
194}
195
196
197USBLIB_DECL(int) USBLibGetClientInfo(char *pszDeviceIdent, char **ppszClientPath, int *pInstance)
198{
199 LogFlow((USBLIBR3 ":USBLibGetClientInfo pszDeviceIdent=%s ppszClientPath=%p pInstance=%p\n",
200 pszDeviceIdent, ppszClientPath, pInstance));
201
202 AssertPtrReturn(pInstance, VERR_INVALID_PARAMETER);
203 AssertPtrReturn(ppszClientPath, VERR_INVALID_PARAMETER);
204 AssertPtrReturn(pszDeviceIdent, VERR_INVALID_PARAMETER);
205
206 VBOXUSBREQ_CLIENT_INFO Req;
207 bzero(&Req, sizeof(Req));
208 RTStrPrintf(Req.szDeviceIdent, sizeof(Req.szDeviceIdent), "%s", pszDeviceIdent);
209
210 int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_CLIENT_INFO, &Req, sizeof(Req));
211 if (RT_SUCCESS(rc))
212 {
213 *pInstance = Req.Instance;
214 rc = RTStrDupEx(ppszClientPath, Req.szClientPath);
215 if (RT_SUCCESS(rc))
216 return VINF_SUCCESS;
217
218 LogRel((USBLIBR3 ":USBLibGetClientInfo RTStrDupEx failed! rc=%Rrc szClientPath=%s\n", rc, Req.szClientPath));
219 }
220 else
221 LogRel((USBLIBR3 ":USBLibGetClientInfo VBOXUSBMON_IOCTL_CLIENTPATH failed! rc=%Rrc\n", rc));
222
223 return rc;
224}
225
226
227USBLIB_DECL(int) USBLibResetDevice(char *pszDevicePath, bool fReattach)
228{
229 LogFlow((USBLIBR3 ":USBLibResetDevice pszDevicePath=%s\n", pszDevicePath));
230
231 size_t cbPath = strlen(pszDevicePath) + 1;
232 size_t cbReq = sizeof(VBOXUSBREQ_RESET_DEVICE) + cbPath;
233 VBOXUSBREQ_RESET_DEVICE *pReq = (VBOXUSBREQ_RESET_DEVICE *)RTMemTmpAllocZ(cbReq);
234 if (RT_UNLIKELY(!pReq))
235 return VERR_NO_MEMORY;
236
237 pReq->fReattach = fReattach;
238 if (strlcpy(pReq->szDevicePath, pszDevicePath, cbPath) >= cbPath)
239 {
240 LogRel((USBLIBR3 ":USBLibResetDevice buffer overflow. cbPath=%u pszDevicePath=%s\n", cbPath, pszDevicePath));
241 return VERR_BUFFER_OVERFLOW;
242 }
243
244 int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_RESET_DEVICE, pReq, cbReq);
245 if (RT_FAILURE(rc))
246 LogRel((USBLIBR3 ":VBOXUSBMON_IOCTL_RESET_DEVICE failed! rc=%Rrc\n", rc));
247
248 RTMemFree(pReq);
249 return rc;
250}
251
252
253static int usblibDoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
254{
255 if (g_hFile == NIL_RTFILE)
256 {
257 LogRel((USBLIBR3 ":IOCtl failed, device not open.\n"));
258 return VERR_FILE_NOT_FOUND;
259 }
260
261 VBOXUSBREQ Hdr;
262 Hdr.u32Magic = VBOXUSBMON_MAGIC;
263 Hdr.cbData = cbData; /* Don't include full size because the header size is fixed. */
264 Hdr.pvDataR3 = pvData;
265
266 int rc = ioctl(RTFileToNative(g_hFile), iFunction, &Hdr);
267 if (rc < 0)
268 {
269 rc = errno;
270 LogRel((USBLIBR3 ":IOCtl failed iFunction=%x errno=%d g_file=%d\n", iFunction, rc, RTFileToNative(g_hFile)));
271 return RTErrConvertFromErrno(rc);
272 }
273
274 rc = Hdr.rc;
275 if (RT_UNLIKELY(RT_FAILURE(rc)))
276 LogRel((USBLIBR3 ":Function (%x) failed. rc=%Rrc\n", iFunction, rc));
277
278 return rc;
279}
Note: See TracBrowser for help on using the repository browser.

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