1 | /** $Id: USBLib-solaris.cpp 47496 2013-07-31 15:52:29Z 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 |
|
---|
61 |
|
---|
62 | /*******************************************************************************
|
---|
63 | * Internal Functions *
|
---|
64 | *******************************************************************************/
|
---|
65 | static int usblibDoIOCtl(unsigned iFunction, void *pvData, size_t cbData);
|
---|
66 |
|
---|
67 |
|
---|
68 | USBLIB_DECL(int) USBLibInit(void)
|
---|
69 | {
|
---|
70 | LogFlow((USBLIBR3 ":USBLibInit\n"));
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Already open?
|
---|
74 | * This isn't properly serialized, but we'll be fine with the current usage.
|
---|
75 | */
|
---|
76 | if (g_cUsers)
|
---|
77 | {
|
---|
78 | ASMAtomicIncU32(&g_cUsers);
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 |
|
---|
82 | RTFILE File;
|
---|
83 | int rc = RTFileOpen(&File, VBOXUSB_DEVICE_NAME, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
84 | if (RT_FAILURE(rc))
|
---|
85 | {
|
---|
86 | LogRel((USBLIBR3 ":failed to open the VBoxUSB monitor device node '%s' rc=%Rrc\n", VBOXUSB_DEVICE_NAME, rc));
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 | g_hFile = File;
|
---|
90 |
|
---|
91 | ASMAtomicIncU32(&g_cUsers);
|
---|
92 | /*
|
---|
93 | * Check the USBMonitor version.
|
---|
94 | */
|
---|
95 | VBOXUSBREQ_GET_VERSION Req;
|
---|
96 | bzero(&Req, sizeof(Req));
|
---|
97 | rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_GET_VERSION, &Req, sizeof(Req));
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | {
|
---|
100 | if ( Req.u32Major != VBOXUSBMON_VERSION_MAJOR
|
---|
101 | || Req.u32Minor < VBOXUSBMON_VERSION_MINOR)
|
---|
102 | {
|
---|
103 | rc = VERR_VERSION_MISMATCH;
|
---|
104 | LogRel((USBLIBR3 ":USBMonitor version mismatch! driver v%d.%d, expecting ~v%d.%d\n",
|
---|
105 | Req.u32Major, Req.u32Minor, VBOXUSBMON_VERSION_MAJOR, VBOXUSBMON_VERSION_MINOR));
|
---|
106 |
|
---|
107 | RTFileClose(File);
|
---|
108 | g_hFile = NIL_RTFILE;
|
---|
109 | ASMAtomicDecU32(&g_cUsers);
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | LogRel((USBLIBR3 ":USBMonitor driver version query failed. rc=%Rrc\n", rc));
|
---|
116 | RTFileClose(File);
|
---|
117 | g_hFile = NIL_RTFILE;
|
---|
118 | ASMAtomicDecU32(&g_cUsers);
|
---|
119 | return rc;
|
---|
120 | }
|
---|
121 |
|
---|
122 | return VINF_SUCCESS;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | USBLIB_DECL(int) USBLibTerm(void)
|
---|
127 | {
|
---|
128 | LogFlow((USBLIBR3 ":USBLibTerm\n"));
|
---|
129 |
|
---|
130 | if (!g_cUsers)
|
---|
131 | return VERR_WRONG_ORDER;
|
---|
132 | if (ASMAtomicDecU32(&g_cUsers) != 0)
|
---|
133 | return VINF_SUCCESS;
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * We're the last guy, close down the connection.
|
---|
137 | */
|
---|
138 | RTFILE File = g_hFile;
|
---|
139 | g_hFile = NIL_RTFILE;
|
---|
140 | if (File == NIL_RTFILE)
|
---|
141 | return VERR_INTERNAL_ERROR;
|
---|
142 |
|
---|
143 | int rc = RTFileClose(File);
|
---|
144 | AssertRC(rc);
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | USBLIB_DECL(void *) USBLibAddFilter(PCUSBFILTER pFilter)
|
---|
150 | {
|
---|
151 | LogFlow((USBLIBR3 ":USBLibAddFilter pFilter=%p\n", pFilter));
|
---|
152 |
|
---|
153 | VBOXUSBREQ_ADD_FILTER Req;
|
---|
154 | Req.Filter = *pFilter;
|
---|
155 | Req.uId = 0;
|
---|
156 |
|
---|
157 | int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_ADD_FILTER, &Req, sizeof(Req));
|
---|
158 | if (RT_SUCCESS(rc))
|
---|
159 | return (void *)Req.uId;
|
---|
160 |
|
---|
161 | AssertMsgFailed((USBLIBR3 ":VBOXUSBMON_IOCTL_ADD_FILTER failed! rc=%Rrc\n", rc));
|
---|
162 | return NULL;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | USBLIB_DECL(void) USBLibRemoveFilter(void *pvId)
|
---|
167 | {
|
---|
168 | LogFlow((USBLIBR3 ":USBLibRemoveFilter pvId=%p\n", pvId));
|
---|
169 |
|
---|
170 | VBOXUSBREQ_REMOVE_FILTER Req;
|
---|
171 | Req.uId = (uintptr_t)pvId;
|
---|
172 |
|
---|
173 | int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_REMOVE_FILTER, &Req, sizeof(Req));
|
---|
174 | if (RT_SUCCESS(rc))
|
---|
175 | return;
|
---|
176 |
|
---|
177 | AssertMsgFailed((USBLIBR3 ":VBOXUSBMON_IOCTL_REMOVE_FILTER failed! rc=%Rrc\n", rc));
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | USBLIB_DECL(int) USBLibGetClientInfo(char *pszDeviceIdent, char **ppszClientPath, int *pInstance)
|
---|
182 | {
|
---|
183 | LogFlow((USBLIBR3 ":USBLibGetClientInfo pszDeviceIdent=%s ppszClientPath=%p pInstance=%p\n",
|
---|
184 | pszDeviceIdent, ppszClientPath, pInstance));
|
---|
185 |
|
---|
186 | AssertPtrReturn(pInstance, VERR_INVALID_PARAMETER);
|
---|
187 | AssertPtrReturn(ppszClientPath, VERR_INVALID_PARAMETER);
|
---|
188 | AssertPtrReturn(pszDeviceIdent, VERR_INVALID_PARAMETER);
|
---|
189 |
|
---|
190 | VBOXUSBREQ_CLIENT_INFO Req;
|
---|
191 | bzero(&Req, sizeof(Req));
|
---|
192 | RTStrPrintf(Req.szDeviceIdent, sizeof(Req.szDeviceIdent), "%s", pszDeviceIdent);
|
---|
193 |
|
---|
194 | int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_CLIENT_INFO, &Req, sizeof(Req));
|
---|
195 | if (RT_SUCCESS(rc))
|
---|
196 | {
|
---|
197 | *pInstance = Req.Instance;
|
---|
198 | rc = RTStrDupEx(ppszClientPath, Req.szClientPath);
|
---|
199 | if (RT_SUCCESS(rc))
|
---|
200 | return VINF_SUCCESS;
|
---|
201 |
|
---|
202 | LogRel((USBLIBR3 ":USBLibGetClientInfo RTStrDupEx failed! rc=%Rrc szClientPath=%s\n", rc, Req.szClientPath));
|
---|
203 | }
|
---|
204 | else
|
---|
205 | LogRel((USBLIBR3 ":USBLibGetClientInfo VBOXUSBMON_IOCTL_CLIENTPATH failed! rc=%Rrc\n", rc));
|
---|
206 |
|
---|
207 | return rc;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | USBLIB_DECL(int) USBLibResetDevice(char *pszDevicePath, bool fReattach)
|
---|
212 | {
|
---|
213 | LogFlow((USBLIBR3 ":USBLibResetDevice pszDevicePath=%s\n", pszDevicePath));
|
---|
214 |
|
---|
215 | size_t cbReq = sizeof(VBOXUSBREQ_RESET_DEVICE) + strlen(pszDevicePath);
|
---|
216 | VBOXUSBREQ_RESET_DEVICE *pReq = (VBOXUSBREQ_RESET_DEVICE *)RTMemTmpAllocZ(cbReq);
|
---|
217 | if (RT_UNLIKELY(!pReq))
|
---|
218 | return VERR_NO_MEMORY;
|
---|
219 |
|
---|
220 | pReq->fReattach = fReattach;
|
---|
221 | strcpy(pReq->szDevicePath, pszDevicePath);
|
---|
222 |
|
---|
223 | int rc = usblibDoIOCtl(VBOXUSBMON_IOCTL_RESET_DEVICE, pReq, cbReq);
|
---|
224 | if (RT_FAILURE(rc))
|
---|
225 | LogRel((USBLIBR3 ":VBOXUSBMON_IOCTL_RESET_DEVICE failed! rc=%Rrc\n", rc));
|
---|
226 |
|
---|
227 | RTMemFree(pReq);
|
---|
228 | return rc;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | static int usblibDoIOCtl(unsigned iFunction, void *pvData, size_t cbData)
|
---|
233 | {
|
---|
234 | if (g_hFile == NIL_RTFILE)
|
---|
235 | {
|
---|
236 | LogRel((USBLIBR3 ":IOCtl failed, device not open.\n"));
|
---|
237 | return VERR_FILE_NOT_FOUND;
|
---|
238 | }
|
---|
239 |
|
---|
240 | VBOXUSBREQ Hdr;
|
---|
241 | Hdr.u32Magic = VBOXUSBMON_MAGIC;
|
---|
242 | Hdr.cbData = cbData; /* Don't include full size because the header size is fixed. */
|
---|
243 | Hdr.pvDataR3 = pvData;
|
---|
244 |
|
---|
245 | int rc = ioctl(RTFileToNative(g_hFile), iFunction, &Hdr);
|
---|
246 | if (rc < 0)
|
---|
247 | {
|
---|
248 | rc = errno;
|
---|
249 | LogRel((USBLIBR3 ":IOCtl failed iFunction=%x errno=%d g_file=%d\n", iFunction, rc, RTFileToNative(g_hFile)));
|
---|
250 | return RTErrConvertFromErrno(rc);
|
---|
251 | }
|
---|
252 |
|
---|
253 | rc = Hdr.rc;
|
---|
254 | if (RT_UNLIKELY(RT_FAILURE(rc)))
|
---|
255 | LogRel((USBLIBR3 ":Function (%x) failed. rc=%Rrc\n", iFunction, rc));
|
---|
256 |
|
---|
257 | return rc;
|
---|
258 | }
|
---|