VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxDev-haiku.c@ 58053

Last change on this file since 58053 was 58053, checked in by vboxsync, 9 years ago

VBoxGuest: s/vbgd/VGDrv/ + started on page.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/* $Id: VBoxDev-haiku.c 58053 2015-10-06 14:42:35Z vboxsync $ */
2/** @file
3 * VBoxGuest kernel driver, Haiku Guest Additions, implementation.
4 */
5
6/*
7 * Copyright (C) 2012-2015 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 * This code is based on:
20 *
21 * VirtualBox Guest Additions for Haiku.
22 * Copyright (c) 2011 Mike Smith <mike@scgtrp.net>
23 * François Revol <revol@free.fr>
24 *
25 * Permission is hereby granted, free of charge, to any person
26 * obtaining a copy of this software and associated documentation
27 * files (the "Software"), to deal in the Software without
28 * restriction, including without limitation the rights to use,
29 * copy, modify, merge, publish, distribute, sublicense, and/or sell
30 * copies of the Software, and to permit persons to whom the
31 * Software is furnished to do so, subject to the following
32 * conditions:
33 *
34 * The above copyright notice and this permission notice shall be
35 * included in all copies or substantial portions of the Software.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
38 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
39 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
40 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
41 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
42 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
43 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
44 * OTHER DEALINGS IN THE SOFTWARE.
45 */
46
47
48/*********************************************************************************************************************************
49* Header Files *
50*********************************************************************************************************************************/
51#include <sys/param.h>
52#include <sys/types.h>
53#include <sys/uio.h>
54#include <OS.h>
55#include <Drivers.h>
56#include <KernelExport.h>
57#include <PCI.h>
58
59#include "VBoxGuest-haiku.h"
60#include "VBoxGuestInternal.h"
61#include <VBox/log.h>
62#include <iprt/assert.h>
63#include <iprt/initterm.h>
64#include <iprt/process.h>
65#include <iprt/mem.h>
66#include <iprt/asm.h>
67
68#define DRIVER_NAME "vboxdev"
69#define DEVICE_NAME "misc/vboxguest"
70#define MODULE_NAME "generic/vboxguest"
71
72
73/*********************************************************************************************************************************
74* Internal Functions *
75*********************************************************************************************************************************/
76static status_t VBoxGuestHaikuOpen(const char *name, uint32 flags, void **cookie);
77static status_t VBoxGuestHaikuClose(void *cookie);
78static status_t VBoxGuestHaikuFree(void *cookie);
79static status_t VBoxGuestHaikuIOCtl(void *cookie, uint32 op, void *data, size_t len);
80static status_t VBoxGuestHaikuSelect(void *cookie, uint8 event, uint32 ref, selectsync *sync);
81static status_t VBoxGuestHaikuDeselect(void *cookie, uint8 event, selectsync *sync);
82static status_t VBoxGuestHaikuWrite(void *cookie, off_t position, const void *data, size_t *numBytes);
83static status_t VBoxGuestHaikuRead(void *cookie, off_t position, void *data, size_t *numBytes);
84
85static device_hooks g_VBoxGuestHaikuDeviceHooks =
86{
87 VBoxGuestHaikuOpen,
88 VBoxGuestHaikuClose,
89 VBoxGuestHaikuFree,
90 VBoxGuestHaikuIOCtl,
91 VBoxGuestHaikuRead,
92 VBoxGuestHaikuWrite,
93 VBoxGuestHaikuSelect,
94 VBoxGuestHaikuDeselect,
95};
96
97
98/**
99 * Driver open hook.
100 *
101 * @param name The name of the device as returned by publish_devices.
102 * @param flags Open flags.
103 * @param cookie Where to store the session pointer.
104 *
105 * @return Haiku status code.
106 */
107static status_t VBoxGuestHaikuOpen(const char *name, uint32 flags, void **cookie)
108{
109 int rc;
110 PVBOXGUESTSESSION pSession;
111
112 LogFlow((DRIVER_NAME ":VBoxGuestHaikuOpen\n"));
113
114 /*
115 * Create a new session.
116 */
117 rc = VGDrvCommonCreateUserSession(&g_DevExt, &pSession);
118 if (RT_SUCCESS(rc))
119 {
120 Log((DRIVER_NAME ":VBoxGuestHaikuOpen success: g_DevExt=%p pSession=%p rc=%d pid=%d\n",&g_DevExt, pSession, rc,(int)RTProcSelf()));
121 ASMAtomicIncU32(&cUsers);
122 *cookie = pSession;
123 return B_OK;
124 }
125
126 LogRel((DRIVER_NAME ":VBoxGuestHaikuOpen: failed. rc=%d\n", rc));
127 return RTErrConvertToErrno(rc);
128}
129
130
131/**
132 * Driver close hook.
133 * @param cookie The session.
134 *
135 * @return Haiku status code.
136 */
137static status_t VBoxGuestHaikuClose(void *cookie)
138{
139 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
140 Log(("VBoxGuestHaikuClose: pSession=%p\n", pSession));
141
142 /** @todo r=ramshankar: should we really be using the session spinlock here? */
143 RTSpinlockAcquire(g_DevExt.SessionSpinlock);
144
145 /* @todo we don't know if it belongs to this session!! */
146 if (sState.selectSync)
147 {
148 //dprintf(DRIVER_NAME "close: unblocking select %p %x\n", sState.selectSync, sState.selectEvent);
149 notify_select_event(sState.selectSync, sState.selectEvent);
150 sState.selectEvent = (uint8_t)0;
151 sState.selectRef = (uint32_t)0;
152 sState.selectSync = (void *)NULL;
153 }
154
155 RTSpinlockRelease(g_DevExt.SessionSpinlock);
156 return B_OK;
157}
158
159
160/**
161 * Driver free hook.
162 * @param cookie The session.
163 *
164 * @return Haiku status code.
165 */
166static status_t VBoxGuestHaikuFree(void *cookie)
167{
168 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
169 Log(("VBoxGuestHaikuFree: pSession=%p\n", pSession));
170
171 /*
172 * Close the session if it's still hanging on to the device...
173 */
174 if (VALID_PTR(pSession))
175 {
176 VGDrvCommonCloseSession(&g_DevExt, pSession);
177 ASMAtomicDecU32(&cUsers);
178 }
179 else
180 Log(("VBoxGuestHaikuFree: si_drv1=%p!\n", pSession));
181 return B_OK;
182}
183
184
185/**
186 * Driver IOCtl entry.
187 * @param cookie The session.
188 * @param op The operation to perform.
189 * @param data The data associated with the operation.
190 * @param len Size of the data in bytes.
191 *
192 * @return Haiku status code.
193 */
194static status_t VBoxGuestHaikuIOCtl(void *cookie, uint32 op, void *data, size_t len)
195{
196 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
197 Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl cookie=%p op=0x%08x data=%p len=%lu)\n", cookie, op, data, len));
198
199 int rc = B_OK;
200
201 /*
202 * Validate the input.
203 */
204 if (RT_UNLIKELY(!VALID_PTR(pSession)))
205 return EINVAL;
206
207 /*
208 * Validate the request wrapper.
209 */
210#if 0
211 if (IOCPARM_LEN(ulCmd) != sizeof(VBGLBIGREQ))
212 {
213 Log((DRIVER_NAME ": VBoxGuestHaikuIOCtl: bad request %lu size=%lu expected=%d\n", ulCmd, IOCPARM_LEN(ulCmd),
214 sizeof(VBGLBIGREQ)));
215 return ENOTTY;
216 }
217#endif
218
219 if (RT_UNLIKELY(len > _1M * 16))
220 {
221 dprintf(DRIVER_NAME ": VBoxGuestHaikuIOCtl: bad size %#x; pArg=%p Cmd=%lu.\n", (unsigned)len, data, op);
222 return EINVAL;
223 }
224
225 /*
226 * Read the request.
227 */
228 void *pvBuf = NULL;
229 if (RT_LIKELY(len > 0))
230 {
231 pvBuf = RTMemTmpAlloc(len);
232 if (RT_UNLIKELY(!pvBuf))
233 {
234 LogRel((DRIVER_NAME ":VBoxGuestHaikuIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", len));
235 return ENOMEM;
236 }
237
238 /** @todo r=ramshankar: replace with RTR0MemUserCopyFrom() */
239 rc = user_memcpy(pvBuf, data, len);
240 if (RT_UNLIKELY(rc < 0))
241 {
242 RTMemTmpFree(pvBuf);
243 LogRel((DRIVER_NAME ":VBoxGuestHaikuIOCtl: user_memcpy failed; pvBuf=%p data=%p op=%d. rc=%d\n", pvBuf, data, op, rc));
244 return EFAULT;
245 }
246 if (RT_UNLIKELY(!VALID_PTR(pvBuf)))
247 {
248 RTMemTmpFree(pvBuf);
249 LogRel((DRIVER_NAME ":VBoxGuestHaikuIOCtl: pvBuf invalid pointer %p\n", pvBuf));
250 return EINVAL;
251 }
252 }
253 Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl: pSession=%p pid=%d.\n", pSession,(int)RTProcSelf()));
254
255 /*
256 * Process the IOCtl.
257 */
258 size_t cbDataReturned;
259 rc = VGDrvCommonIoCtl(op, &g_DevExt, pSession, pvBuf, len, &cbDataReturned);
260 if (RT_SUCCESS(rc))
261 {
262 rc = 0;
263 if (RT_UNLIKELY(cbDataReturned > len))
264 {
265 Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl: too much output data %d expected %d\n", cbDataReturned, len));
266 cbDataReturned = len;
267 }
268 if (cbDataReturned > 0)
269 {
270 rc = user_memcpy(data, pvBuf, cbDataReturned);
271 if (RT_UNLIKELY(rc < 0))
272 {
273 Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl: user_memcpy failed; pvBuf=%p pArg=%p Cmd=%lu. rc=%d\n", pvBuf, data, op, rc));
274 rc = EFAULT;
275 }
276 }
277 }
278 else
279 {
280 Log((DRIVER_NAME ":VBoxGuestHaikuIOCtl: VbgdCommonIoCtl failed. rc=%d\n", rc));
281 rc = EFAULT;
282 }
283 RTMemTmpFree(pvBuf);
284 return rc;
285}
286
287
288/**
289 * Driver select hook.
290 *
291 * @param cookie The session.
292 * @param event The event.
293 * @param ref ???
294 * @param sync ???
295 *
296 * @return Haiku status code.
297 */
298static status_t VBoxGuestHaikuSelect(void *cookie, uint8 event, uint32 ref, selectsync *sync)
299{
300 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
301 status_t err = B_OK;
302
303 switch (event)
304 {
305 case B_SELECT_READ:
306 break;
307 default:
308 return EINVAL;
309 }
310
311 RTSpinlockAcquire(g_DevExt.SessionSpinlock);
312
313 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
314 if (pSession->u32MousePosChangedSeq != u32CurSeq)
315 {
316 pSession->u32MousePosChangedSeq = u32CurSeq;
317 notify_select_event(sync, event);
318 }
319 else if (sState.selectSync == NULL)
320 {
321 sState.selectEvent = (uint8_t)event;
322 sState.selectRef = (uint32_t)ref;
323 sState.selectSync = (void *)sync;
324 }
325 else
326 err = B_WOULD_BLOCK;
327
328 RTSpinlockRelease(g_DevExt.SessionSpinlock);
329
330 return err;
331}
332
333
334/**
335 * Driver deselect hook.
336 * @param cookie The session.
337 * @param event The event.
338 * @param sync ???
339 *
340 * @return Haiku status code.
341 */
342static status_t VBoxGuestHaikuDeselect(void *cookie, uint8 event, selectsync *sync)
343{
344 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
345 status_t err = B_OK;
346 //dprintf(DRIVER_NAME "deselect(,%d,%p)\n", event, sync);
347
348 RTSpinlockAcquire(g_DevExt.SessionSpinlock);
349
350 if (sState.selectSync == sync)
351 {
352 //dprintf(DRIVER_NAME "deselect: dropping: %p %x\n", sState.selectSync, sState.selectEvent);
353 sState.selectEvent = (uint8_t)0;
354 sState.selectRef = (uint32_t)0;
355 sState.selectSync = NULL;
356 }
357 else
358 err = B_OK;
359
360 RTSpinlockRelease(g_DevExt.SessionSpinlock);
361 return err;
362}
363
364
365/**
366 * Driver write hook.
367 * @param cookie The session.
368 * @param position The offset.
369 * @param data Pointer to the data.
370 * @param numBytes Where to store the number of bytes written.
371 *
372 * @return Haiku status code.
373 */
374static status_t VBoxGuestHaikuWrite(void *cookie, off_t position, const void *data, size_t *numBytes)
375{
376 *numBytes = 0;
377 return B_OK;
378}
379
380
381/**
382 * Driver read hook.
383 * @param cookie The session.
384 * @param position The offset.
385 * @param data Pointer to the data.
386 * @param numBytes Where to store the number of bytes read.
387 *
388 * @return Haiku status code.
389 */
390static status_t VBoxGuestHaikuRead(void *cookie, off_t position, void *data, size_t *numBytes)
391{
392 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)cookie;
393
394 if (*numBytes == 0)
395 return B_OK;
396
397 uint32_t u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
398 if (pSession->u32MousePosChangedSeq != u32CurSeq)
399 {
400 pSession->u32MousePosChangedSeq = u32CurSeq;
401 *numBytes = 1;
402 return B_OK;
403 }
404
405 *numBytes = 0;
406 return B_OK;
407}
408
409
410int32 api_version = B_CUR_DRIVER_API_VERSION;
411
412status_t init_hardware()
413{
414 return get_module(MODULE_NAME, (module_info **)&g_VBoxGuest);
415}
416
417status_t init_driver()
418{
419 return B_OK;
420}
421
422device_hooks* find_device(const char *name)
423{
424 static device_hooks g_VBoxGuestHaikuDeviceHooks =
425 {
426 VBoxGuestHaikuOpen,
427 VBoxGuestHaikuClose,
428 VBoxGuestHaikuFree,
429 VBoxGuestHaikuIOCtl,
430 VBoxGuestHaikuRead,
431 VBoxGuestHaikuWrite,
432 VBoxGuestHaikuSelect,
433 VBoxGuestHaikuDeselect
434 };
435 return &g_VBoxGuestHaikuDeviceHooks;
436}
437
438const char** publish_devices()
439{
440 static const char *devices[] = { DEVICE_NAME, NULL };
441 return devices;
442}
443
444void uninit_driver()
445{
446 put_module(MODULE_NAME);
447}
448
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