VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-netbsd.c@ 70059

Last change on this file since 70059 was 70059, checked in by vboxsync, 7 years ago

VBoxGuest-netbsd.c: oops, fix doxygen fallout from the renaming.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.2 KB
Line 
1/* $Id: VBoxGuest-netbsd.c 70059 2017-12-11 15:11:15Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions Driver for NetBSD.
4 */
5
6/*
7 * Copyright (C) 2007-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/select.h>
34#include <sys/conf.h>
35#include <sys/kernel.h>
36#include <sys/kmem.h>
37#include <sys/module.h>
38#include <sys/device.h>
39#include <sys/bus.h>
40#include <sys/poll.h>
41#include <sys/proc.h>
42#include <sys/stat.h>
43#include <sys/selinfo.h>
44#include <sys/queue.h>
45#include <sys/lock.h>
46#include <sys/types.h>
47#include <sys/conf.h>
48#include <sys/malloc.h>
49#include <sys/uio.h>
50#include <sys/file.h>
51#include <sys/filedesc.h>
52#include <sys/vfs_syscalls.h>
53#include <dev/pci/pcivar.h>
54#include <dev/pci/pcireg.h>
55#include <dev/pci/pcidevs.h>
56
57#include <dev/wscons/wsconsio.h>
58#include <dev/wscons/wsmousevar.h>
59#include <dev/wscons/tpcalibvar.h>
60
61#ifdef PVM
62# undef PVM
63#endif
64#include "VBoxGuestInternal.h"
65#include <VBox/log.h>
66#include <iprt/assert.h>
67#include <iprt/initterm.h>
68#include <iprt/process.h>
69#include <iprt/mem.h>
70#include <iprt/asm.h>
71
72
73/*********************************************************************************************************************************
74* Defined Constants And Macros *
75*********************************************************************************************************************************/
76/** The module name. */
77#define DEVICE_NAME "vboxguest"
78
79
80/*********************************************************************************************************************************
81* Structures and Typedefs *
82*********************************************************************************************************************************/
83typedef struct VBoxGuestDeviceState
84{
85 device_t sc_dev;
86 pci_chipset_tag_t sc_pc;
87
88 bus_space_tag_t sc_iot;
89 bus_space_handle_t sc_ioh;
90 bus_addr_t sc_iobase;
91 bus_size_t sc_iosize;
92
93 bus_space_tag_t sc_memt;
94 bus_space_handle_t sc_memh;
95
96 /** Size of the memory area. */
97 bus_size_t sc_memsize;
98
99 /** IRQ resource handle. */
100 pci_intr_handle_t ih;
101 /** Pointer to the IRQ handler. */
102 void *pfnIrqHandler;
103
104 /** Controller features, limits and status. */
105 u_int vboxguest_state;
106
107 device_t sc_wsmousedev;
108 VMMDevReqMouseStatus *sc_vmmmousereq;
109 PVBOXGUESTSESSION sc_session;
110 struct tpcalib_softc sc_tpcalib;
111} vboxguest_softc;
112
113
114struct vboxguest_fdata
115{
116 vboxguest_softc *sc;
117 PVBOXGUESTSESSION session;
118};
119
120#define VBOXGUEST_STATE_INITOK 1 << 0
121
122
123/*********************************************************************************************************************************
124* Internal Functions *
125*********************************************************************************************************************************/
126/*
127 * Driver(9) autoconf machinery.
128 */
129static int VBoxGuestNetBSDMatch(device_t parent, cfdata_t match, void *aux);
130static void VBoxGuestNetBSDAttach(device_t parent, device_t self, void *aux);
131static void VBoxGuestNetBSDWsmAttach(vboxguest_softc *sc);
132static int VBoxGuestNetBSDDetach(device_t self, int flags);
133
134/*
135 * IRQ related functions.
136 */
137static int VBoxGuestNetBSDAddIRQ(vboxguest_softc *sc, struct pci_attach_args *pa);
138static void VBoxGuestNetBSDRemoveIRQ(vboxguest_softc *sc);
139static int VBoxGuestNetBSDISR(void *pvState);
140
141/*
142 * Character device file handlers.
143 */
144static int VBoxGuestNetBSDOpen(dev_t device, int flags, int fmt, struct lwp *process);
145static int VBoxGuestNetBSDClose(struct file *fp);
146static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long cmd, void *addr);
147static int VBoxGuestNetBSDIOCtlSlow(struct vboxguest_fdata *fdata, u_long command, void *data);
148static int VBoxGuestNetBSDPoll(struct file *fp, int events);
149
150/*
151 * wsmouse(4) accessops
152 */
153static int VBoxGuestNetBSDWsmEnable(void *cookie);
154static void VBoxGuestNetBSDWsmDisable(void *cookie);
155static int VBoxGuestNetBSDWsmIOCtl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l);
156
157static int VBoxGuestNetBSDSetMouseStatus(vboxguest_softc *sc, uint32_t fStatus);
158
159
160/*********************************************************************************************************************************
161* Global Variables *
162*********************************************************************************************************************************/
163extern struct cfdriver vboxguest_cd; /* CFDRIVER_DECL */
164extern struct cfattach vboxguest_ca; /* CFATTACH_DECL */
165
166/*
167 * The /dev/vboxguest character device entry points.
168 */
169static struct cdevsw g_VBoxGuestNetBSDChrDevSW =
170{
171 VBoxGuestNetBSDOpen,
172 noclose,
173 noread,
174 nowrite,
175 noioctl,
176 nostop,
177 notty,
178 nopoll,
179 nommap,
180 nokqfilter,
181};
182
183static const struct fileops vboxguest_fileops = {
184 .fo_read = fbadop_read,
185 .fo_write = fbadop_write,
186 .fo_ioctl = VBoxGuestNetBSDIOCtl,
187 .fo_fcntl = fnullop_fcntl,
188 .fo_poll = VBoxGuestNetBSDPoll,
189 .fo_stat = fbadop_stat,
190 .fo_close = VBoxGuestNetBSDClose,
191 .fo_kqfilter = fnullop_kqfilter,
192 .fo_restart = fnullop_restart
193};
194
195
196const struct wsmouse_accessops vboxguest_wsm_accessops = {
197 VBoxGuestNetBSDWsmEnable,
198 VBoxGuestNetBSDWsmIOCtl,
199 VBoxGuestNetBSDWsmDisable,
200};
201
202
203static struct wsmouse_calibcoords vboxguest_wsm_default_calib = {
204 .minx = VMMDEV_MOUSE_RANGE_MIN,
205 .miny = VMMDEV_MOUSE_RANGE_MIN,
206 .maxx = VMMDEV_MOUSE_RANGE_MAX,
207 .maxy = VMMDEV_MOUSE_RANGE_MAX,
208 .samplelen = WSMOUSE_CALIBCOORDS_RESET,
209};
210
211/** Device extention & session data association structure. */
212static VBOXGUESTDEVEXT g_DevExt;
213
214static vboxguest_softc *g_SC;
215
216/** Reference counter */
217static volatile uint32_t cUsers;
218/** selinfo structure used for polling. */
219static struct selinfo g_SelInfo;
220
221
222CFATTACH_DECL_NEW(vboxguest, sizeof(vboxguest_softc),
223 VBoxGuestNetBSDMatch, VBoxGuestNetBSDAttach, VBoxGuestNetBSDDetach, NULL);
224
225
226static int VBoxGuestNetBSDMatch(device_t parent, cfdata_t match, void *aux)
227{
228 const struct pci_attach_args *pa = aux;
229
230 if (RT_UNLIKELY(g_SC != NULL)) /* should not happen */
231 return 0;
232
233 if ( PCI_VENDOR(pa->pa_id) == VMMDEV_VENDORID
234 && PCI_PRODUCT(pa->pa_id) == VMMDEV_DEVICEID)
235 {
236 return 1;
237 }
238
239 return 0;
240}
241
242
243static void VBoxGuestNetBSDAttach(device_t parent, device_t self, void *aux)
244{
245 int rc = VINF_SUCCESS;
246 int iResId = 0;
247 vboxguest_softc *sc;
248 struct pci_attach_args *pa = aux;
249 bus_space_tag_t iot, memt;
250 bus_space_handle_t ioh, memh;
251 bus_dma_segment_t seg;
252 int ioh_valid, memh_valid;
253
254 KASSERT(g_SC == NULL);
255
256 cUsers = 0;
257
258 aprint_normal(": VirtualBox Guest\n");
259
260 sc = device_private(self);
261 sc->sc_dev = self;
262
263 /*
264 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
265 */
266 rc = RTR0Init(0);
267 if (RT_FAILURE(rc))
268 {
269 LogFunc(("RTR0Init failed.\n"));
270 aprint_error_dev(sc->sc_dev, "RTR0Init failed\n");
271 return;
272 }
273
274 sc->sc_pc = pa->pa_pc;
275
276 /*
277 * Allocate I/O port resource.
278 */
279 ioh_valid = (pci_mapreg_map(pa, PCI_BAR0,
280 PCI_MAPREG_TYPE_IO, 0,
281 &sc->sc_iot, &sc->sc_ioh,
282 &sc->sc_iobase, &sc->sc_iosize) == 0);
283
284 if (ioh_valid)
285 {
286
287 /*
288 * Map the MMIO region.
289 */
290 memh_valid = (pci_mapreg_map(pa, PCI_BAR1,
291 PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR,
292 &sc->sc_memt, &sc->sc_memh,
293 NULL, &sc->sc_memsize) == 0);
294 if (memh_valid)
295 {
296 /*
297 * Call the common device extension initializer.
298 */
299 rc = VGDrvCommonInitDevExt(&g_DevExt, sc->sc_iobase,
300 bus_space_vaddr(sc->sc_memt, sc->sc_memh),
301 sc->sc_memsize,
302#if ARCH_BITS == 64
303 VBOXOSTYPE_NetBSD_x64,
304#else
305 VBOXOSTYPE_NetBSD,
306#endif
307 VMMDEV_EVENT_MOUSE_POSITION_CHANGED);
308 if (RT_SUCCESS(rc))
309 {
310 /*
311 * Add IRQ of VMMDev.
312 */
313 rc = VBoxGuestNetBSDAddIRQ(sc, pa);
314 if (RT_SUCCESS(rc))
315 {
316 sc->vboxguest_state |= VBOXGUEST_STATE_INITOK;
317 VBoxGuestNetBSDWsmAttach(sc);
318
319 g_SC = sc;
320 return;
321 }
322 VGDrvCommonDeleteDevExt(&g_DevExt);
323 }
324 else
325 {
326 aprint_error_dev(sc->sc_dev, "init failed\n");
327 }
328 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
329 }
330 else
331 {
332 aprint_error_dev(sc->sc_dev, "MMIO mapping failed\n");
333 }
334 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
335 }
336 else
337 {
338 aprint_error_dev(sc->sc_dev, "IO mapping failed\n");
339 }
340
341 RTR0Term();
342 return;
343}
344
345
346/**
347 * Sets IRQ for VMMDev.
348 *
349 * @returns NetBSD error code.
350 * @param sc Pointer to the state info structure.
351 * @param pa Pointer to the PCI attach arguments.
352 */
353static int VBoxGuestNetBSDAddIRQ(vboxguest_softc *sc, struct pci_attach_args *pa)
354{
355 int iResId = 0;
356 int rc = 0;
357 const char *intrstr;
358#if __NetBSD_Prereq__(6, 99, 39)
359 char intstrbuf[100];
360#endif
361
362 LogFlow((DEVICE_NAME ": %s\n", __func__));
363
364 if (pci_intr_map(pa, &sc->ih))
365 {
366 aprint_error_dev(sc->sc_dev, "couldn't map interrupt.\n");
367 return VERR_DEV_IO_ERROR;
368 }
369
370 intrstr = pci_intr_string(sc->sc_pc, sc->ih
371#if __NetBSD_Prereq__(6, 99, 39)
372 , intstrbuf, sizeof(intstrbuf)
373#endif
374 );
375 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
376
377 sc->pfnIrqHandler = pci_intr_establish(sc->sc_pc, sc->ih, IPL_BIO, VBoxGuestNetBSDISR, sc);
378 if (sc->pfnIrqHandler == NULL)
379 {
380 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
381 return VERR_DEV_IO_ERROR;
382 }
383
384 return VINF_SUCCESS;
385}
386
387
388/*
389 * Optionally attach wsmouse(4) device as a child.
390 */
391static void VBoxGuestNetBSDWsmAttach(vboxguest_softc *sc)
392{
393 struct wsmousedev_attach_args am = { &vboxguest_wsm_accessops, sc };
394
395 PVBOXGUESTSESSION session = NULL;
396 VMMDevReqMouseStatus *req = NULL;
397 int rc;
398
399 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &session);
400 if (RT_FAILURE(rc))
401 goto fail;
402
403 rc = VbglR0GRAlloc((VMMDevRequestHeader **)&req, sizeof(*req),
404 VMMDevReq_GetMouseStatus);
405 if (RT_FAILURE(rc))
406 goto fail;
407
408 sc->sc_wsmousedev = config_found_ia(sc->sc_dev, "wsmousedev", &am, wsmousedevprint);
409 if (sc->sc_wsmousedev == NULL)
410 goto fail;
411
412 sc->sc_session = session;
413 sc->sc_vmmmousereq = req;
414
415 tpcalib_init(&sc->sc_tpcalib);
416 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
417 &vboxguest_wsm_default_calib, 0, 0);
418 return;
419
420 fail:
421 if (session != NULL)
422 VGDrvCommonCloseSession(&g_DevExt, session);
423 if (req != NULL)
424 VbglR0GRFree((VMMDevRequestHeader *)req);
425}
426
427
428static int VBoxGuestNetBSDDetach(device_t self, int flags)
429{
430 vboxguest_softc *sc;
431 sc = device_private(self);
432
433 LogFlow((DEVICE_NAME ": %s\n", __func__));
434
435 if (cUsers > 0)
436 return EBUSY;
437
438 if ((sc->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0)
439 return 0;
440
441 /*
442 * Reverse what we did in VBoxGuestNetBSDAttach.
443 */
444 if (sc->sc_vmmmousereq != NULL)
445 VbglR0GRFree((VMMDevRequestHeader *)sc->sc_vmmmousereq);
446
447 VBoxGuestNetBSDRemoveIRQ(sc);
448
449 VGDrvCommonDeleteDevExt(&g_DevExt);
450
451 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
452 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
453
454 RTR0Term();
455
456 return config_detach_children(self, flags);
457}
458
459
460/**
461 * Removes IRQ for VMMDev.
462 *
463 * @param vboxguest Opaque pointer to the state info structure.
464 */
465static void VBoxGuestNetBSDRemoveIRQ(vboxguest_softc *sc)
466{
467 LogFlow((DEVICE_NAME ": %s\n", __func__));
468
469 if (sc->pfnIrqHandler)
470 {
471 pci_intr_disestablish(sc->sc_pc, sc->pfnIrqHandler);
472 }
473}
474
475
476/**
477 * Interrupt service routine.
478 *
479 * @returns Whether the interrupt was from VMMDev.
480 * @param pvState Opaque pointer to the device state.
481 */
482static int VBoxGuestNetBSDISR(void *pvState)
483{
484 LogFlow((DEVICE_NAME ": %s: pvState=%p\n", __func__, pvState));
485
486 bool fOurIRQ = VGDrvCommonISR(&g_DevExt);
487
488 return fOurIRQ ? 0 : 1;
489}
490
491
492/*
493 * Called by VGDrvCommonISR() if mouse position changed
494 */
495void VGDrvNativeISRMousePollEvent(PVBOXGUESTDEVEXT pDevExt)
496{
497 vboxguest_softc *sc = g_SC;
498
499 LogFlow((DEVICE_NAME ": %s\n", __func__));
500
501 /*
502 * Wake up poll waiters.
503 */
504 selnotify(&g_SelInfo, 0, 0);
505
506 if (sc->sc_vmmmousereq != NULL) {
507 int x, y;
508 int rc;
509
510 sc->sc_vmmmousereq->mouseFeatures = 0;
511 sc->sc_vmmmousereq->pointerXPos = 0;
512 sc->sc_vmmmousereq->pointerYPos = 0;
513
514 rc = VbglR0GRPerform(&sc->sc_vmmmousereq->header);
515 if (RT_FAILURE(rc))
516 return;
517
518 tpcalib_trans(&sc->sc_tpcalib,
519 sc->sc_vmmmousereq->pointerXPos,
520 sc->sc_vmmmousereq->pointerYPos,
521 &x, &y);
522
523 wsmouse_input(sc->sc_wsmousedev,
524 0, /* buttons */
525 x, y,
526 0, 0, /* z, w */
527 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
528 }
529}
530
531
532static int VBoxGuestNetBSDSetMouseStatus(vboxguest_softc *sc, uint32_t fStatus)
533{
534 VBGLIOCSETMOUSESTATUS Req;
535 int rc;
536
537 VBGLREQHDR_INIT(&Req.Hdr, SET_MOUSE_STATUS);
538 Req.u.In.fStatus = fStatus;
539 rc = VGDrvCommonIoCtl(VBGL_IOCTL_SET_MOUSE_STATUS,
540 &g_DevExt,
541 sc->sc_session,
542 &Req.Hdr, sizeof(Req));
543 if (RT_SUCCESS(rc))
544 rc = Req.Hdr.rc;
545
546 return rc;
547}
548
549
550static int
551VBoxGuestNetBSDWsmEnable(void *cookie)
552{
553 vboxguest_softc *sc = cookie;
554 int rc;
555
556 rc = VBoxGuestNetBSDSetMouseStatus(sc, VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
557 | VMMDEV_MOUSE_NEW_PROTOCOL);
558 if (RT_FAILURE(rc))
559 return RTErrConvertToErrno(rc);
560
561 return 0;
562}
563
564
565static void
566VBoxGuestNetBSDWsmDisable(void *cookie)
567{
568 vboxguest_softc *sc = cookie;
569 VBoxGuestNetBSDSetMouseStatus(sc, 0);
570}
571
572
573static int
574VBoxGuestNetBSDWsmIOCtl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
575{
576 vboxguest_softc *sc = cookie;
577
578 switch (cmd) {
579 case WSMOUSEIO_GTYPE:
580 *(u_int *)data = WSMOUSE_TYPE_TPANEL;
581 break;
582
583 case WSMOUSEIO_SCALIBCOORDS:
584 case WSMOUSEIO_GCALIBCOORDS:
585 return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
586
587 default:
588 return EPASSTHROUGH;
589 }
590 return 0;
591}
592
593
594/**
595 * File open handler
596 *
597 */
598static int VBoxGuestNetBSDOpen(dev_t device, int flags, int fmt, struct lwp *process)
599{
600 int rc;
601 vboxguest_softc *sc;
602 struct vboxguest_fdata *fdata;
603 file_t *fp;
604 int fd, error;
605
606 LogFlow((DEVICE_NAME ": %s\n", __func__));
607
608 if ((sc = device_lookup_private(&vboxguest_cd, minor(device))) == NULL)
609 {
610 printf("device_lookup_private failed\n");
611 return (ENXIO);
612 }
613
614 if ((sc->vboxguest_state & VBOXGUEST_STATE_INITOK) == 0)
615 {
616 aprint_error_dev(sc->sc_dev, "device not configured\n");
617 return (ENXIO);
618 }
619
620 fdata = kmem_alloc(sizeof(*fdata), KM_SLEEP);
621 if (fdata == NULL)
622 {
623 return (ENOMEM);
624 }
625
626 fdata->sc = sc;
627
628 if ((error = fd_allocfile(&fp, &fd)) != 0)
629 {
630 kmem_free(fdata, sizeof(*fdata));
631 return error;
632 }
633
634 /*
635 * Create a new session.
636 */
637 rc = VGDrvCommonCreateUserSession(&g_DevExt, &fdata->session);
638 if (! RT_SUCCESS(rc))
639 {
640 aprint_error_dev(sc->sc_dev, "VBox session creation failed\n");
641 closef(fp); /* ??? */
642 kmem_free(fdata, sizeof(*fdata));
643 return RTErrConvertToErrno(rc);
644 }
645 ASMAtomicIncU32(&cUsers);
646 return fd_clone(fp, fd, flags, &vboxguest_fileops, fdata);
647
648}
649
650/**
651 * File close handler
652 *
653 */
654static int VBoxGuestNetBSDClose(struct file *fp)
655{
656 struct vboxguest_fdata *fdata = fp->f_data;
657 vboxguest_softc *sc = fdata->sc;
658
659 LogFlow((DEVICE_NAME ": %s\n", __func__));
660
661 VGDrvCommonCloseSession(&g_DevExt, fdata->session);
662 ASMAtomicDecU32(&cUsers);
663
664 kmem_free(fdata, sizeof(*fdata));
665
666 return 0;
667}
668
669/**
670 * IOCTL handler
671 *
672 */
673static int VBoxGuestNetBSDIOCtl(struct file *fp, u_long command, void *data)
674{
675 struct vboxguest_fdata *fdata = fp->f_data;
676
677 if (VBGL_IOCTL_IS_FAST(command))
678 return VGDrvCommonIoCtlFast(command, &g_DevExt, fdata->session);
679
680 return VBoxGuestNetBSDIOCtlSlow(fdata, command, data);
681}
682
683static int VBoxGuestNetBSDIOCtlSlow(struct vboxguest_fdata *fdata, u_long command, void *data)
684{
685 vboxguest_softc *sc = fdata->sc;
686 size_t cbReq = IOCPARM_LEN(command);
687 PVBGLREQHDR pHdr = NULL;
688 void *pvUser = NULL;
689 int err, rc;
690
691 LogFlow(("%s: command=%#lx data=%p\n", __func__, command, data));
692
693 /*
694 * Buffered request?
695 */
696 if ((command & IOC_DIRMASK) == IOC_INOUT)
697 {
698 /* will be validated by VGDrvCommonIoCtl() */
699 pHdr = (PVBGLREQHDR)data;
700 }
701
702 /*
703 * Big unbuffered request? "data" is the userland pointer.
704 */
705 else if ((command & IOC_DIRMASK) == IOC_VOID && cbReq != 0)
706 {
707 /*
708 * Read the header, validate it and figure out how much that
709 * needs to be buffered.
710 */
711 VBGLREQHDR Hdr;
712
713 if (RT_UNLIKELY(cbReq < sizeof(Hdr)))
714 return ENOTTY;
715
716 pvUser = data;
717 err = copyin(pvUser, &Hdr, sizeof(Hdr));
718 if (RT_UNLIKELY(err != 0))
719 return err;
720
721 if (RT_UNLIKELY(Hdr.uVersion != VBGLREQHDR_VERSION))
722 return ENOTTY;
723
724 if (cbReq > 16 * _1M)
725 return EINVAL;
726
727 if (Hdr.cbOut == 0)
728 Hdr.cbOut = Hdr.cbIn;
729
730 if (RT_UNLIKELY( Hdr.cbIn < sizeof(Hdr) || Hdr.cbIn > cbReq
731 || Hdr.cbOut < sizeof(Hdr) || Hdr.cbOut > cbReq))
732 return EINVAL;
733
734 /*
735 * Allocate buffer and copy in the data.
736 */
737 cbReq = RT_MAX(Hdr.cbIn, Hdr.cbOut);
738
739 pHdr = (PVBGLREQHDR)RTMemTmpAlloc(cbReq);
740 if (RT_UNLIKELY(pHdr == NULL))
741 {
742 LogRel(("%s: command=%#lx data=%p: unable to allocate %zu bytes\n",
743 __func__, command, data, cbReq));
744 return ENOMEM;
745 }
746
747 err = copyin(pvUser, pHdr, Hdr.cbIn);
748 if (err != 0)
749 {
750 RTMemTmpFree(pHdr);
751 return err;
752 }
753
754 if (Hdr.cbIn < cbReq)
755 memset((uint8_t *)pHdr + Hdr.cbIn, '\0', cbReq - Hdr.cbIn);
756 }
757
758 /*
759 * Process the IOCtl.
760 */
761 rc = VGDrvCommonIoCtl(command, &g_DevExt, fdata->session, pHdr, cbReq);
762 if (RT_SUCCESS(rc))
763 {
764 err = 0;
765
766 /*
767 * If unbuffered, copy back the result before returning.
768 */
769 if (pvUser != NULL)
770 {
771 size_t cbOut = pHdr->cbOut;
772 if (cbOut > cbReq)
773 {
774 LogRel(("%s: command=%#lx data=%p: too much output: %zu > %zu\n",
775 __func__, command, data, cbOut, cbReq));
776 cbOut = cbReq;
777 }
778
779 err = copyout(pHdr, pvUser, cbOut);
780 RTMemTmpFree(pHdr);
781 }
782 }
783 else
784 {
785 LogRel(("%s: command=%#lx data=%p: error %Rrc\n",
786 __func__, command, data, rc));
787
788 if (pvUser != NULL)
789 RTMemTmpFree(pHdr);
790
791 err = RTErrConvertToErrno(rc);
792 }
793
794 return err;
795}
796
797static int VBoxGuestNetBSDPoll(struct file *fp, int events)
798{
799 struct vboxguest_fdata *fdata = fp->f_data;
800 vboxguest_softc *sc = fdata->sc;
801
802 int rc = 0;
803 int events_processed;
804
805 uint32_t u32CurSeq;
806
807 LogFlow((DEVICE_NAME ": %s\n", __func__));
808
809 u32CurSeq = ASMAtomicUoReadU32(&g_DevExt.u32MousePosChangedSeq);
810 if (fdata->session->u32MousePosChangedSeq != u32CurSeq)
811 {
812 events_processed = events & (POLLIN | POLLRDNORM);
813 fdata->session->u32MousePosChangedSeq = u32CurSeq;
814 }
815 else
816 {
817 events_processed = 0;
818
819 selrecord(curlwp, &g_SelInfo);
820 }
821
822 return events_processed;
823}
824
825
826/**
827 * @note This code is duplicated on other platforms with variations, so please
828 * keep them all up to date when making changes!
829 */
830int VBOXCALL VBoxGuestIDC(void *pvSession, uintptr_t uReq, PVBGLREQHDR pReqHdr, size_t cbReq)
831{
832 /*
833 * Simple request validation (common code does the rest).
834 */
835 int rc;
836 if ( RT_VALID_PTR(pReqHdr)
837 && cbReq >= sizeof(*pReqHdr))
838 {
839 /*
840 * All requests except the connect one requires a valid session.
841 */
842 PVBOXGUESTSESSION pSession = (PVBOXGUESTSESSION)pvSession;
843 if (pSession)
844 {
845 if ( RT_VALID_PTR(pSession)
846 && pSession->pDevExt == &g_DevExt)
847 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
848 else
849 rc = VERR_INVALID_HANDLE;
850 }
851 else if (uReq == VBGL_IOCTL_IDC_CONNECT)
852 {
853 rc = VGDrvCommonCreateKernelSession(&g_DevExt, &pSession);
854 if (RT_SUCCESS(rc))
855 {
856 rc = VGDrvCommonIoCtl(uReq, &g_DevExt, pSession, pReqHdr, cbReq);
857 if (RT_FAILURE(rc))
858 VGDrvCommonCloseSession(&g_DevExt, pSession);
859 }
860 }
861 else
862 rc = VERR_INVALID_HANDLE;
863 }
864 else
865 rc = VERR_INVALID_POINTER;
866 return rc;
867}
868
869
870MODULE(MODULE_CLASS_DRIVER, vboxguest, "pci");
871
872static const struct cfiattrdata wsmousedevcf_iattrdata = {
873 "wsmousedev", 1, {
874 { "mux", "0", 0 },
875 }
876};
877
878/* device vboxguest: wsmousedev */
879static const struct cfiattrdata * const vboxguest_attrs[] = { &wsmousedevcf_iattrdata, NULL };
880CFDRIVER_DECL(vboxguest, DV_DULL, vboxguest_attrs);
881
882static struct cfdriver * const cfdriver_ioconf_vboxguest[] = {
883 &vboxguest_cd, NULL
884};
885
886
887static const struct cfparent vboxguest_pspec = {
888 "pci", "pci", DVUNIT_ANY
889};
890static int vboxguest_loc[] = { -1, -1 };
891
892
893static const struct cfparent wsmousedev_pspec = {
894 "wsmousedev", "vboxguest", DVUNIT_ANY
895};
896static int wsmousedev_loc[] = { 0 };
897
898
899static struct cfdata cfdata_ioconf_vboxguest[] = {
900 /* vboxguest0 at pci? dev ? function ? */
901 {
902 .cf_name = "vboxguest",
903 .cf_atname = "vboxguest",
904 .cf_unit = 0, /* Only unit 0 is ever used */
905 .cf_fstate = FSTATE_NOTFOUND,
906 .cf_loc = vboxguest_loc,
907 .cf_flags = 0,
908 .cf_pspec = &vboxguest_pspec,
909 },
910
911 /* wsmouse* at vboxguest? */
912 { "wsmouse", "wsmouse", 0, FSTATE_STAR, wsmousedev_loc, 0, &wsmousedev_pspec },
913
914 { NULL, NULL, 0, 0, NULL, 0, NULL }
915};
916
917static struct cfattach * const vboxguest_cfattachinit[] = {
918 &vboxguest_ca, NULL
919};
920
921static const struct cfattachinit cfattach_ioconf_vboxguest[] = {
922 { "vboxguest", vboxguest_cfattachinit },
923 { NULL, NULL }
924};
925
926static int
927vboxguest_modcmd(modcmd_t cmd, void *opaque)
928{
929 devmajor_t bmajor, cmajor;
930 register_t retval;
931 int error;
932
933 LogFlow((DEVICE_NAME ": %s\n", __func__));
934
935 switch (cmd)
936 {
937 case MODULE_CMD_INIT:
938 error = config_init_component(cfdriver_ioconf_vboxguest,
939 cfattach_ioconf_vboxguest,
940 cfdata_ioconf_vboxguest);
941 if (error)
942 break;
943
944 bmajor = cmajor = NODEVMAJOR;
945 error = devsw_attach("vboxguest",
946 NULL, &bmajor,
947 &g_VBoxGuestNetBSDChrDevSW, &cmajor);
948 if (error)
949 {
950 if (error == EEXIST)
951 error = 0; /* maybe built-in ... improve eventually */
952 else
953 break;
954 }
955
956 error = do_sys_mknod(curlwp, "/dev/vboxguest",
957 0666|S_IFCHR, makedev(cmajor, 0),
958 &retval, UIO_SYSSPACE);
959 if (error == EEXIST)
960 error = 0;
961 break;
962
963 case MODULE_CMD_FINI:
964 error = config_fini_component(cfdriver_ioconf_vboxguest,
965 cfattach_ioconf_vboxguest,
966 cfdata_ioconf_vboxguest);
967 if (error)
968 break;
969
970 devsw_detach(NULL, &g_VBoxGuestNetBSDChrDevSW);
971 break;
972
973 default:
974 return ENOTTY;
975 }
976 return error;
977}
Note: See TracBrowser for help on using the repository browser.

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